zhangmeng
2021-12-10 5990bac28af438a914165441b3c33a370b320d16
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2019 Devolutions <info@devolutions.net>
//
// 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 <stdint.h>
#include <string.h>
 
#include <nng/nng.h>
 
#include "core/nng_impl.h"
#include "core/tcp.h"
 
typedef struct {
    nng_stream_dialer ops;
    char *            host;
    char *            port;
    int               af; // address family
    bool              closed;
    nng_sockaddr      sa;
    nni_tcp_dialer *  d;      // platform dialer implementation
    nni_aio *         resaio; // resolver aio
    nni_aio *         conaio; // platform connection aio
    nni_list          conaios;
    nni_mtx           mtx;
} tcp_dialer;
 
static void
tcp_dial_cancel(nni_aio *aio, void *arg, int rv)
{
    tcp_dialer *d = arg;
 
    nni_mtx_lock(&d->mtx);
    if (nni_aio_list_active(aio)) {
        nni_aio_list_remove(aio);
        nni_aio_finish_error(aio, rv);
 
        if (nni_list_empty(&d->conaios)) {
            nni_aio_abort(d->conaio, NNG_ECANCELED);
            nni_aio_abort(d->resaio, NNG_ECANCELED);
        }
    }
    nni_mtx_unlock(&d->mtx);
}
 
static void
tcp_dial_start_next(tcp_dialer *d)
{
    if (nni_list_empty(&d->conaios)) {
        return;
    }
    nni_resolv_ip(d->host, d->port, d->af, false, &d->sa, d->resaio);
}
 
static void
tcp_dial_res_cb(void *arg)
{
    tcp_dialer *d = arg;
    nni_aio *   aio;
    int         rv;
 
    nni_mtx_lock(&d->mtx);
    if (d->closed || ((aio = nni_list_first(&d->conaios)) == NULL)) {
        // ignore this.
        while ((aio = nni_list_first(&d->conaios)) != NULL) {
            nni_list_remove(&d->conaios, aio);
            nni_aio_finish_error(aio, NNG_ECLOSED);
        }
        nni_mtx_unlock(&d->mtx);
        return;
    }
 
    if ((rv = nni_aio_result(d->resaio)) != 0) {
        nni_list_remove(&d->conaios, aio);
        nni_aio_finish_error(aio, rv);
 
        // try DNS again for next connection...
        tcp_dial_start_next(d);
 
    } else {
        nni_tcp_dial(d->d, &d->sa, d->conaio);
    }
 
    nni_mtx_unlock(&d->mtx);
}
 
static void
tcp_dial_con_cb(void *arg)
{
    tcp_dialer *d = arg;
    nng_aio *   aio;
    int         rv;
 
    nni_mtx_lock(&d->mtx);
    rv = nni_aio_result(d->conaio);
    if ((d->closed) || ((aio = nni_list_first(&d->conaios)) == NULL)) {
        if (rv == 0) {
            // Make sure we discard the underlying connection.
            nng_stream_free(nni_aio_get_output(d->conaio, 0));
            nni_aio_set_output(d->conaio, 0, NULL);
        }
        nni_mtx_unlock(&d->mtx);
        return;
    }
    nni_list_remove(&d->conaios, aio);
    if (rv != 0) {
        nni_aio_finish_error(aio, rv);
    } else {
        nni_aio_set_output(aio, 0, nni_aio_get_output(d->conaio, 0));
        nni_aio_finish(aio, 0, 0);
    }
 
    tcp_dial_start_next(d);
    nni_mtx_unlock(&d->mtx);
}
 
static void
tcp_dialer_close(void *arg)
{
    tcp_dialer *d = arg;
    nni_aio *   aio;
    nni_mtx_lock(&d->mtx);
    d->closed = true;
    while ((aio = nni_list_first(&d->conaios)) != NULL) {
        nni_list_remove(&d->conaios, aio);
        nni_aio_finish_error(aio, NNG_ECLOSED);
    }
    nni_tcp_dialer_close(d->d);
    nni_mtx_unlock(&d->mtx);
}
 
static void
tcp_dialer_free(void *arg)
{
    tcp_dialer *d = arg;
 
    if (d == NULL) {
        return;
    }
 
    nni_aio_stop(d->resaio);
    nni_aio_stop(d->conaio);
    nni_aio_free(d->resaio);
    nni_aio_free(d->conaio);
 
    if (d->d != NULL) {
        nni_tcp_dialer_close(d->d);
        nni_tcp_dialer_fini(d->d);
    }
    nni_mtx_fini(&d->mtx);
    nni_strfree(d->host);
    nni_strfree(d->port);
    NNI_FREE_STRUCT(d);
}
 
static void
tcp_dialer_dial(void *arg, nng_aio *aio)
{
    tcp_dialer *d = arg;
    int         rv;
    if (nni_aio_begin(aio) != 0) {
        return;
    }
    nni_mtx_lock(&d->mtx);
    if (d->closed) {
        nni_mtx_unlock(&d->mtx);
        nni_aio_finish_error(aio, NNG_ECLOSED);
        return;
    }
    if ((rv = nni_aio_schedule(aio, tcp_dial_cancel, d)) != 0) {
        nni_mtx_unlock(&d->mtx);
        nni_aio_finish_error(aio, rv);
        return;
    }
    nni_list_append(&d->conaios, aio);
    if (nni_list_first(&d->conaios) == aio) {
        tcp_dial_start_next(d);
    }
    nni_mtx_unlock(&d->mtx);
}
 
static int
tcp_dialer_get(
    void *arg, const char *name, void *buf, size_t *szp, nni_type t)
{
    tcp_dialer *d = arg;
    return (nni_tcp_dialer_get(d->d, name, buf, szp, t));
}
 
static int
tcp_dialer_set(
    void *arg, const char *name, const void *buf, size_t sz, nni_type t)
{
    tcp_dialer *d = arg;
    return (nni_tcp_dialer_set(d->d, name, buf, sz, t));
}
 
static int
tcp_dialer_alloc(tcp_dialer **dp)
{
    int         rv;
    tcp_dialer *d;
 
    if ((d = NNI_ALLOC_STRUCT(d)) == NULL) {
        return (NNG_ENOMEM);
    }
 
    nni_mtx_init(&d->mtx);
    nni_aio_list_init(&d->conaios);
 
    if (((rv = nni_aio_alloc(&d->resaio, tcp_dial_res_cb, d)) != 0) ||
        ((rv = nni_aio_alloc(&d->conaio, tcp_dial_con_cb, d)) != 0) ||
        ((rv = nni_tcp_dialer_init(&d->d)) != 0)) {
        tcp_dialer_free(d);
        return (rv);
    }
 
    d->ops.sd_close = tcp_dialer_close;
    d->ops.sd_free  = tcp_dialer_free;
    d->ops.sd_dial  = tcp_dialer_dial;
    d->ops.sd_get   = tcp_dialer_get;
    d->ops.sd_set   = tcp_dialer_set;
 
    *dp = d;
    return (0);
}
 
int
nni_tcp_dialer_alloc(nng_stream_dialer **dp, const nng_url *url)
{
    tcp_dialer *d;
    int         rv;
    const char *p;
 
    if ((rv = nni_init()) != 0) {
        return (rv);
    }
 
    if ((rv = tcp_dialer_alloc(&d)) != 0) {
        return (rv);
    }
 
    if (((p = url->u_port) == NULL) || (strlen(p) == 0)) {
        p = nni_url_default_port(url->u_scheme);
    }
 
    if ((strlen(p) == 0) || (strlen(url->u_hostname) == 0)) {
        // Dialer needs both a destination hostname and port.
        tcp_dialer_free(d);
        return (NNG_EADDRINVAL);
    }
 
    if (strchr(url->u_scheme, '4') != NULL) {
        d->af = NNG_AF_INET;
    } else if (strchr(url->u_scheme, '6') != NULL) {
        d->af = NNG_AF_INET6;
    } else {
        d->af = NNG_AF_UNSPEC;
    }
 
    if (((d->host = nng_strdup(url->u_hostname)) == NULL) ||
        ((d->port = nng_strdup(p)) == NULL)) {
        tcp_dialer_free(d);
        return (NNG_ENOMEM);
    }
 
    *dp = (void *) d;
    return (0);
}
 
typedef struct {
    nng_stream_listener ops;
    nni_tcp_listener *  l;
    nng_sockaddr        sa;
} tcp_listener;
 
static void
tcp_listener_close(void *arg)
{
    tcp_listener *l = arg;
    nni_tcp_listener_close(l->l);
}
 
static void
tcp_listener_free(void *arg)
{
    tcp_listener *l = arg;
    nni_tcp_listener_fini(l->l);
    NNI_FREE_STRUCT(l);
}
 
static int
tcp_listener_listen(void *arg)
{
    tcp_listener *l = arg;
    return (nni_tcp_listener_listen(l->l, &l->sa));
}
 
static void
tcp_listener_accept(void *arg, nng_aio *aio)
{
    tcp_listener *l = arg;
    nni_tcp_listener_accept(l->l, aio);
}
 
static int
tcp_listener_get_port(void *arg, void *buf, size_t *szp, nni_type t)
{
    tcp_listener *l = arg;
    int           rv;
    nng_sockaddr  sa;
    size_t        sz;
    int           port;
    uint8_t *     paddr;
 
    sz = sizeof(sa);
    rv = nni_tcp_listener_get(
        l->l, NNG_OPT_LOCADDR, &sa, &sz, NNI_TYPE_SOCKADDR);
    if (rv != 0) {
        return (rv);
    }
 
    switch (sa.s_family) {
    case NNG_AF_INET:
        paddr = (void *) &sa.s_in.sa_port;
        break;
 
    case NNG_AF_INET6:
        paddr = (void *) &sa.s_in6.sa_port;
        break;
 
    default:
        paddr = NULL;
        break;
    }
 
    if (paddr == NULL) {
        return (NNG_ESTATE);
    }
 
    NNI_GET16(paddr, port);
    return (nni_copyout_int(port, buf, szp, t));
}
 
static int
tcp_listener_get(
    void *arg, const char *name, void *buf, size_t *szp, nni_type t)
{
    tcp_listener *l = arg;
    if (strcmp(name, NNG_OPT_TCP_BOUND_PORT) == 0) {
        return (tcp_listener_get_port(l, buf, szp, t));
    }
    return (nni_tcp_listener_get(l->l, name, buf, szp, t));
}
 
static int
tcp_listener_set(
    void *arg, const char *name, const void *buf, size_t sz, nni_type t)
{
    tcp_listener *l = arg;
    return (nni_tcp_listener_set(l->l, name, buf, sz, t));
}
 
static int
tcp_listener_alloc_addr(nng_stream_listener **lp, const nng_sockaddr *sa)
{
    tcp_listener *l;
    int           rv;
 
    if ((l = NNI_ALLOC_STRUCT(l)) == NULL) {
        return (NNG_ENOMEM);
    }
    if ((rv = nni_tcp_listener_init(&l->l)) != 0) {
        NNI_FREE_STRUCT(l);
        return (rv);
    }
    l->sa = *sa;
 
    l->ops.sl_free   = tcp_listener_free;
    l->ops.sl_close  = tcp_listener_close;
    l->ops.sl_listen = tcp_listener_listen;
    l->ops.sl_accept = tcp_listener_accept;
    l->ops.sl_get    = tcp_listener_get;
    l->ops.sl_set    = tcp_listener_set;
 
    *lp = (void *) l;
    return (0);
}
 
int
nni_tcp_listener_alloc(nng_stream_listener **lp, const nng_url *url)
{
    nni_aio *    aio;
    int          af;
    int          rv;
    nng_sockaddr sa;
    const char * h;
 
    if ((rv = nni_init()) != 0) {
        return (rv);
    }
    if (strchr(url->u_scheme, '4') != NULL) {
        af = NNG_AF_INET;
    } else if (strchr(url->u_scheme, '6') != NULL) {
        af = NNG_AF_INET6;
    } else {
        af = NNG_AF_UNSPEC;
    }
 
    if ((rv = nng_aio_alloc(&aio, NULL, NULL)) != 0) {
        return (rv);
    }
 
    h = url->u_hostname;
 
    // Wildcard special case, which means bind to INADDR_ANY.
    if ((h != NULL) && ((strcmp(h, "*") == 0) || (strcmp(h, "") == 0))) {
        h = NULL;
    }
    nni_resolv_ip(h, url->u_port, af, true, &sa, aio);
    nni_aio_wait(aio);
 
    if ((rv = nni_aio_result(aio)) != 0) {
        nni_aio_free(aio);
        return (rv);
    }
    nni_aio_free(aio);
 
    return (tcp_listener_alloc_addr(lp, &sa));
}