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
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
|
// 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/nng_impl.h"
#include <string.h>
// Experimental UDP transport. Unicast only.
typedef struct udp_pipe udp_pipe;
typedef struct udp_ep udp_ep;
// OP code, 8 bits
enum udp_opcode {
OPCODE_DATA = 0,
OPCODE_CREQ = 1,
OPCODE_CACK = 2,
OPCODE_DISC = 3,
OPCODE_MESH = 4,
};
// Disconnect reason, must be 16 bits
typedef enum udp_disc_reason {
DISC_CLOSED = 0, // normal close
DISC_TYPE = 1, // bad SP type
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
} udp_disc_reason;
#ifndef NNG_UDP_TXQUEUE_LEN
#define NNG_UDP_TXQUEUE_LEN 32
#endif
#ifndef NNG_UDP_RXQUEUE_LEN
#define NNG_UDP_RXQUEUE_LEN 16
#endif
#ifndef NNG_UDP_RECVMAX
#define NNG_UDP_RECVMAX 65000 // largest permitted by spec
#endif
#ifndef NNG_UDP_COPYMAX // threshold for copying instead of loan up
#define NNG_UDP_COPYMAX 1024
#endif
#ifndef NNG_UDP_REFRESH
#define NNG_UDP_REFRESH (5 * NNI_SECOND)
#endif
#ifndef NNG_UDP_CONNRETRY
#define NNG_UDP_CONNRETRY (NNI_SECOND / 5)
#endif
#define UDP_EP_ROLE(ep) ((ep)->dialer ? "dialer " : "listener")
// NB: Each of the following messages is exactly 20 bytes in size
typedef struct udp_sp_msg {
uint8_t us_ver;
uint8_t us_op_code;
uint16_t us_type;
uint16_t us_params[2];
} udp_sp_msg;
#define us_length us_params[0] // for DATA requests
#define us_recvmax us_params[0] // for CREQ, CACK
#define us_refresh us_params[1] // for CREQ, CACK
#define us_reason us_params[0] // for DISC
// Like a NIC driver, this is a "descriptor" for UDP TX packets.
// This allows us to create a circular ring of these to support
// queueing for TX gracefully.
typedef struct udp_txdesc {
udp_sp_msg header; // UDP transport message headers
nni_msg *payload; // may be null, only for data messages
nng_sockaddr sa;
bool submitted; // true if submitted
} udp_txdesc;
typedef struct udp_txring {
udp_txdesc *descs;
uint16_t head;
uint16_t tail;
uint16_t count;
uint16_t size;
} udp_txring;
typedef enum {
PIPE_CONN_INIT, // pipe is created, but not yet matched to a peer
PIPE_CONN_MATCH, // pipe matched to peer, but not added to SP socket
PIPE_CONN_DONE, // pipe is fully owned by the SP socket
} udp_pipe_state;
#define UDP_TXRING_SZ 128
// UDP pipe resend (CREQ) in msec (nng_duration)
#define UDP_PIPE_REFRESH(p) ((p)->refresh)
// UDP pipe timeout in msec (nng_duration)
#define UDP_PIPE_TIMEOUT(p) ((p)->refresh * 5)
struct udp_pipe {
udp_ep *ep;
nni_pipe *npipe;
nng_sockaddr peer_addr;
uint16_t peer;
uint16_t proto;
uint64_t id;
uint32_t self_id;
uint32_t peer_id;
uint16_t sndmax; // peer's max recv size
uint16_t rcvmax; // max recv size
bool closed;
bool dialer;
nng_duration refresh; // seconds, for the protocol
nng_time next_wake;
nng_time next_creq;
nng_time expire;
nni_list_node node;
nni_lmq rx_mq;
nni_list rx_aios;
udp_pipe_state state;
};
struct udp_ep {
nng_udp *udp;
nni_mtx mtx;
uint16_t proto;
uint16_t peer;
uint16_t af; // address family
bool started;
bool closed;
bool stopped;
bool cooldown;
nng_url *url;
const char *host; // for dialers
nni_aio *useraio;
nni_aio *connaio;
nni_aio timeaio;
nni_aio resaio;
bool dialer;
bool tx_busy; // true if tx pending
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_sockaddr mesh_sa; // mesh source address (ours)
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
udp_sp_msg *rx_msg; // contains the received message header
uint16_t rcvmax; // max payload, trimmed to uint16_t
uint16_t copymax;
udp_txring tx_ring;
nni_time next_wake;
nni_aio_completions complq;
nni_resolv_item resolv;
nni_stat_item st_rcv_max;
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 udp_ep_start(udp_ep *);
static void udp_resolv_cb(void *);
static void udp_rx_cb(void *);
static void udp_recv_data(
udp_ep *ep, udp_sp_msg *msg, size_t len, const nng_sockaddr *sa);
static void udp_send_disc_full(
udp_ep *ep, const nng_sockaddr *sa, udp_disc_reason reason);
static void udp_send_disc(udp_ep *ep, udp_pipe *p, udp_disc_reason reason);
static void udp_ep_match(udp_ep *ep);
static nng_err udp_add_pipe(udp_ep *ep, udp_pipe *p);
static void udp_remove_pipe(udp_pipe *p);
static void
udp_tran_init(void)
{
}
static void
udp_tran_fini(void)
{
}
static void
udp_pipe_close(void *arg)
{
udp_pipe *p = arg;
udp_ep *ep = p->ep;
nni_aio *aio;
nni_mtx_lock(&ep->mtx);
udp_remove_pipe(p);
udp_send_disc(ep, p, DISC_CLOSED);
while ((aio = nni_list_first(&p->rx_aios)) != NULL) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, NNG_ECLOSED);
}
nni_mtx_unlock(&ep->mtx);
}
static void
udp_pipe_stop(void *arg)
{
udp_pipe *p = arg;
udp_ep *ep = p->ep;
udp_pipe_close(arg);
nni_mtx_lock(&ep->mtx);
udp_remove_pipe(p);
nni_mtx_unlock(&ep->mtx);
}
static int
udp_pipe_init(void *arg, nni_pipe *npipe)
{
udp_pipe *p = arg;
p->npipe = npipe;
nni_aio_list_init(&p->rx_aios);
nni_lmq_init(&p->rx_mq, NNG_UDP_RXQUEUE_LEN);
return (0);
}
static nng_err
udp_pipe_start(udp_pipe *p, udp_ep *ep, const nng_sockaddr *sa)
{
nni_time now = nni_clock();
p->ep = ep;
p->proto = ep->proto;
p->peer = ep->peer;
p->peer_addr = *sa;
p->dialer = ep->dialer;
p->refresh = p->dialer ? NNG_UDP_CONNRETRY : ep->refresh;
p->rcvmax = ep->rcvmax;
p->id = nng_sockaddr_hash(sa);
p->expire = now + (p->dialer ? (5 * NNI_SECOND) : UDP_PIPE_TIMEOUT(p));
return (udp_add_pipe(ep, p));
}
static void
udp_pipe_fini(void *arg)
{
udp_pipe *p = arg;
nng_msg *m;
// call with ep->mtx lock held
while (!nni_lmq_empty(&p->rx_mq)) {
nni_lmq_get(&p->rx_mq, &m);
nni_msg_free(m);
}
nni_lmq_fini(&p->rx_mq);
NNI_ASSERT(nni_list_empty(&p->rx_aios));
}
static udp_pipe *
udp_find_pipe(udp_ep *ep, const nng_sockaddr *peer_addr)
{
uint64_t id = nng_sockaddr_hash(peer_addr);
udp_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
udp_remove_pipe(udp_pipe *p)
{
// ep locked
udp_ep *ep = p->ep;
uint64_t id = p->id;
if (id == 0) {
return;
}
p->id = 0;
for (;;) {
udp_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 (p->state < PIPE_CONN_DONE) {
nni_list_node_remove(&p->node);
nni_pipe_rele(p->npipe);
}
}
static nng_err
udp_add_pipe(udp_ep *ep, udp_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;
}
}
return (nni_id_set(&ep->pipes, id, p));
}
static void
udp_pipe_schedule(udp_pipe *p)
{
udp_ep *ep = p->ep;
bool changed = false;
if (p->expire < ep->next_wake) {
ep->next_wake = p->expire;
changed = true;
}
if (p->next_wake < ep->next_wake) {
ep->next_wake = p->next_wake;
changed = true;
}
if (changed) {
nni_aio_abort(&ep->timeaio, NNG_EINTR);
}
}
static void
udp_start_rx(udp_ep *ep)
{
nni_iov iov;
if (ep->closed) {
return;
}
// We use this trick to collect the message header so that we can
// do the entire message in a single iov, which avoids the need to
// scatter/gather (which can be problematic for platforms that cannot
// do scatter/gather due to missing recvmsg.)
(void) nni_msg_insert(ep->rx_payload, NULL, sizeof(udp_sp_msg));
iov.iov_buf = nni_msg_body(ep->rx_payload);
iov.iov_len = nni_msg_len(ep->rx_payload);
ep->rx_msg = nni_msg_body(ep->rx_payload);
nni_msg_trim(ep->rx_payload, sizeof(udp_sp_msg));
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
udp_start_tx(udp_ep *ep)
{
udp_txring *ring = &ep->tx_ring;
udp_txdesc *desc;
nng_msg *msg;
if ((!ring->count) || (!ep->started) || ep->tx_busy || ep->stopped) {
return;
}
ep->tx_busy = true;
// NB: This does not advance the tail yet.
// The tail will be advanced when the operation is complete.
desc = &ring->descs[ring->tail];
nni_iov iov[3];
int niov = 0;
NNI_ASSERT(desc->submitted);
iov[0].iov_buf = &desc->header;
iov[0].iov_len = sizeof(desc->header);
niov++;
if ((msg = desc->payload) != NULL) {
if (nni_msg_header_len(msg) > 0) {
iov[niov].iov_buf = nni_msg_header(msg);
iov[niov].iov_len = nni_msg_header_len(msg);
niov++;
}
if (nni_msg_len(msg) > 0) {
iov[niov].iov_buf = nni_msg_body(msg);
iov[niov].iov_len = nni_msg_len(msg);
niov++;
}
}
nni_aio_set_input(&ep->tx_aio, 0, &desc->sa);
nni_aio_set_iov(&ep->tx_aio, niov, iov);
// it should *never* take this long, but allow for ARP resolution
nni_aio_set_timeout(&ep->tx_aio, NNI_SECOND * 10);
nng_udp_send(ep->udp, &ep->tx_aio);
}
static void
udp_queue_tx(
udp_ep *ep, const nng_sockaddr *sa, udp_sp_msg *msg, nni_msg *payload)
{
udp_txring *ring = &ep->tx_ring;
udp_txdesc *desc = &ring->descs[ring->head];
if (ring->count == ring->size || !ep->started) {
// ring is full
nni_stat_inc(&ep->st_snd_nobuf, 1);
if (payload != NULL) {
nni_msg_free(payload);
}
return;
}
#ifdef NNG_LITTLE_ENDIAN
// This covers modern GCC, clang, Visual Studio.
desc->header = *msg;
#else
// Fix the endianness, so other routines don't have to.
// We only have to do this for systems that are not known
// (at compile time) to be little endian.
desc->header.us_ver = 0x1;
desc->header.us_op_code = msg->us_op_code;
NNI_PUT16LE(&desc->header.us_type, msg->us_type);
NNI_PUT16LE(&desc->header.us_params[0], msg->us_params[0]);
NNI_PUT16LE(&desc->header.us_params[1], msg->us_params[1]);
#endif
desc->payload = payload;
desc->sa = *sa;
desc->submitted = true;
ring->count++;
ring->head++;
if (ring->head == ring->size) {
ring->head = 0;
}
udp_start_tx(ep);
}
static void
udp_finish_tx(udp_ep *ep)
{
udp_txring *ring = &ep->tx_ring;
udp_txdesc *desc;
NNI_ASSERT(ring->count > 0);
desc = &ring->descs[ring->tail];
NNI_ASSERT(desc->submitted);
if (desc->payload != NULL) {
nni_msg_free(desc->payload);
desc->payload = NULL;
}
desc->submitted = false;
ring->tail++;
ring->count--;
if (ring->tail == ring->size) {
ring->tail = 0;
}
ep->tx_busy = false;
// possibly start another tx going
udp_start_tx(ep);
}
static void
udp_send_disc(udp_ep *ep, udp_pipe *p, udp_disc_reason reason)
{
nni_aio *aio;
if (p->closed) {
return;
}
p->closed = true;
while ((aio = nni_list_first(&p->rx_aios)) != NULL) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, NNG_ECLOSED);
}
udp_send_disc_full(ep, &p->peer_addr, reason);
nni_pipe_close(p->npipe);
}
static void
udp_send_disc_full(udp_ep *ep, const nng_sockaddr *sa, udp_disc_reason reason)
{
udp_sp_msg disc;
disc.us_ver = 0x1;
disc.us_op_code = OPCODE_DISC;
disc.us_type = ep->proto;
disc.us_reason = (uint16_t) reason;
disc.us_params[1] = 0;
udp_queue_tx(ep, sa, (void *) &disc, NULL);
}
static void
udp_send_creq(udp_ep *ep, udp_pipe *p)
{
udp_sp_msg creq;
creq.us_ver = 0x1;
creq.us_op_code = OPCODE_CREQ;
creq.us_type = p->proto;
creq.us_recvmax = p->rcvmax;
creq.us_refresh = (p->refresh + NNI_SECOND - 1) / NNI_SECOND;
p->next_creq = nni_clock() + UDP_PIPE_REFRESH(p);
p->next_wake = p->next_creq;
udp_pipe_schedule(p);
udp_queue_tx(ep, &p->peer_addr, (void *) &creq, NULL);
}
static void
udp_send_cack(udp_ep *ep, udp_pipe *p)
{
udp_sp_msg cack;
cack.us_ver = 0x01;
cack.us_op_code = OPCODE_CACK;
cack.us_type = p->proto;
cack.us_recvmax = p->rcvmax;
cack.us_refresh = (p->refresh + NNI_SECOND - 1) / NNI_SECOND;
udp_queue_tx(ep, &p->peer_addr, (void *) &cack, NULL);
}
static void
udp_recv_disc(udp_ep *ep, udp_sp_msg *disc, const nng_sockaddr *sa)
{
udp_pipe *p;
nni_aio *aio;
char buf[NNG_MAXADDRSTRLEN];
nng_log_debug("NNG-UDP-DISC", "Received disconnect from %s reason %d",
nng_str_sockaddr(sa, buf, sizeof(buf)), disc->us_reason);
p = udp_find_pipe(ep, sa);
if (p != NULL) {
p->closed = true;
while ((aio = nni_list_first(&p->rx_aios)) != NULL) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, NNG_ECLOSED);
}
nni_pipe_close(p->npipe);
}
}
// Receive data for the pipe. Returns true if we used
// the message, false otherwise.
static void
udp_recv_data(udp_ep *ep, udp_sp_msg *dreq, size_t len, const nng_sockaddr *sa)
{
// NB: ep mtx is locked
udp_pipe *p;
nni_aio *aio;
nni_msg *msg;
nni_time now;
if ((p = udp_find_pipe(ep, sa)) == NULL) {
nni_stat_inc(&ep->st_rcv_nomatch, 1);
return;
}
now = nni_clock();
// Make sure the message wasn't truncated, and that it fits within
// our maximum agreed upon payload.
if ((dreq->us_length > len) || (dreq->us_length > p->rcvmax)) {
nni_stat_inc(&ep->st_rcv_toobig, 1);
udp_send_disc(ep, p, DISC_MSGSIZE);
return;
}
p->expire = now + UDP_PIPE_TIMEOUT(p);
p->next_wake = now + UDP_PIPE_REFRESH(p);
udp_pipe_schedule(p);
// trim the message down to its
nni_msg_chop(
ep->rx_payload, nni_msg_len(ep->rx_payload) - dreq->us_length);
// We have a choice to make. Drop this message (easiest), or
// drop the oldest. We drop the oldest because generally we
// find that applications prefer to have more recent data rather
// than keeping stale data.
if (nni_lmq_full(&p->rx_mq)) {
nni_msg *old;
(void) nni_lmq_get(&p->rx_mq, &old);
nni_msg_free(old);
nni_stat_inc(&ep->st_rcv_nobuf, 1);
}
// Short message, just alloc and copy
if (len <= ep->copymax) {
nni_stat_inc(&ep->st_rcv_copy, 1);
if (nng_msg_alloc(&msg, len) != 0) {
if (p->npipe != NULL) {
nni_pipe_bump_error(p->npipe, NNG_ENOMEM);
}
return;
}
nni_msg_set_address(msg, sa);
memcpy(nni_msg_body(msg), nni_msg_body(ep->rx_payload), len);
nni_lmq_put(&p->rx_mq, msg);
nni_msg_realloc(ep->rx_payload, ep->rcvmax);
} else {
nni_stat_inc(&ep->st_rcv_nocopy, 1);
// Message size larger than copy break, do zero copy
msg = ep->rx_payload;
if (nng_msg_alloc(&ep->rx_payload, ep->rcvmax) != 0) {
ep->rx_payload = msg; // make sure we put it back
if (p->npipe != NULL) {
nni_pipe_bump_error(p->npipe, NNG_ENOMEM);
}
return;
}
if (len > nng_msg_len(msg)) {
// chop off any unfilled tail
nng_msg_chop(msg, nng_msg_len(msg) - len);
}
nni_msg_set_address(msg, sa);
nni_lmq_put(&p->rx_mq, msg);
}
while (((aio = nni_list_first(&p->rx_aios)) != NULL) &&
(!nni_lmq_empty(&p->rx_mq))) {
nni_aio_list_remove(aio);
nni_lmq_get(&p->rx_mq, &msg);
nni_aio_set_msg(aio, msg);
nni_aio_completions_add(
&ep->complq, aio, 0, nni_aio_count(aio));
}
}
static void
udp_recv_creq(udp_ep *ep, udp_sp_msg *creq, nng_sockaddr *sa)
{
udp_pipe *p;
nni_time now;
now = nni_clock();
if (ep->closed) {
// endpoint is closing down, just drop it without further ado
return;
}
if (ep->dialer) {
// dialers do not accept CREQ requests
udp_send_disc_full(ep, sa, DISC_REFUSED);
return;
}
if ((p = udp_find_pipe(ep, sa))) {
if (p->peer != creq->us_type) {
udp_send_disc(ep, p, DISC_TYPE);
return;
}
// so we know who it is from.. this is a refresh.
if (creq->us_refresh == 0) {
udp_send_disc(ep, p, DISC_NEGO);
return;
}
if ((creq->us_refresh * NNI_SECOND) < p->refresh) {
p->refresh = creq->us_refresh * NNI_SECOND;
}
p->next_wake = now + UDP_PIPE_REFRESH(p);
p->expire = now + UDP_PIPE_TIMEOUT(p);
udp_pipe_schedule(p);
udp_send_cack(ep, p);
return;
}
// new pipe
if (creq->us_refresh == 0) {
udp_send_disc_full(ep, sa, DISC_NEGO);
return;
}
if (nni_pipe_alloc_listener((void **) &p, ep->nlistener) != 0) {
udp_send_disc_full(ep, sa, DISC_NOBUF);
return;
}
if (udp_pipe_start(p, ep, sa) != NNG_OK) {
udp_send_disc(ep, p, DISC_NOBUF);
nni_pipe_close(p->npipe);
return;
}
if ((creq->us_refresh * NNI_SECOND) < p->refresh) {
p->refresh = (creq->us_refresh * NNI_SECOND);
}
p->peer = creq->us_type;
p->sndmax = creq->us_recvmax;
p->next_wake = now + UDP_PIPE_REFRESH(p);
udp_pipe_schedule(p);
p->state = PIPE_CONN_MATCH;
nni_list_append(&ep->connpipes, p);
udp_send_cack(ep, p);
udp_ep_match(ep);
}
static void
udp_recv_cack(udp_ep *ep, udp_sp_msg *cack, const nng_sockaddr *sa)
{
udp_pipe *p;
nni_time now;
if ((p = udp_find_pipe(ep, sa)) && (!p->closed)) {
if (p->peer != cack->us_type) {
udp_send_disc(ep, p, DISC_TYPE);
return;
}
// so we know who it is from.. this is a refresh.
p->sndmax = cack->us_recvmax;
p->peer = cack->us_type;
if (cack->us_refresh == 0) {
udp_send_disc(ep, p, DISC_NEGO);
return;
}
// Always reset this, as dialers may have started with an
// unreasonably low value.
p->refresh = ep->refresh;
if ((cack->us_refresh * NNI_SECOND) < p->refresh) {
p->refresh = cack->us_refresh * NNI_SECOND;
}
now = nni_clock();
p->next_wake = now + UDP_PIPE_REFRESH(p);
p->expire = now + UDP_PIPE_TIMEOUT(p);
udp_pipe_schedule(p);
if (p->state < PIPE_CONN_MATCH) {
p->state = PIPE_CONN_MATCH;
nni_list_append(&ep->connpipes, p);
udp_ep_match(ep);
}
return;
}
}
static void
udp_tx_cb(void *arg)
{
udp_ep *ep = arg;
nni_mtx_lock(&ep->mtx);
udp_finish_tx(ep);
nni_mtx_unlock(&ep->mtx);
}
// In the case of unicast UDP, we don't know
// whether the message arrived from a connected peer as part of a
// logical connection, or is a message related to connection management.
static void
udp_rx_cb(void *arg)
{
udp_ep *ep = arg;
nni_aio *aio = &ep->rx_aio;
int rv;
size_t n;
udp_sp_msg *hdr;
nng_sockaddr *sa;
nni_aio_completions complq;
// for a received packet we are either receiving it for a
// connection we already have established, or for a new connection.
// Dialers cannot receive connection requests (as a safety
// precaution).
nni_mtx_lock(&ep->mtx);
if ((rv = nni_aio_result(aio)) != 0) {
// 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:
ep->cooldown = false;
goto finish;
break;
default:
ep->cooldown = true;
nni_sleep_aio(5, aio);
nni_mtx_unlock(&ep->mtx);
return;
}
}
if (ep->cooldown) {
ep->cooldown = false;
goto finish;
}
// Received message will be in the ep rx header.
hdr = ep->rx_msg;
sa = &ep->rx_sa;
n = nng_aio_count(aio);
if ((n >= sizeof(*hdr)) && (hdr->us_ver == 1)) {
n -= sizeof(*hdr);
#ifndef NNG_LITTLE_ENDIAN
// Fix the endianness, so other routines don't have to.
// We only have to do this for systems that are not known
// (at compile time) to be little endian.
hdr->us_type = NNI_GET16LE(&hdr->us_type);
hdr->us_params[0] = NNI_GET16LE(&hdr->us_params[0]);
hdr->us_params[1] = NNI_GET16LE(&hdr->us_params[1]);
#endif
switch (hdr->us_op_code) {
case OPCODE_DATA:
udp_recv_data(ep, hdr, n, sa);
break;
case OPCODE_CREQ:
udp_recv_creq(ep, hdr, sa);
break;
case OPCODE_CACK:
udp_recv_cack(ep, hdr, sa);
break;
case OPCODE_DISC:
udp_recv_disc(ep, hdr, sa);
break;
case OPCODE_MESH: // TODO:
// udp_recv_mesh(ep, &hdr->mesh, sa);
// break;
default:
udp_send_disc_full(ep, sa, DISC_PROTO);
break;
}
}
finish:
// start another receive
udp_start_rx(ep);
// grab the list of completions so we can finish them.
complq = ep->complq;
nni_aio_completions_init(&ep->complq);
nni_mtx_unlock(&ep->mtx);
// now run the completions -- synchronously
nni_aio_completions_run(&complq);
}
static void
udp_pipe_send(void *arg, nni_aio *aio)
{
udp_pipe *p = arg;
udp_ep *ep;
udp_sp_msg dreq;
nng_msg *msg;
size_t count = 0;
msg = nni_aio_get_msg(aio);
ep = p->ep;
if (msg != NULL) {
count = nni_msg_len(msg) + nni_msg_header_len(msg);
}
nni_aio_reset(aio);
nni_mtx_lock(&ep->mtx);
if ((nni_msg_len(msg) + nni_msg_header_len(msg)) > p->sndmax) {
nni_mtx_unlock(&ep->mtx);
// 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_aio_finish(aio, 0, count);
nni_stat_inc(&ep->st_snd_toobig, 1);
nni_msg_free(msg);
return;
}
dreq.us_ver = 1;
dreq.us_type = ep->proto;
dreq.us_op_code = OPCODE_DATA;
dreq.us_length = (uint16_t) count;
// Just queue it, or fail it.
udp_queue_tx(ep, &p->peer_addr, (void *) &dreq, msg);
nni_mtx_unlock(&ep->mtx);
nni_aio_finish(aio, 0, count);
}
static void
udp_pipe_recv_cancel(nni_aio *aio, void *arg, nng_err rv)
{
udp_pipe *p = arg;
udp_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
udp_pipe_recv(void *arg, nni_aio *aio)
{
udp_pipe *p = arg;
udp_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, udp_pipe_recv_cancel, p)) {
nni_mtx_unlock(&ep->mtx);
return;
}
nni_list_append(&p->rx_aios, aio);
nni_mtx_unlock(&ep->mtx);
}
static uint16_t
udp_pipe_peer(void *arg)
{
udp_pipe *p = arg;
return (p->peer);
}
static nng_err
udp_pipe_get_recvmax(void *arg, void *v, size_t *szp, nni_type t)
{
udp_pipe *p = arg;
udp_ep *ep = p->ep;
nng_err rv;
nni_mtx_lock(&ep->mtx);
rv = nni_copyout_size(p->rcvmax, v, szp, t);
nni_mtx_unlock(&ep->mtx);
return (rv);
}
static nng_err
udp_pipe_get_remaddr(void *arg, void *v, size_t *szp, nni_type t)
{
udp_pipe *p = arg;
udp_ep *ep = p->ep;
nng_err rv;
nni_mtx_lock(&ep->mtx);
rv = nni_copyout_sockaddr(&p->peer_addr, v, szp, t);
nni_mtx_unlock(&ep->mtx);
return (rv);
}
static nni_option udp_pipe_options[] = {
{
.o_name = NNG_OPT_RECVMAXSZ,
.o_get = udp_pipe_get_recvmax,
},
{
.o_name = NNG_OPT_REMADDR,
.o_get = udp_pipe_get_remaddr,
},
{
.o_name = NULL,
},
};
static nng_err
udp_pipe_getopt(
void *arg, const char *name, void *buf, size_t *szp, nni_type t)
{
udp_pipe *p = arg;
int rv;
rv = nni_getopt(udp_pipe_options, name, p, buf, szp, t);
return (rv);
}
static void
udp_ep_fini(void *arg)
{
udp_ep *ep = arg;
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);
}
for (int i = 0; i < ep->tx_ring.size; i++) {
nni_msg_free(ep->tx_ring.descs[i].payload);
ep->tx_ring.descs[i].payload = NULL;
}
nni_msg_free(ep->rx_payload); // safe even if msg is null
nni_id_map_fini(&ep->pipes);
NNI_FREE_STRUCTS(ep->tx_ring.descs, ep->tx_ring.size);
}
static void
udp_ep_close(void *arg)
{
udp_ep *ep = arg;
udp_pipe *p;
nni_aio *aio;
uint32_t cursor;
uint64_t key;
nni_mtx_lock(&ep->mtx);
ep->closed = true;
// leave tx open so we can send disconnects
nni_aio_close(&ep->resaio);
nni_aio_close(&ep->rx_aio);
nni_aio_close(&ep->timeaio);
// close all the underlying pipes, so the peer can see it.
while (nni_id_visit(&ep->pipes, &key, (void **) &p, &cursor)) {
nni_pipe_close(p->npipe);
}
while ((aio = nni_list_first(&ep->connaios)) != NULL) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, NNG_ECONNABORTED);
}
nni_mtx_unlock(&ep->mtx);
}
static void
udp_ep_stop(void *arg)
{
udp_ep *ep = arg;
nni_aio_stop(&ep->resaio);
nni_aio_stop(&ep->rx_aio);
nni_aio_stop(&ep->timeaio);
// We optionally linger a little bit (up to a half second)
// so that the disconnect messages can get pushed out. On
// most systems this should only take a single millisecond.
nni_time linger =
nni_clock() + NNI_SECOND / 2; // half second to drain, max
nni_mtx_lock(&ep->mtx);
while ((ep->tx_ring.count > 0) && (nni_clock() < linger)) {
nni_mtx_unlock(&ep->mtx);
nng_msleep(1);
nni_mtx_lock(&ep->mtx);
}
if (ep->tx_ring.count > 0) {
nng_log_warn("NNG-UDP-LINGER",
"Lingering timed out on endpoint close, peer "
"notifications dropped");
}
ep->stopped = true;
nni_mtx_unlock(&ep->mtx);
// finally close the tx channel
nni_aio_stop(&ep->tx_aio);
}
// timer handler - sends out additional creqs as needed,
// reaps stale connections, and handles linger.
static void
udp_timer_cb(void *arg)
{
udp_ep *ep = arg;
udp_pipe *p;
int 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 = ep->refresh;
ep->next_wake = NNI_TIME_NEVER;
while (nni_id_visit(&ep->pipes, NULL, (void **) &p, &cursor)) {
if (now > p->expire) {
char buf[128];
nni_aio *aio;
nng_log_info("NNG-UDP-INACTIVE",
"Pipe peer %s timed out due to inactivity",
nng_str_sockaddr(&p->peer_addr, buf, sizeof(buf)));
// Possibly alert the dialer, so it can restart a
// new attempt.
if ((ep->dialer) && (p->peer_id == 0) &&
(aio = nni_list_first(&ep->connaios))) {
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, NNG_ETIMEDOUT);
}
// If we're still on the connect list, then we need
// take responsibility for cleaning this up.
if (nni_list_node_active(&p->node)) {
nni_pipe_close(p->npipe);
continue;
}
// This will probably not be received by the peer,
// since we aren't getting anything from them. But
// having it on the wire may help debugging later.
nni_stat_inc(&ep->st_peer_inactive, 1);
udp_send_disc(ep, p, DISC_INACTIVE);
continue;
}
if (p->dialer && now > p->next_creq) {
udp_send_creq(ep, p);
}
if (p->next_wake < ep->next_wake) {
ep->next_wake = p->next_wake;
}
}
refresh = ep->next_wake == NNI_TIME_NEVER
? NNG_DURATION_INFINITE
: (nng_duration) (ep->next_wake - now);
nni_sleep_aio(refresh, &ep->timeaio);
nni_mtx_unlock(&ep->mtx);
}
static int
udp_ep_init(
udp_ep *ep, nng_url *url, nni_sock *sock, nni_dialer *d, nni_listener *l)
{
int rv;
nni_mtx_init(&ep->mtx);
nni_id_map_init(&ep->pipes, 1, 0xFFFFFFFF, true);
NNI_LIST_INIT(&ep->connpipes, udp_pipe, node);
nni_aio_list_init(&ep->connaios);
nni_aio_init(&ep->rx_aio, udp_rx_cb, ep);
nni_aio_init(&ep->tx_aio, udp_tx_cb, ep);
nni_aio_init(&ep->timeaio, udp_timer_cb, ep);
nni_aio_init(&ep->resaio, udp_resolv_cb, ep);
nni_aio_completions_init(&ep->complq);
ep->tx_ring.descs =
NNI_ALLOC_STRUCTS(ep->tx_ring.descs, NNG_UDP_TXQUEUE_LEN);
if (ep->tx_ring.descs == NULL) {
NNI_FREE_STRUCT(ep);
return (NNG_ENOMEM);
}
ep->tx_ring.size = NNG_UDP_TXQUEUE_LEN;
if (strcmp(url->u_scheme, "udp") == 0) {
ep->af = NNG_AF_UNSPEC;
} else if (strcmp(url->u_scheme, "udp4") == 0) {
ep->af = NNG_AF_INET;
} else if (strcmp(url->u_scheme, "udp6") == 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_UDP_REFRESH; // one minute by default
ep->rcvmax = NNG_UDP_RECVMAX;
ep->copymax = NNG_UDP_COPYMAX;
if ((rv = nni_msg_alloc(&ep->rx_payload, ep->rcvmax) != 0)) {
NNI_FREE_STRUCTS(ep->tx_ring.descs, NNG_UDP_TXQUEUE_LEN);
return (rv);
}
NNI_STAT_LOCK(rcv_max_info, "rcv_max", "maximum receive size",
NNG_STAT_LEVEL, NNG_UNIT_BYTES);
NNI_STAT_LOCK(copy_max_info, "copy_max",
"threshold to switch to loan-up", 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_copy_info, "rcv_copy",
"received messages copied (small)", NNG_STAT_COUNTER,
NNG_UNIT_MESSAGES);
NNI_STAT_LOCK(rcv_nocopy_info, "rcv_nocopy",
"received messages zero copy (large)", 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_copy_max, ©_max_info, &ep->mtx);
nni_stat_init_lock(&ep->st_rcv_copy, &rcv_copy_info, &ep->mtx);
nni_stat_init_lock(&ep->st_rcv_nocopy, &rcv_nocopy_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_copy_max);
nni_listener_add_stat(l, &ep->st_rcv_copy);
nni_listener_add_stat(l, &ep->st_rcv_nocopy);
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_copy_max);
nni_dialer_add_stat(d, &ep->st_rcv_copy);
nni_dialer_add_stat(d, &ep->st_rcv_nocopy);
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);
return (0);
}
static int
udp_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 (0);
}
static nng_err
udp_dialer_init(void *arg, nng_url *url, nni_dialer *ndialer)
{
udp_ep *ep = arg;
nng_err rv;
nni_sock *sock = nni_dialer_sock(ndialer);
ep->ndialer = ndialer;
if ((rv = udp_ep_init(ep, url, sock, ndialer, NULL)) != NNG_OK) {
return (rv);
}
if ((rv = udp_check_url(url, false)) != NNG_OK) {
return (rv);
}
return (NNG_OK);
}
static nng_err
udp_listener_init(void *arg, nng_url *url, nni_listener *nlistener)
{
udp_ep *ep = arg;
nng_err rv;
nni_sock *sock = nni_listener_sock(nlistener);
ep->nlistener = nlistener;
if ((rv = udp_ep_init(ep, url, sock, NULL, nlistener)) != NNG_OK) {
return (rv);
}
// Check for invalid URL components.
if (((rv = udp_check_url(url, true)) != 0) ||
((rv = nni_url_to_address(&ep->self_sa, url)) != NNG_OK)) {
return (rv);
}
return (0);
}
static void
udp_ep_cancel(nni_aio *aio, void *arg, nng_err rv)
{
udp_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
udp_resolv_cb(void *arg)
{
udp_ep *ep = arg;
udp_pipe *p;
nni_aio *aio;
int 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_mtx_unlock(&ep->mtx);
nni_aio_finish_error(aio, NNG_ECLOSED);
return;
}
if ((rv = nni_aio_result(&ep->resaio)) != 0) {
nni_aio_list_remove(aio);
nni_mtx_unlock(&ep->mtx);
nng_log_warn("NNG-UDP-RESOLV",
"Failed resolving IP address: %s", nng_strerror(rv));
nni_aio_finish_error(aio, 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;
}
if (ep->udp == NULL) {
if ((rv = nng_udp_open(&ep->udp, &ep->self_sa)) != NNG_OK) {
nni_aio_list_remove(aio);
nni_mtx_unlock(&ep->mtx);
nni_aio_finish_error(aio, rv);
return;
}
}
// places a "hold" on the ep
if ((rv = nni_pipe_alloc_dialer((void **) &p, ep->ndialer)) !=
NNG_OK) {
nni_aio_list_remove(aio);
nni_mtx_unlock(&ep->mtx);
nni_aio_finish_error(aio, rv);
return;
}
if ((rv = udp_pipe_start(p, ep, &ep->peer_sa)) != NNG_OK) {
nni_aio_list_remove(aio);
nni_pipe_close(p->npipe);
nni_mtx_unlock(&ep->mtx);
nni_aio_finish_error(aio, rv);
return;
}
udp_pipe_schedule(p);
udp_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.
udp_send_creq(ep, p);
nni_mtx_unlock(&ep->mtx);
}
static void
udp_ep_connect(void *arg, nni_aio *aio)
{
udp_ep *ep = arg;
nni_mtx_lock(&ep->mtx);
if (!nni_aio_start(aio, udp_ep_cancel, ep)) {
nni_mtx_unlock(&ep->mtx);
return;
}
if (ep->closed) {
nni_mtx_unlock(&ep->mtx);
nni_aio_finish_error(aio, NNG_ECLOSED);
return;
}
if (ep->started) {
nni_mtx_unlock(&ep->mtx);
nni_aio_finish_error(aio, NNG_EBUSY);
return;
}
NNI_ASSERT(nni_list_empty(&ep->connaios));
ep->dialer = true;
nni_list_append(&ep->connaios, aio);
// 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
udp_ep_get_port(void *arg, void *buf, size_t *szp, nni_type t)
{
udp_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
udp_ep_get_locaddr(void *arg, void *v, size_t *szp, nni_opt_type t)
{
udp_ep *ep = arg;
nng_err rv;
nng_sockaddr sa;
if (ep->udp != NULL) {
(void) nng_udp_sockname(ep->udp, &sa);
} else {
sa = ep->self_sa;
}
rv = nni_copyout_sockaddr(&sa, v, szp, t);
return (rv);
}
static nng_err
udp_ep_get_remaddr(void *arg, void *v, size_t *szp, nni_opt_type t)
{
udp_ep *ep = arg;
nng_err rv;
nng_sockaddr sa;
if (!ep->dialer) {
return (NNG_ENOTSUP);
}
sa = ep->peer_sa;
rv = nni_copyout_sockaddr(&sa, v, szp, t);
return (rv);
}
static nng_err
udp_ep_get_recvmaxsz(void *arg, void *v, size_t *szp, nni_opt_type t)
{
udp_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
udp_ep_set_recvmaxsz(void *arg, const void *v, size_t sz, nni_opt_type t)
{
udp_ep *ep = arg;
size_t val;
nng_err rv;
if ((rv = nni_copyin_size(&val, v, sz, 0, 65000, t)) == NNG_OK) {
if ((val == 0) || (val > 65000)) {
val = 65000;
}
nni_mtx_lock(&ep->mtx);
if (ep->started) {
nni_mtx_unlock(&ep->mtx);
return (NNG_EBUSY);
}
ep->rcvmax = (uint16_t) val;
nni_mtx_unlock(&ep->mtx);
nni_stat_set_value(&ep->st_rcv_max, val);
}
return (rv);
}
static nng_err
udp_ep_get_copymax(void *arg, void *v, size_t *szp, nni_opt_type t)
{
udp_ep *ep = arg;
nng_err rv;
nni_mtx_lock(&ep->mtx);
rv = nni_copyout_size(ep->copymax, v, szp, t);
nni_mtx_unlock(&ep->mtx);
return (rv);
}
static nng_err
udp_ep_set_copymax(void *arg, const void *v, size_t sz, nni_opt_type t)
{
udp_ep *ep = arg;
size_t val;
nng_err rv;
if ((rv = nni_copyin_size(&val, v, sz, 0, 65000, t)) == NNG_OK) {
nni_mtx_lock(&ep->mtx);
if (ep->started) {
nni_mtx_unlock(&ep->mtx);
return (NNG_EBUSY);
}
ep->copymax = (uint16_t) val;
nni_mtx_unlock(&ep->mtx);
nni_stat_set_value(&ep->st_copy_max, val);
}
return (rv);
}
// this just looks for pipes waiting for an aio, and aios waiting for
// a connection, and matches them together.
static void
udp_ep_match(udp_ep *ep)
{
nng_aio *aio = nni_list_first(&ep->connaios);
udp_pipe *p = nni_list_first(&ep->connpipes);
if ((aio == NULL) || (p == NULL)) {
return;
}
p->state = PIPE_CONN_DONE;
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
udp_ep_start(udp_ep *ep)
{
ep->started = true;
udp_start_rx(ep);
}
static nng_err
udp_ep_bind(void *arg, nng_url *url)
{
udp_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);
udp_ep_start(ep);
nni_mtx_unlock(&ep->mtx);
return (rv);
}
static void
udp_ep_accept(void *arg, nni_aio *aio)
{
udp_ep *ep = arg;
nni_aio_reset(aio);
nni_mtx_lock(&ep->mtx);
if (ep->closed) {
nni_mtx_unlock(&ep->mtx);
nni_aio_finish_error(aio, NNG_ECLOSED);
return;
}
if (!nni_aio_start(aio, udp_ep_cancel, ep)) {
nni_mtx_unlock(&ep->mtx);
return;
}
nni_aio_list_append(&ep->connaios, aio);
udp_ep_match(ep);
nni_mtx_unlock(&ep->mtx);
}
static size_t
udp_pipe_size(void)
{
return (sizeof(udp_pipe));
}
static nni_sp_pipe_ops udp_pipe_ops = {
.p_size = udp_pipe_size,
.p_init = udp_pipe_init,
.p_fini = udp_pipe_fini,
.p_stop = udp_pipe_stop,
.p_send = udp_pipe_send,
.p_recv = udp_pipe_recv,
.p_close = udp_pipe_close,
.p_peer = udp_pipe_peer,
.p_getopt = udp_pipe_getopt,
};
static const nni_option udp_ep_opts[] = {
{
.o_name = NNG_OPT_RECVMAXSZ,
.o_get = udp_ep_get_recvmaxsz,
.o_set = udp_ep_set_recvmaxsz,
},
{
.o_name = NNG_OPT_UDP_COPY_MAX,
.o_get = udp_ep_get_copymax,
.o_set = udp_ep_set_copymax,
},
{
.o_name = NNG_OPT_LOCADDR,
.o_get = udp_ep_get_locaddr,
},
{
.o_name = NNG_OPT_REMADDR,
.o_get = udp_ep_get_remaddr,
},
{
.o_name = NNG_OPT_TCP_BOUND_PORT,
.o_get = udp_ep_get_port,
},
// terminate list
{
.o_name = NULL,
},
};
static nng_err
udp_dialer_getopt(
void *arg, const char *name, void *buf, size_t *szp, nni_type t)
{
udp_ep *ep = arg;
return (nni_getopt(udp_ep_opts, name, ep, buf, szp, t));
}
static nng_err
udp_dialer_setopt(
void *arg, const char *name, const void *buf, size_t sz, nni_type t)
{
udp_ep *ep = arg;
return (nni_setopt(udp_ep_opts, name, ep, buf, sz, t));
}
static nng_err
udp_listener_getopt(
void *arg, const char *name, void *buf, size_t *szp, nni_type t)
{
udp_ep *ep = arg;
return (nni_getopt(udp_ep_opts, name, ep, buf, szp, t));
}
static nng_err
udp_listener_setopt(
void *arg, const char *name, const void *buf, size_t sz, nni_type t)
{
udp_ep *ep = arg;
return (nni_setopt(udp_ep_opts, name, ep, buf, sz, t));
}
static nni_sp_dialer_ops udp_dialer_ops = {
.d_size = sizeof(udp_ep),
.d_init = udp_dialer_init,
.d_fini = udp_ep_fini,
.d_connect = udp_ep_connect,
.d_close = udp_ep_close,
.d_stop = udp_ep_stop,
.d_getopt = udp_dialer_getopt,
.d_setopt = udp_dialer_setopt,
};
static nni_sp_listener_ops udp_listener_ops = {
.l_size = sizeof(udp_ep),
.l_init = udp_listener_init,
.l_fini = udp_ep_fini,
.l_bind = udp_ep_bind,
.l_accept = udp_ep_accept,
.l_close = udp_ep_close,
.l_stop = udp_ep_stop,
.l_getopt = udp_listener_getopt,
.l_setopt = udp_listener_setopt,
};
static nni_sp_tran udp_tran = {
.tran_scheme = "udp",
.tran_dialer = &udp_dialer_ops,
.tran_listener = &udp_listener_ops,
.tran_pipe = &udp_pipe_ops,
.tran_init = udp_tran_init,
.tran_fini = udp_tran_fini,
};
static nni_sp_tran udp4_tran = {
.tran_scheme = "udp4",
.tran_dialer = &udp_dialer_ops,
.tran_listener = &udp_listener_ops,
.tran_pipe = &udp_pipe_ops,
.tran_init = udp_tran_init,
.tran_fini = udp_tran_fini,
};
#ifdef NNG_ENABLE_IPV6
static nni_sp_tran udp6_tran = {
.tran_scheme = "udp6",
.tran_dialer = &udp_dialer_ops,
.tran_listener = &udp_listener_ops,
.tran_pipe = &udp_pipe_ops,
.tran_init = udp_tran_init,
.tran_fini = udp_tran_fini,
};
#endif
void
nni_sp_udp_register(void)
{
nni_sp_tran_register(&udp_tran);
nni_sp_tran_register(&udp4_tran);
#ifdef NNG_ENABLE_IPV6
nni_sp_tran_register(&udp6_tran);
#endif
}
|