diff options
Diffstat (limited to 'src/core/transport.c')
| -rw-r--r-- | src/core/transport.c | 72 |
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, }; |
