Skip to content

Commit f126104

Browse files
Add UDP echo server and fix wolfIP socket integration
Co-Authored-By: daniele@wolfssl.com <daniele@wolfssl.com>
1 parent 7617c27 commit f126104

3 files changed

Lines changed: 99 additions & 3 deletions

File tree

fullstack/freertos-wolfip-wolfssl-https/src/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ static void testTask(void* pvParameters) {
2020
printf("Failed to start wolfIP network task\n");
2121
return;
2222
}
23+
24+
printf("Starting UDP echo server...\n");
25+
ret = wolfIP_Start_UDP_Echo();
26+
if (ret != 0) {
27+
printf("Failed to start UDP echo server\n");
28+
return;
29+
}
2330

24-
printf("Network stack running...\n");
31+
printf("Network stack and UDP echo server running...\n");
2532
for(;;) {
2633
vTaskDelay(xDelay);
2734
}

fullstack/freertos-wolfip-wolfssl-https/src/wolfip_freertos.c

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <poll.h>
1212
#include <sys/socket.h>
1313
#include <sys/random.h>
14+
#include <errno.h>
1415

1516
/* Implementation of wolfIP's required random number generator */
1617
uint32_t wolfIP_getrandom(void) {
@@ -75,6 +76,24 @@ static int tap_init(struct ll *dev, const char *ifname) {
7576
return -1;
7677
}
7778

79+
/* Configure IP address */
80+
struct sockaddr_in *addr = (struct sockaddr_in *)&ifr.ifr_addr;
81+
addr->sin_family = AF_INET;
82+
addr->sin_addr.s_addr = inet_addr("192.168.1.10");
83+
if (ioctl(sock_fd, SIOCSIFADDR, &ifr) < 0) {
84+
perror("ioctl SIOCSIFADDR");
85+
close(sock_fd);
86+
return -1;
87+
}
88+
89+
/* Configure netmask */
90+
addr->sin_addr.s_addr = inet_addr("255.255.255.0");
91+
if (ioctl(sock_fd, SIOCSIFNETMASK, &ifr) < 0) {
92+
perror("ioctl SIOCSIFNETMASK");
93+
close(sock_fd);
94+
return -1;
95+
}
96+
7897
close(sock_fd);
7998
return 0;
8099
}
@@ -85,7 +104,10 @@ static int tap_poll(struct ll *ll, void *buf, uint32_t len) {
85104

86105
pfd.fd = tap_fd;
87106
pfd.events = POLLIN;
88-
ret = poll(&pfd, 1, 1); /* Short timeout */
107+
108+
do {
109+
ret = poll(&pfd, 1, 1); /* Short timeout */
110+
} while (ret < 0 && errno == EINTR);
89111

90112
if (ret < 0) {
91113
perror("poll");
@@ -95,7 +117,11 @@ static int tap_poll(struct ll *ll, void *buf, uint32_t len) {
95117
return 0;
96118
}
97119

98-
return read(tap_fd, buf, len);
120+
do {
121+
ret = read(tap_fd, buf, len);
122+
} while (ret < 0 && errno == EINTR);
123+
124+
return ret;
99125
}
100126

101127
static int tap_send(struct ll *ll, void *buf, uint32_t len) {
@@ -153,6 +179,65 @@ int wolfIP_FreeRTOS_Init(void) {
153179
return 0;
154180
}
155181

182+
static void UDP_Echo_Task(void* pvParameters) {
183+
int sockfd;
184+
uint8_t buf[1024];
185+
int ret;
186+
struct wolfIP_sockaddr_in addr;
187+
struct wolfIP_sockaddr_in client_addr;
188+
socklen_t client_len;
189+
190+
sockfd = wolfIP_sock_socket(g_wolfip, AF_INET, SOCK_DGRAM, 0);
191+
if (sockfd < 0) {
192+
printf("Failed to create UDP socket\n");
193+
return;
194+
}
195+
196+
memset(&addr, 0, sizeof(addr));
197+
addr.sin_family = AF_INET;
198+
addr.sin_port = htons(UDP_TEST_PORT);
199+
addr.sin_addr.s_addr = htonl(INADDR_ANY);
200+
201+
if (wolfIP_sock_bind(g_wolfip, sockfd, (struct wolfIP_sockaddr*)&addr, sizeof(addr)) < 0) {
202+
printf("Failed to bind UDP socket\n");
203+
wolfIP_sock_close(g_wolfip, sockfd);
204+
return;
205+
}
206+
207+
printf("UDP Echo Server running on port %d\n", UDP_TEST_PORT);
208+
209+
while (1) {
210+
client_len = sizeof(client_addr);
211+
ret = wolfIP_sock_recvfrom(g_wolfip, sockfd, buf, sizeof(buf), 0,
212+
(struct wolfIP_sockaddr*)&client_addr, &client_len);
213+
if (ret > 0) {
214+
uint32_t ip = ntohl(client_addr.sin_addr.s_addr);
215+
printf("Received %d bytes from %d.%d.%d.%d:%d\n", ret,
216+
(ip >> 24) & 0xFF,
217+
(ip >> 16) & 0xFF,
218+
(ip >> 8) & 0xFF,
219+
ip & 0xFF,
220+
ntohs(client_addr.sin_port));
221+
wolfIP_sock_sendto(g_wolfip, sockfd, buf, ret, 0,
222+
(struct wolfIP_sockaddr*)&client_addr, client_len);
223+
}
224+
vTaskDelay(pdMS_TO_TICKS(10));
225+
}
226+
}
227+
228+
int wolfIP_Start_UDP_Echo(void) {
229+
BaseType_t ret;
230+
231+
ret = xTaskCreate(UDP_Echo_Task,
232+
"UDP_Echo",
233+
WOLFIP_TASK_STACK_SIZE,
234+
NULL,
235+
tskIDLE_PRIORITY + 1,
236+
NULL);
237+
238+
return (ret == pdPASS) ? 0 : -1;
239+
}
240+
156241
int wolfIP_FreeRTOS_Start(void) {
157242
BaseType_t ret;
158243

fullstack/freertos-wolfip-wolfssl-https/src/wolfip_freertos.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
#define WOLFIP_TASK_PRIORITY (tskIDLE_PRIORITY + 2)
1010
#define WOLFIP_TASK_STACK_SIZE (8 * 1024)
1111
#define WOLFIP_POLL_INTERVAL_MS 10
12+
#define UDP_TEST_PORT 7777
1213

1314
/* Initialize wolfIP with FreeRTOS */
1415
int wolfIP_FreeRTOS_Init(void);
1516

1617
/* Start wolfIP network task */
1718
int wolfIP_FreeRTOS_Start(void);
1819

20+
/* Start UDP echo server task */
21+
int wolfIP_Start_UDP_Echo(void);
22+
1923
#endif /* WOLFIP_FREERTOS_H */

0 commit comments

Comments
 (0)