Merge pull request #3149 from cesanta/is_tls

set is_tls at mg_listen and mg_connect level
This commit is contained in:
Sergio R. Caprile 2025-05-26 10:06:38 -03:00 committed by GitHub
commit 6b6acd3967
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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->fn = fn;
c->is_client = true; c->is_client = true;
c->fn_data = fn_data; 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_call(c, MG_EV_OPEN, (void *) url);
MG_DEBUG(("%lu %ld %s", c->id, c->fd, url));
mg_resolve(c, url); mg_resolve(c, url);
} }
return c; 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); LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
c->fn = fn; c->fn = fn;
c->fn_data = fn_data; c->fn_data = fn_data;
c->is_tls = (mg_url_is_ssl(url) != 0);
mg_call(c, MG_EV_OPEN, NULL); 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)); MG_DEBUG(("%lu %ld %s", c->id, c->fd, url));
} }
return c; 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_SYN ? MIP_TCP_SYN_MS
: type == MIP_TTYPE_FIN ? MIP_TCP_FIN_MS : type == MIP_TTYPE_FIN ? MIP_TCP_FIN_MS
: MIP_TCP_KEEPALIVE_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->timer = ifp->now + n;
s->ttype = type; s->ttype = type;
MG_VERBOSE(("%lu %d -> %llx", c->id, type, s->timer)); 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; return NULL;
} }
struct connstate *s = (struct connstate *) (c + 1); 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); s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq);
memcpy(s->mac, pkt->eth->src, sizeof(s->mac)); memcpy(s->mac, pkt->eth->src, sizeof(s->mac));
settmout(c, MIP_TTYPE_KEEPALIVE); 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->pfn_data = lsn->pfn_data;
c->fn = lsn->fn; c->fn = lsn->fn;
c->fn_data = lsn->fn_data; c->fn_data = lsn->fn_data;
c->is_tls = lsn->is_tls;
mg_call(c, MG_EV_OPEN, NULL); mg_call(c, MG_EV_OPEN, NULL);
mg_call(c, MG_EV_ACCEPT, 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; return c;
} }
@ -4949,6 +4952,7 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
settmout(c, MIP_TTYPE_KEEPALIVE); settmout(c, MIP_TTYPE_KEEPALIVE);
mg_call(c, MG_EV_CONNECT, NULL); // Let user know mg_call(c, MG_EV_CONNECT, NULL); // Let user know
if (c->is_tls_hs) mg_tls_handshake(c); 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) { } else if (c != NULL && c->is_connecting && pkt->tcp->flags != TH_ACK) {
// mg_hexdump(pkt->raw.buf, pkt->raw.len); // mg_hexdump(pkt->raw.buf, pkt->raw.len);
tx_tcp_pkt(ifp, pkt, TH_RST | TH_ACK, pkt->tcp->ack, NULL, 0); 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) { for (c = mgr->conns; c != NULL; c = tmp) {
tmp = c->next; tmp = c->next;
struct connstate *s = (struct connstate *) (c + 1); 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_call(c, MG_EV_POLL, &now);
MG_VERBOSE(("%lu .. %c%c%c%c%c %lu %lu", c->id, c->is_tls ? 'T' : 't', 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_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h',
c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c', c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c',
mg_tls_pending(c), c->rtls.len)); mg_tls_pending(c), c->rtls.len));
// order is important, TLS conn close with > 1 record in buffer (below) // 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); handle_tls_recv(c);
if (can_write(c)) write_conn(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) if (c->is_draining && c->send.len == 0 && s->ttype != MIP_TTYPE_FIN)
init_closure(c); init_closure(c);
// For non-TLS, close immediately upon completing the 3-way closure // 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_call(c, MG_EV_CONNECT, NULL);
MG_EPOLL_MOD(c, 0); MG_EPOLL_MOD(c, 0);
if (c->is_tls_hs) mg_tls_handshake(c); 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 { } else {
mg_error(c, "socket error"); mg_error(c, "socket error");
} }
@ -9021,6 +9028,7 @@ void mg_connect_resolved(struct mg_connection *c) {
if (rc == 0) { // Success if (rc == 0) { // Success
setlocaddr(FD(c), &c->loc); setlocaddr(FD(c), &c->loc);
mg_call(c, MG_EV_CONNECT, NULL); // Send MG_EV_CONNECT to the user 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 } 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)); MG_DEBUG(("%lu %ld -> %M pend", c->id, c->fd, mg_print_ip_port, &c->rem));
c->is_connecting = 1; 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->pfn_data = lsn->pfn_data;
c->fn = lsn->fn; c->fn = lsn->fn;
c->fn_data = lsn->fn_data; 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, MG_DEBUG(("%lu %ld accepted %M -> %M", c->id, c->fd, mg_print_ip_port,
&c->rem, mg_print_ip_port, &c->loc)); &c->rem, mg_print_ip_port, &c->loc));
mg_call(c, MG_EV_OPEN, NULL); mg_call(c, MG_EV_OPEN, NULL);
mg_call(c, MG_EV_ACCEPT, 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->fn = fn;
c->is_client = true; c->is_client = true;
c->fn_data = fn_data; 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_call(c, MG_EV_OPEN, (void *) url);
MG_DEBUG(("%lu %ld %s", c->id, c->fd, url));
mg_resolve(c, url); mg_resolve(c, url);
} }
return c; 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); LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
c->fn = fn; c->fn = fn;
c->fn_data = fn_data; c->fn_data = fn_data;
c->is_tls = (mg_url_is_ssl(url) != 0);
mg_call(c, MG_EV_OPEN, NULL); 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)); MG_DEBUG(("%lu %ld %s", c->id, c->fd, url));
} }
return c; return c;

View File

@ -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_SYN ? MIP_TCP_SYN_MS
: type == MIP_TTYPE_FIN ? MIP_TCP_FIN_MS : type == MIP_TTYPE_FIN ? MIP_TCP_FIN_MS
: MIP_TCP_KEEPALIVE_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->timer = ifp->now + n;
s->ttype = type; s->ttype = type;
MG_VERBOSE(("%lu %d -> %llx", c->id, type, s->timer)); 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; return NULL;
} }
struct connstate *s = (struct connstate *) (c + 1); 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); s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq);
memcpy(s->mac, pkt->eth->src, sizeof(s->mac)); memcpy(s->mac, pkt->eth->src, sizeof(s->mac));
settmout(c, MIP_TTYPE_KEEPALIVE); 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->pfn_data = lsn->pfn_data;
c->fn = lsn->fn; c->fn = lsn->fn;
c->fn_data = lsn->fn_data; c->fn_data = lsn->fn_data;
c->is_tls = lsn->is_tls;
mg_call(c, MG_EV_OPEN, NULL); mg_call(c, MG_EV_OPEN, NULL);
mg_call(c, MG_EV_ACCEPT, 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; return c;
} }
@ -784,6 +786,7 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
settmout(c, MIP_TTYPE_KEEPALIVE); settmout(c, MIP_TTYPE_KEEPALIVE);
mg_call(c, MG_EV_CONNECT, NULL); // Let user know mg_call(c, MG_EV_CONNECT, NULL); // Let user know
if (c->is_tls_hs) mg_tls_handshake(c); 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) { } else if (c != NULL && c->is_connecting && pkt->tcp->flags != TH_ACK) {
// mg_hexdump(pkt->raw.buf, pkt->raw.len); // mg_hexdump(pkt->raw.buf, pkt->raw.len);
tx_tcp_pkt(ifp, pkt, TH_RST | TH_ACK, pkt->tcp->ack, NULL, 0); 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) { for (c = mgr->conns; c != NULL; c = tmp) {
tmp = c->next; tmp = c->next;
struct connstate *s = (struct connstate *) (c + 1); 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_call(c, MG_EV_POLL, &now);
MG_VERBOSE(("%lu .. %c%c%c%c%c %lu %lu", c->id, c->is_tls ? 'T' : 't', 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_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h',
c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c', c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c',
mg_tls_pending(c), c->rtls.len)); mg_tls_pending(c), c->rtls.len));
// order is important, TLS conn close with > 1 record in buffer (below) // 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); handle_tls_recv(c);
if (can_write(c)) write_conn(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) if (c->is_draining && c->send.len == 0 && s->ttype != MIP_TTYPE_FIN)
init_closure(c); init_closure(c);
// For non-TLS, close immediately upon completing the 3-way closure // 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_call(c, MG_EV_CONNECT, NULL);
MG_EPOLL_MOD(c, 0); MG_EPOLL_MOD(c, 0);
if (c->is_tls_hs) mg_tls_handshake(c); 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 { } else {
mg_error(c, "socket error"); mg_error(c, "socket error");
} }
@ -413,6 +414,7 @@ void mg_connect_resolved(struct mg_connection *c) {
if (rc == 0) { // Success if (rc == 0) { // Success
setlocaddr(FD(c), &c->loc); setlocaddr(FD(c), &c->loc);
mg_call(c, MG_EV_CONNECT, NULL); // Send MG_EV_CONNECT to the user 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 } 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)); MG_DEBUG(("%lu %ld -> %M pend", c->id, c->fd, mg_print_ip_port, &c->rem));
c->is_connecting = 1; 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->pfn_data = lsn->pfn_data;
c->fn = lsn->fn; c->fn = lsn->fn;
c->fn_data = lsn->fn_data; 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, MG_DEBUG(("%lu %ld accepted %M -> %M", c->id, c->fd, mg_print_ip_port,
&c->rem, mg_print_ip_port, &c->loc)); &c->rem, mg_print_ip_port, &c->loc));
mg_call(c, MG_EV_OPEN, NULL); mg_call(c, MG_EV_OPEN, NULL);
mg_call(c, MG_EV_ACCEPT, 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 // HTTP request handler function
static void fn(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) { if (ev == MG_EV_ACCEPT) {
if (c->fn_data != NULL) { // TLS listener! if (c->is_tls) { // TLS listener!
struct mg_tls_opts opts = {0}; struct mg_tls_opts opts = {0};
opts.cert = mg_unpacked("/certs/server_cert.pem"); opts.cert = mg_unpacked("/certs/server_cert.pem");
opts.key = mg_unpacked("/certs/server_key.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) { void web_init(struct mg_mgr *mgr) {
s_settings.device_name = strdup("My Device"); s_settings.device_name = strdup("My Device");
mg_http_listen(mgr, HTTP_URL, fn, NULL); 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, mg_timer_add(mgr, 3600 * 1000, MG_TIMER_RUN_NOW | MG_TIMER_REPEAT,
timer_sntp_fn, mgr); 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) { 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), struct mg_tls_opts opts = {.cert = mg_str(s_tls_cert),
.key = mg_str(s_tls_key)}; .key = mg_str(s_tls_key)};
mg_tls_init(c, &opts); 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 // Connected to server. Extract host name from URL
struct mg_str host = mg_url_host(s_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"), struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"),
.name = mg_url_host(s_url)}; .name = mg_url_host(s_url)};
mg_tls_init(c, &opts); 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 // Proxy TCP connection established. Send CONNECT request
struct mg_str host = mg_url_host(url); 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"), struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"),
.name = host}; .name = host};
mg_tls_init(c, &opts); 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 // We use the same event handler function for HTTP and HTTPS connections
// fn_data is NULL for plain HTTP, and non-NULL for HTTPS // fn_data is NULL for plain HTTP, and non-NULL for HTTPS
static void fn(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; struct mg_tls_opts opts;
memset(&opts, 0, sizeof(opts)); memset(&opts, 0, sizeof(opts));
#ifdef TLS_TWOWAY #ifdef TLS_TWOWAY
@ -91,12 +91,12 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
} }
int main(void) { int main(void) {
struct mg_mgr mgr; // Event manager struct mg_mgr mgr; // Event manager
mg_log_set(MG_LL_DEBUG); // Set log level mg_log_set(MG_LL_DEBUG); // Set log level
mg_mgr_init(&mgr); // Initialise event manager mg_mgr_init(&mgr); // Initialise event manager
mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener 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 for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
mg_mgr_free(&mgr); mg_mgr_free(&mgr);
return 0; return 0;
} }

View File

@ -58,7 +58,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (c2 == NULL) { if (c2 == NULL) {
mg_error(c, "Cannot create backend connection"); mg_error(c, "Cannot create backend connection");
} else { } else {
if (mg_url_is_ssl(s_backend_url)) { if (c->is_tls) {
struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"), struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"),
.name = mg_url_host(s_backend_url)}; .name = mg_url_host(s_backend_url)};
mg_tls_init(c2, &opts); 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 // 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; struct mg_tls_opts opts;
memset(&opts, 0, sizeof(opts)); memset(&opts, 0, sizeof(opts));
@ -102,7 +102,7 @@ void setup() {
// Setup HTTP & HTTPS listeners. Respond "ok" on any HTTP request // 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, "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() { void loop() {

View File

@ -52,7 +52,7 @@ static void signal_handler(int signo) {
// Event handler for the listening connection. // Event handler for the listening connection.
// Simply serve static files from `s_root_dir` // Simply serve static files from `s_root_dir`
static void cb(struct mg_connection *c, int ev, void *ev_data) { 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; struct mg_tls_opts opts;
memset(&opts, 0, sizeof(opts)); memset(&opts, 0, sizeof(opts));
#ifdef TLS_TWOWAY #ifdef TLS_TWOWAY
@ -164,8 +164,8 @@ int main(int argc, char *argv[]) {
s_addr1)); s_addr1));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if ((c = mg_http_listen(&mgr, s_addr2, cb, (void *) 1)) == NULL) { if ((c = mg_http_listen(&mgr, s_addr2, cb, NULL)) == NULL) {
MG_ERROR(("Cannot listen on %s. Use http://ADDR:PORT or :PORT", MG_ERROR(("Cannot listen on %s. Use https://ADDR:PORT",
s_addr2)); s_addr2));
exit(EXIT_FAILURE); 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) { if (ev == MG_EV_CONNECT) {
// Connected to server. Extract host name from URL // Connected to server. Extract host name from URL
struct mg_str host = mg_url_host(s_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"), struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"),
.name = host}; .name = host};
mg_tls_init(c, &opts); mg_tls_init(c, &opts);

View File

@ -276,7 +276,7 @@ static void handle_dhcp_get(struct mg_connection *c) {
// HTTP request handler function // HTTP request handler function
static void fn(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) { if (ev == MG_EV_ACCEPT) {
if (c->fn_data != NULL) { // TLS if (c->is_tls) {
struct mg_tls_opts opts = {0}; struct mg_tls_opts opts = {0};
opts.cert = mg_str(s_tls_cert); opts.cert = mg_str(s_tls_cert);
opts.key = mg_str(s_tls_key); 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) { void web_init(struct mg_mgr *mgr) {
mg_http_listen(mgr, HTTP_URL, fn, NULL); 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(c->mgr, 1000, MG_TIMER_REPEAT, timer_mqtt_fn, c->mgr);
mg_timer_add(mgr, 3600 * 1000, MG_TIMER_RUN_NOW | MG_TIMER_REPEAT, 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) { if (ev == MG_EV_OPEN) {
// c->is_hexdumping = 1; // c->is_hexdumping = 1;
} else if (ev == MG_EV_CONNECT) { } 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"), struct mg_tls_opts opts = {.ca = mg_unpacked("/ca.pem"),
.cert = mg_unpacked("/crt.pem"), .cert = mg_unpacked("/crt.pem"),
.key = mg_unpacked("/key.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) { 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 = {}; struct mg_tls_opts opts = {};
opts.ca = mg_str(TLS_CA); opts.ca = mg_str(TLS_CA);
mg_tls_init(c, &opts); 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)); MG_INFO(("%lu CREATED", c->id));
// c->is_hexdumping = 1; // c->is_hexdumping = 1;
} else if (ev == MG_EV_CONNECT) { } 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"), struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"),
.name = mg_url_host(s_url)}; .name = mg_url_host(s_url)};
mg_tls_init(c, &opts); 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 // On error, log error message
MG_ERROR(("%p %s", c->fd, (char *) ev_data)); MG_ERROR(("%p %s", c->fd, (char *) ev_data));
} else if (ev == MG_EV_CONNECT) { } 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"), struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"),
.name = mg_url_host(s_url)}; .name = mg_url_host(s_url)};
mg_tls_init(c, &opts); 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) { static void fn(struct mg_connection *c, int ev, void *ev_data) {
struct conndata *cd = (struct conndata *) c->data; struct conndata *cd = (struct conndata *) c->data;
if (ev == MG_EV_ACCEPT) { if (ev == MG_EV_ACCEPT) {
if (c->fn_data != NULL) { // TLS listener! if (c->is_tls) { // TLS listener!
struct mg_tls_opts opts = {0}; struct mg_tls_opts opts = {0};
opts.cert = mg_unpacked("/certs/server_cert.pem"); opts.cert = mg_unpacked("/certs/server_cert.pem");
opts.key = mg_unpacked("/certs/server_key.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) { 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), struct mg_tls_opts opts = {.cert = mg_str(s_tls_cert),
.key = mg_str(s_tls_key)}; .key = mg_str(s_tls_key)};
mg_tls_init(c, &opts); 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) { if (ev == MG_EV_OPEN) {
MG_INFO(("%lu CREATED", c->id)); MG_INFO(("%lu CREATED", c->id));
// c->is_hexdumping = 1; // 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), struct mg_tls_opts opts = {.ca = mg_str(s_ca_cert),
.name = mg_url_host(MQTTS_URL)}; .name = mg_url_host(MQTTS_URL)};
mg_tls_init(c, &opts); mg_tls_init(c, &opts);
@ -292,7 +292,7 @@ int main(int argc, char *argv[]) {
MG_INFO(("Init done, starting main loop")); 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, "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) { while (s_signo == 0) {
mg_mgr_poll(&mgr, 100); 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) { static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) { if (ev == MG_EV_OPEN) {
c->is_hexdumping = 1; 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_str ca = mg_file_read(&mg_fs_posix, s_ca_path);
struct mg_tls_opts opts = {.ca = ca, .name = mg_url_host(s_url)}; struct mg_tls_opts opts = {.ca = ca, .name = mg_url_host(s_url)};
mg_tls_init(c, &opts); 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) { static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) { if (ev == MG_EV_OPEN) {
// c->is_hexdumping = 1; // 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 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 cert = mg_file_read(&mg_fs_posix, s_cert_path);
struct mg_str key = mg_file_read(&mg_fs_posix, s_key_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 // Connected to server. Extract host name from URL
struct mg_str host = mg_url_host(s_url); struct mg_str host = mg_url_host(s_url);
// If s_url is https://, tell client connection to use TLS // If s_url is https://, init TLS client connection
if (mg_url_is_ssl(s_url)) { if (c->is_tls) {
struct mg_tls_opts opts = {.ca = s_ca, .name = host}; struct mg_tls_opts opts = {.ca = s_ca, .name = host};
mg_tls_init(c, &opts); 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. // Event handler for the listening HTTP/HTTPS connection.
static void wcb(struct mg_connection *c, int ev, void *ev_data) { 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}; struct mg_tls_opts opts = {.cert = (char *) s_ssl_cert, .key = (char *) s_ssl_key};
mg_tls_init(c, &opts); mg_tls_init(c, &opts);
} else if (ev == MG_EV_HTTP_MSG) { } else if (ev == MG_EV_HTTP_MSG) {
@ -89,7 +89,7 @@ int main(int argc, char *argv[]) {
mg_mgr_init(&mgr); mg_mgr_init(&mgr);
mg_log_set(MG_LL_DEBUG); mg_log_set(MG_LL_DEBUG);
mg_http_listen(&mgr, s_http_addr, wcb, NULL); 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); 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) { static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) { if (ev == MG_EV_OPEN) {
c->is_hexdumping = 1; 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}; struct mg_tls_opts opts = {.cert = (char *) s_ssl_cert, .key = (char *) s_ssl_key};
mg_tls_init(c, &opts); mg_tls_init(c, &opts);
} else if (ev == MG_EV_HTTP_MSG) { } else if (ev == MG_EV_HTTP_MSG) {
@ -85,7 +85,7 @@ int main(int argc, char *argv[]) {
mg_mgr_init(&mgr); mg_mgr_init(&mgr);
mg_log_set(MG_LL_DEBUG); mg_log_set(MG_LL_DEBUG);
mg_http_listen(&mgr, s_ws_addr, fn, NULL); 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); mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr);
// Start infinite event loop // Start infinite event loop