summaryrefslogtreecommitdiff
path: root/src/platform/windows/win_tcpdial.c
blob: 1225b560f50b64346fe3d61740496e8dadfd8613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include "core/nng_impl.h"

#ifdef NNG_PLATFORM_WINDOWS

#include "win_tcp.h"

#include <malloc.h>
#include <stdio.h>

int
nni_tcp_dialer_init(nni_tcp_dialer **dp)
{
	nni_tcp_dialer *d;
	int             rv;
	SOCKET          s;
	DWORD           nbytes;
	GUID            guid = WSAID_CONNECTEX;

	if ((d = NNI_ALLOC_STRUCT(d)) == NULL) {
		return (NNG_ENOMEM);
	}
	ZeroMemory(d, sizeof(*d));
	nni_mtx_init(&d->mtx);
	nni_aio_list_init(&d->aios);

	// Create a scratch socket for use with ioctl.
	s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
	if (s == INVALID_SOCKET) {
		rv = nni_win_error(GetLastError());
		nni_tcp_dialer_fini(d);
		return (rv);
	}

	// Look up the function pointer.
	if (WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid,
	        sizeof(guid), &d->connectex, sizeof(d->connectex), &nbytes,
	        NULL, NULL) == SOCKET_ERROR) {
		rv = nni_win_error(GetLastError());
		closesocket(s);
		nni_tcp_dialer_fini(d);
		return (rv);
	}

	closesocket(s);

	*dp = d;
	return (0);
}

void
nni_tcp_dialer_close(nni_tcp_dialer *d)
{
	nni_mtx_lock(&d->mtx);
	if (!d->closed) {
		nni_aio *aio;
		d->closed = true;

		NNI_LIST_FOREACH (&d->aios, aio) {
			nni_tcp_conn *c;

			if ((c = nni_aio_get_prov_extra(aio, 0)) != NULL) {
				c->conn_rv = NNG_ECLOSED;
				CancelIoEx((HANDLE) c->s, &c->conn_io.olpd);
			}
		}
	}
	nni_mtx_unlock(&d->mtx);
}

void
nni_tcp_dialer_fini(nni_tcp_dialer *d)
{
	nni_tcp_dialer_close(d);
	nni_mtx_lock(&d->mtx);
	if (!nni_list_empty(&d->aios)) {
		nni_mtx_unlock(&d->mtx);
		nni_reap(&d->reap, (nni_cb) nni_tcp_dialer_fini, d);
		return;
	}
	nni_mtx_unlock(&d->mtx);

	nni_mtx_fini(&d->mtx);
	NNI_FREE_STRUCT(d);
}

static void
tcp_dial_cancel(nni_aio *aio, void *arg, int rv)
{
	nni_tcp_dialer *d = arg;
	nni_tcp_conn *  c;

	nni_mtx_lock(&d->mtx);
	if ((c = nni_aio_get_prov_extra(aio, 0)) != NULL) {
		if (c->conn_rv == 0) {
			c->conn_rv = rv;
		}
		CancelIoEx((HANDLE) c->s, &c->conn_io.olpd);
	}
	nni_mtx_unlock(&d->mtx);
}

static void
tcp_dial_cb(nni_win_io *io, int rv, size_t cnt)
{
	nni_tcp_conn *  c   = io->ptr;
	nni_tcp_dialer *d   = c->dialer;
	nni_aio *       aio = c->conn_aio;

	NNI_ARG_UNUSED(cnt);

	nni_mtx_lock(&d->mtx);
	if ((aio = c->conn_aio) == NULL) {
		// This should never occur.
		nni_mtx_unlock(&d->mtx);
		return;
	}

	c->conn_aio = NULL;
	nni_aio_set_prov_extra(aio, 0, NULL);
	nni_aio_list_remove(aio);
	if (c->conn_rv != 0) {
		rv = c->conn_rv;
	}
	nni_mtx_unlock(&d->mtx);

	if (rv != 0) {
		nni_tcp_conn_fini(c);
		nni_aio_finish_error(aio, rv);
	} else {
		DWORD yes = 1;
		(void) setsockopt(c->s, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT,
		    (char *) &yes, sizeof(yes));
		nni_aio_set_output(aio, 0, c);
		nni_aio_finish(aio, 0, 0);
	}
}

int
nni_tcp_dialer_set_src_addr(nni_tcp_dialer *d, const nni_sockaddr *sa)
{
	SOCKADDR_STORAGE     ss;
	struct sockaddr_in * sin;
	struct sockaddr_in6 *sin6;
	size_t               sslen;

	if ((sslen = nni_win_nn2sockaddr(&ss, sa)) == 0) {
		return (NNG_EADDRINVAL);
	}
	// Ensure we are either IPv4 or IPv6, and port is not set.  (We
	// do not allow binding to a specific port.)
	switch (ss.ss_family) {
	case AF_INET:
		sin = (void *) &ss;
		if (sin->sin_port != 0) {
			return (NNG_EADDRINVAL);
		}
		break;
	case AF_INET6:
		sin6 = (void *) &ss;
		if (sin6->sin6_port != 0) {
			return (NNG_EADDRINVAL);
		}
		break;
	default:
		return (NNG_EADDRINVAL);
	}
	nni_mtx_lock(&d->mtx);
	if (d->closed) {
		nni_mtx_unlock(&d->mtx);
		return (NNG_ECLOSED);
	}
	d->src    = ss;
	d->srclen = sslen;
	nni_mtx_unlock(&d->mtx);
	return (0);
}

void
nni_tcp_dialer_dial(nni_tcp_dialer *d, const nni_sockaddr *sa, nni_aio *aio)
{
	SOCKET           s;
	SOCKADDR_STORAGE ss;
	int              len;
	nni_tcp_conn *   c;
	int              rv;

	if (nni_aio_begin(aio) != 0) {
		return;
	}

	if ((len = nni_win_nn2sockaddr(&ss, sa)) <= 0) {
		nni_aio_finish_error(aio, NNG_EADDRINVAL);
		return;
	}

	if ((s = socket(ss.ss_family, SOCK_STREAM, 0)) == INVALID_SOCKET) {
		nni_aio_finish_error(aio, nni_win_error(GetLastError()));
		return;
	}

	if ((rv = nni_win_tcp_conn_init(&c, s)) != 0) {
		nni_tcp_conn_fini(c);
		nni_aio_finish_error(aio, rv);
		return;
	}

	c->peername = ss;

	if ((rv = nni_win_io_init(&c->conn_io, tcp_dial_cb, c)) != 0) {
		nni_aio_finish_error(aio, rv);
		return;
	}

	nni_mtx_lock(&d->mtx);
	if (d->closed) {
		nni_mtx_unlock(&d->mtx);
		nni_tcp_conn_fini(c);
		nni_aio_finish_error(aio, NNG_ECLOSED);
		return;
	}

	// Windows ConnectEx requires the socket to be bound
	// first. We just bind to an ephemeral address in the
	// same family, unless a different default was requested.
	if (d->srclen != 0) {
		len = (int) d->srclen;
		memcpy(&c->sockname, &d->src, len);
	} else {
		ZeroMemory(&c->sockname, sizeof(c->sockname));
		c->sockname.ss_family = ss.ss_family;
	}
	if (bind(s, (SOCKADDR *) &c->sockname, len) != 0) {
		rv = nni_win_error(GetLastError());
		nni_mtx_unlock(&d->mtx);
		nni_tcp_conn_fini(c);
		nni_aio_finish_error(aio, rv);
		return;
	}

	c->dialer = d;
	nni_aio_set_prov_extra(aio, 0, c);
	if ((rv = nni_aio_schedule(aio, tcp_dial_cancel, d)) != 0) {
		nni_mtx_unlock(&d->mtx);
		nni_tcp_conn_fini(c);
		nni_aio_finish_error(aio, rv);
		return;
	}
	c->conn_aio = aio;
	nni_aio_list_append(&d->aios, aio);

	// dialing is concurrent.
	if (!d->connectex(s, (struct sockaddr *) &c->peername, len, NULL, 0,
	        NULL, &c->conn_io.olpd)) {
		if ((rv = GetLastError()) != ERROR_IO_PENDING) {
			nni_aio_list_remove(aio);
			nni_mtx_unlock(&d->mtx);

			nni_tcp_conn_fini(c);
			nni_aio_finish_error(aio, rv);
			return;
		}
	}
	nni_mtx_unlock(&d->mtx);
}

#endif // NNG_PLATFORM_WINDOWS