diff options
| author | Garrett D'Amore <garrett@damore.org> | 2017-12-28 16:02:53 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2017-12-28 16:02:53 -0800 |
| commit | aa3bb50aeca3b7350a41f0538817c49d9656d207 (patch) | |
| tree | 4abafbdc7553e52776394e067fa1ff9a08a14a57 /src | |
| parent | 838d92deabffbaae1b1a9e447d2088f966078726 (diff) | |
| download | nng-aa3bb50aeca3b7350a41f0538817c49d9656d207.tar.gz nng-aa3bb50aeca3b7350a41f0538817c49d9656d207.tar.bz2 nng-aa3bb50aeca3b7350a41f0538817c49d9656d207.zip | |
Fix compilation warnings, bugs, and crashes found on Windows.
This addresses a number of problems that were found on Windows,
including one bug that actually turned up in testing on POSIX.
Diffstat (limited to 'src')
| -rw-r--r-- | src/platform/posix/posix_epdesc.c | 2 | ||||
| -rw-r--r-- | src/platform/windows/win_tcp.c | 10 | ||||
| -rw-r--r-- | src/platform/windows/win_thread.c | 9 | ||||
| -rw-r--r-- | src/supplemental/http/http.c | 7 | ||||
| -rw-r--r-- | src/supplemental/http/server.c | 68 | ||||
| -rw-r--r-- | src/supplemental/mbedtls/tls.c | 8 |
6 files changed, 48 insertions, 56 deletions
diff --git a/src/platform/posix/posix_epdesc.c b/src/platform/posix/posix_epdesc.c index 80711ed0..7b168679 100644 --- a/src/platform/posix/posix_epdesc.c +++ b/src/platform/posix/posix_epdesc.c @@ -318,6 +318,7 @@ nni_posix_epdesc_accept(nni_posix_epdesc *ed, nni_aio *aio) // connection is ready for us. There isn't anything else for us to // do really, as that will have been done in listen. nni_mtx_lock(&ed->mtx); + aio->a_pipe = NULL; // If we can't start, it means that the AIO was stopped. if ((rv = nni_aio_start(aio, nni_posix_epdesc_cancel, ed)) != 0) { nni_mtx_unlock(&ed->mtx); @@ -343,6 +344,7 @@ nni_posix_epdesc_connect(nni_posix_epdesc *ed, nni_aio *aio) int fd; nni_mtx_lock(&ed->mtx); + aio->a_pipe = NULL; // If we can't start, it means that the AIO was stopped. if ((rv = nni_aio_start(aio, nni_posix_epdesc_cancel, ed)) != 0) { nni_mtx_unlock(&ed->mtx); diff --git a/src/platform/windows/win_tcp.c b/src/platform/windows/win_tcp.c index 01818af4..93cead91 100644 --- a/src/platform/windows/win_tcp.c +++ b/src/platform/windows/win_tcp.c @@ -163,6 +163,12 @@ nni_win_tcp_pipe_cancel(nni_win_event *evt) static void nni_win_tcp_pipe_finish(nni_win_event *evt, nni_aio *aio) { + if ((evt->status == 0) && (evt->count == 0)) { + // Windows sometimes returns a zero read. Convert these + // into an NNG_ECLOSED. (We are never supposed to come + // back with zero length read.) + evt->status = NNG_ECLOSED; + } nni_aio_finish(aio, evt->status, evt->count); } @@ -263,10 +269,10 @@ nni_plat_tcp_ep_init(nni_plat_tcp_ep **epp, const nni_sockaddr *lsa, ep->s = INVALID_SOCKET; - if (rsa->s_un.s_family != NNG_AF_UNSPEC) { + if ((rsa != NULL) && (rsa->s_un.s_family != NNG_AF_UNSPEC)) { ep->remlen = nni_win_nn2sockaddr(&ep->remaddr, rsa); } - if (lsa->s_un.s_family != NNG_AF_UNSPEC) { + if ((lsa != NULL) && (lsa->s_un.s_family != NNG_AF_UNSPEC)) { ep->loclen = nni_win_nn2sockaddr(&ep->locaddr, lsa); } diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c index 0d9e7387..6cfb4162 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -14,20 +14,19 @@ #ifdef NNG_PLATFORM_WINDOWS +#include <stdlib.h> + void * nni_alloc(size_t sz) { - void *v; - - v = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz); - return (v); + return (calloc(sz, 1)); } void nni_free(void *b, size_t z) { NNI_ARG_UNUSED(z); - HeapFree(GetProcessHeap(), 0, b); + free(b); } void diff --git a/src/supplemental/http/http.c b/src/supplemental/http/http.c index 08a8eb13..f414c5c7 100644 --- a/src/supplemental/http/http.c +++ b/src/supplemental/http/http.c @@ -299,11 +299,12 @@ http_rd_cancel(nni_aio *aio, int rv) nni_mtx_lock(&http->mtx); if (nni_aio_list_active(aio)) { - nni_aio_list_remove(aio); if (aio == nni_list_first(&http->rdq)) { - http_close(http); + nni_aio_cancel(http->rd_aio, NNG_ECANCELED); + } else { + nni_aio_list_remove(aio); + nni_aio_finish_error(aio, rv); } - nni_aio_finish_error(aio, rv); } nni_mtx_unlock(&http->mtx); } diff --git a/src/supplemental/http/server.c b/src/supplemental/http/server.c index 92853898..ea7f15ce 100644 --- a/src/supplemental/http/server.c +++ b/src/supplemental/http/server.c @@ -51,6 +51,7 @@ typedef struct http_sconn { nni_aio * txaio; nni_aio * txdataio; nni_http_tran tran; + nni_reap_item reap; } http_sconn; struct nni_http_server { @@ -60,12 +61,10 @@ struct nni_http_server { int starts; nni_list handlers; nni_list conns; - nni_list reaps; nni_mtx mtx; nni_cv cv; bool closed; bool tls; - nni_task cleanup; nni_aio * accaio; nni_plat_tcp_ep *tep; }; @@ -74,7 +73,7 @@ static nni_list http_servers; static nni_mtx http_servers_lk; static void -http_sconn_fini(void *arg) +http_sconn_reap(void *arg) { http_sconn *sc = arg; NNI_ASSERT(!sc->finished); @@ -100,6 +99,12 @@ http_sconn_fini(void *arg) } static void +http_sconn_fini(http_sconn *sc) +{ + nni_reap(&sc->reap, http_sconn_reap, sc); +} + +static void http_sconn_close(http_sconn *sc) { nni_http_server *s; @@ -111,16 +116,16 @@ http_sconn_close(http_sconn *sc) nni_http *h; sc->closed = true; // Close the underlying transport. - if ((h = sc->http) != NULL) { - nni_http_close(h); - } if (nni_list_node_active(&sc->node)) { nni_list_remove(&s->conns, sc); - nni_list_append(&s->reaps, sc); + if (nni_list_empty(&s->conns)) { + nni_cv_wake(&s->cv); + } } - nni_task_dispatch(&s->cleanup); - } else { - nni_panic("DOUBLE CLOSE!\n"); + if ((h = sc->http) != NULL) { + nni_http_close(h); + } + http_sconn_fini(sc); } nni_mtx_unlock(&s->mtx); } @@ -544,34 +549,14 @@ http_server_acccb(void *arg) nni_mtx_lock(&s->mtx); sc->server = s; if (s->closed) { - nni_http_close(sc->http); - sc->closed = true; - nni_list_append(&s->reaps, sc); nni_mtx_unlock(&s->mtx); + http_sconn_close(sc); return; } nni_list_append(&s->conns, sc); - nni_mtx_unlock(&s->mtx); nni_http_read_req(sc->http, sc->req, sc->rxaio); nni_plat_tcp_ep_accept(s->tep, s->accaio); -} - -static void -http_server_cleanup(void *arg) -{ - nni_http_server *s = arg; - http_sconn * sc; - nni_mtx_lock(&s->mtx); - while ((sc = nni_list_first(&s->reaps)) != NULL) { - nni_list_remove(&s->reaps, sc); - nni_mtx_unlock(&s->mtx); - http_sconn_fini(sc); - nni_mtx_lock(&s->mtx); - } - if (nni_list_empty(&s->reaps) && nni_list_empty(&s->conns)) { - nni_cv_wake(&s->cv); - } nni_mtx_unlock(&s->mtx); } @@ -593,7 +578,8 @@ http_server_fini(nni_http_server *s) http_handler *h; nni_mtx_lock(&s->mtx); - while ((!nni_list_empty(&s->conns)) || (!nni_list_empty(&s->reaps))) { + nni_aio_stop(s->accaio); + while (!nni_list_empty(&s->conns)) { nni_cv_wait(&s->cv); } if (s->tep != NULL) { @@ -604,7 +590,6 @@ http_server_fini(nni_http_server *s) http_handler_fini(h); } nni_mtx_unlock(&s->mtx); - nni_task_wait(&s->cleanup); nni_aio_fini(s->accaio); nni_cv_fini(&s->cv); nni_mtx_fini(&s->mtx); @@ -634,10 +619,8 @@ http_server_init(nni_http_server **serverp, nng_sockaddr *sa) } nni_mtx_init(&s->mtx); nni_cv_init(&s->cv, &s->mtx); - nni_task_init(NULL, &s->cleanup, http_server_cleanup, s); NNI_LIST_INIT(&s->handlers, http_handler, node); NNI_LIST_INIT(&s->conns, http_sconn, node); - NNI_LIST_INIT(&s->reaps, http_sconn, node); if ((rv = nni_aio_init(&s->accaio, http_server_acccb, s)) != 0) { http_server_fini(s); return (rv); @@ -740,15 +723,16 @@ http_server_stop(nni_http_server *s) nni_plat_tcp_ep_close(s->tep); } - // This marks the server as "shutting down" -- existing - // connections finish their activity and close. - // - // XXX: figure out how to shut down connections that are - // blocked waiting to receive a request. We won't do this for - // upgraded connections... + // Stopping the server is a hard stop -- it aborts any work being + // done by clients. (No graceful shutdown). NNI_LIST_FOREACH (&s->conns, sc) { - sc->close = true; + nni_list_remove(&s->conns, sc); + if (sc->http != NULL) { + nni_http_close(sc->http); + } + http_sconn_fini(sc); } + nni_cv_wake(&s->cv); } void diff --git a/src/supplemental/mbedtls/tls.c b/src/supplemental/mbedtls/tls.c index 94503df5..d64447ac 100644 --- a/src/supplemental/mbedtls/tls.c +++ b/src/supplemental/mbedtls/tls.c @@ -76,11 +76,11 @@ struct nni_tls { bool tls_closed; // upper TLS layer closed bool tcp_closed; // underlying TCP buffer closed uint8_t * sendbuf; // send buffer - int sendlen; // amount of data in send buffer - int sendoff; // offset of start of send data + size_t sendlen; // amount of data in send buffer + size_t sendoff; // offset of start of send data uint8_t * recvbuf; // recv buffer - int recvlen; // amount of data in recv buffer - int recvoff; // offset of start of recv data + size_t recvlen; // amount of data in recv buffer + size_t recvoff; // offset of start of recv data nni_list sends; // upper side sends nni_list recvs; // upper recv aios nni_aio * handshake; // handshake aio (upper) |
