Merge pull request #2560 from cesanta/fn_data

remove fn_data from event handler signature
This commit is contained in:
Sergio R. Caprile 2024-01-09 15:40:46 -03:00 committed by GitHub
commit 5826d0e41c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
75 changed files with 516 additions and 582 deletions

View File

@ -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

View File

@ -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);

View File

@ -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 = {};

View File

@ -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;
}

View File

@ -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");

View File

@ -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) {

View File

@ -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;
}

View File

@ -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);

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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
}
}

View File

@ -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;

View File

@ -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) {

View File

@ -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

View File

@ -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) {

View File

@ -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);
@ -40,7 +40,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
data[1] += c->recv.len;
fwrite(c->recv.buf, 1, c->recv.len, stdout);
c->recv.len = 0; // And cleanup the receive buffer. Streming!
if (data[1] >= data[0]) * (bool *) fn_data = true;
if (data[1] >= data[0]) * (bool *) c->fn_data = true;
} else {
struct mg_http_message hm;
int n = mg_http_parse((char *) c->recv.buf, c->recv.len, &hm);
@ -50,13 +50,13 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
data[0] = n + hm.body.len;
data[1] = c->recv.len;
c->recv.len = 0; // Cleanup receive buffer
if (data[1] >= data[0]) * (bool *) fn_data = true;
if (data[1] >= data[0]) * (bool *) c->fn_data = true;
}
}
} 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;
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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

View File

@ -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;
}

View File

@ -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[]) {

View File

@ -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");

File diff suppressed because one or more lines are too long

View File

@ -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
}
}

View File

@ -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

View File

@ -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

View File

@ -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
}
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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

View File

@ -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

View File

@ -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");
}

View File

@ -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) {

View File

@ -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.

View File

@ -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;
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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

View File

@ -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

View File

@ -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) {

View File

@ -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) {

View File

@ -4,7 +4,7 @@
#include "mongoose.h"
const char *s_listening_url = "http://0.0.0.0:8000";
void uart_bridge_fn(struct mg_connection *, int, void *, void *);
void uart_bridge_fn(struct mg_connection *, int, void *);
int main(void) {
struct mg_mgr mgr;

View File

@ -64,24 +64,23 @@ void config_write(struct mg_str config) {
#endif
// Event handler for a connected Websocket client
static void ws_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
static void ws_fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
mg_ws_upgrade(c, evd, NULL);
mg_ws_upgrade(c, ev_data, NULL);
} else if (ev == MG_EV_WS_OPEN) {
// c->is_hexdumping = 1;
c->data[0] = 'W'; // When WS handhake is done, mark us as WS client
} else if (ev == MG_EV_WS_MSG) {
struct mg_ws_message *wm = (struct mg_ws_message *) evd;
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
uart_write(wm->data.ptr, wm->data.len); // Send to UART
c->recv.len = 0; // Discard received data
} else if (ev == MG_EV_CLOSE) {
if (c->is_listening) s_state.websocket.c = NULL;
}
(void) fnd;
}
// Event handler for a connected TCP client
static void tcp_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
static void tcp_fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_ACCEPT) {
// c->is_hexdumping = 1;
c->data[0] = 'T'; // When client is connected, mark us as TCP client
@ -91,7 +90,7 @@ static void tcp_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
} else if (ev == MG_EV_CLOSE) {
if (c->is_listening) s_state.tcp.c = NULL;
}
(void) fnd, (void) evd;
(void) ev_data;
}
// Extract topic name from the MQTT address
@ -102,7 +101,7 @@ static struct mg_str mqtt_topic(const char *name, const char *dflt) {
}
// Event handler for MQTT connection
static void mq_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
static void mq_fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) {
// c->is_hexdumping = 1;
} else if (ev == MG_EV_MQTT_OPEN) {
@ -113,12 +112,11 @@ static void mq_fn(struct mg_connection *c, int ev, void *evd, void *fnd) {
sub_opts.qos = 1;
mg_mqtt_sub(c, &sub_opts); // Subscribe to RX topic
} else if (ev == MG_EV_MQTT_MSG) {
struct mg_mqtt_message *mm = evd; // MQTT message
struct mg_mqtt_message *mm = ev_data; // MQTT message
uart_write(mm->data.ptr, mm->data.len); // Send to UART
} else if (ev == MG_EV_CLOSE) {
s_state.mqtt.c = NULL;
}
(void) fnd, (void) evd;
}
// Software timer with a frequency close to the scheduling time slot

View File

@ -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) {

View File

@ -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

View File

@ -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);

View File

@ -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
}
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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

View File

@ -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) {

View File

@ -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,9 +270,9 @@ 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
if (c->fn_data != NULL) { // TLS
struct mg_tls_opts opts = {0};
opts.cert = mg_str(s_tls_cert);
opts.key = mg_str(s_tls_key);
@ -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) {

View File

@ -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));

View File

@ -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));

View File

@ -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));

View File

@ -13,10 +13,10 @@ 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) {
} else 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) {
@ -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));

View File

@ -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) {
@ -4191,289 +4187,6 @@ struct mg_connection *mg_mqtt_listen(struct mg_mgr *mgr, const char *url,
return c;
}
#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);
}
addr->is_ip6 = true;
return true;
}
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);
}
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
@ -5583,6 +5296,289 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
}
#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);
}
addr->is_ip6 = true;
return true;
}
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);
}
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/ota_dummy.c"
#endif
@ -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;
}

View File

@ -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, ...);

View File

@ -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,

View File

@ -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
}

View File

@ -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, ...);

View File

@ -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;
}
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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;
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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) {

View File

@ -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;
}