From 7e927b43ad48b471a58b50b4be322d679ae93e13 Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Tue, 23 Feb 2016 12:43:13 +0100 Subject: [PATCH] Initial support for SSL in TCPUART 2 PUBLISHED_FROM=93cb3cf49ce9173a7361af17c1bd47858bb2602e --- mongoose.c | 26 ++++++++++++++------------ mongoose.h | 15 ++++++++++----- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/mongoose.c b/mongoose.c index 8266693c..55c52592 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2560,12 +2560,9 @@ const char *mg_set_ssl(struct mg_connection *nc, const char *cert, } #endif /* MG_ENABLE_SSL */ -struct mg_connection *mg_if_accept_tcp_cb(struct mg_connection *lc, - union socket_address *sa, - size_t sa_len) { +struct mg_connection *mg_if_accept_new_conn(struct mg_connection *lc) { struct mg_add_sock_opts opts; struct mg_connection *nc; - (void) sa_len; memset(&opts, 0, sizeof(opts)); nc = mg_create_connection(lc->mgr, lc->handler, opts); if (nc == NULL) return NULL; @@ -2574,17 +2571,19 @@ struct mg_connection *mg_if_accept_tcp_cb(struct mg_connection *lc, nc->proto_handler = lc->proto_handler; nc->user_data = lc->user_data; nc->recv_mbuf_limit = lc->recv_mbuf_limit; - nc->sa = *sa; mg_add_conn(nc->mgr, nc); - if (lc->ssl_ctx == NULL) { - /* For non-SSL connections deliver MG_EV_ACCEPT right away. */ - mg_call(nc, NULL, MG_EV_ACCEPT, &nc->sa); - } DBG(("%p %p %d %d, %p %p", lc, nc, nc->sock, (int) nc->flags, lc->ssl_ctx, nc->ssl)); return nc; } +void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa, + size_t sa_len) { + (void) sa_len; + nc->sa = *sa; + mg_call(nc, NULL, MG_EV_ACCEPT, &nc->sa); +} + void mg_send(struct mg_connection *nc, const void *buf, int len) { nc->last_io_time = mg_time(); if (nc->flags & MG_F_UDP) { @@ -3102,7 +3101,7 @@ static void mg_accept_conn(struct mg_connection *lc) { DBG(("%p: failed to accept: %d", lc, errno)); return; } - nc = mg_if_accept_tcp_cb(lc, &sa, sa_len); + nc = mg_if_accept_new_conn(lc); if (nc == NULL) { closesocket(sock); return; @@ -3115,8 +3114,11 @@ static void mg_accept_conn(struct mg_connection *lc) { DBG(("SSL error")); mg_close_conn(nc); } - } + } else #endif + { + mg_if_accept_tcp_cb(nc, &sa, sa_len); + } } /* 'sa' must be an initialized address to bind to */ @@ -3331,7 +3333,7 @@ static void mg_ssl_begin(struct mg_connection *nc) { socklen_t sa_len = sizeof(sa); /* In case port was set to 0, get the real port number */ (void) getsockname(nc->sock, &sa.sa, &sa_len); - mg_call(nc, NULL, MG_EV_ACCEPT, &sa); + mg_if_accept_tcp_cb(nc, &sa, sa_len); } else { mg_if_connect_cb(nc, 0); } diff --git a/mongoose.h b/mongoose.h index a7df261f..928b49e5 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1454,11 +1454,16 @@ void mg_if_connect_cb(struct mg_connection *nc, int err); /* Set up a listening TCP socket on a given address. rv = 0 -> ok. */ int mg_if_listen_tcp(struct mg_connection *nc, union socket_address *sa); -/* Deliver a new TCP connection. Returns NULL in case on error (unable to - * create connection, in which case interface state should be discarded. */ -struct mg_connection *mg_if_accept_tcp_cb(struct mg_connection *lc, - union socket_address *sa, - size_t sa_len); + +/* + * Deliver a new TCP connection. Returns NULL in case on error (unable to + * create connection, in which case interface state should be discarded. + * This is phase 1 of the two-phase process - MG_EV_ACCEPT will be delivered + * when mg_if_accept_tcp_cb is invoked. + */ +struct mg_connection *mg_if_accept_new_conn(struct mg_connection *lc); +void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa, + size_t sa_len); /* Request that a "listening" UDP socket be created. */ int mg_if_listen_udp(struct mg_connection *nc, union socket_address *sa);