set is_tls at mg_listen and mg_connect level

This commit is contained in:
Sergio R. Caprile 2025-05-23 16:53:08 -03:00
parent 29594283c1
commit b740c80855
25 changed files with 69 additions and 49 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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
@ -95,7 +95,7 @@ int main(void) {
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
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;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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