diff --git a/Makefile b/Makefile index 93c341f6..7ceb6c90 100644 --- a/Makefile +++ b/Makefile @@ -9,8 +9,7 @@ INCS ?= -Isrc -I. SSL ?= MBEDTLS CWD ?= $(realpath $(CURDIR)) ENV ?= -e Tmp=. -e WINEDEBUG=-all -D ?= docker -DOCKER ?= $(D) run --rm $(ENV) -v $(CWD):$(CWD) -w $(CWD) +DOCKER ?= docker run --platform linux/amd64 --rm $(ENV) -v $(CWD):$(CWD) -w $(CWD) VCFLAGS = /nologo /W3 /O2 /MD /I. $(DEFS) $(TFLAGS) IPV6 ?= 1 ASAN ?= -fsanitize=address,undefined -fno-sanitize-recover=all @@ -42,13 +41,13 @@ examples: test/packed_fs.c: Makefile src/ssi.h test/fuzz.c test/data/a.txt $(CC) $(CFLAGS) test/pack.c -o pack - ./pack Makefile src/ssi.h test/fuzz.c test/data/a.txt test/data/range.txt > $@ + $(RUN) ./pack Makefile src/ssi.h test/fuzz.c test/data/a.txt test/data/range.txt > $@ DIR ?= test/data/ OUT ?= fs_packed.c mkfs: $(CC) $(CFLAGS) test/pack.c -o pack - ./pack -s $(DIR) `find $(DIR) -type f` > $(OUT) + $(RUN) ./pack -s $(DIR) `find $(DIR) -type f` > $(OUT) # find $(DIR) -type f | sed -e s,^$(DIR),,g -e s,^/,,g # Check that all external (exported) symbols have "mg_" prefix @@ -56,14 +55,15 @@ mg_prefix: mongoose.c mongoose.h $(CC) mongoose.c $(CFLAGS) -c -o /tmp/x.o && nm /tmp/x.o | grep ' T' | grep -v 'mg_' ; test $$? = 1 # C++ build +test++: test test++: CC = g++ test++: C_WARN = -std=c++2a -Wno-vla -Wno-shadow -Wno-missing-field-initializers -Wno-deprecated -test++: test musl: test musl: ASAN = musl: WARN += -Wno-sign-conversion musl: CC = $(DOCKER) mdashnet/cc1 gcc +musl: RUN = $(DOCKER) mdashnet/cc1 # Make sure we can build from an unamalgamated sources unamalgamated: $(HDRS) Makefile test/packed_fs.c diff --git a/docs/README.md b/docs/README.md index 6b9325e0..73e104a5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1799,81 +1799,6 @@ Return value: None mg_mqtt_sub(c, mg_str("my/topic"), 1); ``` -### mg\_mqtt\_next\_sub() - -```c -size_t mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic, uint8_t *qos, size_t pos); -``` - -Traverse list of subscribed topics. -Used to implement MQTT server when `MQTT_CMD_SUBSCRIBE` is received. -Initial position `pos` should be 4. - -Parameters: -- `mgs` - MQTT message -- `topic` - Pointer to `mg_str` to receive topic -- `qos` - Pointer to `uint8_t` to receive QoS -- `pos` - Position to list from - -Return value: Next position, or 0 when done - -Usage example: - -```c -// Mongoose events handler -void fn(struct mg_connection *c, int ev, void *evd, void *fnd) { - if (ev == MG_EV_MQTT_CMD) { - struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data; - if (mm->cmd == MQTT_CMD_SUBSCRIBE) { - size_t pos = 4; - uint8_t qos; - struct mg_str topic; - // Iterate over all subscribed topics - while ((pos = mg_mqtt_next_sub(mm, &topic, &qos, pos)) > 0) { - LOG(LL_INFO, ("SUB [%.*s]", (int) topic.len, topic.ptr)); - } - } - } -} -``` - -### mg\_mqtt\_next\_unsub() - -```c -size_t mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic, size_t pos); -``` - -Same as `mg_mqtt_next_sub()`, but for unsubscribed topics. The difference -is that there is no QoS in unsubscribe request. - -Parameters: -- `mgs` - MQTT message -- `topic` - Pointer to `mg_str` to receive topic -- `pos` - Position from which to list - -Return value: Next position, or 0 when done - -Usage example: - -```c -// Mongoose events handler -void fn(struct mg_connection *c, int ev, void *evd, void *fnd) { - if (ev == MG_EV_MQTT_CMD) { - struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data; - if (mm->cmd == MQTT_CMD_UNSUBSCRIBE) { - size_t pos = 4; - struct mg_str topic; - if (mm->cmd == MQTT_CMD_UNSUBSCRIBE) { - // Iterate over all unsubscribed topics - while ((pos = mg_mqtt_next_unsub(mm, &topic, pos)) > 0) { - LOG(LL_INFO, ("SUB [%.*s]", (int) topic.len, topic.ptr)); - } - } - } - } -} -``` - ### mg\_mqtt\_send_header() ```c diff --git a/examples/mqtt-server/main.c b/examples/mqtt-server/main.c index d902e790..e2b80ae8 100644 --- a/examples/mqtt-server/main.c +++ b/examples/mqtt-server/main.c @@ -26,6 +26,32 @@ static void signal_handler(int signo) { s_signo = signo; } +static size_t mg_mqtt_next_topic(struct mg_mqtt_message *msg, + struct mg_str *topic, uint8_t *qos, + size_t pos) { + unsigned char *buf = (unsigned char *) msg->dgram.ptr + pos; + size_t new_pos; + if (pos >= msg->dgram.len) return 0; + + topic->len = (size_t) (((unsigned) buf[0]) << 8 | buf[1]); + topic->ptr = (char *) buf + 2; + new_pos = pos + 2 + topic->len + (qos == NULL ? 0 : 1); + if ((size_t) new_pos > msg->dgram.len) return 0; + if (qos != NULL) *qos = buf[2 + topic->len]; + return new_pos; +} + +size_t mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic, + uint8_t *qos, size_t pos) { + uint8_t tmp; + return mg_mqtt_next_topic(msg, topic, qos == NULL ? &tmp : qos, pos); +} + +size_t mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic, + size_t pos) { + return mg_mqtt_next_topic(msg, topic, NULL, pos); +} + // Event handler function static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (ev == MG_EV_MQTT_CMD) { diff --git a/mongoose.c b/mongoose.c index 7faca03e..605b7a3f 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3172,32 +3172,6 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version, return MQTT_OK; } -static size_t mg_mqtt_next_topic(struct mg_mqtt_message *msg, - struct mg_str *topic, uint8_t *qos, - size_t pos) { - unsigned char *buf = (unsigned char *) msg->dgram.ptr + pos; - size_t new_pos; - if (pos >= msg->dgram.len) return 0; - - topic->len = (size_t) (((unsigned) buf[0]) << 8 | buf[1]); - topic->ptr = (char *) buf + 2; - new_pos = pos + 2 + topic->len + (qos == NULL ? 0 : 1); - if ((size_t) new_pos > msg->dgram.len) return 0; - if (qos != NULL) *qos = buf[2 + topic->len]; - return new_pos; -} - -size_t mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic, - uint8_t *qos, size_t pos) { - uint8_t tmp; - return mg_mqtt_next_topic(msg, topic, qos == NULL ? &tmp : qos, pos); -} - -size_t mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic, - size_t pos) { - return mg_mqtt_next_topic(msg, topic, NULL, pos); -} - static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (ev == MG_EV_READ) { @@ -3439,7 +3413,7 @@ void mg_close_conn(struct mg_connection *c) { // 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 closed", c->id)); + MG_DEBUG(("%lu %p closed", c->id, c->fd)); mg_tls_free(c); mg_iobuf_free(&c->recv); @@ -4280,7 +4254,6 @@ static void close_conn(struct mg_connection *c) { #if MG_ARCH == MG_ARCH_FREERTOS_TCP FreeRTOS_FD_CLR(c->fd, c->mgr->ss, eSELECT_ALL); #endif - c->fd = NULL; } mg_close_conn(c); } @@ -4380,10 +4353,9 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { MG_ERROR(("%lu OOM", lsn->id)); closesocket(fd); } else { - char buf[40]; + // char buf[40]; tomgaddr(&usa, &c->rem, sa_len != sizeof(usa.sin)); - mg_straddr(&c->rem, buf, sizeof(buf)); - MG_DEBUG(("%lu accepted %s", c->id, buf)); + // mg_straddr(&c->rem, buf, sizeof(buf)); LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); c->fd = S2PTR(fd); MG_EPOLL_ADD(c); @@ -4396,6 +4368,9 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { c->pfn_data = lsn->pfn_data; c->fn = lsn->fn; c->fn_data = lsn->fn_data; + MG_DEBUG(("%lu %p accepted %x.%hu -> %x.%hu", c->id, c->fd, + mg_ntohl(c->rem.ip), mg_ntohs(c->rem.port), mg_ntohl(c->loc.ip), + mg_ntohs(c->loc.port))); mg_call(c, MG_EV_OPEN, NULL); mg_call(c, MG_EV_ACCEPT, NULL); } @@ -5918,6 +5893,7 @@ void mg_ws_upgrade(struct mg_connection *c, struct mg_http_message *hm, ws_handshake(c, wskey, wsproto, fmt, &ap); va_end(ap); c->is_websocket = 1; + c->is_resp = 0; mg_call(c, MG_EV_WS_OPEN, hm); } } diff --git a/mongoose.h b/mongoose.h index 30cc8634..076e7a42 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1324,10 +1324,6 @@ void mg_mqtt_sub(struct mg_connection *, struct mg_str topic, int qos); int mg_mqtt_parse(const uint8_t *, size_t, uint8_t, struct mg_mqtt_message *); void mg_mqtt_send_header(struct mg_connection *, uint8_t cmd, uint8_t flags, uint32_t len); -size_t mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic, - uint8_t *qos, size_t pos); -size_t mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic, - size_t pos); void mg_mqtt_ping(struct mg_connection *); void mg_mqtt_pong(struct mg_connection *); void mg_mqtt_disconnect(struct mg_connection *); diff --git a/src/mqtt.c b/src/mqtt.c index 4daa3bc4..3e39d506 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -185,32 +185,6 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version, return MQTT_OK; } -static size_t mg_mqtt_next_topic(struct mg_mqtt_message *msg, - struct mg_str *topic, uint8_t *qos, - size_t pos) { - unsigned char *buf = (unsigned char *) msg->dgram.ptr + pos; - size_t new_pos; - if (pos >= msg->dgram.len) return 0; - - topic->len = (size_t) (((unsigned) buf[0]) << 8 | buf[1]); - topic->ptr = (char *) buf + 2; - new_pos = pos + 2 + topic->len + (qos == NULL ? 0 : 1); - if ((size_t) new_pos > msg->dgram.len) return 0; - if (qos != NULL) *qos = buf[2 + topic->len]; - return new_pos; -} - -size_t mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic, - uint8_t *qos, size_t pos) { - uint8_t tmp; - return mg_mqtt_next_topic(msg, topic, qos == NULL ? &tmp : qos, pos); -} - -size_t mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic, - size_t pos) { - return mg_mqtt_next_topic(msg, topic, NULL, pos); -} - static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { if (ev == MG_EV_READ) { diff --git a/src/mqtt.h b/src/mqtt.h index ca072c4d..f93619d5 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -56,10 +56,6 @@ void mg_mqtt_sub(struct mg_connection *, struct mg_str topic, int qos); int mg_mqtt_parse(const uint8_t *, size_t, uint8_t, struct mg_mqtt_message *); void mg_mqtt_send_header(struct mg_connection *, uint8_t cmd, uint8_t flags, uint32_t len); -size_t mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic, - uint8_t *qos, size_t pos); -size_t mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic, - size_t pos); void mg_mqtt_ping(struct mg_connection *); void mg_mqtt_pong(struct mg_connection *); void mg_mqtt_disconnect(struct mg_connection *); diff --git a/src/net.c b/src/net.c index 6f7e95da..5be5b9e9 100644 --- a/src/net.c +++ b/src/net.c @@ -157,7 +157,7 @@ void mg_close_conn(struct mg_connection *c) { // 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 closed", c->id)); + MG_DEBUG(("%lu %p closed", c->id, c->fd)); mg_tls_free(c); mg_iobuf_free(&c->recv); diff --git a/src/sock.c b/src/sock.c index 925d142c..84589828 100644 --- a/src/sock.c +++ b/src/sock.c @@ -322,7 +322,6 @@ static void close_conn(struct mg_connection *c) { #if MG_ARCH == MG_ARCH_FREERTOS_TCP FreeRTOS_FD_CLR(c->fd, c->mgr->ss, eSELECT_ALL); #endif - c->fd = NULL; } mg_close_conn(c); } @@ -422,10 +421,9 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { MG_ERROR(("%lu OOM", lsn->id)); closesocket(fd); } else { - char buf[40]; + // char buf[40]; tomgaddr(&usa, &c->rem, sa_len != sizeof(usa.sin)); - mg_straddr(&c->rem, buf, sizeof(buf)); - MG_DEBUG(("%lu accepted %s", c->id, buf)); + // mg_straddr(&c->rem, buf, sizeof(buf)); LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); c->fd = S2PTR(fd); MG_EPOLL_ADD(c); @@ -438,6 +436,9 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { c->pfn_data = lsn->pfn_data; c->fn = lsn->fn; c->fn_data = lsn->fn_data; + MG_DEBUG(("%lu %p accepted %x.%hu -> %x.%hu", c->id, c->fd, + mg_ntohl(c->rem.ip), mg_ntohs(c->rem.port), mg_ntohl(c->loc.ip), + mg_ntohs(c->loc.port))); mg_call(c, MG_EV_OPEN, NULL); mg_call(c, MG_EV_ACCEPT, NULL); } diff --git a/src/ws.c b/src/ws.c index cb5807b4..9abb8c1c 100644 --- a/src/ws.c +++ b/src/ws.c @@ -272,6 +272,7 @@ void mg_ws_upgrade(struct mg_connection *c, struct mg_http_message *hm, ws_handshake(c, wskey, wsproto, fmt, &ap); va_end(ap); c->is_websocket = 1; + c->is_resp = 0; mg_call(c, MG_EV_WS_OPEN, hm); } }