diff options
| author | Garrett D'Amore <garrett@damore.org> | 2025-10-25 11:43:59 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2025-10-25 20:11:51 -0700 |
| commit | cc419cf01d9c060a3bd3fc318f9b9bc9e736dae9 (patch) | |
| tree | 2f26cd782a54bed35e03b451003deee5df701983 /src/platform/windows/win_tcpdial.c | |
| parent | ebc479a61b8f2d2f3a6d846d18debfb9022e6010 (diff) | |
| download | nng-cc419cf01d9c060a3bd3fc318f9b9bc9e736dae9.tar.gz nng-cc419cf01d9c060a3bd3fc318f9b9bc9e736dae9.tar.bz2 nng-cc419cf01d9c060a3bd3fc318f9b9bc9e736dae9.zip | |
Add stream direct address functions for socket addresses.
This is going to be used to facilitate debugging, and eliminate some
inconveniences around these things. We plan to move the pipe functions
to use these directly, hopefully moving away from the pipe_getopt hack.
(The transport API will need to grow these. For now this is just the
streams.)
Diffstat (limited to 'src/platform/windows/win_tcpdial.c')
| -rw-r--r-- | src/platform/windows/win_tcpdial.c | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/src/platform/windows/win_tcpdial.c b/src/platform/windows/win_tcpdial.c index 72e4f8a7..a3d64869 100644 --- a/src/platform/windows/win_tcpdial.c +++ b/src/platform/windows/win_tcpdial.c @@ -9,11 +9,10 @@ // found online at https://opensource.org/licenses/MIT. // -#include "core/nng_impl.h" +#include "../../core/nng_impl.h" #include "win_tcp.h" -#include <malloc.h> #include <stdio.h> struct nni_tcp_dialer { @@ -145,8 +144,9 @@ tcp_dial_cb(nni_win_io *io, int rv, size_t cnt) nng_stream_free(&c->ops); nni_aio_finish_error(aio, rv); } else { - DWORD yes = 1; - int len; + DWORD yes = 1; + int len; + SOCKADDR_STORAGE sa; (void) setsockopt(c->s, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, (char *) &yes, sizeof(yes)); @@ -157,8 +157,13 @@ tcp_dial_cb(nni_win_io *io, int rv, size_t cnt) (void) setsockopt( c->s, IPPROTO_TCP, TCP_NODELAY, (char *) &nd, sizeof(nd)); - len = sizeof(SOCKADDR_STORAGE); - (void) getsockname(c->s, (SOCKADDR *) &c->sockname, &len); + len = sizeof(sa); + (void) getsockname(c->s, (SOCKADDR *) &sa, &len); + nni_win_sockaddr2nn(&c->sockname, &sa, len); + + len = sizeof(sa); + (void) getpeername(c->s, (SOCKADDR *) &sa, &len); + nni_win_sockaddr2nn(&c->peername, &sa, len); nni_aio_set_output(aio, 0, c); nni_aio_finish(aio, 0, 0); @@ -169,19 +174,21 @@ void nni_tcp_dial(nni_tcp_dialer *d, const nni_sockaddr *sa, nni_aio *aio) { SOCKET s; - SOCKADDR_STORAGE ss; + SOCKADDR_STORAGE peername; + SOCKADDR_STORAGE sockname; int len; nni_tcp_conn *c; int rv; nni_aio_reset(aio); - if ((len = nni_win_nn2sockaddr(&ss, sa)) <= 0) { + if ((len = nni_win_nn2sockaddr(&peername, sa)) <= 0) { nni_aio_finish_error(aio, NNG_EADDRINVAL); return; } - if ((s = socket(ss.ss_family, SOCK_STREAM, 0)) == INVALID_SOCKET) { + if ((s = socket(peername.ss_family, SOCK_STREAM, 0)) == + INVALID_SOCKET) { nni_aio_finish_error(aio, nni_win_error(GetLastError())); return; } @@ -192,8 +199,6 @@ nni_tcp_dial(nni_tcp_dialer *d, const nni_sockaddr *sa, nni_aio *aio) return; } - c->peername = ss; - nni_win_io_init(&c->conn_io, tcp_dial_cb, c); nni_mtx_lock(&d->mtx); @@ -209,12 +214,13 @@ nni_tcp_dial(nni_tcp_dialer *d, const nni_sockaddr *sa, nni_aio *aio) // same family, unless a different default was requested. if (d->srclen != 0) { len = (int) d->srclen; + memcpy(&sockname, &d->src, len); memcpy(&c->sockname, &d->src, len); } else { - ZeroMemory(&c->sockname, sizeof(c->sockname)); - c->sockname.ss_family = ss.ss_family; + ZeroMemory(&sockname, sizeof(sockname)); + sockname.ss_family = peername.ss_family; } - if (bind(s, (SOCKADDR *) &c->sockname, len) != 0) { + if (bind(s, (SOCKADDR *) &sockname, len) != 0) { rv = nni_win_error(GetLastError()); nni_mtx_unlock(&d->mtx); nng_stream_free(&c->ops); @@ -222,6 +228,8 @@ nni_tcp_dial(nni_tcp_dialer *d, const nni_sockaddr *sa, nni_aio *aio) return; } + nni_win_sockaddr2nn(&c->sockname, &sockname, sizeof(sockname)); + if (!nni_aio_start(aio, tcp_dial_cancel, d)) { nni_mtx_unlock(&d->mtx); nng_stream_free(&c->ops); @@ -234,7 +242,7 @@ nni_tcp_dial(nni_tcp_dialer *d, const nni_sockaddr *sa, nni_aio *aio) // dialing is concurrent. if (!nni_win_connectex( - s, (SOCKADDR *) &c->peername, len, &c->conn_io.olpd)) { + s, (SOCKADDR *) &peername, len, &c->conn_io.olpd)) { if ((rv = GetLastError()) != ERROR_IO_PENDING) { nni_aio_list_remove(aio); nni_mtx_unlock(&d->mtx); |
