Skip to content

Commit 3f1cec6

Browse files
committed
Addressed review comments
1 parent 077eed3 commit 3f1cec6

11 files changed

Lines changed: 296 additions & 145 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CC?=gcc
22
CFLAGS:=-Wall -Werror -Wextra -I. -D_GNU_SOURCE
3-
CFLAGS+=-g -ggdb
3+
CFLAGS+=-g -ggdb -Wdeclaration-after-statement
44
LDFLAGS+=-pthread
55

66
CPPCHECK=cppcheck

src/http/httpd.c

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "wolfip.h"
2222
#include "httpd.h"
2323
#include <ctype.h>
24+
#include <string.h>
2425

2526
static const char *http_status_text(int status_code) {
2627
switch (status_code) {
@@ -187,7 +188,6 @@ void http_send_response_chunk_end(struct http_client *hc) {
187188
hc->ssl = NULL;
188189
wolfIP_sock_close(hc->httpd->ipstack, hc->client_sd);
189190
hc->client_sd = 0;
190-
return;
191191
}
192192
} else {
193193
if (wolfIP_sock_send(hc->httpd->ipstack, hc->client_sd, "0\r\n\r\n", 5, 0) <= 0) {
@@ -217,27 +217,47 @@ void http_send_418_teapot(struct http_client *hc) {
217217
http_status_text(HTTP_STATUS_TEAPOT), "text/plain", 0);
218218
}
219219

220-
int http_url_decode(char *buf, size_t len) {
220+
static int http_hex_value(char c)
221+
{
222+
if (c >= '0' && c <= '9')
223+
return c - '0';
224+
c = (char)tolower((unsigned char)c);
225+
if (c >= 'a' && c <= 'f')
226+
return 10 + (c - 'a');
227+
return -1;
228+
}
229+
230+
int http_url_decode(char *buf, size_t len)
231+
{
221232
char *p = buf;
222-
char *q;
223-
while (p < buf + len) {
224-
q = strchr(p, '%');
225-
if (!q) {
226-
break;
227-
}
228-
/* Ensure we have two more hex digits */
229-
if (q + 2 >= buf + len) {
230-
break; /* Malformed escape */
231-
}
232-
/* Validate hex characters before conversion */
233-
if (!isxdigit((unsigned char)q[1]) || !isxdigit((unsigned char)q[2])) {
233+
char *end = buf + len;
234+
int hi;
235+
int lo;
236+
size_t tail;
237+
238+
while (p < end) {
239+
char *percent = memchr(p, '%', (size_t)(end - p));
240+
if (!percent)
234241
break;
235-
}
236-
*q = (char) strtol(q + 1, NULL, 16);
237-
memmove(q + 1, q + 3, len - (q + 3 - buf));
242+
243+
if (percent + 2 >= end)
244+
return HTTP_URL_DECODE_ERR_TRUNCATED;
245+
246+
hi = http_hex_value(percent[1]);
247+
lo = http_hex_value(percent[2]);
248+
if (hi < 0 || lo < 0)
249+
return HTTP_URL_DECODE_ERR_BAD_ESCAPE;
250+
251+
*percent = (char)((hi << 4) | lo);
252+
253+
tail = (size_t)(end - (percent + 3));
254+
memmove(percent + 1, percent + 3, tail);
255+
end -= 2;
238256
len -= 2;
257+
p = percent + 1;
239258
}
240-
return len;
259+
260+
return (int)len;
241261
}
242262

243263
int http_url_encode(char *buf, size_t len, size_t max_len) {
@@ -258,8 +278,11 @@ int http_url_encode(char *buf, size_t len, size_t max_len) {
258278
*(q + 2) = '0';
259279
len += 2;
260280
}
261-
if (q && (len < max_len))
281+
if (q) {
282+
if (len >= max_len)
283+
return -1; /* No space for the null terminator */
262284
q[len] = '\0';
285+
}
263286
return len;
264287
}
265288

@@ -269,10 +292,18 @@ static int parse_http_request(struct http_client *hc, uint8_t *buf, size_t len)
269292
char *q;
270293
size_t n;
271294
int ret;
295+
int decoded_len;
272296
struct http_request req;
273297
struct http_url *url = NULL;
274298
memset(&req, 0, sizeof(struct http_request));
275-
http_url_decode(p, len); /* Decode can be done in place */
299+
decoded_len = http_url_decode(p, len); /* Decode can be done in place */
300+
if (decoded_len < 0) {
301+
http_send_response_headers(hc, HTTP_STATUS_BAD_REQUEST,
302+
http_status_text(HTTP_STATUS_BAD_REQUEST), "text/plain", 0);
303+
return decoded_len;
304+
}
305+
len = (size_t)decoded_len;
306+
end = p + len;
276307
if (len < 4)
277308
goto bad_request;
278309
/* Parse the request line */

src/http/httpd.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ void http_send_500_server_error(struct http_client *hc);
7878
void http_send_503_service_unavailable(struct http_client *hc);
7979
void http_send_418_teapot(struct http_client *hc);
8080

81+
/* URL decoding return codes */
82+
#define HTTP_URL_DECODE_ERR_TRUNCATED (-1)
83+
#define HTTP_URL_DECODE_ERR_BAD_ESCAPE (-2)
84+
85+
/* Returns the decoded length on success, or negative error code on failure. */
8186
int http_url_decode(char *buf, size_t len);
8287
int http_url_encode(char *buf, size_t len, size_t max_len);
8388

src/port/posix/bsd_socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ int wolfIP_sock_fcntl(struct wolfIP *ipstack, int fd, int cmd, int arg) {
153153
int fcntl(int fd, int cmd, ...) {
154154
va_list ap;
155155
int arg;
156+
int ret;
156157
va_start(ap, cmd);
157158
arg = va_arg(ap, int);
158159
va_end(ap);
159-
int ret;
160160
if (in_the_stack) {
161161
return host_fcntl(fd, cmd, arg);
162162
} else {

src/port/posix/linux_tap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ void print_buffer(uint8_t *buf, int len)
4848
static int tap_poll(struct wolfIP_ll_dev *ll, void *buf, uint32_t len)
4949
{
5050
struct pollfd pfd;
51-
(void)ll;
5251
int ret;
52+
(void)ll;
5353
pfd.fd = tap_fd;
5454
pfd.events = POLLIN;
5555
ret = poll(&pfd, 1, 2);

src/port/wolfssl_io.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@
2424
#include <wolfssl/ssl.h>
2525
#include <wolfssl/wolfcrypt/memory.h>
2626

27-
#define MAX_WOLFIP_CTX 8
27+
#ifndef EAGAIN
28+
#define EAGAIN (11)
29+
#endif
30+
31+
32+
#ifndef MAX_WOLFIP_CTX
33+
#define MAX_WOLFIP_CTX 8 /* Default value */
34+
#endif
2835

2936
struct ctx_entry {
3037
WOLFSSL_CTX *ctx;
@@ -62,13 +69,14 @@ static void wolfIP_register_stack(WOLFSSL_CTX *ctx, struct wolfIP *stack)
6269
static int wolfIP_io_recv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
6370
{
6471
struct wolfip_io_desc *desc = (struct wolfip_io_desc *)ctx;
72+
int ret;
6573
(void)ssl;
6674

6775
if (!desc || !desc->stack)
6876
return WOLFSSL_CBIO_ERR_GENERAL;
6977

70-
int ret = wolfIP_sock_recv(desc->stack, desc->fd, buf, sz, 0);
71-
if (ret == -11 || ret == -1)
78+
ret = wolfIP_sock_recv(desc->stack, desc->fd, buf, sz, 0);
79+
if (ret == -EAGAIN || ret == -1)
7280
return WOLFSSL_CBIO_ERR_WANT_READ;
7381
if (ret <= 0)
7482
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
@@ -78,13 +86,14 @@ static int wolfIP_io_recv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
7886
static int wolfIP_io_send(WOLFSSL* ssl, char* buf, int sz, void* ctx)
7987
{
8088
struct wolfip_io_desc *desc = (struct wolfip_io_desc *)ctx;
89+
int ret;
8190
(void)ssl;
8291

8392
if (!desc || !desc->stack)
8493
return WOLFSSL_CBIO_ERR_GENERAL;
8594

86-
int ret = wolfIP_sock_send(desc->stack, desc->fd, buf, sz, 0);
87-
if (ret == -11 || ret == -1)
95+
ret = wolfIP_sock_send(desc->stack, desc->fd, buf, sz, 0);
96+
if (ret == -EAGAIN || ret == -1)
8897
return WOLFSSL_CBIO_ERR_WANT_WRITE;
8998
if (ret <= 0)
9099
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
@@ -101,8 +110,20 @@ int wolfSSL_SetIO_wolfIP_CTX(WOLFSSL_CTX* ctx, struct wolfIP *s)
101110

102111
int wolfSSL_SetIO_wolfIP(WOLFSSL* ssl, int fd)
103112
{
104-
WOLFSSL_CTX *ctx = wolfSSL_get_SSL_CTX(ssl);
105-
struct wolfIP *stack = wolfIP_lookup_stack(ctx);
113+
WOLFSSL_CTX *ctx;
114+
struct wolfIP *stack;
115+
116+
if (!ssl)
117+
return -1;
118+
119+
ctx = wolfSSL_get_SSL_CTX(ssl);
120+
121+
if (!ctx)
122+
return -1;
123+
124+
stack = wolfIP_lookup_stack(ctx);
125+
if (fd < 0)
126+
return -1;
106127

107128
if (!stack)
108129
return WOLFSSL_CBIO_ERR_GENERAL;
@@ -116,6 +137,5 @@ int wolfSSL_SetIO_wolfIP(WOLFSSL* ssl, int fd)
116137
return 0;
117138
}
118139
}
119-
120140
return -1;
121141
}

src/test/tcp_echo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ int main() {
6363
printf("Echo server listening on port %d\n", PORT);
6464

6565
while (1) {
66+
ssize_t bytes_read;
6667
// Accept a client connection
6768
if ((client_fd = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
6869
perror("Accept failed");
@@ -71,7 +72,6 @@ int main() {
7172

7273
printf("Client connected, fd: %d\n", client_fd);
7374

74-
ssize_t bytes_read;
7575
while ((bytes_read = read(client_fd, buffer, BUFFER_SIZE)) > 0) {
7676
write(client_fd, buffer, bytes_read); // Echo data back to the client
7777
}

src/test/test_ttl_expired.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,14 @@ static struct mem_ep *mem_ep_lookup(struct wolfIP_ll_dev *ll)
138138
static int mem_ll_poll(struct wolfIP_ll_dev *ll, void *buf, uint32_t len)
139139
{
140140
struct mem_ep *ep = mem_ep_lookup(ll);
141+
struct mem_link *link;
142+
int idx;
143+
int ret = 0;
144+
141145
if (!ep)
142146
return -1;
143-
struct mem_link *link = ep->link;
144-
int idx = ep->idx;
145-
int ret = 0;
147+
link = ep->link;
148+
idx = ep->idx;
146149

147150
pthread_mutex_lock(&link->lock);
148151
if (link->ready[idx]) {
@@ -161,10 +164,13 @@ static int mem_ll_poll(struct wolfIP_ll_dev *ll, void *buf, uint32_t len)
161164
static int mem_ll_send(struct wolfIP_ll_dev *ll, void *buf, uint32_t len)
162165
{
163166
struct mem_ep *ep = mem_ep_lookup(ll);
167+
struct mem_link *link;
168+
int dst;
169+
164170
if (!ep)
165171
return -1;
166-
struct mem_link *link = ep->link;
167-
int dst = 1 - ep->idx;
172+
link = ep->link;
173+
dst = 1 - ep->idx;
168174

169175
pthread_mutex_lock(&link->lock);
170176
while (link->ready[dst])
@@ -283,8 +289,10 @@ static void *poll_thread(void *arg)
283289
struct wolfIP *s = (struct wolfIP *)arg;
284290
while (running) {
285291
struct timespec ts;
292+
uint64_t now;
293+
286294
clock_gettime(CLOCK_MONOTONIC, &ts);
287-
uint64_t now = (uint64_t)ts.tv_sec * 1000ULL + ts.tv_nsec / 1000000ULL;
295+
now = (uint64_t)ts.tv_sec * 1000ULL + ts.tv_nsec / 1000000ULL;
288296
wolfIP_poll(s, now);
289297
usleep(1000);
290298
}
@@ -293,7 +301,6 @@ static void *poll_thread(void *arg)
293301

294302
int main(void)
295303
{
296-
setvbuf(stdout, NULL, _IONBF, 0);
297304
struct wolfIP *router;
298305
struct wolfIP_ll_dev *iface0;
299306
struct wolfIP_ll_dev *iface1;
@@ -304,6 +311,9 @@ int main(void)
304311
uint8_t router1_mac[6] = {0x02,0x00,0x00,0x00,0xCC,0x01};
305312
uint8_t frame[LINK_MTU];
306313
int rc = EXIT_FAILURE;
314+
int n;
315+
316+
setvbuf(stdout, NULL, _IONBF, 0);
307317

308318
mem_link_init(&link);
309319
wolfIP_init_static(&router);
@@ -333,7 +343,7 @@ int main(void)
333343
build_ttl_frame(frame, host_mac, router0_mac, HOST_IP, DEST_IP);
334344
mem_host_send(&link, frame, sizeof(struct eth_hdr) + sizeof(struct ipv4_hdr) + sizeof(struct icmp_echo));
335345

336-
int n = mem_host_recv(&link, frame, sizeof(frame), 1000);
346+
n = mem_host_recv(&link, frame, sizeof(frame), 1000);
337347
if (n > 0) {
338348
struct eth_hdr *eth = (struct eth_hdr *)frame;
339349
struct ipv4_hdr *ip = (struct ipv4_hdr *)(frame + sizeof(*eth));

0 commit comments

Comments
 (0)