aboutsummaryrefslogtreecommitdiff
path: root/src/transport/tls
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-11-27 14:21:20 -0800
committerGarrett D'Amore <garrett@damore.org>2017-12-26 15:31:53 -0800
commit93db6fe3aaff421d61a15993ba6827b742ab00d1 (patch)
treed4d6372cb5d606ba9bcdb60b88b6271086940895 /src/transport/tls
parentc9bf5a76b0d6aead6ae91af71ada51a17881ac0a (diff)
downloadnng-93db6fe3aaff421d61a15993ba6827b742ab00d1.tar.gz
nng-93db6fe3aaff421d61a15993ba6827b742ab00d1.tar.bz2
nng-93db6fe3aaff421d61a15993ba6827b742ab00d1.zip
fixes #2 Websocket transport
This is a rather large changeset -- it fundamentally adds websocket transport, but as part of this changeset we added a generic framework for both HTTP and websocket. We also made some supporting changes to the core, such as changing the way timeouts work for AIOs and adding additional state keeping for AIOs, and adding a common framework for deferred finalization (to avoid certain kinds of circular deadlocks during resource cleanup). We also invented a new initialization framework so that we can avoid wiring in knowledge about them into the master initialization framework. The HTTP framework is not yet complete, but it is good enough for simple static serving and building additional services on top of -- including websocket. We expect both websocket and HTTP support to evolve considerably, and so these are not part of the public API yet. Property support for the websocket transport (in particular address properties) is still missing, as is support for TLS. The websocket transport here is a bit more robust than the original nanomsg implementation, as it supports multiple sockets listening at the same port sharing the same HTTP server instance, discriminating between them based on URI (and possibly the virtual host). Websocket is enabled by default at present, and work to conditionalize HTTP and websocket further (to minimize bloat) is still pending.
Diffstat (limited to 'src/transport/tls')
-rw-r--r--src/transport/tls/tls.c70
1 files changed, 21 insertions, 49 deletions
diff --git a/src/transport/tls/tls.c b/src/transport/tls/tls.c
index 8dcb3f60..9c794e64 100644
--- a/src/transport/tls/tls.c
+++ b/src/transport/tls/tls.c
@@ -10,7 +10,6 @@
#include <stdbool.h>
#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
#include "core/nng_impl.h"
@@ -498,46 +497,6 @@ nni_tls_pipe_getopt_remaddr(void *arg, void *v, size_t *szp)
return (rv);
}
-static int
-nni_tls_parse_pair(char *pair, char **hostp, char **servp)
-{
- char *host, *serv, *end;
-
- if (pair[0] == '[') {
- host = pair + 1;
- // IP address enclosed ... for IPv6 usually.
- if ((end = strchr(host, ']')) == NULL) {
- return (NNG_EADDRINVAL);
- }
- *end = '\0';
- serv = end + 1;
- if (*serv == ':') {
- serv++;
- } else if (*serv != '\0') {
- return (NNG_EADDRINVAL);
- }
- } else {
- host = pair;
- serv = strchr(host, ':');
- if (serv != NULL) {
- *serv = '\0';
- serv++;
- }
- }
- if ((strlen(host) == 0) || (strcmp(host, "*") == 0)) {
- *hostp = NULL;
- } else {
- *hostp = host;
- }
- if ((serv == NULL) || (strlen(serv) == 0)) {
- *servp = NULL;
- } else {
- *servp = serv;
- }
- // Stash the port in big endian (network) byte order.
- return (0);
-}
-
// Note that the url *must* be in a modifiable buffer.
int
nni_tls_parse_url(char *url, char **lhost, char **lserv, char **rhost,
@@ -555,8 +514,9 @@ nni_tls_parse_url(char *url, char **lhost, char **lserv, char **rhost,
// is the second part.
*h1 = '\0';
h1++;
- if (((rv = nni_tls_parse_pair(h1, rhost, rserv)) != 0) ||
- ((rv = nni_tls_parse_pair(url, lhost, lserv)) != 0)) {
+ if (((rv = nni_tran_parse_host_port(h1, rhost, rserv)) != 0) ||
+ ((rv = nni_tran_parse_host_port(url, lhost, lserv)) !=
+ 0)) {
return (rv);
}
if ((*rserv == NULL) || (*rhost == NULL)) {
@@ -566,7 +526,7 @@ nni_tls_parse_url(char *url, char **lhost, char **lserv, char **rhost,
} else if (mode == NNI_EP_MODE_DIAL) {
*lhost = NULL;
*lserv = NULL;
- if ((rv = nni_tls_parse_pair(url, rhost, rserv)) != 0) {
+ if ((rv = nni_tran_parse_host_port(url, rhost, rserv)) != 0) {
return (rv);
}
if ((*rserv == NULL) || (*rhost == NULL)) {
@@ -577,7 +537,7 @@ nni_tls_parse_url(char *url, char **lhost, char **lserv, char **rhost,
NNI_ASSERT(mode == NNI_EP_MODE_LISTEN);
*rhost = NULL;
*rserv = NULL;
- if ((rv = nni_tls_parse_pair(url, lhost, lserv)) != 0) {
+ if ((rv = nni_tran_parse_host_port(url, lhost, lserv)) != 0) {
return (rv);
}
// We have to have a port to listen on!
@@ -657,9 +617,13 @@ nni_tls_ep_init(void **epp, const char *url, nni_sock *sock, int mode)
return (NNG_EADDRINVAL);
}
+ if ((rv = nni_aio_init(&aio, NULL, NULL)) != 0) {
+ return (rv);
+ }
// Parse the URLs first.
rv = nni_tls_parse_url(buf, &lhost, &lserv, &rhost, &rserv, mode);
if (rv != 0) {
+ nni_aio_fini(aio);
return (rv);
}
if (mode == NNI_EP_MODE_DIAL) {
@@ -672,10 +636,6 @@ nni_tls_ep_init(void **epp, const char *url, nni_sock *sock, int mode)
authmode = NNI_TLS_CONFIG_AUTH_MODE_NONE;
}
- if ((rv = nni_aio_init(&aio, NULL, NULL)) != 0) {
- return (rv);
- }
-
// XXX: arguably we could defer this part to the point we do a bind
// or connect!
@@ -683,7 +643,11 @@ nni_tls_ep_init(void **epp, const char *url, nni_sock *sock, int mode)
aio->a_addr = &rsa;
nni_plat_tcp_resolv(rhost, rserv, NNG_AF_UNSPEC, passive, aio);
nni_aio_wait(aio);
+ nni_strfree(rserv);
if ((rv = nni_aio_result(aio)) != 0) {
+ nni_strfree(rhost);
+ nni_strfree(lhost);
+ nni_strfree(lserv);
nni_aio_fini(aio);
return (rv);
}
@@ -695,7 +659,10 @@ nni_tls_ep_init(void **epp, const char *url, nni_sock *sock, int mode)
aio->a_addr = &lsa;
nni_plat_tcp_resolv(lhost, lserv, NNG_AF_UNSPEC, passive, aio);
nni_aio_wait(aio);
+ nni_strfree(lhost);
+ nni_strfree(lserv);
if ((rv = nni_aio_result(aio)) != 0) {
+ nni_strfree(rhost);
nni_aio_fini(aio);
return (rv);
}
@@ -705,12 +672,14 @@ nni_tls_ep_init(void **epp, const char *url, nni_sock *sock, int mode)
nni_aio_fini(aio);
if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) {
+ nni_strfree(rhost);
return (NNG_ENOMEM);
}
nni_mtx_init(&ep->mtx);
if (nni_strlcpy(ep->addr, url, sizeof(ep->addr)) >= sizeof(ep->addr)) {
NNI_FREE_STRUCT(ep);
+ nni_strfree(rhost);
return (NNG_EADDRINVAL);
}
@@ -718,15 +687,18 @@ nni_tls_ep_init(void **epp, const char *url, nni_sock *sock, int mode)
((rv = nni_tls_config_init(&ep->cfg, tlsmode)) != 0) ||
((rv = nni_tls_config_auth_mode(ep->cfg, authmode)) != 0) ||
((rv = nni_aio_init(&ep->aio, nni_tls_ep_cb, ep)) != 0)) {
+ nni_strfree(rhost);
nni_tls_ep_fini(ep);
return (rv);
}
if ((tlsmode == NNI_TLS_CONFIG_CLIENT) && (rhost != NULL)) {
if ((rv = nni_tls_config_server_name(ep->cfg, rhost)) != 0) {
+ nni_strfree(rhost);
nni_tls_ep_fini(ep);
return (rv);
}
}
+ nni_strfree(rhost);
ep->proto = nni_sock_proto(sock);
ep->authmode = authmode;