aboutsummaryrefslogtreecommitdiff
path: root/src/platform/windows/win_net.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-08-20 10:45:27 -0700
committerGarrett D'Amore <garrett@damore.org>2017-08-21 07:18:31 -0700
commit75adda86be49e6839e50443f0bae5875d9910897 (patch)
treee29b8b449c863be01e5f02945940dc390b239462 /src/platform/windows/win_net.c
parent6305e16ab64e42fd9791819d416a6e3534439b0b (diff)
downloadnng-75adda86be49e6839e50443f0bae5875d9910897.tar.gz
nng-75adda86be49e6839e50443f0bae5875d9910897.tar.bz2
nng-75adda86be49e6839e50443f0bae5875d9910897.zip
fixes #41 Move DNS out of tcp transport
This moves the DNS related functionality into common code, and also removes all the URL parsing stuff out of the platform specific code and into the transports. Now the transports just take sockaddr's on initialization. (We may want to move this until later.) We also add UDP resolution as another separate API.
Diffstat (limited to 'src/platform/windows/win_net.c')
-rw-r--r--src/platform/windows/win_net.c85
1 files changed, 19 insertions, 66 deletions
diff --git a/src/platform/windows/win_net.c b/src/platform/windows/win_net.c
index dee9b745..07c30e11 100644
--- a/src/platform/windows/win_net.c
+++ b/src/platform/windows/win_net.c
@@ -25,7 +25,6 @@ struct nni_plat_tcp_ep {
SOCKET acc_s;
nni_win_event con_ev;
nni_win_event acc_ev;
- int mode;
int started;
int bound;
@@ -285,19 +284,12 @@ nni_plat_tcp_pipe_fini(nni_plat_tcp_pipe *pipe)
NNI_FREE_STRUCT(pipe);
}
-extern int nni_tcp_parse_url(char *, char **, char **, char **, char **);
-
int
-nni_plat_tcp_ep_init(nni_plat_tcp_ep **epp, const char *url, int mode)
+nni_plat_tcp_ep_init(nni_plat_tcp_ep **epp, const nni_sockaddr *lsa,
+ const nni_sockaddr *rsa, int mode)
{
- char buf[NNG_MAXADDRLEN];
nni_plat_tcp_ep *ep;
- char * rhost;
- char * rserv;
- char * lhost;
- char * lserv;
int rv;
- nni_aio aio;
SOCKET s;
DWORD nbytes;
GUID guid1 = WSAID_CONNECTEX;
@@ -308,55 +300,13 @@ nni_plat_tcp_ep_init(nni_plat_tcp_ep **epp, const char *url, int mode)
}
ZeroMemory(ep, sizeof(ep));
- ep->mode = mode;
- ep->s = INVALID_SOCKET;
-
- nni_aio_init(&aio, NULL, NULL);
+ ep->s = INVALID_SOCKET;
- snprintf(buf, sizeof(buf), "%s", url);
- if (mode == NNI_EP_MODE_DIAL) {
- rv = nni_tcp_parse_url(buf, &rhost, &rserv, &lhost, &lserv);
- if (rv != 0) {
- goto fail;
- }
- // Have to ahve a remote destination.
- if ((rhost == NULL) || (rserv == NULL)) {
- rv = NNG_EADDRINVAL;
- goto fail;
- }
- } else {
- rv = nni_tcp_parse_url(buf, &lhost, &lserv, &rhost, &rserv);
- if (rv != 0) {
- goto fail;
- }
- // Remote destination makes no sense when listening.
- if ((rhost != NULL) || (rserv != NULL)) {
- rv = NNG_EADDRINVAL;
- goto fail;
- }
- if (lserv == NULL) {
- // missing port to listen on!
- rv = NNG_EADDRINVAL;
- goto fail;
- }
+ if (rsa->s_un.s_family != NNG_AF_UNSPEC) {
+ ep->remlen = nni_win_tcp_addr(&ep->remaddr, rsa);
}
-
- if ((rserv != NULL) || (rhost != NULL)) {
- nni_plat_tcp_resolv(rhost, rserv, NNG_AF_INET6, 0, &aio);
- nni_aio_wait(&aio);
- if ((rv = nni_aio_result(&aio)) != 0) {
- goto fail;
- }
- ep->remlen = nni_win_tcp_addr(&ep->remaddr, &aio.a_addrs[0]);
- }
-
- if ((lserv != NULL) || (lhost != NULL)) {
- nni_plat_tcp_resolv(lhost, lserv, NNG_AF_INET6, 1, &aio);
- nni_aio_wait(&aio);
- if ((rv = nni_aio_result(&aio)) != 0) {
- goto fail;
- }
- ep->loclen = nni_win_tcp_addr(&ep->locaddr, &aio.a_addrs[0]);
+ if (lsa->s_un.s_family != NNG_AF_UNSPEC) {
+ ep->loclen = nni_win_tcp_addr(&ep->locaddr, lsa);
}
// Create a scratch socket for use with ioctl.
@@ -392,7 +342,6 @@ nni_plat_tcp_ep_init(nni_plat_tcp_ep **epp, const char *url, int mode)
goto fail;
}
- nni_aio_fini(&aio);
*epp = ep;
return (0);
@@ -401,7 +350,6 @@ fail:
closesocket(s);
}
nni_plat_tcp_ep_fini(ep);
- nni_aio_fini(&aio);
return (rv);
}
@@ -433,14 +381,11 @@ nni_win_tcp_listen(nni_plat_tcp_ep *ep)
BOOL yes;
SOCKET s;
- if (ep->mode != NNI_EP_MODE_LISTEN) {
- return (NNG_EINVAL);
- }
if (ep->started) {
return (NNG_EBUSY);
}
- s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
+ s = socket(ep->locaddr.ss_family, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET) {
rv = nni_win_error(GetLastError());
goto fail;
@@ -598,7 +543,7 @@ nni_win_tcp_con_finish(nni_win_event *evt, nni_aio *aio)
s = ep->s;
ep->s = INVALID_SOCKET;
- // The socket was already registere with the IOCP.
+ // The socket was already registered with the IOCP.
if (((rv = evt->status) != 0) ||
((rv = nni_win_tcp_pipe_init(&pipe, s)) != 0)) {
@@ -621,8 +566,15 @@ nni_win_tcp_con_start(nni_win_event *evt, nni_aio *aio)
SOCKADDR_STORAGE bss;
int len;
int rv;
+ int family;
- s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
+ if (ep->loclen > 0) {
+ family = ep->locaddr.ss_family;
+ } else {
+ family = ep->remaddr.ss_family;
+ }
+
+ s = socket(family, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET) {
evt->status = nni_win_error(GetLastError());
evt->count = 0;
@@ -632,7 +584,7 @@ nni_win_tcp_con_start(nni_win_event *evt, nni_aio *aio)
nni_win_tcp_sockinit(s);
// Windows ConnectEx requires the socket to be bound first.
- if (ep->loclen != 0) {
+ if (ep->loclen > 0) {
bss = ep->locaddr;
len = ep->loclen;
} else {
@@ -644,6 +596,7 @@ nni_win_tcp_con_start(nni_win_event *evt, nni_aio *aio)
evt->status = nni_win_error(GetLastError());
evt->count = 0;
closesocket(s);
+
return (1);
}
// Register with the I/O completion port so we can get the