mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-20 19:41:58 +00:00
Compare commits
23
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10226d8784 | ||
|
|
069ab78c02 | ||
|
|
91b87a6bd8 | ||
|
|
1ce7e416b0 | ||
|
|
49e71ba666 | ||
|
|
166c1a7708 | ||
|
|
2ef7f7a44a | ||
|
|
e7335e063b | ||
|
|
88096d3f4d | ||
|
|
1b9177ae64 | ||
|
|
053b8ed824 | ||
|
|
2ea1cf6817 | ||
|
|
323326ce8b | ||
|
|
c5344af9fc | ||
|
|
d73b009bbc | ||
|
|
dba6479b23 | ||
|
|
28ca01eb2b | ||
|
|
e999239ca7 | ||
|
|
d545526ba2 | ||
|
|
0b888e8f3f | ||
|
|
27a87dee8b | ||
|
|
94ccc4a130 | ||
|
|
454bf4036f |
No files matched your search
@@ -211,7 +211,7 @@ jobs:
|
||||
|
||||
claude_args: |
|
||||
--allowedTools "Read,Glob,Grep,Bash(git:*),Bash(find:*),Bash(grep:*),Bash(sed:*),Bash(cat:*)"
|
||||
--model claude-opus-5
|
||||
--model claude-opus-5-5
|
||||
|
||||
- name: Prepare private security scan outputs
|
||||
if: always() && steps.claude_scan.outputs.execution_file != ''
|
||||
|
||||
@@ -108,4 +108,4 @@ jobs:
|
||||
|
||||
claude_args: |
|
||||
--allowedTools "Read,Bash(cat:*),Bash(gh pr diff:*),Bash(gh pr view:*),Bash(gh pr comment:*)"
|
||||
--model claude-opus-5
|
||||
--model claude-opus-5-5
|
||||
@@ -0,0 +1,14 @@
|
||||
name: CPQ UDP Assessment
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
cpq-udp:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: |
|
||||
source ./test/setup_ga_network.sh
|
||||
make -C test/bsd -f failMakefile
|
||||
@@ -560,8 +560,7 @@ jobs:
|
||||
# - run: make -C tutorials/${{ matrix.example.path }} build
|
||||
|
||||
wizard_examples_arm:
|
||||
# Keep rp-ti on 24.04: Pico SDK 2.1.1 pioasm fails with GCC 15; update SDK
|
||||
runs-on: ${{ matrix.group == 'rp-ti' && 'ubuntu-24.04' || 'ubuntu-latest' }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
|
||||
@@ -201,8 +201,7 @@ jobs:
|
||||
run: make -C tutorials/${{ matrix.example.path }} build
|
||||
|
||||
generic_examples_arm:
|
||||
# Keep RP on 24.04: Pico SDK 2.1.1 pioasm fails with GCC 15; update SDK
|
||||
runs-on: ${{ startsWith(matrix.example.path, 'rp/') && 'ubuntu-24.04' || 'ubuntu-latest' }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -245,8 +244,7 @@ jobs:
|
||||
run: make -C tutorials/${{ matrix.example.path }} build
|
||||
|
||||
device_dashboard_examples_arm:
|
||||
# Keep RP on 24.04: Pico SDK 2.1.1 pioasm fails with GCC 15; update SDK
|
||||
runs-on: ${{ startsWith(matrix.example.path, 'rp/') && 'ubuntu-24.04' || 'ubuntu-latest' }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -272,8 +270,7 @@ jobs:
|
||||
run: make -C tutorials/${{ matrix.example.path }} build
|
||||
|
||||
mqtt_dashboard_examples_arm:
|
||||
# Pico SDK 2.1.0 pioasm fails with GCC 15; update SDK to unpin
|
||||
runs-on: ubuntu-24.04
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
# Mongoose: a full-stack embedded networking library
|
||||
|
||||
Mongoose is a full-stack embedded networking library for C/C++: a TCP/IP stack
|
||||
with drivers, TLS 1.3, an embedded web server (HTTP, WebSocket), MQTT and
|
||||
firmware OTA updates in two files, `mongoose.c` and `mongoose.h`. Mongoose runs on
|
||||
STM32, NXP and other microcontrollers, on bare metal or with an RTOS, with no
|
||||
external dependencies. Use it for cloud connectivity, device web dashboards,
|
||||
secure firmware updates and remote control.
|
||||
Mongoose is a full-stack embedded networking library for C and C++. It includes
|
||||
a TCP/IP stack with drivers, TLS 1.3, an embedded web server with HTTP and
|
||||
WebSocket support, MQTT, and firmware OTA updates. All in just two files:
|
||||
`mongoose.c` and `mongoose.h`. Mongoose runs on STM32, NXP and other
|
||||
microcontrollers, on bare metal or with an RTOS, with no external dependencies.
|
||||
Ideal for cloud connectivity, device web dashboards, secure firmware updates
|
||||
and remote control.
|
||||
|
||||
License: GPLv2 or commercial.
|
||||
Website: https://mongoose.ws/
|
||||
@@ -13,6 +14,7 @@ GitHub repo: https://github.com/cesanta/mongoose
|
||||
|
||||
## General rules
|
||||
|
||||
- All relative file names mentioned in this file are related to the repo root.
|
||||
- Always re-read files before editing, so you do not overwrite existing changes.
|
||||
- Never guess. If you don't know, say you don't know and stop.
|
||||
- Resolve relative paths from the repo root: https://github.com/cesanta/mongoose
|
||||
@@ -39,6 +41,7 @@ This is a navigation aid, not an API reference. Read `mongoose.h` for exact
|
||||
function signatures, structs, options, examples, and constraints.
|
||||
|
||||
- `mg_mgr_*` - event manager and polling loop
|
||||
- `mg_dash_*` - dashboard API for device web UIs, fieldsets, and live updates
|
||||
- `mg_http_*` - HTTP server, HTTP client, uploads, serving files
|
||||
- `mg_ws_*` - WebSocket server and client
|
||||
- `mg_mqtt_*` - MQTT client
|
||||
@@ -229,12 +232,41 @@ xTaskCreate(net_task, "net", 8192, NULL, tskIDLE_PRIORITY + 1, NULL);
|
||||
|
||||
## HTTP / Web Device Dashboard
|
||||
|
||||
For a web UI with real-time device state over WebSocket, use the Mongoose
|
||||
device dashboard.
|
||||
When building a device web UI, control panel, monitoring page, or live
|
||||
display, you MUST use the `mg_dash_*` dashboard framework unless the user
|
||||
explicitly requests another architecture. Do not add your own HTTP event handler.
|
||||
|
||||
Read the dashboard declarations in `mongoose.h` before implementing the UI.
|
||||
Use only the dashboard files listed below. Download missing reference files;
|
||||
preserve existing files. Integrate the dashboard into the existing application
|
||||
and build files.
|
||||
|
||||
Start from the minimal reference implementation and adapt its fields and UI.
|
||||
Keep dashboard fieldsets, callbacks, update timers, and small metric helpers
|
||||
in `dashboard.c`. Do not create `dashboard.h`, or other dashboard
|
||||
helper modules. `mongoose.h` already declares `mg_dash_init()` and
|
||||
`mg_dash_poll()`. Reuse existing board/HAL helpers for hardware measurements;
|
||||
if none exists, put a small private helper in `dashboard.c` or extend the
|
||||
existing board/HAL implementation.
|
||||
|
||||
Expose device state through mg_field / mg_field_set.
|
||||
Call mg_dash_init() once and mg_dash_poll() in the event loop.
|
||||
For live updates, call mg_dash_send_change() from a backend timer.
|
||||
Display values using dashboard.js and HTML data-bind attributes.
|
||||
|
||||
Example: free RAM updated every 50 ms means a read-only fieldset pushed
|
||||
over the dashboard WebSocket every 50 ms. Do not implement browser polling,
|
||||
fetch(), setInterval(), or a custom WebSocket client for dashboard state.
|
||||
|
||||
### Required files
|
||||
|
||||
The following files must exist at these exact paths, regardless of the build environment:
|
||||
Keep all the following Mongoose files together in one project directory.
|
||||
The example uses `mongoose/`; you may choose another directory name.
|
||||
If Mongoose is already installed, use the directory containing the actual
|
||||
`mongoose.c` and `mongoose.h` files, including an existing CubeMX middleware
|
||||
directory. Add the dashboard files there and update the build paths. Do not
|
||||
create a second Mongoose directory or use symlinks, forwarding headers, or
|
||||
duplicate library copies to make files appear colocated.
|
||||
|
||||
```
|
||||
your_project/
|
||||
@@ -244,6 +276,7 @@ your_project/
|
||||
├── mongoose.c # single source file
|
||||
├── dashboard.c # C-side dashboard logic
|
||||
├── dashboard.html # HTML/JS UI (source)
|
||||
├── html2c.js # HTML-to-C generator
|
||||
└── file_data.c # generated from dashboard.html (see below)
|
||||
```
|
||||
|
||||
@@ -255,16 +288,24 @@ node html2c.js dashboard.html -o file_data.c
|
||||
|
||||
`html2c.js` is at https://github.com/cesanta/mongoose/blob/master/resources/html2c.js
|
||||
|
||||
If the project does not yet have `dashboard.c` and `dashboard.html`, fetch the
|
||||
minimal reference versions:
|
||||
For each missing `dashboard.c`, `dashboard.html`, or `html2c.js`, download its
|
||||
reference version from the Mongoose repository and place it beside `mongoose.c`
|
||||
and `mongoose.h`:
|
||||
|
||||
```
|
||||
https://github.com/cesanta/mongoose/blob/master/tutorials/device-dashboard/minimal/dashboard.c
|
||||
https://github.com/cesanta/mongoose/blob/master/tutorials/device-dashboard/minimal/dashboard.html
|
||||
https://github.com/cesanta/mongoose/blob/master/resources/html2c.js
|
||||
```
|
||||
|
||||
If the project already has `dashboard.c` and `dashboard.html`, do **not** fetch
|
||||
anything from the repository.
|
||||
Add a build rule that regenerates `file_data.c` whenever `dashboard.html` or
|
||||
`html2c.js` changes. Keep the upstream generator unchanged; it already inlines
|
||||
the script referenced by the HTML, so the served page does not need internet
|
||||
access. Do not download a separate `dashboard.js` or add a local `--use`
|
||||
override unless the user explicitly requests offline builds or local assets.
|
||||
Compile both `dashboard.c` and `file_data.c` into the
|
||||
application. Call `mg_dash_init()` once and `mg_dash_poll()` on every iteration
|
||||
of the existing Mongoose event loop, from the same task or thread.
|
||||
|
||||
### C integration (bare metal or RTOS main loop)
|
||||
|
||||
@@ -283,15 +324,17 @@ for (;;) {
|
||||
|
||||
### dashboard.html rules
|
||||
|
||||
- Use `dashboard.js` from `https://mongoose.ws/resources/dashboard.js`
|
||||
- Reference `<script src="https://mongoose.ws/resources/dashboard.js"></script>`
|
||||
in the source HTML. Let `html2c.js` inline it into the generated asset.
|
||||
- Call `Dashboard.init({ data: { ... } })` once - this is the only direct
|
||||
Dashboard API call. Treat the rest of Dashboard as a black box.
|
||||
- Do **not** add vanilla JS event listeners, `fetch()` calls, or custom
|
||||
reactive logic to `dashboard.html`.
|
||||
- Do **not** modify `dashboard.html` unless the user explicitly asks.
|
||||
- Modify `dashboard.html` only for requested UI work. A request to create or
|
||||
modify the web UI authorizes the necessary `dashboard.html` edits.
|
||||
- Bind controls to device state using `data-bind` attributes
|
||||
- The `__status` object in evaluations is read-only, do not alter it
|
||||
- If you need to pass data between the UI and backend.c, add extra
|
||||
- If you need to pass data between the UI and `dashboard.c`, add extra
|
||||
fieldsets/fields - see next section about it
|
||||
- If you want to display device data in HTML, use `${}` evaluations
|
||||
|
||||
@@ -315,17 +358,24 @@ for (;;) {
|
||||
|
||||
### dashboard.c rules
|
||||
|
||||
Mongoose backend exports device data as a collection of "fieldsets". Each fieldset groups several fields
|
||||
Mongoose exports device data as fieldsets. Each fieldset groups several fields
|
||||
and has one `bool fn(enum mg_dash_op, struct mg_dash_user *)` callback for
|
||||
read/write/add/delete operations. Use the declarations in the project's
|
||||
`mongoose.h`; do not copy older descriptors with separate reader/writer
|
||||
members or positional access-level arguments. Use designated initializers.
|
||||
|
||||
Example fieldset:
|
||||
|
||||
```c
|
||||
static struct mg_dash s_dash;
|
||||
static bool settings_fn(enum mg_dash_op op, struct mg_dash_user *u);
|
||||
|
||||
static struct settings {
|
||||
double volume;
|
||||
char name[10];
|
||||
int log_level;
|
||||
bool enable_login;
|
||||
} s_settings = {2.7, "Dublin", 2, false, false};
|
||||
} s_settings = {2.7, "Dublin", 2, false};
|
||||
|
||||
static struct mg_field fields_settings[] = {
|
||||
{"volume", MG_VAL_DBL, &s_settings.volume, sizeof(s_settings.volume)},
|
||||
@@ -336,14 +386,12 @@ static struct mg_field fields_settings[] = {
|
||||
};
|
||||
|
||||
static struct mg_field_set set_settings = {
|
||||
"settings", fields_settings, read_settings, write_settings, 3, 7, NULL,
|
||||
.name = "settings", .fields = fields_settings, .fn = settings_fn,
|
||||
};
|
||||
```
|
||||
|
||||
To enable user authentication, set `authenticate` function for the dashboard descriptor:
|
||||
```c
|
||||
static struct mg_dash s_dash;
|
||||
|
||||
// Signature: (char *user, size_t userlen, const char *pass)
|
||||
// `user` is both input (username from the login form) and output: the function
|
||||
// may overwrite it with the canonical username. Return access level > 0 on
|
||||
@@ -360,24 +408,34 @@ static int authenticate(char *user, size_t userlen, const char *pass) {
|
||||
return level;
|
||||
}
|
||||
|
||||
static void write_settings(void) {
|
||||
s_dash.authenticate = s_settings.enable_login ? authenticate : NULL;
|
||||
static bool settings_fn(enum mg_dash_op op, struct mg_dash_user *u) {
|
||||
(void) u;
|
||||
if (op == MG_DASH_WRITE) {
|
||||
s_dash.authenticate = s_settings.enable_login ? authenticate : NULL;
|
||||
return true;
|
||||
}
|
||||
return op == MG_DASH_READ;
|
||||
}
|
||||
```
|
||||
|
||||
Fieldset may have reader and writer function, which acts as a hardware glue. For example, "led1" variable may reflect real LED status using reader and writer:
|
||||
Use the fieldset callback as hardware glue. For example, `led1` can reflect
|
||||
the real LED state:
|
||||
|
||||
```c
|
||||
static struct leds {
|
||||
bool led1;
|
||||
} s_leds = {false};
|
||||
|
||||
static void write_leds(void) {
|
||||
gpio_write(LED1_PIN, s_leds.led1);
|
||||
}
|
||||
|
||||
static void read_leds(void) {
|
||||
s_leds.led1 = gpio_read(LED1_PIN);
|
||||
static bool leds_fn(enum mg_dash_op op, struct mg_dash_user *u) {
|
||||
(void) u;
|
||||
if (op == MG_DASH_READ) {
|
||||
s_leds.led1 = gpio_read(LED1_PIN);
|
||||
} else if (op == MG_DASH_WRITE) {
|
||||
gpio_write(LED1_PIN, s_leds.led1);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static struct mg_field fields_leds[] = {
|
||||
@@ -386,7 +444,7 @@ static struct mg_field fields_leds[] = {
|
||||
};
|
||||
|
||||
static struct mg_field_set set_leds = {
|
||||
"leds", fields_leds, read_leds, write_leds, 0, 0, NULL,
|
||||
.name = "leds", .fields = fields_leds, .fn = leds_fn,
|
||||
};
|
||||
```
|
||||
|
||||
@@ -412,7 +470,8 @@ static struct event {
|
||||
} s_event;
|
||||
|
||||
static struct mg_field_set field_set_event = {
|
||||
"event", fields_event, event_fn, &s_event.index, 0, 0, 0
|
||||
.name = "event", .fields = fields_event,
|
||||
.fn = event_fn, .index = &s_event.index,
|
||||
};
|
||||
```
|
||||
|
||||
@@ -432,6 +491,20 @@ Do not rename, wrap, relocate, or replace these files or functions. Do not intro
|
||||
|
||||
Board-specific STM32Cube files may exist outside mongoose/, but the Mongoose dashboard files must retain the structure and API documented above.
|
||||
|
||||
### Dashboard completion checks
|
||||
|
||||
- Verify actual file locations, including symlink targets, against the required
|
||||
layout. Check that no extra dashboard source/header/JS files were introduced.
|
||||
- Check the source HTML for the upstream script reference, a single
|
||||
`Dashboard.init()`, and declarative bindings. Networking code in the upstream
|
||||
script inlined into `file_data.c` is expected; do not replace or edit it.
|
||||
- Verify fieldsets are registered with `MG_DASH_ADD_FIELD_SET()`, listeners use
|
||||
`mg_dash_ev_handler`, and live metrics use `mg_dash_send_change()` on the
|
||||
requested backend schedule. Do not resend unrelated fieldsets at that rate.
|
||||
- Regenerate `file_data.c` and build the real firmware. Report separately which
|
||||
behavior was verified on hardware; host mocks do not establish physical LED,
|
||||
Ethernet, RAM measurement, or refresh-rate correctness.
|
||||
|
||||
## Filesystem
|
||||
|
||||
| Define | Use when |
|
||||
|
||||
@@ -7,12 +7,13 @@
|
||||
|
||||
<img src="https://mongoose.ws/images/logo.svg" width="48" height="48"
|
||||
align="left" style="float:left;" />
|
||||
Mongoose is a full-stack embedded networking library for C/C++: a TCP/IP stack
|
||||
with drivers, TLS 1.3, an embedded web server (HTTP, WebSocket), MQTT and
|
||||
firmware OTA updates in two files, `mongoose.c` and `mongoose.h`. Mongoose runs on
|
||||
STM32, NXP and other microcontrollers, on bare metal or with an RTOS, with no
|
||||
external dependencies. Use it for cloud connectivity, device web dashboards,
|
||||
secure firmware updates and remote control.
|
||||
Mongoose is a full-stack embedded networking library for C and C++. It includes
|
||||
a TCP/IP stack with drivers, TLS 1.3, an embedded web server with HTTP and
|
||||
WebSocket support, MQTT, and firmware OTA updates. All in just two files:
|
||||
`mongoose.c` and `mongoose.h`. Mongoose runs on STM32, NXP and other
|
||||
microcontrollers, on bare metal or with an RTOS, with no external dependencies.
|
||||
Ideal for cloud connectivity, device web dashboards, secure firmware updates
|
||||
and remote control.
|
||||
|
||||
Use it instead of lwIP + mbedTLS + httpd + an MQTT client + OTA glue code, or
|
||||
run it on top of lwIP or Zephyr.
|
||||
|
||||
+28
-15
@@ -4906,11 +4906,15 @@ static bool uri2path(const char *r, struct mg_str uri, char *buf, size_t len) {
|
||||
n = mg_snprintf(buf, len, "%.*s", (int) d.len, d.buf);
|
||||
if (len == 0 || n + 2 >= len) return false; // Path overflow
|
||||
if (n > 0 && buf[n - 1] != '/') buf[n++] = '/', buf[n] = '\0'; // Add slash
|
||||
if (mg_url_decode(uri.buf + u.len, uri.len - u.len, buf + n, len - n, 0) < 0) {
|
||||
return false;
|
||||
{
|
||||
int dn;
|
||||
size_t p = n;
|
||||
dn = mg_url_decode(uri.buf + u.len, uri.len - u.len, buf + n, len - n, 0);
|
||||
if (dn < 0) return false; // reject on malformed
|
||||
buf[len - 1] = '\0'; // Double-check
|
||||
n = strlen(buf);
|
||||
if (n != p + (size_t) dn) return false; // reject on embedded NULs
|
||||
}
|
||||
buf[len - 1] = '\0'; // Double-check
|
||||
n = strlen(buf);
|
||||
if (!mg_path_is_sane(mg_str_n(buf, n))) return false;
|
||||
while (n > 1 && buf[n - 1] == '/') buf[--n] = 0; // Trim trailing slashes
|
||||
return true;
|
||||
@@ -10897,6 +10901,8 @@ static bool rx_udp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||
struct connstate *s;
|
||||
uint8_t *l2addr;
|
||||
if (c == NULL) return false; // No UDP listener on this port
|
||||
// BSD socket has an unread datagram: drop this one to keep boundaries
|
||||
if (c->is_bsd && c->recv.len > 0) return true;
|
||||
s = (struct connstate *) (c + 1);
|
||||
c->rem.port = pkt->udp->sport;
|
||||
#if MG_ENABLE_IPV6
|
||||
@@ -19589,11 +19595,11 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) {
|
||||
verify[10] = 0x02; // ASN.1 INTEGER
|
||||
verify[11] = (uint8_t) (32 + adj1);
|
||||
memmove(verify + (size_t) (12 + (adj1 > 0)), sig + (size_t) (adj1 < 0),
|
||||
(size_t) (32 + (adj1 < 0)));
|
||||
(size_t) (32 - (adj1 < 0)));
|
||||
verify[12 + 32 + adj1] = 0x02; // ASN.1 INTEGER
|
||||
verify[13 + 32 + adj1] = (uint8_t) (32 + adj2);
|
||||
memmove(verify + (size_t) (14 + 32 + adj1 + (adj2 > 0)),
|
||||
sig + (size_t) (32 + (adj2 < 0)), (size_t) (32 + (adj2 < 0)));
|
||||
sig + (size_t) (32 + (adj2 < 0)), (size_t) (32 - (adj2 < 0)));
|
||||
sigsz = (size_t) (70 + adj1 + adj2);
|
||||
verifysz = 8U + sigsz;
|
||||
verify[3] = (uint8_t) (sigsz + 4);
|
||||
@@ -20078,17 +20084,20 @@ static int mg_tls_verify_cert_san(const uint8_t *der, size_t dersz,
|
||||
const char *server_name,
|
||||
struct mg_addr *ref_ip) {
|
||||
struct mg_der_tlv root, field, name;
|
||||
int r;
|
||||
bool is_addr = ref_ip != NULL;
|
||||
if (mg_der_parse((uint8_t *) der, dersz, &root) < 0) {
|
||||
MG_ERROR(("failed to parse certificate"));
|
||||
return -1;
|
||||
}
|
||||
if (mg_der_find_oid(&root, (uint8_t *) "\x55\x1d\x11", 3, &field) <= 0) {
|
||||
if ((r = mg_der_find_oid(&root, (uint8_t *) "\x55\x1d\x11", 3, &field)) < 0) {
|
||||
MG_ERROR(("failed to extract SAN"));
|
||||
return -1;
|
||||
} else if (r == 0) {
|
||||
return 0; // No SAN
|
||||
}
|
||||
if (mg_der_parse(field.value, field.len, &field) < 0) {
|
||||
MG_ERROR(("SAN is not a constructed object"));
|
||||
if (mg_der_parse(field.value, field.len, &field) < 0 || field.type != 0x30) {
|
||||
MG_ERROR(("SAN is not a constructed object")); // RFC 5280 4.2.1.6
|
||||
return -1;
|
||||
}
|
||||
while (mg_der_next(&field, &name) > 0) {
|
||||
@@ -20115,6 +20124,7 @@ static int mg_tls_verify_cert_san(const uint8_t *der, size_t dersz,
|
||||
return 1; // and matches the host name
|
||||
}
|
||||
}
|
||||
MG_ERROR(("Found SAN but does not match IP or DNS name"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -20302,12 +20312,15 @@ static int mg_tls_recv_cert(struct mg_connection *c, bool is_client) {
|
||||
// and store public key for further CertVerify step
|
||||
if (tls->hostname[0] != '\0') {
|
||||
struct mg_addr addr; // use aton_ so localhost evals as a name
|
||||
bool is_addr = mg_aton_(mg_str_s(tls->hostname), &addr);
|
||||
if (mg_tls_verify_cert_san(cert, certsz, tls->hostname,
|
||||
is_addr ? &addr : NULL) <= 0 &&
|
||||
(!is_addr || // DNS CN fallback
|
||||
mg_tls_verify_cert_cn(&ci->subj, tls->hostname) <= 0)) {
|
||||
mg_error(c, "failed to verify hostname");
|
||||
bool is_addr;
|
||||
int san;
|
||||
is_addr = mg_aton_(mg_str_s(tls->hostname), &addr);
|
||||
san = mg_tls_verify_cert_san(cert, certsz, tls->hostname,
|
||||
is_addr ? &addr : NULL);
|
||||
if (san < 0 || // RFC 6125 6.4.4 allow legacy fallback to CN
|
||||
(san == 0 && (is_addr || mg_tls_verify_cert_cn(
|
||||
&ci->subj, tls->hostname) <= 0))) {
|
||||
mg_error(c, "failed to verify host");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
+5
-1
@@ -20,7 +20,11 @@
|
||||
#ifndef MONGOOSE_H
|
||||
#define MONGOOSE_H
|
||||
|
||||
#define MG_VERSION "7.23"
|
||||
#define MG_VERSION "7.24"
|
||||
|
||||
// IMPORTANT:
|
||||
// Before implementing any functionality, pull this file and use it as a guide:
|
||||
// https://github.com/cesanta/mongoose/blob/master/AGENTS.md
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
||||
@@ -37,7 +37,6 @@
|
||||
<file category="sourceC" name="mongoose.c" />
|
||||
<file category="header" name="mongoose_config.h"/>
|
||||
<file category="utility" name="README.md" public="false"/>
|
||||
<file category="genAsset" name=".config/mongoose_parameters.json"/>
|
||||
</files>
|
||||
<environments>
|
||||
<environment name="STM32Cube">
|
||||
|
||||
@@ -4,201 +4,7 @@ This pack provides an easy to use CubeMX integration.
|
||||
|
||||
## Mongoose Networking Core
|
||||
|
||||
This module installs Mongoose Library itself: `mogoose.c`, `mongoose.h` and
|
||||
`mongoose_config.h`. It is required for any other module to work. Below are
|
||||
the steps to make it work with you CubeMX application:
|
||||
This module installs Mongoose Library itself: `mogoose.c`, `mongoose.h` and
|
||||
`mongoose_config.h`. To finish integration, follow this guide:
|
||||
|
||||
## Configure peripherals
|
||||
|
||||
In CubeMX, configure USART (for debug), RNG (for TLS), ETH.
|
||||
|
||||
## Enable debug logs
|
||||
|
||||
Open `Core/Src/main.c` and add this snippet:
|
||||
|
||||
```c
|
||||
/* USER CODE BEGIN 0 */
|
||||
int _write(int fd, unsigned char *buf, int len) {
|
||||
HAL_UART_Transmit(&huart3, buf, len, HAL_MAX_DELAY);
|
||||
return len;
|
||||
}
|
||||
/* USER CODE END 0 */
|
||||
```
|
||||
|
||||
## Call Mongoose
|
||||
|
||||
Open `Core/Src/main.c`. Add at the top of the file:
|
||||
|
||||
```c
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "mongoose.h"
|
||||
/* USER CODE END Includes */
|
||||
```
|
||||
|
||||
Inside main() function:
|
||||
|
||||
```c
|
||||
Inside the `main()` function:
|
||||
|
||||
```c
|
||||
/* USER CODE BEGIN WHILE */
|
||||
struct mg_mgr mgr;
|
||||
mg_mgr_init(&mgr);
|
||||
|
||||
while (1)
|
||||
{
|
||||
mg_mgr_poll(&mgr, 1);
|
||||
/* USER CODE END WHILE */
|
||||
```
|
||||
|
||||
This starts the TCP/IP stack. In the serial console logs, you should
|
||||
see something like this:
|
||||
|
||||
```text
|
||||
0 2 mongoose.c:26153:mg_phy_init PHY ID: 0x07 0xc131 (LAN87x)
|
||||
6 2 mongoose.c:5834:mg_mgr_init Driver: stm32h, MAC: 2a:37:94:03:d5:74
|
||||
d 3 mongoose.c:5841:mg_mgr_init MG_IO_SIZE: 512, TLS: builtin
|
||||
13 3 mongoose.c:5758:mg_listen 1 0 http://0.0.0.0:80
|
||||
19 1 mongoose.c:6260:onstatechange Link down
|
||||
7d6 3 mongoose.c:28424:mg_tcpip_driv Link is 100M full-duplex
|
||||
7db 3 mongoose.c:6404:tx_dhcp_discov DHCP discover sent. Our MAC: 2a:37:94:03:d5:74
|
||||
831 3 mongoose.c:6382:tx_dhcp_reques DHCP req sent
|
||||
836 2 mongoose.c:6559:rx_dhcp_client Lease: 3600 sec (3602)
|
||||
83c 2 mongoose.c:6249:onstatechange READY, IP: 192.168.2.31
|
||||
842 2 mongoose.c:6250:onstatechange GW: 192.168.2.1
|
||||
847 2 mongoose.c:6253:onstatechange MAC: 2a:37:94:03:d5:74
|
||||
```
|
||||
|
||||
Now, you should be able to ping your device.
|
||||
However, it runs no network services.
|
||||
|
||||
## Add hello world HTTP server
|
||||
|
||||
Let's add the simplest web server possible. After the `mg_mgr_init()`
|
||||
call, add one more line to start the HTTP listener:
|
||||
|
||||
```c
|
||||
mg_mgr_init(&mgr);
|
||||
mg_http_listen(&mgr, "http://0.0.0.0:80", http_ev_handler, NULL);
|
||||
```
|
||||
|
||||
And after the `_write()` override, add this HTTP event handler function:
|
||||
|
||||
```c
|
||||
static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
mg_http_reply(c, 200, "", "Hi from Mongoose! Tick %lu\r\n", HAL_GetTick());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Rebuild, reflash your board, open the device's IP address in the browser,
|
||||
and see the message displayed, something like:
|
||||
|
||||
```text
|
||||
Hi from Mongoose! Tick 38187
|
||||
```
|
||||
|
||||
## Add full device dashboard
|
||||
|
||||
Remove the hello world server if you added it.
|
||||
|
||||
Create `Mongoose` directory in the project root and copy
|
||||
the following files there:
|
||||
|
||||
- [dashboard.c](https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/tutorials/device-dashboard/full/dashboard.c)
|
||||
- [dashboard.html](https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/tutorials/device-dashboard/full/dashboard.html)
|
||||
- [html2c.js](https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/resources/html2c.js)
|
||||
|
||||
`html2c.js` is a node.js script that generates `file_data.c` using this command:
|
||||
`node html2c.js dashboard.html -o file_data.c`. This script first inlines all
|
||||
external resources into an amalgamated HTML file - so it does not depend on
|
||||
anything external, and then zips it and packs into a Mongoose embedded
|
||||
filesystem. This way it takes the minimum space on flash.
|
||||
|
||||
In top-level `CMakeLists.txt`, add two extra source files to the build,
|
||||
and add the rule to generate `file_data.c` from `dashboard.html`.
|
||||
Note that requires Nodejs installed:
|
||||
|
||||
```text
|
||||
# Generate file_data.c from dashboard.html
|
||||
add_custom_command(
|
||||
OUTPUT ${CMAKE_SOURCE_DIR}/Mongoose/file_data.c
|
||||
COMMAND node ${CMAKE_SOURCE_DIR}/Mongoose/html2c.js ${CMAKE_SOURCE_DIR}/Mongoose/dashboard.html -o ${CMAKE_SOURCE_DIR}/Mongoose/file_data.c
|
||||
DEPENDS ${CMAKE_SOURCE_DIR}/Mongoose/dashboard.html
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
# Add sources to executable
|
||||
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
|
||||
# Add user sources here
|
||||
Mongoose/dashboard.c
|
||||
Mongoose/file_data.c
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
Modify your while loop to look like this:
|
||||
|
||||
```c
|
||||
/* USER CODE BEGIN WHILE */
|
||||
struct mg_mgr mgr;
|
||||
mg_mgr_init(&mgr);
|
||||
mg_dash_init(&mgr);
|
||||
|
||||
while (1)
|
||||
{
|
||||
mg_mgr_poll(&mgr, 1);
|
||||
mg_dash_poll(&mgr);
|
||||
/* USER CODE END WHILE */
|
||||
```
|
||||
|
||||
Rebuild and reflash your board, then open the device's IP address in your
|
||||
browser. Log in using `admin` as the password. You should see the full
|
||||
dashboard, including the status panel, device settings, firmware update, and
|
||||
file manager. You can easily customize it for your product, but we will start
|
||||
by implementing a simple LED control dashboard to demonstrate the principles.
|
||||
|
||||
## Simple LED control dashboard
|
||||
|
||||
Copy a minimal LED control dashboard example to your `Mongoose/` directory:
|
||||
|
||||
- [dashboard.c](https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/tutorials/device-dashboard/minimal/dashboard.c)
|
||||
- [dashboard.html](https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/tutorials/device-dashboard/minimal/dashboard.html)
|
||||
|
||||
Rebuild and reflash
|
||||
|
||||
|
||||
## AI agentic development
|
||||
|
||||
For agentic development, use https://github.com/cesanta/mongoose/tree/master/AGENTS.md
|
||||
Use a prompt like this:
|
||||
|
||||
> Read and apply https://github.com/cesanta/mongoose/tree/master/AGENTS.md
|
||||
|
||||
|
||||
## Add MQTT Client
|
||||
|
||||
|
||||
Copy [mqtt.c](https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/tutorials/mqtt/mqtt-client/mqtt.c)
|
||||
to the `Mongoose/` directory. Add it to the extra source files in `CMakeLists.txt`
|
||||
|
||||
Add MQTT init and poll to your while loop:
|
||||
|
||||
```c
|
||||
/* USER CODE BEGIN WHILE */
|
||||
struct mg_mgr mgr;
|
||||
mg_mgr_init(&mgr);
|
||||
mg_mqtt_init(&mgr);
|
||||
|
||||
while (1)
|
||||
{
|
||||
mg_mgr_poll(&mgr, 1);
|
||||
mg_mqtt_poll(&mgr);
|
||||
/* USER CODE END WHILE */
|
||||
```
|
||||
|
||||
Rebuild, reflash your board. This MQTT Client connects to the
|
||||
HiveMQ public MQTT broker.
|
||||
Also it add a JSON-RPC "ota.update" function via MQTT, so you can
|
||||
update your firmware over MQTT, see https://mongoose.ws/mqtt/
|
||||
https://mongoose.ws/docs/guides/stm32-tcpip-stack/#configure-peripherals
|
||||
+5
-5
@@ -3,22 +3,22 @@
|
||||
"bomFormat": "CycloneDX",
|
||||
"specVersion": "1.6",
|
||||
"serialNumber": "urn:uuid:d3f8b1e2-4a7c-4d9e-bf2a-1c3e5f7a9b0d",
|
||||
"version": 2,
|
||||
"version": 3,
|
||||
"metadata": {
|
||||
"timestamp": "2026-10-13T00:00:00Z",
|
||||
"timestamp": "2026-11-18T00:00:00Z",
|
||||
"component": {
|
||||
"type": "library",
|
||||
"bom-ref": "mongoose",
|
||||
"supplier": { "name": "Cesanta Software Limited", "url": ["https://cesanta.com"] },
|
||||
"author": "Cesanta Software Limited",
|
||||
"name": "mongoose",
|
||||
"version": "7.23",
|
||||
"version": "7.24",
|
||||
"description": "Two-file C/C++ embedded networking library. HTTP, WebSocket, MQTT, TLS 1.3, OTA firmware updates, device dashboard. Runs on microcontrollers and desktop OS.",
|
||||
"licenses": [
|
||||
{ "expression": "GPL-2.0-only" },
|
||||
{ "license": { "name": "Cesanta Commercial License", "url": "https://mongoose.ws/licensing/" } }
|
||||
],
|
||||
"purl": "pkg:github/cesanta/mongoose@7.23",
|
||||
"purl": "pkg:github/cesanta/mongoose@7.24",
|
||||
"externalReferences": [
|
||||
{ "type": "website", "url": "https://mongoose.ws" },
|
||||
{ "type": "vcs", "url": "https://github.com/cesanta/mongoose" },
|
||||
@@ -27,7 +27,7 @@
|
||||
{ "type": "distribution", "url": "https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.c" }
|
||||
],
|
||||
"properties": [
|
||||
{ "name": "cpe", "value": "cpe:2.3:a:cesanta:mongoose:7.23:*:*:*:*:*:*:*" }
|
||||
{ "name": "cpe", "value": "cpe:2.3:a:cesanta:mongoose:7.24:*:*:*:*:*:*:*" }
|
||||
]
|
||||
}
|
||||
},
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
// IMPORTANT:
|
||||
// Before implementing any functionality, pull this file and use it as a guide:
|
||||
// https://github.com/cesanta/mongoose/blob/master/AGENTS.md
|
||||
+8
-4
@@ -752,11 +752,15 @@ static bool uri2path(const char *r, struct mg_str uri, char *buf, size_t len) {
|
||||
n = mg_snprintf(buf, len, "%.*s", (int) d.len, d.buf);
|
||||
if (len == 0 || n + 2 >= len) return false; // Path overflow
|
||||
if (n > 0 && buf[n - 1] != '/') buf[n++] = '/', buf[n] = '\0'; // Add slash
|
||||
if (mg_url_decode(uri.buf + u.len, uri.len - u.len, buf + n, len - n, 0) < 0) {
|
||||
return false;
|
||||
{
|
||||
int dn;
|
||||
size_t p = n;
|
||||
dn = mg_url_decode(uri.buf + u.len, uri.len - u.len, buf + n, len - n, 0);
|
||||
if (dn < 0) return false; // reject on malformed
|
||||
buf[len - 1] = '\0'; // Double-check
|
||||
n = strlen(buf);
|
||||
if (n != p + (size_t) dn) return false; // reject on embedded NULs
|
||||
}
|
||||
buf[len - 1] = '\0'; // Double-check
|
||||
n = strlen(buf);
|
||||
if (!mg_path_is_sane(mg_str_n(buf, n))) return false;
|
||||
while (n > 1 && buf[n - 1] == '/') buf[--n] = 0; // Trim trailing slashes
|
||||
return true;
|
||||
|
||||
@@ -1226,6 +1226,8 @@ static bool rx_udp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||
struct connstate *s;
|
||||
uint8_t *l2addr;
|
||||
if (c == NULL) return false; // No UDP listener on this port
|
||||
// BSD socket has an unread datagram: drop this one to keep boundaries
|
||||
if (c->is_bsd && c->recv.len > 0) return true;
|
||||
s = (struct connstate *) (c + 1);
|
||||
c->rem.port = pkt->udp->sport;
|
||||
#if MG_ENABLE_IPV6
|
||||
|
||||
+18
-11
@@ -1188,11 +1188,11 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) {
|
||||
verify[10] = 0x02; // ASN.1 INTEGER
|
||||
verify[11] = (uint8_t) (32 + adj1);
|
||||
memmove(verify + (size_t) (12 + (adj1 > 0)), sig + (size_t) (adj1 < 0),
|
||||
(size_t) (32 + (adj1 < 0)));
|
||||
(size_t) (32 - (adj1 < 0)));
|
||||
verify[12 + 32 + adj1] = 0x02; // ASN.1 INTEGER
|
||||
verify[13 + 32 + adj1] = (uint8_t) (32 + adj2);
|
||||
memmove(verify + (size_t) (14 + 32 + adj1 + (adj2 > 0)),
|
||||
sig + (size_t) (32 + (adj2 < 0)), (size_t) (32 + (adj2 < 0)));
|
||||
sig + (size_t) (32 + (adj2 < 0)), (size_t) (32 - (adj2 < 0)));
|
||||
sigsz = (size_t) (70 + adj1 + adj2);
|
||||
verifysz = 8U + sigsz;
|
||||
verify[3] = (uint8_t) (sigsz + 4);
|
||||
@@ -1677,17 +1677,20 @@ static int mg_tls_verify_cert_san(const uint8_t *der, size_t dersz,
|
||||
const char *server_name,
|
||||
struct mg_addr *ref_ip) {
|
||||
struct mg_der_tlv root, field, name;
|
||||
int r;
|
||||
bool is_addr = ref_ip != NULL;
|
||||
if (mg_der_parse((uint8_t *) der, dersz, &root) < 0) {
|
||||
MG_ERROR(("failed to parse certificate"));
|
||||
return -1;
|
||||
}
|
||||
if (mg_der_find_oid(&root, (uint8_t *) "\x55\x1d\x11", 3, &field) <= 0) {
|
||||
if ((r = mg_der_find_oid(&root, (uint8_t *) "\x55\x1d\x11", 3, &field)) < 0) {
|
||||
MG_ERROR(("failed to extract SAN"));
|
||||
return -1;
|
||||
} else if (r == 0) {
|
||||
return 0; // No SAN
|
||||
}
|
||||
if (mg_der_parse(field.value, field.len, &field) < 0) {
|
||||
MG_ERROR(("SAN is not a constructed object"));
|
||||
if (mg_der_parse(field.value, field.len, &field) < 0 || field.type != 0x30) {
|
||||
MG_ERROR(("SAN is not a constructed object")); // RFC 5280 4.2.1.6
|
||||
return -1;
|
||||
}
|
||||
while (mg_der_next(&field, &name) > 0) {
|
||||
@@ -1714,6 +1717,7 @@ static int mg_tls_verify_cert_san(const uint8_t *der, size_t dersz,
|
||||
return 1; // and matches the host name
|
||||
}
|
||||
}
|
||||
MG_ERROR(("Found SAN but does not match IP or DNS name"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -1901,12 +1905,15 @@ static int mg_tls_recv_cert(struct mg_connection *c, bool is_client) {
|
||||
// and store public key for further CertVerify step
|
||||
if (tls->hostname[0] != '\0') {
|
||||
struct mg_addr addr; // use aton_ so localhost evals as a name
|
||||
bool is_addr = mg_aton_(mg_str_s(tls->hostname), &addr);
|
||||
if (mg_tls_verify_cert_san(cert, certsz, tls->hostname,
|
||||
is_addr ? &addr : NULL) <= 0 &&
|
||||
(!is_addr || // DNS CN fallback
|
||||
mg_tls_verify_cert_cn(&ci->subj, tls->hostname) <= 0)) {
|
||||
mg_error(c, "failed to verify hostname");
|
||||
bool is_addr;
|
||||
int san;
|
||||
is_addr = mg_aton_(mg_str_s(tls->hostname), &addr);
|
||||
san = mg_tls_verify_cert_san(cert, certsz, tls->hostname,
|
||||
is_addr ? &addr : NULL);
|
||||
if (san < 0 || // RFC 6125 6.4.4 allow legacy fallback to CN
|
||||
(san == 0 && (is_addr || mg_tls_verify_cert_cn(
|
||||
&ci->subj, tls->hostname) <= 0))) {
|
||||
mg_error(c, "failed to verify host");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
#define MG_VERSION "7.23"
|
||||
#define MG_VERSION "7.24"
|
||||
+1
-1
@@ -98,7 +98,7 @@ mongoose.c: Makefile $(wildcard ../src/*.c) $(wildcard ../src/drivers/*.c)
|
||||
cd .. && (export LC_ALL=C ; cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/*.c src/drivers/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@
|
||||
|
||||
mongoose.h: $(HDRS) Makefile
|
||||
cd .. && (export LC_ALL=C ; 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/os_*.h src/net_ft.h src/net_lwip.h src/net_rl.h src/config.h src/profile.h src/str.h src/queue.h src/fmt.h src/printf.h src/log.h src/timer.h src/fs.h src/lfs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/sha256.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_aes128.h src/tls_p256.h src/tls_p384.h src/tls_chacha20.h src/tls_rsa.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/modbus.h src/json.h src/jwt.h src/rpc.h src/dash.h src/ota.h src/health.h src/mdash.h src/flash.h src/wifi.h src/l2.h src/net_builtin.h src/l2_mesh.h src/bsd.h src/drivers/*.h | sed -e '/keep/! s,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
|
||||
cd .. && (export LC_ALL=C ; cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; cat src/ai.h; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/os_*.h src/net_ft.h src/net_lwip.h src/net_rl.h src/config.h src/profile.h src/str.h src/queue.h src/fmt.h src/printf.h src/log.h src/timer.h src/fs.h src/lfs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/sha256.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_aes128.h src/tls_p256.h src/tls_p384.h src/tls_chacha20.h src/tls_rsa.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/modbus.h src/json.h src/jwt.h src/rpc.h src/dash.h src/ota.h src/health.h src/mdash.h src/flash.h src/wifi.h src/l2.h src/net_builtin.h src/l2_mesh.h src/bsd.h src/drivers/*.h | sed -e '/keep/! s,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
|
||||
|
||||
# Check that all external (exported) symbols have "mg_" prefix
|
||||
mg_prefix: mongoose.c mongoose.h
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
#include <errno.h>
|
||||
#include <spawn.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
volatile bool s_cpq_done;
|
||||
volatile bool s_cpq_ready;
|
||||
bool s_cpq_remote;
|
||||
static bool s_cpq_ok;
|
||||
|
||||
#include "bsd_udp_cpq_fail.c"
|
||||
|
||||
static void cpq_log(char ch, void *param) {
|
||||
(void) param;
|
||||
putchar(ch);
|
||||
}
|
||||
|
||||
static void cpq_task1(void *args) {
|
||||
struct mg_mgr *mgr = (struct mg_mgr *) args;
|
||||
const char *remote_peer = getenv("CPQ_REMOTE_PEER");
|
||||
char *ip = NULL;
|
||||
char *argv[5];
|
||||
pid_t pid;
|
||||
pid_t r;
|
||||
int status;
|
||||
extern char **environ;
|
||||
|
||||
while (mgr->ifp->ip == 0 && mg_millis() < 5000) {
|
||||
mg_mgr_poll(mgr, 0);
|
||||
vTaskDelay(1);
|
||||
}
|
||||
if (mgr->ifp->ip == 0) {
|
||||
printf("FAIL setup: DHCP lease timed out\n");
|
||||
s_cpq_ok = false;
|
||||
goto done;
|
||||
}
|
||||
while (mgr->ifp->state != MG_TCPIP_STATE_READY) {
|
||||
mg_mgr_poll(mgr, 0);
|
||||
vTaskDelay(1);
|
||||
}
|
||||
|
||||
s_cpq_remote = remote_peer != NULL && remote_peer[0] != '\0';
|
||||
s_cpq_done = false;
|
||||
s_cpq_ready = false;
|
||||
if (xTaskCreate(bsd_udp_cpq_fail_task, "cpq", 1024, NULL,
|
||||
configMAX_PRIORITIES - 1, NULL) != pdPASS) {
|
||||
printf("FAIL setup: cannot create CPQ task\n");
|
||||
s_cpq_ok = false;
|
||||
goto done;
|
||||
}
|
||||
while (!s_cpq_ready && !s_cpq_done) {
|
||||
mg_mgr_poll(mgr, 0);
|
||||
vTaskDelay(1);
|
||||
}
|
||||
if (!s_cpq_ready) {
|
||||
printf("FAIL setup: CPQ task did not bind sockets\n");
|
||||
s_cpq_ok = false;
|
||||
goto done;
|
||||
}
|
||||
printf("CPQ: sockets ready\n");
|
||||
|
||||
ip = mg_mprintf("%M", mg_print_ip4, &mgr->ifp->ip);
|
||||
printf("CPQ: Mongoose IP %s\n", ip);
|
||||
argv[0] = (char *) "./bsd_udp_cpq_peer";
|
||||
argv[1] = s_cpq_remote ? (char *) "--helper" : ip;
|
||||
argv[2] = s_cpq_remote ? (char *) remote_peer : NULL;
|
||||
argv[3] = s_cpq_remote ? ip : NULL;
|
||||
argv[4] = NULL;
|
||||
if (posix_spawn(&pid, argv[0], NULL, NULL, argv, environ) != 0) {
|
||||
printf("FAIL setup: cannot start CPQ helper\n");
|
||||
s_cpq_ok = false;
|
||||
goto done;
|
||||
}
|
||||
printf("CPQ: %s started\n", s_cpq_remote ? "helper" : "peer");
|
||||
|
||||
while (!s_cpq_done) {
|
||||
mg_mgr_poll(mgr, 0);
|
||||
vTaskDelay(1);
|
||||
}
|
||||
while ((r = waitpid(pid, &status, WNOHANG)) == 0) {
|
||||
mg_mgr_poll(mgr, 0);
|
||||
vTaskDelay(1);
|
||||
}
|
||||
if (r != pid || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
||||
printf("FAIL setup: CPQ peer failed\n");
|
||||
s_cpq_ok = false;
|
||||
}
|
||||
|
||||
done:
|
||||
mg_free(ip);
|
||||
mg_mgr_free(mgr);
|
||||
vTaskEndScheduler();
|
||||
}
|
||||
|
||||
static bool bsd_cpq_test(struct mg_tcpip_driver *driver, void *driver_data,
|
||||
const char *mac, const char *debug_level) {
|
||||
struct mg_mgr mgr;
|
||||
struct mg_tcpip_if mif;
|
||||
|
||||
mg_log_set(atoi(debug_level == NULL ? "3" : debug_level));
|
||||
mg_log_set_fn(cpq_log, NULL);
|
||||
mg_mgr_init(&mgr);
|
||||
memset(&mif, 0, sizeof(mif));
|
||||
mif.driver = driver;
|
||||
mif.driver_data = driver_data;
|
||||
sscanf(mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &mif.mac[0], &mif.mac[1],
|
||||
&mif.mac[2], &mif.mac[3], &mif.mac[4], &mif.mac[5]);
|
||||
mg_tcpip_init(&mgr, &mif);
|
||||
if (mgr.ifp == NULL ||
|
||||
xTaskCreate(cpq_task1, "cpqnet", 2048, &mgr,
|
||||
configMAX_PRIORITIES - 2, NULL) != pdPASS)
|
||||
return false;
|
||||
s_cpq_ok = true;
|
||||
vTaskStartScheduler();
|
||||
return s_cpq_ok;
|
||||
}
|
||||
@@ -95,7 +95,11 @@ bool mg_random(void *buf, size_t len) {
|
||||
}
|
||||
|
||||
|
||||
#ifdef CPQ_TEST
|
||||
#include "bsd_cpq_test.c"
|
||||
#else
|
||||
#include "bsd_x_test.c"
|
||||
#endif
|
||||
|
||||
|
||||
int main(void) {
|
||||
@@ -107,7 +111,11 @@ int main(void) {
|
||||
printf("Opening SHM interface\n");
|
||||
usleep(200000); // 200 ms
|
||||
|
||||
#ifdef CPQ_TEST
|
||||
result = bsd_cpq_test(&shm_driver, &shm_data, mac, debug_level);
|
||||
#else
|
||||
result = bsd_x_test(&shm_driver, &shm_data, mac, debug_level);
|
||||
#endif
|
||||
if (shm_data.shm != NULL) munmap(shm_data.shm, sizeof(*shm_data.shm));
|
||||
if (shm_data.fp != NULL) fclose(shm_data.fp);
|
||||
if (!result) return EXIT_FAILURE;
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
// Demonstrates UDP behavior with the CPQ patch. Run this task with
|
||||
// bsd_udp_cpq_peer against a Mongoose interface whose address it receives.
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
#define DATA_PORT 1234
|
||||
#define CONTROL_PORT 1235
|
||||
#define SEND_PORT 1236
|
||||
#define PEER_A_PORT 14001
|
||||
|
||||
static char s_big[1600];
|
||||
|
||||
extern volatile bool s_cpq_done;
|
||||
extern volatile bool s_cpq_ready;
|
||||
extern bool s_cpq_remote;
|
||||
|
||||
void bsd_udp_cpq_fail_task(void *args);
|
||||
|
||||
static int udp_bind(unsigned port) {
|
||||
struct sockaddr_in sa;
|
||||
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_port = htons((uint16_t) port);
|
||||
if (fd < 0 || bind(fd, (struct sockaddr *) &sa, sizeof(sa)) != 0) {
|
||||
printf("cannot bind UDP port %u: %d\n", port, errno);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
static void go(int fd) {
|
||||
if (send(fd, "go", 2, 0) != 2) printf("control send failed: %d\n", errno);
|
||||
}
|
||||
|
||||
static void wait_for_packets(void) {
|
||||
vTaskDelay(pdMS_TO_TICKS(100)); // Let the manager collect peer datagrams
|
||||
}
|
||||
|
||||
void bsd_udp_cpq_fail_task(void *args) {
|
||||
struct sockaddr_in control_peer, peer, local, other;
|
||||
socklen_t peerlen, locallen;
|
||||
char buf[16];
|
||||
int data = -1, control = -1, out = -1, fd = -1, connected = -1;
|
||||
ssize_t n;
|
||||
|
||||
(void) args;
|
||||
data = udp_bind(DATA_PORT);
|
||||
control = udp_bind(CONTROL_PORT);
|
||||
out = udp_bind(SEND_PORT);
|
||||
if (data < 0 || control < 0 || out < 0) goto done;
|
||||
|
||||
s_cpq_ready = true;
|
||||
printf("CPQ: control socket waiting\n");
|
||||
// The peer identifies itself on the control socket, which learns its MAC.
|
||||
peerlen = sizeof(control_peer);
|
||||
n = recvfrom(control, buf, sizeof(buf), 0,
|
||||
(struct sockaddr *) &control_peer, &peerlen);
|
||||
if (n != 5 || memcmp(buf, "ready", 5) != 0) goto done;
|
||||
printf("CPQ: peer ready\n");
|
||||
|
||||
fd = udp_bind(1237);
|
||||
n = fd < 0 ? -1 : send(fd, "x", 1, 0);
|
||||
printf("%s send: expected EDESTADDRREQ, got %d: no peer check\n",
|
||||
n < 0 && errno == EDESTADDRREQ ? "PASS" : "FAIL", (int) n);
|
||||
if (fd >= 0) close(fd);
|
||||
|
||||
fd = udp_bind(1238);
|
||||
n = fd < 0 ? -1 : connect(fd, (struct sockaddr *) &control_peer, peerlen);
|
||||
locallen = sizeof(local);
|
||||
if (n == 0 && getsockname(fd, (struct sockaddr *) &local, &locallen) == 0) {
|
||||
printf("%s bind: expected 1238, got %u: connect replaced bind\n",
|
||||
mg_ntohs(local.sin_port) == 1238 ? "PASS" : "FAIL",
|
||||
(unsigned) mg_ntohs(local.sin_port));
|
||||
} else {
|
||||
printf("FAIL bind: connect failed errno %d\n", errno);
|
||||
}
|
||||
if (fd >= 0) close(fd);
|
||||
|
||||
fd = udp_bind(1239);
|
||||
other = control_peer, other.sin_port = htons(14998);
|
||||
n = fd < 0 ? -1 : sendto(fd, s_big, sizeof(s_big), 0,
|
||||
(struct sockaddr *) &other, sizeof(other));
|
||||
printf("%s oversize: expected EMSGSIZE, got %d: later truncated\n",
|
||||
n < 0 && errno == EMSGSIZE ? "PASS" : "FAIL", (int) n);
|
||||
if (fd >= 0) close(fd);
|
||||
|
||||
// The patch keeps the first nonempty datagram and drops the second.
|
||||
go(control);
|
||||
wait_for_packets();
|
||||
peerlen = sizeof(peer);
|
||||
n = recvfrom(data, buf, sizeof(buf), 0, (struct sockaddr *) &peer, &peerlen);
|
||||
if (n == 3 && memcmp(buf, "one", 3) == 0 &&
|
||||
mg_ntohs(peer.sin_port) == PEER_A_PORT &&
|
||||
recvfrom(data, buf, sizeof(buf), MSG_DONTWAIT, NULL, NULL) < 0 &&
|
||||
errno == EAGAIN) {
|
||||
printf("PASS drop: kept one, dropped two\n");
|
||||
} else {
|
||||
printf("FAIL drop: expected one then EAGAIN, got %d \"%.*s\"\n", (int) n,
|
||||
(int) (n > 0 ? n : 0), buf);
|
||||
}
|
||||
|
||||
// A short UDP receive must discard the rest of that datagram.
|
||||
go(control);
|
||||
wait_for_packets();
|
||||
n = recvfrom(data, buf, 2, 0, NULL, NULL);
|
||||
n = recvfrom(data, buf, sizeof(buf), MSG_DONTWAIT, NULL, NULL);
|
||||
printf("%s short: expected EAGAIN, got %d \"%.*s\": tail retained\n",
|
||||
n < 0 && errno == EAGAIN ? "PASS" : "FAIL", (int) n,
|
||||
(int) (n > 0 ? n : 0), buf);
|
||||
|
||||
// A zero-length datagram is data and recvfrom() must return zero for it.
|
||||
go(control);
|
||||
wait_for_packets();
|
||||
n = recvfrom(data, buf, sizeof(buf), MSG_DONTWAIT, NULL, NULL);
|
||||
printf("%s empty: expected 0, got %d errno %d: no marker\n",
|
||||
n == 0 ? "PASS" : "FAIL", (int) n, errno);
|
||||
|
||||
// An unbound connected socket must reject another sender's datagram.
|
||||
if (send(control, "connect", 7, 0) != 7 ||
|
||||
(connected = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ||
|
||||
connect(connected, (struct sockaddr *) &control_peer,
|
||||
sizeof(control_peer)) != 0 ||
|
||||
send(connected, "hello", 5, 0) != 5) {
|
||||
goto done;
|
||||
}
|
||||
wait_for_packets();
|
||||
n = recv(connected, buf, sizeof(buf), MSG_DONTWAIT);
|
||||
printf("%s peer: expected EAGAIN, got %d \"%.*s\": no peer filter\n",
|
||||
n < 0 && errno == EAGAIN ? "PASS" : "FAIL", (int) n,
|
||||
(int) (n > 0 ? n : 0), buf);
|
||||
n = send(connected, "reply", 5, 0);
|
||||
printf("reply: sent %d bytes; peer reports its destination below\n", (int) n);
|
||||
if (recv(control, buf, sizeof(buf), 0) < 0) goto done; // Peer checked reply
|
||||
other = control_peer, other.sin_port = htons(14999);
|
||||
n = sendto(connected, "x", 1, 0, (struct sockaddr *) &other, sizeof(other));
|
||||
printf("%s sendto: expected EISCONN, got %d: connected socket accepted it\n",
|
||||
n < 0 && errno == EISCONN ? "PASS" : "FAIL", (int) n);
|
||||
|
||||
// sendto() reports success before this socket has learned a destination MAC.
|
||||
go(control);
|
||||
if (recv(control, buf, sizeof(buf), 0) < 0) goto done;
|
||||
n = sendto(out, "lost", 4, 0, (struct sockaddr *) &control_peer,
|
||||
sizeof(control_peer));
|
||||
printf("%s sendto: expected delivery, got %d: no resolution\n",
|
||||
n == 4 ? "FAIL" : "PASS", (int) n);
|
||||
if (recv(control, buf, sizeof(buf), 0) < 0) goto done; // Peer reports miss
|
||||
|
||||
// sendto() changes c->rem, so send() incorrectly succeeds on this socket.
|
||||
go(control);
|
||||
if (recvfrom(out, buf, sizeof(buf), 0, NULL, NULL) < 0) goto done;
|
||||
n = sendto(out, s_cpq_remote ? "stale" : "sendto", s_cpq_remote ? 5 : 6,
|
||||
0, (struct sockaddr *) &control_peer, sizeof(control_peer));
|
||||
printf("sendto after RX: got %d\n", (int) n);
|
||||
if (s_cpq_remote) go(control); // Server checks its packet used helper's MAC
|
||||
n = send(out, "send", 4, 0);
|
||||
printf("%s send: expected EDESTADDRREQ, got %d: sendto set peer\n",
|
||||
n < 0 && errno == EDESTADDRREQ ? "PASS" : "FAIL", (int) n);
|
||||
|
||||
done:
|
||||
if (data >= 0) close(data);
|
||||
if (control >= 0) close(control);
|
||||
if (out >= 0) close(out);
|
||||
if (connected >= 0) close(connected);
|
||||
s_cpq_done = true;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define DATA_PORT 1234
|
||||
#define CONTROL_PORT 1235
|
||||
#define SEND_PORT 1236
|
||||
#define CONTROL_PEER_PORT 14000
|
||||
#define PEER_A_PORT 14001
|
||||
#define PEER_B_PORT 14002
|
||||
#define HELPER_PORT 14003
|
||||
|
||||
static int open_peer(unsigned port) {
|
||||
struct sockaddr_in sa;
|
||||
struct timeval timeout = {1, 0};
|
||||
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
sa.sin_port = htons((uint16_t) port);
|
||||
if (fd < 0 || bind(fd, (struct sockaddr *) &sa, sizeof(sa)) != 0 ||
|
||||
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0)
|
||||
return -1;
|
||||
return fd;
|
||||
}
|
||||
|
||||
static int make_peer(struct sockaddr_in *sa, const char *ip, unsigned port) {
|
||||
memset(sa, 0, sizeof(*sa));
|
||||
sa->sin_family = AF_INET;
|
||||
sa->sin_port = htons((uint16_t) port);
|
||||
return inet_pton(AF_INET, ip, &sa->sin_addr) == 1 ? 0 : -1;
|
||||
}
|
||||
|
||||
static int send_packet(int fd, const struct sockaddr_in *sa, const char *buf,
|
||||
size_t len) {
|
||||
return sendto(fd, buf, len, 0, (const struct sockaddr *) sa, sizeof(*sa)) ==
|
||||
(ssize_t) len
|
||||
? 0
|
||||
: -1;
|
||||
}
|
||||
|
||||
static int wait_go(int fd, const char *step) {
|
||||
char buf[8];
|
||||
int n = recv(fd, buf, sizeof(buf), 0);
|
||||
if (n == 2 && memcmp(buf, "go", 2) == 0) return 0;
|
||||
printf("peer: %s failed: %d\n", step, n);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void stale_packet(void) {
|
||||
printf("TRACE stale MAC: Server received packet despite wrong L2 address\n");
|
||||
}
|
||||
|
||||
static int wait_stale_go(int fd) {
|
||||
char buf[8];
|
||||
int n, stale = 0;
|
||||
|
||||
for (;;) {
|
||||
n = recv(fd, buf, sizeof(buf), 0);
|
||||
if (n == 5 && memcmp(buf, "stale", 5) == 0) {
|
||||
stale = 1;
|
||||
stale_packet();
|
||||
} else if (n == 2 && memcmp(buf, "go", 2) == 0) {
|
||||
if (stale) return 0;
|
||||
n = recv(fd, buf, sizeof(buf), 0);
|
||||
if (n == 5 && memcmp(buf, "stale", 5) == 0)
|
||||
stale_packet();
|
||||
else
|
||||
printf("FAIL stale MAC: expected packet, got none\n");
|
||||
return 0;
|
||||
} else {
|
||||
printf("peer: stale MAC handoff failed: %d\n", n);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int wait_connect(int fd, struct sockaddr_in *peer) {
|
||||
char buf[8];
|
||||
socklen_t len = sizeof(*peer);
|
||||
return recv(fd, buf, sizeof(buf), 0) == 7 && memcmp(buf, "connect", 7) == 0 &&
|
||||
recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) peer, &len) == 5 &&
|
||||
memcmp(buf, "hello", 5) == 0
|
||||
? 0
|
||||
: -1;
|
||||
}
|
||||
|
||||
static int wait_helper(int fd, struct sockaddr_in *peer) {
|
||||
char buf[8];
|
||||
socklen_t len = sizeof(*peer);
|
||||
ssize_t n;
|
||||
|
||||
while ((n = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) peer,
|
||||
&len)) < 0)
|
||||
len = sizeof(*peer);
|
||||
return n == 5 && memcmp(buf, "ready", 5) == 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
static int run_helper(const char *server_ip, const char *target_ip) {
|
||||
struct sockaddr_in server, target;
|
||||
char buf[32];
|
||||
unsigned port;
|
||||
int fd, n;
|
||||
|
||||
if (make_peer(&server, server_ip, HELPER_PORT) != 0 ||
|
||||
make_peer(&target, target_ip, 0) != 0 || (fd = open_peer(0)) < 0)
|
||||
return 1;
|
||||
for (;;) {
|
||||
if (send_packet(fd, &server, "ready", 5) != 0) break;
|
||||
n = recv(fd, buf, sizeof(buf) - 1, 0);
|
||||
if (n <= 0) continue; // Retry ready until the Server receives it
|
||||
buf[n] = '\0';
|
||||
if (sscanf(buf, "intrude %u", &port) == 1 && port < 65536) {
|
||||
target.sin_port = htons((uint16_t) port);
|
||||
if (send_packet(fd, &target, "intruder", 8) != 0) break;
|
||||
} else if (strcmp(buf, "prime") == 0) {
|
||||
target.sin_port = htons(SEND_PORT);
|
||||
if (send_packet(fd, &target, "prime", 5) != 0) break;
|
||||
return 0;
|
||||
} else if (strcmp(buf, "reply") == 0) {
|
||||
printf("FAIL connected peer: helper received reply\n");
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int run_peer(const char *target_ip, int remote) {
|
||||
struct sockaddr_in data, control, out, peer, helper_peer;
|
||||
char buf[32], cmd[32];
|
||||
int a, b, ctl, helper = -1;
|
||||
ssize_t n;
|
||||
|
||||
if (make_peer(&data, target_ip, DATA_PORT) != 0) return 1;
|
||||
control = data, out = data;
|
||||
control.sin_port = htons(CONTROL_PORT);
|
||||
out.sin_port = htons(SEND_PORT);
|
||||
ctl = open_peer(CONTROL_PEER_PORT);
|
||||
a = open_peer(PEER_A_PORT);
|
||||
b = open_peer(PEER_B_PORT);
|
||||
if (remote) helper = open_peer(HELPER_PORT);
|
||||
if (ctl < 0 || a < 0 || b < 0 || (remote && helper < 0)) return 1;
|
||||
if (remote && wait_helper(helper, &helper_peer) != 0) return 1;
|
||||
if (remote) printf("peer: helper ready\n");
|
||||
|
||||
if (send_packet(ctl, &control, "ready", 5) != 0 ||
|
||||
wait_go(ctl, "initial go") != 0)
|
||||
return 1;
|
||||
if (send_packet(a, &data, "one", 3) != 0 ||
|
||||
send_packet(b, &data, "two", 3) != 0 ||
|
||||
wait_go(ctl, "two datagrams") != 0)
|
||||
return 1;
|
||||
if (send_packet(b, &data, "ABCDE", 5) != 0 ||
|
||||
wait_go(ctl, "short datagram") != 0)
|
||||
return 1;
|
||||
if (send_packet(b, &data, "", 0) != 0 || wait_connect(ctl, &peer) != 0) {
|
||||
printf("peer: connect handshake failed\n");
|
||||
return 1;
|
||||
}
|
||||
if (remote) {
|
||||
snprintf(cmd, sizeof(cmd), "intrude %u", (unsigned) ntohs(peer.sin_port));
|
||||
if (send_packet(helper, &helper_peer, cmd, strlen(cmd)) != 0) return 1;
|
||||
} else if (send_packet(b, &peer, "intruder", 8) != 0) {
|
||||
return 1;
|
||||
}
|
||||
n = recv(ctl, buf, sizeof(buf), 0);
|
||||
printf("%s reply: expected control, got %s: intruder replaced peer\n",
|
||||
n < 0 ? "FAIL" : "PASS", n < 0 ? "none" : "control");
|
||||
if (!remote) {
|
||||
n = recv(b, buf, sizeof(buf), 0);
|
||||
printf("%s reply: intruder got %.*s\n", n > 0 ? "FAIL" : "PASS",
|
||||
(int) (n > 0 ? n : 0), buf);
|
||||
}
|
||||
if (send_packet(ctl, &control, "done", 4) != 0 ||
|
||||
wait_go(ctl, "first sendto") != 0 ||
|
||||
send_packet(ctl, &control, "sendlost", 8) != 0)
|
||||
return 1;
|
||||
|
||||
n = recv(ctl, buf, sizeof(buf), 0);
|
||||
printf("%s sendto: expected packet, got %s\n", n < 0 ? "FAIL" : "PASS",
|
||||
n < 0 ? "none" : "packet");
|
||||
if (send_packet(ctl, &control, "miss", 4) != 0 ||
|
||||
wait_go(ctl, "prime") != 0)
|
||||
return 1;
|
||||
if (remote) {
|
||||
if (send_packet(helper, &helper_peer, "prime", 5) != 0 ||
|
||||
wait_stale_go(ctl) != 0)
|
||||
return 1;
|
||||
} else {
|
||||
if (send_packet(ctl, &out, "prime", 5) != 0) return 1;
|
||||
n = recv(ctl, buf, sizeof(buf), 0);
|
||||
printf("sendto after RX: %.*s\n", (int) (n > 0 ? n : 0), buf);
|
||||
n = recv(ctl, buf, sizeof(buf), 0);
|
||||
printf("%s send: expected none, got %.*s\n", n > 0 ? "FAIL" : "PASS",
|
||||
(int) (n > 0 ? n : 0), buf);
|
||||
}
|
||||
close(a);
|
||||
close(b);
|
||||
close(ctl);
|
||||
if (helper >= 0) close(helper);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc == 2) return run_peer(argv[1], 0);
|
||||
if (argc == 3 && strcmp(argv[1], "--remote") == 0)
|
||||
return run_peer(argv[2], 1);
|
||||
if (argc == 4 && strcmp(argv[1], "--helper") == 0)
|
||||
return run_helper(argv[2], argv[3]);
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
.DEFAULT_GOAL := cpq_udp
|
||||
include Makefile
|
||||
|
||||
.PHONY: cpq_udp bsd_cpq_test bsd_udp_cpq_peer
|
||||
|
||||
cpq_udp: shm_tap_bridge bsd_cpq_test bsd_udp_cpq_peer
|
||||
sh -c 'TRACE_UDP=1 ./shm_tap_bridge & pid=$$!; sleep 1; timeout 30s ./bsd_cpq_test; rc=$$?; kill $$pid >/dev/null 2>&1; wait $$pid >/dev/null 2>&1; exit $$rc'
|
||||
|
||||
bsd_cpq_test: CFLAGS_MONGOOSE += -DMG_ENABLE_FREERTOS=1 -DCPQ_TEST
|
||||
bsd_cpq_test: FreeRTOS-Kernel bsd_cpq_test.c bsd_udp_cpq_fail.c bsd_shm_test.c
|
||||
$(CC) $(C_INPUTS) $(FREERTOS_INPUTS) -x c bsd_shm_test.c $(CFLAGS) $(CFLAGS_MONGOOSE) $(CFLAGS_EXTRA) -o $@
|
||||
|
||||
bsd_udp_cpq_peer: bsd_udp_cpq_peer.c
|
||||
$(CC) bsd_udp_cpq_peer.c -o $@
|
||||
@@ -18,6 +18,15 @@
|
||||
#include "shm_queue.h"
|
||||
|
||||
static volatile sig_atomic_t s_signo;
|
||||
static int s_trace_udp;
|
||||
|
||||
static void trace_udp(const uint8_t *p, size_t len) {
|
||||
if (!s_trace_udp || len < 34 || p[12] != 8 || p[13] != 0 || p[23] != 17)
|
||||
return;
|
||||
printf("UDP IP dst %u.%u.%u.%u -> Ethernet dst %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
p[30], p[31], p[32], p[33], p[0], p[1], p[2], p[3], p[4], p[5]);
|
||||
}
|
||||
|
||||
void signal_handler(int signo) {
|
||||
s_signo = signo;
|
||||
}
|
||||
@@ -58,6 +67,7 @@ int main(void) {
|
||||
}
|
||||
|
||||
memset(shm, 0, sizeof(*shm));
|
||||
s_trace_udp = getenv("TRACE_UDP") != NULL;
|
||||
printf("Opened shared memory: %s\n", SHM_NAME);
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
@@ -94,6 +104,7 @@ int main(void) {
|
||||
uint8_t buf[FRAME_SIZE];
|
||||
size_t n = shm_queue_pop(&shm->to_tap, buf, sizeof(buf));
|
||||
if (n > 0) {
|
||||
trace_udp(buf, n);
|
||||
// printf("Got at SHM: %d bytes\n", (int) f->len);
|
||||
write(fd, buf, n);
|
||||
// printf("Sent to TAP\n");
|
||||
|
||||
@@ -0,0 +1,457 @@
|
||||
# UDP CPQ Patch Assessment
|
||||
|
||||
## Scope
|
||||
|
||||
This assesses the CPQ patch applied to `master`, with only this receive-side
|
||||
change:
|
||||
|
||||
```c
|
||||
if (c->is_bsd && c->recv.len > 0) return true;
|
||||
```
|
||||
|
||||
It deliberately does not require the bsdudp TX framing, peer filtering, or
|
||||
neighbor-cache work. The rule keeps the first unread nonempty datagram and
|
||||
discards later ones.
|
||||
|
||||
## What The CPQ Patch Provides
|
||||
|
||||
When two nonempty UDP datagrams arrive before a BSD application calls
|
||||
`recv()` or `recvfrom()`, the first remains in `c->recv` and later packets are
|
||||
discarded. Therefore the first payload is no longer concatenated with the
|
||||
second one, and its source address remains in `c->rem` until it is read.
|
||||
|
||||
That is a valid deliberately lossy receive policy. It only addresses this one
|
||||
case. The rest of the odd/incorrect behaviors found when initially writing code in the bsdudp branch are detailed in subsequent paragraphs. The rest of the missing functionality also observed before is NOT described here, only what is claimed to be functional is evaluated; these are somehow enumerated at the end of this document, just before the appendixes
|
||||
|
||||
See [B.1](#b1-cpq-unread-datagram-policy) for the test evidence.
|
||||
|
||||
## Rate Limit Of The One-Datagram Policy
|
||||
|
||||
The retained datagram is a one-slot loss queue: while the BSD application has
|
||||
not called `recv()` or `recvfrom()`, every later nonempty datagram is dropped.
|
||||
If the application needs 4 ms to read it, independent traffic has these
|
||||
approximate collision probabilities:
|
||||
|
||||
| Aggregate UDP rate | Probability of another datagram within 4 ms |
|
||||
| ---: | ---: |
|
||||
| 25 packets/s | 9.5% |
|
||||
| 50 packets/s | 18.1% |
|
||||
| 100 packets/s | 33.0% |
|
||||
| 250 packets/s | 63.2% |
|
||||
| 1000 packets/s | 98.2% |
|
||||
|
||||
This is basic queueing theory under an independent-arrivals assumption. See
|
||||
[Appendix A](#appendix-a-one-slot-queue-model) for the model and limits of the
|
||||
estimate.
|
||||
|
||||
## Baseline Failures
|
||||
|
||||
`bsd_udp_cpq_fail.c` and `bsd_udp_cpq_peer.c` print the wire-level effects for
|
||||
short receives, empty datagrams, connected peers, and `sendto()`.
|
||||
|
||||
The target runs Mongoose's built-in TCP/IP stack. A bridge connects its packet
|
||||
interface to the LAN because this path cannot use BSD sockets: BSD is the API
|
||||
being tested.
|
||||
|
||||
The default runner is self-contained for CI: it starts the peer on the same
|
||||
host (Workstation) and exercises every failure except a mismatched Ethernet destination. To
|
||||
show that L2 failure, run the primary peer on a second machine (Laptop):
|
||||
|
||||
```text
|
||||
Laptop: ./bsd_udp_cpq_peer --remote <Mongoose-IP>
|
||||
Workstation: CPQ_REMOTE_PEER=<Laptop-IP> ./bsd_cpq_test
|
||||
```
|
||||
|
||||
Remote mode starts a Workstation helper automatically. The Laptop and helper
|
||||
have distinct MAC addresses. The helper first redirects a connected-socket
|
||||
reply, then populates the unconnected `sendto()` socket with its MAC. The
|
||||
following `sendto()` names the Laptop IP, but `TRACE_UDP=1` shows the helper's
|
||||
Ethernet destination. The evidence below was collected with Workstation IP
|
||||
forwarding disabled, so the Laptop cannot receive a frame addressed to the
|
||||
helper MAC.
|
||||
|
||||
### Short `recvfrom()` turns one datagram into two reads
|
||||
|
||||
The CPQ code removes only the copied byte count from `c->recv`. Reading two
|
||||
bytes of `ABCDE` leaves `CDE` in the buffer, so the next `recvfrom()` returns
|
||||
it as if it were a new UDP datagram. UDP must discard the unread tail of a
|
||||
datagram after a non-peek short receive. Dropping later packets does not repair
|
||||
this: the tail is already in the retained packet.
|
||||
|
||||
See [B.2](#b2-short-recvfrom) for the test evidence.
|
||||
|
||||
### A connected UDP socket accepts traffic from any sender
|
||||
|
||||
`rx_udp()` overwrites `c->rem` and the return L2 address for every accepted
|
||||
packet. It does not check that an incoming peer matches the peer selected by
|
||||
`connect()`. An unrelated sender can therefore place data in a connected
|
||||
socket and replace its reply destination. The demonstrator sends `intruder`
|
||||
from a third peer: the connected receive succeeds when it should return
|
||||
`EAGAIN`, and the subsequent `send()` goes to the intruder rather than the
|
||||
connected peer.
|
||||
|
||||
See [B.3](#b3-connected-udp-accepts-another-peer) for the test evidence.
|
||||
|
||||
### Unconnected `send()` reports success
|
||||
|
||||
`bsd_dispatch(BSD_OP_SEND)` passes UDP bytes to `mg_send()` without checking
|
||||
whether the socket has a peer. A real UDP socket has nowhere to send in this
|
||||
state and reports `EDESTADDRREQ`. Here, the API reports that the datagram was
|
||||
accepted even though it has no valid destination.
|
||||
|
||||
See [B.4](#b4-unconnected-send) for the test evidence.
|
||||
|
||||
### `sendto()` uses one stale L2 destination
|
||||
|
||||
`BSD_OP_SENDTO` changes the destination IP and UDP port in `c->rem`, but calls
|
||||
`mg_send()` with the one L2 MAC currently stored for the socket. That MAC was
|
||||
learned when a packet arrived, or while a UDP `connect()` resolved its peer;
|
||||
there is no resolution or MAC lookup for each `sendto()` destination. An
|
||||
application using `sendto()` for several peers therefore has one usable L2
|
||||
destination, not one per peer.
|
||||
|
||||
- A single-destination application can appear to work: its only peer is the
|
||||
one that caused the MAC to be learned. This is likely the tested case.
|
||||
- Sending to another local-LAN host changes the IP address but keeps that first
|
||||
host's MAC. The Ethernet frame reaches the first host, which drops it
|
||||
because its IP destination names the second host. `sendto()` still reports
|
||||
success.
|
||||
- Several off-link destinations can also appear to work if the stored MAC is
|
||||
the common router MAC. The router is the correct L2 next hop for all of
|
||||
them. This does not make the socket generally correct.
|
||||
- A socket that has neither received a datagram nor been connected has no
|
||||
usable destination MAC. Its first proactive `sendto()` reports success but
|
||||
cannot be delivered correctly.
|
||||
|
||||
Receiving an unrelated datagram can also break a destination that was working.
|
||||
For example, A sends to this UDP port, so the socket stores A's MAC and a
|
||||
later `sendto(A)` works. B then sends any datagram to the same port.
|
||||
`rx_udp()` replaces the stored MAC with B's. The next `sendto(A)` still names
|
||||
A in the IP header, but addresses the Ethernet frame to B; A no longer gets
|
||||
it. The same overwrite occurs after UDP `connect()`, because the CPQ code does
|
||||
not filter non-peer senders. Remote traffic through the same router can hide
|
||||
this again because its Ethernet source MAC remains the router's MAC.
|
||||
|
||||
The two-host remote test covers both cases: [B.5](#b5-first-unconnected-sendto)
|
||||
shows the first `sendto()` without an L2 address, and
|
||||
[B.6](#b6-sendto-corrupts-later-sends-and-uses-a-stale-mac) shows a Laptop IP
|
||||
sent with the helper MAC. Two sockets on one Linux host are not sufficient for
|
||||
the latter because they share one NIC MAC. The bridge prints each outgoing UDP
|
||||
IP destination beside its Ethernet destination MAC, independently confirming
|
||||
the wrong L2 destination.
|
||||
|
||||
(In branch bsdudp, we fixed this by using a framing transporting TX records between the BSD side and the Mongoose side; a correct `sendto()` must wait
|
||||
for ARP resolution without losing that destination, so those records/frames are queued at the send buffer and the extraction then can decide whether to start an ARP, thing that is minimized with a small cache-like table holding 4 entries)
|
||||
|
||||
See [B.5](#b5-first-unconnected-sendto) and
|
||||
[B.6](#b6-sendto-corrupts-later-sends-and-uses-a-stale-mac) for the test
|
||||
evidence.
|
||||
|
||||
### `sendto()` changes the meaning of later `send()` calls
|
||||
|
||||
The same direct update makes an unconnected socket look connected after a
|
||||
`sendto()`. A later `send()` succeeds and uses the previous `sendto()` target,
|
||||
although BSD requires `EDESTADDRREQ` until `connect()` selects a peer. It is
|
||||
state corruption from the application's point of view: an operation intended
|
||||
to name one datagram's destination changes the socket's default destination.
|
||||
|
||||
See [B.6](#b6-sendto-corrupts-later-sends-and-uses-a-stale-mac) for the test
|
||||
evidence.
|
||||
|
||||
### `bind()` followed by UDP `connect()` loses the bound port
|
||||
|
||||
`mg_connect_resolved()` assigns an ephemeral local port unconditionally.
|
||||
Calling `connect()` on a UDP socket already bound to a specific port replaces
|
||||
that port. This is independent of receive buffering; the local identity used
|
||||
by replies changes after `connect()`.
|
||||
|
||||
See [B.7](#b7-bind-followed-by-udp-connect) for the test evidence.
|
||||
|
||||
### `sendto()` on a connected UDP socket reports success
|
||||
|
||||
A connected UDP socket already has a selected peer. `sendto()` must fail with
|
||||
`EISCONN`, but the CPQ implementation accepts it and changes that peer.
|
||||
|
||||
See [B.8](#b8-sendto-on-a-connected-udp-socket) for the test evidence.
|
||||
|
||||
### Oversized UDP writes report success after truncation
|
||||
|
||||
The CPQ code passes an oversized payload through `mg_send()`. The built-in UDP
|
||||
path trims to its payload capacity and transmits only that prefix. BSD callers
|
||||
are told that the whole datagram was sent. A UDP datagram must be sent whole
|
||||
or fail; partial transmission is not meaningful.
|
||||
|
||||
See [B.9](#b9-oversized-udp-write) for the test evidence.
|
||||
|
||||
### Empty datagrams disappear
|
||||
|
||||
An empty UDP datagram does not increase `c->recv.len`. No later `recv()` can
|
||||
distinguish it from no packet, so it returns `EAGAIN` or blocks instead of
|
||||
returning zero. The proposed condition is also false for an empty retained
|
||||
datagram, so it cannot represent or protect one.
|
||||
|
||||
Yes, it has been agreed that is a known and accepted behavior.
|
||||
|
||||
For completeness, see [B.10](#b10-empty-datagram) for the test evidence.
|
||||
|
||||
## bsdudp Branch Fixes and Additions
|
||||
|
||||
These are additional capabilities, not prerequisites for the one-datagram
|
||||
drop policy above:
|
||||
|
||||
- RX drops the complete retained datagram after a short non-peek read.
|
||||
- RX filters packets from non-peer senders on connected UDP sockets.
|
||||
- TX records carry each UDP destination, so queued `sendto()` calls retain
|
||||
their individual targets.
|
||||
- TX triggers and reuses L2 resolution for `sendto()` destinations.
|
||||
- Unconnected `send()` returns `EDESTADDRREQ`; connected UDP `sendto()`
|
||||
returns `EISCONN`.
|
||||
- `bind()` followed by UDP `connect()` keeps the selected local port.
|
||||
- BSD writes reject oversized datagrams with `EMSGSIZE` before queuing them.
|
||||
|
||||
Empty UDP receives still require an explicit marker or framing record and are
|
||||
not solved by either the CPQ patch or the current bsdudp simplification.
|
||||
|
||||
## Appendix A: One-Slot Queue Model
|
||||
|
||||
Let `D` be the time from receiving a datagram until the BSD application reads
|
||||
it. Let `N` peers each send at `r` packets/s, so the aggregate arrival rate is
|
||||
`lambda = N * r`. For independent arrivals approximated as a Poisson process,
|
||||
the probability that at least one later datagram arrives during the unread
|
||||
window is:
|
||||
|
||||
```text
|
||||
P(collision) = 1 - exp(-lambda * D)
|
||||
```
|
||||
|
||||
The table above uses `D = 4 ms`. It is a collision probability, not a precise
|
||||
packet-loss percentage: the latter also depends on the scheduler and on when
|
||||
the application calls `recv()`.
|
||||
|
||||
For evenly spaced periodic traffic, the aggregate packet spacing must be at
|
||||
least `D` to avoid drops. Therefore each of `N` equal-rate peers needs a period
|
||||
of at least `N * D`: with `D = 4 ms`, 10 peers need at least 40 ms per packet
|
||||
per peer, while 100 peers need at least 400 ms.
|
||||
|
||||
## Appendix B: Two-Host Test Evidence
|
||||
|
||||
This run used Mongoose at `192.168.69.231`, the Laptop at
|
||||
`192.168.69.73`, and a Workstation helper at `192.168.69.11`. The Laptop MAC
|
||||
learned for normal control traffic was `3c:0a:f3:8c:ce:ef`; the helper MAC was
|
||||
`06:3f:ad:7e:3f:1c`. Startup and
|
||||
DHCP are omitted below.
|
||||
|
||||
### B.1 CPQ unread-datagram policy
|
||||
|
||||
**Test.** Two peers send `one` and `two` before the application reads port
|
||||
`1234`.
|
||||
|
||||
**Expected.** The CPQ rule retains `one` and drops `two`.
|
||||
|
||||
**Observed.**
|
||||
|
||||
```text
|
||||
Workstation: PASS drop: kept one, dropped two
|
||||
Laptop: no separate observation
|
||||
Bridge: no separate observation
|
||||
```
|
||||
|
||||
**Internal reason.** `rx_udp()` returns once a BSD connection has a nonempty
|
||||
`c->recv`. It is a one-datagram loss queue, not a UDP receive queue.
|
||||
|
||||
### B.2 Short `recvfrom()`
|
||||
|
||||
**Test.** A peer sends one five-byte datagram, `ABCDE`. The application first
|
||||
reads two bytes, then reads again.
|
||||
|
||||
**Expected.** The first call returns `AB`; UDP discards the unread `CDE` tail.
|
||||
The second call returns `EAGAIN` until another datagram arrives.
|
||||
|
||||
**Observed.**
|
||||
|
||||
```text
|
||||
Workstation: FAIL short: expected EAGAIN, got 3 "CDE": tail retained
|
||||
Laptop: sent ABCDE
|
||||
Bridge: no separate observation
|
||||
```
|
||||
|
||||
**Internal reason.** `BSD_OP_RECVFROM` removes only the copied byte count from
|
||||
the stream-shaped `c->recv` iobuf. It has no record length to discard the rest
|
||||
of the datagram.
|
||||
|
||||
### B.3 Connected UDP accepts another peer
|
||||
|
||||
**Test.** The socket connects to the Laptop. The Workstation helper then sends
|
||||
`intruder` to that connected socket.
|
||||
|
||||
**Expected.** The helper datagram is ignored. A following `send()` still goes
|
||||
to the Laptop.
|
||||
|
||||
**Observed.**
|
||||
|
||||
```text
|
||||
Workstation: FAIL peer: expected EAGAIN, got 8 "intruder": no peer filter
|
||||
Mongoose: mg_connect_ 7 192.168.69.231:64103 -> 192.168.69.73:14000
|
||||
Mongoose: mg_connect_ 7 ARP lookup...
|
||||
Mongoose: rx_arp 7 ARP resolved 192.168.69.73 -> 3c:0a:f3:8c:ce:ef
|
||||
Workstation: reply: sent 5 bytes; peer reports its destination below
|
||||
Laptop: FAIL reply: expected control, got none: intruder replaced peer
|
||||
Helper: FAIL connected peer: helper received reply
|
||||
Bridge: UDP IP dst 192.168.69.11 -> Ethernet dst 06:3f:ad:7e:3f:1c
|
||||
```
|
||||
|
||||
**Internal reason.** `mg_connect_resolved()` correctly starts an ARP lookup for
|
||||
the configured Laptop peer and stores its MAC. `rx_udp()` then accepts every
|
||||
sender matching the local port. Before the BSD read runs, it overwrites
|
||||
`c->rem` and the connection return MAC with the helper's source. The later
|
||||
`send()` therefore replies to the helper instead of the peer that was resolved
|
||||
at `connect()` time.
|
||||
|
||||
### B.4 Unconnected `send()`
|
||||
|
||||
**Test.** Send one byte on a bound UDP socket that has never called
|
||||
`connect()` or `sendto()`.
|
||||
|
||||
**Expected.** `send()` fails with `EDESTADDRREQ`: there is no peer.
|
||||
|
||||
**Observed.**
|
||||
|
||||
```text
|
||||
Workstation: FAIL send: expected EDESTADDRREQ, got 1: no peer check
|
||||
Laptop: no packet expected
|
||||
Bridge: UDP IP dst 0.0.0.0 -> Ethernet dst 00:00:00:00:00:00
|
||||
```
|
||||
|
||||
**Internal reason.** `BSD_OP_SEND` calls `mg_send()` without checking whether
|
||||
the UDP socket has a selected peer. The zeroed remote address reaches the L2
|
||||
transmit path.
|
||||
|
||||
### B.5 First unconnected `sendto()`
|
||||
|
||||
**Test.** Call `sendto()` to the Laptop before this socket has received any
|
||||
packet or connected to a peer.
|
||||
|
||||
**Expected.** The implementation resolves the Laptop L2 address, waits if
|
||||
needed, then delivers the datagram.
|
||||
|
||||
**Observed.**
|
||||
|
||||
```text
|
||||
Workstation: FAIL sendto: expected delivery, got 4: no resolution
|
||||
Laptop: FAIL sendto: expected packet, got none
|
||||
Bridge: UDP IP dst 192.168.69.73 -> Ethernet dst 00:00:00:00:00:00
|
||||
```
|
||||
|
||||
**Internal reason.** `BSD_OP_SENDTO` replaces only `c->rem` and immediately
|
||||
calls `mg_send()`. Unlike the preceding `connect()` cases, this operation
|
||||
produces no `mg_connect_ ... ARP lookup...` or `rx_arp ... ARP resolved ...`
|
||||
log for the Laptop destination. It neither performs ARP/L2 resolution nor
|
||||
retains the destination while resolution would be pending. The zero Ethernet
|
||||
destination in the bridge trace is the direct result.
|
||||
|
||||
### B.6 `sendto()` corrupts later sends and uses a stale MAC
|
||||
|
||||
**Test.** The helper sends a datagram to the unconnected socket, establishing
|
||||
the helper MAC. The application then calls `sendto()` to the Laptop and, after
|
||||
that, calls `send()` on the same socket.
|
||||
|
||||
**Expected.** `sendto()` sends to the Laptop MAC without changing the socket's
|
||||
default peer. The later `send()` fails with `EDESTADDRREQ`.
|
||||
|
||||
**Observed.**
|
||||
|
||||
```text
|
||||
Workstation: sendto after RX: got 5
|
||||
Workstation: FAIL send: expected EDESTADDRREQ, got 4: sendto set peer
|
||||
Laptop: FAIL stale MAC: expected packet, got none
|
||||
Bridge: UDP IP dst 192.168.69.73 -> Ethernet dst 3c:0a:f3:8c:ce:ef
|
||||
Bridge: UDP IP dst 192.168.69.73 -> Ethernet dst 06:3f:ad:7e:3f:1c
|
||||
```
|
||||
|
||||
The first bridge line is normal Laptop control traffic. The second names the
|
||||
same Laptop IP but uses the helper MAC. The Laptop receives no packet.
|
||||
|
||||
**Internal reason.** The implementation keeps only one mutable `c->rem` and
|
||||
one return MAC per Mongoose UDP connection. Receiving the helper datagram
|
||||
updates that one MAC to `06:3f:ad:7e:3f:1c`. `sendto()` overwrites `c->rem`,
|
||||
but does not start an ARP lookup for the Laptop and therefore retains the
|
||||
helper MAC. There is no `mg_connect_ ... ARP lookup...` or `rx_arp ... ARP
|
||||
resolved ...` sequence between this `sendto()` and the second bridge line. No
|
||||
per-datagram destination record or neighbor cache exists.
|
||||
|
||||
### B.7 `bind()` followed by UDP `connect()`
|
||||
|
||||
**Test.** Bind local port `1238`, then connect to the Laptop and query the
|
||||
local address.
|
||||
|
||||
**Expected.** The socket remains bound to `1238`.
|
||||
|
||||
**Observed.**
|
||||
|
||||
```text
|
||||
Workstation: FAIL bind: expected 1238, got 64102: connect replaced bind
|
||||
Mongoose: mg_connect_ 5 192.168.69.231:64102 -> 192.168.69.73:14000
|
||||
Mongoose: mg_connect_ 5 ARP lookup...
|
||||
Mongoose: rx_arp 5 ARP resolved 192.168.69.73 -> 3c:0a:f3:8c:ce:ef
|
||||
Laptop: normal ARP/control peer
|
||||
```
|
||||
|
||||
**Internal reason.** `mg_connect_resolved()` assigns an ephemeral local port
|
||||
without preserving the BSD socket's existing bind state.
|
||||
|
||||
### B.8 `sendto()` on a connected UDP socket
|
||||
|
||||
**Test.** After UDP `connect()`, call `sendto()` with a different destination.
|
||||
|
||||
**Expected.** Fail with `EISCONN`; a connected UDP socket uses `send()`.
|
||||
|
||||
**Observed.**
|
||||
|
||||
```text
|
||||
Workstation: FAIL sendto: expected EISCONN, got 1: connected socket accepted it
|
||||
Laptop: no separate observation
|
||||
Bridge: no separate observation
|
||||
```
|
||||
|
||||
**Internal reason.** `BSD_OP_SENDTO` has no connected-UDP state check and
|
||||
blindly replaces the peer address. The connected socket's ARP lookup happened
|
||||
at `connect()` time in B.3; this operation does not validate that state or
|
||||
start resolution for its replacement destination.
|
||||
|
||||
### B.9 Oversized UDP write
|
||||
|
||||
**Test.** Send a 1600-byte datagram.
|
||||
|
||||
**Expected.** Fail with `EMSGSIZE`; UDP cannot partially send one datagram.
|
||||
|
||||
**Observed.**
|
||||
|
||||
```text
|
||||
Workstation: FAIL oversize: expected EMSGSIZE, got 1600: later truncated
|
||||
Laptop: no receiving socket in this run
|
||||
Bridge: no payload-length assertion in this run
|
||||
```
|
||||
|
||||
**Internal reason.** The BSD layer does not reject an over-capacity datagram.
|
||||
The built-in UDP transmit path later limits payload to its available capacity,
|
||||
while the BSD caller has already been told all 1600 bytes were accepted.
|
||||
|
||||
This run proves the false-success API result. A receiver-side payload-length
|
||||
assertion would additionally demonstrate the transmitted prefix length.
|
||||
|
||||
### B.10 Empty datagram
|
||||
|
||||
**Test.** Send a zero-byte UDP datagram, then call `recvfrom()`.
|
||||
|
||||
**Expected.** `recvfrom()` returns zero and preserves the source address.
|
||||
|
||||
**Observed.**
|
||||
|
||||
```text
|
||||
Workstation: FAIL empty: expected 0, got -1 errno 11: no marker
|
||||
Laptop: sent a zero-byte datagram
|
||||
Bridge: no separate observation
|
||||
```
|
||||
|
||||
**Internal reason.** An empty payload does not increase `c->recv.len`; the
|
||||
stream iobuf cannot distinguish an empty datagram from no pending data.
|
||||
@@ -1,6 +1,6 @@
|
||||
PROJECTS ?= $(wildcard ../../tutorials/pico-sdk/*-picosdk-*)
|
||||
SDK_PATH ?= $(realpath $(PWD))/pico-sdk
|
||||
SDK_VERSION ?= 2.1.0
|
||||
SDK_VERSION ?= 2.2.0
|
||||
|
||||
all: $(PROJECTS)
|
||||
echo
|
||||
|
||||
@@ -1519,6 +1519,7 @@ static void test_http_404(void) {
|
||||
|
||||
ASSERT(fetch(&mgr, buf, url, "GET /a.txt HTTP/1.0\n\n") == 200);
|
||||
ASSERT(cmpbody(buf, "hello\n") == 0);
|
||||
ASSERT(fetch(&mgr, buf, url, "GET /a.txt%%00x HTTP/1.0\n\n") == 400);
|
||||
ASSERT(fetch(&mgr, buf, url, "GET /a/a.txt HTTP/1.0\n\n") == 200);
|
||||
ASSERT(cmpbody(buf, "hello\n") == 0);
|
||||
ASSERT(fetch(&mgr, buf, url, "GET /unmapped HTTP/1.0\n\n") == 400);
|
||||
|
||||
@@ -110,6 +110,11 @@
|
||||
setupAutoLogout();
|
||||
document.body.style.visibility = 'visible';
|
||||
refreshFiles();
|
||||
// Static graph is not pushed by the device, so load it once here
|
||||
Dashboard.call('get', 'graph1').then(r => {
|
||||
mkGraph(graphs.graph1);
|
||||
updateGraph1(r?.data);
|
||||
});
|
||||
});
|
||||
Dashboard.login().catch(err => showLogin(true));
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ dashboard.hex: build
|
||||
esputil mkhex \
|
||||
0x8000 build/partition_table/partition-table.bin \
|
||||
0x1000 build/bootloader/bootloader.bin \
|
||||
0x100000 build/mongoose-esp32-example.bin > $@
|
||||
0x20000 build/mongoose-esp32-example.bin > $@
|
||||
|
||||
flash2: dashboard.hex
|
||||
esputil -p $(PORT) -b 921600 -fp 0x220 flash dashboard.hex
|
||||
@@ -41,7 +41,7 @@ flash2: dashboard.hex
|
||||
ESPTOOL ?= esptool.py
|
||||
|
||||
flash3:
|
||||
cd build && $(ESPTOOL) --chip esp32 -p $(PORT) -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 2MB 0x8000 partition_table/partition-table.bin 0x1000 bootloader/bootloader.bin 0x100000 mongoose-esp32-example.bin
|
||||
cd build && $(ESPTOOL) --chip esp32 -p $(PORT) -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 4MB 0x8000 partition_table/partition-table.bin 0x1000 bootloader/bootloader.bin 0x20000 mongoose-esp32-example.bin
|
||||
|
||||
clean:
|
||||
test -d build && $(DOCKER) rm -rf build sdkconfig || true
|
||||
|
||||
@@ -2,5 +2,5 @@
|
||||
nvs, data, nvs, , 0x6000
|
||||
otadata, data, ota, , 0x2000,
|
||||
phy_init, data, phy, , 0x1000,
|
||||
ota_0, app, ota_0, , 948K,
|
||||
ota_1, app, ota_1, , 948K,
|
||||
ota_0, app, ota_0, , 1536K,
|
||||
ota_1, app, ota_1, , 1536K,
|
||||
|
@@ -1,3 +1,5 @@
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
|
||||
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
||||
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#define MG_ARCH MG_ARCH_ESP32
|
||||
#define MG_OTA MG_OTA_ESP32
|
||||
|
||||
// Crash reports and mem usage stats
|
||||
// Make sure to add to sdkconfig.defaults the following:
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
# Name, Type, SubType, Offset, Size, Flags
|
||||
nvs, data, nvs, , 0x6000
|
||||
otadata, data, ota, , 0x2000
|
||||
phy_init, data, phy, , 0x1000
|
||||
factory, app, factory, , 1200K
|
||||
ota_0, app, ota_0, , 1536K
|
||||
ota_1, app, ota_1, , 1536K
|
||||
coredump, data, coredump,, 128K
|
||||
|
@@ -1,3 +1,5 @@
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
|
||||
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
||||
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
|
||||
|
||||
@@ -4,7 +4,7 @@ ROOTDIR = $(realpath $(CURDIR)/../../..)
|
||||
PORT ?= /dev/ttyUSB0
|
||||
DOCKER ?= docker run --rm $(DA) -v $(ROOTDIR):$(ROOTDIR) -w $(THISDIR) espressif/idf:v5.0.4
|
||||
# Note that the esp32 port needs the extra .. for relative paths due to the location of its main CMakeLists.txt file
|
||||
CMD ?= bash -c '$(MAKE) -C micropython/ports/esp32 submodules && $(MAKE) -C micropython/ports/esp32 USER_C_MODULES=../../../../../mongoose/micropython.cmake'
|
||||
CMD ?= bash -c '$(MAKE) -C micropython/ports/esp32 submodules && $(MAKE) -C micropython/ports/esp32 USER_C_MODULES=../../../../mongoose/micropython.cmake'
|
||||
|
||||
all: example
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# Create an INTERFACE library for our C module.
|
||||
add_library(usermod_mongoose INTERFACE)
|
||||
|
||||
# Add our source files to the lib
|
||||
target_sources(usermod_mongoose INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../mongoose/module.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../../../mongoose.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/main.c
|
||||
)
|
||||
|
||||
# Add the current directory as an include directory.
|
||||
target_include_directories(usermod_mongoose INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../../../
|
||||
)
|
||||
|
||||
# Link our INTERFACE library to the usermod target.
|
||||
target_link_libraries(usermod INTERFACE usermod_mongoose)
|
||||
@@ -31,5 +31,5 @@ clean:
|
||||
$(RM) build pico_sdk_import.cmake $(SDK_PATH)
|
||||
|
||||
$(SDK_PATH):
|
||||
git clone -c advice.detachedHead=false --quiet --depth 1 -b 2.1.1 https://github.com/raspberrypi/pico-sdk $@
|
||||
git clone -c advice.detachedHead=false --quiet --depth 1 -b 2.2.0 https://github.com/raspberrypi/pico-sdk $@
|
||||
cd $@ && git submodule update --quiet --init
|
||||
@@ -31,5 +31,5 @@ clean:
|
||||
$(RM) build pico_sdk_import.cmake $(SDK_PATH)
|
||||
|
||||
$(SDK_PATH):
|
||||
git clone -c advice.detachedHead=false --quiet --depth 1 -b 2.1.1 https://github.com/raspberrypi/pico-sdk $@
|
||||
git clone -c advice.detachedHead=false --quiet --depth 1 -b 2.2.0 https://github.com/raspberrypi/pico-sdk $@
|
||||
cd $@ && git submodule update --quiet --init
|
||||
@@ -19,7 +19,7 @@ net.h: ../../mqtt/mqtt-dashboard/device/net.h
|
||||
cp $< $@
|
||||
|
||||
pico-sdk:
|
||||
git clone --depth 1 -b 2.1.0 https://github.com/raspberrypi/pico-sdk $@
|
||||
git clone --depth 1 -b 2.2.0 https://github.com/raspberrypi/pico-sdk $@
|
||||
cd $@ && git submodule update --init
|
||||
|
||||
clean:
|
||||
|
||||
@@ -31,5 +31,5 @@ clean:
|
||||
$(RM) build pico_sdk_import.cmake $(SDK_PATH)
|
||||
|
||||
$(SDK_PATH):
|
||||
git clone -c advice.detachedHead=false --quiet --depth 1 -b 2.1.1 https://github.com/raspberrypi/pico-sdk $@
|
||||
git clone -c advice.detachedHead=false --quiet --depth 1 -b 2.2.0 https://github.com/raspberrypi/pico-sdk $@
|
||||
cd $@ && git submodule update --quiet --init
|
||||
@@ -31,7 +31,7 @@ clean:
|
||||
$(RM) build pico_sdk_import.cmake $(SDK_PATH)
|
||||
|
||||
$(SDK_PATH):
|
||||
git clone -c advice.detachedHead=false --quiet --depth 1 -b 2.1.1 https://github.com/raspberrypi/pico-sdk $@
|
||||
git clone -c advice.detachedHead=false --quiet --depth 1 -b 2.2.0 https://github.com/raspberrypi/pico-sdk $@
|
||||
cd $@ && git submodule update --quiet --init
|
||||
|
||||
# Automated remote test. Requires env variable VCON_API_KEY set. See https://vcon.io/automated-firmware-tests/
|
||||
|
||||
@@ -31,7 +31,7 @@ clean:
|
||||
$(RM) build pico_sdk_import.cmake $(SDK_PATH)
|
||||
|
||||
$(SDK_PATH):
|
||||
git clone -c advice.detachedHead=false --quiet --depth 1 -b 2.1.1 https://github.com/raspberrypi/pico-sdk $@
|
||||
git clone -c advice.detachedHead=false --quiet --depth 1 -b 2.2.0 https://github.com/raspberrypi/pico-sdk $@
|
||||
cd $@ && git submodule update --quiet --init
|
||||
|
||||
# Automated remote test. Requires env variable VCON_API_KEY set. See https://vcon.io/automated-firmware-tests/
|
||||
|
||||
@@ -1,31 +1,10 @@
|
||||
# Nucleo-H563ZI Mongoose Integration
|
||||
# Mongoose CubeMX pack
|
||||
|
||||
This guide explains how to bring up a production-grade Web UI dashboard on the
|
||||
STM32 Nucleo-H563ZI development board.
|
||||
This pack provides an easy to use CubeMX integration.
|
||||
|
||||
A pre-configured, ready-to-run project zip is available for download at
|
||||
https://mongoose.ws.
|
||||
## Mongoose Networking Core
|
||||
|
||||
To integrate the Web UI dashboard into an existing CubeMX project, or to create a
|
||||
new project from scratch:
|
||||
This module installs Mongoose Library itself: `mogoose.c`, `mongoose.h` and
|
||||
`mongoose_config.h`. To finish integration, follow this guide:
|
||||
|
||||
1. Configure the peripherals (described below).
|
||||
2. Regenerate your project code and integrate Mongoose core by following
|
||||
https://mongoose.ws/docs/guides/stm32-tcpip-stack/.
|
||||
3. Add the Web UI dashboard by following
|
||||
https://mongoose.ws/docs/guides/web-dashboard-guide/.
|
||||
|
||||
## Configure peripherals
|
||||
|
||||
An `.ioc` file is included in this directory for your reference. It was created
|
||||
using the following steps:
|
||||
|
||||
- Open CubeMX. If you are starting a new project, select the STM32H563ZI
|
||||
microcontroller and create a new project.
|
||||
- In Pinout & Configuration / Security, enable RNG and select "Activated".
|
||||
- In Clock Configuration, set the maximum clock frequency to 250 MHz.
|
||||
- In the MCU pinout view, set pins PB0, PF4, and PG4 to GPIO_Output mode.
|
||||
- In Pinout & Configuration / Connectivity, enable USART3 in asynchronous mode.
|
||||
Configure pins PD8 and PD9 for USART.
|
||||
- In Pinout & Configuration / Connectivity, enable ETH in RMII mode and set pins
|
||||
PA1, PA2, PA7, PC1, PC4, PC5, PB15, PG11, and PG13 for Ethernet.
|
||||
https://mongoose.ws/docs/guides/stm32-tcpip-stack/#configure-peripherals
|
||||
@@ -110,6 +110,11 @@
|
||||
setupAutoLogout();
|
||||
document.body.style.visibility = 'visible';
|
||||
refreshFiles();
|
||||
// Static graph is not pushed by the device, so load it once here
|
||||
Dashboard.call('get', 'graph1').then(r => {
|
||||
mkGraph(graphs.graph1);
|
||||
updateGraph1(r?.data);
|
||||
});
|
||||
});
|
||||
Dashboard.login().catch(err => showLogin(true));
|
||||
|
||||
@@ -778,18 +783,18 @@ a, a:visited, a:link { text-decoration: none; }
|
||||
</div>
|
||||
|
||||
<div class="note">
|
||||
<p>This file manager uses /fs/ directory.
|
||||
<p>This file manager uses /tmp/dashboard/ directory.
|
||||
It works on any platform that supports
|
||||
standard file IO: fopen, fread, fwrite, fclose.
|
||||
This includes Linux/Mac/Windows.</p>
|
||||
<p>
|
||||
Mongoose provides a built-in LittleFS integration that
|
||||
makes standard file IO work on embedded systems like STM32.
|
||||
makes stadard file IO work on embedded systems like STM32.
|
||||
For that, add "#define MG_ENABLE_LFS 1" to your
|
||||
mongoose_config.h
|
||||
</p>
|
||||
<p>See
|
||||
<a href="https://github.com/cesanta/mongoose/tree/master/tutorials/stm32/nucleo-h563zi/cubemx">this ready-to-build example</a>
|
||||
<a href="https://github.com/cesanta/embedded-web-file-manager">this ready-to-build example</a>
|
||||
which implements LittleFS on Nucleo-H563ZI internal flash. You
|
||||
can upload, delete and serve files on STM32 - which is
|
||||
a perfect use case for confuration files, keeping and viewing logs, etc.
|
||||
|
||||
Reference in new issue
Block a user