diff options
Diffstat (limited to 'src/transport/tls')
| -rw-r--r-- | src/transport/tls/tls.c | 441 |
1 files changed, 309 insertions, 132 deletions
diff --git a/src/transport/tls/tls.c b/src/transport/tls/tls.c index 8b02702f..58b43f2c 100644 --- a/src/transport/tls/tls.c +++ b/src/transport/tls/tls.c @@ -53,7 +53,6 @@ struct tlstran_pipe { nni_aio * txaio; nni_aio * rxaio; nni_aio * negoaio; - nni_aio * connaio; nni_msg * rxmsg; nni_mtx mtx; }; @@ -64,25 +63,34 @@ struct tlstran_ep { uint16_t af; uint16_t proto; size_t rcvmax; + bool started; + bool closed; bool fini; + int refcnt; int authmode; nni_url * url; nni_list pipes; nni_reap_item reap; nng_stream_dialer * dialer; nng_stream_listener *listener; + nni_aio * useraio; + nni_aio * connaio; + nni_aio * timeaio; + nni_list busypipes; // busy pipes -- ones passed to socket + nni_list waitpipes; // pipes waiting to match to socket + nni_list negopipes; // pipes busy negotiating const char * host; nng_sockaddr src; nng_sockaddr sa; nni_dialer * ndialer; nni_listener * nlistener; + nni_stat_item st_rcvmaxsz; }; 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_conn_cb(void *); static void tlstran_pipe_nego_cb(void *); static void tlstran_ep_fini(void *); @@ -105,7 +113,6 @@ tlstran_pipe_close(void *arg) nni_aio_close(p->rxaio); nni_aio_close(p->txaio); nni_aio_close(p->negoaio); - nni_aio_close(p->connaio); nng_stream_close(p->tls); } @@ -118,7 +125,6 @@ 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); } static int @@ -138,8 +144,9 @@ tlstran_pipe_fini(void *arg) 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_list_node_remove(&p->node); + ep->refcnt--; + if (ep->fini && (ep->refcnt == 0)) { nni_reap(&ep->reap, tlstran_ep_fini, ep); } nni_mtx_unlock(&ep->mtx); @@ -147,14 +154,13 @@ tlstran_pipe_fini(void *arg) nni_aio_fini(p->rxaio); nni_aio_fini(p->txaio); nni_aio_fini(p->negoaio); - nni_aio_fini(p->connaio); nng_stream_free(p->tls); nni_msg_free(p->rxmsg); NNI_FREE_STRUCT(p); } static int -tlstran_pipe_alloc(tlstran_pipe **pipep, tlstran_ep *ep) +tlstran_pipe_alloc(tlstran_pipe **pipep) { tlstran_pipe *p; int rv; @@ -166,7 +172,6 @@ tlstran_pipe_alloc(tlstran_pipe **pipep, tlstran_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->connaio, tlstran_pipe_conn_cb, p)) != 0) || ((rv = nni_aio_init(&p->negoaio, tlstran_pipe_nego_cb, p)) != 0)) { tlstran_pipe_fini(p); return (rv); @@ -174,12 +179,8 @@ tlstran_pipe_alloc(tlstran_pipe **pipep, tlstran_ep *ep) 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->rcvmax = ep->rcvmax; - p->proto = ep->proto; - p->ep = ep; - *pipep = p; + *pipep = p; return (0); } @@ -195,67 +196,21 @@ tlstran_pipe_reap(tlstran_pipe *p) } static void -tlstran_pipe_conn_cancel(nni_aio *aio, void *arg, int rv) +tlstran_ep_match(tlstran_ep *ep) { - tlstran_pipe *p = arg; - - nni_mtx_lock(&p->ep->mtx); - if (aio == p->useraio) { - p->useraio = NULL; - nni_aio_close(p->negoaio); - nni_aio_close(p->connaio); - nni_aio_finish_error(aio, rv); - tlstran_pipe_reap(p); - } - nni_mtx_unlock(&p->ep->mtx); -} - -static void -tlstran_pipe_conn_cb(void *arg) -{ - tlstran_pipe *p = arg; - tlstran_ep * ep = p->ep; - nni_aio * aio = p->connaio; - nni_aio * uaio; - nni_iov iov; - int rv; - - nni_mtx_lock(&ep->mtx); - if ((rv = nni_aio_result(aio)) == 0) { - p->tls = nni_aio_get_output(aio, 0); - } else { - p->tls = NULL; - } - - if ((uaio = p->useraio) == NULL) { - nni_mtx_unlock(&ep->mtx); - tlstran_pipe_reap(p); - return; - } + nni_aio * aio; + tlstran_pipe *p; - if (rv != 0) { - p->useraio = NULL; - nni_mtx_unlock(&ep->mtx); - nni_aio_finish_error(uaio, rv); - tlstran_pipe_reap(p); + if (((aio = ep->useraio) == NULL) || + ((p = nni_list_first(&ep->waitpipes)) == NULL)) { 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->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); - nng_stream_send(p->tls, p->negoaio); - nni_mtx_unlock(&ep->mtx); + nni_list_remove(&ep->waitpipes, p); + nni_list_append(&ep->busypipes, p); + ep->useraio = NULL; + p->rcvmax = ep->rcvmax; + nni_aio_set_output(aio, 0, p); + nni_aio_finish(aio, 0, 0); } static void @@ -268,12 +223,6 @@ tlstran_pipe_nego_cb(void *arg) int rv; nni_mtx_lock(&ep->mtx); - if ((uaio = p->useraio) == NULL) { - nni_mtx_unlock(&ep->mtx); - tlstran_pipe_reap(p); - return; - } - if ((rv = nni_aio_result(aio)) != 0) { goto error; } @@ -314,17 +263,33 @@ tlstran_pipe_nego_cb(void *arg) } NNI_GET16(&p->rxlen[4], p->peer); - p->useraio = NULL; + + // We are all ready now. We put this in the wait list, and + // then try to run the matcher. + nni_list_remove(&ep->negopipes, p); + nni_list_append(&ep->waitpipes, p); + + tlstran_ep_match(ep); nni_mtx_unlock(&ep->mtx); - nni_aio_set_output(uaio, 0, p); - nni_aio_finish(uaio, 0, 0); return; error: - p->useraio = NULL; + if (ep->ndialer != NULL) { + nni_dialer_bump_error(ep->ndialer, rv); + } else { + nni_listener_bump_error(ep->nlistener, rv); + } + + nng_stream_close(p->tls); + + // If we are waiting to negotiate on a client side, then a failure + // here has to be passed to the user app. + if ((ep->dialer != NULL) && ((uaio = ep->useraio) != NULL)) { + ep->useraio = NULL; + nni_aio_finish_error(uaio, rv); + } nni_mtx_unlock(&ep->mtx); - nni_aio_finish_error(uaio, rv); tlstran_pipe_reap(p); } @@ -350,6 +315,7 @@ tlstran_pipe_send_cb(void *arg) nni_aio_list_remove(aio); nni_mtx_unlock(&p->mtx); nni_aio_finish_error(aio, rv); + nni_pipe_bump_error(p->npipe, rv); return; } @@ -362,10 +328,11 @@ tlstran_pipe_send_cb(void *arg) } nni_aio_list_remove(aio); tlstran_pipe_send_start(p); - nni_mtx_unlock(&p->mtx); msg = nni_aio_get_msg(aio); n = nni_msg_len(msg); + nni_pipe_bump_tx(p->npipe, n); + nni_mtx_unlock(&p->mtx); nni_aio_set_msg(aio, NULL); nni_msg_free(msg); nni_aio_finish_synch(aio, 0, n); @@ -434,19 +401,22 @@ tlstran_pipe_recv_cb(void *arg) nni_aio_list_remove(aio); msg = p->rxmsg; p->rxmsg = NULL; + n = nni_msg_len(msg); if (!nni_list_empty(&p->recvq)) { tlstran_pipe_recv_start(p); } + nni_pipe_bump_rx(p->npipe, n); nni_mtx_unlock(&p->mtx); nni_aio_set_msg(aio, msg); - nni_aio_finish_synch(aio, 0, nni_msg_len(msg)); + nni_aio_finish_synch(aio, 0, n); return; recv_error: nni_aio_list_remove(aio); msg = p->rxmsg; p->rxmsg = NULL; + nni_pipe_bump_error(p->npipe, rv); // Intentionally, we do not queue up another receive. // The protocol should notice this error and close the pipe. nni_mtx_unlock(&p->mtx); @@ -610,19 +580,54 @@ tlstran_pipe_peer(void *arg) } static void +tlstran_pipe_start(tlstran_pipe *p, nng_stream *conn, tlstran_ep *ep) +{ + nni_iov iov; + + ep->refcnt++; + + p->tls = conn; + p->ep = ep; + p->proto = ep->proto; + + 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_list_append(&ep->negopipes, p); + + nni_aio_set_timeout(p->negoaio, 10000); // 10 sec timeout to negotiate + nng_stream_send(p->tls, p->negoaio); +} + +static void tlstran_ep_fini(void *arg) { tlstran_ep *ep = arg; nni_mtx_lock(&ep->mtx); ep->fini = true; - if (!nni_list_empty(&ep->pipes)) { + if (ep->refcnt != 0) { nni_mtx_unlock(&ep->mtx); return; } + nni_mtx_unlock(&ep->mtx); + nni_aio_stop(ep->timeaio); + nni_aio_stop(ep->connaio); nng_stream_dialer_free(ep->dialer); nng_stream_listener_free(ep->listener); - nni_mtx_unlock(&ep->mtx); + nni_aio_fini(ep->timeaio); + nni_aio_fini(ep->connaio); nni_mtx_fini(&ep->mtx); NNI_FREE_STRUCT(ep); @@ -635,21 +640,28 @@ tlstran_ep_close(void *arg) 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->txaio); - nni_aio_close(p->rxaio); - if (p->tls != NULL) { - nng_stream_close(p->tls); - } - } + ep->closed = true; + nni_aio_close(ep->timeaio); + if (ep->dialer != NULL) { nng_stream_dialer_close(ep->dialer); } if (ep->listener != NULL) { nng_stream_listener_close(ep->listener); } + NNI_LIST_FOREACH (&ep->negopipes, p) { + tlstran_pipe_close(p); + } + NNI_LIST_FOREACH (&ep->waitpipes, p) { + tlstran_pipe_close(p); + } + NNI_LIST_FOREACH (&ep->busypipes, p) { + tlstran_pipe_close(p); + } + if (ep->useraio != NULL) { + nni_aio_finish_error(ep->useraio, NNG_ECLOSED); + ep->useraio = NULL; + } nni_mtx_unlock(&ep->mtx); } @@ -712,6 +724,130 @@ tlstran_url_parse_source(nni_url *url, nng_sockaddr *sa, const nni_url *surl) return (rv); } +static void +tlstran_timer_cb(void *arg) +{ + tlstran_ep *ep = arg; + if (nni_aio_result(ep->timeaio) == 0) { + nng_stream_listener_accept(ep->listener, ep->connaio); + } +} + +static void +tlstran_accept_cb(void *arg) +{ + tlstran_ep * ep = arg; + nni_aio * aio = ep->connaio; + tlstran_pipe *p; + int rv; + nng_stream * conn; + + nni_mtx_lock(&ep->mtx); + + if ((rv = nni_aio_result(aio)) != 0) { + goto error; + } + + conn = nni_aio_get_output(aio, 0); + if ((rv = tlstran_pipe_alloc(&p)) != 0) { + nng_stream_free(conn); + goto error; + } + + if (ep->closed) { + tlstran_pipe_fini(p); + nng_stream_free(conn); + rv = NNG_ECLOSED; + goto error; + } + tlstran_pipe_start(p, conn, ep); + nng_stream_listener_accept(ep->listener, ep->connaio); + nni_mtx_unlock(&ep->mtx); + return; + +error: + nni_listener_bump_error(ep->nlistener, rv); + switch (rv) { + + case NNG_ENOMEM: + nng_sleep_aio(10, ep->timeaio); + break; + + default: + if (!ep->closed) { + nng_stream_listener_accept(ep->listener, ep->connaio); + } + break; + } + nni_mtx_unlock(&ep->mtx); +} + +static void +tlstran_dial_cb(void *arg) +{ + tlstran_ep * ep = arg; + nni_aio * aio = ep->connaio; + tlstran_pipe *p; + int rv; + nng_stream * conn; + + if ((rv = nni_aio_result(aio)) != 0) { + goto error; + } + + conn = nni_aio_get_output(aio, 0); + if ((rv = tlstran_pipe_alloc(&p)) != 0) { + nng_stream_free(conn); + goto error; + } + nni_mtx_lock(&ep->mtx); + if (ep->closed) { + tlstran_pipe_fini(p); + nng_stream_free(conn); + rv = NNG_ECLOSED; + } else { + tlstran_pipe_start(p, conn, ep); + } + nni_mtx_unlock(&ep->mtx); + return; + +error: + // Error connecting. We need to pass this straight back + // to the user. + nni_dialer_bump_error(ep->ndialer, rv); + nni_mtx_lock(&ep->mtx); + if ((aio = ep->useraio) != NULL) { + ep->useraio = NULL; + nni_aio_finish_error(aio, rv); + } + nni_mtx_unlock(&ep->mtx); + return; +} + +static int +tlstran_ep_init(tlstran_ep **epp, nng_url *url, nni_sock *sock) +{ + tlstran_ep *ep; + + if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { + return (NNG_ENOMEM); + } + nni_mtx_init(&ep->mtx); + NNI_LIST_INIT(&ep->busypipes, tlstran_pipe, node); + NNI_LIST_INIT(&ep->waitpipes, tlstran_pipe, node); + NNI_LIST_INIT(&ep->negopipes, tlstran_pipe, node); + + ep->proto = nni_sock_proto_id(sock); + ep->url = url; + + nni_stat_init(&ep->st_rcvmaxsz, "rcvmaxsz", "maximum receive size"); + nni_stat_set_type(&ep->st_rcvmaxsz, NNG_STAT_LEVEL); + nni_stat_set_unit(&ep->st_rcvmaxsz, NNG_UNIT_BYTES); + + *epp = ep; + return (0); +} + static int tlstran_ep_init_dialer(void **dp, nni_url *url, nni_dialer *ndialer) { @@ -735,16 +871,11 @@ tlstran_ep_init_dialer(void **dp, nni_url *url, nni_dialer *ndialer) return (NNG_EADDRINVAL); } - if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { - return (NNG_ENOMEM); + if (((rv = tlstran_ep_init(&ep, url, sock)) != 0) || + ((rv = nni_aio_init(&ep->connaio, tlstran_dial_cb, ep)) != 0)) { + return (rv); } - - nni_mtx_init(&ep->mtx); - NNI_LIST_INIT(&ep->pipes, tlstran_pipe, node); - ep->authmode = NNG_TLS_AUTH_MODE_REQUIRED; - ep->url = url; - ep->proto = nni_sock_proto_id(sock); ep->ndialer = ndialer; if ((rv != 0) || @@ -758,6 +889,7 @@ tlstran_ep_init_dialer(void **dp, nni_url *url, nni_dialer *ndialer) tlstran_ep_fini(ep); return (rv); } + nni_dialer_add_stat(ndialer, &ep->st_rcvmaxsz); *dp = ep; return (0); } @@ -790,17 +922,14 @@ tlstran_ep_init_listener(void **lp, nni_url *url, nni_listener *nlistener) (url->u_query != NULL)) { return (NNG_EADDRINVAL); } - if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { - return (NNG_ENOMEM); + if (((rv = tlstran_ep_init(&ep, url, sock)) != 0) || + ((rv = nni_aio_init(&ep->connaio, tlstran_accept_cb, ep)) != 0) || + ((rv = nni_aio_init(&ep->timeaio, tlstran_timer_cb, ep)) != 0)) { + return (rv); } - nni_mtx_init(&ep->mtx); - NNI_LIST_INIT(&ep->pipes, tlstran_pipe, node); - ep->authmode = NNG_TLS_AUTH_MODE_NONE; - ep->url = url; ep->af = af; - ep->proto = nni_sock_proto_id(sock); ep->nlistener = nlistener; if (strlen(host) == 0) { @@ -832,35 +961,60 @@ tlstran_ep_init_listener(void **lp, nni_url *url, nni_listener *nlistener) return (rv); } + nni_listener_add_stat(nlistener, &ep->st_rcvmaxsz); *lp = ep; return (0); } static void +tlstran_ep_cancel(nni_aio *aio, void *arg, int rv) +{ + tlstran_ep *ep = arg; + nni_mtx_lock(&ep->mtx); + if (ep->useraio == aio) { + ep->useraio = NULL; + nni_aio_finish_error(aio, rv); + if (ep->ndialer) { + nni_dialer_bump_error(ep->ndialer, rv); + } else { + nni_listener_bump_error(ep->nlistener, rv); + } + } + nni_mtx_unlock(&ep->mtx); +} + +static void tlstran_ep_connect(void *arg, nni_aio *aio) { - tlstran_ep * ep = arg; - tlstran_pipe *p; - int rv; + tlstran_ep *ep = arg; + int rv; if (nni_aio_begin(aio) != 0) { return; } + nni_mtx_lock(&ep->mtx); - if ((rv = tlstran_pipe_alloc(&p, ep)) != 0) { + if (ep->closed) { nni_mtx_unlock(&ep->mtx); - nni_aio_finish_error(aio, rv); + nni_aio_finish_error(aio, NNG_ECLOSED); + nni_dialer_bump_error(ep->ndialer, NNG_ECLOSED); return; } - - if ((rv = nni_aio_schedule(aio, tlstran_pipe_conn_cancel, p)) != 0) { + if (ep->useraio != NULL) { + nni_mtx_unlock(&ep->mtx); + nni_aio_finish_error(aio, NNG_EBUSY); + nni_dialer_bump_error(ep->ndialer, NNG_EBUSY); + return; + } + if ((rv = nni_aio_schedule(aio, tlstran_ep_cancel, ep)) != 0) { nni_mtx_unlock(&ep->mtx); + nni_dialer_bump_error(ep->ndialer, rv); nni_aio_finish_error(aio, rv); - tlstran_pipe_reap(p); return; } - p->useraio = aio; - nng_stream_dialer_dial(ep->dialer, p->connaio); + ep->useraio = aio; + + nng_stream_dialer_dial(ep->dialer, ep->connaio); nni_mtx_unlock(&ep->mtx); } @@ -871,7 +1025,9 @@ tlstran_ep_bind(void *arg) int rv; nni_mtx_lock(&ep->mtx); - rv = nng_stream_listener_listen(ep->listener); + if ((rv = nng_stream_listener_listen(ep->listener)) != 0) { + nni_listener_bump_error(ep->nlistener, rv); + } nni_mtx_unlock(&ep->mtx); return (rv); @@ -880,29 +1036,38 @@ tlstran_ep_bind(void *arg) static void tlstran_ep_accept(void *arg, nni_aio *aio) { - tlstran_ep * ep = arg; - tlstran_pipe *p; - int rv; + tlstran_ep *ep = arg; + int rv; if (nni_aio_begin(aio) != 0) { return; } nni_mtx_lock(&ep->mtx); - if ((rv = tlstran_pipe_alloc(&p, ep)) != 0) { + if (ep->closed) { nni_mtx_unlock(&ep->mtx); - nni_aio_finish_error(aio, rv); + nni_aio_finish_error(aio, NNG_ECLOSED); + nni_listener_bump_error(ep->nlistener, NNG_ECLOSED); return; } - - if ((rv = nni_aio_schedule(aio, tlstran_pipe_conn_cancel, p)) != 0) { + if (ep->useraio != NULL) { + nni_mtx_unlock(&ep->mtx); + nni_aio_finish_error(aio, NNG_EBUSY); + nni_listener_bump_error(ep->nlistener, NNG_EBUSY); + return; + } + if ((rv = nni_aio_schedule(aio, tlstran_ep_cancel, ep)) != 0) { nni_mtx_unlock(&ep->mtx); nni_aio_finish_error(aio, rv); - tlstran_pipe_reap(p); + nni_listener_bump_error(ep->nlistener, rv); return; } - - p->useraio = aio; - nng_stream_listener_accept(ep->listener, p->connaio); + ep->useraio = aio; + if (!ep->started) { + ep->started = true; + nng_stream_listener_accept(ep->listener, ep->connaio); + } else { + tlstran_ep_match(ep); + } nni_mtx_unlock(&ep->mtx); } @@ -913,8 +1078,20 @@ tlstran_ep_set_recvmaxsz(void *arg, const void *v, size_t sz, nni_type t) size_t val; int rv; if ((rv = nni_copyin_size(&val, v, sz, 0, NNI_MAXSZ, t)) == 0) { + tlstran_pipe *p; nni_mtx_lock(&ep->mtx); ep->rcvmax = val; + NNI_LIST_FOREACH (&ep->waitpipes, p) { + p->rcvmax = val; + } + NNI_LIST_FOREACH (&ep->negopipes, p) { + p->rcvmax = val; + } + NNI_LIST_FOREACH (&ep->busypipes, p) { + p->rcvmax = val; + } + nni_stat_set_value(&ep->st_rcvmaxsz, val); + nni_mtx_unlock(&ep->mtx); } return (rv); |
