diff --git a/mongoose.c b/mongoose.c index e889ced8..37146b5d 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4327,7 +4327,7 @@ static uint32_t csumup(uint32_t sum, const void *buf, size_t len) { static uint16_t csumfin(uint32_t sum) { while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16); - return mg_htons((uint16_t) (~sum & 0xffff)); + return mg_htons((uint16_t) ((uint16_t) ~sum & 0xffff)); } static uint16_t ipcsum(const void *buf, size_t len) { @@ -5011,10 +5011,11 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) { static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) { uint16_t frag = mg_ntohs(pkt->ip->frag); if (frag & IP_MORE_FRAGS_MSK || frag & IP_FRAG_OFFSET_MSK) { - struct mg_connection *c = getpeer(ifp->mgr, pkt, false); - if (c) mg_error(c, "Received fragmented packet"); + struct mg_connection *c; if (pkt->ip->proto == 17) pkt->udp = (struct udp *) (pkt->ip + 1); if (pkt->ip->proto == 6) pkt->tcp = (struct tcp *) (pkt->ip + 1); + c = getpeer(ifp->mgr, pkt, false); + if (c) mg_error(c, "Received fragmented packet"); } else if (pkt->ip->proto == 1) { pkt->icmp = (struct icmp *) (pkt->ip + 1); if (pkt->pay.len < sizeof(*pkt->icmp)) return; @@ -16159,7 +16160,7 @@ NS_INTERNAL bigint *bi_crt(BI_CTX *ctx, bigint *bi, bigint *dP, bigint *dQ, int mg_rsa_mod_pow(const uint8_t *mod, size_t modsz, const uint8_t *exp, size_t expsz, const uint8_t *msg, size_t msgsz, uint8_t *out, size_t outsz) { BI_CTX *bi_ctx = bi_initialize(); - bigint *m1; + bigint *m1; bigint *n = bi_import(bi_ctx, mod, (int) modsz); bigint *e = bi_import(bi_ctx, exp, (int) expsz); bigint *h = bi_import(bi_ctx, msg, (int) msgsz); diff --git a/src/net_builtin.c b/src/net_builtin.c index 75549078..a9e54f8d 100644 --- a/src/net_builtin.c +++ b/src/net_builtin.c @@ -845,10 +845,11 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) { static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) { uint16_t frag = mg_ntohs(pkt->ip->frag); if (frag & IP_MORE_FRAGS_MSK || frag & IP_FRAG_OFFSET_MSK) { - struct mg_connection *c = getpeer(ifp->mgr, pkt, false); - if (c) mg_error(c, "Received fragmented packet"); + struct mg_connection *c; if (pkt->ip->proto == 17) pkt->udp = (struct udp *) (pkt->ip + 1); if (pkt->ip->proto == 6) pkt->tcp = (struct tcp *) (pkt->ip + 1); + c = getpeer(ifp->mgr, pkt, false); + if (c) mg_error(c, "Received fragmented packet"); } else if (pkt->ip->proto == 1) { pkt->icmp = (struct icmp *) (pkt->ip + 1); if (pkt->pay.len < sizeof(*pkt->icmp)) return; diff --git a/src/tls_rsa.c b/src/tls_rsa.c index 85d5fa2e..752bf9ea 100644 --- a/src/tls_rsa.c +++ b/src/tls_rsa.c @@ -1576,7 +1576,7 @@ NS_INTERNAL bigint *bi_crt(BI_CTX *ctx, bigint *bi, bigint *dP, bigint *dQ, int mg_rsa_mod_pow(const uint8_t *mod, size_t modsz, const uint8_t *exp, size_t expsz, const uint8_t *msg, size_t msgsz, uint8_t *out, size_t outsz) { BI_CTX *bi_ctx = bi_initialize(); - bigint *m1; + bigint *m1; bigint *n = bi_import(bi_ctx, mod, (int) modsz); bigint *e = bi_import(bi_ctx, exp, (int) expsz); bigint *h = bi_import(bi_ctx, msg, (int) msgsz); diff --git a/test/mip_test.c b/test/mip_test.c index 251c1385..56f02936 100644 --- a/test/mip_test.c +++ b/test/mip_test.c @@ -1,11 +1,11 @@ #define MG_ENABLE_TCPIP 1 #define MG_ENABLE_TCPIP_DRIVER_INIT 0 -#include "mongoose.c" // order is important, this one first +#include "mongoose.c" // order is important, this one first #include "driver_mock.c" static int s_num_tests = 0; -static bool s_sent_fragment = 0; +static int s_sent_fragment = 0; static int s_seg_sent = 0; #ifdef NO_SLEEP_ABORT @@ -25,6 +25,13 @@ static int s_seg_sent = 0; } \ } while (0) +static void test_csum(void) { + uint8_t ip[20] = {0x45, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x28, 0x11, + 0x94, 0xcf, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x01}; + ASSERT(ipcsum(ip, 20) == 0); + // UDP and TCP checksum calc funcions use the same basic calls as ipcsum() +} + static void test_statechange(void) { char tx[1540]; struct mg_tcpip_if iface; @@ -47,8 +54,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { static void frag_recv_fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_ERROR) { - if (s_sent_fragment) { + if (s_sent_fragment > 0) { + ASSERT(s_sent_fragment == 1); ASSERT(strcmp((char *) ev_data, "Received fragmented packet") == 0); + s_sent_fragment = 2; } } (void) c, (void) ev_data; @@ -57,11 +66,11 @@ static void frag_recv_fn(struct mg_connection *c, int ev, void *ev_data) { // mock send to a non-existent peer using the listener connection static void frag_send_fn(struct mg_connection *c, int ev, void *ev_data) { static bool s_sent; - static int s_seg_sizes[] = {416, 416, 368}; // based on len=1200 and MTU=500 + static int s_seg_sizes[] = {416, 416, 368}; // based on len=1200 and MTU=500 if (ev == MG_EV_POLL) { if (!s_sent) { struct connstate *s = (struct connstate *) (c + 1); - s->dmss = 1500; // mock set some destination MSS way larger + s->dmss = 1500; // mock set some destination MSS way larger c->send.len = 1200; // setting TCP payload size s_sent = true; } @@ -284,8 +293,9 @@ static void test_frag_recv_path(void) { ip.len = mg_htons(sizeof(struct ip) + sizeof(struct tcp) + 1000); ip.frag |= IP_MORE_FRAGS_MSK; // setting More Fragments bit to 1 create_tcp_pkt(&e, &ip, 1001, 1, TH_PUSH | TH_ACK, 1000); - s_sent_fragment = true; - mg_mgr_poll(&mgr, 0); + s_sent_fragment = 1; // "enable" fn + mg_mgr_poll(&mgr, 0); // call it (process fake frag IP) + ASSERT(s_sent_fragment == 2); // check it followed the right path s_driver_data.len = 0; mg_mgr_free(&mgr); @@ -320,6 +330,7 @@ static void test_fragmentation(void) { } int main(void) { + test_csum(); test_statechange(); test_poll(); test_retransmit();