From 6f4a783863112c8cb2fc358beffae4a32664cce7 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Mon, 8 Jan 2024 17:34:34 -0300 Subject: [PATCH] remove fn_data from event handler signature --- .../arduino/teensy41-http/teensy41-http.ino | 2 +- examples/arduino/w5500-http/w5500-http.ino | 2 +- examples/arduino/w5500-mqtt/net.c | 2 +- examples/captive-dns-server/main.c | 3 +- examples/device-dashboard/net.c | 7 +- examples/embedded-filesystem/main.c | 3 +- examples/esp32/micropython/mongoose/main.c | 3 +- .../http-client-server/src/main/main.c | 6 +- examples/file-upload-html-form/main.c | 3 +- examples/file-upload-multiple-posts/main.c | 3 +- examples/file-upload-single-post/main.c | 4 +- examples/http-client/main.c | 6 +- examples/http-proxy-client/main.c | 4 +- examples/http-restful-server/main.c | 5 +- examples/http-reverse-proxy/main.c | 8 +- examples/http-server/main.c | 3 +- examples/http-streaming-client/main.c | 6 +- examples/huge-response/main.c | 3 +- examples/json-rpc-over-websocket/main.c | 3 +- examples/live-log/main.c | 3 +- .../microchip/same54-xpro/mqtt-client/main.c | 3 +- examples/micropython/mongoose/main.c | 3 +- examples/mip-pcap/main.c | 10 +- examples/modbus-dashboard/net.c | 11 +- examples/mqtt-client-aws-iot/main.c | 4 +- examples/mqtt-client/main.c | 3 +- examples/mqtt-dashboard/device/net.c | 3 +- examples/mqtt-over-ws-client/main.c | 4 +- examples/mqtt-server/main.c | 3 +- examples/multi-threaded-12m/main.c | 3 +- examples/multi-threaded/main.c | 3 +- .../rt1020-evk-make-baremetal-builtin/main.c | 4 +- .../rt1020-evk-make-freertos-builtin/main.c | 4 +- examples/rp2040/pico-rmii/main.c | 2 +- examples/smtp-client/main.c | 4 +- examples/sntp-time-sync/main.c | 4 +- examples/socks5-server/main.c | 6 +- .../main.c | 3 +- .../main.c | 3 +- .../nucleo-g031-make-baremetal-builtin/main.c | 4 +- examples/tcp/main.c | 9 +- .../main.c | 3 +- examples/timers/main.c | 3 +- examples/udp-ssdp-search/main.c | 4 +- examples/video-stream/main.c | 3 +- .../ch32v307-make-baremetal-builtin/main.c | 2 +- examples/websocket-client/main.c | 4 +- examples/websocket-server/main.c | 3 +- examples/webui-login/main.c | 3 +- examples/webui-plain/main.c | 3 +- examples/webui-push-rest/main.c | 3 +- examples/webui-push-ws/main.c | 3 +- examples/webui-rest/main.c | 3 +- examples/wifi-router-dashboard/net.c | 6 +- examples/zephyr/http-client/src/main.c | 8 +- examples/zephyr/http-server/src/main.c | 6 +- examples/zephyr/mqtt-aws-client/src/main.c | 4 +- examples/zephyr/websocket-server/src/main.c | 5 +- mongoose.c | 1105 ++++++++--------- mongoose.h | 2 +- src/dns.c | 4 +- src/event.c | 6 +- src/event.h | 2 +- src/http.c | 17 +- src/mqtt.c | 7 +- src/sntp.c | 5 +- src/sock.c | 4 +- src/ws.c | 4 +- test/fuzz.c | 5 +- test/mip_tap_test.c | 13 +- test/mip_test.c | 16 +- test/unit_test.c | 136 +- 72 files changed, 750 insertions(+), 814 deletions(-) diff --git a/examples/arduino/teensy41-http/teensy41-http.ino b/examples/arduino/teensy41-http/teensy41-http.ino index f93686305f5..90a6d54e902 100644 --- a/examples/arduino/teensy41-http/teensy41-http.ino +++ b/examples/arduino/teensy41-http/teensy41-http.ino @@ -25,7 +25,7 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function // Simple HTTP server that runs on port 8000 // Mongoose event handler function, gets called by the mg_mgr_poll() // See https://mongoose.ws/documentation/#2-minute-integration-guide -static void simple_http_listener(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void simple_http_listener(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { // The MG_EV_HTTP_MSG event means HTTP request. `hm` holds parsed request, // see https://mongoose.ws/documentation/#struct-mg_http_message diff --git a/examples/arduino/w5500-http/w5500-http.ino b/examples/arduino/w5500-http/w5500-http.ino index 93959785a9b..abf3fe75597 100644 --- a/examples/arduino/w5500-http/w5500-http.ino +++ b/examples/arduino/w5500-http/w5500-http.ino @@ -38,7 +38,7 @@ void setup() { // Setup HTTP listener. Respond "ok" on any HTTP request mg_http_listen( &mgr, "http://0.0.0.0", - [](struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + [](struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) mg_http_reply(c, 200, "", "ok\n"); }, &mgr); diff --git a/examples/arduino/w5500-mqtt/net.c b/examples/arduino/w5500-mqtt/net.c index 07f90dd9fe8..e756410b2c9 100644 --- a/examples/arduino/w5500-mqtt/net.c +++ b/examples/arduino/w5500-mqtt/net.c @@ -15,7 +15,7 @@ void mqtt_publish(const char *message) { if (mqtt_connection) mg_mqtt_pub(mqtt_connection, &opts); } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_MQTT_OPEN) { MG_INFO(("%lu CONNECTED to %s", c->id, MQTT_SERVER)); struct mg_mqtt_opts opts = {}; diff --git a/examples/captive-dns-server/main.c b/examples/captive-dns-server/main.c index e69e501b047..db60f53e6fc 100644 --- a/examples/captive-dns-server/main.c +++ b/examples/captive-dns-server/main.c @@ -20,7 +20,7 @@ uint8_t answer[] = { 1, 2, 3, 4 // 4 bytes - IP address }; -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { c->is_hexdumping = 1; } else if (ev == MG_EV_READ) { @@ -41,7 +41,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } mg_iobuf_del(&c->recv, 0, c->recv.len); } - (void) fn_data; (void) ev_data; } diff --git a/examples/device-dashboard/net.c b/examples/device-dashboard/net.c index 91d295114c3..2d8d3b1c1f4 100644 --- a/examples/device-dashboard/net.c +++ b/examples/device-dashboard/net.c @@ -51,7 +51,7 @@ int ui_event_next(int no, struct ui_event *e) { // SNTP connection event handler. When we get a response from an SNTP server, // adjust s_boot_timestamp. We'll get a valid time from that point on -static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void sfn(struct mg_connection *c, int ev, void *ev_data) { uint64_t *expiration_time = (uint64_t *) c->data; if (ev == MG_EV_OPEN) { *expiration_time = mg_millis() + 3000; // Store expiration time in 3s @@ -62,7 +62,6 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } else if (ev == MG_EV_POLL) { if (mg_millis() > *expiration_time) c->is_closing = 1; } - (void) fn_data; } static void timer_sntp_fn(void *param) { // SNTP timer function. Sync up time @@ -262,9 +261,9 @@ static void handle_device_eraselast(struct mg_connection *c) { } // HTTP request handler function -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_ACCEPT) { - if (fn_data != NULL) { // TLS listener! + if (c->fn_data != NULL) { // TLS listener! struct mg_tls_opts opts = {0}; opts.cert = mg_unpacked("/certs/server_cert.pem"); opts.key = mg_unpacked("/certs/server_key.pem"); diff --git a/examples/embedded-filesystem/main.c b/examples/embedded-filesystem/main.c index 3a42fdf3f02..c1813266cc0 100644 --- a/examples/embedded-filesystem/main.c +++ b/examples/embedded-filesystem/main.c @@ -6,7 +6,7 @@ const char *s_listening_url = "http://0.0.0.0:8000"; // HTTP request handler function -void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_serve_opts opts = { .root_dir = "/web_root", @@ -14,7 +14,6 @@ void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { }; mg_http_serve_dir(c, ev_data, &opts); } - (void) fn_data; } int main(void) { diff --git a/examples/esp32/micropython/mongoose/main.c b/examples/esp32/micropython/mongoose/main.c index 6a72d00507f..fa2914a2518 100644 --- a/examples/esp32/micropython/mongoose/main.c +++ b/examples/esp32/micropython/mongoose/main.c @@ -8,9 +8,8 @@ static void t_fn(void *arg) { // Tick periodically (void) arg; } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) mg_http_reply(c, 200, "", "hi\n"); - (void) fn_data; (void) ev_data; } diff --git a/examples/esp8266/http-client-server/src/main/main.c b/examples/esp8266/http-client-server/src/main/main.c index 8399f37daa7..6aa027e8db9 100644 --- a/examples/esp8266/http-client-server/src/main/main.c +++ b/examples/esp8266/http-client-server/src/main/main.c @@ -9,8 +9,8 @@ #define SERVER_URL "http://0.0.0.0:80" #define CLIENT_URL "http://info.cern.ch" -// Event handler for an server (accepted) connection -static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +// Event handler for a server (accepted) connection +static void cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { mg_http_reply(c, 200, "", "Hello from ESP!\n"); } @@ -20,7 +20,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { // To enable TLS for HTTP, // 1. Copy "ca.pem" file to the ESP8266 flash FS // 2. Add TLS init snippet for the connection, see examples/http-client -static void cb2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void cb2(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_CONNECT) { struct mg_str s = mg_url_host(CLIENT_URL); mg_printf(c, "GET / HTTP/1.0\r\nHost: %.*s\r\n\r\n", (int) s.len, s.ptr); diff --git a/examples/file-upload-html-form/main.c b/examples/file-upload-html-form/main.c index 8fa06e20425..ce08a515903 100644 --- a/examples/file-upload-html-form/main.c +++ b/examples/file-upload-html-form/main.c @@ -16,7 +16,7 @@ // // Also, consider changing -DMG_IO_SIZE=SOME_BIG_VALUE to increase IO buffer // increment when reading data. -static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; MG_INFO(("New request to: [%.*s], body size: %lu", (int) hm->uri.len, @@ -35,7 +35,6 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_serve_dir(c, ev_data, &opts); } } - (void) fn_data; } int main(void) { diff --git a/examples/file-upload-multiple-posts/main.c b/examples/file-upload-multiple-posts/main.c index b1b8c4b9e70..8895da52f31 100644 --- a/examples/file-upload-multiple-posts/main.c +++ b/examples/file-upload-multiple-posts/main.c @@ -6,7 +6,7 @@ // HTTP request handler function. It implements the following endpoints: // /upload - Saves the next file chunk // all other URI - serves web_root/ directory -static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/upload")) { @@ -25,7 +25,6 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_serve_dir(c, ev_data, &opts); } } - (void) fn_data; } int main(void) { diff --git a/examples/file-upload-single-post/main.c b/examples/file-upload-single-post/main.c index 982e918e1fc..b9bd64231b7 100644 --- a/examples/file-upload-single-post/main.c +++ b/examples/file-upload-single-post/main.c @@ -11,7 +11,7 @@ // HTTP request handler function. It implements the following endpoints: // /upload - Saves the next file chunk // all other URI - serves web_root/ directory -static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_READ) { // Parse the incoming data ourselves. If we can parse the request, // store two size_t variables in the c->data: expected len and recv len. @@ -38,7 +38,7 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } } } - (void) fn_data, (void) ev_data; + (void) ev_data; } int main(void) { diff --git a/examples/http-client/main.c b/examples/http-client/main.c index a1f76cdd71f..965b220798f 100644 --- a/examples/http-client/main.c +++ b/examples/http-client/main.c @@ -15,7 +15,7 @@ static const char *s_post_data = NULL; // POST data static const uint64_t s_timeout_ms = 1500; // Connect timeout in milliseconds // Print HTTP response and signal that we're done -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { // Connection created. Store connect expiration time in c->data *(uint64_t *) c->data = mg_millis() + s_timeout_ms; @@ -50,9 +50,9 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; printf("%.*s", (int) hm->message.len, hm->message.ptr); c->is_draining = 1; // Tell mongoose to close this connection - *(bool *) fn_data = true; // Tell event loop to stop + *(bool *) c->fn_data = true; // Tell event loop to stop } else if (ev == MG_EV_ERROR) { - *(bool *) fn_data = true; // Error, tell event loop to stop + *(bool *) c->fn_data = true; // Error, tell event loop to stop } } diff --git a/examples/http-proxy-client/main.c b/examples/http-proxy-client/main.c index 35adb96b5ba..aea5d00015f 100644 --- a/examples/http-proxy-client/main.c +++ b/examples/http-proxy-client/main.c @@ -10,8 +10,8 @@ #include "mongoose.h" // Print HTTP response and signal that we're done -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - const char *url = fn_data; +static void fn(struct mg_connection *c, int ev, void *ev_data) { + const char *url = c->fn_data; static bool connected; if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; diff --git a/examples/http-restful-server/main.c b/examples/http-restful-server/main.c index 9e2b76d41b2..b00c5d1eec0 100644 --- a/examples/http-restful-server/main.c +++ b/examples/http-restful-server/main.c @@ -56,8 +56,8 @@ static const char *s_tls_key = // We use the same event handler function for HTTP and HTTPS connections // fn_data is NULL for plain HTTP, and non-NULL for HTTPS -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - if (ev == MG_EV_ACCEPT && fn_data != NULL) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { + if (ev == MG_EV_ACCEPT && c->fn_data != NULL) { struct mg_tls_opts opts = { #ifdef TLS_TWOWAY .ca = mg_str(s_tls_ca), @@ -89,7 +89,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_serve_dir(c, ev_data, &opts); } } - (void) fn_data; } int main(void) { diff --git a/examples/http-reverse-proxy/main.c b/examples/http-reverse-proxy/main.c index 2d7d47c1351..d61225d0185 100644 --- a/examples/http-reverse-proxy/main.c +++ b/examples/http-reverse-proxy/main.c @@ -36,8 +36,8 @@ static void forward_request(struct mg_http_message *hm, (int) hm->uri.len, hm->uri.ptr)); } -static void fn2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - struct mg_connection *c2 = (struct mg_connection *) fn_data; +static void fn2(struct mg_connection *c, int ev, void *ev_data) { + struct mg_connection *c2 = (struct mg_connection *) c->fn_data; if (ev == MG_EV_READ) { // All incoming data from the backend, forward to the client if (c2 != NULL) mg_send(c2, c->recv.buf, c->recv.len); @@ -48,8 +48,8 @@ static void fn2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { (void) ev_data; } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - struct mg_connection *c2 = fn_data; +static void fn(struct mg_connection *c, int ev, void *ev_data) { + struct mg_connection *c2 = c->fn_data; if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; // Client request, create backend connection Note that we're passing diff --git a/examples/http-server/main.c b/examples/http-server/main.c index f2efbc5feb6..9e1e19ae5a7 100644 --- a/examples/http-server/main.c +++ b/examples/http-server/main.c @@ -18,7 +18,7 @@ static void signal_handler(int signo) { // Event handler for the listening connection. // Simply serve static files from `s_root_dir` -static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = ev_data, tmp = {0}; struct mg_str unknown = mg_str_n("?", 1), *cl; @@ -33,7 +33,6 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { (int) hm->uri.len, hm->uri.ptr, (int) tmp.uri.len, tmp.uri.ptr, (int) cl->len, cl->ptr)); } - (void) fn_data; } static void usage(const char *prog) { diff --git a/examples/http-streaming-client/main.c b/examples/http-streaming-client/main.c index 5c46a1a8070..a36c17f22aa 100644 --- a/examples/http-streaming-client/main.c +++ b/examples/http-streaming-client/main.c @@ -15,7 +15,7 @@ static const char *s_url = "http://info.cern.ch"; // Print HTTP response and signal that we're done -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_CONNECT) { // Connected to server. Extract host name from URL struct mg_str host = mg_url_host(s_url); @@ -54,9 +54,9 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } } } else if (ev == MG_EV_CLOSE) { - *(bool *) fn_data = true; // tell event loop to stop + *(bool *) c->fn_data = true; // tell event loop to stop } else if (ev == MG_EV_ERROR) { - *(bool *) fn_data = true; // Error, tell event loop to stop + *(bool *) c->fn_data = true; // Error, tell event loop to stop } (void) ev_data; } diff --git a/examples/huge-response/main.c b/examples/huge-response/main.c index 209face52f7..3cc164fcc5a 100644 --- a/examples/huge-response/main.c +++ b/examples/huge-response/main.c @@ -41,7 +41,7 @@ static size_t printdata(mg_pfn_t out, void *ptr, va_list *ap) { return n; } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = ev_data; if (mg_http_match_uri(hm, "/api/data")) { @@ -66,7 +66,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_serve_dir(c, hm, &opts); } } - (void) fn_data; } static void timer_fn(void *arg) { diff --git a/examples/json-rpc-over-websocket/main.c b/examples/json-rpc-over-websocket/main.c index 308aa397652..2a4ce3aa3fb 100644 --- a/examples/json-rpc-over-websocket/main.c +++ b/examples/json-rpc-over-websocket/main.c @@ -26,7 +26,7 @@ static void rpc_mul(struct mg_rpc_req *r) { // This RESTful server implements the following endpoints: // /websocket - upgrade to Websocket, and implement websocket echo server // any other URI serves static files from s_web_root -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { // c->is_hexdumping = 1; } else if (ev == MG_EV_WS_OPEN) { @@ -51,7 +51,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (io.buf) mg_ws_send(c, (char *) io.buf, io.len, WEBSOCKET_OP_TEXT); mg_iobuf_free(&io); } - (void) fn_data; } static void timer_fn(void *arg) { diff --git a/examples/live-log/main.c b/examples/live-log/main.c index b4b198b2b6b..204e0c85329 100644 --- a/examples/live-log/main.c +++ b/examples/live-log/main.c @@ -7,7 +7,7 @@ // /api/log/static - returns contents of log.txt file // /api/log/live - hangs forever, and returns live log messages // all other URI - serves web_root/ directory -static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/log/static")) { @@ -21,7 +21,6 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_serve_dir(c, ev_data, &opts); } } - (void) fn_data; } static void log_message(const char *filename, const char *message) { diff --git a/examples/microchip/same54-xpro/mqtt-client/main.c b/examples/microchip/same54-xpro/mqtt-client/main.c index 0988574a6ea..8656f4d4475 100644 --- a/examples/microchip/same54-xpro/mqtt-client/main.c +++ b/examples/microchip/same54-xpro/mqtt-client/main.c @@ -31,7 +31,7 @@ void mg_random(void *buf, size_t len) { // Use on-board RNG } } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { MG_INFO(("%lu CREATED", c->id)); // c->is_hexdumping = 1; @@ -66,7 +66,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { MG_INFO(("%lu CLOSED", c->id)); s_conn = NULL; // Mark that we're closed } - (void) fn_data; } // Timer function - recreate client connection if it is closed diff --git a/examples/micropython/mongoose/main.c b/examples/micropython/mongoose/main.c index c600b2ab429..c3c413f42f2 100644 --- a/examples/micropython/mongoose/main.c +++ b/examples/micropython/mongoose/main.c @@ -8,9 +8,8 @@ static void t_fn(void *arg) { // Tick periodically (void) arg; } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) mg_http_reply(c, 200, "", "hi\n"); - (void) fn_data; (void) ev_data; } diff --git a/examples/mip-pcap/main.c b/examples/mip-pcap/main.c index 81935ad4a36..87b5679b04d 100644 --- a/examples/mip-pcap/main.c +++ b/examples/mip-pcap/main.c @@ -42,20 +42,20 @@ static size_t pcap_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) { return received; } -static void fn2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn2(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; MG_DEBUG(("Got response (%d) %.*s...", (int) hm->message.len, 12, hm->message.ptr)); c->is_draining = 1; } else if (ev == MG_EV_CONNECT) { - mg_printf(c, "GET %s HTTP/1.1\r\n\r\n", mg_url_uri((char *) fn_data)); + mg_printf(c, "GET %s HTTP/1.1\r\n\r\n", mg_url_uri((char *) c->fn_data)); } else if (ev == MG_EV_CLOSE) { - free(fn_data); + free(c->fn_data); } } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/debug")) { @@ -75,7 +75,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { hm->message.ptr); } } - (void) ev_data, (void) fn_data; + (void) ev_data; } int main(int argc, char *argv[]) { diff --git a/examples/modbus-dashboard/net.c b/examples/modbus-dashboard/net.c index d261708f8f1..aef60afd14d 100644 --- a/examples/modbus-dashboard/net.c +++ b/examples/modbus-dashboard/net.c @@ -40,7 +40,7 @@ uint64_t mg_now(void) { // SNTP connection event handler. When we get a response from an SNTP server, // adjust s_boot_timestamp. We'll get a valid time from that point on -static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void sfn(struct mg_connection *c, int ev, void *ev_data) { uint64_t *expiration_time = (uint64_t *) c->data; if (ev == MG_EV_OPEN) { *expiration_time = mg_millis() + 3000; // Store expiration time in 3s @@ -51,7 +51,6 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } else if (ev == MG_EV_POLL) { if (mg_millis() > *expiration_time) c->is_closing = 1; } - (void) fn_data; } // SNTP timer function. Sync up time @@ -99,7 +98,7 @@ static void handle_settings_get(struct mg_connection *c) { } // Modbus handler function -static void mfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void mfn(struct mg_connection *c, int ev, void *ev_data) { struct conndata *cd = (struct conndata *) c->data; if (ev == MG_EV_READ) { MG_INFO(("%lu RECEIVED %lu", c->id, c->recv.len)); @@ -117,7 +116,7 @@ static void mfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { c->is_closing = 1; } } - (void) ev_data, (void) fn_data; + (void) ev_data; } static void send8(struct mg_connection *c, uint8_t val) { @@ -242,10 +241,10 @@ static size_t print_mb_resp(void (*out)(char, void *), void *ptr, va_list *ap) { } // HTTP request handler function -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { struct conndata *cd = (struct conndata *) c->data; if (ev == MG_EV_ACCEPT) { - if (fn_data != NULL) { // TLS listener! + if (c->fn_data != NULL) { // TLS listener! struct mg_tls_opts opts = {0}; opts.cert = mg_unpacked("/certs/server_cert.pem"); opts.key = mg_unpacked("/certs/server_key.pem"); diff --git a/examples/mqtt-client-aws-iot/main.c b/examples/mqtt-client-aws-iot/main.c index 808c644501f..62f4b85c5a1 100644 --- a/examples/mqtt-client-aws-iot/main.c +++ b/examples/mqtt-client-aws-iot/main.c @@ -38,7 +38,7 @@ static int s_qos = 1; #include "mongoose.h" -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { // c->is_hexdumping = 1; } else if (ev == MG_EV_CONNECT) { @@ -84,7 +84,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) { MG_INFO(("Got event %d, stopping...", ev)); - *(bool *) fn_data = true; // Signal that we're done + *(bool *) c->fn_data = true; // Signal that we're done } } diff --git a/examples/mqtt-client/main.c b/examples/mqtt-client/main.c index c715de988c2..1012ca4f49b 100644 --- a/examples/mqtt-client/main.c +++ b/examples/mqtt-client/main.c @@ -24,7 +24,7 @@ static void signal_handler(int signo) { s_signo = signo; } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { MG_INFO(("%lu CREATED", c->id)); // c->is_hexdumping = 1; @@ -65,7 +65,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { MG_INFO(("%lu CLOSED", c->id)); s_conn = NULL; // Mark that we're closed } - (void) fn_data; } // Timer function - recreate client connection if it is closed diff --git a/examples/mqtt-dashboard/device/net.c b/examples/mqtt-dashboard/device/net.c index f86bbcd9e84..2fec222b5e9 100644 --- a/examples/mqtt-dashboard/device/net.c +++ b/examples/mqtt-dashboard/device/net.c @@ -190,7 +190,7 @@ static void rpc_ota_upload(struct mg_rpc_req *r) { } } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { MG_INFO(("%lu CREATED", c->id)); // c->is_hexdumping = 1; @@ -223,7 +223,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { MG_INFO(("%lu CLOSED", c->id)); s_conn = NULL; // Mark that we're closed } - (void) fn_data; } // Timer function - recreate client connection if it is closed diff --git a/examples/mqtt-over-ws-client/main.c b/examples/mqtt-over-ws-client/main.c index c9d0008b538..74d2633b1a3 100644 --- a/examples/mqtt-over-ws-client/main.c +++ b/examples/mqtt-over-ws-client/main.c @@ -20,7 +20,7 @@ static const char *s_url = static const char *s_topic = "mg/test"; -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_ERROR) { // On error, log error message MG_ERROR(("%p %s", c->fd, (char *) ev_data)); @@ -89,7 +89,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) { - *(bool *) fn_data = true; // Signal that we're done + *(bool *) c->fn_data = true; // Signal that we're done } } diff --git a/examples/mqtt-server/main.c b/examples/mqtt-server/main.c index 0b96ece494a..77835e5f052 100644 --- a/examples/mqtt-server/main.c +++ b/examples/mqtt-server/main.c @@ -53,7 +53,7 @@ size_t mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic, } // Event handler function -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_MQTT_CMD) { struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data; MG_DEBUG(("cmd %d qos %d", mm->cmd, mm->qos)); @@ -131,7 +131,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { LIST_DELETE(struct sub, &s_subs, sub); } } - (void) fn_data; } int main(void) { diff --git a/examples/multi-threaded-12m/main.c b/examples/multi-threaded-12m/main.c index 64f49c1f17d..63bdcf527df 100644 --- a/examples/multi-threaded-12m/main.c +++ b/examples/multi-threaded-12m/main.c @@ -43,7 +43,7 @@ static void *thread_function(void *param) { } // HTTP request callback -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN && c->is_listening) { // Start worker thread struct thread_data *data = calloc(1, sizeof(*data)); // Worker owns it @@ -75,7 +75,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_ws_send(wc, data->ptr, data->len, WEBSOCKET_OP_TEXT); } } - (void) fn_data; } int main(void) { diff --git a/examples/multi-threaded/main.c b/examples/multi-threaded/main.c index 83ba42d0262..b220cfa52e9 100644 --- a/examples/multi-threaded/main.c +++ b/examples/multi-threaded/main.c @@ -39,7 +39,7 @@ static void *thread_function(void *param) { } // HTTP request callback -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/fast")) { @@ -58,7 +58,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { struct mg_str *data = (struct mg_str *) ev_data; mg_http_reply(c, 200, "", "Result: %.*s\n", data->len, data->ptr); } - (void) fn_data; } int main(void) { diff --git a/examples/nxp/rt1020-evk-make-baremetal-builtin/main.c b/examples/nxp/rt1020-evk-make-baremetal-builtin/main.c index e41e5cd0fce..e44b248caae 100644 --- a/examples/nxp/rt1020-evk-make-baremetal-builtin/main.c +++ b/examples/nxp/rt1020-evk-make-baremetal-builtin/main.c @@ -32,8 +32,8 @@ static void timer_fn(void *arg) { } #ifndef RUNINFLASH -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) fn_data; +static void fn(struct mg_connection *c, int ev, void *ev_data) { + struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->fn_data; if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/hello")) { // Request to /api/hello diff --git a/examples/nxp/rt1020-evk-make-freertos-builtin/main.c b/examples/nxp/rt1020-evk-make-freertos-builtin/main.c index 0cf3bd027b1..ea7bd3a3388 100644 --- a/examples/nxp/rt1020-evk-make-freertos-builtin/main.c +++ b/examples/nxp/rt1020-evk-make-freertos-builtin/main.c @@ -21,8 +21,8 @@ static void timer_fn(void *arg) { ifp->ndrop, ifp->nerr)); } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) fn_data; +static void fn(struct mg_connection *c, int ev, void *ev_data) { + struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->fn_data; if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/hello")) { // Request to /api/hello diff --git a/examples/rp2040/pico-rmii/main.c b/examples/rp2040/pico-rmii/main.c index 3c2ed5a3e2c..f41eb814790 100644 --- a/examples/rp2040/pico-rmii/main.c +++ b/examples/rp2040/pico-rmii/main.c @@ -10,7 +10,7 @@ #define CLKREFPIN 20 // either 20 or 22 -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { mg_http_reply(c, 200, "", "ok\n"); } diff --git a/examples/smtp-client/main.c b/examples/smtp-client/main.c index fa7e73541c4..4c2f34ac03c 100644 --- a/examples/smtp-client/main.c +++ b/examples/smtp-client/main.c @@ -13,7 +13,7 @@ static bool s_quit; enum { EHLO, STARTTLS, STARTTLS_WAIT, AUTH, FROM, TO, DATA, BODY, QUIT, END }; -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { uint8_t *state = (uint8_t *) c->data; if (ev == MG_EV_OPEN) { // c->is_hexdumping = 1; @@ -69,7 +69,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { MG_INFO(("TLS handshake done! Sending EHLO again")); mg_printf(c, "EHLO myname\r\n"); } - (void) fn_data, (void) ev_data; + (void) ev_data; } int main(void) { diff --git a/examples/sntp-time-sync/main.c b/examples/sntp-time-sync/main.c index 8e89e8d21bd..27801ac096b 100644 --- a/examples/sntp-time-sync/main.c +++ b/examples/sntp-time-sync/main.c @@ -23,7 +23,7 @@ time_t my_time(time_t *tp) { } // SNTP client callback -static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void sfn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_SNTP_TIME) { int64_t t = *(int64_t *) ev_data; MG_INFO(("Got SNTP time: %lld ms from epoch", t)); @@ -31,7 +31,7 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } else if (ev == MG_EV_CLOSE) { s_sntp_conn = NULL; } - (void) fn_data, (void) c; + (void) c; } // Called every 5 seconds. Increase that for production case. diff --git a/examples/socks5-server/main.c b/examples/socks5-server/main.c index bcdc862dbba..f673147a9a4 100644 --- a/examples/socks5-server/main.c +++ b/examples/socks5-server/main.c @@ -79,14 +79,13 @@ static void exchange(struct mg_connection *c) { } } -static void fn2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn2(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_READ) { exchange(c); } else if (ev == MG_EV_CLOSE) { disband(c); } (void) ev_data; - (void) fn_data; } // Request, https://www.ietf.org/rfc/rfc1928.txt paragraph 4 @@ -149,7 +148,7 @@ static void request(struct mg_connection *c) { c->data[0] = STATE_ESTABLISHED; // Mark ourselves as connected } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_READ) { // We use the first label byte as a state if (c->data[0] == STATE_HANDSHAKE) handshake(c); @@ -158,7 +157,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } else if (ev == MG_EV_CLOSE) { disband(c); } - (void) fn_data; (void) ev_data; } diff --git a/examples/stm32/nucleo-f429zi-make-baremetal-builtin-rndis/main.c b/examples/stm32/nucleo-f429zi-make-baremetal-builtin-rndis/main.c index 228c90e393b..dce948711ec 100644 --- a/examples/stm32/nucleo-f429zi-make-baremetal-builtin-rndis/main.c +++ b/examples/stm32/nucleo-f429zi-make-baremetal-builtin-rndis/main.c @@ -63,7 +63,7 @@ static bool usb_up(struct mg_tcpip_if *ifp) { return tud_inited() && tud_ready() && tud_connected(); } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/debug")) { @@ -74,7 +74,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_reply(c, 200, "", "hi\n"); } } - (void) fn_data; } int main(void) { diff --git a/examples/stm32/nucleo-f746zg-make-baremetal-builtin-rndis/main.c b/examples/stm32/nucleo-f746zg-make-baremetal-builtin-rndis/main.c index 228c90e393b..dce948711ec 100644 --- a/examples/stm32/nucleo-f746zg-make-baremetal-builtin-rndis/main.c +++ b/examples/stm32/nucleo-f746zg-make-baremetal-builtin-rndis/main.c @@ -63,7 +63,7 @@ static bool usb_up(struct mg_tcpip_if *ifp) { return tud_inited() && tud_ready() && tud_connected(); } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/debug")) { @@ -74,7 +74,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_reply(c, 200, "", "hi\n"); } } - (void) fn_data; } int main(void) { diff --git a/examples/stm32/nucleo-g031-make-baremetal-builtin/main.c b/examples/stm32/nucleo-g031-make-baremetal-builtin/main.c index 4a0292b938c..7a4be44c50e 100644 --- a/examples/stm32/nucleo-g031-make-baremetal-builtin/main.c +++ b/examples/stm32/nucleo-g031-make-baremetal-builtin/main.c @@ -42,8 +42,8 @@ static void timer_fn(void *arg) { ifp->nerr)); } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) fn_data; +static void fn(struct mg_connection *c, int ev, void *ev_data) { + struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->fn_data; if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/hello")) { // Request to /api/hello diff --git a/examples/tcp/main.c b/examples/tcp/main.c index e5175288b22..58938f64f8f 100644 --- a/examples/tcp/main.c +++ b/examples/tcp/main.c @@ -13,8 +13,8 @@ static struct c_res_s { } c_res; // CLIENT event handler -static void cfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - int *i = &((struct c_res_s *) fn_data)->i; +static void cfn(struct mg_connection *c, int ev, void *ev_data) { + int *i = &((struct c_res_s *) c->fn_data)->i; if (ev == MG_EV_OPEN) { MG_INFO(("CLIENT has been initialized")); } else if (ev == MG_EV_CONNECT) { @@ -33,7 +33,7 @@ static void cfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } else if (ev == MG_EV_CLOSE) { MG_INFO(("CLIENT disconnected")); // signal we are done - ((struct c_res_s *) fn_data)->c = NULL; + ((struct c_res_s *) c->fn_data)->c = NULL; } else if (ev == MG_EV_ERROR) { MG_INFO(("CLIENT error: %s", (char *) ev_data)); } else if (ev == MG_EV_POLL && *i != 0) { @@ -51,7 +51,7 @@ static void cfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } // SERVER event handler -static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void sfn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN && c->is_listening == 1) { MG_INFO(("SERVER is listening")); } else if (ev == MG_EV_ACCEPT) { @@ -72,7 +72,6 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } else if (ev == MG_EV_ERROR) { MG_INFO(("SERVER error: %s", (char *) ev_data)); } - (void) fn_data; } // Timer function - recreate client connection if it is closed diff --git a/examples/ti/ek-tm4c1294xl-make-baremetal-builtin-rndis/main.c b/examples/ti/ek-tm4c1294xl-make-baremetal-builtin-rndis/main.c index c8ce33e38ca..750f4831044 100644 --- a/examples/ti/ek-tm4c1294xl-make-baremetal-builtin-rndis/main.c +++ b/examples/ti/ek-tm4c1294xl-make-baremetal-builtin-rndis/main.c @@ -61,7 +61,7 @@ static bool usb_up(struct mg_tcpip_if *ifp) { return tud_inited() && tud_ready() && tud_connected(); } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/debug")) { @@ -72,7 +72,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_reply(c, 200, "", "hi\n"); } } - (void) fn_data; } int main(void) { diff --git a/examples/timers/main.c b/examples/timers/main.c index 53f2acaa05d..656427ea8a7 100644 --- a/examples/timers/main.c +++ b/examples/timers/main.c @@ -11,7 +11,7 @@ static const char *s_listen_on = "http://localhost:8000"; static const char *s_web_root = "web_root"; // This RESTful server implements the following endpoints: -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/websocket")) { @@ -28,7 +28,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT); mg_iobuf_del(&c->recv, 0, c->recv.len); } - (void) fn_data; } static void timer_fn(void *arg) { diff --git a/examples/udp-ssdp-search/main.c b/examples/udp-ssdp-search/main.c index 4315ddabb2c..70e0c293c0c 100644 --- a/examples/udp-ssdp-search/main.c +++ b/examples/udp-ssdp-search/main.c @@ -5,8 +5,8 @@ static const char *s_ssdp_url = "udp://239.255.255.250:1900"; -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - MG_DEBUG(("%p got event: %d %p %p", c, ev, ev_data, fn_data)); +static void fn(struct mg_connection *c, int ev, void *ev_data) { + MG_DEBUG(("%p got event: %d %p", c, ev, ev_data)); if (ev == MG_EV_OPEN) { // c->is_hexdumping = 1; } else if (ev == MG_EV_RESOLVE) { diff --git a/examples/video-stream/main.c b/examples/video-stream/main.c index 441386c9c95..5bd731d5270 100644 --- a/examples/video-stream/main.c +++ b/examples/video-stream/main.c @@ -6,7 +6,7 @@ // HTTP request handler function. It implements the following endpoints: // /api/video1 - hangs forever, returns MJPEG video stream // all other URI - serves web_root/ directory -static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/video1")) { @@ -22,7 +22,6 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_serve_dir(c, ev_data, &opts); } } - (void) fn_data; } // The image stream is simulated by sending MJPEG frames specified by the diff --git a/examples/wch/ch32v307-make-baremetal-builtin/main.c b/examples/wch/ch32v307-make-baremetal-builtin/main.c index 981634c51b3..571744b3604 100644 --- a/examples/wch/ch32v307-make-baremetal-builtin/main.c +++ b/examples/wch/ch32v307-make-baremetal-builtin/main.c @@ -59,7 +59,7 @@ void SysTick_Init(void) { } // https://mongoose.ws/documentation/#2-minute-integration-guide -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; mg_http_reply(c, 200, "", "ok %p %p\r\n", hm, fn_data); diff --git a/examples/websocket-client/main.c b/examples/websocket-client/main.c index bc21ea7c631..6ef87067ccf 100644 --- a/examples/websocket-client/main.c +++ b/examples/websocket-client/main.c @@ -10,7 +10,7 @@ static const char *s_url = "ws://localhost:8000/websocket"; // Print websocket response and signal that we're done -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { c->is_hexdumping = 1; } else if (ev == MG_EV_ERROR) { @@ -26,7 +26,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE || ev == MG_EV_WS_MSG) { - *(bool *) fn_data = true; // Signal that we're done + *(bool *) c->fn_data = true; // Signal that we're done } } diff --git a/examples/websocket-server/main.c b/examples/websocket-server/main.c index cacd266290b..3c96c488a6a 100644 --- a/examples/websocket-server/main.c +++ b/examples/websocket-server/main.c @@ -12,7 +12,7 @@ static const char *s_web_root = "."; // /websocket - upgrade to Websocket, and implement websocket echo server // /rest - respond with JSON string {"result": 123} // any other URI serves static files from s_web_root -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { // c->is_hexdumping = 1; } else if (ev == MG_EV_HTTP_MSG) { @@ -34,7 +34,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { struct mg_ws_message *wm = (struct mg_ws_message *) ev_data; mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT); } - (void) fn_data; } int main(void) { diff --git a/examples/webui-login/main.c b/examples/webui-login/main.c index 99d50a30d49..0704fb703dc 100644 --- a/examples/webui-login/main.c +++ b/examples/webui-login/main.c @@ -43,7 +43,7 @@ static struct user *getuser(struct mg_http_message *hm) { return NULL; } -void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; struct user *u = getuser(hm); @@ -63,7 +63,6 @@ void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_serve_dir(c, ev_data, &opts); } } - (void) fn_data; } int main(void) { diff --git a/examples/webui-plain/main.c b/examples/webui-plain/main.c index 96825ad9944..c126798ec61 100644 --- a/examples/webui-plain/main.c +++ b/examples/webui-plain/main.c @@ -29,7 +29,7 @@ static void update_config(struct mg_str json, const char *path, char **value) { } } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN && c->is_listening) { s_config.url = strdup(MQTT_SERVER); s_config.pub = strdup(MQTT_PUBLISH_TOPIC); @@ -52,7 +52,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_serve_dir(c, ev_data, &opts); } } - (void) fn_data; } int main(void) { diff --git a/examples/webui-push-rest/main.c b/examples/webui-push-rest/main.c index 5c67a2ce5e0..0ef3b731dd4 100644 --- a/examples/webui-push-rest/main.c +++ b/examples/webui-push-rest/main.c @@ -37,7 +37,7 @@ static size_t printdata(mg_pfn_t out, void *ptr, va_list *ap) { (void) ap; } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = ev_data; if (mg_http_match_uri(hm, "/api/data")) { @@ -58,7 +58,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_serve_dir(c, hm, &opts); } } - (void) fn_data; } static void timer_fn(void *arg) { diff --git a/examples/webui-push-ws/main.c b/examples/webui-push-ws/main.c index cf6226df584..f66fdcdb490 100644 --- a/examples/webui-push-ws/main.c +++ b/examples/webui-push-ws/main.c @@ -10,7 +10,7 @@ static const char *s_listen_on = "http://localhost:8000"; static const char *s_web_root = "web_root"; -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/watch")) { @@ -21,7 +21,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_serve_dir(c, ev_data, &opts); } } - (void) fn_data; } // Push to all watchers diff --git a/examples/webui-rest/main.c b/examples/webui-rest/main.c index 155bcf9c899..4317770348d 100644 --- a/examples/webui-rest/main.c +++ b/examples/webui-rest/main.c @@ -13,7 +13,7 @@ static const char *s_http_addr = "http://localhost:8000"; // HTTP port static const char *s_root_dir = "web_root"; -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/f1")) { @@ -34,7 +34,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_serve_dir(c, hm, &opts); } } - (void) fn_data; } int main(void) { diff --git a/examples/wifi-router-dashboard/net.c b/examples/wifi-router-dashboard/net.c index d88a574db8e..549caba5258 100644 --- a/examples/wifi-router-dashboard/net.c +++ b/examples/wifi-router-dashboard/net.c @@ -109,7 +109,7 @@ static int event_next(int no, struct event *e) { // SNTP connection event handler. When we get a response from an SNTP server, // adjust s_boot_timestamp. We'll get a valid time from that point on -static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void sfn(struct mg_connection *c, int ev, void *ev_data) { uint64_t *expiration_time = (uint64_t *) c->data; if (ev == MG_EV_OPEN) { *expiration_time = mg_millis() + 3000; // Store expiration time in 3s @@ -120,7 +120,6 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } else if (ev == MG_EV_POLL) { if (mg_millis() > *expiration_time) c->is_closing = 1; } - (void) fn_data; } static void timer_sntp_fn(void *param) { // SNTP timer function. Sync up time @@ -271,7 +270,7 @@ static void handle_dhcp_get(struct mg_connection *c) { } // HTTP request handler function -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_ACCEPT) { if (fn_data != NULL) { // TLS struct mg_tls_opts opts = {0}; @@ -316,7 +315,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { hm->method.ptr, (int) hm->uri.len, hm->uri.ptr, (int) 3, &c->send.buf[9])); } - (void) fn_data; } void web_init(struct mg_mgr *mgr) { diff --git a/examples/zephyr/http-client/src/main.c b/examples/zephyr/http-client/src/main.c index 9f786aacae1..a2c77df55b4 100644 --- a/examples/zephyr/http-client/src/main.c +++ b/examples/zephyr/http-client/src/main.c @@ -14,7 +14,7 @@ static int s_connected = 0; static bool done = false; // Print HTTP response and signal that we're done -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { // Connection created. Store connect expiration time in c->data *(int64_t *) c->data = mg_millis() + s_timeout_ms; @@ -49,9 +49,9 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; MG_INFO(("%.*s", (int) hm->message.len, hm->message.ptr)); c->is_draining = 1; // Tell mongoose to close this connection - *(bool *) fn_data = true; // Tell event loop to stop + *(bool *) c->fn_data = true; // Tell event loop to stop } else if (ev == MG_EV_ERROR) { - *(bool *) fn_data = true; // Error, tell event loop to stop + *(bool *) c->fn_data = true; // Error, tell event loop to stop } } @@ -63,7 +63,7 @@ time_t ourtime(time_t *tp) { } // SNTP callback. Modifies s_boot_timestamp, to make ourtime() correct -static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void sfn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_SNTP_TIME) { int64_t t = *(int64_t *) ev_data; MG_INFO(("Got SNTP time: %lld ms from epoch", t)); diff --git a/examples/zephyr/http-server/src/main.c b/examples/zephyr/http-server/src/main.c index 6f250f5a9c1..b306fdb0930 100644 --- a/examples/zephyr/http-server/src/main.c +++ b/examples/zephyr/http-server/src/main.c @@ -11,8 +11,8 @@ static time_t s_boot_timestamp = 0; static struct mg_connection *s_sntp_conn = NULL; // Event handler for the listening HTTP/HTTPS connection. -static void wcb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - if (ev == MG_EV_ACCEPT && fn_data != NULL) { +static void wcb(struct mg_connection *c, int ev, void *ev_data) { + if (ev == MG_EV_ACCEPT && c->fn_data != NULL) { struct mg_tls_opts opts = {.cert = s_ssl_cert, .key = s_ssl_key}; mg_tls_init(c, &opts); } else if (ev == MG_EV_HTTP_MSG) { @@ -49,7 +49,7 @@ time_t ourtime(time_t *tp) { } // SNTP callback. Modifies s_boot_timestamp, to make ourtime() correct -static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void sfn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_SNTP_TIME) { int64_t t = *(int64_t *) ev_data; MG_INFO(("Got SNTP time: %lld ms from epoch", t)); diff --git a/examples/zephyr/mqtt-aws-client/src/main.c b/examples/zephyr/mqtt-aws-client/src/main.c index 606b433fb81..2d88bdeba0d 100644 --- a/examples/zephyr/mqtt-aws-client/src/main.c +++ b/examples/zephyr/mqtt-aws-client/src/main.c @@ -16,7 +16,7 @@ static const char *s_tx_topic = "d/tx"; static int s_qos = 1; static int s_connected = 0; -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { // c->is_hexdumping = 1; } else if (ev == MG_EV_ERROR) { @@ -67,7 +67,7 @@ time_t ourtime(time_t *tp) { } // SNTP callback. Modifies s_boot_timestamp, to make ourtime() correct -static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void sfn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_SNTP_TIME) { int64_t t = *(int64_t *) ev_data; MG_INFO(("Got SNTP time: %lld ms from epoch", t)); diff --git a/examples/zephyr/websocket-server/src/main.c b/examples/zephyr/websocket-server/src/main.c index ee9aa69c4a0..781e6d29b59 100644 --- a/examples/zephyr/websocket-server/src/main.c +++ b/examples/zephyr/websocket-server/src/main.c @@ -13,7 +13,7 @@ static struct mg_connection *s_sntp_conn = NULL; // This RESTful server implements the following endpoints: // /websocket - upgrade to Websocket, and implement websocket echo server // /api/rest - respond with JSON string {"result": 123} -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { c->is_hexdumping = 1; } else if (ev == MG_EV_ACCEPT && fn_data != NULL) { @@ -35,7 +35,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { MG_INFO(("Got wm: %p, data: %p, %d = %*.s", wm, wm->data.ptr, wm->data.len, wm->data.len, wm->data.ptr)); mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT); } - (void) fn_data; } // example system time()-like function @@ -46,7 +45,7 @@ time_t ourtime(time_t *tp) { } // SNTP callback. Modifies s_boot_timestamp, to make ourtime() correct -static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void sfn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_SNTP_TIME) { int64_t t = *(int64_t *) ev_data; MG_INFO(("Got SNTP time: %lld ms from epoch", t)); diff --git a/mongoose.c b/mongoose.c index b892772c5e5..b3d8d1cd239 100644 --- a/mongoose.c +++ b/mongoose.c @@ -852,8 +852,7 @@ bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) { return true; } -static void dns_cb(struct mg_connection *c, int ev, void *ev_data, - void *fn_data) { +static void dns_cb(struct mg_connection *c, int ev, void *ev_data) { struct dns_data *d, *tmp; struct dns_data **head = (struct dns_data **) &c->mgr->active_dns_requests; if (ev == MG_EV_POLL) { @@ -907,7 +906,6 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data, mg_dns_free(head, d); } } - (void) fn_data; } static bool mg_dns_send(struct mg_connection *c, const struct mg_str *name, @@ -1006,8 +1004,8 @@ void mg_call(struct mg_connection *c, int ev, void *ev_data) { // Run user-defined handler first, in order to give it an ability // to intercept processing (e.g. clean input buffer) before the // protocol handler kicks in - if (c->fn != NULL) c->fn(c, ev, ev_data, c->fn_data); - if (c->pfn != NULL) c->pfn(c, ev, ev_data, c->pfn_data); + if (c->fn != NULL) c->fn(c, ev, ev_data); + if (c->pfn != NULL) c->pfn(c, ev, ev_data); } void mg_error(struct mg_connection *c, const char *fmt, ...) { @@ -1018,7 +1016,7 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) { va_end(ap); MG_ERROR(("%lu %ld %s", c->id, c->fd, buf)); c->is_closing = 1; // Set is_closing before sending MG_EV_CALL - mg_call(c, MG_EV_ERROR, buf); // Let user handler to override it + mg_call(c, MG_EV_ERROR, buf); // Let user handler override it } #ifdef MG_ENABLE_LINES @@ -2286,7 +2284,7 @@ void mg_http_reply(struct mg_connection *c, int code, const char *headers, c->is_resp = 0; } -static void http_cb(struct mg_connection *, int, void *, void *); +static void http_cb(struct mg_connection *, int, void *); static void restore_http_cb(struct mg_connection *c) { mg_fs_close((struct mg_fd *) c->pfn_data); c->pfn_data = NULL; @@ -2300,10 +2298,9 @@ char *mg_http_etag(char *buf, size_t len, size_t size, time_t mtime) { return buf; } -static void static_cb(struct mg_connection *c, int ev, void *ev_data, - void *fn_data) { +static void static_cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_WRITE || ev == MG_EV_POLL) { - struct mg_fd *fd = (struct mg_fd *) fn_data; + struct mg_fd *fd = (struct mg_fd *) c->pfn_data; // Read to send IO buffer directly, avoid extra on-stack buffer size_t n, max = MG_IO_SIZE, space; size_t *cl = (size_t *) &c->data[(sizeof(c->data) - sizeof(size_t)) / @@ -2828,7 +2825,7 @@ static int skip_chunk(const char *buf, int len, int *pl, int *dl) { return i + 2 + n + 2; } -static void http_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { +static void http_cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_READ || ev == MG_EV_CLOSE) { struct mg_http_message hm; size_t ofs = 0; // Parsing offset @@ -2890,10 +2887,10 @@ static void http_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { } if (ofs > 0) mg_iobuf_del(&c->recv, 0, ofs); // Delete processed data } - (void) evd, (void) fnd; + (void) ev_data; } -static void mg_hfn(struct mg_connection *c, int ev, void *ev_data, void *fnd) { +static void mg_hfn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/quit")) { @@ -2908,7 +2905,7 @@ static void mg_hfn(struct mg_connection *c, int ev, void *ev_data, void *fnd) { mg_http_reply(c, 200, "", "hi\n"); } } else if (ev == MG_EV_CLOSE) { - if (c->data[0] == 'X') *(bool *) fnd = true; + if (c->data[0] == 'X') *(bool *) c->fn_data = true; } } @@ -4062,7 +4059,8 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version, } if (p > end) return MQTT_MALFORMED; if (version == 5 && p + 2 < end) { - len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &m->props_size); + len_len = + (uint32_t) decode_varint(p, (size_t) (end - p), &m->props_size); if (!len_len) return MQTT_MALFORMED; m->props_start = (size_t) (p + len_len - buf); p += len_len + m->props_size; @@ -4078,8 +4076,7 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version, return MQTT_OK; } -static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data, - void *fn_data) { +static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_READ) { for (;;) { uint8_t version = c->is_mqtt5 ? 5 : 4; @@ -4147,7 +4144,6 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data, } } (void) ev_data; - (void) fn_data; } void mg_mqtt_ping(struct mg_connection *nc) { @@ -4192,444 +4188,161 @@ struct mg_connection *mg_mqtt_listen(struct mg_mgr *mgr, const char *url, } #ifdef MG_ENABLE_LINES -#line 1 "src/net.c" +#line 1 "src/net_builtin.c" #endif +#if defined(MG_ENABLE_TCPIP) && MG_ENABLE_TCPIP +#define MG_EPHEMERAL_PORT_BASE 32768 +#define PDIFF(a, b) ((size_t) (((char *) (b)) - ((char *) (a)))) +#ifndef MIP_TCP_KEEPALIVE_MS +#define MIP_TCP_KEEPALIVE_MS 45000 // TCP keep-alive period, ms +#endif +#define MIP_TCP_ACK_MS 150 // Timeout for ACKing +#define MIP_TCP_ARP_MS 100 // Timeout for ARP response +#define MIP_TCP_SYN_MS 15000 // Timeout for connection establishment +#define MIP_TCP_FIN_MS 1000 // Timeout for closing connection +struct connstate { + uint32_t seq, ack; // TCP seq/ack counters + uint64_t timer; // TCP keep-alive / ACK timer + uint8_t mac[6]; // Peer MAC address + uint8_t ttype; // Timer type. 0: ack, 1: keep-alive +#define MIP_TTYPE_KEEPALIVE 0 // Connection is idle for long, send keepalive +#define MIP_TTYPE_ACK 1 // Peer sent us data, we have to ack it soon +#define MIP_TTYPE_ARP 2 // ARP resolve sent, waiting for response +#define MIP_TTYPE_SYN 3 // SYN sent, waiting for response +#define MIP_TTYPE_FIN 4 // FIN sent, waiting until terminating the connection + uint8_t tmiss; // Number of keep-alive misses + struct mg_iobuf raw; // For TLS only. Incoming raw data +}; +#pragma pack(push, 1) +struct lcp { + uint8_t addr, ctrl, proto[2], code, id, len[2]; +}; +struct eth { + uint8_t dst[6]; // Destination MAC address + uint8_t src[6]; // Source MAC address + uint16_t type; // Ethernet type +}; -size_t mg_vprintf(struct mg_connection *c, const char *fmt, va_list *ap) { - size_t old = c->send.len; - mg_vxprintf(mg_pfn_iobuf, &c->send, fmt, ap); - return c->send.len - old; -} +struct ip { + uint8_t ver; // Version + uint8_t tos; // Unused + uint16_t len; // Length + uint16_t id; // Unused + uint16_t frag; // Fragmentation +#define IP_FRAG_OFFSET_MSK 0xFF1F +#define IP_MORE_FRAGS_MSK 0x20 + uint8_t ttl; // Time to live + uint8_t proto; // Upper level protocol + uint16_t csum; // Checksum + uint32_t src; // Source IP + uint32_t dst; // Destination IP +}; -size_t mg_printf(struct mg_connection *c, const char *fmt, ...) { - size_t len = 0; - va_list ap; - va_start(ap, fmt); - len = mg_vprintf(c, fmt, &ap); - va_end(ap); - return len; -} +struct ip6 { + uint8_t ver; // Version + uint8_t opts[3]; // Options + uint16_t len; // Length + uint8_t proto; // Upper level protocol + uint8_t ttl; // Time to live + uint8_t src[16]; // Source IP + uint8_t dst[16]; // Destination IP +}; -static bool mg_atonl(struct mg_str str, struct mg_addr *addr) { - uint32_t localhost = mg_htonl(0x7f000001); - if (mg_vcasecmp(&str, "localhost") != 0) return false; - memcpy(addr->ip, &localhost, sizeof(uint32_t)); - addr->is_ip6 = false; - return true; -} +struct icmp { + uint8_t type; + uint8_t code; + uint16_t csum; +}; -static bool mg_atone(struct mg_str str, struct mg_addr *addr) { - if (str.len > 0) return false; - memset(addr->ip, 0, sizeof(addr->ip)); - addr->is_ip6 = false; - return true; -} +struct arp { + uint16_t fmt; // Format of hardware address + uint16_t pro; // Format of protocol address + uint8_t hlen; // Length of hardware address + uint8_t plen; // Length of protocol address + uint16_t op; // Operation + uint8_t sha[6]; // Sender hardware address + uint32_t spa; // Sender protocol address + uint8_t tha[6]; // Target hardware address + uint32_t tpa; // Target protocol address +}; -static bool mg_aton4(struct mg_str str, struct mg_addr *addr) { - uint8_t data[4] = {0, 0, 0, 0}; - size_t i, num_dots = 0; - for (i = 0; i < str.len; i++) { - if (str.ptr[i] >= '0' && str.ptr[i] <= '9') { - int octet = data[num_dots] * 10 + (str.ptr[i] - '0'); - if (octet > 255) return false; - data[num_dots] = (uint8_t) octet; - } else if (str.ptr[i] == '.') { - if (num_dots >= 3 || i == 0 || str.ptr[i - 1] == '.') return false; - num_dots++; - } else { - return false; - } - } - if (num_dots != 3 || str.ptr[i - 1] == '.') return false; - memcpy(&addr->ip, data, sizeof(data)); - addr->is_ip6 = false; - return true; -} +struct tcp { + uint16_t sport; // Source port + uint16_t dport; // Destination port + uint32_t seq; // Sequence number + uint32_t ack; // Acknowledgement number + uint8_t off; // Data offset + uint8_t flags; // TCP flags +#define TH_FIN 0x01 +#define TH_SYN 0x02 +#define TH_RST 0x04 +#define TH_PUSH 0x08 +#define TH_ACK 0x10 +#define TH_URG 0x20 +#define TH_ECE 0x40 +#define TH_CWR 0x80 + uint16_t win; // Window + uint16_t csum; // Checksum + uint16_t urp; // Urgent pointer +}; -static bool mg_v4mapped(struct mg_str str, struct mg_addr *addr) { - int i; - uint32_t ipv4; - if (str.len < 14) return false; - if (str.ptr[0] != ':' || str.ptr[1] != ':' || str.ptr[6] != ':') return false; - for (i = 2; i < 6; i++) { - if (str.ptr[i] != 'f' && str.ptr[i] != 'F') return false; - } - // struct mg_str s = mg_str_n(&str.ptr[7], str.len - 7); - if (!mg_aton4(mg_str_n(&str.ptr[7], str.len - 7), addr)) return false; - memcpy(&ipv4, addr->ip, sizeof(ipv4)); - memset(addr->ip, 0, sizeof(addr->ip)); - addr->ip[10] = addr->ip[11] = 255; - memcpy(&addr->ip[12], &ipv4, 4); - addr->is_ip6 = true; - return true; -} +struct udp { + uint16_t sport; // Source port + uint16_t dport; // Destination port + uint16_t len; // UDP length + uint16_t csum; // UDP checksum +}; -static bool mg_aton6(struct mg_str str, struct mg_addr *addr) { - size_t i, j = 0, n = 0, dc = 42; - addr->scope_id = 0; - if (str.len > 2 && str.ptr[0] == '[') str.ptr++, str.len -= 2; - if (mg_v4mapped(str, addr)) return true; - for (i = 0; i < str.len; i++) { - if ((str.ptr[i] >= '0' && str.ptr[i] <= '9') || - (str.ptr[i] >= 'a' && str.ptr[i] <= 'f') || - (str.ptr[i] >= 'A' && str.ptr[i] <= 'F')) { - unsigned long val; - if (i > j + 3) return false; - // MG_DEBUG(("%lu %lu [%.*s]", i, j, (int) (i - j + 1), &str.ptr[j])); - val = mg_unhexn(&str.ptr[j], i - j + 1); - addr->ip[n] = (uint8_t) ((val >> 8) & 255); - addr->ip[n + 1] = (uint8_t) (val & 255); - } else if (str.ptr[i] == ':') { - j = i + 1; - if (i > 0 && str.ptr[i - 1] == ':') { - dc = n; // Double colon - if (i > 1 && str.ptr[i - 2] == ':') return false; - } else if (i > 0) { - n += 2; - } - if (n > 14) return false; - addr->ip[n] = addr->ip[n + 1] = 0; // For trailing :: - } else if (str.ptr[i] == '%') { // Scope ID - for (i = i + 1; i < str.len; i++) { - if (str.ptr[i] < '0' || str.ptr[i] > '9') return false; - addr->scope_id = (uint8_t) (addr->scope_id * 10); - addr->scope_id = (uint8_t) (addr->scope_id + (str.ptr[i] - '0')); - } - } else { - return false; - } - } - if (n < 14 && dc == 42) return false; - if (n < 14) { - memmove(&addr->ip[dc + (14 - n)], &addr->ip[dc], n - dc + 2); - memset(&addr->ip[dc], 0, 14 - n); - } +struct dhcp { + uint8_t op, htype, hlen, hops; + uint32_t xid; + uint16_t secs, flags; + uint32_t ciaddr, yiaddr, siaddr, giaddr; + uint8_t hwaddr[208]; + uint32_t magic; + uint8_t options[32]; +}; - addr->is_ip6 = true; - return true; +#pragma pack(pop) + +struct pkt { + struct mg_str raw; // Raw packet data + struct mg_str pay; // Payload data + struct eth *eth; + struct llc *llc; + struct arp *arp; + struct ip *ip; + struct ip6 *ip6; + struct icmp *icmp; + struct tcp *tcp; + struct udp *udp; + struct dhcp *dhcp; +}; + +static void send_syn(struct mg_connection *c); + +static void mkpay(struct pkt *pkt, void *p) { + pkt->pay = + mg_str_n((char *) p, (size_t) (&pkt->raw.ptr[pkt->raw.len] - (char *) p)); } -bool mg_aton(struct mg_str str, struct mg_addr *addr) { - // MG_INFO(("[%.*s]", (int) str.len, str.ptr)); - return mg_atone(str, addr) || mg_atonl(str, addr) || mg_aton4(str, addr) || - mg_aton6(str, addr); +static uint32_t csumup(uint32_t sum, const void *buf, size_t len) { + const uint8_t *p = (const uint8_t *) buf; + for (size_t i = 0; i < len; i++) sum += i & 1 ? p[i] : (uint32_t) (p[i] << 8); + return sum; } -struct mg_connection *mg_alloc_conn(struct mg_mgr *mgr) { - struct mg_connection *c = - (struct mg_connection *) calloc(1, sizeof(*c) + mgr->extraconnsize); - if (c != NULL) { - c->mgr = mgr; - c->send.align = c->recv.align = c->rtls.align = MG_IO_SIZE; - c->id = ++mgr->nextid; - MG_PROF_INIT(c); - } - return c; -} - -void mg_close_conn(struct mg_connection *c) { - mg_resolve_cancel(c); // Close any pending DNS query - LIST_DELETE(struct mg_connection, &c->mgr->conns, c); - if (c == c->mgr->dns4.c) c->mgr->dns4.c = NULL; - if (c == c->mgr->dns6.c) c->mgr->dns6.c = NULL; - // Order of operations is important. `MG_EV_CLOSE` event must be fired - // before we deallocate received data, see #1331 - mg_call(c, MG_EV_CLOSE, NULL); - MG_DEBUG(("%lu %ld closed", c->id, c->fd)); - MG_PROF_DUMP(c); - MG_PROF_FREE(c); - - mg_tls_free(c); - mg_iobuf_free(&c->recv); - mg_iobuf_free(&c->send); - mg_iobuf_free(&c->rtls); - mg_bzero((unsigned char *) c, sizeof(*c)); - free(c); -} - -struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, - mg_event_handler_t fn, void *fn_data) { - struct mg_connection *c = NULL; - if (url == NULL || url[0] == '\0') { - MG_ERROR(("null url")); - } else if ((c = mg_alloc_conn(mgr)) == NULL) { - MG_ERROR(("OOM")); - } else { - LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); - c->is_udp = (strncmp(url, "udp:", 4) == 0); - c->fd = (void *) (size_t) MG_INVALID_SOCKET; - c->fn = fn; - c->is_client = true; - c->fn_data = fn_data; - MG_DEBUG(("%lu %ld %s", c->id, c->fd, url)); - mg_call(c, MG_EV_OPEN, (void *) url); - mg_resolve(c, url); - } - return c; -} - -struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url, - mg_event_handler_t fn, void *fn_data) { - struct mg_connection *c = NULL; - if ((c = mg_alloc_conn(mgr)) == NULL) { - MG_ERROR(("OOM %s", url)); - } else if (!mg_open_listener(c, url)) { - MG_ERROR(("Failed: %s, errno %d", url, errno)); - MG_PROF_FREE(c); - free(c); - c = NULL; - } else { - c->is_listening = 1; - c->is_udp = strncmp(url, "udp:", 4) == 0; - LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); - c->fn = fn; - c->fn_data = fn_data; - mg_call(c, MG_EV_OPEN, NULL); - if (mg_url_is_ssl(url)) c->is_tls = 1; // Accepted connection must - MG_DEBUG(("%lu %ld %s", c->id, c->fd, url)); - } - return c; -} - -struct mg_connection *mg_wrapfd(struct mg_mgr *mgr, int fd, - mg_event_handler_t fn, void *fn_data) { - struct mg_connection *c = mg_alloc_conn(mgr); - if (c != NULL) { - c->fd = (void *) (size_t) fd; - c->fn = fn; - c->fn_data = fn_data; - MG_EPOLL_ADD(c); - mg_call(c, MG_EV_OPEN, NULL); - LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); - } - return c; -} - -struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds, - unsigned flags, void (*fn)(void *), void *arg) { - struct mg_timer *t = (struct mg_timer *) calloc(1, sizeof(*t)); - if (t != NULL) { - mg_timer_init(&mgr->timers, t, milliseconds, flags, fn, arg); - t->id = mgr->timerid++; - } - return t; -} - -long mg_io_recv(struct mg_connection *c, void *buf, size_t len) { - if (c->rtls.len == 0) return MG_IO_WAIT; - if (len > c->rtls.len) len = c->rtls.len; - memcpy(buf, c->rtls.buf, len); - mg_iobuf_del(&c->rtls, 0, len); - return (long) len; -} - -void mg_mgr_free(struct mg_mgr *mgr) { - struct mg_connection *c; - struct mg_timer *tmp, *t = mgr->timers; - while (t != NULL) tmp = t->next, free(t), t = tmp; - mgr->timers = NULL; // Important. Next call to poll won't touch timers - for (c = mgr->conns; c != NULL; c = c->next) c->is_closing = 1; - mg_mgr_poll(mgr, 0); -#if MG_ENABLE_FREERTOS_TCP - FreeRTOS_DeleteSocketSet(mgr->ss); -#endif - MG_DEBUG(("All connections closed")); -#if MG_ENABLE_EPOLL - if (mgr->epoll_fd >= 0) close(mgr->epoll_fd), mgr->epoll_fd = -1; -#endif - mg_tls_ctx_free(mgr); -} - -void mg_mgr_init(struct mg_mgr *mgr) { - memset(mgr, 0, sizeof(*mgr)); -#if MG_ENABLE_EPOLL - if ((mgr->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) - MG_ERROR(("epoll_create1 errno %d", errno)); -#else - mgr->epoll_fd = -1; -#endif -#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK - // clang-format off - { WSADATA data; WSAStartup(MAKEWORD(2, 2), &data); } - // clang-format on -#elif MG_ENABLE_FREERTOS_TCP - mgr->ss = FreeRTOS_CreateSocketSet(); -#elif defined(__unix) || defined(__unix__) || defined(__APPLE__) - // Ignore SIGPIPE signal, so if client cancels the request, it - // won't kill the whole process. - signal(SIGPIPE, SIG_IGN); -#endif - mgr->pipe = MG_INVALID_SOCKET; - mgr->dnstimeout = 3000; - mgr->dns4.url = "udp://8.8.8.8:53"; - mgr->dns6.url = "udp://[2001:4860:4860::8888]:53"; - mg_tls_ctx_init(mgr); -} - -#ifdef MG_ENABLE_LINES -#line 1 "src/net_builtin.c" -#endif - - -#if defined(MG_ENABLE_TCPIP) && MG_ENABLE_TCPIP -#define MG_EPHEMERAL_PORT_BASE 32768 -#define PDIFF(a, b) ((size_t) (((char *) (b)) - ((char *) (a)))) - -#ifndef MIP_TCP_KEEPALIVE_MS -#define MIP_TCP_KEEPALIVE_MS 45000 // TCP keep-alive period, ms -#endif - -#define MIP_TCP_ACK_MS 150 // Timeout for ACKing -#define MIP_TCP_ARP_MS 100 // Timeout for ARP response -#define MIP_TCP_SYN_MS 15000 // Timeout for connection establishment -#define MIP_TCP_FIN_MS 1000 // Timeout for closing connection - -struct connstate { - uint32_t seq, ack; // TCP seq/ack counters - uint64_t timer; // TCP keep-alive / ACK timer - uint8_t mac[6]; // Peer MAC address - uint8_t ttype; // Timer type. 0: ack, 1: keep-alive -#define MIP_TTYPE_KEEPALIVE 0 // Connection is idle for long, send keepalive -#define MIP_TTYPE_ACK 1 // Peer sent us data, we have to ack it soon -#define MIP_TTYPE_ARP 2 // ARP resolve sent, waiting for response -#define MIP_TTYPE_SYN 3 // SYN sent, waiting for response -#define MIP_TTYPE_FIN 4 // FIN sent, waiting until terminating the connection - uint8_t tmiss; // Number of keep-alive misses - struct mg_iobuf raw; // For TLS only. Incoming raw data -}; - -#pragma pack(push, 1) - -struct lcp { - uint8_t addr, ctrl, proto[2], code, id, len[2]; -}; - -struct eth { - uint8_t dst[6]; // Destination MAC address - uint8_t src[6]; // Source MAC address - uint16_t type; // Ethernet type -}; - -struct ip { - uint8_t ver; // Version - uint8_t tos; // Unused - uint16_t len; // Length - uint16_t id; // Unused - uint16_t frag; // Fragmentation -#define IP_FRAG_OFFSET_MSK 0xFF1F -#define IP_MORE_FRAGS_MSK 0x20 - uint8_t ttl; // Time to live - uint8_t proto; // Upper level protocol - uint16_t csum; // Checksum - uint32_t src; // Source IP - uint32_t dst; // Destination IP -}; - -struct ip6 { - uint8_t ver; // Version - uint8_t opts[3]; // Options - uint16_t len; // Length - uint8_t proto; // Upper level protocol - uint8_t ttl; // Time to live - uint8_t src[16]; // Source IP - uint8_t dst[16]; // Destination IP -}; - -struct icmp { - uint8_t type; - uint8_t code; - uint16_t csum; -}; - -struct arp { - uint16_t fmt; // Format of hardware address - uint16_t pro; // Format of protocol address - uint8_t hlen; // Length of hardware address - uint8_t plen; // Length of protocol address - uint16_t op; // Operation - uint8_t sha[6]; // Sender hardware address - uint32_t spa; // Sender protocol address - uint8_t tha[6]; // Target hardware address - uint32_t tpa; // Target protocol address -}; - -struct tcp { - uint16_t sport; // Source port - uint16_t dport; // Destination port - uint32_t seq; // Sequence number - uint32_t ack; // Acknowledgement number - uint8_t off; // Data offset - uint8_t flags; // TCP flags -#define TH_FIN 0x01 -#define TH_SYN 0x02 -#define TH_RST 0x04 -#define TH_PUSH 0x08 -#define TH_ACK 0x10 -#define TH_URG 0x20 -#define TH_ECE 0x40 -#define TH_CWR 0x80 - uint16_t win; // Window - uint16_t csum; // Checksum - uint16_t urp; // Urgent pointer -}; - -struct udp { - uint16_t sport; // Source port - uint16_t dport; // Destination port - uint16_t len; // UDP length - uint16_t csum; // UDP checksum -}; - -struct dhcp { - uint8_t op, htype, hlen, hops; - uint32_t xid; - uint16_t secs, flags; - uint32_t ciaddr, yiaddr, siaddr, giaddr; - uint8_t hwaddr[208]; - uint32_t magic; - uint8_t options[32]; -}; - -#pragma pack(pop) - -struct pkt { - struct mg_str raw; // Raw packet data - struct mg_str pay; // Payload data - struct eth *eth; - struct llc *llc; - struct arp *arp; - struct ip *ip; - struct ip6 *ip6; - struct icmp *icmp; - struct tcp *tcp; - struct udp *udp; - struct dhcp *dhcp; -}; - -static void send_syn(struct mg_connection *c); - -static void mkpay(struct pkt *pkt, void *p) { - pkt->pay = - mg_str_n((char *) p, (size_t) (&pkt->raw.ptr[pkt->raw.len] - (char *) p)); -} - -static uint32_t csumup(uint32_t sum, const void *buf, size_t len) { - const uint8_t *p = (const uint8_t *) buf; - for (size_t i = 0; i < len; i++) sum += i & 1 ? p[i] : (uint32_t) (p[i] << 8); - return sum; -} - -static uint16_t csumfin(uint32_t sum) { - while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16); - return mg_htons(~sum & 0xffff); +static uint16_t csumfin(uint32_t sum) { + while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16); + return mg_htons(~sum & 0xffff); } static uint16_t ipcsum(const void *buf, size_t len) { @@ -5446,142 +5159,425 @@ void mg_tcpip_init(struct mg_mgr *mgr, struct mg_tcpip_if *ifp) { // MG_EPHEMERAL_PORT_BASE to 65535 if (ifp->tx.ptr == NULL || ifp->recv_queue.buf == NULL) MG_ERROR(("OOM")); } -} +} + +void mg_tcpip_free(struct mg_tcpip_if *ifp) { + free(ifp->recv_queue.buf); + free((char *) ifp->tx.ptr); +} + +static void send_syn(struct mg_connection *c) { + struct connstate *s = (struct connstate *) (c + 1); + uint32_t isn = mg_htonl((uint32_t) mg_ntohs(c->loc.port)); + struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + uint32_t rem_ip; + memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); + tx_tcp(ifp, s->mac, rem_ip, TH_SYN, c->loc.port, c->rem.port, isn, 0, NULL, + 0); +} + +void mg_connect_resolved(struct mg_connection *c) { + struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + uint32_t rem_ip; + memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); + c->is_resolving = 0; + if (ifp->eport < MG_EPHEMERAL_PORT_BASE) ifp->eport = MG_EPHEMERAL_PORT_BASE; + memcpy(c->loc.ip, &ifp->ip, sizeof(uint32_t)); + c->loc.port = mg_htons(ifp->eport++); + MG_DEBUG(("%lu %M -> %M", c->id, mg_print_ip_port, &c->loc, mg_print_ip_port, + &c->rem)); + mg_call(c, MG_EV_RESOLVE, NULL); + if (c->is_udp && (rem_ip == 0xffffffff || rem_ip == (ifp->ip | ~ifp->mask))) { + struct connstate *s = (struct connstate *) (c + 1); + memset(s->mac, 0xFF, sizeof(s->mac)); // global or local broadcast + } else if (((rem_ip & ifp->mask) == (ifp->ip & ifp->mask))) { + // If we're in the same LAN, fire an ARP lookup. + MG_DEBUG(("%lu ARP lookup...", c->id)); + arp_ask(ifp, rem_ip); + settmout(c, MIP_TTYPE_ARP); + c->is_arplooking = 1; + c->is_connecting = 1; + } else if ((*((uint8_t *) &rem_ip) & 0xE0) == 0xE0) { + struct connstate *s = (struct connstate *) (c + 1); // 224 to 239, E0 to EF + uint8_t mcastp[3] = {0x01, 0x00, 0x5E}; // multicast group + memcpy(s->mac, mcastp, 3); + memcpy(s->mac + 3, ((uint8_t *) &rem_ip) + 1, 3); // 23 LSb + s->mac[3] &= 0x7F; + } else { + struct connstate *s = (struct connstate *) (c + 1); + memcpy(s->mac, ifp->gwmac, sizeof(ifp->gwmac)); + if (c->is_udp) { + mg_call(c, MG_EV_CONNECT, NULL); + } else { + send_syn(c); + settmout(c, MIP_TTYPE_SYN); + c->is_connecting = 1; + } + } +} + +bool mg_open_listener(struct mg_connection *c, const char *url) { + c->loc.port = mg_htons(mg_url_port(url)); + return true; +} + +static void write_conn(struct mg_connection *c) { + long len = c->is_tls ? mg_tls_send(c, c->send.buf, c->send.len) + : mg_io_send(c, c->send.buf, c->send.len); + if (len == MG_IO_ERR) { + mg_error(c, "tx err"); + } else if (len > 0) { + mg_iobuf_del(&c->send, 0, (size_t) len); + mg_call(c, MG_EV_WRITE, &len); + } +} + +static void init_closure(struct mg_connection *c) { + struct connstate *s = (struct connstate *) (c + 1); + if (c->is_udp == false && c->is_listening == false && + c->is_connecting == false) { // For TCP conns, + struct mg_tcpip_if *ifp = + (struct mg_tcpip_if *) c->mgr->priv; // send TCP FIN + uint32_t rem_ip; + memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); + tx_tcp(ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port, c->rem.port, + mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0); + settmout(c, MIP_TTYPE_FIN); + } +} + +static void close_conn(struct mg_connection *c) { + struct connstate *s = (struct connstate *) (c + 1); + mg_iobuf_free(&s->raw); // For TLS connections, release raw data + mg_close_conn(c); +} + +static bool can_write(struct mg_connection *c) { + return c->is_connecting == 0 && c->is_resolving == 0 && c->send.len > 0 && + c->is_tls_hs == 0 && c->is_arplooking == 0; +} + +void mg_mgr_poll(struct mg_mgr *mgr, int ms) { + struct mg_connection *c, *tmp; + uint64_t now = mg_millis(); + mg_tcpip_poll((struct mg_tcpip_if *) mgr->priv, now); + mg_timer_poll(&mgr->timers, now); + for (c = mgr->conns; c != NULL; c = tmp) { + tmp = c->next; + struct connstate *s = (struct connstate *) (c + 1); + mg_call(c, MG_EV_POLL, &now); + MG_VERBOSE(("%lu .. %c%c%c%c%c", c->id, c->is_tls ? 'T' : 't', + c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h', + c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c')); + if (can_write(c)) write_conn(c); + if (c->is_draining && c->send.len == 0 && s->ttype != MIP_TTYPE_FIN) + init_closure(c); + if (c->is_closing) close_conn(c); + } + (void) ms; +} + +bool mg_send(struct mg_connection *c, const void *buf, size_t len) { + struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; + bool res = false; + uint32_t rem_ip; + memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); + if (ifp->ip == 0 || ifp->state != MG_TCPIP_STATE_READY) { + mg_error(c, "net down"); + } else if (c->is_udp) { + struct connstate *s = (struct connstate *) (c + 1); + len = trim_len(c, len); // Trimming length if necessary + tx_udp(ifp, s->mac, ifp->ip, c->loc.port, rem_ip, c->rem.port, buf, len); + res = true; + } else { + res = mg_iobuf_add(&c->send, c->send.len, buf, len); + } + return res; +} +#endif // MG_ENABLE_TCPIP + +#ifdef MG_ENABLE_LINES +#line 1 "src/net.c" +#endif + + + + + + + + + +size_t mg_vprintf(struct mg_connection *c, const char *fmt, va_list *ap) { + size_t old = c->send.len; + mg_vxprintf(mg_pfn_iobuf, &c->send, fmt, ap); + return c->send.len - old; +} + +size_t mg_printf(struct mg_connection *c, const char *fmt, ...) { + size_t len = 0; + va_list ap; + va_start(ap, fmt); + len = mg_vprintf(c, fmt, &ap); + va_end(ap); + return len; +} + +static bool mg_atonl(struct mg_str str, struct mg_addr *addr) { + uint32_t localhost = mg_htonl(0x7f000001); + if (mg_vcasecmp(&str, "localhost") != 0) return false; + memcpy(addr->ip, &localhost, sizeof(uint32_t)); + addr->is_ip6 = false; + return true; +} + +static bool mg_atone(struct mg_str str, struct mg_addr *addr) { + if (str.len > 0) return false; + memset(addr->ip, 0, sizeof(addr->ip)); + addr->is_ip6 = false; + return true; +} + +static bool mg_aton4(struct mg_str str, struct mg_addr *addr) { + uint8_t data[4] = {0, 0, 0, 0}; + size_t i, num_dots = 0; + for (i = 0; i < str.len; i++) { + if (str.ptr[i] >= '0' && str.ptr[i] <= '9') { + int octet = data[num_dots] * 10 + (str.ptr[i] - '0'); + if (octet > 255) return false; + data[num_dots] = (uint8_t) octet; + } else if (str.ptr[i] == '.') { + if (num_dots >= 3 || i == 0 || str.ptr[i - 1] == '.') return false; + num_dots++; + } else { + return false; + } + } + if (num_dots != 3 || str.ptr[i - 1] == '.') return false; + memcpy(&addr->ip, data, sizeof(data)); + addr->is_ip6 = false; + return true; +} + +static bool mg_v4mapped(struct mg_str str, struct mg_addr *addr) { + int i; + uint32_t ipv4; + if (str.len < 14) return false; + if (str.ptr[0] != ':' || str.ptr[1] != ':' || str.ptr[6] != ':') return false; + for (i = 2; i < 6; i++) { + if (str.ptr[i] != 'f' && str.ptr[i] != 'F') return false; + } + // struct mg_str s = mg_str_n(&str.ptr[7], str.len - 7); + if (!mg_aton4(mg_str_n(&str.ptr[7], str.len - 7), addr)) return false; + memcpy(&ipv4, addr->ip, sizeof(ipv4)); + memset(addr->ip, 0, sizeof(addr->ip)); + addr->ip[10] = addr->ip[11] = 255; + memcpy(&addr->ip[12], &ipv4, 4); + addr->is_ip6 = true; + return true; +} + +static bool mg_aton6(struct mg_str str, struct mg_addr *addr) { + size_t i, j = 0, n = 0, dc = 42; + addr->scope_id = 0; + if (str.len > 2 && str.ptr[0] == '[') str.ptr++, str.len -= 2; + if (mg_v4mapped(str, addr)) return true; + for (i = 0; i < str.len; i++) { + if ((str.ptr[i] >= '0' && str.ptr[i] <= '9') || + (str.ptr[i] >= 'a' && str.ptr[i] <= 'f') || + (str.ptr[i] >= 'A' && str.ptr[i] <= 'F')) { + unsigned long val; + if (i > j + 3) return false; + // MG_DEBUG(("%lu %lu [%.*s]", i, j, (int) (i - j + 1), &str.ptr[j])); + val = mg_unhexn(&str.ptr[j], i - j + 1); + addr->ip[n] = (uint8_t) ((val >> 8) & 255); + addr->ip[n + 1] = (uint8_t) (val & 255); + } else if (str.ptr[i] == ':') { + j = i + 1; + if (i > 0 && str.ptr[i - 1] == ':') { + dc = n; // Double colon + if (i > 1 && str.ptr[i - 2] == ':') return false; + } else if (i > 0) { + n += 2; + } + if (n > 14) return false; + addr->ip[n] = addr->ip[n + 1] = 0; // For trailing :: + } else if (str.ptr[i] == '%') { // Scope ID + for (i = i + 1; i < str.len; i++) { + if (str.ptr[i] < '0' || str.ptr[i] > '9') return false; + addr->scope_id = (uint8_t) (addr->scope_id * 10); + addr->scope_id = (uint8_t) (addr->scope_id + (str.ptr[i] - '0')); + } + } else { + return false; + } + } + if (n < 14 && dc == 42) return false; + if (n < 14) { + memmove(&addr->ip[dc + (14 - n)], &addr->ip[dc], n - dc + 2); + memset(&addr->ip[dc], 0, 14 - n); + } -void mg_tcpip_free(struct mg_tcpip_if *ifp) { - free(ifp->recv_queue.buf); - free((char *) ifp->tx.ptr); + addr->is_ip6 = true; + return true; } -static void send_syn(struct mg_connection *c) { - struct connstate *s = (struct connstate *) (c + 1); - uint32_t isn = mg_htonl((uint32_t) mg_ntohs(c->loc.port)); - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; - uint32_t rem_ip; - memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); - tx_tcp(ifp, s->mac, rem_ip, TH_SYN, c->loc.port, c->rem.port, isn, 0, NULL, - 0); +bool mg_aton(struct mg_str str, struct mg_addr *addr) { + // MG_INFO(("[%.*s]", (int) str.len, str.ptr)); + return mg_atone(str, addr) || mg_atonl(str, addr) || mg_aton4(str, addr) || + mg_aton6(str, addr); } -void mg_connect_resolved(struct mg_connection *c) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; - uint32_t rem_ip; - memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); - c->is_resolving = 0; - if (ifp->eport < MG_EPHEMERAL_PORT_BASE) ifp->eport = MG_EPHEMERAL_PORT_BASE; - memcpy(c->loc.ip, &ifp->ip, sizeof(uint32_t)); - c->loc.port = mg_htons(ifp->eport++); - MG_DEBUG(("%lu %M -> %M", c->id, mg_print_ip_port, &c->loc, mg_print_ip_port, - &c->rem)); - mg_call(c, MG_EV_RESOLVE, NULL); - if (c->is_udp && (rem_ip == 0xffffffff || rem_ip == (ifp->ip | ~ifp->mask))) { - struct connstate *s = (struct connstate *) (c + 1); - memset(s->mac, 0xFF, sizeof(s->mac)); // global or local broadcast - } else if (((rem_ip & ifp->mask) == (ifp->ip & ifp->mask))) { - // If we're in the same LAN, fire an ARP lookup. - MG_DEBUG(("%lu ARP lookup...", c->id)); - arp_ask(ifp, rem_ip); - settmout(c, MIP_TTYPE_ARP); - c->is_arplooking = 1; - c->is_connecting = 1; - } else if ((*((uint8_t *) &rem_ip) & 0xE0) == 0xE0) { - struct connstate *s = (struct connstate *) (c + 1); // 224 to 239, E0 to EF - uint8_t mcastp[3] = {0x01, 0x00, 0x5E}; // multicast group - memcpy(s->mac, mcastp, 3); - memcpy(s->mac + 3, ((uint8_t *) &rem_ip) + 1, 3); // 23 LSb - s->mac[3] &= 0x7F; - } else { - struct connstate *s = (struct connstate *) (c + 1); - memcpy(s->mac, ifp->gwmac, sizeof(ifp->gwmac)); - if (c->is_udp) { - mg_call(c, MG_EV_CONNECT, NULL); - } else { - send_syn(c); - settmout(c, MIP_TTYPE_SYN); - c->is_connecting = 1; - } +struct mg_connection *mg_alloc_conn(struct mg_mgr *mgr) { + struct mg_connection *c = + (struct mg_connection *) calloc(1, sizeof(*c) + mgr->extraconnsize); + if (c != NULL) { + c->mgr = mgr; + c->send.align = c->recv.align = c->rtls.align = MG_IO_SIZE; + c->id = ++mgr->nextid; + MG_PROF_INIT(c); } + return c; } -bool mg_open_listener(struct mg_connection *c, const char *url) { - c->loc.port = mg_htons(mg_url_port(url)); - return true; +void mg_close_conn(struct mg_connection *c) { + mg_resolve_cancel(c); // Close any pending DNS query + LIST_DELETE(struct mg_connection, &c->mgr->conns, c); + if (c == c->mgr->dns4.c) c->mgr->dns4.c = NULL; + if (c == c->mgr->dns6.c) c->mgr->dns6.c = NULL; + // Order of operations is important. `MG_EV_CLOSE` event must be fired + // before we deallocate received data, see #1331 + mg_call(c, MG_EV_CLOSE, NULL); + MG_DEBUG(("%lu %ld closed", c->id, c->fd)); + MG_PROF_DUMP(c); + MG_PROF_FREE(c); + + mg_tls_free(c); + mg_iobuf_free(&c->recv); + mg_iobuf_free(&c->send); + mg_iobuf_free(&c->rtls); + mg_bzero((unsigned char *) c, sizeof(*c)); + free(c); } -static void write_conn(struct mg_connection *c) { - long len = c->is_tls ? mg_tls_send(c, c->send.buf, c->send.len) - : mg_io_send(c, c->send.buf, c->send.len); - if (len == MG_IO_ERR) { - mg_error(c, "tx err"); - } else if (len > 0) { - mg_iobuf_del(&c->send, 0, (size_t) len); - mg_call(c, MG_EV_WRITE, &len); +struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, + mg_event_handler_t fn, void *fn_data) { + struct mg_connection *c = NULL; + if (url == NULL || url[0] == '\0') { + MG_ERROR(("null url")); + } else if ((c = mg_alloc_conn(mgr)) == NULL) { + MG_ERROR(("OOM")); + } else { + LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); + c->is_udp = (strncmp(url, "udp:", 4) == 0); + c->fd = (void *) (size_t) MG_INVALID_SOCKET; + c->fn = fn; + c->is_client = true; + c->fn_data = fn_data; + MG_DEBUG(("%lu %ld %s", c->id, c->fd, url)); + mg_call(c, MG_EV_OPEN, (void *) url); + mg_resolve(c, url); } + return c; } -static void init_closure(struct mg_connection *c) { - struct connstate *s = (struct connstate *) (c + 1); - if (c->is_udp == false && c->is_listening == false && - c->is_connecting == false) { // For TCP conns, - struct mg_tcpip_if *ifp = - (struct mg_tcpip_if *) c->mgr->priv; // send TCP FIN - uint32_t rem_ip; - memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); - tx_tcp(ifp, s->mac, rem_ip, TH_FIN | TH_ACK, c->loc.port, c->rem.port, - mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0); - settmout(c, MIP_TTYPE_FIN); +struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url, + mg_event_handler_t fn, void *fn_data) { + struct mg_connection *c = NULL; + if ((c = mg_alloc_conn(mgr)) == NULL) { + MG_ERROR(("OOM %s", url)); + } else if (!mg_open_listener(c, url)) { + MG_ERROR(("Failed: %s, errno %d", url, errno)); + MG_PROF_FREE(c); + free(c); + c = NULL; + } else { + c->is_listening = 1; + c->is_udp = strncmp(url, "udp:", 4) == 0; + LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); + c->fn = fn; + c->fn_data = fn_data; + mg_call(c, MG_EV_OPEN, NULL); + if (mg_url_is_ssl(url)) c->is_tls = 1; // Accepted connection must + MG_DEBUG(("%lu %ld %s", c->id, c->fd, url)); } + return c; } -static void close_conn(struct mg_connection *c) { - struct connstate *s = (struct connstate *) (c + 1); - mg_iobuf_free(&s->raw); // For TLS connections, release raw data - mg_close_conn(c); +struct mg_connection *mg_wrapfd(struct mg_mgr *mgr, int fd, + mg_event_handler_t fn, void *fn_data) { + struct mg_connection *c = mg_alloc_conn(mgr); + if (c != NULL) { + c->fd = (void *) (size_t) fd; + c->fn = fn; + c->fn_data = fn_data; + MG_EPOLL_ADD(c); + mg_call(c, MG_EV_OPEN, NULL); + LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); + } + return c; } -static bool can_write(struct mg_connection *c) { - return c->is_connecting == 0 && c->is_resolving == 0 && c->send.len > 0 && - c->is_tls_hs == 0 && c->is_arplooking == 0; +struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds, + unsigned flags, void (*fn)(void *), void *arg) { + struct mg_timer *t = (struct mg_timer *) calloc(1, sizeof(*t)); + if (t != NULL) { + mg_timer_init(&mgr->timers, t, milliseconds, flags, fn, arg); + t->id = mgr->timerid++; + } + return t; } -void mg_mgr_poll(struct mg_mgr *mgr, int ms) { - struct mg_connection *c, *tmp; - uint64_t now = mg_millis(); - mg_tcpip_poll((struct mg_tcpip_if *) mgr->priv, now); - mg_timer_poll(&mgr->timers, now); - for (c = mgr->conns; c != NULL; c = tmp) { - tmp = c->next; - struct connstate *s = (struct connstate *) (c + 1); - mg_call(c, MG_EV_POLL, &now); - MG_VERBOSE(("%lu .. %c%c%c%c%c", c->id, c->is_tls ? 'T' : 't', - c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h', - c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c')); - if (can_write(c)) write_conn(c); - if (c->is_draining && c->send.len == 0 && s->ttype != MIP_TTYPE_FIN) - init_closure(c); - if (c->is_closing) close_conn(c); - } - (void) ms; +long mg_io_recv(struct mg_connection *c, void *buf, size_t len) { + if (c->rtls.len == 0) return MG_IO_WAIT; + if (len > c->rtls.len) len = c->rtls.len; + memcpy(buf, c->rtls.buf, len); + mg_iobuf_del(&c->rtls, 0, len); + return (long) len; } -bool mg_send(struct mg_connection *c, const void *buf, size_t len) { - struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; - bool res = false; - uint32_t rem_ip; - memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t)); - if (ifp->ip == 0 || ifp->state != MG_TCPIP_STATE_READY) { - mg_error(c, "net down"); - } else if (c->is_udp) { - struct connstate *s = (struct connstate *) (c + 1); - len = trim_len(c, len); // Trimming length if necessary - tx_udp(ifp, s->mac, ifp->ip, c->loc.port, rem_ip, c->rem.port, buf, len); - res = true; - } else { - res = mg_iobuf_add(&c->send, c->send.len, buf, len); - } - return res; +void mg_mgr_free(struct mg_mgr *mgr) { + struct mg_connection *c; + struct mg_timer *tmp, *t = mgr->timers; + while (t != NULL) tmp = t->next, free(t), t = tmp; + mgr->timers = NULL; // Important. Next call to poll won't touch timers + for (c = mgr->conns; c != NULL; c = c->next) c->is_closing = 1; + mg_mgr_poll(mgr, 0); +#if MG_ENABLE_FREERTOS_TCP + FreeRTOS_DeleteSocketSet(mgr->ss); +#endif + MG_DEBUG(("All connections closed")); +#if MG_ENABLE_EPOLL + if (mgr->epoll_fd >= 0) close(mgr->epoll_fd), mgr->epoll_fd = -1; +#endif + mg_tls_ctx_free(mgr); +} + +void mg_mgr_init(struct mg_mgr *mgr) { + memset(mgr, 0, sizeof(*mgr)); +#if MG_ENABLE_EPOLL + if ((mgr->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) + MG_ERROR(("epoll_create1 errno %d", errno)); +#else + mgr->epoll_fd = -1; +#endif +#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK + // clang-format off + { WSADATA data; WSAStartup(MAKEWORD(2, 2), &data); } + // clang-format on +#elif MG_ENABLE_FREERTOS_TCP + mgr->ss = FreeRTOS_CreateSocketSet(); +#elif defined(__unix) || defined(__unix__) || defined(__APPLE__) + // Ignore SIGPIPE signal, so if client cancels the request, it + // won't kill the whole process. + signal(SIGPIPE, SIG_IGN); +#endif + mgr->pipe = MG_INVALID_SOCKET; + mgr->dnstimeout = 3000; + mgr->dns4.url = "udp://8.8.8.8:53"; + mgr->dns6.url = "udp://[2001:4860:4860::8888]:53"; + mg_tls_ctx_init(mgr); } -#endif // MG_ENABLE_TCPIP #ifdef MG_ENABLE_LINES #line 1 "src/ota_dummy.c" @@ -6630,7 +6626,7 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) { return res; } -static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { +static void sntp_cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_READ) { int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len); if (milliseconds > 0) { @@ -6644,8 +6640,7 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { mg_sntp_request(c); } else if (ev == MG_EV_CLOSE) { } - (void) fnd; - (void) evd; + (void) ev_data; } void mg_sntp_request(struct mg_connection *c) { @@ -7296,7 +7291,7 @@ static bool mg_socketpair(MG_SOCKET_TYPE sp[2], union usa usa[2]) { } // mg_wakeup() event handler -static void wufn(struct mg_connection *c, int ev, void *evd, void *fnd) { +static void wufn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_READ) { unsigned long *id = (unsigned long *) c->recv.buf; // MG_INFO(("Got data")); @@ -7316,7 +7311,7 @@ static void wufn(struct mg_connection *c, int ev, void *evd, void *fnd) { closesocket(c->mgr->pipe); // When we're closing, close the other c->mgr->pipe = MG_INVALID_SOCKET; // side of the socketpair, too } - (void) evd, (void) fnd; + (void) ev_data; } bool mg_wakeup_init(struct mg_mgr *mgr) { @@ -13736,8 +13731,7 @@ static bool mg_ws_client_handshake(struct mg_connection *c) { return false; // Continue event handler } -static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data, - void *fn_data) { +static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data) { struct ws_msg msg; size_t ofs = (size_t) c->pfn_data; @@ -13803,7 +13797,6 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data, } } } - (void) fn_data; (void) ev_data; } diff --git a/mongoose.h b/mongoose.h index ca54a243618..aa4b815d4d0 100644 --- a/mongoose.h +++ b/mongoose.h @@ -2096,7 +2096,7 @@ typedef uint64_t uECC_word_t; struct mg_connection; typedef void (*mg_event_handler_t)(struct mg_connection *, int ev, - void *ev_data, void *fn_data); + void *ev_data); void mg_call(struct mg_connection *c, int ev, void *ev_data); void mg_error(struct mg_connection *c, const char *fmt, ...); diff --git a/src/dns.c b/src/dns.c index 675f20ded62..0df92881801 100644 --- a/src/dns.c +++ b/src/dns.c @@ -134,8 +134,7 @@ bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) { return true; } -static void dns_cb(struct mg_connection *c, int ev, void *ev_data, - void *fn_data) { +static void dns_cb(struct mg_connection *c, int ev, void *ev_data) { struct dns_data *d, *tmp; struct dns_data **head = (struct dns_data **) &c->mgr->active_dns_requests; if (ev == MG_EV_POLL) { @@ -189,7 +188,6 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data, mg_dns_free(head, d); } } - (void) fn_data; } static bool mg_dns_send(struct mg_connection *c, const struct mg_str *name, diff --git a/src/event.c b/src/event.c index b50d246a527..e6217f40f5f 100644 --- a/src/event.c +++ b/src/event.c @@ -19,8 +19,8 @@ void mg_call(struct mg_connection *c, int ev, void *ev_data) { // Run user-defined handler first, in order to give it an ability // to intercept processing (e.g. clean input buffer) before the // protocol handler kicks in - if (c->fn != NULL) c->fn(c, ev, ev_data, c->fn_data); - if (c->pfn != NULL) c->pfn(c, ev, ev_data, c->pfn_data); + if (c->fn != NULL) c->fn(c, ev, ev_data); + if (c->pfn != NULL) c->pfn(c, ev, ev_data); } void mg_error(struct mg_connection *c, const char *fmt, ...) { @@ -31,5 +31,5 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) { va_end(ap); MG_ERROR(("%lu %ld %s", c->id, c->fd, buf)); c->is_closing = 1; // Set is_closing before sending MG_EV_CALL - mg_call(c, MG_EV_ERROR, buf); // Let user handler to override it + mg_call(c, MG_EV_ERROR, buf); // Let user handler override it } diff --git a/src/event.h b/src/event.h index 8fb521d5654..1241437925e 100644 --- a/src/event.h +++ b/src/event.h @@ -2,7 +2,7 @@ struct mg_connection; typedef void (*mg_event_handler_t)(struct mg_connection *, int ev, - void *ev_data, void *fn_data); + void *ev_data); void mg_call(struct mg_connection *c, int ev, void *ev_data); void mg_error(struct mg_connection *c, const char *fmt, ...); diff --git a/src/http.c b/src/http.c index c948868331e..16608089a0d 100644 --- a/src/http.c +++ b/src/http.c @@ -1,7 +1,7 @@ +#include "http.h" #include "arch.h" #include "base64.h" #include "fmt.h" -#include "http.h" #include "json.h" #include "log.h" #include "net.h" @@ -431,7 +431,7 @@ void mg_http_reply(struct mg_connection *c, int code, const char *headers, c->is_resp = 0; } -static void http_cb(struct mg_connection *, int, void *, void *); +static void http_cb(struct mg_connection *, int, void *); static void restore_http_cb(struct mg_connection *c) { mg_fs_close((struct mg_fd *) c->pfn_data); c->pfn_data = NULL; @@ -445,10 +445,9 @@ char *mg_http_etag(char *buf, size_t len, size_t size, time_t mtime) { return buf; } -static void static_cb(struct mg_connection *c, int ev, void *ev_data, - void *fn_data) { +static void static_cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_WRITE || ev == MG_EV_POLL) { - struct mg_fd *fd = (struct mg_fd *) fn_data; + struct mg_fd *fd = (struct mg_fd *) c->pfn_data; // Read to send IO buffer directly, avoid extra on-stack buffer size_t n, max = MG_IO_SIZE, space; size_t *cl = (size_t *) &c->data[(sizeof(c->data) - sizeof(size_t)) / @@ -973,7 +972,7 @@ static int skip_chunk(const char *buf, int len, int *pl, int *dl) { return i + 2 + n + 2; } -static void http_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { +static void http_cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_READ || ev == MG_EV_CLOSE) { struct mg_http_message hm; size_t ofs = 0; // Parsing offset @@ -1035,10 +1034,10 @@ static void http_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { } if (ofs > 0) mg_iobuf_del(&c->recv, 0, ofs); // Delete processed data } - (void) evd, (void) fnd; + (void) ev_data; } -static void mg_hfn(struct mg_connection *c, int ev, void *ev_data, void *fnd) { +static void mg_hfn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/quit")) { @@ -1053,7 +1052,7 @@ static void mg_hfn(struct mg_connection *c, int ev, void *ev_data, void *fnd) { mg_http_reply(c, 200, "", "hi\n"); } } else if (ev == MG_EV_CLOSE) { - if (c->data[0] == 'X') *(bool *) fnd = true; + if (c->data[0] == 'X') *(bool *) c->fn_data = true; } } diff --git a/src/mqtt.c b/src/mqtt.c index 5301f586f1f..8531424bcf2 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -410,7 +410,8 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version, } if (p > end) return MQTT_MALFORMED; if (version == 5 && p + 2 < end) { - len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &m->props_size); + len_len = + (uint32_t) decode_varint(p, (size_t) (end - p), &m->props_size); if (!len_len) return MQTT_MALFORMED; m->props_start = (size_t) (p + len_len - buf); p += len_len + m->props_size; @@ -426,8 +427,7 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version, return MQTT_OK; } -static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data, - void *fn_data) { +static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_READ) { for (;;) { uint8_t version = c->is_mqtt5 ? 5 : 4; @@ -495,7 +495,6 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data, } } (void) ev_data; - (void) fn_data; } void mg_mqtt_ping(struct mg_connection *nc) { diff --git a/src/sntp.c b/src/sntp.c index 046ac6b5ee9..b3f2962ceb3 100644 --- a/src/sntp.c +++ b/src/sntp.c @@ -38,7 +38,7 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) { return res; } -static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { +static void sntp_cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_READ) { int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len); if (milliseconds > 0) { @@ -52,8 +52,7 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { mg_sntp_request(c); } else if (ev == MG_EV_CLOSE) { } - (void) fnd; - (void) evd; + (void) ev_data; } void mg_sntp_request(struct mg_connection *c) { diff --git a/src/sock.c b/src/sock.c index 4823caecbd4..6d98dd2a85c 100644 --- a/src/sock.c +++ b/src/sock.c @@ -620,7 +620,7 @@ static bool mg_socketpair(MG_SOCKET_TYPE sp[2], union usa usa[2]) { } // mg_wakeup() event handler -static void wufn(struct mg_connection *c, int ev, void *evd, void *fnd) { +static void wufn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_READ) { unsigned long *id = (unsigned long *) c->recv.buf; // MG_INFO(("Got data")); @@ -640,7 +640,7 @@ static void wufn(struct mg_connection *c, int ev, void *evd, void *fnd) { closesocket(c->mgr->pipe); // When we're closing, close the other c->mgr->pipe = MG_INVALID_SOCKET; // side of the socketpair, too } - (void) evd, (void) fnd; + (void) ev_data; } bool mg_wakeup_init(struct mg_mgr *mgr) { diff --git a/src/ws.c b/src/ws.c index 38f904db20c..7fc8b116559 100644 --- a/src/ws.c +++ b/src/ws.c @@ -163,8 +163,7 @@ static bool mg_ws_client_handshake(struct mg_connection *c) { return false; // Continue event handler } -static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data, - void *fn_data) { +static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data) { struct ws_msg msg; size_t ofs = (size_t) c->pfn_data; @@ -230,7 +229,6 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data, } } } - (void) fn_data; (void) ev_data; } diff --git a/test/fuzz.c b/test/fuzz.c index a0eb8d5a4cc..5301c147a27 100644 --- a/test/fuzz.c +++ b/test/fuzz.c @@ -14,12 +14,11 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *, size_t); int LLVMFuzzerTestOneInput(const uint8_t *, size_t); #endif -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn(struct mg_connection *c, int ev, void *ev_data) { struct mg_http_serve_opts opts = {.root_dir = "."}; if (ev == MG_EV_HTTP_MSG) { mg_http_serve_dir(c, (struct mg_http_message *) ev_data, &opts); } - (void) fn_data; } int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { @@ -110,7 +109,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { const char *url = "http://localhost:12345"; struct mg_connection *c = mg_http_connect(&mgr, url, fn, NULL); mg_iobuf_add(&c->recv, 0, data, size); - c->pfn(c, MG_EV_READ, NULL, NULL); + c->pfn(c, MG_EV_READ, NULL); // manually invoke protocol event handler mg_mgr_free(&mgr); free(pkt); diff --git a/test/mip_tap_test.c b/test/mip_tap_test.c index dcb8a93aab0..4fd1e3efcfd 100644 --- a/test/mip_tap_test.c +++ b/test/mip_tap_test.c @@ -59,18 +59,16 @@ struct Post_reply { }; char *fetch(struct mg_mgr *mgr, const char *url, const char *post_data); -static void f_http_fetch_query(struct mg_connection *c, int ev, void *ev_data, - void *fn_data); +static void f_http_fetch_query(struct mg_connection *c, int ev, void *ev_data); int get_response_code(char *); // Returns HTTP status code from full char* msg -static void f_http_fetch_query(struct mg_connection *c, int ev, void *ev_data, - void *fn_data) { +static void f_http_fetch_query(struct mg_connection *c, int ev, void *ev_data) { static char *http_response = 0; static bool http_response_allocated = 0; // So that we will update out parameter unsigned int http_responses_received = 0; struct Post_reply *post_reply_l; - post_reply_l = (struct Post_reply *) fn_data; + post_reply_l = (struct Post_reply *) c->fn_data; if (ev == MG_EV_CONNECT) { mg_printf(c, post_reply_l->post); @@ -159,8 +157,7 @@ static void test_http_fetch(struct mg_mgr *mgr) { static struct mg_connection *s_conn; static char s_topic[16]; -static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data, - void *fn_data) { +static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_MQTT_OPEN) { // MQTT connect is successful struct mg_mqtt_opts sub_opts; @@ -180,7 +177,7 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data, ASSERT(0); if (mm->data.len != 2 || strcmp(mm->data.ptr, "hi")) ASSERT(0); mg_mqtt_disconnect(c, NULL); - *(bool *) fn_data = true; + *(bool *) c->fn_data = true; } else if (ev == MG_EV_CLOSE) { s_conn = NULL; } diff --git a/test/mip_test.c b/test/mip_test.c index 5e2dcdd90fd..a143b999a25 100644 --- a/test/mip_test.c +++ b/test/mip_test.c @@ -30,25 +30,25 @@ static void test_statechange(void) { onstatechange(&iface); } -static void ph(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - if (ev == MG_EV_POLL) ++(*(int *) fn_data); +static void ph(struct mg_connection *c, int ev, void *ev_data) { + if (ev == MG_EV_POLL) ++(*(int *) c->fn_data); (void) c, (void) ev_data; } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - (void) c, (void) ev, (void) ev_data, (void) fn_data; +static void fn(struct mg_connection *c, int ev, void *ev_data) { + (void) c, (void) ev, (void) ev_data; } -static void frag_recv_fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void frag_recv_fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_ERROR) { if (s_sent_fragment) { ASSERT(strcmp((char*) ev_data, "Received fragmented packet") == 0); } } - (void) c, (void) ev_data, (void) fn_data; + (void) c, (void) ev_data; } -static void frag_send_fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void frag_send_fn(struct mg_connection *c, int ev, void *ev_data) { static bool s_sent; static int s_seg_sizes[] = {416, 416, 368}; if (ev == MG_EV_POLL) { @@ -60,7 +60,7 @@ static void frag_send_fn(struct mg_connection *c, int ev, void *ev_data, void *f // Checking TCP segment sizes (ev_data points to the TCP payload length) ASSERT(*(int *) ev_data == s_seg_sizes[s_seg_sent++]); } - (void) c, (void) ev_data, (void) fn_data; + (void) c, (void) ev_data; } static void test_poll(void) { diff --git a/test/unit_test.c b/test/unit_test.c index 79d1315a95d..a219b2a30ca 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -344,10 +344,10 @@ static void test_iobuf(void) { mg_iobuf_free(&io); } -static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { +static void sntp_cb(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_SNTP_TIME) { - int64_t received = *(int64_t *) evd; - *(int64_t *) fnd = received; + int64_t received = *(int64_t *) ev_data; + *(int64_t *) c->fn_data = received; MG_DEBUG(("got time: %lld", received)); #if MG_ARCH == MG_ARCH_UNIX struct timeval tv = {0, 0}; @@ -409,16 +409,16 @@ struct mqtt_data { #define flags_released (1 << 3) #define flags_completed (1 << 4) -static void mqtt_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { - struct mqtt_data *test_data = (struct mqtt_data *) fnd; +static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) { + struct mqtt_data *test_data = (struct mqtt_data *) c->fn_data; char *buf = test_data->msg; if (ev == MG_EV_MQTT_OPEN) { - buf[0] = *(int *) evd == 0 ? 'X' : 'Y'; + buf[0] = *(int *) ev_data == 0 ? 'X' : 'Y'; } else if (ev == MG_EV_CLOSE) { buf[0] = 0; } else if (ev == MG_EV_MQTT_CMD) { - struct mg_mqtt_message *mm = (struct mg_mqtt_message *) evd; + struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data; if (mm->cmd == MQTT_CMD_SUBACK) { test_data->flags = flags_subscribed; } else if (mm->cmd == MQTT_CMD_PUBACK) { // here we assume the broker @@ -431,7 +431,7 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { test_data->flags |= flags_completed; } } else if (ev == MG_EV_MQTT_MSG) { - struct mg_mqtt_message *mm = (struct mg_mqtt_message *) evd; + struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data; snprintf(test_data->topic, test_data->topicsize, "%.*s", (int) mm->topic.len, mm->topic.ptr); snprintf(buf + 1, test_data->msgsize - 2, "%.*s", (int) mm->data.len, @@ -686,8 +686,8 @@ static void test_mqtt(void) { test_mqtt_ver(5); } -static void eh1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - struct mg_tls_opts *topts = (struct mg_tls_opts *) fn_data; +static void eh1(struct mg_connection *c, int ev, void *ev_data) { + struct mg_tls_opts *topts = (struct mg_tls_opts *) c->fn_data; if (ev == MG_EV_ACCEPT && topts != NULL) mg_tls_init(c, topts); if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; @@ -751,7 +751,6 @@ static void eh1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { struct mg_ws_message *wm = (struct mg_ws_message *) ev_data; mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_BINARY); } - (void) fn_data; } struct fetch_data { @@ -759,8 +758,8 @@ struct fetch_data { int code, closed; }; -static void fcb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - struct fetch_data *fd = (struct fetch_data *) fn_data; +static void fcb(struct mg_connection *c, int ev, void *ev_data) { + struct fetch_data *fd = (struct fetch_data *) c->fn_data; if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; snprintf(fd->buf, FETCH_BUF_SIZE, "%.*s", (int) hm->message.len, @@ -827,8 +826,8 @@ static bool cmpheader(const char *buf, const char *name, const char *value) { return h != NULL && mg_strcmp(*h, mg_str(value)) == 0; } -static void wcb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - int *p = (int *) fn_data; +static void wcb(struct mg_connection *c, int ev, void *ev_data) { + int *p = (int *) c->fn_data; if (ev == MG_EV_WS_OPEN) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; struct mg_str *wsproto = mg_http_get_header(hm, "Sec-WebSocket-Protocol"); @@ -848,7 +847,7 @@ static void wcb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } } -static void ew2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void ew2(struct mg_connection *c, int ev, void *ev_data) { size_t size = 65 * 1024 + 737; if (ev == MG_EV_WS_OPEN) { char *msg = (char *) calloc(1, size + 1); @@ -866,7 +865,7 @@ static void ew2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (wm->data.ptr[i] != 'A') ok = 0; } ASSERT(ok == 1); - *(int *) fn_data = 1; + *(int *) c->fn_data = 1; } } } @@ -897,10 +896,10 @@ static void test_ws(void) { ASSERT(mgr.conns == NULL); } -static void eh9(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void eh9(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_ERROR) { ASSERT(!strcmp((char *) ev_data, "socket error")); - *(int *) fn_data = 7; + *(int *) c->fn_data = 7; } (void) c; } @@ -1166,7 +1165,7 @@ static void test_http_server(void) { ASSERT(mgr.conns == NULL); } -static void h4(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void h4(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; MG_INFO(("[%.*s %.*s] message len %d", (int) hm->method.len, hm->method.ptr, @@ -1190,7 +1189,6 @@ static void h4(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_serve_dir(c, hm, &opts); } } - (void) fn_data; } static void test_http_404(void) { @@ -1242,8 +1240,8 @@ static void test_tls(void) { #endif } -static void f3(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - int *ok = (int *) fn_data; +static void f3(struct mg_connection *c, int ev, void *ev_data) { + int *ok = (int *) c->fn_data; // MG_INFO(("%d", ev)); if (ev == MG_EV_CONNECT) { // c->is_hexdumping = 1; @@ -1358,27 +1356,27 @@ static void test_host_validation(void) { #endif } -static void f4(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void f4(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; mg_printf(c, "HTTP/1.0 200 OK\n\n%.*s/%s", (int) hm->uri.len, hm->uri.ptr, "abcdef"); - strcat((char *) fn_data, "m"); + strcat((char *) c->fn_data, "m"); c->is_draining = 1; } else if (ev == MG_EV_CLOSE) { - strcat((char *) fn_data, "c"); + strcat((char *) c->fn_data, "c"); } } -static void f4c(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void f4c(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_CONNECT) { mg_printf(c, "GET /foo/bar HTTP/1.0\n\n"); } else if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; ASSERT(mg_strcmp(hm->body, mg_str("/foo/bar/abcdef")) == 0); - strcat((char *) fn_data, "m"); + strcat((char *) c->fn_data, "m"); } else if (ev == MG_EV_CLOSE) { - strcat((char *) fn_data, "c"); + strcat((char *) c->fn_data, "c"); } } @@ -1398,11 +1396,11 @@ static void test_http_no_content_length(void) { ASSERT(mgr.conns == NULL); } -static void f5(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void f5(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; mg_http_reply(c, 200, "", "%.*s", (int) hm->uri.len, hm->uri.ptr); - (*(int *) fn_data)++; + (*(int *) c->fn_data)++; } } @@ -1616,7 +1614,7 @@ static void test_http_parse(void) { } } -static void ehr(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void ehr(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; struct mg_http_serve_opts opts; @@ -1624,7 +1622,6 @@ static void ehr(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { opts.root_dir = "./test/data"; mg_http_serve_dir(c, hm, &opts); } - (void) fn_data; } static void test_http_range(void) { @@ -2109,10 +2106,10 @@ static void test_str(void) { } } -static void fn1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void fn1(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_ERROR) { - free(*(char **) fn_data); // See #2263 - *(char **) fn_data = mg_mprintf("%s", (char *) ev_data); + free(*(char **) c->fn_data); // See #2263 + *(char **) c->fn_data = mg_mprintf("%s", (char *) ev_data); } (void) c; } @@ -2329,7 +2326,7 @@ static void test_crc32(void) { ASSERT(mg_crc32(mg_crc32(0, "ab", 2), "c", 1) == 891568578); } -static void us(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void us(struct mg_connection *c, int ev, void *ev_data) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (ev == MG_EV_HTTP_MSG && mg_http_match_uri(hm, "/upload")) { MG_DEBUG(("Got all %lu bytes!", (unsigned long) hm->body.len)); @@ -2340,11 +2337,10 @@ static void us(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } else if (ev == MG_EV_HTTP_MSG) { mg_http_reply(c, 200, "", "ok\n"); } - (void) fn_data; } -static void uc(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - const char **s = (const char **) fn_data; +static void uc(struct mg_connection *c, int ev, void *ev_data) { + const char **s = (const char **) c->fn_data; if (ev == MG_EV_OPEN) { // c->is_hexdumping = 1; } else if (ev == MG_EV_CONNECT) { @@ -2360,7 +2356,6 @@ static void uc(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { ASSERT(mg_strcmp(hm->body, mg_str(*s)) == 0); *s = NULL; } - (void) fn_data; } static void test_http_upload(void) { @@ -2380,7 +2375,7 @@ static void test_http_upload(void) { #define LONG_CHUNK "chunk with length taking up more than two hex digits" -static void eX(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void eX(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); c->data[0] = 1; @@ -2398,10 +2393,10 @@ static void eX(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { c->data[0] = 0; } } - (void) ev_data, (void) fn_data; + (void) ev_data; } -static void eY(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void eY(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\n"); c->data[0] = 1; @@ -2411,19 +2406,19 @@ static void eY(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (c->data[0] == 12) mg_send(c, "bc", 2); if (c->data[0] == 30) mg_send(c, "d", 1), c->is_resp = 0, c->data[0] = 0; } - (void) ev_data, (void) fn_data; + (void) ev_data; } -static void eZ(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void eZ(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { mg_http_reply(c, 200, "", "abcd"); } - (void) ev_data, (void) fn_data; + (void) ev_data; } // Do not delete chunks as they arrive -static void eh4(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - uint32_t *crc = (uint32_t *) fn_data; +static void eh4(struct mg_connection *c, int ev, void *ev_data) { + uint32_t *crc = (uint32_t *) c->fn_data; if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; *crc = mg_crc32(*crc, hm->body.ptr, hm->body.len); @@ -2486,8 +2481,8 @@ struct stream_status { }; // Consume recv buffer after letting it reach MG_MAX_RECV_SIZE -static void eh8(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - struct stream_status *status = (struct stream_status *) fn_data; +static void eh8(struct mg_connection *c, int ev, void *ev_data) { + struct stream_status *status = (struct stream_status *) c->fn_data; if (c->is_listening) return; ASSERT(c->recv.len <= MG_MAX_RECV_SIZE); @@ -2523,19 +2518,17 @@ static void eh8(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } // Toggle c->is_full to prevent max_recv_buf_size reached read errors -static void eh10(struct mg_connection *c, int ev, void *ev_data, - void *fn_data) { +static void eh10(struct mg_connection *c, int ev, void *ev_data) { if (c->recv.len >= MG_MAX_RECV_SIZE && ev == MG_EV_READ) c->is_full = true; - eh8(c, ev, ev_data, fn_data); + eh8(c, ev, ev_data); if (c->recv.len < MG_MAX_RECV_SIZE && ev == MG_EV_POLL) c->is_full = false; } // Send buffer larger than MG_MAX_RECV_SIZE to server -static void eh11(struct mg_connection *c, int ev, void *ev_data, - void *fn_data) { - struct stream_status *status = (struct stream_status *) fn_data; +static void eh11(struct mg_connection *c, int ev, void *ev_data) { + struct stream_status *status = (struct stream_status *) c->fn_data; if (ev == MG_EV_CONNECT) { size_t len = MG_MAX_RECV_SIZE * 2; struct mg_iobuf buf = {NULL, 0, 0, 0}; @@ -2548,7 +2541,6 @@ static void eh11(struct mg_connection *c, int ev, void *ev_data, mg_iobuf_free(&buf); } (void) ev_data; - (void) fn_data; } static void test_http_stream_buffer(void) { @@ -2601,7 +2593,7 @@ static void test_multipart(void) { ASSERT(mg_http_next_multipart(mg_str(s), ofs, &part) == 0); } -static void eh7(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void eh7(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; struct mg_http_serve_opts sopts; @@ -2610,7 +2602,6 @@ static void eh7(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { sopts.fs = &mg_fs_packed; mg_http_serve_dir(c, hm, &sopts); } - (void) fn_data; } static void test_packed(void) { @@ -2655,14 +2646,14 @@ int send(int sock, const void *buf, size_t len, int flags) { } #endif -static void u1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void u1(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_CONNECT) { - ((int *) fn_data)[0] += 1; + ((int *) c->fn_data)[0] += 1; mg_send(c, "hi", 2); } else if (ev == MG_EV_WRITE) { - ((int *) fn_data)[0] += 100; + ((int *) c->fn_data)[0] += 100; } else if (ev == MG_EV_READ) { - ((int *) fn_data)[0] += 10; + ((int *) c->fn_data)[0] += 10; mg_iobuf_free(&c->recv); } (void) ev_data; @@ -2698,7 +2689,7 @@ static void test_check_ip_acl(void) { -1); // not yet supported } -static void w3(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void w3(struct mg_connection *c, int ev, void *ev_data) { // MG_INFO(("ev %d", ev)); if (ev == MG_EV_WS_OPEN) { char buf[8192]; @@ -2711,20 +2702,20 @@ static void w3(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } else if (ev == MG_EV_WS_MSG) { struct mg_ws_message *wm = (struct mg_ws_message *) ev_data; ASSERT(mg_strcmp(wm->data, mg_str("lebowski")) == 0); - ((int *) fn_data)[0]++; + ((int *) c->fn_data)[0]++; } else if (ev == MG_EV_CLOSE) { - ((int *) fn_data)[0] += 10; + ((int *) c->fn_data)[0] += 10; } } -static void w2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void w2(struct mg_connection *c, int ev, void *ev_data) { struct mg_str msg = mg_str_n("lebowski", 8); if (ev == MG_EV_HTTP_MSG) { mg_ws_upgrade(c, (struct mg_http_message *) ev_data, NULL); } else if (ev == MG_EV_WS_OPEN) { mg_ws_send(c, "x", 1, WEBSOCKET_OP_PONG); } else if (ev == MG_EV_POLL && c->is_websocket) { - size_t ofs, n = (size_t) fn_data; + size_t ofs, n = (size_t) c->fn_data; if (n < msg.len) { // Send "msg" char by char using fragmented frames // mg_ws_send() sets the FIN flag in the WS header. Clean it @@ -2771,7 +2762,7 @@ static void test_ws_fragmentation(void) { ASSERT(mgr.conns == NULL); } -static void h7(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { +static void h7(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; struct mg_http_serve_opts opts; @@ -2779,7 +2770,6 @@ static void h7(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { opts.root_dir = "./test/data,/foo=./src"; mg_http_serve_dir(c, hm, &opts); } - (void) fn_data; } static void test_rewrites(void) { @@ -3111,8 +3101,8 @@ static void test_rpc(void) { ASSERT(head == NULL); } -static void ph(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - if (ev == MG_EV_POLL) ++(*(int *) fn_data); +static void ph(struct mg_connection *c, int ev, void *ev_data) { + if (ev == MG_EV_POLL) ++(*(int *) c->fn_data); (void) c, (void) ev_data; }