From 1df8bc0dfe7806fa45a91fe1c8e1aa92b042f520 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Mon, 27 Feb 2023 10:22:52 -0300 Subject: [PATCH 1/7] Update test.yml Fix embedded matrix workflow not signalling changes and not running --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b9ac4190..756f00e3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -201,7 +201,7 @@ jobs: steps: - uses: actions/checkout@v3 with: { fetch-depth: 2 } - - run: if ./test/match_changed_files.sh '^src|^examples/${{ matrix.example.path }}'; then sudo apt -y update ; sudo apt -y install gcc-arm-none-eabi ; fi + - run: if ./test/match_changed_files.sh '^src|^examples/${{ matrix.example.path }}'; then echo GO=1 >> $GITHUB_ENV ; fi - if: ${{ env.GO == 1 }} run: sudo apt -y update ; sudo apt -y install gcc-arm-none-eabi - if: ${{ env.GO == 1 }} From 4e7584167b16be0ff12baedab082a03fc1b08df7 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Mon, 27 Feb 2023 10:54:32 -0300 Subject: [PATCH 2/7] force check and failures --- examples/stm32/nucleo-f429zi-rndis/main.c | 2 +- src/tcpip/tcpip.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/stm32/nucleo-f429zi-rndis/main.c b/examples/stm32/nucleo-f429zi-rndis/main.c index 7777b5d5..ba6dd324 100644 --- a/examples/stm32/nucleo-f429zi-rndis/main.c +++ b/examples/stm32/nucleo-f429zi-rndis/main.c @@ -95,7 +95,7 @@ int main(void) { .mask = mg_htonl(MG_U32(255, 255, 255, 0)), .enable_dhcp_server = true, .driver = &driver, - .queue.len = 4096}; + .recv_queue.size = 4096}; s_ifp = &mif; mg_tcpip_init(&mgr, &mif); mg_timer_add(&mgr, 500, MG_TIMER_REPEAT, blink_cb, &mgr); diff --git a/src/tcpip/tcpip.h b/src/tcpip/tcpip.h index 8c042619..371faed4 100644 --- a/src/tcpip/tcpip.h +++ b/src/tcpip/tcpip.h @@ -64,8 +64,8 @@ struct mg_tcpip_spi { #if MG_ENABLE_TCPIP #if !defined(MG_ENABLE_DRIVER_STM32H) && !defined(MG_ENABLE_DRIVER_TM4C) -#define MG_ENABLE_DRIVER_STM32 1 + #define MG_ENABLE_DRIVER_STM32 1 #else -#define MG_ENABLE_DRIVER_STM32 0 + #define MG_ENABLE_DRIVER_STM32 0 #endif #endif From 520507afd965ccef83f83ed2e96e8a65291af733 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Mon, 27 Feb 2023 11:28:42 -0300 Subject: [PATCH 3/7] unbreak RNDIS examples --- examples/rp2040/pico-rmii/driver_rp2040_rmii.c | 3 +-- examples/rp2040/pico-rndis-dashboard/main.c | 4 ++-- examples/stm32/nucleo-f429zi-rndis/main.c | 3 +-- examples/stm32/nucleo-f746zg-rndis/main.c | 5 ++--- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/examples/rp2040/pico-rmii/driver_rp2040_rmii.c b/examples/rp2040/pico-rmii/driver_rp2040_rmii.c index 5a244d24..b61bd450 100644 --- a/examples/rp2040/pico-rmii/driver_rp2040_rmii.c +++ b/examples/rp2040/pico-rmii/driver_rp2040_rmii.c @@ -256,7 +256,6 @@ static bool mg_tcpip_driver_rp2040_rmii_init(struct mg_tcpip_if *ifp) { (struct mg_tcpip_driver_rp2040_rmii_data *) ifp->driver_data; uint rx_sm_addr, tx_sm_addr; s_ifp = ifp; - if (ifp->queue.len == 0) ifp->queue.len = 8192; rx_sm_addr = pio_add_program(pio0, &rmii_rx_program); tx_sm_addr = pio_add_program(pio0, &rmii_tx_program); smi_wr_addr = pio_add_program(pio0, &smi_wr_program); @@ -338,7 +337,7 @@ static void rx_irq(void) { } static size_t mg_tcpip_driver_rp2040_rmii_rx(void *buf, size_t buflen, struct mg_tcpip_if *ifp) { - size_t len = mg_tcpip_qread(buf, ifp); + size_t len = 0; // mg_tcpip_qread(buf, ifp); if (len == 0) return 0; len -= 4; // exclude CRC from frame length uint32_t crc = crc_calc(buf, len); // calculate CRC and compare diff --git a/examples/rp2040/pico-rndis-dashboard/main.c b/examples/rp2040/pico-rndis-dashboard/main.c index 07846396..1ebd3292 100644 --- a/examples/rp2040/pico-rndis-dashboard/main.c +++ b/examples/rp2040/pico-rndis-dashboard/main.c @@ -56,13 +56,13 @@ int main(void) { mg_mgr_init(&mgr); // and attach it to the MIP interface mg_timer_add(&mgr, 500, MG_TIMER_REPEAT, blink_cb, &mgr); - struct mg_tcpip_driver driver = {.tx = usb_tx, .rx = mg_tcpip_driver_rx, .up = usb_up}; + struct mg_tcpip_driver driver = {.tx = usb_tx, .up = usb_up}; struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 0x77}, .ip = mg_htonl(MG_U32(192, 168, 3, 1)), .mask = mg_htonl(MG_U32(255, 255, 255, 0)), .enable_dhcp_server = true, .driver = &driver, - .queue.len = 4096}; + .recv_queue.size = 4096}; s_ifp = &mif; mg_tcpip_init(&mgr, &mif); tusb_init(); diff --git a/examples/stm32/nucleo-f429zi-rndis/main.c b/examples/stm32/nucleo-f429zi-rndis/main.c index ba6dd324..b4bbe32e 100644 --- a/examples/stm32/nucleo-f429zi-rndis/main.c +++ b/examples/stm32/nucleo-f429zi-rndis/main.c @@ -88,8 +88,7 @@ int main(void) { MG_INFO(("Init TCP/IP stack ...")); - struct mg_tcpip_driver driver = { - .tx = usb_tx, .rx = mg_tcpip_driver_rx, .up = usb_up}; + struct mg_tcpip_driver driver = {.tx = usb_tx, .up = usb_up}; struct mg_tcpip_if mif = {.mac = GENERATE_LOCALLY_ADMINISTERED_MAC(), .ip = mg_htonl(MG_U32(192, 168, 3, 1)), .mask = mg_htonl(MG_U32(255, 255, 255, 0)), diff --git a/examples/stm32/nucleo-f746zg-rndis/main.c b/examples/stm32/nucleo-f746zg-rndis/main.c index 8aee5af3..6bf7ff83 100644 --- a/examples/stm32/nucleo-f746zg-rndis/main.c +++ b/examples/stm32/nucleo-f746zg-rndis/main.c @@ -88,14 +88,13 @@ int main(void) { MG_INFO(("Init TCP/IP stack ...")); - struct mg_tcpip_driver driver = { - .tx = usb_tx, .rx = mg_tcpip_driver_rx, .up = usb_up}; + struct mg_tcpip_driver driver = {.tx = usb_tx, .up = usb_up}; struct mg_tcpip_if mif = {.mac = GENERATE_LOCALLY_ADMINISTERED_MAC(), .ip = mg_htonl(MG_U32(192, 168, 3, 1)), .mask = mg_htonl(MG_U32(255, 255, 255, 0)), .enable_dhcp_server = true, .driver = &driver, - .queue.len = 4096}; + .recv_queue.size = 4096}; s_ifp = &mif; mg_tcpip_init(&mgr, &mif); mg_timer_add(&mgr, 500, MG_TIMER_REPEAT, blink_cb, &mgr); From bba361016d07f06ce17b2aecd632c05d056f96cc Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Mon, 27 Feb 2023 17:21:33 -0300 Subject: [PATCH 4/7] Update RMII ex, enhance MIP --- .../rp2040/pico-rmii/driver_rp2040_rmii.c | 59 +++++-------------- examples/rp2040/pico-rmii/main.c | 2 + mongoose.c | 34 ++++++----- mongoose.h | 15 +++-- src/tcpip/tcpip.c | 22 +++---- src/tcpip/tcpip.h | 19 +++--- src/util.c | 12 ++-- 7 files changed, 73 insertions(+), 90 deletions(-) diff --git a/examples/rp2040/pico-rmii/driver_rp2040_rmii.c b/examples/rp2040/pico-rmii/driver_rp2040_rmii.c index b61bd450..e77b81d1 100644 --- a/examples/rp2040/pico-rmii/driver_rp2040_rmii.c +++ b/examples/rp2040/pico-rmii/driver_rp2040_rmii.c @@ -29,21 +29,21 @@ static dma_channel_config dmacfg_tx; static uint8_t s_rxbuf[2][ETH_PKT_SIZE]; // ping-pong buffer static uint8_t s_txbuf[ETH_PKT_SIZE]; -static struct mg_tcpip_if *s_ifp; // MIP interface +static struct mg_tcpip_if *s_ifp; // MIP interface #define rmii_tx_wrap_target 0 #define rmii_tx_wrap 8 static const uint16_t rmii_tx_program_instructions[] = { // .wrap_target - 0xe000, // 0: set pins, 0 side 0 - 0x80a0, // 1: pull block side 0 - 0xe03d, // 2: set x, 29 side 0 - 0xf101, // 3: set pins, 1 side 1 [1] - 0x1144, // 4: jmp x--, 4 side 1 [1] - 0xf103, // 5: set pins, 3 side 1 [1] - 0x7002, // 6: out pins, 2 side 1 - 0x10e6, // 7: jmp !osre, 6 side 1 + 0xe000, // 0: set pins, 0 side 0 + 0x80a0, // 1: pull block side 0 + 0xe03d, // 2: set x, 29 side 0 + 0xf101, // 3: set pins, 1 side 1 [1] + 0x1144, // 4: jmp x--, 4 side 1 [1] + 0xf103, // 5: set pins, 3 side 1 [1] + 0x7002, // 6: out pins, 2 side 1 + 0x10e6, // 7: jmp !osre, 6 side 1 // .wrap }; @@ -216,21 +216,6 @@ static inline void smi_rd_init(PIO pio, uint sm, uint addr, uint gpio) { pio_sm_init(pio, sm, addr, &c); } -static inline uint32_t crc_calc(const uint8_t *data, int length) { - static const uint32_t crclut[16] = { - // table for polynomial 0xEDB88320 (reflected) - 0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC, 0x76DC4190, 0x6B6B51F4, - 0x4DB26158, 0x5005713C, 0xEDB88320, 0xF00F9344, 0xD6D6A3E8, 0xCB61B38C, - 0x9B64C2B0, 0x86D3D2D4, 0xA00AE278, 0xBDBDF21C}; - uint32_t crc = 0xFFFFFFFF; - while (--length >= 0) { - uint8_t byte = *data++; - crc = crclut[(crc ^ byte) & 0x0F] ^ (crc >> 4); - crc = crclut[(crc ^ (byte >> 4)) & 0x0F] ^ (crc >> 4); - } - return ~crc; -} - static void eth_write_phy(int addr, int reg, int val, uint8_t gpio) { uint16_t op = 0x5002 | (addr << 7) | (reg << 2); // b0101aaaaarrrrr10 smi_wr_init(pio0, SMITXSM, smi_wr_addr, gpio); @@ -299,12 +284,12 @@ static bool mg_tcpip_driver_rp2040_rmii_init(struct mg_tcpip_if *ifp) { } static size_t mg_tcpip_driver_rp2040_rmii_tx(const void *buf, size_t len, - struct mg_tcpip_if *ifp) { + struct mg_tcpip_if *ifp) { dma_channel_wait_for_finish_blocking(dma_tx); memset(s_txbuf, 0, 60); // pre-pad memcpy(s_txbuf, buf, len); if (len < 60) len = 60; // pad - uint32_t crc = crc_calc(s_txbuf, len); // host is little-endian + uint32_t crc = mg_crc32(0, s_txbuf, len); // host is little-endian memcpy(s_txbuf + len, (uint8_t *) &crc, 4); len += 4; sleep_us(10); // enforce IFG in case software has been lightning fast... @@ -324,31 +309,17 @@ static void rx_irq(void) { 1; // 2 buffers, switch to the available one as fast as possible size_t len = ETH_PKT_SIZE - hw->transfer_count; dma_channel_abort(dma_rx); - dma_channel_set_write_addr(dma_rx, s_rxbuf[rxno], true); // restart DMA + dma_channel_set_write_addr(dma_rx, s_rxbuf[rxno], true); // restart DMA pio_interrupt_clear(pio0, 0); // ACK PIO IRQ so the state machine resumes receiving // NOTE(scaprile) Here we could check addressing to avoid queuing frames not - // for us, or we can defer that for later as we do with CRC The max amount of - // time we can linger here is what it takes for the other buffer to fill - // (<8us) and that includes irq chaining + // for us. The max amount of time we can linger here is what it takes for the + // other buffer to fill (<8us) and that includes irq chaining if (len >= 64 && len <= ETH_PKT_SIZE) mg_tcpip_qwrite(s_rxbuf[s_rxno], len, s_ifp); s_rxno = rxno; } -static size_t mg_tcpip_driver_rp2040_rmii_rx(void *buf, size_t buflen, struct mg_tcpip_if *ifp) { - size_t len = 0; // mg_tcpip_qread(buf, ifp); - if (len == 0) return 0; - len -= 4; // exclude CRC from frame length - uint32_t crc = crc_calc(buf, len); // calculate CRC and compare - // NOTE(scaprile) Here we could check addressing to avoid delivering frames - // not for us, though MIP already does that (now ?) - if (memcmp(&((uint8_t *) buf)[len], &crc, - sizeof(crc))) // host is little-endian - return 0; - return len; -} - static bool mg_tcpip_driver_rp2040_rmii_up(struct mg_tcpip_if *ifp) { struct mg_tcpip_driver_rp2040_rmii_data *d = (struct mg_tcpip_driver_rp2040_rmii_data *) ifp->driver_data; @@ -360,6 +331,6 @@ static bool mg_tcpip_driver_rp2040_rmii_up(struct mg_tcpip_if *ifp) { struct mg_tcpip_driver mg_tcpip_driver_rp2040_rmii = { mg_tcpip_driver_rp2040_rmii_init, mg_tcpip_driver_rp2040_rmii_tx, - mg_tcpip_driver_rp2040_rmii_rx, + NULL, mg_tcpip_driver_rp2040_rmii_up, }; diff --git a/examples/rp2040/pico-rmii/main.c b/examples/rp2040/pico-rmii/main.c index d7e69cbc..6ea96ea1 100644 --- a/examples/rp2040/pico-rmii/main.c +++ b/examples/rp2040/pico-rmii/main.c @@ -42,6 +42,8 @@ int main(void) { struct mg_tcpip_if mif = { .mac = {2, 0, 1, 2, 3, 5}, .ip = 0, + .enable_mac_check = true, + .enable_crc32_check = true, .driver = &mg_tcpip_driver_rp2040_rmii, .driver_data = &driver_data, }; diff --git a/mongoose.c b/mongoose.c index 7a0971c9..6b258f5b 100644 --- a/mongoose.c +++ b/mongoose.c @@ -5672,11 +5672,16 @@ uint16_t mg_ntohs(uint16_t net) { } uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len) { - int i; + static const uint32_t crclut[16] = { + // table for polynomial 0xEDB88320 (reflected) + 0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC, 0x76DC4190, 0x6B6B51F4, + 0x4DB26158, 0x5005713C, 0xEDB88320, 0xF00F9344, 0xD6D6A3E8, 0xCB61B38C, + 0x9B64C2B0, 0x86D3D2D4, 0xA00AE278, 0xBDBDF21C}; crc = ~crc; while (len--) { - crc ^= *(unsigned char *) buf++; - for (i = 0; i < 8; i++) crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1; + uint8_t byte = *(uint8_t *)buf++; + crc = crclut[(crc ^ byte) & 0x0F] ^ (crc >> 4); + crc = crclut[(crc ^ (byte >> 4)) & 0x0F] ^ (crc >> 4); } return ~crc; } @@ -5755,7 +5760,6 @@ uint64_t mg_millis(void) { } #endif - #ifdef MG_ENABLE_LINES #line 1 "src/ws.c" #endif @@ -7867,10 +7871,16 @@ static void mg_tcpip_rx(struct mg_tcpip_if *ifp, void *buf, size_t len) { pkt.raw.len = len; pkt.eth = (struct eth *) buf; if (pkt.raw.len < sizeof(*pkt.eth)) return; // Truncated - runt? - if (memcmp(pkt.eth->dst, ifp->mac, sizeof(pkt.eth->dst)) != 0 && - memcmp(pkt.eth->dst, broadcast, sizeof(pkt.eth->dst)) != 0) { - // Not for us. Drop silently - } else if (pkt.eth->type == mg_htons(0x806)) { + if (ifp->enable_mac_check && + memcmp(pkt.eth->dst, ifp->mac, sizeof(pkt.eth->dst)) != 0 && + memcmp(pkt.eth->dst, broadcast, sizeof(pkt.eth->dst)) != 0) + return; + if (ifp->enable_crc32_check && len > 4) { + len -= 4; // TODO(scaprile): check on bigendian + uint32_t crc = mg_crc32(0, (const char *) buf, len); + if (memcmp((void *) ((size_t) buf + len), &crc, sizeof(crc))) return; + } + if (pkt.eth->type == mg_htons(0x806)) { pkt.arp = (struct arp *) (pkt.eth + 1); if (sizeof(*pkt.eth) + sizeof(*pkt.arp) > pkt.raw.len) return; // Truncated rx_arp(ifp, &pkt); @@ -7971,11 +7981,6 @@ void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp) { } } -size_t mg_tcpip_driver_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) { - (void) buf, (void) len, (void) ifp; - return 0; -} - void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) { // If MAC address is not set, make a random one if (ifp->mac[0] == 0 && ifp->mac[1] == 0 && ifp->mac[2] == 0 && @@ -7990,7 +7995,8 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) { } else { size_t framesize = 1540; ifp->tx.ptr = (char *) calloc(1, framesize), ifp->tx.len = framesize; - ifp->recv_queue.size = ifp->driver->rx ? framesize : 8192; + if (ifp->recv_queue.size == 0) + ifp->recv_queue.size = ifp->driver->rx ? framesize : 8192; ifp->recv_queue.buf = (char *) calloc(1, ifp->recv_queue.size); ifp->timer_1000ms = mg_millis(); mgr->priv = ifp; diff --git a/mongoose.h b/mongoose.h index 6bc4bce9..69496b80 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1514,11 +1514,13 @@ struct mg_tcpip_driver { // Network interface struct mg_tcpip_if { - uint8_t mac[6]; // MAC address. Must be set to a valid MAC - uint32_t ip, mask, gw; // IP address, mask, default gateway - struct mg_str tx; // Output (TX) buffer - bool enable_dhcp_client; // Enable DCHP client - bool enable_dhcp_server; // Enable DCHP server + uint8_t mac[6]; // MAC address. Must be set to a valid MAC + uint32_t ip, mask, gw; // IP address, mask, default gateway + struct mg_str tx; // Output (TX) buffer + bool enable_dhcp_client; // Enable DCHP client + bool enable_dhcp_server; // Enable DCHP server + bool enable_crc32_check; // Do a CRC check on rx frames and strip it + bool enable_mac_check; // Do a MAC check on rx frames struct mg_tcpip_driver *driver; // Low level driver void *driver_data; // Driver-specific data struct mg_mgr *mgr; // Mongoose event manager @@ -1543,9 +1545,6 @@ struct mg_tcpip_if { void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *); void mg_tcpip_free(struct mg_tcpip_if *); void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp); -size_t mg_tcpip_qread(void *buf, struct mg_tcpip_if *ifp); -// conveniency rx function for IRQ-driven drivers -size_t mg_tcpip_driver_rx(void *buf, size_t len, struct mg_tcpip_if *ifp); extern struct mg_tcpip_driver mg_tcpip_driver_stm32; extern struct mg_tcpip_driver mg_tcpip_driver_w5500; diff --git a/src/tcpip/tcpip.c b/src/tcpip/tcpip.c index 5273f608..02330b2c 100644 --- a/src/tcpip/tcpip.c +++ b/src/tcpip/tcpip.c @@ -693,10 +693,16 @@ static void mg_tcpip_rx(struct mg_tcpip_if *ifp, void *buf, size_t len) { pkt.raw.len = len; pkt.eth = (struct eth *) buf; if (pkt.raw.len < sizeof(*pkt.eth)) return; // Truncated - runt? - if (memcmp(pkt.eth->dst, ifp->mac, sizeof(pkt.eth->dst)) != 0 && - memcmp(pkt.eth->dst, broadcast, sizeof(pkt.eth->dst)) != 0) { - // Not for us. Drop silently - } else if (pkt.eth->type == mg_htons(0x806)) { + if (ifp->enable_mac_check && + memcmp(pkt.eth->dst, ifp->mac, sizeof(pkt.eth->dst)) != 0 && + memcmp(pkt.eth->dst, broadcast, sizeof(pkt.eth->dst)) != 0) + return; + if (ifp->enable_crc32_check && len > 4) { + len -= 4; // TODO(scaprile): check on bigendian + uint32_t crc = mg_crc32(0, (const char *) buf, len); + if (memcmp((void *) ((size_t) buf + len), &crc, sizeof(crc))) return; + } + if (pkt.eth->type == mg_htons(0x806)) { pkt.arp = (struct arp *) (pkt.eth + 1); if (sizeof(*pkt.eth) + sizeof(*pkt.arp) > pkt.raw.len) return; // Truncated rx_arp(ifp, &pkt); @@ -797,11 +803,6 @@ void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp) { } } -size_t mg_tcpip_driver_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) { - (void) buf, (void) len, (void) ifp; - return 0; -} - void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) { // If MAC address is not set, make a random one if (ifp->mac[0] == 0 && ifp->mac[1] == 0 && ifp->mac[2] == 0 && @@ -816,7 +817,8 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) { } else { size_t framesize = 1540; ifp->tx.ptr = (char *) calloc(1, framesize), ifp->tx.len = framesize; - ifp->recv_queue.size = ifp->driver->rx ? framesize : 8192; + if (ifp->recv_queue.size == 0) + ifp->recv_queue.size = ifp->driver->rx ? framesize : 8192; ifp->recv_queue.buf = (char *) calloc(1, ifp->recv_queue.size); ifp->timer_1000ms = mg_millis(); mgr->priv = ifp; diff --git a/src/tcpip/tcpip.h b/src/tcpip/tcpip.h index 371faed4..677dba60 100644 --- a/src/tcpip/tcpip.h +++ b/src/tcpip/tcpip.h @@ -15,11 +15,13 @@ struct mg_tcpip_driver { // Network interface struct mg_tcpip_if { - uint8_t mac[6]; // MAC address. Must be set to a valid MAC - uint32_t ip, mask, gw; // IP address, mask, default gateway - struct mg_str tx; // Output (TX) buffer - bool enable_dhcp_client; // Enable DCHP client - bool enable_dhcp_server; // Enable DCHP server + uint8_t mac[6]; // MAC address. Must be set to a valid MAC + uint32_t ip, mask, gw; // IP address, mask, default gateway + struct mg_str tx; // Output (TX) buffer + bool enable_dhcp_client; // Enable DCHP client + bool enable_dhcp_server; // Enable DCHP server + bool enable_crc32_check; // Do a CRC check on rx frames and strip it + bool enable_mac_check; // Do a MAC check on rx frames struct mg_tcpip_driver *driver; // Low level driver void *driver_data; // Driver-specific data struct mg_mgr *mgr; // Mongoose event manager @@ -44,9 +46,6 @@ struct mg_tcpip_if { void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *); void mg_tcpip_free(struct mg_tcpip_if *); void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp); -size_t mg_tcpip_qread(void *buf, struct mg_tcpip_if *ifp); -// conveniency rx function for IRQ-driven drivers -size_t mg_tcpip_driver_rx(void *buf, size_t len, struct mg_tcpip_if *ifp); extern struct mg_tcpip_driver mg_tcpip_driver_stm32; extern struct mg_tcpip_driver mg_tcpip_driver_w5500; @@ -64,8 +63,8 @@ struct mg_tcpip_spi { #if MG_ENABLE_TCPIP #if !defined(MG_ENABLE_DRIVER_STM32H) && !defined(MG_ENABLE_DRIVER_TM4C) - #define MG_ENABLE_DRIVER_STM32 1 +#define MG_ENABLE_DRIVER_STM32 1 #else - #define MG_ENABLE_DRIVER_STM32 0 +#define MG_ENABLE_DRIVER_STM32 0 #endif #endif diff --git a/src/util.c b/src/util.c index 6f7f5c0c..e2d44730 100644 --- a/src/util.c +++ b/src/util.c @@ -48,11 +48,16 @@ uint16_t mg_ntohs(uint16_t net) { } uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len) { - int i; + static const uint32_t crclut[16] = { + // table for polynomial 0xEDB88320 (reflected) + 0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC, 0x76DC4190, 0x6B6B51F4, + 0x4DB26158, 0x5005713C, 0xEDB88320, 0xF00F9344, 0xD6D6A3E8, 0xCB61B38C, + 0x9B64C2B0, 0x86D3D2D4, 0xA00AE278, 0xBDBDF21C}; crc = ~crc; while (len--) { - crc ^= *(unsigned char *) buf++; - for (i = 0; i < 8; i++) crc = crc & 1 ? (crc >> 1) ^ 0xedb88320 : crc >> 1; + uint8_t byte = *(uint8_t *)buf++; + crc = crclut[(crc ^ byte) & 0x0F] ^ (crc >> 4); + crc = crclut[(crc ^ (byte >> 4)) & 0x0F] ^ (crc >> 4); } return ~crc; } @@ -130,4 +135,3 @@ uint64_t mg_millis(void) { #endif } #endif - From 38b129de667b29ff4e344ce3221f0ae6f7326d8c Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Tue, 28 Feb 2023 14:04:02 -0300 Subject: [PATCH 5/7] Test some examples on Windows closes #2047 --- .github/workflows/test.yml | 28 +++++++++++++++++++++------- Makefile | 6 ++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 756f00e3..e50ac83e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -52,13 +52,6 @@ jobs: run: sudo apt -y update ; sudo apt -y install libmbedtls-dev valgrind - if: ${{ env.GO == 1 }} run: make unamalgamated && make valgrind && make mg_prefix - examples: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - run: sudo apt -y install libmbedtls-dev libpcap-dev - - run: make clean examples - - run: make clean test MG_ENABLE_POLL=1 macos: runs-on: macos-latest env: { HOMEBREW_NO_AUTO_UPDATE: 1 } @@ -101,6 +94,27 @@ jobs: - uses: actions/checkout@v3 - run: make riscv + examples: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - run: sudo apt -y install libmbedtls-dev libpcap-dev + - run: make clean examples + - run: make clean test MG_ENABLE_POLL=1 + examples_win: + runs-on: windows-latest + steps: + - uses: actions/checkout@v3 +# - uses: egor-tensin/setup-mingw@v2 +# with: +# platform: x64 + - name: test they build + run: make examples_win + shell: cmd + - name: test they clean + run: make clean_examples_win + shell: cmd + test_f7: runs-on: ubuntu-latest steps: diff --git a/Makefile b/Makefile index dd954698..1497021f 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ IPV6 ?= 1 ASAN ?= -fsanitize=address,undefined,alignment -fno-sanitize-recover=all -fno-omit-frame-pointer -fno-common ASAN_OPTIONS ?= detect_leaks=1 EXAMPLES := $(dir $(wildcard examples/*/Makefile)) +EXAMPLES_WIN := $(dir $(wildcard examples/device-dashboard/Makefile) $(wildcard examples/file-*/Makefile) $(wildcard examples/http-*/Makefile) $(wildcard examples/mqtt-*/Makefile) $(wildcard examples/websocket-*/Makefile) $(wildcard examples/webui-*/Makefile)) PREFIX ?= /usr/local VERSION ?= $(shell cut -d'"' -f2 src/version.h) COMMON_CFLAGS ?= $(C_WARN) $(WARN) $(INCS) $(DEFS) -DMG_ENABLE_IPV6=$(IPV6) $(TFLAGS) -pthread @@ -57,6 +58,11 @@ mip_tap_test: test/mip_tap_test.c mongoose.c mongoose.h Makefile examples: @for X in $(EXAMPLES); do test -f $$X/Makefile || continue; $(MAKE) -C $$X example || exit 1; done +examples_win: + $(foreach X, $(EXAMPLES_WIN), $(MAKE) -C $(X) example &) +clean_examples_win: + $(foreach X, $(EXAMPLES_WIN), $(MAKE) -C $(X) clean &) + test/packed_fs.c: Makefile src/ssi.h test/fuzz.c test/data/a.txt $(CC) $(CFLAGS) test/pack.c -o pack $(RUN) ./pack Makefile src/ssi.h test/fuzz.c test/data/a.txt test/data/range.txt > $@ From 9ba20304c2dd394c0aa45f84a799d575b5ee18cf Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Tue, 28 Feb 2023 14:14:57 -0300 Subject: [PATCH 6/7] Update test.yml --- .github/workflows/test.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e50ac83e..514f9a82 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -114,6 +114,13 @@ jobs: - name: test they clean run: make clean_examples_win shell: cmd + examples_mac: + runs-on: macos-latest + env: { HOMEBREW_NO_AUTO_UPDATE: 1 } + steps: + - uses: actions/checkout@v3 + - run: make examples_mac + - run: make clean_examples_mac test_f7: runs-on: ubuntu-latest From 873e4d6547efa6b17a1f6f68371711f0a35c2901 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Tue, 28 Feb 2023 14:18:39 -0300 Subject: [PATCH 7/7] Update Makefile --- Makefile | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 1497021f..957f5e1a 100644 --- a/Makefile +++ b/Makefile @@ -13,14 +13,16 @@ IPV6 ?= 1 ASAN ?= -fsanitize=address,undefined,alignment -fno-sanitize-recover=all -fno-omit-frame-pointer -fno-common ASAN_OPTIONS ?= detect_leaks=1 EXAMPLES := $(dir $(wildcard examples/*/Makefile)) +EXAMPLES_MAC := $(filter-out examples/mip-pcap/ examples/mip-tap/, $(EXAMPLES)) EXAMPLES_WIN := $(dir $(wildcard examples/device-dashboard/Makefile) $(wildcard examples/file-*/Makefile) $(wildcard examples/http-*/Makefile) $(wildcard examples/mqtt-*/Makefile) $(wildcard examples/websocket-*/Makefile) $(wildcard examples/webui-*/Makefile)) +EXAMPLES_EMBEDDED := $(filter-out $(wildcard examples/zephyr/*/), $(dir $(wildcard examples/*/*/Makefile))) PREFIX ?= /usr/local VERSION ?= $(shell cut -d'"' -f2 src/version.h) COMMON_CFLAGS ?= $(C_WARN) $(WARN) $(INCS) $(DEFS) -DMG_ENABLE_IPV6=$(IPV6) $(TFLAGS) -pthread CFLAGS ?= $(OPTS) $(ASAN) $(COMMON_CFLAGS) VALGRIND_CFLAGS ?= $(OPTS) $(COMMON_CFLAGS) VALGRIND_RUN ?= valgrind --tool=memcheck --gen-suppressions=all --leak-check=full --show-leak-kinds=all --leak-resolution=high --track-origins=yes --error-exitcode=1 --exit-on-first-error=yes --fair-sched=yes -.PHONY: examples test valgrind mip_test +.PHONY: clean_examples examples mip_test test valgrind ifeq "$(findstring ++,$(CC))" "" # $(CC) does not end with ++, i.e. we're using C. Apply C flags @@ -57,6 +59,13 @@ mip_tap_test: test/mip_tap_test.c mongoose.c mongoose.h Makefile examples: @for X in $(EXAMPLES); do test -f $$X/Makefile || continue; $(MAKE) -C $$X example || exit 1; done +clean_examples: + for X in $(EXAMPLES); do test -f $$X/Makefile || continue; $(MAKE) -C $$X clean || exit 1; done + +examples_mac: + for X in $(EXAMPLES_MAC); do test -f $$X/Makefile || continue; $(MAKE) -C $$X example || exit 1; done +clean_examples_mac: + for X in $(EXAMPLES_MAC); do test -f $$X/Makefile || continue; $(MAKE) -C $$X clean || exit 1; done examples_win: $(foreach X, $(EXAMPLES_WIN), $(MAKE) -C $(X) example &) @@ -187,6 +196,11 @@ mongoose.c: Makefile $(wildcard src/*.c) $(wildcard src/tcpip/*.c) mongoose.h: $(HDRS) Makefile (cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/net_*.h src/config.h src/str.h src/queue.h src/fmt.h src/log.h src/timer.h src/fs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/json.h src/rpc.h src/tcpip/tcpip.h src/tcpip/driver_*.h | sed -e '/keep/! s,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@ -clean: + +clean: clean_examples clean_embedded rm -rf $(PROG) *.exe *.o *.dSYM *_test* ut fuzzer *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb slow-unit* _CL_* infer-out data.txt crash-* test/packed_fs.c pack arduino tmp - find examples -maxdepth 3 -name zephyr -prune -o -name Makefile -print | xargs dirname | xargs -n1 make clean -C + #find examples -maxdepth 3 -name zephyr -prune -o -name Makefile -print | xargs dirname | xargs -n1 make clean -C + +clean_embedded: + for X in $(EXAMPLES_EMBEDDED); do test -f $$X/Makefile || continue; $(MAKE) -C $$X clean || exit 1; done +