aboutsummaryrefslogtreecommitdiff
path: root/src/core/transport.c
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/core/transport.c
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/core/transport.c')
-rw-r--r--src/core/transport.c72
1 files changed, 71 insertions, 1 deletions
diff --git a/src/core/transport.c b/src/core/transport.c
index 359b03fd..31da773f 100644
--- a/src/core/transport.c
+++ b/src/core/transport.c
@@ -13,6 +13,7 @@
#include "transport/ipc/ipc.h"
#include "transport/tcp/tcp.h"
#include "transport/tls/tls.h"
+#include "transport/ws/websocket.h"
#include "transport/zerotier/zerotier.h"
#include <stdio.h>
@@ -105,6 +106,72 @@ nni_tran_find(const char *addr)
return (NULL);
}
+// nni_tran_parse_host_port is a convenience routine to parse the host portion
+// of a URL (which includes a DNS name or IP address and an optional service
+// name or port, separated by a colon) into its host and port name parts. It
+// understands IPv6 address literals when surrounded by brackets ([]).
+// If either component is empty, then NULL is passed back for the value,
+// otherwise a string suitable for freeing with nni_strfree is supplied.
+int
+nni_tran_parse_host_port(const char *pair, char **hostp, char **portp)
+{
+ const char *hstart;
+ const char *pstart;
+ char * host;
+ char * port;
+ size_t hlen, plen;
+
+ if (pair[0] == '[') {
+ hstart = pair + 1;
+ hlen = 0;
+ while (hstart[hlen] != ']') {
+ if (hstart[hlen] == '\0') {
+ return (NNG_EADDRINVAL);
+ }
+ hlen++;
+ }
+ pstart = hstart + hlen + 1; // skip over the trailing ']'
+ } else {
+ // Normal thing.
+ hstart = pair;
+ hlen = 0;
+ while ((hstart[hlen] != ':') && (hstart[hlen] != '\0')) {
+ hlen++;
+ }
+ pstart = hstart + hlen;
+ }
+ if (pstart[0] == ':') {
+ pstart++;
+ }
+ plen = strlen(pstart);
+
+ host = NULL;
+ if (hostp) {
+ if ((hlen > 1) || ((hlen == 1) && (*hstart != '*'))) {
+ if ((host = nni_alloc(hlen + 1)) == NULL) {
+ return (NNG_ENOMEM);
+ }
+ memcpy(host, hstart, hlen);
+ host[hlen] = '\0';
+ }
+ }
+
+ port = NULL;
+ if ((plen != 0) && (portp)) {
+ if ((port = nni_strdup(pstart)) == NULL) {
+ nni_strfree(host);
+ return (NNG_ENOMEM);
+ }
+ }
+ if (hostp) {
+ *hostp = host;
+ }
+ if (portp) {
+ *portp = port;
+ }
+ return (0);
+}
+
int
nni_tran_chkopt(const char *name, const void *v, size_t sz)
{
@@ -154,9 +221,12 @@ static nni_tran_ctor nni_tran_ctors[] = {
#ifdef NNG_HAVE_TLS
nng_tls_register,
#endif
-#ifdef NNI_HAVE_ZEROTIER
+#ifdef NNG_HAVE_ZEROTIER
nng_zt_register,
#endif
+#ifdef NNG_HAVE_WEBSOCKET
+ nng_ws_register,
+#endif
NULL,
};