zhangmeng
2024-04-22 16935f4aebffdd1b6580b844391a0aa0f4f3012b
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//
// Copyright 2020 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.
//
 
// Silence complaints about inet_addr()
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1
 
#include "core/nng_impl.h"
 
#ifdef NNG_PLATFORM_WINDOWS
 
#include <malloc.h>
#include <stdio.h>
 
struct nni_plat_udp {
    SOCKET           s;
    nni_mtx          lk;
    nni_cv           cv;
    nni_list         rxq;
    nni_win_io       rxio;
    int              cancel_rv;
    bool             closed;
    SOCKADDR_STORAGE rxsa;
    int              rxsalen;
};
 
static void udp_recv_cb(nni_win_io *, int, size_t);
static void udp_recv_start(nni_plat_udp *);
 
// nni_plat_udp_open initializes a UDP socket, binding to the local
// address specified specified.
int
nni_plat_udp_open(nni_plat_udp **udpp, nni_sockaddr *sa)
{
    nni_plat_udp *   u;
    SOCKADDR_STORAGE ss;
    int              sslen;
    DWORD            no;
    int              rv;
 
    if ((sslen = nni_win_nn2sockaddr(&ss, sa)) < 0) {
        return (NNG_EADDRINVAL);
    }
 
    if ((u = NNI_ALLOC_STRUCT(u)) == NULL) {
        return (NNG_ENOMEM);
    }
    nni_aio_list_init(&u->rxq);
    nni_mtx_init(&u->lk);
    nni_cv_init(&u->cv, &u->lk);
 
    u->s = socket(ss.ss_family, SOCK_DGRAM, IPPROTO_UDP);
    if (u->s == INVALID_SOCKET) {
        rv = nni_win_error(GetLastError());
        nni_plat_udp_close(u);
        return (rv);
    }
    // Don't inherit the handle (CLOEXEC really).
    SetHandleInformation((HANDLE) u->s, HANDLE_FLAG_INHERIT, 0);
    no = 0;
    (void) setsockopt(
        u->s, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &no, sizeof(no));
 
    if (((rv = nni_win_io_init(&u->rxio, udp_recv_cb, u)) != 0) ||
        ((rv = nni_win_io_register((HANDLE) u->s)) != 0)) {
        nni_plat_udp_close(u);
        return (rv);
    }
 
    // Bind the local address
    if (bind(u->s, (struct sockaddr *) &ss, sslen) == SOCKET_ERROR) {
        rv = nni_win_error(GetLastError());
        nni_plat_udp_close(u);
        return (rv);
    }
 
    *udpp = u;
    return (rv);
}
 
// nni_plat_udp_close closes the underlying UDP socket.
void
nni_plat_udp_close(nni_plat_udp *u)
{
    nni_mtx_lock(&u->lk);
    u->closed = true;
    if (!nni_list_empty(&u->rxq)) {
        CancelIoEx((HANDLE) u->s, &u->rxio.olpd);
    }
    while (!nni_list_empty(&u->rxq)) {
        nni_cv_wait(&u->cv);
    }
    nni_mtx_unlock(&u->lk);
 
    if (u->s != INVALID_SOCKET) {
        closesocket(u->s);
    }
 
    nni_win_io_fini(&u->rxio);
    nni_mtx_fini(&u->lk);
    nni_cv_fini(&u->cv);
    NNI_FREE_STRUCT(u);
}
 
// nni_plat_udp_send sends the data in the aio to the the
// destination specified in the nni_aio.  The iovs are the UDP payload.
void
nni_plat_udp_send(nni_plat_udp *u, nni_aio *aio)
{
    SOCKADDR_STORAGE to;
    int              tolen;
    nng_sockaddr *   sa;
    unsigned         naiov;
    nni_iov *        aiov;
    WSABUF *         iov;
    int              rv;
    DWORD            nsent;
 
    if (nni_aio_begin(aio) != 0) {
        return;
    }
    sa = nni_aio_get_input(aio, 0);
    if ((tolen = nni_win_nn2sockaddr(&to, sa)) < 0) {
        nni_aio_finish_error(aio, NNG_EADDRINVAL);
        return;
    }
 
    nni_aio_get_iov(aio, &naiov, &aiov);
    iov = _malloca(sizeof(*iov) * naiov);
 
    // NB: UDP send runs "quickly" on Windows, without any need for
    // a blocking or asynchronous operation. If the message can't be
    // sent immediately (or queued for it), then it is dropped.
 
    nni_mtx_lock(&u->lk);
    if ((u->s == INVALID_SOCKET) || u->closed) {
        nni_mtx_unlock(&u->lk);
        nni_aio_finish_error(aio, NNG_ECLOSED);
        _freea(iov);
        return;
    }
 
    // Put the AIOs in Windows form.
    for (unsigned i = 0; i < naiov; i++) {
        iov[i].buf = aiov[i].iov_buf;
        iov[i].len = (ULONG) aiov[i].iov_len;
    }
 
    // We can use a "non-overlapping" send; there is little point in
    // handling UDP send completions asynchronously.
    rv = WSASendTo(u->s, iov, (DWORD) naiov, &nsent, 0,
        (struct sockaddr *) &to, tolen, NULL, NULL);
 
    if (rv == SOCKET_ERROR) {
        rv    = nni_win_error(GetLastError());
        nsent = 0;
    }
    nni_mtx_unlock(&u->lk);
 
    _freea(iov);
 
    nni_aio_finish(aio, rv, nsent);
 
    return;
}
 
static void
udp_recv_cancel(nni_aio *aio, void *arg, int rv)
{
    nni_plat_udp *u = arg;
    nni_mtx_lock(&u->lk);
    if (aio == nni_list_first(&u->rxq)) {
        u->cancel_rv = rv;
        CancelIoEx((HANDLE) u->s, &u->rxio.olpd);
    } else if (nni_aio_list_active(aio)) {
        nni_aio_list_remove(aio);
        nni_aio_finish_error(aio, rv);
        nni_cv_wake(&u->cv);
    }
    nni_mtx_unlock(&u->lk);
}
 
static void
udp_recv_cb(nni_win_io *io, int rv, size_t num)
{
    nni_plat_udp *u = io->ptr;
    nni_sockaddr *sa;
    nni_aio *     aio;
 
    nni_mtx_lock(&u->lk);
    if ((aio = nni_list_first(&u->rxq)) == NULL) {
        // Should indicate that it was closed.
        nni_mtx_unlock(&u->lk);
        return;
    }
    if (u->cancel_rv != 0) {
        rv           = u->cancel_rv;
        u->cancel_rv = 0;
    }
 
    // convert address from Windows form...
    if ((sa = nni_aio_get_input(aio, 0)) != NULL) {
        if (nni_win_sockaddr2nn(sa, &u->rxsa) != 0) {
            rv  = NNG_EADDRINVAL;
            num = 0;
        }
    }
 
    nni_aio_list_remove(aio);
    udp_recv_start(u);
    nni_mtx_unlock(&u->lk);
 
    nni_aio_finish_sync(aio, rv, num);
}
 
static void
udp_recv_start(nni_plat_udp *u)
{
    int      rv;
    DWORD    flags;
    nni_iov *aiov;
    unsigned naiov;
    WSABUF * iov;
    nni_aio *aio;
 
    if ((u->s == INVALID_SOCKET) || (u->closed)) {
        while ((aio = nni_list_first(&u->rxq)) != NULL) {
            nni_list_remove(&u->rxq, aio);
            nni_aio_finish_error(aio, NNG_ECLOSED);
        }
        nni_cv_wake(&u->cv);
        return;
    }
 
again:
    if ((aio = nni_list_first(&u->rxq)) == NULL) {
        return;
    }
 
    u->rxsalen = sizeof(SOCKADDR_STORAGE);
    nni_aio_get_iov(aio, &naiov, &aiov);
 
    // This is a stack allocation- it should always succeed - or
    // throw an exception if there is not sufficient stack space.
    // (Turns out it can allocate from the heap, but same semantics.)
    iov = _malloca(sizeof(*iov) * naiov);
 
    // Put the AIOs in Windows form.
    for (unsigned i = 0; i < naiov; i++) {
        iov[i].buf = aiov[i].iov_buf;
        iov[i].len = (ULONG) aiov[i].iov_len;
    }
 
    // Note that the IOVs for the event were prepared on entry
    // already. The actual aio's iov array we don't touch.
    flags = 0;
 
    rv = WSARecvFrom(u->s, iov, (DWORD) naiov, NULL, &flags,
        (struct sockaddr *) &u->rxsa, &u->rxsalen, &u->rxio.olpd, NULL);
 
    _freea(iov);
 
    if ((rv == SOCKET_ERROR) &&
        ((rv = GetLastError()) != ERROR_IO_PENDING)) {
        // Synchronous failure.
        nni_aio_finish_error(aio, nni_win_error(rv));
        goto again;
    }
}
 
// nni_plat_udp_pipe_recv recvs a message, storing it in the iovs
// from the UDP payload.  If the UDP payload will not fit, then
// NNG_EMSGSIZE results.
void
nni_plat_udp_recv(nni_plat_udp *u, nni_aio *aio)
{
    int rv;
    if (nni_aio_begin(aio) != 0) {
        return;
    }
    nni_mtx_lock(&u->lk);
    if (u->closed) {
        nni_mtx_unlock(&u->lk);
        nni_aio_finish_error(aio, NNG_ECLOSED);
        return;
    }
    if ((rv = nni_aio_schedule(aio, udp_recv_cancel, u)) != 0) {
        nni_mtx_unlock(&u->lk);
        nni_aio_finish_error(aio, rv);
        return;
    }
    nni_list_append(&u->rxq, aio);
    if (nni_list_first(&u->rxq) == aio) {
        udp_recv_start(u);
    }
    nni_mtx_unlock(&u->lk);
}
 
int
nni_plat_udp_sockname(nni_plat_udp *udp, nni_sockaddr *sa)
{
    SOCKADDR_STORAGE ss;
    int              sz;
 
    sz = sizeof(ss);
    if (getsockname(udp->s, (SOCKADDR *) &ss, &sz) < 0) {
        return (nni_win_error(GetLastError()));
    }
    return (nni_win_sockaddr2nn(sa, &ss));
}
 
int
nni_win_udp_sysinit(void)
{
    WSADATA data;
    if (WSAStartup(MAKEWORD(2, 2), &data) != 0) {
        NNI_ASSERT(LOBYTE(data.wVersion) == 2);
        NNI_ASSERT(HIBYTE(data.wVersion) == 2);
        return (nni_win_error(GetLastError()));
    }
    return (0);
}
 
void
nni_win_udp_sysfini(void)
{
    WSACleanup();
}
 
#endif // NNG_PLATFORM_WINDOWS