-
-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #844 WebSocket wildcard host errors
fixes #1224 wss fails on IPV6 address This fixes bugs and inconsistencies in the way addresses are handled for HTTP (and consequently websocket). The Host: address line needs to look at numeric IPs and treat wildcards as if they are not specified, and needs to understand the IPv6 address format using brackets (e.g. [::1]:80).
- Loading branch information
Showing
5 changed files
with
305 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Copyright 2019 Staysail Systems, Inc. <[email protected]> | ||
// Copyright 2020 Staysail Systems, Inc. <[email protected]> | ||
// Copyright 2018 Capitar IT Group BV <[email protected]> | ||
// | ||
// This software is supplied under the terms of the MIT License, a | ||
|
@@ -257,8 +257,9 @@ resolv_ip(const char *host, const char *serv, int passive, int family, | |
// NB: must remain valid until this is completed. So we have to | ||
// keep our own copy. | ||
|
||
if (host != NULL && nni_strnlen(host, sizeof(item->name_buf)) >= | ||
sizeof(item->name_buf)) { | ||
if (host != NULL && | ||
nni_strnlen(host, sizeof(item->name_buf)) >= | ||
sizeof(item->name_buf)) { | ||
NNI_FREE_STRUCT(item); | ||
nni_aio_finish_error(aio, NNG_EADDRINVAL); | ||
return; | ||
|
@@ -353,6 +354,110 @@ resolv_worker(void *unused) | |
nni_mtx_unlock(&resolv_mtx); | ||
} | ||
|
||
int | ||
parse_ip(const char *addr, nng_sockaddr *sa, bool want_port) | ||
{ | ||
struct addrinfo hints; | ||
struct addrinfo *results; | ||
int rv; | ||
bool v6 = false; | ||
bool wrapped = false; | ||
char * port; | ||
char * host; | ||
char * buf; | ||
size_t buf_len; | ||
|
||
if (addr == NULL) { | ||
addr = ""; | ||
} | ||
|
||
buf_len = strlen(addr) + 1; | ||
if ((buf = nni_alloc(buf_len)) == NULL) { | ||
return (NNG_ENOMEM); | ||
} | ||
memcpy(buf, addr, buf_len); | ||
host = buf; | ||
if (*host == '[') { | ||
v6 = true; | ||
wrapped = true; | ||
host++; | ||
} else { | ||
char *s; | ||
for (s = host; *s != '\0'; s++) { | ||
if (*s == '.') { | ||
break; | ||
} | ||
if (*s == ':') { | ||
v6 = true; | ||
break; | ||
} | ||
} | ||
} | ||
for (port = host; *port != '\0'; port++) { | ||
if (wrapped) { | ||
if (*port == ']') { | ||
*port++ = '\0'; | ||
wrapped = false; | ||
break; | ||
} | ||
} else if (!v6) { | ||
if (*port == ':') { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (wrapped) { | ||
// Never got the closing bracket. | ||
rv = NNG_EADDRINVAL; | ||
goto done; | ||
} | ||
|
||
if ((!want_port) && (*port != '\0')) { | ||
rv = NNG_EADDRINVAL; | ||
goto done; | ||
} else if (*port == ':') { | ||
*port++ = '\0'; | ||
} | ||
|
||
if (*port == '\0') { | ||
port = "0"; | ||
} | ||
|
||
memset(&hints, 0, sizeof(hints)); | ||
hints.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST | AI_PASSIVE; | ||
if (v6) { | ||
hints.ai_family = AF_INET6; | ||
} | ||
#ifdef AI_ADDRCONFIG | ||
hints.ai_flags |= AI_ADDRCONFIG; | ||
#endif | ||
|
||
rv = getaddrinfo(host, port, &hints, &results); | ||
if ((rv != 0) || (results == NULL)) { | ||
rv = nni_plat_errno(rv); | ||
goto done; | ||
} | ||
nni_posix_sockaddr2nn(sa, (void *) results->ai_addr); | ||
freeaddrinfo(results); | ||
|
||
done: | ||
nni_free(buf, buf_len); | ||
return (rv); | ||
} | ||
|
||
int | ||
nni_parse_ip(const char *addr, nni_sockaddr *sa) | ||
{ | ||
return (parse_ip(addr, sa, false)); | ||
} | ||
|
||
int | ||
nni_parse_ip_port(const char *addr, nni_sockaddr *sa) | ||
{ | ||
return (parse_ip(addr, sa, true)); | ||
} | ||
|
||
int | ||
nni_posix_resolv_sysinit(void) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Copyright 2019 Staysail Systems, Inc. <[email protected]> | ||
// Copyright 2020 Staysail Systems, Inc. <[email protected]> | ||
// Copyright 2018 Capitar IT Group BV <[email protected]> | ||
// | ||
// This software is supplied under the terms of the MIT License, a | ||
|
@@ -316,6 +316,108 @@ resolv_worker(void *notused) | |
nni_mtx_unlock(&resolv_mtx); | ||
} | ||
|
||
int | ||
parse_ip(const char *addr, nng_sockaddr *sa, bool want_port) | ||
{ | ||
struct addrinfo hints; | ||
struct addrinfo *results; | ||
int rv; | ||
bool v6 = false; | ||
bool wrapped = false; | ||
char * port; | ||
char * host; | ||
char * buf; | ||
size_t buf_len; | ||
|
||
if (addr == NULL) { | ||
addr = ""; | ||
} | ||
|
||
buf_len = strlen(addr) + 1; | ||
if ((buf = nni_alloc(buf_len)) == NULL) { | ||
return (NNG_ENOMEM); | ||
} | ||
memcpy(buf, addr, buf_len); | ||
host = buf; | ||
if (*host == '[') { | ||
v6 = true; | ||
wrapped = true; | ||
host++; | ||
} else { | ||
char *s; | ||
for (s = host; *s != '\0'; s++) { | ||
if (*s == '.') { | ||
break; | ||
} | ||
if (*s == ':') { | ||
v6 = true; | ||
break; | ||
} | ||
} | ||
} | ||
for (port = host; *port != '\0'; port++) { | ||
if (wrapped) { | ||
if (*port == ']') { | ||
*port++ = '\0'; | ||
wrapped = false; | ||
break; | ||
} | ||
} else if (!v6) { | ||
if (*port == ':') { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (wrapped) { | ||
// Never got the closing bracket. | ||
rv = NNG_EADDRINVAL; | ||
goto done; | ||
} | ||
|
||
if ((!want_port) && (*port != '\0')) { | ||
rv = NNG_EADDRINVAL; | ||
goto done; | ||
} else if (*port == ':') { | ||
*port++ = '\0'; | ||
} | ||
|
||
if (*port == '\0') { | ||
port = "0"; | ||
} | ||
|
||
memset(&hints, 0, sizeof(hints)); | ||
hints.ai_flags = | ||
AI_ADDRCONFIG | AI_NUMERICSERV | AI_NUMERICHOST | AI_PASSIVE; | ||
if (v6) { | ||
hints.ai_family = AF_INET6; | ||
} | ||
|
||
rv = getaddrinfo(host, port, &hints, &results); | ||
if ((rv != 0) || (results == NULL)) { | ||
rv = nni_win_error(rv); | ||
goto done; | ||
} | ||
nni_win_sockaddr2nn(sa, (void *) results->ai_addr); | ||
freeaddrinfo(results); | ||
|
||
done: | ||
nni_free(buf, buf_len); | ||
return (rv); | ||
} | ||
|
||
int | ||
nni_parse_ip(const char *addr, nni_sockaddr *sa) | ||
{ | ||
return (parse_ip(addr, sa, false)); | ||
} | ||
|
||
int | ||
nni_parse_ip_port(const char *addr, nni_sockaddr *sa) | ||
{ | ||
return (parse_ip(addr, sa, true)); | ||
} | ||
|
||
int | ||
nni_win_resolv_sysinit(void) | ||
{ | ||
|
Oops, something went wrong.