|
19 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
20 | 20 | */
|
21 | 21 |
|
22 |
| -#include "wolfip_freertos.h" |
23 | 22 | #include <stdio.h>
|
24 | 23 | #include <string.h>
|
25 |
| -#include <unistd.h> |
26 |
| -#include <fcntl.h> |
27 |
| -#include <sys/ioctl.h> |
28 |
| -#include <linux/if.h> |
29 |
| -#include <linux/if_tun.h> |
30 | 24 | #include <arpa/inet.h>
|
31 |
| -#include <sys/time.h> |
32 |
| -#include <poll.h> |
33 |
| -#include <sys/socket.h> |
34 |
| -#include <sys/random.h> |
35 |
| -#include <errno.h> |
36 |
| - |
37 |
| -/* Implementation of wolfIP's required random number generator */ |
38 |
| -uint32_t wolfIP_getrandom(void) { |
39 |
| - uint32_t ret; |
40 |
| - getrandom(&ret, sizeof(ret), 0); |
41 |
| - return ret; |
42 |
| -} |
43 |
| - |
44 |
| -struct wolfIP *g_wolfip = NULL; |
45 |
| -static TaskHandle_t g_network_task = NULL; |
46 |
| -static int tap_fd = -1; |
47 |
| - |
48 |
| -/* TUN/TAP device functions */ |
49 |
| -static int tap_init(struct ll *dev, const char *ifname) { |
50 |
| - struct ifreq ifr; |
51 |
| - int sock_fd; |
52 |
| - |
53 |
| - if ((tap_fd = open("/dev/net/tun", O_RDWR)) < 0) { |
54 |
| - perror("Error opening /dev/net/tun"); |
55 |
| - return -1; |
56 |
| - } |
57 |
| - |
58 |
| - memset(&ifr, 0, sizeof(ifr)); |
59 |
| - ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
60 |
| - strncpy(ifr.ifr_name, ifname, IFNAMSIZ); |
61 |
| - |
62 |
| - if (ioctl(tap_fd, TUNSETIFF, (void *)&ifr) < 0) { |
63 |
| - perror("ioctl TUNSETIFF"); |
64 |
| - close(tap_fd); |
65 |
| - return -1; |
66 |
| - } |
67 |
| - |
68 |
| - /* Get MAC address */ |
69 |
| - if (ioctl(tap_fd, SIOCGIFHWADDR, &ifr) < 0) { |
70 |
| - perror("ioctl SIOCGIFHWADDR"); |
71 |
| - close(tap_fd); |
72 |
| - return -1; |
73 |
| - } |
74 |
| - |
75 |
| - strncpy(dev->ifname, ifname, sizeof(dev->ifname) - 1); |
76 |
| - memcpy(dev->mac, ifr.ifr_hwaddr.sa_data, 6); |
77 |
| - dev->mac[5] ^= 1; /* Make MAC unique */ |
| 25 | +#include "wolfip_freertos.h" |
| 26 | +#include "../../../wolfip/wolfip.h" |
78 | 27 |
|
79 |
| - /* Configure network interface */ |
80 |
| - sock_fd = socket(AF_INET, SOCK_DGRAM, 0); |
81 |
| - if (sock_fd < 0) { |
82 |
| - perror("socket"); |
83 |
| - close(tap_fd); |
84 |
| - return -1; |
85 |
| - } |
| 28 | +/* External functions from linux_tap.c */ |
| 29 | +extern int tap_init(struct ll *ll, const char *ifname, uint32_t host_ip); |
86 | 30 |
|
87 |
| - /* Set interface UP */ |
88 |
| - if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0) { |
89 |
| - perror("ioctl SIOCGIFFLAGS"); |
90 |
| - close(sock_fd); |
91 |
| - return -1; |
92 |
| - } |
93 |
| - ifr.ifr_flags |= IFF_UP | IFF_RUNNING; |
94 |
| - if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) < 0) { |
95 |
| - perror("ioctl SIOCSIFFLAGS"); |
96 |
| - close(sock_fd); |
97 |
| - return -1; |
98 |
| - } |
| 31 | +static struct wolfIP *g_ipstack = NULL; |
99 | 32 |
|
100 |
| - close(sock_fd); |
101 |
| - return 0; |
102 |
| -} |
103 |
| - |
104 |
| -static int tap_poll(struct ll *ll, void *buf, uint32_t len) { |
105 |
| - struct pollfd pfd; |
| 33 | +int wolfip_init(void) |
| 34 | +{ |
| 35 | + struct wolfIP *ipstack; |
| 36 | + ip4 ip, netmask, gateway; |
| 37 | + struct ll *dev; |
106 | 38 | int ret;
|
107 | 39 |
|
108 |
| - pfd.fd = tap_fd; |
109 |
| - pfd.events = POLLIN; |
110 |
| - |
111 |
| - do { |
112 |
| - ret = poll(&pfd, 1, 1); /* Short timeout */ |
113 |
| - } while (ret < 0 && errno == EINTR); |
114 |
| - |
115 |
| - if (ret < 0) { |
116 |
| - perror("poll"); |
117 |
| - return -1; |
118 |
| - } |
119 |
| - if (ret == 0) { |
120 |
| - return 0; |
121 |
| - } |
122 |
| - |
123 |
| - do { |
124 |
| - ret = read(tap_fd, buf, len); |
125 |
| - } while (ret < 0 && errno == EINTR); |
126 |
| - |
127 |
| - return ret; |
128 |
| -} |
129 |
| - |
130 |
| -static int tap_send(struct ll *ll, void *buf, uint32_t len) { |
131 |
| - return write(tap_fd, buf, len); |
132 |
| -} |
133 |
| - |
134 |
| -/* Network task implementation */ |
135 |
| -static void wolfIP_NetworkTask(void *pvParameters) { |
136 |
| - TickType_t last_wake_time; |
137 |
| - const TickType_t frequency = pdMS_TO_TICKS(WOLFIP_POLL_INTERVAL_MS); |
138 |
| - struct timeval tv; |
139 |
| - |
140 |
| - last_wake_time = xTaskGetTickCount(); |
141 |
| - |
142 |
| - while (1) { |
143 |
| - gettimeofday(&tv, NULL); |
144 |
| - wolfIP_poll(g_wolfip, tv.tv_sec * 1000 + tv.tv_usec / 1000); |
145 |
| - vTaskDelayUntil(&last_wake_time, frequency); |
146 |
| - } |
147 |
| -} |
148 |
| - |
149 |
| -int wolfIP_FreeRTOS_Init(void) { |
150 |
| - struct ll *tapdev; |
151 |
| - |
152 | 40 | /* Initialize wolfIP */
|
153 |
| - wolfIP_init_static(&g_wolfip); |
154 |
| - if (!g_wolfip) { |
| 41 | + wolfIP_init_static(&ipstack); |
| 42 | + if (!ipstack) { |
155 | 43 | printf("Failed to initialize wolfIP\n");
|
156 | 44 | return -1;
|
157 | 45 | }
|
158 |
| - |
159 |
| - /* Setup TUN/TAP interface */ |
160 |
| - tapdev = wolfIP_getdev(g_wolfip); |
161 |
| - if (!tapdev) { |
162 |
| - printf("Failed to get device from wolfIP\n"); |
| 46 | + |
| 47 | + /* Configure IP settings */ |
| 48 | + ip = htonl(0x0A0A000A); /* 10.10.0.10 */ |
| 49 | + netmask = htonl(0xFFFFFF00); /* 255.255.255.0 */ |
| 50 | + gateway = htonl(0x0A0A0001); /* 10.10.0.1 */ |
| 51 | + |
| 52 | + /* Get device interface */ |
| 53 | + dev = wolfIP_getdev(ipstack); |
| 54 | + if (!dev) { |
| 55 | + printf("Failed to get device interface\n"); |
163 | 56 | return -1;
|
164 | 57 | }
|
165 | 58 |
|
166 |
| - printf("Initializing TAP device wtap0...\n"); |
167 |
| - if (tap_init(tapdev, "wtap0") < 0) { |
168 |
| - printf("Failed to initialize TAP device: %s\n", strerror(errno)); |
| 59 | + /* Initialize TAP device */ |
| 60 | + ret = tap_init(dev, "wtap0", ip); |
| 61 | + if (ret < 0) { |
| 62 | + printf("Failed to initialize TAP device\n"); |
169 | 63 | return -1;
|
170 | 64 | }
|
171 |
| - printf("TAP device initialized successfully\n"); |
172 | 65 |
|
173 |
| - /* Set device callbacks */ |
174 |
| - tapdev->poll = tap_poll; |
175 |
| - tapdev->send = tap_send; |
176 |
| - |
177 |
| - /* Configure IP settings */ |
178 |
| - printf("Configuring network settings:\n"); |
179 |
| - printf(" IP: 10.10.0.10\n"); |
180 |
| - printf(" Netmask: 255.255.255.0\n"); |
181 |
| - printf(" Gateway: 10.10.0.1\n"); |
182 |
| - |
183 |
| - wolfIP_ipconfig_set(g_wolfip, |
184 |
| - atoip4("10.10.0.10"), /* IP */ |
185 |
| - atoip4("255.255.255.0"), /* Netmask */ |
186 |
| - atoip4("10.10.0.1")); /* Gateway */ |
187 |
| - |
188 |
| - printf("Network configuration complete\n"); |
| 66 | + /* Set IP configuration */ |
| 67 | + wolfIP_ipconfig_set(ipstack, ip, netmask, gateway); |
| 68 | + |
| 69 | + /* Print IP configuration */ |
| 70 | + wolfIP_ipconfig_get(ipstack, &ip, &netmask, &gateway); |
| 71 | + printf("IP Configuration:\n"); |
| 72 | + printf("IP: %d.%d.%d.%d\n", |
| 73 | + (ntohl(ip) >> 24) & 0xFF, |
| 74 | + (ntohl(ip) >> 16) & 0xFF, |
| 75 | + (ntohl(ip) >> 8) & 0xFF, |
| 76 | + ntohl(ip) & 0xFF); |
| 77 | + printf("Netmask: %d.%d.%d.%d\n", |
| 78 | + (ntohl(netmask) >> 24) & 0xFF, |
| 79 | + (ntohl(netmask) >> 16) & 0xFF, |
| 80 | + (ntohl(netmask) >> 8) & 0xFF, |
| 81 | + ntohl(netmask) & 0xFF); |
| 82 | + printf("Gateway: %d.%d.%d.%d\n", |
| 83 | + (ntohl(gateway) >> 24) & 0xFF, |
| 84 | + (ntohl(gateway) >> 16) & 0xFF, |
| 85 | + (ntohl(gateway) >> 8) & 0xFF, |
| 86 | + ntohl(gateway) & 0xFF); |
| 87 | + |
| 88 | + g_ipstack = ipstack; |
189 | 89 | return 0;
|
190 | 90 | }
|
191 | 91 |
|
192 |
| -static void UDP_Echo_Task(void* pvParameters) { |
193 |
| - int sockfd; |
194 |
| - uint8_t buf[1024]; |
195 |
| - int ret; |
196 |
| - struct wolfIP_sockaddr_in addr; |
197 |
| - struct wolfIP_sockaddr_in client_addr; |
198 |
| - socklen_t client_len; |
199 |
| - |
200 |
| - sockfd = wolfIP_sock_socket(g_wolfip, AF_INET, SOCK_DGRAM, 0); |
201 |
| - if (sockfd < 0) { |
202 |
| - printf("Failed to create UDP socket\n"); |
203 |
| - return; |
204 |
| - } |
205 |
| - |
206 |
| - memset(&addr, 0, sizeof(addr)); |
207 |
| - addr.sin_family = AF_INET; |
208 |
| - addr.sin_port = htons(UDP_TEST_PORT); |
209 |
| - addr.sin_addr.s_addr = htonl(INADDR_ANY); |
210 |
| - |
211 |
| - if (wolfIP_sock_bind(g_wolfip, sockfd, (struct wolfIP_sockaddr*)&addr, sizeof(addr)) < 0) { |
212 |
| - printf("Failed to bind UDP socket\n"); |
213 |
| - wolfIP_sock_close(g_wolfip, sockfd); |
214 |
| - return; |
215 |
| - } |
216 |
| - |
217 |
| - printf("UDP Echo Server running on port %d\n", UDP_TEST_PORT); |
218 |
| - |
219 |
| - while (1) { |
220 |
| - client_len = sizeof(client_addr); |
221 |
| - ret = wolfIP_sock_recvfrom(g_wolfip, sockfd, buf, sizeof(buf), 0, |
222 |
| - (struct wolfIP_sockaddr*)&client_addr, &client_len); |
223 |
| - if (ret > 0) { |
224 |
| - uint32_t ip = ntohl(client_addr.sin_addr.s_addr); |
225 |
| - printf("Received %d bytes from %d.%d.%d.%d:%d\n", ret, |
226 |
| - (ip >> 24) & 0xFF, |
227 |
| - (ip >> 16) & 0xFF, |
228 |
| - (ip >> 8) & 0xFF, |
229 |
| - ip & 0xFF, |
230 |
| - ntohs(client_addr.sin_port)); |
231 |
| - wolfIP_sock_sendto(g_wolfip, sockfd, buf, ret, 0, |
232 |
| - (struct wolfIP_sockaddr*)&client_addr, client_len); |
233 |
| - } |
234 |
| - vTaskDelay(pdMS_TO_TICKS(10)); |
235 |
| - } |
236 |
| -} |
237 |
| - |
238 |
| -int wolfIP_Start_UDP_Echo(void) { |
239 |
| - BaseType_t ret; |
240 |
| - |
241 |
| - ret = xTaskCreate(UDP_Echo_Task, |
242 |
| - "UDP_Echo", |
243 |
| - WOLFIP_TASK_STACK_SIZE, |
244 |
| - NULL, |
245 |
| - tskIDLE_PRIORITY + 1, |
246 |
| - NULL); |
247 |
| - |
248 |
| - return (ret == pdPASS) ? 0 : -1; |
249 |
| -} |
250 |
| - |
251 |
| -int wolfIP_FreeRTOS_Start(void) { |
252 |
| - BaseType_t ret; |
253 |
| - |
254 |
| - if (!g_wolfip) { |
255 |
| - printf("wolfIP not initialized\n"); |
256 |
| - return -1; |
257 |
| - } |
258 |
| - |
259 |
| - ret = xTaskCreate(wolfIP_NetworkTask, |
260 |
| - "WolfIP_Net", |
261 |
| - WOLFIP_TASK_STACK_SIZE, |
262 |
| - NULL, |
263 |
| - WOLFIP_TASK_PRIORITY, |
264 |
| - &g_network_task); |
265 |
| - |
266 |
| - return (ret == pdPASS) ? 0 : -1; |
| 92 | +struct wolfIP* wolfip_get_stack(void) |
| 93 | +{ |
| 94 | + return g_ipstack; |
267 | 95 | }
|
0 commit comments