diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-08-06 19:01:12 +0300 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-08-06 19:01:12 +0300 |
| commit | ae944a8de32c107eea9427104e153c25e4a681f1 (patch) | |
| tree | 7029f7668fe3e1a9899da57bf6c1e60e0394bacb /src/transport/tls | |
| parent | d7f7c896c0ede24249ef63b1e45b1878bf4bd473 (diff) | |
| download | nng-ae944a8de32c107eea9427104e153c25e4a681f1.tar.gz nng-ae944a8de32c107eea9427104e153c25e4a681f1.tar.bz2 nng-ae944a8de32c107eea9427104e153c25e4a681f1.zip | |
Revert "fixes #599 nng_dial sync should not return until added to socket"
This changeset needs work. We are seeing errors described by
This reverts commit d7f7c896c0ede24249ef63b1e45b1878bf4bd473.
Diffstat (limited to 'src/transport/tls')
| -rw-r--r-- | src/transport/tls/tls.c | 530 |
1 files changed, 272 insertions, 258 deletions
diff --git a/src/transport/tls/tls.c b/src/transport/tls/tls.c index 88e201b9..e6701f5f 100644 --- a/src/transport/tls/tls.c +++ b/src/transport/tls/tls.c @@ -29,23 +29,16 @@ typedef struct tlstran_pipe tlstran_pipe; // tlstran_pipe is one end of a TLS connection. struct tlstran_pipe { - nni_tls * tls; - uint16_t peer; - uint16_t proto; - size_t rcvmax; - bool nodelay; - bool keepalive; - nni_atomic_flag reaped; - nni_list_node node; // When pending, we are on ep list. - tlstran_ep * ep; - nni_tcp_dialer *dialer; // Client side. - nni_sockaddr sa; - nni_reap_item reap; - bool closed; + nni_tls *tls; + uint16_t peer; + uint16_t proto; + size_t rcvmax; + bool nodelay; + bool keepalive; nni_list sendq; nni_list recvq; - nni_aio *useraio; + nni_aio *user_negaio; uint8_t txlen[sizeof(uint64_t)]; uint8_t rxlen[sizeof(uint64_t)]; @@ -55,9 +48,7 @@ struct tlstran_pipe { size_t wantrxhead; nni_aio *txaio; nni_aio *rxaio; - nni_aio *negoaio; - nni_aio *connaio; - nni_aio *rslvaio; + nni_aio *negaio; nni_msg *rxmsg; nni_mtx mtx; }; @@ -68,25 +59,27 @@ struct tlstran_ep { size_t rcvmax; bool nodelay; bool keepalive; - bool fini; - nni_reap_item reap; - nni_cb dtor; int authmode; nng_tls_config *cfg; nni_url * url; nni_mtx mtx; - nni_list pipes; }; struct tlstran_dialer { tlstran_ep ep; // must be first nni_tcp_dialer *dialer; uint16_t af; + nni_aio * aio; + nni_aio * user_aio; + bool resolving; + nng_sockaddr sa; }; struct tlstran_listener { tlstran_ep ep; // must be first nni_tcp_listener *listener; + nni_aio * aio; + nni_aio * user_aio; nng_sockaddr sa; nng_sockaddr bsa; // bound addr }; @@ -95,10 +88,9 @@ static void tlstran_pipe_send_start(tlstran_pipe *); static void tlstran_pipe_recv_start(tlstran_pipe *); static void tlstran_pipe_send_cb(void *); static void tlstran_pipe_recv_cb(void *); -static void tlstran_pipe_rslv_cb(void *); -static void tlstran_pipe_conn_cb(void *); static void tlstran_pipe_nego_cb(void *); -static void tlstran_pipe_reap(tlstran_pipe *); +static void tlstran_dialer_cb(void *); +static void tlstran_listener_cb(void *); static int tlstran_init(void) @@ -116,15 +108,9 @@ tlstran_pipe_close(void *arg) { tlstran_pipe *p = arg; - nni_mtx_lock(&p->mtx); - p->closed = true; - nni_mtx_unlock(&p->mtx); - nni_aio_close(p->rxaio); nni_aio_close(p->txaio); - nni_aio_close(p->negoaio); - nni_aio_close(p->connaio); - nni_aio_close(p->rslvaio); + nni_aio_close(p->negaio); nni_tls_close(p->tls); } @@ -136,53 +122,26 @@ tlstran_pipe_stop(void *arg) nni_aio_stop(p->rxaio); nni_aio_stop(p->txaio); - nni_aio_stop(p->negoaio); - nni_aio_stop(p->connaio); - nni_aio_stop(p->rslvaio); + nni_aio_stop(p->negaio); } static void tlstran_pipe_fini(void *arg) { tlstran_pipe *p = arg; - tlstran_ep * ep; - - if (p == NULL) { - return; - } - tlstran_pipe_stop(p); - if ((ep = p->ep) != NULL) { - nni_mtx_lock(&ep->mtx); - nni_list_remove(&ep->pipes, p); - if (ep->fini && nni_list_empty(&ep->pipes)) { - nni_reap(&ep->reap, ep->dtor, ep); - } - nni_mtx_unlock(&ep->mtx); - } nni_aio_fini(p->rxaio); nni_aio_fini(p->txaio); - nni_aio_fini(p->negoaio); - nni_aio_fini(p->connaio); - nni_aio_fini(p->rslvaio); + nni_aio_fini(p->negaio); if (p->tls != NULL) { nni_tls_fini(p->tls); } nni_msg_free(p->rxmsg); - nni_mtx_fini(&p->mtx); NNI_FREE_STRUCT(p); } -static void -tlstran_pipe_reap(tlstran_pipe *p) -{ - if (!nni_atomic_flag_test_and_set(&p->reaped)) { - nni_reap(&p->reap, tlstran_pipe_fini, p); - } -} - static int -tlstran_pipe_init(tlstran_pipe **pipep, tlstran_ep *ep) +tlstran_pipe_init(tlstran_pipe **pipep, nni_tls *tls) { tlstran_pipe *p; int rv; @@ -191,110 +150,46 @@ tlstran_pipe_init(tlstran_pipe **pipep, tlstran_ep *ep) return (NNG_ENOMEM); } nni_mtx_init(&p->mtx); - nni_aio_list_init(&p->recvq); - nni_aio_list_init(&p->sendq); - nni_atomic_flag_reset(&p->reaped); - nni_list_append(&ep->pipes, p); - - p->proto = ep->proto; - p->rcvmax = ep->rcvmax; - p->nodelay = ep->nodelay; - p->keepalive = ep->keepalive; - p->ep = ep; if (((rv = nni_aio_init(&p->txaio, tlstran_pipe_send_cb, p)) != 0) || ((rv = nni_aio_init(&p->rxaio, tlstran_pipe_recv_cb, p)) != 0) || - ((rv = nni_aio_init(&p->rslvaio, tlstran_pipe_rslv_cb, p)) != 0) || - ((rv = nni_aio_init(&p->connaio, tlstran_pipe_conn_cb, p)) != 0) || - ((rv = nni_aio_init(&p->negoaio, tlstran_pipe_nego_cb, p)) != 0)) { - tlstran_pipe_reap(p); + ((rv = nni_aio_init(&p->negaio, tlstran_pipe_nego_cb, p)) != 0)) { + tlstran_pipe_fini(p); return (rv); } + nni_aio_list_init(&p->recvq); + nni_aio_list_init(&p->sendq); + p->tls = tls; *pipep = p; return (0); } static void -tlstran_pipe_rslv_cb(void *arg) +tlstran_pipe_cancel_nego(nni_aio *aio, int rv) { - tlstran_pipe * p = arg; - tlstran_dialer *d = (void *) p->ep; - nni_aio * aio = p->rslvaio; - int rv; - - nni_mtx_lock(&p->ep->mtx); - if ((rv = nni_aio_result(aio)) != 0) { - nni_aio *uaio; - if ((uaio = p->useraio) != NULL) { - p->useraio = NULL; - nni_aio_finish_error(uaio, rv); - } - nni_mtx_unlock(&p->ep->mtx); - tlstran_pipe_reap(p); - return; - } - if (d->dialer != NULL) { - nni_tcp_dialer_dial(d->dialer, &p->sa, p->connaio); - } - nni_mtx_unlock(&p->ep->mtx); -} - -static void -tlstran_pipe_conn_cb(void *arg) -{ - tlstran_pipe *p = arg; - nni_aio * aio = p->connaio; - nni_iov iov; - int rv; - - nni_mtx_lock(&p->ep->mtx); - - if ((rv = nni_aio_result(aio)) == 0) { - nni_tcp_conn *tcp = nni_aio_get_output(aio, 0); - if ((rv = nni_tls_init(&p->tls, p->ep->cfg, tcp)) != 0) { - nni_tcp_conn_fini(tcp); - } - } + tlstran_pipe *p = nni_aio_get_prov_data(aio); - if (rv != 0) { - nni_aio *uaio; - if ((uaio = p->useraio) != NULL) { - p->useraio = NULL; - nni_aio_finish_error(uaio, rv); - } - nni_mtx_unlock(&p->ep->mtx); - tlstran_pipe_reap(p); + nni_mtx_lock(&p->mtx); + if (p->user_negaio != aio) { + nni_mtx_unlock(&p->mtx); return; } + p->user_negaio = NULL; + nni_mtx_unlock(&p->mtx); - p->txlen[0] = 0; - p->txlen[1] = 'S'; - p->txlen[2] = 'P'; - p->txlen[3] = 0; - NNI_PUT16(&p->txlen[4], p->proto); - NNI_PUT16(&p->txlen[6], 0); - - p->gotrxhead = 0; - p->gottxhead = 0; - p->wantrxhead = 8; - p->wanttxhead = 8; - iov.iov_len = 8; - iov.iov_buf = &p->txlen[0]; - nni_aio_set_iov(p->negoaio, 1, &iov); - nni_tls_send(p->tls, p->negoaio); - nni_mtx_unlock(&p->ep->mtx); + nni_aio_abort(p->negaio, rv); + nni_aio_finish_error(aio, rv); } static void tlstran_pipe_nego_cb(void *arg) { tlstran_pipe *p = arg; - nni_aio * aio = p->negoaio; - nni_aio * uaio; + nni_aio * aio = p->negaio; int rv; - nni_mtx_lock(&p->ep->mtx); + nni_mtx_lock(&p->mtx); if ((rv = nni_aio_result(aio)) != 0) { goto done; } @@ -313,7 +208,7 @@ tlstran_pipe_nego_cb(void *arg) nni_aio_set_iov(aio, 1, &iov); // send it down... nni_tls_send(p->tls, aio); - nni_mtx_unlock(&p->ep->mtx); + nni_mtx_unlock(&p->mtx); return; } if (p->gotrxhead < p->wantrxhead) { @@ -322,7 +217,7 @@ tlstran_pipe_nego_cb(void *arg) iov.iov_buf = &p->rxlen[p->gotrxhead]; nni_aio_set_iov(aio, 1, &iov); nni_tls_recv(p->tls, aio); - nni_mtx_unlock(&p->ep->mtx); + nni_mtx_unlock(&p->mtx); return; } // We have both sent and received the headers. Lets check the @@ -337,23 +232,17 @@ tlstran_pipe_nego_cb(void *arg) NNI_GET16(&p->rxlen[4], p->peer); done: - if ((uaio = p->useraio) != NULL) { - p->useraio = NULL; - if (rv == 0) { - // These can fail. Note that the TLS stack - // automatically starts out in NODELAY to make the - // handshake performant. - (void) nni_tls_set_nodelay(p->tls, p->nodelay); - (void) nni_tls_set_keepalive(p->tls, p->keepalive); - nni_aio_set_output(uaio, 0, p); - nni_aio_finish(uaio, 0, 0); - nni_mtx_unlock(&p->ep->mtx); - return; - } - nni_aio_finish_error(aio, rv); + if (rv == 0) { + // These can fail. Note that the TLS stack automatically + // starts out in NODELAY to make the handshake performant. + (void) nni_tls_set_nodelay(p->tls, p->nodelay); + (void) nni_tls_set_keepalive(p->tls, p->keepalive); + } + if ((aio = p->user_negaio) != NULL) { + p->user_negaio = NULL; + nni_aio_finish(aio, rv, 0); } - nni_mtx_unlock(&p->ep->mtx); - tlstran_pipe_reap(p); + nni_mtx_unlock(&p->mtx); } static void @@ -367,18 +256,17 @@ tlstran_pipe_send_cb(void *arg) nni_aio * txaio = p->txaio; nni_mtx_lock(&p->mtx); - rv = p->closed ? NNG_ECLOSED : nni_aio_result(txaio); - if (rv != 0) { + aio = nni_list_first(&p->sendq); + + if ((rv = nni_aio_result(txaio)) != 0) { // Intentionally we do not queue up another transfer. // There's an excellent chance that the pipe is no longer // usable, with a partial transfer. // The protocol should see this error, and close the // pipe itself, we hope. - while ((aio = nni_list_first(&p->sendq)) != NULL) { - nni_aio_list_remove(aio); - nni_aio_finish_error(aio, rv); - } + nni_aio_list_remove(aio); nni_mtx_unlock(&p->mtx); + nni_aio_finish_error(aio, rv); return; } @@ -389,7 +277,6 @@ tlstran_pipe_send_cb(void *arg) nni_mtx_unlock(&p->mtx); return; } - aio = nni_list_first(&p->sendq); nni_aio_list_remove(aio); tlstran_pipe_send_start(p); nni_mtx_unlock(&p->mtx); @@ -412,14 +299,11 @@ tlstran_pipe_recv_cb(void *arg) nni_aio * rxaio = p->rxaio; nni_mtx_lock(&p->mtx); + aio = nni_list_first(&p->recvq); if ((rv = nni_aio_result(p->rxaio)) != 0) { goto recv_error; } - if (p->closed) { - rv = NNG_ECLOSED; - goto recv_error; - } n = nni_aio_count(rxaio); nni_aio_iov_advance(rxaio, n); @@ -464,7 +348,6 @@ tlstran_pipe_recv_cb(void *arg) } // We read a message completely. Let the user know the good news. - aio = nni_list_first(&p->recvq); nni_aio_list_remove(aio); msg = p->rxmsg; p->rxmsg = NULL; @@ -478,16 +361,14 @@ tlstran_pipe_recv_cb(void *arg) return; recv_error: - while ((aio = nni_list_first(&p->recvq)) != NULL) { - nni_aio_list_remove(aio); - nni_aio_finish_error(aio, rv); - } + nni_aio_list_remove(aio); msg = p->rxmsg; p->rxmsg = NULL; // Intentionally, we do not queue up another receive. // The protocol should notice this error and close the pipe. nni_mtx_unlock(&p->mtx); nni_msg_free(msg); + nni_aio_finish_error(aio, rv); } static void @@ -563,11 +444,6 @@ tlstran_pipe_send(void *arg, nni_aio *aio) return; } nni_mtx_lock(&p->mtx); - if (p->closed) { - nni_mtx_unlock(&p->mtx); - nni_aio_finish_error(aio, NNG_ECLOSED); - return; - } if ((rv = nni_aio_schedule(aio, tlstran_pipe_send_cancel, p)) != 0) { nni_mtx_unlock(&p->mtx); nni_aio_finish_error(aio, rv); @@ -610,7 +486,7 @@ tlstran_pipe_recv_start(tlstran_pipe *p) nni_iov iov; NNI_ASSERT(p->rxmsg == NULL); - // Schedule a read of the header. + // Schedule a read of the IPC header. rxaio = p->rxaio; iov.iov_buf = p->rxlen; iov.iov_len = sizeof(p->rxlen); @@ -629,11 +505,6 @@ tlstran_pipe_recv(void *arg, nni_aio *aio) return; } nni_mtx_lock(&p->mtx); - if (p->closed) { - nni_mtx_unlock(&p->mtx); - nni_aio_finish_error(aio, NNG_ECLOSED); - return; - } if ((rv = nni_aio_schedule(aio, tlstran_pipe_recv_cancel, p)) != 0) { nni_mtx_unlock(&p->mtx); nni_aio_finish_error(aio, rv); @@ -698,40 +569,56 @@ tlstran_pipe_get_nodelay(void *arg, void *v, size_t *szp, nni_opt_type t) } static void -tlstran_ep_close(tlstran_ep *ep) +tlstran_pipe_start(void *arg, nni_aio *aio) { - tlstran_pipe *p; - nni_mtx_lock(&ep->mtx); - NNI_LIST_FOREACH (&ep->pipes, p) { - nni_aio_close(p->negoaio); - nni_aio_close(p->connaio); - nni_aio_close(p->rslvaio); + tlstran_pipe *p = arg; + nni_aio * negaio; + nni_iov iov; + int rv; + + if (nni_aio_begin(aio) != 0) { + return; } - nni_mtx_unlock(&ep->mtx); + nni_mtx_lock(&p->mtx); + if ((rv = nni_aio_schedule(aio, tlstran_pipe_cancel_nego, p)) != 0) { + nni_mtx_unlock(&p->mtx); + nni_aio_finish_error(aio, rv); + return; + } + p->txlen[0] = 0; + p->txlen[1] = 'S'; + p->txlen[2] = 'P'; + p->txlen[3] = 0; + NNI_PUT16(&p->txlen[4], p->proto); + NNI_PUT16(&p->txlen[6], 0); + + p->user_negaio = aio; + p->gotrxhead = 0; + p->gottxhead = 0; + p->wantrxhead = 8; + p->wanttxhead = 8; + negaio = p->negaio; + iov.iov_len = 8; + iov.iov_buf = &p->txlen[0]; + nni_aio_set_iov(negaio, 1, &iov); + nni_tls_send(p->tls, negaio); + nni_mtx_unlock(&p->mtx); } static void tlstran_dialer_fini(void *arg) { - tlstran_dialer *d = arg; - tlstran_ep * ep = &d->ep; + tlstran_dialer *d = arg; - nni_mtx_lock(&ep->mtx); - d->ep.fini = true; + nni_aio_stop(d->aio); if (d->dialer != NULL) { nni_tcp_dialer_fini(d->dialer); - d->dialer = NULL; - } - if (!nni_list_empty(&ep->pipes)) { - nni_mtx_unlock(&ep->mtx); - return; } - nni_mtx_unlock(&ep->mtx); - if (ep->cfg != NULL) { - nni_tls_config_fini(ep->cfg); + nni_aio_fini(d->aio); + if (d->ep.cfg != NULL) { + nni_tls_config_fini(d->ep.cfg); } - nni_mtx_fini(&ep->mtx); - + nni_mtx_fini(&d->ep.mtx); NNI_FREE_STRUCT(d); } @@ -740,8 +627,8 @@ tlstran_dialer_close(void *arg) { tlstran_dialer *d = arg; + nni_aio_close(d->aio); nni_tcp_dialer_close(d->dialer); - tlstran_ep_close(&d->ep); } static int @@ -777,48 +664,110 @@ tlstran_dialer_init(void **dp, nni_url *url, nni_sock *sock) } nni_mtx_init(&d->ep.mtx); - NNI_LIST_INIT(&d->ep.pipes, tlstran_pipe, node); d->ep.authmode = NNG_TLS_AUTH_MODE_REQUIRED; d->ep.url = url; d->ep.proto = nni_sock_proto_id(sock); d->ep.nodelay = true; d->ep.keepalive = false; - d->ep.dtor = tlstran_dialer_fini; - d->af = af; if (((rv = nni_tcp_dialer_init(&d->dialer)) != 0) || ((rv = nni_tls_config_init(&d->ep.cfg, NNG_TLS_MODE_CLIENT)) != 0) || ((rv = nng_tls_config_auth_mode(d->ep.cfg, d->ep.authmode)) != 0) || - ((rv = nng_tls_config_server_name(d->ep.cfg, host)) != 0)) { + ((rv = nng_tls_config_server_name(d->ep.cfg, host)) != 0) || + ((rv = nni_aio_init(&d->aio, tlstran_dialer_cb, d)) != 0)) { tlstran_dialer_fini(d); return (rv); } + d->af = af; *dp = d; return (0); } static void -tlstran_pipe_conn_cancel(nni_aio *aio, int rv) +tlstran_dialer_cb(void *arg) { - tlstran_pipe *p = nni_aio_get_prov_data(aio); + tlstran_dialer *d = arg; + tlstran_pipe * p; + nni_tcp_conn * conn; + nni_tls * tls; + nni_aio * aio; + int rv; - nni_mtx_lock(&p->ep->mtx); - if (p->useraio != aio) { - nni_mtx_unlock(&p->ep->mtx); + nni_mtx_lock(&d->ep.mtx); + aio = d->user_aio; + rv = nni_aio_result(d->aio); + + if (aio == NULL) { + nni_mtx_unlock(&d->ep.mtx); + if ((rv == 0) && !d->resolving) { + conn = nni_aio_get_output(d->aio, 0); + nni_tcp_conn_fini(conn); + } + return; + } + + if (rv != 0) { + d->user_aio = NULL; + nni_mtx_unlock(&d->ep.mtx); + nni_aio_finish_error(aio, rv); return; } - // Close the underlying AIOs. This will abort the operation. - // The pipe is removed from pending list at completion callback. - p->useraio = NULL; - nni_aio_close(p->negoaio); - nni_aio_close(p->connaio); - nni_aio_close(p->rslvaio); - nni_mtx_unlock(&p->ep->mtx); + if (d->resolving) { + // Name resolution complete. Now go to next step. + d->resolving = false; + nni_tcp_dialer_dial(d->dialer, &d->sa, d->aio); + nni_mtx_unlock(&d->ep.mtx); + return; + } + d->user_aio = NULL; + conn = nni_aio_get_output(d->aio, 0); + NNI_ASSERT(conn != NULL); + if ((rv = nni_tls_init(&tls, d->ep.cfg, conn)) != 0) { + nni_mtx_unlock(&d->ep.mtx); + nni_tcp_conn_fini(conn); + nni_aio_finish_error(aio, rv); + return; + } + + if ((rv = tlstran_pipe_init(&p, tls)) != 0) { + nni_mtx_unlock(&d->ep.mtx); + nni_tls_fini(tls); + nni_aio_finish_error(aio, rv); + return; + } + + p->proto = d->ep.proto; + p->rcvmax = d->ep.rcvmax; + p->nodelay = d->ep.nodelay; + p->keepalive = d->ep.keepalive; + nni_mtx_unlock(&d->ep.mtx); + + (void) nni_tls_set_nodelay(tls, p->nodelay); + (void) nni_tls_set_keepalive(tls, p->keepalive); + + nni_aio_set_output(aio, 0, p); + nni_aio_finish(aio, 0, 0); +} + +static void +tlstran_dialer_cancel(nni_aio *aio, int rv) +{ + tlstran_dialer *d = nni_aio_get_prov_data(aio); + + nni_mtx_lock(&d->ep.mtx); + if (d->user_aio != aio) { + nni_mtx_unlock(&d->ep.mtx); + return; + } + d->user_aio = NULL; + nni_mtx_unlock(&d->ep.mtx); + + nni_aio_abort(d->aio, rv); nni_aio_finish_error(aio, rv); } @@ -826,52 +775,45 @@ static void tlstran_dialer_connect(void *arg, nni_aio *aio) { tlstran_dialer *d = arg; - tlstran_pipe * p = NULL; int rv; if (nni_aio_begin(aio) != 0) { return; } nni_mtx_lock(&d->ep.mtx); - if (((rv = tlstran_pipe_init(&p, &d->ep)) != 0) || - ((rv = nni_aio_schedule(aio, tlstran_pipe_conn_cancel, p)) != 0)) { + NNI_ASSERT(d->user_aio == NULL); + + if ((rv = nni_aio_schedule(aio, tlstran_dialer_cancel, d)) != 0) { nni_mtx_unlock(&d->ep.mtx); nni_aio_finish_error(aio, rv); - tlstran_pipe_reap(p); return; } - p->useraio = aio; - p->dialer = d->dialer; + d->user_aio = aio; - // Start the name resolution. - nni_aio_set_input(p->rslvaio, 0, &p->sa); + d->resolving = true; + + // Start the name resolution. Callback will see resolving, and then + // switch to doing actual connect. + nni_aio_set_input(d->aio, 0, &d->sa); nni_tcp_resolv( - d->ep.url->u_hostname, d->ep.url->u_port, d->af, 0, p->rslvaio); + d->ep.url->u_hostname, d->ep.url->u_port, d->af, 0, d->aio); nni_mtx_unlock(&d->ep.mtx); } static void tlstran_listener_fini(void *arg) { - tlstran_listener *l = arg; - tlstran_ep * ep = &l->ep; + tlstran_listener *l = arg; - nni_mtx_lock(&ep->mtx); - l->ep.fini = true; + nni_aio_stop(l->aio); if (l->listener != NULL) { nni_tcp_listener_fini(l->listener); - l->listener = NULL; } - if (!nni_list_empty(&ep->pipes)) { - nni_mtx_unlock(&ep->mtx); - return; - } - nni_mtx_unlock(&ep->mtx); - if (ep->cfg != NULL) { - nni_tls_config_fini(ep->cfg); + nni_aio_fini(l->aio); + if (l->ep.cfg != NULL) { + nni_tls_config_fini(l->ep.cfg); } - nni_mtx_fini(&ep->mtx); - + nni_mtx_fini(&l->ep.mtx); NNI_FREE_STRUCT(l); } @@ -880,7 +822,7 @@ tlstran_listener_close(void *arg) { tlstran_listener *l = arg; - tlstran_ep_close(&l->ep); + nni_aio_close(l->aio); nni_tcp_listener_close(l->listener); } @@ -917,13 +859,11 @@ tlstran_listener_init(void **lp, nni_url *url, nni_sock *sock) return (NNG_ENOMEM); } nni_mtx_init(&l->ep.mtx); - NNI_LIST_INIT(&l->ep.pipes, tlstran_pipe, node); l->ep.url = url; l->ep.authmode = NNG_TLS_AUTH_MODE_NONE; l->ep.keepalive = false; l->ep.nodelay = true; l->ep.proto = nni_sock_proto_id(sock); - l->ep.dtor = tlstran_listener_fini; if (strlen(host) == 0) { host = NULL; @@ -956,7 +896,8 @@ tlstran_listener_init(void **lp, nni_url *url, nni_sock *sock) ((rv = nni_tls_config_init(&l->ep.cfg, NNG_TLS_MODE_SERVER)) != 0) || ((rv = nng_tls_config_auth_mode(l->ep.cfg, l->ep.authmode)) != - 0)) { + 0) || + ((rv = nni_aio_init(&l->aio, tlstran_listener_cb, l)) != 0)) { tlstran_listener_fini(l); return (rv); } @@ -981,26 +922,98 @@ tlstran_listener_bind(void *arg) } static void -tlstran_listener_accept(void *arg, nni_aio *aio) +tlstran_listener_cb(void *arg) { tlstran_listener *l = arg; + nni_aio * aio; + int rv; tlstran_pipe * p = NULL; + nni_tcp_conn * conn; + nni_tls * tls; + + nni_mtx_lock(&l->ep.mtx); + rv = nni_aio_result(l->aio); + aio = l->user_aio; + l->user_aio = NULL; + + if (aio == NULL) { + nni_mtx_unlock(&l->ep.mtx); + if (rv == 0) { + conn = nni_aio_get_output(l->aio, 0); + nni_tcp_conn_fini(conn); + } + return; + } + if (rv != 0) { + nni_mtx_unlock(&l->ep.mtx); + nni_aio_finish_error(aio, rv); + return; + } + + conn = nni_aio_get_output(l->aio, 0); + if ((rv = nni_tls_init(&tls, l->ep.cfg, conn)) != 0) { + nni_mtx_unlock(&l->ep.mtx); + nni_tcp_conn_fini(conn); + nni_aio_finish_error(aio, rv); + return; + } + if ((rv = tlstran_pipe_init(&p, tls)) != 0) { + nni_mtx_unlock(&l->ep.mtx); + nni_tls_fini(tls); + nni_aio_finish_error(aio, rv); + return; + } + p->proto = l->ep.proto; + p->rcvmax = l->ep.rcvmax; + p->nodelay = l->ep.nodelay; + p->keepalive = l->ep.keepalive; + + (void) nni_tls_set_nodelay(tls, p->nodelay); + (void) nni_tls_set_keepalive(tls, p->keepalive); + + nni_mtx_unlock(&l->ep.mtx); + + nni_aio_set_output(aio, 0, p); + nni_aio_finish(aio, 0, 0); +} + +static void +tlstran_listener_cancel(nni_aio *aio, int rv) +{ + tlstran_listener *l = nni_aio_get_prov_data(aio); + + nni_mtx_lock(&l->ep.mtx); + if (l->user_aio != aio) { + nni_mtx_unlock(&l->ep.mtx); + return; + } + l->user_aio = NULL; + nni_mtx_unlock(&l->ep.mtx); + + nni_aio_abort(l->aio, rv); + nni_aio_finish_error(aio, rv); +} + +static void +tlstran_listener_accept(void *arg, nni_aio *aio) +{ + tlstran_listener *l = arg; int rv; if (nni_aio_begin(aio) != 0) { return; } nni_mtx_lock(&l->ep.mtx); - if (((rv = tlstran_pipe_init(&p, &l->ep)) != 0) || - ((rv = nni_aio_schedule(aio, tlstran_pipe_conn_cancel, p)) != 0)) { + NNI_ASSERT(l->user_aio == NULL); + + if ((rv = nni_aio_schedule(aio, tlstran_listener_cancel, l)) != 0) { nni_mtx_unlock(&l->ep.mtx); nni_aio_finish_error(aio, rv); - tlstran_pipe_reap(p); return; } - p->useraio = aio; + l->user_aio = aio; - nni_tcp_listener_accept(l->listener, p->connaio); + nni_tcp_listener_accept(l->listener, l->aio); nni_mtx_unlock(&l->ep.mtx); } @@ -1293,6 +1306,7 @@ static nni_tran_option tlstran_pipe_options[] = { static nni_tran_pipe_ops tlstran_pipe_ops = { .p_fini = tlstran_pipe_fini, + .p_start = tlstran_pipe_start, .p_stop = tlstran_pipe_stop, .p_send = tlstran_pipe_send, .p_recv = tlstran_pipe_recv, |
