Embedded TCP/IP stack for microcontrollers
Mongoose has its own TCP/IP stack, with network drivers, written for
microcontrollers. It runs on bare metal or under any RTOS and needs no lwIP and
no vendor middleware. If you need an embedded TCP/IP stack
for STM32, NXP, RP2350 or a similar MCU, and the device also has to serve a
web page, talk MQTT or take firmware updates, Mongoose gives you all of that
in one library: two files, mongoose.c and mongoose.h.
We wrote our own stack because most products we saw spent more time gluing lwIP, a vendor Ethernet driver, mbedTLS and an HTTP server together than on the product itself. With Mongoose the driver, the stack, TLS 1.3, HTTP, WebSocket, MQTT and OTA share one event loop and one config file.
What it implements
Bottom to top, layer by layer:
| Layer | What Mongoose provides |
|---|---|
| Drivers | Ethernet (on-chip MACs, W5500, W5100), Wi-Fi (CYW43, ST67W6, RW612), cellular modems over PPP, IEEE 802.15.4 radios |
| Network | ARP, IPv4, IPv6, ICMP |
| Transport | TCP, UDP |
| Services | DHCP client and server, async DNS, SNTP |
| Security | TLS 1.3 |
| Application | HTTP, WebSocket, MQTT, file serving, firmware OTA, see embedded web server |
The built-in TCP/IP stack is optional. Mongoose can also run on any stack with a BSD socket API, such as lwIP or Zephyr, and on macOS, Windows, Linux, Android and iOS. The same goes for TLS: use the built-in TLS 1.3, or run on top of OpenSSL, mbedTLS or wolfSSL.
Enabling the stack
You turn the stack on in mongoose_config.h. On STM32Cube projects,
MG_ARCH_CUBE is enough: it enables the stack and selects the Ethernet driver
for your STM32 family:
#define MG_ARCH MG_ARCH_CUBE // Also enables the built-in stack and Ethernet driver
On other platforms, enable the stack and its driver explicitly. For example,
here is the mongoose_config.h snippet for the NXP FRDM-RT1186:
#define MG_ARCH MG_ARCH_ARMGCC
#define MG_ENABLE_TCPIP 1 // Enable built-in TCP/IP stack
#define MG_ENABLE_DRIVER_NETC 1 // Enable built-in RT1186 Ethernet driver
mg_mgr_init() starts the driver and the network interface. Then run the
usual poll loop. That's all the networking code you need:
#include "mongoose.h"
int main(void) {
INIT_HARDWARE(); // Board-specific: clocks, pins, UART
struct mg_mgr mgr;
mg_mgr_init(&mgr);
for (;;) {
mg_mgr_poll(&mgr, 1);
}
return 0;
}
Network drivers
Mongoose ships drivers for on-chip Ethernet MACs, SPI Ethernet chips, Wi-Fi
modules, cellular modems over PPP and IEEE 802.15.4 radios. They all plug into
the same struct mg_tcpip_if, so switching from Ethernet to Wi-Fi doesn't
touch your application code.
| Platform | Connectivity |
|---|---|
| STM32F, STM32H5, STM32H7, STM32N6, STM32C5 | On-chip Ethernet |
| NXP i.MX RT10xx, RT11xx, RT118x, MCXN, MCXE | On-chip Ethernet |
| Renesas RA6 and RA8, Microchip SAM E54, Infineon XMC, TI TM4C and TMS570 | On-chip Ethernet |
| NXP RW612 | Ethernet, Wi-Fi, IEEE 802.15.4 |
| RP2040, RP2350, STM32, and other MCUs | CYW43 Wi-Fi over SPI or SDIO |
| STM32 and other MCUs | ST67W6 Wi-Fi over SPI |
| Any MCU with SPI | W5500, W5100 Ethernet |
| Any MCU connected to a modem | Cellular modems |
| NXP MCXW71 and RW612, Silicon Labs MG24, Nordic nRF52 | IEEE 802.15.4 radio |
Some boards need pin, PHY or SDK setup for their driver. That stays in the driver; IP configuration and your application don't see it.
IPv4 configuration
By default the interface uses DHCP. It sends its hostname and gets back an address, netmask, gateway, DNS server and, if the server offers one, an SNTP server. Then it resolves the gateway's MAC with ARP and reports itself ready.
For a static address, set MG_TCPIP_IP, MG_TCPIP_MASK and MG_TCPIP_GW.
Your code gets MG_TCPIP_EV_STATE_CHANGE as the interface goes from link down
to link up, DHCP, IP assigned and ready. Per-interface counters track
received, sent and dropped frames, which helps a lot when debugging in the
field.
IPv6 configuration
Set MG_ENABLE_IPV6=1 to run IPv6 next to IPv4. The stack handles link-local
and global addresses, router advertisements and SLAAC.
To fix the addresses at build time, use MG_TCPIP_IPV6_LINKLOCAL,
MG_TCPIP_GLOBAL, MG_TCPIP_PREFIX_LEN and MG_TCPIP_GW6. IPv6 state
changes come as MG_TCPIP_EV_STATE6_CHANGE.
TCP, UDP, DNS and time
Raw TCP and UDP go through the normal Mongoose API: mg_connect() for a
client, mg_listen() for a server. mg_http_listen() and mg_mqtt_connect()
sit on the same event manager, and one handler function gets the connect,
read, write and close events for all of them.
The DNS resolver uses the server from DHCP or your config. If DHCP hands out
an SNTP server, you get MG_TCPIP_EV_DHCP_SNTP and can pass it to the SNTP
client. Networking, protocols and your own timers all run in one loop, so
there is no task per protocol to size and synchronise.
Bare metal and RTOS
On bare metal, call mg_mgr_poll() from the main loop. Each call runs the
stack timers, processes received frames, sends what is queued and delivers
events to your handlers. There is no scheduler.
Under FreeRTOS, Zephyr, ThreadX, CMSIS-RTOS, RT-Thread or TI-RTOS, run the same loop in one task and make all Mongoose calls from that task. Drivers that receive in an interrupt only queue the frame; the TCP/IP processing happens in the poll loop, not in the ISR.
Memory footprint
We measured a bare-metal STM32H723 build with the built-in Ethernet driver,
using arm-none-eabi-gcc -Os and newlib-nano:
| Configuration | Flash | RAM |
|---|---|---|
| TCP/IP stack + HTTP server | about 36 KB | about 10 KB, plus about 1.6 KB per connection |
| Same, with built-in TLS 1.3 | about 82 KB | about 10 KB, plus about 14 KB per TLS connection |
| Ethernet DMA buffers | - | about 12 KB |
The STM32 TCP/IP footprint article shows how we measured, so you can repeat it on your own board. Your numbers will move with the driver, compiler, enabled protocols and buffer sizes, all of which are compile-time options.
All allocations go through mg_calloc() and mg_free(). Point them at a
fixed-size pool if you want network memory kept apart from the rest of the
firmware.
One hardware gotcha: on MCUs with split RAM, the Ethernet DMA descriptors and buffers must live in a region the DMA can reach. STM32H7 is the classic case. That is a linker script fix, not a stack setting.
BSD socket API
If you have existing socket code, set MG_ENABLE_BSD_SOCKETS=1. Mongoose then
provides socket(), bind(), listen(), accept(), connect(), send(),
recv(), select(), poll() and the usual address helpers on top of the
built-in stack. Mongoose does not provide header files like arpa/inet.h and
such, including mongoose.h is sufficient.
This is meant for porting. For new code the native event API is shorter: no blocking calls, and HTTP, WebSocket, MQTT, TLS and timers come with it.
Using Mongoose on top of another stack
You don't have to use the built-in stack. Mongoose also runs on any BSD socket stack: lwIP with sockets enabled, Zephyr, FreeRTOS+TCP, and the native sockets on Linux, macOS and Windows.
If your product already depends on another stack, for example because it was certified with it, keep that stack and use Mongoose above it for HTTP, WebSocket, MQTT, TLS and OTA. Move to the built-in stack when you would rather have one networking library than a vendor stack plus glue.
How it compares to other embedded TCP/IP stacks
lwIP is in almost every MCU SDK, but it is only the TCP/IP layer. In a typical product the chip vendor provides the Ethernet driver, mbedTLS provides TLS, and the team writes or ports HTTP, MQTT and OTA, plus the glue between them. lwIP is very configurable, which also means a lot of low-level options to get right. Mongoose has a few options, each named for what it does.
FreeRTOS+TCP, Zephyr and NetX Duo are each tied to their own RTOS. CycloneTCP covers more layers, but as separate components.
| Stack | License | Drivers | Bare metal | RTOS support | TLS | HTTP, WebSocket, MQTT | Firmware OTA |
|---|---|---|---|---|---|---|---|
| lwIP | BSD | No | Yes | Any | External (mbedTLS) | HTTP, MQTT; no WebSocket | External |
| FreeRTOS+TCP | MIT | Yes | No | FreeRTOS | External (mbedTLS) | Separate libraries | External |
| Zephyr | Apache 2.0 | Yes | No | Zephyr | Included (mbedTLS) | Yes | Included component |
| NetX Duo | MIT | No | No | ThreadX | Add-on (NetX Secure) | Add-ons | External |
| CycloneTCP | GPLv2 or commercial | Yes | Yes | Any | Add-on (CycloneSSL) | Yes | CycloneOTA component |
| Mongoose | GPLv2 or commercial | Yes | Yes | Any | Built-in | Built-in | Built-in |
The difference is how much you integrate yourself. With Mongoose the driver, stack, protocols and TLS come from one codebase, with one event manager and one config file. For a stack-by-stack breakdown of footprint, security and licensing, see the full embedded TCP/IP stack comparison.
When to choose Mongoose, and when not to
Mongoose is a good fit when:
- you start a new product on an MCU and want one networking library, with direct support from the people who wrote it
- the device needs more than TCP/IP: a web dashboard, MQTT, TLS or OTA
- you run bare metal, or want to stay free to change the RTOS later
- you want AI coding tools to get it right: the whole API is in
mongoose.h, which fits in one context window - security matters and you want a stack that is maintained full time: Mongoose is fuzzed continuously with OSS-Fuzz, reports are coordinated with security groups, and commercial customers are notified before a CVE is published (see security)
Look elsewhere when:
- the product is already certified or validated with lwIP or another stack; you can still use Mongoose on top of it for the application protocols
- the product is built on Zephyr and you want to stay with Zephyr's native network stack
- you need a permissive license and cannot use GPLv2 or buy a commercial license
Mongoose itself has been in production since 2004 and is used by hundreds of companies, including NASA on the International Space Station.
Getting started
- Pick the closest board tutorial from the supported platforms list.
- Enable
MG_ENABLE_TCPIPand the driver for your MAC or module. - Add your protocol, an HTTP server or MQTT client, on the same manager.
For a step-by-step walkthrough, follow the STM32 TCP/IP stack guide or the RP2040 and RP2350 TCP/IP stack guide. For a side-by-side integration with lwIP, see lwIP vs Mongoose.
FAQ
Which TCP/IP stack should I use on STM32?
For a new STM32 product that needs a web UI, MQTT, TLS or OTA, Mongoose's
built-in stack is the shortest route: STM32Cube projects only need
MG_ARCH_CUBE, and the Ethernet driver is included. If your project already
uses the lwIP that STM32Cube generates, you can keep it and run Mongoose on
top of it.
Can Mongoose replace lwIP?
Yes. The built-in stack covers what lwIP does on a typical MCU product, ARP, IPv4, IPv6, TCP, UDP, DHCP and DNS, and adds the drivers and the application protocols. Mongoose can also run on top of lwIP if you would rather keep it.
Does the Mongoose TCP/IP stack need an RTOS?
No. It runs on bare metal from a single poll loop. It also works inside one task under FreeRTOS, Zephyr, ThreadX and other RTOSes.
How much flash and RAM does it need?
On a bare-metal STM32H723, the stack with an HTTP server takes about 36 KB of flash and 10 KB of RAM, plus about 1.6 KB per connection. With TLS 1.3 it is about 82 KB of flash and about 14 KB of RAM per TLS connection.
What license is it under?
GPLv2, or a commercial license from Cesanta for closed-source products. See licensing.