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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
|
// Copyright 2025 Staysail Systems, Inc. <info@staysail.tech>
//
// 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/aio.h"
#include "../../../core/defs.h"
#include "../../../core/idhash.h"
#include "../../../core/message.h"
#include "../../../core/nng_impl.h"
#include "../../../core/options.h"
#include "../../../core/pipe.h"
#include "../../../core/platform.h"
#include "../../../core/socket.h"
#include "../../../core/stats.h"
#include "../../../supplemental/tls/tls_common.h"
#include "nng/nng.h"
#include <string.h>
// Experimental DTLS transport. Unicast only.
//
typedef struct dtls_pipe dtls_pipe;
typedef struct dtls_ep dtls_ep;
typedef struct dtls_conn dtls_conn;
const uint8_t PROTO_VERSION = 1;
// OP code, 8 bits
enum dtls_opcode {
OPCODE_DATA = 0,
OPCODE_CREQ = 1,
OPCODE_CACK = 2,
OPCODE_DISC = 3,
};
// Disconnect reason, must be 16 bits
typedef enum dtls_disc_reason {
DISC_CLOSED = 0, // normal close
DISC_TYPE = 1, // bad SP type
DISC_NOTCONN = 2, // no such connection
DISC_REFUSED = 3, // refused by policy
DISC_MSGSIZE = 4, // message too large
DISC_NEGO = 5, // neogtiation failed
DISC_INACTIVE = 6, // closed due to inactivity
DISC_PROTO = 7, // other protocol error
DISC_NOBUF = 8, // resources exhausted
} dtls_disc_reason;
#ifndef NNG_DTLS_TXQUEUE_LEN
#define NNG_DTLS_TXQUEUE_LEN 32
#endif
#ifndef NNG_DTLS_RXQUEUE_LEN
#define NNG_DTLS_RXQUEUE_LEN 16
#endif
// The maximum TLS record size
#define DTLS_MAX_RECORD 16384
// For DTLS we use a maximum record size of 16384,
// but we reserve some space for headers. DTLS needs
// 13 bytes, and the transport layer needs 8 bytes.
// To leave some room for the future, we just trim to 64 bytes.
#ifndef NNG_DTLS_RECVMAX
#define NNG_DTLS_RECVMAX (DTLS_MAX_RECORD - 64)
#endif
#ifndef NNG_DTLS_REFRESH
#define NNG_DTLS_REFRESH (5 * NNI_SECOND)
#endif
#ifndef NNG_DTLS_CONNRETRY
#define NNG_DTLS_CONNRETRY (NNI_SECOND / 5)
#endif
// 64-bit protocol header
typedef struct dtls_sp_hdr {
uint8_t us_ver;
uint8_t us_op_code;
uint16_t us_type;
uint16_t us_params[2];
} dtls_sp_hdr;
// DTLS pipe resend (CREQ) in msec (nng_duration)
#define DTLS_PIPE_REFRESH(p) ((p)->refresh)
// DTLS pipe timeout in msec (nng_duration)
#define DTLS_PIPE_TIMEOUT(p) ((p)->refresh * 5)
struct dtls_pipe {
dtls_ep *ep;
nni_pipe *npipe;
nng_sockaddr peer_addr;
uint64_t id; // hash of peer address
uint16_t peer;
uint16_t proto;
bool matched; // true if have matched and given this to SP
bool closed; // true if we are closed (no more send or recv!)
bool dialer; // true if we are dialer
nng_duration refresh; // seconds, for the protocol
nng_time next_wake;
nng_time expire; // inactivity expiration time
nng_time next_refresh;
nni_list_node node;
nni_lmq rx_mq;
// Upper layer queues. These are between the PIPE and SP.
bool send_busy; // true if send is in process
uint16_t send_max; // peer's max recv size
nni_list send_aios;
uint8_t *send_buf;
size_t send_bufsz;
nng_aio send_tls_aio;
bool recv_busy;
bool recv_rdy; // receive is done and data in recvbuf
uint16_t recv_max; // max recv size
nni_list recv_aios;
uint8_t *recv_buf;
size_t recv_bufsz;
nng_aio recv_tls_aio;
// Lower layer queues. These are between the
uint8_t send_op; // usually OPCODE_DATA
uint8_t last_op; // last op code we sent
uint16_t reason; // only for disconnect
nni_mtx lower_mtx; // protects the lower rx_q, etc.
// This is the lower level RX buffer, which contains only
// received ciphertext (content before passed to TLS layer for
// decrypt). The actual pointers may change, as we "swap"
// buffers between the endpoint and the pipe to avoid copying.
nni_list rx_q; // lower aio from the TLS layer
nni_tls_conn tls;
};
struct dtls_ep {
nng_udp *udp;
nni_mtx mtx;
uint16_t proto;
uint16_t peer;
uint16_t af; // address family
bool fini;
bool started;
bool closed;
nng_url *url;
const char *host; // for dialers
nni_aio *useraio;
nni_aio *connaio;
nni_aio timeaio;
nni_aio resaio;
bool dialer;
nni_listener *nlistener;
nni_dialer *ndialer;
nni_msg *rx_payload; // current receive message
nng_sockaddr rx_sa; // addr for last message
nni_aio tx_aio; // aio for TX handling
nni_aio rx_aio; // aio for RX handling
nni_id_map pipes; // pipes (indexed by id)
nni_sockaddr self_sa; // our address
nni_sockaddr peer_sa; // peer address, only for dialer;
nni_list connaios; // aios from accept waiting for a client peer
nni_list connpipes; // pipes waiting to be connected
nng_duration refresh; // refresh interval for connections in seconds
uint16_t rcvmax; // max payload, trimmed to uint16_t
nni_resolv_item resolv;
nng_tls_config *tlscfg;
size_t rx_size; // size of the rx buffer
void *rx_buf;
nni_stat_item st_rcv_max;
nni_stat_item st_rcv_reorder;
nni_stat_item st_rcv_toobig;
nni_stat_item st_rcv_nomatch;
nni_stat_item st_rcv_copy;
nni_stat_item st_rcv_nocopy;
nni_stat_item st_rcv_nobuf;
nni_stat_item st_snd_toobig;
nni_stat_item st_snd_nobuf;
nni_stat_item st_peer_inactive;
nni_stat_item st_copy_max;
};
static void dtls_ep_start(dtls_ep *);
static void dtls_resolv_cb(void *);
static void dtls_rx_cb(void *);
static void dtls_ep_match(dtls_ep *ep);
static void dtls_remove_pipe(dtls_pipe *p);
// BIO send/recv functions for use by the common TLS layer.
static void
dtls_bio_cancel(nng_aio *aio, void *arg, nng_err rv)
{
dtls_pipe *p = arg;
nni_mtx_lock(&p->lower_mtx);
if (nni_aio_list_active(aio)) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, rv);
}
nni_mtx_unlock(&p->lower_mtx);
}
static void
dtls_bio_recv_done(dtls_pipe *p)
{
nng_aio *aio;
nni_msg *msg;
while ((!nni_lmq_empty(&p->rx_mq)) &&
((aio = nni_list_first(&p->rx_q)) != NULL)) {
nni_aio_list_remove(aio);
nni_lmq_get(&p->rx_mq, &msg);
nni_aio_finish_msg(aio, msg);
}
}
static void
dtls_bio_recv(void *arg, nng_aio *aio)
{
dtls_pipe *p = arg;
nni_mtx_lock(&p->lower_mtx);
if (!nni_aio_start(aio, dtls_bio_cancel, p)) {
nni_mtx_unlock(&p->lower_mtx);
return;
}
nni_aio_list_append(&p->rx_q, aio);
dtls_bio_recv_done(p);
nni_mtx_unlock(&p->lower_mtx);
}
static void
dtls_bio_send(void *arg, nng_aio *aio)
{
dtls_pipe *p = arg;
nni_iov iov;
nni_msg *msg;
nni_mtx_lock(&p->lower_mtx);
if (!p->closed) {
nni_aio_set_input(aio, 0, &p->peer_addr);
msg = nni_aio_get_msg(aio);
iov.iov_buf = nni_msg_body(msg);
iov.iov_len = nni_msg_len(msg);
nng_aio_set_iov(aio, 1, &iov);
nng_udp_send(p->ep->udp, aio);
} else {
nni_aio_finish_error(aio, NNG_ECLOSED);
}
nni_mtx_unlock(&p->lower_mtx);
}
static void
dtls_bio_free(void *arg)
{
NNI_ARG_UNUSED(arg);
}
static void
dtls_bio_close(void *arg)
{
NNI_ARG_UNUSED(arg);
}
static void
dtls_bio_stop(void *arg)
{
dtls_pipe *p = arg;
nni_aio_stop(&p->recv_tls_aio);
nni_aio_stop(&p->send_tls_aio);
}
static nni_tls_bio_ops dtls_bio_ops = {
.bio_send = dtls_bio_send,
.bio_recv = dtls_bio_recv,
.bio_close = dtls_bio_close,
.bio_stop = dtls_bio_stop,
.bio_free = dtls_bio_free,
};
static void
dtls_tran_init(void)
{
}
static void
dtls_tran_fini(void)
{
}
//
// Upper layer functions - moving data between TLS and SP.
// TLS acts as kind of a stream for us, so we only see the
// data that is meant for us, but we will send and receive
// control messages that are not just data payloads.
//
static void dtls_pipe_send_cancel(nng_aio *, void *, nng_err);
static void dtls_pipe_send_tls(dtls_pipe *);
static void dtls_pipe_send_tls_cb(void *arg);
static void
dtls_pipe_send(void *arg, nni_aio *aio)
{
dtls_pipe *p = arg;
dtls_ep *ep;
nng_msg *msg;
size_t count = 0;
size_t sndmax;
msg = nni_aio_get_msg(aio);
ep = p->ep;
if (msg != NULL) {
count = nni_msg_len(msg) + nni_msg_header_len(msg);
}
nni_mtx_lock(&ep->mtx);
sndmax = p->send_max;
if (!nni_aio_start(aio, dtls_pipe_send_cancel, p)) {
nni_aio_set_msg(aio, NULL);
nni_msg_free(msg);
nni_mtx_unlock(&ep->mtx);
return;
}
nni_aio_reset(aio);
if ((nni_msg_len(msg) + nni_msg_header_len(msg)) > sndmax) {
// rather failing this with an error, we just drop it
// on the floor. this is on the sender, so there isn't
// a compelling need to disconnect the pipe, since it
// we're not being "ill-behaved" to our peer.
nni_stat_inc(&ep->st_snd_toobig, 1);
nni_mtx_unlock(&ep->mtx);
nni_aio_finish(aio, 0, count);
nni_msg_free(msg);
return;
}
nni_aio_list_append(&p->send_aios, aio);
dtls_pipe_send_tls(p);
nni_mtx_unlock(&ep->mtx);
}
static void
dtls_pipe_send_cancel(nng_aio *aio, void *arg, nng_err err)
{
dtls_pipe *p = arg;
nni_mtx_lock(&p->ep->mtx);
if (nni_aio_list_active(aio)) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, err);
}
nni_mtx_unlock(&p->ep->mtx);
}
// Lower layer send/recv functions, used by the pipe layer.
static void
dtls_pipe_send_tls(dtls_pipe *p)
{
nni_aio *aio;
nng_msg *msg;
uint8_t opcode;
nng_iov iov;
dtls_sp_hdr *hdr = (void *) p->send_buf;
if (p->send_busy || p->closed) {
return;
}
opcode = p->send_op;
// reset the last op
p->send_op = OPCODE_DATA;
hdr->us_ver = PROTO_VERSION;
hdr->us_op_code = opcode;
NNI_PUT16LE(&hdr->us_type, p->proto);
hdr->us_params[0] = 0;
hdr->us_params[1] = 0;
iov.iov_buf = hdr;
iov.iov_len = sizeof(*hdr);
switch (opcode) {
case OPCODE_DATA:
for (;;) {
if ((aio = nni_list_first(&p->send_aios)) == NULL) {
// no work for us!
return;
}
nni_aio_list_remove(aio);
msg = nni_aio_get_msg(aio);
if (nni_msg_header_len(msg) + nni_msg_len(msg) +
sizeof(*hdr) >
p->send_bufsz) {
nng_msg_free(msg);
nni_aio_finish_error(aio, NNG_EMSGSIZE);
continue;
}
break; // for loop
}
size_t len = nni_msg_header_len(msg);
uint8_t *data = (void *) (hdr + 1);
memcpy(data, nni_msg_header(msg), len);
data += len;
memcpy(data, nni_msg_body(msg), nni_msg_len(msg));
len += nni_msg_len(msg);
NNI_PUT16LE(&hdr->us_params[0], (uint16_t) len);
iov.iov_len += len;
nni_msg_free(msg);
nni_aio_set_msg(aio, NULL);
nni_aio_finish(aio, 0, len);
break;
case OPCODE_CREQ:
case OPCODE_CACK:
NNI_PUT16LE(&hdr->us_params[0], p->recv_max);
NNI_PUT16LE(&hdr->us_params[1], p->refresh);
break;
case OPCODE_DISC:
NNI_PUT16LE(&hdr->us_params[0], p->reason);
break;
default:
NNI_ASSERT(false); // this should never happen!
// fall back to sending a disconnect
hdr->us_op_code = OPCODE_DISC;
NNI_PUT16LE(&hdr->us_params[0], DISC_PROTO);
}
p->last_op = opcode;
p->send_busy = true;
nni_aio_set_iov(&p->send_tls_aio, 1, &iov);
nni_tls_send(&p->tls, &p->send_tls_aio);
}
static void
dtls_pipe_send_tls_cb(void *arg)
{
dtls_pipe *p = arg;
nni_mtx_lock(&p->ep->mtx);
p->send_busy = false;
if (p->closed) {
nni_mtx_unlock(&p->ep->mtx);
return;
}
if (nni_aio_result(&p->send_tls_aio) != NNG_OK) {
nni_pipe_close(p->npipe);
nni_mtx_unlock(&p->ep->mtx);
return;
}
dtls_pipe_send_tls(p);
nni_mtx_unlock(&p->ep->mtx);
}
// RECV SIDE
static void dtls_pipe_recv_cancel(nni_aio *, void *, nng_err);
static void dtls_pipe_recv_tls(dtls_pipe *);
static void
dtls_pipe_recv(void *arg, nni_aio *aio)
{
dtls_pipe *p = arg;
dtls_ep *ep = p->ep;
nni_aio_reset(aio);
nni_mtx_lock(&ep->mtx);
if (p->closed) {
nni_mtx_unlock(&ep->mtx);
nni_aio_finish_error(aio, NNG_ECLOSED);
return;
}
if (!nni_aio_start(aio, dtls_pipe_recv_cancel, p)) {
nni_mtx_unlock(&ep->mtx);
return;
}
nni_list_append(&p->recv_aios, aio);
dtls_pipe_recv_tls(p);
nni_mtx_unlock(&ep->mtx);
}
static void
dtls_pipe_recv_cancel(nni_aio *aio, void *arg, nng_err rv)
{
dtls_pipe *p = arg;
dtls_ep *ep = p->ep;
nni_mtx_lock(&ep->mtx);
if (!nni_aio_list_active(aio)) {
nni_mtx_unlock(&ep->mtx);
return;
}
nni_aio_list_remove(aio);
nni_mtx_unlock(&ep->mtx);
nni_aio_finish_error(aio, rv);
}
static void
dtls_pipe_recv_tls_start(dtls_pipe *p)
{
nng_iov iov;
if (p->recv_busy || p->closed) {
return;
}
p->recv_busy = true;
iov.iov_buf = p->recv_buf;
iov.iov_len = p->recv_bufsz;
nni_aio_set_iov(&p->recv_tls_aio, 1, &iov);
nni_tls_recv(&p->tls, &p->recv_tls_aio);
}
static void
dtls_pipe_recv_tls(dtls_pipe *p)
{
nng_aio *aio = nni_list_first(&p->recv_aios);
size_t len;
nng_msg *msg;
nng_err rv;
if (aio == NULL) {
return;
}
if (!p->recv_rdy) {
dtls_pipe_recv_tls_start(p);
return;
}
p->recv_rdy = false;
nni_aio_list_remove(aio);
len = nng_aio_count(&p->recv_tls_aio);
NNI_ASSERT(len >= sizeof(dtls_sp_hdr));
len -= sizeof(dtls_sp_hdr);
if ((rv = nni_msg_alloc(&msg, len)) != NNG_OK) {
nni_aio_finish_error(aio, rv);
return;
}
memcpy(nng_msg_body(msg), p->recv_buf + sizeof(dtls_sp_hdr), len);
nni_aio_finish_msg(aio, msg);
}
static void
dtls_pipe_recv_tls_cb(void *arg)
{
dtls_pipe *p = arg;
dtls_ep *ep = p->ep;
dtls_sp_hdr *hdr = (void *) p->recv_buf;
nng_aio *aio = &p->recv_tls_aio;
uint16_t proto;
uint16_t refresh;
uint16_t rcvmax;
nng_err rv;
nni_mtx_lock(&ep->mtx);
p->recv_busy = false;
if ((rv = nni_aio_result(aio)) != NNG_OK) {
// If we didn't connect yet, issue an error so the peer can see
// a connection failure (e.g. if we failed the TLS handshake.)
if (p->dialer && !p->matched) {
nni_aio *caio;
if ((caio = nni_list_first(&ep->connaios)) != NULL) {
nni_aio_list_remove(caio);
nni_aio_finish_error(caio, rv);
}
}
// Bump a bad receive stat (e.g. someone may have sent us
// garbage.) We do not acknowledge or handle garbage frames
// sent to an open session.
nni_pipe_close(p->npipe);
nni_mtx_unlock(&ep->mtx);
return;
}
// We had a "good" receive (TLS passed at least) from the peer.
if (nni_aio_count(aio) < sizeof(*hdr)) {
// Runt frame.
p->send_op = OPCODE_DISC;
p->reason = DISC_PROTO;
goto bad;
}
if (nni_aio_count(aio) > sizeof(*hdr) + p->recv_max) {
p->send_op = OPCODE_DISC;
p->reason = DISC_MSGSIZE;
goto bad;
}
if (hdr->us_ver != PROTO_VERSION) {
// Bad protocol version
p->send_op = OPCODE_DISC;
p->reason = DISC_PROTO;
goto bad;
}
NNI_GET16LE(&hdr->us_type, proto);
if (proto != p->peer) {
// Bad SP protocol type
p->send_op = OPCODE_DISC;
p->reason = DISC_TYPE;
goto bad;
}
p->expire = nni_clock() + DTLS_PIPE_TIMEOUT(p);
if (!p->matched) {
p->matched = true;
nni_list_append(&p->ep->connpipes, p);
dtls_ep_match(p->ep);
}
switch (hdr->us_op_code) {
case OPCODE_CREQ:
if (p->dialer) {
// dialers don't accept requests
goto bad;
}
NNI_GET16LE(&hdr->us_params[0], rcvmax);
NNI_GET16LE(&hdr->us_params[1], refresh);
if ((refresh > 0) && ((refresh * NNI_SECOND) < p->refresh)) {
p->refresh = refresh * NNI_SECOND;
}
if ((rcvmax > 0) && (rcvmax < NNG_DTLS_RECVMAX)) {
p->send_max = rcvmax;
}
// schedule the CACK reply
p->send_op = OPCODE_CACK;
break;
case OPCODE_CACK:
if (!p->dialer) {
goto bad;
}
NNI_GET16LE(&hdr->us_params[0], rcvmax);
NNI_GET16LE(&hdr->us_params[0], refresh);
if ((refresh > 0) && ((refresh * NNI_SECOND) < p->refresh)) {
p->refresh = refresh * NNI_SECOND;
}
if ((rcvmax > 0) && (rcvmax < NNG_DTLS_RECVMAX)) {
p->send_max = rcvmax;
}
break;
case OPCODE_DISC:
p->closed = true;
nni_mtx_unlock(&ep->mtx);
nni_pipe_close(p->npipe);
return;
case OPCODE_DATA:
p->recv_rdy = true;
dtls_pipe_recv_tls(p);
nni_mtx_unlock(&ep->mtx);
return;
}
bad:
if (p->send_op != OPCODE_DATA) {
dtls_pipe_send_tls(p);
}
dtls_pipe_recv_tls_start(p);
nni_mtx_unlock(&ep->mtx);
}
static void
dtls_pipe_close(void *arg)
{
dtls_pipe *p = arg;
dtls_ep *ep = p->ep;
nni_aio *aio;
nni_mtx_lock(&ep->mtx);
while ((aio = nni_list_first(&p->recv_aios)) != NULL) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, NNG_ECLOSED);
}
while ((aio = nni_list_first(&p->send_aios)) != NULL) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, NNG_ECLOSED);
}
if (p->matched) {
p->send_op = OPCODE_DISC;
p->reason = DISC_CLOSED;
dtls_pipe_send_tls(p);
}
p->closed = true;
nni_mtx_unlock(&ep->mtx);
}
static nng_err dtls_add_pipe(dtls_ep *ep, dtls_pipe *p);
static void
dtls_pipe_stop(void *arg)
{
dtls_pipe *p = arg;
dtls_ep *ep = p->ep;
dtls_pipe_close(arg);
nni_tls_stop(&p->tls);
nni_mtx_lock(&ep->mtx);
dtls_remove_pipe(p);
nni_list_node_remove(&p->node);
nni_mtx_unlock(&ep->mtx);
}
static int
dtls_pipe_alloc(dtls_ep *ep, dtls_pipe **pp, const nng_sockaddr *sa)
{
dtls_pipe *p;
nng_err rv;
if (ep->dialer) {
rv = nni_pipe_alloc_dialer((void **) &p, ep->ndialer);
} else {
rv = nni_pipe_alloc_listener((void *) &p, ep->nlistener);
}
if (rv != NNG_OK) {
nng_log_err("NNG-DTLS-PIPE-ALLOC-FAIL",
"Failed allocating pipe for DTLS: %s", nng_strerror(rv));
return (rv);
}
p->dialer = ep->dialer;
p->ep = ep;
p->proto = ep->proto;
p->peer = ep->peer;
p->peer_addr = *sa;
p->id = nng_sockaddr_hash(sa);
p->refresh = ep->refresh;
p->send_max = NNG_DTLS_RECVMAX;
p->recv_max = ep->rcvmax;
*pp = p;
if (((rv = nni_tls_init(&p->tls, ep->tlscfg, true)) != NNG_OK) ||
((rv = nni_tls_start(&p->tls, &dtls_bio_ops, p, sa)) != NNG_OK) ||
((rv = dtls_add_pipe(ep, p)) != NNG_OK)) {
nni_pipe_close(p->npipe);
nng_log_err("NNG-DTLS-PIPE-ADD-FAIL",
"Failed adding pipe for DTLS: %s", nng_strerror(rv));
return (rv);
}
// We need to start a receiver on the pipe.
dtls_pipe_recv_tls_start(p);
// Also start TLS up and running.
switch (nni_tls_run(&p->tls)) {
case NNG_OK:
case NNG_EAGAIN:
break;
default:
nni_pipe_close(p->npipe);
break;
}
// wake the timer so it knows to resubmit
nni_aio_abort(&ep->timeaio, NNG_ETIMEDOUT);
return (NNG_OK);
}
static size_t
dtls_pipe_size(void)
{
return (NNI_ALIGN_UP(sizeof(dtls_pipe)) +
NNI_ALIGN_UP(nni_tls_engine_conn_size()));
}
static int
dtls_pipe_init(void *arg, nni_pipe *npipe)
{
dtls_pipe *p = arg;
p->npipe = npipe;
size_t bufsz = DTLS_MAX_RECORD; // TODO: Make this a tunable.
if ((p->recv_buf = nni_alloc(bufsz)) == NULL) {
return (NNG_ENOMEM);
}
if ((p->send_buf = nni_alloc(bufsz)) == NULL) {
return (NNG_ENOMEM);
}
p->recv_bufsz = bufsz;
p->send_bufsz = bufsz;
nni_mtx_init(&p->lower_mtx);
nni_aio_init(&p->recv_tls_aio, dtls_pipe_recv_tls_cb, p);
nni_aio_init(&p->send_tls_aio, dtls_pipe_send_tls_cb, p);
nni_aio_list_init(&p->rx_q);
nni_aio_list_init(&p->recv_aios);
nni_aio_list_init(&p->send_aios);
nni_lmq_init(&p->rx_mq, NNG_DTLS_RXQUEUE_LEN);
return (0);
}
static void
dtls_pipe_fini(void *arg)
{
dtls_pipe *p = arg;
nng_msg *m;
nni_tls_fini(&p->tls);
nni_aio_fini(&p->recv_tls_aio);
nni_aio_fini(&p->send_tls_aio);
if (p->recv_buf != NULL) {
nni_free(p->recv_buf, p->recv_bufsz);
}
if (p->send_buf != NULL) {
nni_free(p->send_buf, p->send_bufsz);
}
nni_mtx_lock(&p->lower_mtx);
while (!nni_lmq_empty(&p->rx_mq)) {
nni_lmq_get(&p->rx_mq, &m);
nni_msg_free(m);
}
nni_mtx_unlock(&p->lower_mtx);
nni_mtx_fini(&p->lower_mtx);
nni_lmq_fini(&p->rx_mq);
NNI_ASSERT(nni_list_empty(&p->recv_aios));
NNI_ASSERT(nni_list_empty(&p->send_aios));
}
static dtls_pipe *
dtls_find_pipe(dtls_ep *ep, const nng_sockaddr *peer_addr)
{
uint64_t id = nng_sockaddr_hash(peer_addr);
dtls_pipe *p;
// we'll keep incrementing id until we conclusively match
// or we get a NULL. This is another level of rehashing, but
// it keeps us from having to look up.
for (;;) {
if ((p = nni_id_get(&ep->pipes, id)) == NULL) {
return (NULL);
}
if (nng_sockaddr_equal(&p->peer_addr, peer_addr)) {
return (p);
}
id++;
if (id == 0) {
id = 1;
}
}
}
static void
dtls_remove_pipe(dtls_pipe *p)
{
// ep locked
dtls_ep *ep = p->ep;
uint64_t id = p->id;
bool matched = p->matched;
if (id == 0) {
return;
}
p->id = 0;
for (;;) {
dtls_pipe *srch;
if ((srch = nni_id_get(&ep->pipes, id)) == NULL) {
break;
}
if (srch == p) {
nni_id_remove(&ep->pipes, id);
break;
}
id++;
if (id == 0) {
id = 1;
}
}
if (!matched) {
nni_pipe_rele(p->npipe);
}
}
static nng_err
dtls_add_pipe(dtls_ep *ep, dtls_pipe *p)
{
// Id must be part of the hash
uint64_t id = p->id;
while (nni_id_get(&ep->pipes, id) != NULL) {
id++;
if (id == 0) {
id = 1;
}
}
p->id = id;
return (nni_id_set(&ep->pipes, id, p));
}
static void
dtls_start_rx(dtls_ep *ep)
{
nni_iov iov;
iov.iov_buf = ep->rx_buf;
iov.iov_len = ep->rx_size;
nni_aio_reset(&ep->rx_aio);
nni_aio_set_input(&ep->rx_aio, 0, &ep->rx_sa);
nni_aio_set_iov(&ep->rx_aio, 1, &iov);
nng_udp_recv(ep->udp, &ep->rx_aio);
}
static void
dtls_rx_cb(void *arg)
{
dtls_ep *ep = arg;
dtls_pipe *p;
nni_aio *aio = &ep->rx_aio;
nng_err rv;
nni_msg *msg;
nni_mtx_lock(&ep->mtx);
if ((rv = nni_aio_result(aio)) != NNG_OK) {
// something bad happened on RX... which is unexpected.
// sleep a little bit and hope for recovery.
switch (nni_aio_result(aio)) {
case NNG_ECLOSED:
case NNG_ECANCELED:
case NNG_ESTOPPED:
nni_mtx_unlock(&ep->mtx);
return;
case NNG_ETIMEDOUT:
case NNG_EAGAIN:
case NNG_EINTR:
default:
goto fail;
}
}
// If this came from another host, and we are a dialer, we discard.
// Dialers only talk to the party they explicitly dialed.
if (ep->dialer && !nng_sockaddr_equal(&ep->rx_sa, &ep->peer_sa)) {
goto fail;
}
if ((p = dtls_find_pipe(ep, &ep->rx_sa)) == NULL) {
if (dtls_pipe_alloc(ep, &p, &ep->rx_sa) != NNG_OK) {
goto fail;
}
}
if (p->closed) {
goto fail;
}
NNI_ASSERT(p != NULL);
size_t len = nni_aio_count(aio);
if (nni_msg_alloc(&msg, len) != NNG_OK) {
// TODO BUMP A NO RECV ALLOC STAT
goto fail;
}
memcpy(nni_msg_body(msg), ep->rx_buf, len);
nni_pipe_hold(p->npipe);
nni_mtx_unlock(&ep->mtx);
nni_mtx_lock(&p->lower_mtx);
if (nni_lmq_put(&p->rx_mq, msg) != NNG_OK) {
// TODO: BUMP TXQ FULL STAT
nng_msg_free(msg);
}
dtls_bio_recv_done(p);
nni_mtx_unlock(&p->lower_mtx);
// Run the TLS state machine.
switch (nni_tls_run(&p->tls)) {
case NNG_OK:
case NNG_EAGAIN:
break;
default:
nni_pipe_close(p->npipe);
}
nni_pipe_rele(p->npipe);
nni_mtx_lock(&ep->mtx);
dtls_start_rx(ep);
nni_mtx_unlock(&ep->mtx);
return;
fail:
// start another receive
dtls_start_rx(ep);
nni_mtx_unlock(&ep->mtx);
}
static uint16_t
dtls_pipe_peer(void *arg)
{
dtls_pipe *p = arg;
return (p->peer);
}
static nng_err
dtls_pipe_get_recvmax(void *arg, void *v, size_t *szp, nni_type t)
{
dtls_pipe *p = arg;
dtls_ep *ep = p->ep;
nng_err rv;
nni_mtx_lock(&ep->mtx);
rv = nni_copyout_size(p->recv_max, v, szp, t);
nni_mtx_unlock(&ep->mtx);
return (rv);
}
static nni_option dtls_pipe_options[] = {
{
.o_name = NNG_OPT_RECVMAXSZ,
.o_get = dtls_pipe_get_recvmax,
},
{
.o_name = NULL,
},
};
static nng_err
dtls_pipe_getopt(
void *arg, const char *name, void *buf, size_t *szp, nni_type t)
{
dtls_pipe *p = arg;
return (nni_getopt(dtls_pipe_options, name, p, buf, szp, t));
}
static nng_err
dtls_pipe_peer_cert(void *arg, nng_tls_cert **certp)
{
dtls_pipe *p = arg;
return (nni_tls_peer_cert(&p->tls, certp));
}
static const nng_sockaddr *
dtls_pipe_peer_addr(void *arg)
{
dtls_pipe *p = arg;
return (&p->peer_addr);
}
static const nng_sockaddr *
dtls_pipe_self_addr(void *arg)
{
dtls_pipe *p = arg;
return (&p->ep->self_sa);
}
static void
dtls_ep_fini(void *arg)
{
dtls_ep *ep = arg;
if (ep->tlscfg != NULL) {
nng_tls_config_free(ep->tlscfg);
}
nni_aio_fini(&ep->timeaio);
nni_aio_fini(&ep->resaio);
nni_aio_fini(&ep->tx_aio);
nni_aio_fini(&ep->rx_aio);
if (ep->udp != NULL) {
nng_udp_close(ep->udp);
}
if (ep->rx_size != 0) {
nni_free(ep->rx_buf, ep->rx_size);
}
nni_msg_free(ep->rx_payload); // safe even if msg is null
nni_id_map_fini(&ep->pipes);
nni_mtx_fini(&ep->mtx);
}
static void
dtls_ep_close(void *arg)
{
dtls_ep *ep = arg;
nni_aio *aio;
dtls_pipe *p;
uint64_t key;
uint32_t cursor;
nni_aio_close(&ep->resaio);
nni_aio_close(&ep->rx_aio);
nni_aio_close(&ep->timeaio);
// leave tx open so we can send disconnects
nni_mtx_lock(&ep->mtx);
ep->closed = true;
while ((aio = nni_list_first(&ep->connaios)) != NULL) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, NNG_ECONNABORTED);
}
cursor = 0;
key = 0;
while (nni_id_visit(&ep->pipes, &key, (void **) &p, &cursor)) {
nni_pipe_close(p->npipe);
}
nni_mtx_unlock(&ep->mtx);
}
static void
dtls_ep_stop(void *arg)
{
dtls_ep *ep = arg;
nni_aio_stop(&ep->resaio);
nni_aio_stop(&ep->rx_aio);
nni_aio_stop(&ep->timeaio);
nni_mtx_lock(&ep->mtx);
ep->fini = true;
ep->started = false;
nni_mtx_unlock(&ep->mtx);
}
// timer handler - sends out additional creqs as needed,
// reaps stale connections, and handles linger.
static void
dtls_timer_cb(void *arg)
{
dtls_ep *ep = arg;
dtls_pipe *p;
nng_err rv;
nni_mtx_lock(&ep->mtx);
rv = nni_aio_result(&ep->timeaio);
switch (rv) {
case NNG_ECLOSED:
case NNG_ECANCELED:
case NNG_ESTOPPED:
nni_mtx_unlock(&ep->mtx);
return;
default:
if (ep->closed) {
nni_mtx_unlock(&ep->mtx);
return;
}
break;
}
uint32_t cursor = 0;
nni_time now = nni_clock();
nng_duration refresh = NNG_DURATION_INFINITE;
while (nni_id_visit(&ep->pipes, NULL, (void **) &p, &cursor)) {
if (p->closed) {
continue;
}
NNI_ASSERT(p->refresh > 0);
if (p->expire > 0 && now > p->expire) {
char buf[128];
nng_log_info("NNG-DTLS-INACTIVE",
"Pipe peer %s timed out due to inactivity",
nng_str_sockaddr(&p->peer_addr, buf, sizeof(buf)));
nni_stat_inc(&ep->st_peer_inactive, 1);
nni_pipe_close(p->npipe);
continue;
}
if (p->dialer && now > p->next_refresh) {
p->send_op = OPCODE_CREQ;
p->next_refresh = now + p->refresh;
dtls_pipe_send_tls(p);
}
if (refresh == NNG_DURATION_INFINITE && p->refresh > 0) {
refresh = p->refresh;
} else if ((p->refresh > 0) && (p->refresh < refresh)) {
refresh = p->refresh;
}
}
nni_sleep_aio(refresh, &ep->timeaio);
nni_mtx_unlock(&ep->mtx);
}
static nng_err
dtls_ep_init(
dtls_ep *ep, nng_url *url, nni_sock *sock, nni_dialer *d, nni_listener *l)
{
nni_mtx_init(&ep->mtx);
nni_id_map_init(&ep->pipes, 1, 0xFFFFFFFF, true);
NNI_LIST_INIT(&ep->connpipes, dtls_pipe, node);
nni_aio_list_init(&ep->connaios);
nni_aio_init(&ep->rx_aio, dtls_rx_cb, ep);
nni_aio_init(&ep->timeaio, dtls_timer_cb, ep);
nni_aio_init(&ep->resaio, dtls_resolv_cb, ep);
if (strcmp(url->u_scheme, "dtls") == 0) {
ep->af = NNG_AF_UNSPEC;
} else if (strcmp(url->u_scheme, "dtls4") == 0) {
ep->af = NNG_AF_INET;
} else if (strcmp(url->u_scheme, "dtls6") == 0) {
ep->af = NNG_AF_INET6;
} else {
return (NNG_EADDRINVAL);
}
ep->self_sa.s_family = ep->af;
ep->proto = nni_sock_proto_id(sock);
ep->peer = nni_sock_peer_id(sock);
ep->url = url;
ep->refresh = NNG_DTLS_REFRESH; // one minute by default
ep->rcvmax = NNG_DTLS_RECVMAX;
// receive buffer plus some extra for UDP and TLS headers
if ((ep->rx_buf = nni_alloc(DTLS_MAX_RECORD)) == NULL) {
return (NNG_ENOMEM);
}
ep->rx_size = DTLS_MAX_RECORD;
NNI_STAT_LOCK(rcv_max_info, "rcv_max", "maximum receive size",
NNG_STAT_LEVEL, NNG_UNIT_BYTES);
NNI_STAT_LOCK(rcv_nomatch_info, "rcv_nomatch",
"messages without a matching connection", NNG_STAT_COUNTER,
NNG_UNIT_MESSAGES);
NNI_STAT_LOCK(rcv_toobig_info, "rcv_toobig",
"received messages rejected because too big", NNG_STAT_COUNTER,
NNG_UNIT_MESSAGES);
NNI_STAT_LOCK(rcv_nobuf_info, "rcv_nobuf",
"received messages dropped no buffer", NNG_STAT_COUNTER,
NNG_UNIT_MESSAGES);
NNI_STAT_LOCK(snd_toobig_info, "snd_toobig",
"sent messages rejected because too big", NNG_STAT_COUNTER,
NNG_UNIT_MESSAGES);
NNI_STAT_LOCK(snd_nobuf_info, "snd_nobuf",
"sent messages dropped no buffer", NNG_STAT_COUNTER,
NNG_UNIT_MESSAGES);
NNI_STAT_LOCK(peer_inactive_info, "peer_inactive",
"connections closed due to inactive peer", NNG_STAT_COUNTER,
NNG_UNIT_EVENTS);
nni_stat_init_lock(&ep->st_rcv_max, &rcv_max_info, &ep->mtx);
nni_stat_init_lock(&ep->st_rcv_toobig, &rcv_toobig_info, &ep->mtx);
nni_stat_init_lock(&ep->st_rcv_nomatch, &rcv_nomatch_info, &ep->mtx);
nni_stat_init_lock(&ep->st_rcv_nobuf, &rcv_nobuf_info, &ep->mtx);
nni_stat_init_lock(&ep->st_snd_toobig, &snd_toobig_info, &ep->mtx);
nni_stat_init_lock(&ep->st_snd_nobuf, &snd_nobuf_info, &ep->mtx);
nni_stat_init_lock(
&ep->st_peer_inactive, &peer_inactive_info, &ep->mtx);
if (l) {
NNI_ASSERT(d == NULL);
nni_listener_add_stat(l, &ep->st_rcv_max);
nni_listener_add_stat(l, &ep->st_rcv_toobig);
nni_listener_add_stat(l, &ep->st_rcv_nomatch);
nni_listener_add_stat(l, &ep->st_rcv_nobuf);
nni_listener_add_stat(l, &ep->st_snd_toobig);
nni_listener_add_stat(l, &ep->st_snd_nobuf);
}
if (d) {
NNI_ASSERT(l == NULL);
nni_dialer_add_stat(d, &ep->st_rcv_max);
nni_dialer_add_stat(d, &ep->st_rcv_toobig);
nni_dialer_add_stat(d, &ep->st_rcv_nomatch);
nni_dialer_add_stat(d, &ep->st_rcv_nobuf);
nni_dialer_add_stat(d, &ep->st_snd_toobig);
nni_dialer_add_stat(d, &ep->st_snd_nobuf);
}
// schedule our timer callback - forever for now
// adjusted automatically as we add pipes or other
// actions which require earlier wakeup.
nni_sleep_aio(NNG_DURATION_INFINITE, &ep->timeaio);
// nni_sleep_aio(100, &ep->timeaio);
return (NNG_OK);
}
static nng_err
dtls_check_url(nng_url *url, bool listen)
{
// Check for invalid URL components.
if ((strlen(url->u_path) != 0) && (strcmp(url->u_path, "/") != 0)) {
return (NNG_EADDRINVAL);
}
if ((url->u_fragment != NULL) || (url->u_userinfo != NULL) ||
(url->u_query != NULL)) {
return (NNG_EADDRINVAL);
}
if (!listen) {
if ((strlen(url->u_hostname) == 0) || (url->u_port == 0)) {
return (NNG_EADDRINVAL);
}
}
return (NNG_OK);
}
static nng_err
dtls_dialer_init(void *arg, nng_url *url, nni_dialer *ndialer)
{
dtls_ep *ep = arg;
nng_err rv;
nni_sock *sock = nni_dialer_sock(ndialer);
if ((rv = dtls_check_url(url, false)) != NNG_OK) {
return (rv);
}
ep->ndialer = ndialer;
if ((rv = dtls_ep_init(ep, url, sock, ndialer, NULL)) != NNG_OK) {
return (rv);
}
return (NNG_OK);
}
static nng_err
dtls_listener_init(void *arg, nng_url *url, nni_listener *nlistener)
{
dtls_ep *ep = arg;
nng_err rv;
nni_sock *sock = nni_listener_sock(nlistener);
ep->nlistener = nlistener;
if ((rv = dtls_ep_init(ep, url, sock, NULL, nlistener)) != NNG_OK) {
return (rv);
}
// Check for invalid URL components.
if (((rv = dtls_check_url(url, true)) != NNG_OK) ||
((rv = nni_url_to_address(&ep->self_sa, url)) != NNG_OK)) {
return (rv);
}
return (NNG_OK);
}
static void
dtls_ep_cancel(nni_aio *aio, void *arg, nng_err rv)
{
dtls_ep *ep = arg;
nni_mtx_lock(&ep->mtx);
if (nni_aio_list_active(aio)) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, rv);
nni_aio_abort(&ep->resaio, rv);
}
nni_mtx_unlock(&ep->mtx);
}
static void
dtls_resolv_cb(void *arg)
{
dtls_ep *ep = arg;
dtls_pipe *p;
nni_aio *aio;
nng_err rv;
nni_mtx_lock(&ep->mtx);
if ((aio = nni_list_first(&ep->connaios)) == NULL) {
nni_mtx_unlock(&ep->mtx);
return;
}
if (ep->closed) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, NNG_ECLOSED);
nni_mtx_unlock(&ep->mtx);
return;
}
if ((rv = nni_aio_result(&ep->resaio)) != NNG_OK) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, rv);
nni_mtx_unlock(&ep->mtx);
nng_log_warn("NNG-RESOLV", "Failed resolving IP address: %s",
nng_strerror(rv));
return;
}
// Choose the right port to bind to. The family must match.
if (ep->self_sa.s_family == NNG_AF_UNSPEC) {
ep->self_sa.s_family = ep->peer_sa.s_family;
}
// Close the socket if it was open, because we need to
// start with a fresh port.
if (ep->udp != NULL) {
nng_udp_close(ep->udp);
ep->udp = NULL;
}
if ((rv = nng_udp_open(&ep->udp, &ep->self_sa)) != NNG_OK) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, rv);
nni_mtx_unlock(&ep->mtx);
return;
}
if ((rv = dtls_pipe_alloc(ep, &p, &ep->peer_sa)) != NNG_OK) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, rv);
nni_mtx_unlock(&ep->mtx);
return;
}
dtls_ep_start(ep);
// Send out the connection request. We don't complete
// the user aio until we confirm a connection, so that
// we can supply details like maximum receive message size
// and the protocol the peer is using.
p->send_op = OPCODE_CREQ;
dtls_pipe_send_tls(p);
nni_mtx_unlock(&ep->mtx);
}
static void
dtls_ep_connect(void *arg, nni_aio *aio)
{
dtls_ep *ep = arg;
nni_mtx_lock(&ep->mtx);
if (!nni_aio_start(aio, dtls_ep_cancel, ep)) {
nni_mtx_unlock(&ep->mtx);
return;
}
if (ep->closed) {
nni_aio_finish_error(aio, NNG_ECLOSED);
nni_mtx_unlock(&ep->mtx);
return;
}
if (!nni_list_empty(&ep->connaios)) {
nni_aio_finish_error(aio, NNG_EBUSY);
nni_mtx_unlock(&ep->mtx);
return;
}
ep->dialer = true;
NNI_ASSERT(nni_list_empty(&ep->connaios));
nni_list_append(&ep->connaios, aio);
if (ep->started) {
nni_mtx_unlock(&ep->mtx);
return;
}
// lookup the IP address
memset(&ep->resolv, 0, sizeof(ep->resolv));
ep->resolv.ri_family = ep->af;
ep->resolv.ri_host = ep->url->u_hostname;
ep->resolv.ri_port = ep->url->u_port;
ep->resolv.ri_passive = false;
ep->resolv.ri_sa = &ep->peer_sa;
nni_aio_set_timeout(&ep->resaio, NNI_SECOND * 5);
nni_resolv(&ep->resolv, &ep->resaio);
// wake up for retries
nni_aio_abort(&ep->timeaio, NNG_EINTR);
nni_mtx_unlock(&ep->mtx);
}
static nng_err
dtls_ep_get_port(void *arg, void *buf, size_t *szp, nni_type t)
{
dtls_ep *ep = arg;
nng_sockaddr sa;
int port;
uint8_t *paddr;
nni_mtx_lock(&ep->mtx);
if (ep->udp != NULL) {
(void) nng_udp_sockname(ep->udp, &sa);
} else {
sa = ep->self_sa;
}
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;
}
nni_mtx_unlock(&ep->mtx);
if (paddr == NULL) {
return (NNG_ESTATE);
}
NNI_GET16(paddr, port);
return (nni_copyout_int(port, buf, szp, t));
}
static nng_err
dtls_ep_get_recvmaxsz(void *arg, void *v, size_t *szp, nni_opt_type t)
{
dtls_ep *ep = arg;
nng_err rv;
nni_mtx_lock(&ep->mtx);
rv = nni_copyout_size(ep->rcvmax, v, szp, t);
nni_mtx_unlock(&ep->mtx);
return (rv);
}
static nng_err
dtls_ep_set_recvmaxsz(void *arg, const void *v, size_t sz, nni_opt_type t)
{
dtls_ep *ep = arg;
size_t val;
nng_err rv;
if ((rv = nni_copyin_size(&val, v, sz, 0, NNG_DTLS_RECVMAX, t)) == 0) {
if ((val == 0) || (val > NNG_DTLS_RECVMAX)) {
val = NNG_DTLS_RECVMAX;
}
nni_mtx_lock(&ep->mtx);
if (ep->started) {
nni_mtx_unlock(&ep->mtx);
return (NNG_EBUSY);
}
ep->rcvmax = (uint16_t) val;
nni_stat_set_value(&ep->st_rcv_max, val);
nni_mtx_unlock(&ep->mtx);
}
return (rv);
}
static nng_err
dtls_ep_set_tls(void *arg, nng_tls_config *cfg)
{
dtls_ep *ep = arg;
nni_mtx_lock(&ep->mtx);
if (ep->started) {
nni_mtx_unlock(&ep->mtx);
return (NNG_EBUSY);
}
nng_tls_config *old = ep->tlscfg;
ep->tlscfg = cfg;
nng_tls_config_hold(cfg);
if (old != NULL) {
nng_tls_config_free(old);
}
nni_mtx_unlock(&ep->mtx);
return (NNG_OK);
}
static nng_err
dtls_ep_get_tls(void *arg, nng_tls_config **cfgp)
{
dtls_ep *ep = arg;
nni_mtx_lock(&ep->mtx);
*cfgp = ep->tlscfg;
nni_mtx_unlock(&ep->mtx);
return (NNG_OK);
}
// this just looks for pipes waiting for an aio, and aios waiting for
// a connection, and matches them together.
static void
dtls_ep_match(dtls_ep *ep)
{
nng_aio *aio = nni_list_first(&ep->connaios);
dtls_pipe *p = nni_list_first(&ep->connpipes);
if ((aio == NULL) || (p == NULL)) {
return;
}
nni_aio_list_remove(aio);
nni_list_remove(&ep->connpipes, p);
nni_aio_set_output(aio, 0, p->npipe);
nni_aio_finish(aio, 0, 0);
}
static void
dtls_ep_start(dtls_ep *ep)
{
ep->started = true;
dtls_start_rx(ep);
}
static nng_err
dtls_ep_bind(void *arg, nng_url *url)
{
dtls_ep *ep = arg;
nng_err rv;
nni_mtx_lock(&ep->mtx);
if (ep->started) {
nni_mtx_unlock(&ep->mtx);
return (NNG_EBUSY);
}
rv = nng_udp_open(&ep->udp, &ep->self_sa);
if (rv != NNG_OK) {
nni_mtx_unlock(&ep->mtx);
return (rv);
}
nng_sockaddr sa;
nng_udp_sockname(ep->udp, &sa);
url->u_port = nng_sockaddr_port(&sa);
dtls_ep_start(ep);
nni_mtx_unlock(&ep->mtx);
return (rv);
}
static void
dtls_ep_accept(void *arg, nni_aio *aio)
{
dtls_ep *ep = arg;
nni_aio_reset(aio);
nni_mtx_lock(&ep->mtx);
if (ep->closed) {
nni_aio_finish_error(aio, NNG_ECLOSED);
nni_mtx_unlock(&ep->mtx);
return;
}
if (!nni_aio_start(aio, dtls_ep_cancel, ep)) {
nni_mtx_unlock(&ep->mtx);
return;
}
nni_aio_list_append(&ep->connaios, aio);
dtls_ep_match(ep);
nni_mtx_unlock(&ep->mtx);
}
static nni_sp_pipe_ops dtls_pipe_ops = {
.p_size = dtls_pipe_size,
.p_init = dtls_pipe_init,
.p_fini = dtls_pipe_fini,
.p_stop = dtls_pipe_stop,
.p_send = dtls_pipe_send,
.p_recv = dtls_pipe_recv,
.p_close = dtls_pipe_close,
.p_peer = dtls_pipe_peer,
.p_getopt = dtls_pipe_getopt,
.p_peer_cert = dtls_pipe_peer_cert,
.p_peer_addr = dtls_pipe_peer_addr,
.p_self_addr = dtls_pipe_self_addr,
};
static const nni_option dtls_ep_opts[] = {
{
.o_name = NNG_OPT_RECVMAXSZ,
.o_get = dtls_ep_get_recvmaxsz,
.o_set = dtls_ep_set_recvmaxsz,
},
{
.o_name = NNG_OPT_BOUND_PORT,
.o_get = dtls_ep_get_port,
},
// terminate list
{
.o_name = NULL,
},
};
static nng_err
dtls_dialer_getopt(
void *arg, const char *name, void *buf, size_t *szp, nni_type t)
{
dtls_ep *ep = arg;
return (nni_getopt(dtls_ep_opts, name, ep, buf, szp, t));
}
static nng_err
dtls_dialer_setopt(
void *arg, const char *name, const void *buf, size_t sz, nni_type t)
{
dtls_ep *ep = arg;
return (nni_setopt(dtls_ep_opts, name, ep, buf, sz, t));
}
static nng_err
dtls_listener_getopt(
void *arg, const char *name, void *buf, size_t *szp, nni_type t)
{
dtls_ep *ep = arg;
return (nni_getopt(dtls_ep_opts, name, ep, buf, szp, t));
}
static nng_err
dtls_listener_setopt(
void *arg, const char *name, const void *buf, size_t sz, nni_type t)
{
dtls_ep *ep = arg;
return (nni_setopt(dtls_ep_opts, name, ep, buf, sz, t));
}
static nni_sp_dialer_ops dtls_dialer_ops = {
.d_size = sizeof(dtls_ep),
.d_init = dtls_dialer_init,
.d_fini = dtls_ep_fini,
.d_connect = dtls_ep_connect,
.d_close = dtls_ep_close,
.d_stop = dtls_ep_stop,
.d_set_tls = dtls_ep_set_tls,
.d_get_tls = dtls_ep_get_tls,
.d_getopt = dtls_dialer_getopt,
.d_setopt = dtls_dialer_setopt,
};
static nni_sp_listener_ops dtls_listener_ops = {
.l_size = sizeof(dtls_ep),
.l_init = dtls_listener_init,
.l_fini = dtls_ep_fini,
.l_bind = dtls_ep_bind,
.l_accept = dtls_ep_accept,
.l_close = dtls_ep_close,
.l_stop = dtls_ep_stop,
.l_set_tls = dtls_ep_set_tls,
.l_get_tls = dtls_ep_get_tls,
.l_getopt = dtls_listener_getopt,
.l_setopt = dtls_listener_setopt,
};
static nni_sp_tran dtls_tran = {
.tran_scheme = "dtls",
.tran_dialer = &dtls_dialer_ops,
.tran_listener = &dtls_listener_ops,
.tran_pipe = &dtls_pipe_ops,
.tran_init = dtls_tran_init,
.tran_fini = dtls_tran_fini,
};
static nni_sp_tran dtls4_tran = {
.tran_scheme = "dtls4",
.tran_dialer = &dtls_dialer_ops,
.tran_listener = &dtls_listener_ops,
.tran_pipe = &dtls_pipe_ops,
.tran_init = dtls_tran_init,
.tran_fini = dtls_tran_fini,
};
#ifdef NNG_ENABLE_IPV6
static nni_sp_tran dtls6_tran = {
.tran_scheme = "dtls6",
.tran_dialer = &dtls_dialer_ops,
.tran_listener = &dtls_listener_ops,
.tran_pipe = &dtls_pipe_ops,
.tran_init = dtls_tran_init,
.tran_fini = dtls_tran_fini,
};
#endif
void
nni_sp_dtls_register(void)
{
nni_sp_tran_register(&dtls_tran);
nni_sp_tran_register(&dtls4_tran);
#ifdef NNG_ENABLE_IPV6
nni_sp_tran_register(&dtls6_tran);
#endif
}
|