diff --git a/mongoose.c b/mongoose.c index bef564f7..6bd6e9d6 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4050,8 +4050,9 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, c->fn = fn; c->is_client = true; c->fn_data = fn_data; - MG_DEBUG(("%lu %ld %s", c->id, c->fd, url)); + c->is_tls = (mg_url_is_ssl(url) != 0); mg_call(c, MG_EV_OPEN, (void *) url); + MG_DEBUG(("%lu %ld %s", c->id, c->fd, url)); mg_resolve(c, url); } return c; @@ -4073,8 +4074,8 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url, LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); c->fn = fn; c->fn_data = fn_data; + c->is_tls = (mg_url_is_ssl(url) != 0); 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; @@ -4342,7 +4343,7 @@ static void settmout(struct mg_connection *c, uint8_t type) { : type == MIP_TTYPE_SYN ? MIP_TCP_SYN_MS : type == MIP_TTYPE_FIN ? MIP_TCP_FIN_MS : MIP_TCP_KEEPALIVE_MS; - if (s->ttype == MIP_TTYPE_FIN) return; // skip if 3-way closing + if (s->ttype == MIP_TTYPE_FIN) return; // skip if 3-way closing s->timer = ifp->now + n; s->ttype = type; MG_VERBOSE(("%lu %d -> %llx", c->id, type, s->timer)); @@ -4745,7 +4746,7 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn, return NULL; } struct connstate *s = (struct connstate *) (c + 1); - s->dmss = 536; // assume default, RFC-9293 3.7.1 + s->dmss = 536; // assume default, RFC-9293 3.7.1 s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq); memcpy(s->mac, pkt->eth->src, sizeof(s->mac)); settmout(c, MIP_TTYPE_KEEPALIVE); @@ -4760,8 +4761,10 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn, c->pfn_data = lsn->pfn_data; c->fn = lsn->fn; c->fn_data = lsn->fn_data; + c->is_tls = lsn->is_tls; mg_call(c, MG_EV_OPEN, NULL); mg_call(c, MG_EV_ACCEPT, NULL); + if (!c->is_tls_hs) c->is_tls = 0; // user did not call mg_tls_init() return c; } @@ -4949,6 +4952,7 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) { settmout(c, MIP_TTYPE_KEEPALIVE); mg_call(c, MG_EV_CONNECT, NULL); // Let user know if (c->is_tls_hs) mg_tls_handshake(c); + if (!c->is_tls_hs) c->is_tls = 0; // user did not call mg_tls_init() } else if (c != NULL && c->is_connecting && pkt->tcp->flags != TH_ACK) { // mg_hexdump(pkt->raw.buf, pkt->raw.len); tx_tcp_pkt(ifp, pkt, TH_RST | TH_ACK, pkt->tcp->ack, NULL, 0); @@ -5374,16 +5378,18 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) { for (c = mgr->conns; c != NULL; c = tmp) { tmp = c->next; struct connstate *s = (struct connstate *) (c + 1); + bool is_tls = !c->is_resolving && !c->is_arplooking && !c->is_listening && + !c->is_connecting; mg_call(c, MG_EV_POLL, &now); MG_VERBOSE(("%lu .. %c%c%c%c%c %lu %lu", c->id, c->is_tls ? 'T' : 't', c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h', c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c', mg_tls_pending(c), c->rtls.len)); // order is important, TLS conn close with > 1 record in buffer (below) - if (c->is_tls && (c->rtls.len > 0 || mg_tls_pending(c) > 0)) + if (is_tls && (c->rtls.len > 0 || mg_tls_pending(c) > 0)) handle_tls_recv(c); if (can_write(c)) write_conn(c); - if (!c->is_listening && c->is_tls && !c->is_tls_hs && c->send.len == 0) mg_tls_flush(c); + if (is_tls && c->send.len == 0) mg_tls_flush(c); if (c->is_draining && c->send.len == 0 && s->ttype != MIP_TTYPE_FIN) init_closure(c); // For non-TLS, close immediately upon completing the 3-way closure @@ -8969,6 +8975,7 @@ static void connect_conn(struct mg_connection *c) { mg_call(c, MG_EV_CONNECT, NULL); MG_EPOLL_MOD(c, 0); if (c->is_tls_hs) mg_tls_handshake(c); + if (!c->is_tls_hs) c->is_tls = 0; // user did not call mg_tls_init() } else { mg_error(c, "socket error"); } @@ -9021,6 +9028,7 @@ void mg_connect_resolved(struct mg_connection *c) { if (rc == 0) { // Success setlocaddr(FD(c), &c->loc); mg_call(c, MG_EV_CONNECT, NULL); // Send MG_EV_CONNECT to the user + if (!c->is_tls_hs) c->is_tls = 0; // user did not call mg_tls_init() } else if (MG_SOCK_PENDING(rc)) { // Need to wait for TCP handshake MG_DEBUG(("%lu %ld -> %M pend", c->id, c->fd, mg_print_ip_port, &c->rem)); c->is_connecting = 1; @@ -9076,10 +9084,12 @@ 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; + c->is_tls = lsn->is_tls; MG_DEBUG(("%lu %ld accepted %M -> %M", c->id, c->fd, mg_print_ip_port, &c->rem, mg_print_ip_port, &c->loc)); mg_call(c, MG_EV_OPEN, NULL); mg_call(c, MG_EV_ACCEPT, NULL); + if (!c->is_tls_hs) c->is_tls = 0; // user did not call mg_tls_init() } } diff --git a/src/net.c b/src/net.c index 37659834..ce39134d 100644 --- a/src/net.c +++ b/src/net.c @@ -170,8 +170,9 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, c->fn = fn; c->is_client = true; c->fn_data = fn_data; - MG_DEBUG(("%lu %ld %s", c->id, c->fd, url)); + c->is_tls = (mg_url_is_ssl(url) != 0); mg_call(c, MG_EV_OPEN, (void *) url); + MG_DEBUG(("%lu %ld %s", c->id, c->fd, url)); mg_resolve(c, url); } return c; @@ -193,8 +194,8 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url, LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); c->fn = fn; c->fn_data = fn_data; + c->is_tls = (mg_url_is_ssl(url) != 0); 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; diff --git a/src/net_builtin.c b/src/net_builtin.c index 4ec0d0bb..9b99118c 100644 --- a/src/net_builtin.c +++ b/src/net_builtin.c @@ -177,7 +177,7 @@ static void settmout(struct mg_connection *c, uint8_t type) { : type == MIP_TTYPE_SYN ? MIP_TCP_SYN_MS : type == MIP_TTYPE_FIN ? MIP_TCP_FIN_MS : MIP_TCP_KEEPALIVE_MS; - if (s->ttype == MIP_TTYPE_FIN) return; // skip if 3-way closing + if (s->ttype == MIP_TTYPE_FIN) return; // skip if 3-way closing s->timer = ifp->now + n; s->ttype = type; MG_VERBOSE(("%lu %d -> %llx", c->id, type, s->timer)); @@ -580,7 +580,7 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn, return NULL; } struct connstate *s = (struct connstate *) (c + 1); - s->dmss = 536; // assume default, RFC-9293 3.7.1 + s->dmss = 536; // assume default, RFC-9293 3.7.1 s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq); memcpy(s->mac, pkt->eth->src, sizeof(s->mac)); settmout(c, MIP_TTYPE_KEEPALIVE); @@ -595,8 +595,10 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn, c->pfn_data = lsn->pfn_data; c->fn = lsn->fn; c->fn_data = lsn->fn_data; + c->is_tls = lsn->is_tls; mg_call(c, MG_EV_OPEN, NULL); mg_call(c, MG_EV_ACCEPT, NULL); + if (!c->is_tls_hs) c->is_tls = 0; // user did not call mg_tls_init() return c; } @@ -784,6 +786,7 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) { settmout(c, MIP_TTYPE_KEEPALIVE); mg_call(c, MG_EV_CONNECT, NULL); // Let user know if (c->is_tls_hs) mg_tls_handshake(c); + if (!c->is_tls_hs) c->is_tls = 0; // user did not call mg_tls_init() } else if (c != NULL && c->is_connecting && pkt->tcp->flags != TH_ACK) { // mg_hexdump(pkt->raw.buf, pkt->raw.len); tx_tcp_pkt(ifp, pkt, TH_RST | TH_ACK, pkt->tcp->ack, NULL, 0); @@ -1209,16 +1212,18 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) { for (c = mgr->conns; c != NULL; c = tmp) { tmp = c->next; struct connstate *s = (struct connstate *) (c + 1); + bool is_tls = !c->is_resolving && !c->is_arplooking && !c->is_listening && + !c->is_connecting; mg_call(c, MG_EV_POLL, &now); MG_VERBOSE(("%lu .. %c%c%c%c%c %lu %lu", c->id, c->is_tls ? 'T' : 't', c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h', c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c', mg_tls_pending(c), c->rtls.len)); // order is important, TLS conn close with > 1 record in buffer (below) - if (c->is_tls && (c->rtls.len > 0 || mg_tls_pending(c) > 0)) + if (is_tls && (c->rtls.len > 0 || mg_tls_pending(c) > 0)) handle_tls_recv(c); if (can_write(c)) write_conn(c); - if (!c->is_listening && c->is_tls && !c->is_tls_hs && c->send.len == 0) mg_tls_flush(c); + if (is_tls && c->send.len == 0) mg_tls_flush(c); if (c->is_draining && c->send.len == 0 && s->ttype != MIP_TTYPE_FIN) init_closure(c); // For non-TLS, close immediately upon completing the 3-way closure diff --git a/src/sock.c b/src/sock.c index 47859ea3..e29e4d5e 100644 --- a/src/sock.c +++ b/src/sock.c @@ -361,6 +361,7 @@ static void connect_conn(struct mg_connection *c) { mg_call(c, MG_EV_CONNECT, NULL); MG_EPOLL_MOD(c, 0); if (c->is_tls_hs) mg_tls_handshake(c); + if (!c->is_tls_hs) c->is_tls = 0; // user did not call mg_tls_init() } else { mg_error(c, "socket error"); } @@ -413,6 +414,7 @@ void mg_connect_resolved(struct mg_connection *c) { if (rc == 0) { // Success setlocaddr(FD(c), &c->loc); mg_call(c, MG_EV_CONNECT, NULL); // Send MG_EV_CONNECT to the user + if (!c->is_tls_hs) c->is_tls = 0; // user did not call mg_tls_init() } else if (MG_SOCK_PENDING(rc)) { // Need to wait for TCP handshake MG_DEBUG(("%lu %ld -> %M pend", c->id, c->fd, mg_print_ip_port, &c->rem)); c->is_connecting = 1; @@ -468,10 +470,12 @@ 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; + c->is_tls = lsn->is_tls; MG_DEBUG(("%lu %ld accepted %M -> %M", c->id, c->fd, mg_print_ip_port, &c->rem, mg_print_ip_port, &c->loc)); mg_call(c, MG_EV_OPEN, NULL); mg_call(c, MG_EV_ACCEPT, NULL); + if (!c->is_tls_hs) c->is_tls = 0; // user did not call mg_tls_init() } } diff --git a/tutorials/http/device-dashboard/net.c b/tutorials/http/device-dashboard/net.c index 707db792..d47daae7 100644 --- a/tutorials/http/device-dashboard/net.c +++ b/tutorials/http/device-dashboard/net.c @@ -205,7 +205,7 @@ static void handle_firmware_upload(struct mg_connection *c, // HTTP request handler function static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_ACCEPT) { - if (c->fn_data != NULL) { // TLS listener! + if (c->is_tls) { // TLS listener! struct mg_tls_opts opts = {0}; opts.cert = mg_unpacked("/certs/server_cert.pem"); opts.key = mg_unpacked("/certs/server_key.pem"); @@ -253,7 +253,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { void web_init(struct mg_mgr *mgr) { s_settings.device_name = strdup("My Device"); mg_http_listen(mgr, HTTP_URL, fn, NULL); - mg_http_listen(mgr, HTTPS_URL, fn, (void *) 1); + mg_http_listen(mgr, HTTPS_URL, fn, NULL); mg_timer_add(mgr, 3600 * 1000, MG_TIMER_RUN_NOW | MG_TIMER_REPEAT, timer_sntp_fn, mgr); } diff --git a/tutorials/http/file-upload-single-post/main.c b/tutorials/http/file-upload-single-post/main.c index 35854355..39fdd333 100644 --- a/tutorials/http/file-upload-single-post/main.c +++ b/tutorials/http/file-upload-single-post/main.c @@ -74,7 +74,7 @@ static void handle_uploads(struct mg_connection *c, int ev, void *ev_data) { } static void fn(struct mg_connection *c, int ev, void *ev_data) { - if (ev == MG_EV_ACCEPT && c->fn_data != NULL) { + if (ev == MG_EV_ACCEPT && c->is_tls) { struct mg_tls_opts opts = {.cert = mg_str(s_tls_cert), .key = mg_str(s_tls_key)}; mg_tls_init(c, &opts); diff --git a/tutorials/http/http-client/main.c b/tutorials/http/http-client/main.c index 8aee400b..23f9baf5 100644 --- a/tutorials/http/http-client/main.c +++ b/tutorials/http/http-client/main.c @@ -28,7 +28,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { // Connected to server. Extract host name from URL struct mg_str host = mg_url_host(s_url); - if (mg_url_is_ssl(s_url)) { + if (c->is_tls) { struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"), .name = mg_url_host(s_url)}; mg_tls_init(c, &opts); diff --git a/tutorials/http/http-proxy-client/main.c b/tutorials/http/http-proxy-client/main.c index cd1f0820..de546ade 100644 --- a/tutorials/http/http-proxy-client/main.c +++ b/tutorials/http/http-proxy-client/main.c @@ -21,7 +21,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { // Proxy TCP connection established. Send CONNECT request struct mg_str host = mg_url_host(url); - if (mg_url_is_ssl(url)) { + if (c->is_tls) { struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"), .name = host}; mg_tls_init(c, &opts); diff --git a/tutorials/http/http-restful-server/main.c b/tutorials/http/http-restful-server/main.c index 28c2548e..fd4dde4a 100644 --- a/tutorials/http/http-restful-server/main.c +++ b/tutorials/http/http-restful-server/main.c @@ -52,7 +52,7 @@ 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) { - if (ev == MG_EV_ACCEPT && c->fn_data != NULL) { + if (ev == MG_EV_ACCEPT && c->is_tls) { struct mg_tls_opts opts; memset(&opts, 0, sizeof(opts)); #ifdef TLS_TWOWAY @@ -91,12 +91,12 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { } int main(void) { - struct mg_mgr mgr; // Event manager - mg_log_set(MG_LL_DEBUG); // Set log level - mg_mgr_init(&mgr); // Initialise event manager - mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener - mg_http_listen(&mgr, s_https_addr, fn, (void *) 1); // HTTPS listener - for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop + struct mg_mgr mgr; // Event manager + mg_log_set(MG_LL_DEBUG); // Set log level + mg_mgr_init(&mgr); // Initialise event manager + mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener + mg_http_listen(&mgr, s_https_addr, fn, NULL); // HTTPS listener + for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop mg_mgr_free(&mgr); return 0; } diff --git a/tutorials/http/http-reverse-proxy/main.c b/tutorials/http/http-reverse-proxy/main.c index e6216a85..196b1c83 100644 --- a/tutorials/http/http-reverse-proxy/main.c +++ b/tutorials/http/http-reverse-proxy/main.c @@ -58,7 +58,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { if (c2 == NULL) { mg_error(c, "Cannot create backend connection"); } else { - if (mg_url_is_ssl(s_backend_url)) { + if (c->is_tls) { struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"), .name = mg_url_host(s_backend_url)}; mg_tls_init(c2, &opts); diff --git a/tutorials/http/http-server/arduino/w5500-http/w5500-http.ino b/tutorials/http/http-server/arduino/w5500-http/w5500-http.ino index f2171d9e..b9829f06 100644 --- a/tutorials/http/http-server/arduino/w5500-http/w5500-http.ino +++ b/tutorials/http/http-server/arduino/w5500-http/w5500-http.ino @@ -53,7 +53,7 @@ static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) { } } // Initialise TLS if we're a TLS listener - if (ev == MG_EV_ACCEPT && c->fn_data != NULL) { + if (c->is_tls && ev == MG_EV_ACCEPT) { struct mg_tls_opts opts; memset(&opts, 0, sizeof(opts)); @@ -102,7 +102,7 @@ void setup() { // Setup HTTP & HTTPS listeners. Respond "ok" on any HTTP request mg_http_listen(&mgr, "http://0.0.0.0:80", http_ev_handler, NULL); - mg_http_listen(&mgr, "https://0.0.0.0:443", http_ev_handler, (void *) 1); + mg_http_listen(&mgr, "https://0.0.0.0:443", http_ev_handler, NULL); } void loop() { diff --git a/tutorials/http/http-server/main.c b/tutorials/http/http-server/main.c index 8ad3fd9b..57fb8c8f 100644 --- a/tutorials/http/http-server/main.c +++ b/tutorials/http/http-server/main.c @@ -52,7 +52,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) { - if (ev == MG_EV_ACCEPT && c->fn_data != NULL) { + if (ev == MG_EV_ACCEPT && c->is_tls) { struct mg_tls_opts opts; memset(&opts, 0, sizeof(opts)); #ifdef TLS_TWOWAY @@ -164,8 +164,8 @@ int main(int argc, char *argv[]) { s_addr1)); exit(EXIT_FAILURE); } - if ((c = mg_http_listen(&mgr, s_addr2, cb, (void *) 1)) == NULL) { - MG_ERROR(("Cannot listen on %s. Use http://ADDR:PORT or :PORT", + if ((c = mg_http_listen(&mgr, s_addr2, cb, NULL)) == NULL) { + MG_ERROR(("Cannot listen on %s. Use https://ADDR:PORT", s_addr2)); exit(EXIT_FAILURE); } diff --git a/tutorials/http/http-streaming-client/main.c b/tutorials/http/http-streaming-client/main.c index 24e9dee6..31b6d8f6 100644 --- a/tutorials/http/http-streaming-client/main.c +++ b/tutorials/http/http-streaming-client/main.c @@ -19,7 +19,7 @@ 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); - if (mg_url_is_ssl(s_url)) { + if (c->is_tls) { struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"), .name = host}; mg_tls_init(c, &opts); diff --git a/tutorials/http/wifi-router-dashboard/net.c b/tutorials/http/wifi-router-dashboard/net.c index 76bdba9d..67fc344d 100644 --- a/tutorials/http/wifi-router-dashboard/net.c +++ b/tutorials/http/wifi-router-dashboard/net.c @@ -276,7 +276,7 @@ static void handle_dhcp_get(struct mg_connection *c) { // HTTP request handler function static void fn(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_ACCEPT) { - if (c->fn_data != NULL) { // TLS + if (c->is_tls) { struct mg_tls_opts opts = {0}; opts.cert = mg_str(s_tls_cert); opts.key = mg_str(s_tls_key); @@ -323,7 +323,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { void web_init(struct mg_mgr *mgr) { mg_http_listen(mgr, HTTP_URL, fn, NULL); - mg_http_listen(mgr, HTTPS_URL, fn, (void *) 1); + mg_http_listen(mgr, HTTPS_URL, fn, NULL); // mg_timer_add(c->mgr, 1000, MG_TIMER_REPEAT, timer_mqtt_fn, c->mgr); mg_timer_add(mgr, 3600 * 1000, MG_TIMER_RUN_NOW | MG_TIMER_REPEAT, diff --git a/tutorials/mqtt/mqtt-client-aws-iot/main.c b/tutorials/mqtt/mqtt-client-aws-iot/main.c index e8e24865..2b8ceee3 100644 --- a/tutorials/mqtt/mqtt-client-aws-iot/main.c +++ b/tutorials/mqtt/mqtt-client-aws-iot/main.c @@ -40,7 +40,7 @@ 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) { - if (mg_url_is_ssl(s_url)) { + if (c->is_tls) { struct mg_tls_opts opts = {.ca = mg_unpacked("/ca.pem"), .cert = mg_unpacked("/crt.pem"), .key = mg_unpacked("/key.pem"), diff --git a/tutorials/mqtt/mqtt-client/arduino/sim800-mqtt/sim800-mqtt.ino b/tutorials/mqtt/mqtt-client/arduino/sim800-mqtt/sim800-mqtt.ino index 1f959a0d..425a5b4e 100644 --- a/tutorials/mqtt/mqtt-client/arduino/sim800-mqtt/sim800-mqtt.ino +++ b/tutorials/mqtt/mqtt-client/arduino/sim800-mqtt/sim800-mqtt.ino @@ -59,7 +59,7 @@ void handle_command(struct mg_str msg) { } static void mqtt_ev_handler(struct mg_connection *c, int ev, void *ev_data) { - if (ev == MG_EV_CONNECT && mg_url_is_ssl(MQTT_SERVER)) { + if (c->is_tls && ev == MG_EV_CONNECT) { struct mg_tls_opts opts = {}; opts.ca = mg_str(TLS_CA); mg_tls_init(c, &opts); diff --git a/tutorials/mqtt/mqtt-client/main.c b/tutorials/mqtt/mqtt-client/main.c index 00796d72..f06cbe28 100644 --- a/tutorials/mqtt/mqtt-client/main.c +++ b/tutorials/mqtt/mqtt-client/main.c @@ -29,7 +29,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { MG_INFO(("%lu CREATED", c->id)); // c->is_hexdumping = 1; } else if (ev == MG_EV_CONNECT) { - if (mg_url_is_ssl(s_url)) { + if (c->is_tls) { struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"), .name = mg_url_host(s_url)}; mg_tls_init(c, &opts); diff --git a/tutorials/mqtt/mqtt-over-ws-client/main.c b/tutorials/mqtt/mqtt-over-ws-client/main.c index c324c13b..5d8e3a12 100644 --- a/tutorials/mqtt/mqtt-over-ws-client/main.c +++ b/tutorials/mqtt/mqtt-over-ws-client/main.c @@ -25,7 +25,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { // On error, log error message MG_ERROR(("%p %s", c->fd, (char *) ev_data)); } else if (ev == MG_EV_CONNECT) { - if (mg_url_is_ssl(s_url)) { + if (c->is_tls) { struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"), .name = mg_url_host(s_url)}; mg_tls_init(c, &opts); diff --git a/tutorials/tcp/modbus-dashboard/net.c b/tutorials/tcp/modbus-dashboard/net.c index 82dc27aa..a7130ad4 100644 --- a/tutorials/tcp/modbus-dashboard/net.c +++ b/tutorials/tcp/modbus-dashboard/net.c @@ -223,7 +223,7 @@ static size_t print_mb_resp(void (*out)(char, void *), void *ptr, va_list *ap) { 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 (c->fn_data != NULL) { // TLS listener! + if (c->is_tls) { // TLS listener! struct mg_tls_opts opts = {0}; opts.cert = mg_unpacked("/certs/server_cert.pem"); opts.key = mg_unpacked("/certs/server_key.pem"); diff --git a/tutorials/tcpip/pcap-driver/main.c b/tutorials/tcpip/pcap-driver/main.c index fefb67dd..100cc1e9 100644 --- a/tutorials/tcpip/pcap-driver/main.c +++ b/tutorials/tcpip/pcap-driver/main.c @@ -109,7 +109,7 @@ static void fn2(struct mg_connection *c, int ev, void *ev_data) { } static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) { - if (ev == MG_EV_ACCEPT && c->fn_data != NULL) { + if (ev == MG_EV_ACCEPT && c->is_tls) { struct mg_tls_opts opts = {.cert = mg_str(s_tls_cert), .key = mg_str(s_tls_key)}; mg_tls_init(c, &opts); @@ -146,7 +146,7 @@ static void mqtt_ev_handler(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_OPEN) { MG_INFO(("%lu CREATED", c->id)); // c->is_hexdumping = 1; - } else if (ev == MG_EV_CONNECT && c->fn_data != NULL) { + } else if (ev == MG_EV_CONNECT && c->is_tls) { struct mg_tls_opts opts = {.ca = mg_str(s_ca_cert), .name = mg_url_host(MQTTS_URL)}; mg_tls_init(c, &opts); @@ -292,7 +292,7 @@ int main(int argc, char *argv[]) { MG_INFO(("Init done, starting main loop")); mg_http_listen(&mgr, "http://0.0.0.0:8000", http_ev_handler, NULL); - mg_http_listen(&mgr, "https://0.0.0.0:8443", http_ev_handler, "tls enabled"); + mg_http_listen(&mgr, "https://0.0.0.0:8443", http_ev_handler, NULL); while (s_signo == 0) { mg_mgr_poll(&mgr, 100); diff --git a/tutorials/websocket/websocket-client/main.c b/tutorials/websocket/websocket-client/main.c index b049c14e..a0456012 100644 --- a/tutorials/websocket/websocket-client/main.c +++ b/tutorials/websocket/websocket-client/main.c @@ -14,7 +14,7 @@ static const char *s_ca_path = "ca.pem"; 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 && mg_url_is_ssl(s_url)) { + } else if (c->is_tls && ev == MG_EV_CONNECT) { struct mg_str ca = mg_file_read(&mg_fs_posix, s_ca_path); struct mg_tls_opts opts = {.ca = ca, .name = mg_url_host(s_url)}; mg_tls_init(c, &opts); diff --git a/tutorials/websocket/websocket-server/main.c b/tutorials/websocket/websocket-server/main.c index 22e88d27..0420ac68 100644 --- a/tutorials/websocket/websocket-server/main.c +++ b/tutorials/websocket/websocket-server/main.c @@ -18,7 +18,7 @@ static const char *s_key_path = "key.pem"; 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 && mg_url_is_ssl(s_listen_on)) { + } else if(c->is_tls && ev == MG_EV_ACCEPT) { struct mg_str ca = mg_file_read(&mg_fs_posix, s_ca_path); struct mg_str cert = mg_file_read(&mg_fs_posix, s_cert_path); struct mg_str key = mg_file_read(&mg_fs_posix, s_key_path); diff --git a/tutorials/zephyr/http-client/src/main.c b/tutorials/zephyr/http-client/src/main.c index 3bf27455..5d028b10 100644 --- a/tutorials/zephyr/http-client/src/main.c +++ b/tutorials/zephyr/http-client/src/main.c @@ -28,8 +28,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { // Connected to server. Extract host name from URL struct mg_str host = mg_url_host(s_url); - // If s_url is https://, tell client connection to use TLS - if (mg_url_is_ssl(s_url)) { + // If s_url is https://, init TLS client connection + if (c->is_tls) { struct mg_tls_opts opts = {.ca = s_ca, .name = host}; mg_tls_init(c, &opts); } diff --git a/tutorials/zephyr/http-server/src/main.c b/tutorials/zephyr/http-server/src/main.c index 4e562488..ec822133 100644 --- a/tutorials/zephyr/http-server/src/main.c +++ b/tutorials/zephyr/http-server/src/main.c @@ -13,7 +13,7 @@ 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) { - if (ev == MG_EV_ACCEPT && c->fn_data != NULL) { + if (ev == MG_EV_ACCEPT && c->is_tls) { struct mg_tls_opts opts = {.cert = (char *) s_ssl_cert, .key = (char *) s_ssl_key}; mg_tls_init(c, &opts); } else if (ev == MG_EV_HTTP_MSG) { @@ -89,7 +89,7 @@ int main(int argc, char *argv[]) { mg_mgr_init(&mgr); mg_log_set(MG_LL_DEBUG); mg_http_listen(&mgr, s_http_addr, wcb, NULL); - mg_http_listen(&mgr, s_https_addr, wcb, &mgr); + mg_http_listen(&mgr, s_https_addr, wcb, NULL); mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr); diff --git a/tutorials/zephyr/websocket-server/src/main.c b/tutorials/zephyr/websocket-server/src/main.c index 2c91742c..5c84b456 100644 --- a/tutorials/zephyr/websocket-server/src/main.c +++ b/tutorials/zephyr/websocket-server/src/main.c @@ -17,7 +17,7 @@ static struct mg_connection *s_sntp_conn = NULL; 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 && c->fn_data != NULL) { + } else if (ev == MG_EV_ACCEPT && c->is_tls) { struct mg_tls_opts opts = {.cert = (char *) s_ssl_cert, .key = (char *) s_ssl_key}; mg_tls_init(c, &opts); } else if (ev == MG_EV_HTTP_MSG) { @@ -85,7 +85,7 @@ int main(int argc, char *argv[]) { mg_mgr_init(&mgr); mg_log_set(MG_LL_DEBUG); mg_http_listen(&mgr, s_ws_addr, fn, NULL); - mg_http_listen(&mgr, s_wss_addr, fn, &mgr); + mg_http_listen(&mgr, s_wss_addr, fn, NULL); mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr); // Start infinite event loop