zhangmeng
2024-04-22 16935f4aebffdd1b6580b844391a0aa0f4f3012b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
//
// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
 
#include "core/nng_impl.h"
#include "sockimpl.h"
 
#include <stdio.h>
#include <string.h>
 
// Socket implementation.
 
static nni_list   sock_list;
static nni_id_map sock_ids;
static nni_mtx    sock_lk;
static nni_id_map ctx_ids;
static bool       inited;
 
struct nni_ctx {
    nni_list_node     c_node;
    nni_sock *        c_sock;
    nni_proto_ctx_ops c_ops;
    void *            c_data;
    size_t            c_size;
    bool              c_closed;
    unsigned          c_ref; // protected by global lock
    uint32_t          c_id;
    nng_duration      c_sndtimeo;
    nng_duration      c_rcvtimeo;
};
 
typedef struct nni_sockopt {
    nni_list_node node;
    char *        name;
    nni_type      typ;
    size_t        sz;
    void *        data;
} nni_sockopt;
 
typedef struct nni_sock_pipe_cb {
    nng_pipe_cb cb_fn;
    void *      cb_arg;
} nni_sock_pipe_cb;
 
struct nni_socket {
    nni_list_node s_node;
    nni_mtx       s_mx;
    nni_cv        s_cv;
    nni_cv        s_close_cv;
 
    uint32_t s_id;
    uint32_t s_flags;
    unsigned s_ref;  // protected by global lock
    void *   s_data; // Protocol private
    size_t   s_size;
 
    nni_msgq *s_uwq; // Upper write queue
    nni_msgq *s_urq; // Upper read queue
 
    nni_proto_id s_self_id;
    nni_proto_id s_peer_id;
 
    nni_proto_pipe_ops s_pipe_ops;
    nni_proto_sock_ops s_sock_ops;
    nni_proto_ctx_ops  s_ctx_ops;
 
    // options
    nni_duration s_sndtimeo;  // send timeout
    nni_duration s_rcvtimeo;  // receive timeout
    nni_duration s_reconn;    // reconnect time
    nni_duration s_reconnmax; // max reconnect time
    size_t       s_rcvmaxsz;  // max receive size
    nni_list     s_options;   // opts not handled by sock/proto
    char         s_name[64];  // socket name (legacy compat)
 
    nni_list s_listeners; // active listeners
    nni_list s_dialers;   // active dialers
    nni_list s_pipes;     // active pipes
    nni_list s_ctxs;      // active contexts (protected by global sock_lk)
 
    bool s_closing; // Socket is closing
    bool s_closed;  // Socket closed, protected by global lock
    bool s_ctxwait; // Waiting for contexts to close.
 
    nni_mtx          s_pipe_cbs_mtx;
    nni_sock_pipe_cb s_pipe_cbs[NNG_PIPE_EV_NUM];
 
#ifdef NNG_ENABLE_STATS
    nni_stat_item st_root;      // socket scope
    nni_stat_item st_id;        // socket id
    nni_stat_item st_name;      // socket name
    nni_stat_item st_protocol;  // socket protocol
    nni_stat_item st_dialers;   // number of dialers
    nni_stat_item st_listeners; // number of listeners
    nni_stat_item st_pipes;     // number of pipes
    nni_stat_item st_rx_bytes;  // number of bytes received
    nni_stat_item st_tx_bytes;  // number of bytes received
    nni_stat_item st_rx_msgs;   // number of msgs received
    nni_stat_item st_tx_msgs;   // number of msgs sent
    nni_stat_item st_rejects;   // pipes rejected
#endif
};
 
static void nni_ctx_destroy(nni_ctx *);
 
static void dialer_shutdown_locked(nni_dialer *);
static void listener_shutdown_locked(nni_listener *);
 
#define SOCK(s) ((nni_sock *) (s))
 
static int
sock_get_fd(void *s, unsigned flag, int *fdp)
{
    int           rv;
    nni_pollable *p;
 
    if ((flag & nni_sock_flags(SOCK(s))) == 0) {
        return (NNG_ENOTSUP);
    }
 
    if (flag == NNI_PROTO_FLAG_SND) {
        rv = nni_msgq_get_sendable(SOCK(s)->s_uwq, &p);
    } else {
        rv = nni_msgq_get_recvable(SOCK(s)->s_urq, &p);
    }
 
    if (rv == 0) {
        rv = nni_pollable_getfd(p, fdp);
    }
 
    return (rv);
}
 
static int
sock_get_sendfd(void *s, void *buf, size_t *szp, nni_type t)
{
    int fd;
    int rv;
 
    if ((rv = sock_get_fd(SOCK(s), NNI_PROTO_FLAG_SND, &fd)) != 0) {
        return (rv);
    }
    return (nni_copyout_int(fd, buf, szp, t));
}
 
static int
sock_get_recvfd(void *s, void *buf, size_t *szp, nni_type t)
{
    int fd;
    int rv;
 
    if ((rv = sock_get_fd(SOCK(s), NNI_PROTO_FLAG_RCV, &fd)) != 0) {
        return (rv);
    }
    return (nni_copyout_int(fd, buf, szp, t));
}
 
static int
sock_get_raw(void *s, void *buf, size_t *szp, nni_type t)
{
    bool raw = ((nni_sock_flags(SOCK(s)) & NNI_PROTO_FLAG_RAW) != 0);
    return (nni_copyout_bool(raw, buf, szp, t));
}
 
static int
sock_set_recvtimeo(void *s, const void *buf, size_t sz, nni_type t)
{
    return (nni_copyin_ms(&SOCK(s)->s_rcvtimeo, buf, sz, t));
}
 
static int
sock_get_recvtimeo(void *s, void *buf, size_t *szp, nni_type t)
{
    return (nni_copyout_ms(SOCK(s)->s_rcvtimeo, buf, szp, t));
}
 
static int
sock_set_sendtimeo(void *s, const void *buf, size_t sz, nni_type t)
{
    return (nni_copyin_ms(&SOCK(s)->s_sndtimeo, buf, sz, t));
}
 
static int
sock_get_sendtimeo(void *s, void *buf, size_t *szp, nni_type t)
{
    return (nni_copyout_ms(SOCK(s)->s_sndtimeo, buf, szp, t));
}
 
static int
sock_set_recvbuf(void *s, const void *buf, size_t sz, nni_type t)
{
    int len;
    int rv;
 
    if ((rv = nni_copyin_int(&len, buf, sz, 0, 8192, t)) != 0) {
        return (rv);
    }
    return (nni_msgq_resize(SOCK(s)->s_urq, len));
}
 
static int
sock_get_recvbuf(void *s, void *buf, size_t *szp, nni_type t)
{
    int len = nni_msgq_cap(SOCK(s)->s_urq);
 
    return (nni_copyout_int(len, buf, szp, t));
}
 
static int
sock_set_sendbuf(void *s, const void *buf, size_t sz, nni_type t)
{
    int len;
    int rv;
 
    if ((rv = nni_copyin_int(&len, buf, sz, 0, 8192, t)) != 0) {
        return (rv);
    }
    return (nni_msgq_resize(SOCK(s)->s_uwq, len));
}
 
static int
sock_get_sendbuf(void *s, void *buf, size_t *szp, nni_type t)
{
    int len = nni_msgq_cap(SOCK(s)->s_uwq);
 
    return (nni_copyout_int(len, buf, szp, t));
}
 
static int
sock_get_sockname(void *s, void *buf, size_t *szp, nni_type t)
{
    return (nni_copyout_str(SOCK(s)->s_name, buf, szp, t));
}
 
static int
sock_set_sockname(void *s, const void *buf, size_t sz, nni_type t)
{
    return (nni_copyin_str(
        SOCK(s)->s_name, buf, sizeof(SOCK(s)->s_name), sz, t));
}
 
static int
sock_get_proto(void *s, void *buf, size_t *szp, nni_type t)
{
    return (nni_copyout_int(nni_sock_proto_id(SOCK(s)), buf, szp, t));
}
 
static int
sock_get_peer(void *s, void *buf, size_t *szp, nni_type t)
{
    return (nni_copyout_int(nni_sock_peer_id(SOCK(s)), buf, szp, t));
}
 
static int
sock_get_protoname(void *s, void *buf, size_t *szp, nni_type t)
{
    return (nni_copyout_str(nni_sock_proto_name(SOCK(s)), buf, szp, t));
}
 
static int
sock_get_peername(void *s, void *buf, size_t *szp, nni_type t)
{
    return (nni_copyout_str(nni_sock_peer_name(SOCK(s)), buf, szp, t));
}
 
static const nni_option sock_options[] = {
    {
        .o_name = NNG_OPT_RECVTIMEO,
        .o_get  = sock_get_recvtimeo,
        .o_set  = sock_set_recvtimeo,
    },
    {
        .o_name = NNG_OPT_SENDTIMEO,
        .o_get  = sock_get_sendtimeo,
        .o_set  = sock_set_sendtimeo,
    },
    {
        .o_name = NNG_OPT_RECVFD,
        .o_get  = sock_get_recvfd,
    },
    {
        .o_name = NNG_OPT_SENDFD,
        .o_get  = sock_get_sendfd,
    },
    {
        .o_name = NNG_OPT_RECVBUF,
        .o_get  = sock_get_recvbuf,
        .o_set  = sock_set_recvbuf,
    },
    {
        .o_name = NNG_OPT_SENDBUF,
        .o_get  = sock_get_sendbuf,
        .o_set  = sock_set_sendbuf,
    },
    {
        .o_name = NNG_OPT_SOCKNAME,
        .o_get  = sock_get_sockname,
        .o_set  = sock_set_sockname,
    },
    {
        .o_name = NNG_OPT_RAW,
        .o_get  = sock_get_raw,
    },
    {
        .o_name = NNG_OPT_PROTO,
        .o_get  = sock_get_proto,
    },
    {
        .o_name = NNG_OPT_PEER,
        .o_get  = sock_get_peer,
    },
    {
        .o_name = NNG_OPT_PROTONAME,
        .o_get  = sock_get_protoname,
    },
    {
        .o_name = NNG_OPT_PEERNAME,
        .o_get  = sock_get_peername,
    },
    // terminate list
    {
        .o_name = NULL,
    },
};
 
static void
nni_free_opt(nni_sockopt *opt)
{
    nni_strfree(opt->name);
    nni_free(opt->data, opt->sz);
    NNI_FREE_STRUCT(opt);
}
 
uint32_t
nni_sock_id(nni_sock *s)
{
    return (s->s_id);
}
 
// nni_sock_sendq and nni_sock_recvq are called by the protocol to obtain
// the upper read and write queues.
nni_msgq *
nni_sock_sendq(nni_sock *s)
{
    return (s->s_uwq);
}
 
nni_msgq *
nni_sock_recvq(nni_sock *s)
{
    return (s->s_urq);
}
 
int
nni_sock_find(nni_sock **sockp, uint32_t id)
{
    int       rv;
    nni_sock *s;
 
    if ((rv = nni_init()) != 0) {
        return (rv);
    }
    nni_mtx_lock(&sock_lk);
    if ((s = nni_id_get(&sock_ids, id)) != NULL) {
        if (s->s_closed) {
            rv = NNG_ECLOSED;
        } else {
            s->s_ref++;
            *sockp = s;
        }
    } else {
        rv = NNG_ECLOSED;
    }
    nni_mtx_unlock(&sock_lk);
 
    return (rv);
}
 
void
nni_sock_rele(nni_sock *s)
{
    nni_mtx_lock(&sock_lk);
    s->s_ref--;
    if (s->s_closed && (s->s_ref < 2)) {
        nni_cv_wake(&s->s_close_cv);
    }
    nni_mtx_unlock(&sock_lk);
}
 
#ifdef NNG_ENABLE_STATS
static void
sock_stat_init(nni_sock *s, nni_stat_item *item, const nni_stat_info *info)
{
    nni_stat_init(item, info);
    nni_stat_add(&s->st_root, item);
}
 
static void
sock_stats_init(nni_sock *s)
{
    static const nni_stat_info root_info = {
        .si_name = "socket",
        .si_desc = "socket statistics",
        .si_type = NNG_STAT_SCOPE,
    };
    static const nni_stat_info id_info = {
        .si_name = "id",
        .si_desc = "socket identifier",
        .si_type = NNG_STAT_ID,
    };
    static const nni_stat_info name_info = {
        .si_name  = "name",
        .si_desc  = "socket name",
        .si_type  = NNG_STAT_STRING,
        .si_alloc = true,
    };
    static const nni_stat_info protocol_info = {
        .si_name = "protocol",
        .si_desc = "socket protocol",
        .si_type = NNG_STAT_STRING,
    };
    static const nni_stat_info dialers_info = {
        .si_name   = "dialers",
        .si_desc   = "open dialers",
        .si_type   = NNG_STAT_LEVEL,
        .si_atomic = true,
    };
    static const nni_stat_info listeners_info = {
        .si_name   = "listeners",
        .si_desc   = "open listeners",
        .si_type   = NNG_STAT_LEVEL,
        .si_atomic = true,
    };
    static const nni_stat_info pipes_info = {
        .si_name   = "pipes",
        .si_desc   = "open pipes",
        .si_type   = NNG_STAT_LEVEL,
        .si_atomic = true,
    };
    static const nni_stat_info reject_info = {
        .si_name   = "reject",
        .si_desc   = "rejected pipes",
        .si_type   = NNG_STAT_COUNTER,
        .si_atomic = true,
    };
    static const nni_stat_info tx_msgs_info = {
        .si_name   = "tx_msgs",
        .si_desc   = "sent messages",
        .si_type   = NNG_STAT_COUNTER,
        .si_unit   = NNG_UNIT_MESSAGES,
        .si_atomic = true,
    };
    static const nni_stat_info rx_msgs_info = {
        .si_name   = "rx_msgs",
        .si_desc   = "received messages",
        .si_type   = NNG_STAT_COUNTER,
        .si_unit   = NNG_UNIT_MESSAGES,
        .si_atomic = true,
    };
    static const nni_stat_info tx_bytes_info = {
        .si_name   = "tx_bytes",
        .si_desc   = "sent bytes",
        .si_type   = NNG_STAT_COUNTER,
        .si_unit   = NNG_UNIT_BYTES,
        .si_atomic = true,
    };
    static const nni_stat_info rx_bytes_info = {
        .si_name   = "rx_bytes",
        .si_desc   = "received messages",
        .si_type   = NNG_STAT_COUNTER,
        .si_unit   = NNG_UNIT_BYTES,
        .si_atomic = true,
    };
 
    // To make collection cheap and atomic for the socket,
    // we just use a single lock for the entire chain.
 
    nni_stat_init(&s->st_root, &root_info);
    sock_stat_init(s, &s->st_id, &id_info);
    sock_stat_init(s, &s->st_name, &name_info);
    sock_stat_init(s, &s->st_protocol, &protocol_info);
    sock_stat_init(s, &s->st_dialers, &dialers_info);
    sock_stat_init(s, &s->st_listeners, &listeners_info);
    sock_stat_init(s, &s->st_pipes, &pipes_info);
    sock_stat_init(s, &s->st_rejects, &reject_info);
    sock_stat_init(s, &s->st_tx_msgs, &tx_msgs_info);
    sock_stat_init(s, &s->st_rx_msgs, &rx_msgs_info);
    sock_stat_init(s, &s->st_tx_bytes, &tx_bytes_info);
    sock_stat_init(s, &s->st_rx_bytes, &rx_bytes_info);
 
    nni_stat_set_id(&s->st_id, (int) s->s_id);
    nni_stat_set_string(&s->st_name, s->s_name);
    nni_stat_set_string(&s->st_protocol, nni_sock_proto_name(s));
}
#endif
 
static void
sock_destroy(nni_sock *s)
{
    nni_sockopt *sopt;
 
#ifdef NNG_ENABLE_STATS
    nni_stat_unregister(&s->st_root);
#endif
 
    // The protocol needs to clean up its state.
    if (s->s_data != NULL) {
        s->s_sock_ops.sock_fini(s->s_data);
    }
 
    nni_mtx_lock(&s->s_mx);
    while ((sopt = nni_list_first(&s->s_options)) != NULL) {
        nni_list_remove(&s->s_options, sopt);
        nni_free_opt(sopt);
    }
    nni_mtx_unlock(&s->s_mx);
 
    nni_msgq_fini(s->s_urq);
    nni_msgq_fini(s->s_uwq);
    nni_cv_fini(&s->s_close_cv);
    nni_cv_fini(&s->s_cv);
    nni_mtx_fini(&s->s_mx);
    nni_mtx_fini(&s->s_pipe_cbs_mtx);
    nni_free(s, s->s_size);
}
 
static int
nni_sock_create(nni_sock **sp, const nni_proto *proto)
{
    int       rv;
    nni_sock *s;
    bool      on;
    size_t    sz;
 
    sz = NNI_ALIGN_UP(sizeof(*s)) + proto->proto_sock_ops->sock_size;
    if ((s = nni_zalloc(sz)) == NULL) {
        return (NNG_ENOMEM);
    }
    s->s_data      = s + 1;
    s->s_sndtimeo  = -1;
    s->s_rcvtimeo  = -1;
    s->s_reconn    = NNI_SECOND;
    s->s_reconnmax = 0;
    s->s_rcvmaxsz  = 0; // unlimited by default
    s->s_id        = 0;
    s->s_ref       = 0;
    s->s_self_id   = proto->proto_self;
    s->s_peer_id   = proto->proto_peer;
    s->s_flags     = proto->proto_flags;
    s->s_sock_ops  = *proto->proto_sock_ops;
    s->s_pipe_ops  = *proto->proto_pipe_ops;
    s->s_closed    = false;
    s->s_closing   = false;
 
    if (proto->proto_ctx_ops != NULL) {
        s->s_ctx_ops = *proto->proto_ctx_ops;
    }
 
    NNI_ASSERT(s->s_sock_ops.sock_open != NULL);
    NNI_ASSERT(s->s_sock_ops.sock_close != NULL);
 
    NNI_LIST_NODE_INIT(&s->s_node);
    NNI_LIST_INIT(&s->s_options, nni_sockopt, node);
    NNI_LIST_INIT(&s->s_ctxs, nni_ctx, c_node);
    NNI_LIST_INIT(&s->s_pipes, nni_pipe, p_sock_node);
    NNI_LIST_INIT(&s->s_listeners, nni_listener, l_node);
    NNI_LIST_INIT(&s->s_dialers, nni_dialer, d_node);
    nni_mtx_init(&s->s_mx);
    nni_mtx_init(&s->s_pipe_cbs_mtx);
    nni_cv_init(&s->s_cv, &s->s_mx);
    nni_cv_init(&s->s_close_cv, &sock_lk);
 
#ifdef NNG_ENABLE_STATS
    sock_stats_init(s);
#endif
 
    if (((rv = nni_msgq_init(&s->s_uwq, 0)) != 0) ||
        ((rv = nni_msgq_init(&s->s_urq, 1)) != 0) ||
        ((rv = s->s_sock_ops.sock_init(s->s_data, s)) != 0) ||
        ((rv = nni_sock_setopt(s, NNG_OPT_SENDTIMEO, &s->s_sndtimeo,
              sizeof(nni_duration), NNI_TYPE_DURATION)) != 0) ||
        ((rv = nni_sock_setopt(s, NNG_OPT_RECVTIMEO, &s->s_rcvtimeo,
              sizeof(nni_duration), NNI_TYPE_DURATION)) != 0) ||
        ((rv = nni_sock_setopt(s, NNG_OPT_RECONNMINT, &s->s_reconn,
              sizeof(nni_duration), NNI_TYPE_DURATION)) != 0) ||
        ((rv = nni_sock_setopt(s, NNG_OPT_RECONNMAXT, &s->s_reconnmax,
              sizeof(nni_duration), NNI_TYPE_DURATION)) != 0) ||
        ((rv = nni_sock_setopt(s, NNG_OPT_RECVMAXSZ, &s->s_rcvmaxsz,
              sizeof(size_t), NNI_TYPE_SIZE)) != 0)) {
        sock_destroy(s);
        return (rv);
    }
 
    // These we *attempt* to call so that we are likely to have initial
    // values loaded.  They should not fail, but if they do we don't
    // worry about it.
    on = true;
    (void) nni_sock_setopt(
        s, NNG_OPT_TCP_NODELAY, &on, sizeof(on), NNI_TYPE_BOOL);
    on = false;
    (void) nni_sock_setopt(
        s, NNG_OPT_TCP_KEEPALIVE, &on, sizeof(on), NNI_TYPE_BOOL);
 
    *sp = s;
    return (rv);
}
 
int
nni_sock_sys_init(void)
{
    NNI_LIST_INIT(&sock_list, nni_sock, s_node);
    nni_mtx_init(&sock_lk);
 
    nni_id_map_init(&sock_ids, 1, 0x7fffffff, false);
    nni_id_map_init(&ctx_ids, 1, 0x7fffffff, false);
    inited = true;
    return (0);
}
 
void
nni_sock_sys_fini(void)
{
    nni_id_map_fini(&sock_ids);
    nni_id_map_fini(&ctx_ids);
    nni_mtx_fini(&sock_lk);
    inited = false;
}
 
int
nni_sock_open(nni_sock **sockp, const nni_proto *proto)
{
    nni_sock *s = NULL;
    int       rv;
 
    if (proto->proto_version != NNI_PROTOCOL_VERSION) {
        // unsupported protocol version
        return (NNG_ENOTSUP);
    }
 
    if (((rv = nni_init()) != 0) ||
        ((rv = nni_sock_create(&s, proto)) != 0)) {
        return (rv);
    }
 
    nni_mtx_lock(&sock_lk);
    if (nni_id_alloc(&sock_ids, &s->s_id, s) != 0) {
        sock_destroy(s);
    } else {
        nni_list_append(&sock_list, s);
        s->s_sock_ops.sock_open(s->s_data);
        *sockp = s;
    }
    nni_mtx_unlock(&sock_lk);
 
    // Set the socket name.
    (void) snprintf(s->s_name, sizeof(s->s_name), "%u", s->s_id);
 
#ifdef NNG_ENABLE_STATS
    // Set up basic stat values.
    nni_stat_set_id(&s->st_id, (int) s->s_id);
 
    // Add our stats chain.
    nni_stat_register(&s->st_root);
#endif
 
    return (0);
}
 
// nni_sock_shutdown shuts down the socket; after this point no
// further access to the socket will function, and any threads blocked
// in entry points will be woken (and the functions they are blocked
// in will return NNG_ECLOSED.)
int
nni_sock_shutdown(nni_sock *sock)
{
    nni_pipe *    pipe;
    nni_dialer *  d;
    nni_listener *l;
    nni_ctx *     ctx;
    nni_ctx *     nctx;
 
    nni_mtx_lock(&sock->s_mx);
    if (sock->s_closing) {
        nni_mtx_unlock(&sock->s_mx);
        return (NNG_ECLOSED);
    }
    // Mark us closing, so no more EPs or changes can occur.
    sock->s_closing = true;
 
    // Close the EPs. This prevents new connections from forming
    // but but allows existing ones to drain.
    NNI_LIST_FOREACH (&sock->s_listeners, l) {
        listener_shutdown_locked(l);
    }
    NNI_LIST_FOREACH (&sock->s_dialers, d) {
        dialer_shutdown_locked(d);
    }
 
    nni_mtx_unlock(&sock->s_mx);
 
    // We now mark any owned contexts as closing.
    // XXX: Add context draining support here!
    nni_mtx_lock(&sock_lk);
    nctx = nni_list_first(&sock->s_ctxs);
    while ((ctx = nctx) != NULL) {
        nctx          = nni_list_next(&sock->s_ctxs, ctx);
        ctx->c_closed = true;
        if (ctx->c_ref == 0) {
            // No open operations.  So close it.
            nni_id_remove(&ctx_ids, ctx->c_id);
            nni_list_remove(&sock->s_ctxs, ctx);
            nni_ctx_destroy(ctx);
        }
        // If still has a reference count, then wait for last
        // reference to close before nuking it.
    }
    nni_mtx_unlock(&sock_lk);
 
    // Generally, unless the protocol is blocked trying to perform
    // writes (e.g. a slow reader on the other side), it should be
    // trying to shut things down.  We wait to give it
    // a chance to do so gracefully.
 
    nni_mtx_lock(&sock_lk);
    while (!nni_list_empty(&sock->s_ctxs)) {
        sock->s_ctxwait = true;
        nni_cv_wait(&sock->s_close_cv);
    }
    nni_mtx_unlock(&sock_lk);
 
    nni_mtx_lock(&sock->s_mx);
 
    // At this point, we've done everything we politely can to
    // give the protocol a chance to flush its write side.  Now
    // its time to be a little more insistent.
 
    // Close the upper queues immediately.  This can happen
    // safely while we hold the lock.
    nni_msgq_close(sock->s_urq);
    nni_msgq_close(sock->s_uwq);
 
    // Go through the dialers and listeners, attempting to close them.
    // We might already have a close in progress, in which case
    // we skip past it; it will be removed from another thread.
    NNI_LIST_FOREACH (&sock->s_listeners, l) {
        if (nni_listener_hold(l) == 0) {
            nni_listener_close_rele(l);
        }
    }
    NNI_LIST_FOREACH (&sock->s_dialers, d) {
        if (nni_dialer_hold(d) == 0) {
            nni_dialer_close_rele(d);
        }
    }
 
    // For each pipe, arrange for it to teardown hard.  We would
    // expect there not to be any here.  However, it is possible for
    // a pipe to have been added by an endpoint due to racing conditions
    // in the shutdown.  Therefore it is important that we shutdown pipes
    // *last*.
    NNI_LIST_FOREACH (&sock->s_pipes, pipe) {
        nni_pipe_close(pipe);
    }
 
    // We have to wait for *both* endpoints and pipes to be
    // removed.
    while ((!nni_list_empty(&sock->s_pipes)) ||
        (!nni_list_empty(&sock->s_listeners)) ||
        (!nni_list_empty(&sock->s_dialers))) {
        nni_cv_wait(&sock->s_cv);
    }
 
    sock->s_sock_ops.sock_close(sock->s_data);
 
    nni_cv_wake(&sock->s_cv);
 
    NNI_ASSERT(nni_list_first(&sock->s_pipes) == NULL);
 
    nni_mtx_unlock(&sock->s_mx);
 
    // At this point, there are no threads blocked inside of us
    // that are referencing socket state.  User code should call
    // nng_close to release the last resources.
    return (0);
}
 
// nni_sock_close shuts down the socket, then releases any resources
// associated with it.  It is a programmer error to reference the
// socket after this function is called, as the pointer may reference
// invalid memory or other objects.
void
nni_sock_close(nni_sock *s)
{
    // Shutdown everything if not already done.  This operation
    // is idempotent.
    nni_sock_shutdown(s);
 
    nni_mtx_lock(&sock_lk);
    if (s->s_closed) {
        // Some other thread called close.  All we need to do
        // is drop our reference count.
        nni_mtx_unlock(&sock_lk);
        nni_sock_rele(s);
        return;
    }
    s->s_closed = true;
    nni_id_remove(&sock_ids, s->s_id);
 
    // We might have been removed from the list already, e.g. by
    // nni_sock_closeall.  This is idempotent.
    nni_list_node_remove(&s->s_node);
 
    // Wait for all other references to drop.  Note that we
    // have a reference already (from our caller).
    s->s_ctxwait = true;
    while ((s->s_ref > 1) || (!nni_list_empty(&s->s_ctxs))) {
        nni_cv_wait(&s->s_close_cv);
    }
    nni_mtx_unlock(&sock_lk);
 
    // Because we already shut everything down before, we should not
    // have any child objects.
    nni_mtx_lock(&s->s_mx);
    NNI_ASSERT(nni_list_empty(&s->s_dialers));
    NNI_ASSERT(nni_list_empty(&s->s_listeners));
    NNI_ASSERT(nni_list_empty(&s->s_pipes));
    nni_mtx_unlock(&s->s_mx);
 
    sock_destroy(s);
}
 
void
nni_sock_closeall(void)
{
    nni_sock *s;
 
    if (!inited) {
        return;
    }
    for (;;) {
        nni_mtx_lock(&sock_lk);
        if ((s = nni_list_first(&sock_list)) == NULL) {
            nni_mtx_unlock(&sock_lk);
            return;
        }
        // Bump the reference count.  The close call below
        // will drop it.
        s->s_ref++;
        nni_list_node_remove(&s->s_node);
        nni_mtx_unlock(&sock_lk);
        nni_sock_close(s);
    }
}
 
void
nni_sock_send(nni_sock *sock, nni_aio *aio)
{
    nni_aio_normalize_timeout(aio, sock->s_sndtimeo);
    sock->s_sock_ops.sock_send(sock->s_data, aio);
}
 
void
nni_sock_recv(nni_sock *sock, nni_aio *aio)
{
    nni_aio_normalize_timeout(aio, sock->s_rcvtimeo);
    sock->s_sock_ops.sock_recv(sock->s_data, aio);
}
 
// nni_sock_proto_id returns the socket's 16-bit protocol number.
uint16_t
nni_sock_proto_id(nni_sock *sock)
{
    return (sock->s_self_id.p_id);
}
 
// nni_sock_peer_id returns the socket peer's 16-bit protocol number.
uint16_t
nni_sock_peer_id(nni_sock *sock)
{
    return (sock->s_peer_id.p_id);
}
 
const char *
nni_sock_proto_name(nni_sock *sock)
{
    return (sock->s_self_id.p_name);
}
 
const char *
nni_sock_peer_name(nni_sock *sock)
{
    return (sock->s_peer_id.p_name);
}
 
struct nni_proto_pipe_ops *
nni_sock_proto_pipe_ops(nni_sock *sock)
{
    return (&sock->s_pipe_ops);
}
 
void *
nni_sock_proto_data(nni_sock *sock)
{
    return (sock->s_data);
}
 
int
nni_sock_add_listener(nni_sock *s, nni_listener *l)
{
    nni_sockopt *sopt;
 
    nni_mtx_lock(&s->s_mx);
    if (s->s_closing) {
        nni_mtx_unlock(&s->s_mx);
        return (NNG_ECLOSED);
    }
 
    NNI_LIST_FOREACH (&s->s_options, sopt) {
        int rv;
        rv = nni_listener_setopt(
            l, sopt->name, sopt->data, sopt->sz, sopt->typ);
        if ((rv != 0) && (rv != NNG_ENOTSUP)) {
            nni_mtx_unlock(&s->s_mx);
            return (rv);
        }
    }
 
    nni_list_append(&s->s_listeners, l);
 
#ifdef NNG_ENABLE_STATS
    nni_stat_inc(&s->st_listeners, 1);
#endif
 
    nni_mtx_unlock(&s->s_mx);
    return (0);
}
 
int
nni_sock_add_dialer(nni_sock *s, nni_dialer *d)
{
    nni_sockopt *sopt;
 
    nni_mtx_lock(&s->s_mx);
    if (s->s_closing) {
        nni_mtx_unlock(&s->s_mx);
        return (NNG_ECLOSED);
    }
 
    NNI_LIST_FOREACH (&s->s_options, sopt) {
        int rv;
        rv = nni_dialer_setopt(
            d, sopt->name, sopt->data, sopt->sz, sopt->typ);
        if ((rv != 0) && (rv != NNG_ENOTSUP)) {
            nni_mtx_unlock(&s->s_mx);
            return (rv);
        }
    }
 
    nni_list_append(&s->s_dialers, d);
 
#ifdef NNG_ENABLE_STATS
    nni_stat_inc(&s->st_dialers, 1);
#endif
 
    nni_mtx_unlock(&s->s_mx);
    return (0);
}
 
int
nni_sock_setopt(
    nni_sock *s, const char *name, const void *v, size_t sz, nni_type t)
{
    int          rv;
    nni_sockopt *optv;
    nni_sockopt *oldv = NULL;
 
    nni_mtx_lock(&s->s_mx);
    if (s->s_closing) {
        nni_mtx_unlock(&s->s_mx);
        return (NNG_ECLOSED);
    }
 
    // Protocol options.  The protocol can override options that
    // the socket framework would otherwise supply, like buffer
    // sizes.
    rv = nni_setopt(s->s_sock_ops.sock_options, name, s->s_data, v, sz, t);
    if (rv != NNG_ENOTSUP) {
        nni_mtx_unlock(&s->s_mx);
        return (rv);
    }
 
    // Some options do not go down to transports.  Handle them directly.
    rv = nni_setopt(sock_options, name, s, v, sz, t);
    if (rv != NNG_ENOTSUP) {
        nni_mtx_unlock(&s->s_mx);
        return (rv);
    }
    nni_mtx_unlock(&s->s_mx);
 
    // Validation of generic and transport options.
    // NOTE: Setting transport options via socket is deprecated.
    // These options should be set on the endpoint to which they apply.
    if ((strcmp(name, NNG_OPT_RECONNMINT) == 0) ||
        (strcmp(name, NNG_OPT_RECONNMAXT) == 0)) {
        if ((rv = nni_copyin_ms(NULL, v, sz, t)) != 0) {
            return (rv);
        }
 
    } else if (strcmp(name, NNG_OPT_RECVMAXSZ) == 0) {
        if ((rv = nni_copyin_size(NULL, v, sz, 0, NNI_MAXSZ, t)) !=
            0) {
            return (rv);
        }
 
#if !defined(NNG_ELIDE_DEPRECATED)
        // TCP options, set via socket is deprecated.
    } else if ((strcmp(name, NNG_OPT_TCP_KEEPALIVE) == 0) ||
        (strcmp(name, NNG_OPT_TCP_NODELAY)) == 0) {
        if ((rv = nni_copyin_bool(NULL, v, sz, t)) != 0) {
            return (rv);
        }
#endif
 
#if defined(NNG_SUPP_TLS) && !defined(NNG_ELIDE_DEPRECATED)
        // TLS options may not be supported if TLS is not
        // compiled in.  Supporting all these is deprecated.
    } else if (strcmp(name, NNG_OPT_TLS_CONFIG) == 0) {
        if ((rv = nni_copyin_ptr(NULL, v, sz, t)) != 0) {
            return (rv);
        }
    } else if ((strcmp(name, NNG_OPT_TLS_SERVER_NAME) == 0) ||
        (strcmp(name, NNG_OPT_TLS_CA_FILE) == 0) ||
        (strcmp(name, NNG_OPT_TLS_CERT_KEY_FILE) == 0)) {
        if ((t != NNI_TYPE_OPAQUE) && (t != NNI_TYPE_STRING)) {
            return (NNG_EBADTYPE);
        }
        if (nni_strnlen(v, sz) >= sz) {
            return (NNG_EINVAL);
        }
    } else if ((strcmp(name, NNG_OPT_TLS_AUTH_MODE) == 0)) {
        // 0, 1, or 2 (none, optional, required)
        if ((rv = nni_copyin_int(NULL, v, sz, 0, 2, t)) != 0) {
            return (rv);
        }
#endif
 
#if defined(NNG_PLATFORM_POSIX) && !defined(NNG_SUPPRESS_DEPRECATED)
    } else if (strcmp(name, NNG_OPT_IPC_PERMISSIONS) == 0) {
        // UNIX mode bits are 0777, but allow set id and sticky bits
        if ((rv = nni_copyin_int(NULL, v, sz, 0, 07777, t)) != 0) {
            return (rv);
        }
#endif
 
#if defined(NNG_PLATFORM_WINDOWS) && !defined(NNG_SUPPRESS_DEPRECATED)
    } else if (strcmp(name, NNG_OPT_IPC_SECURITY_DESCRIPTOR) == 0) {
        if ((rv = nni_copyin_ptr(NULL, v, sz, t)) == 0) {
            return (rv);
        }
#endif
    }
 
    // Prepare a copy of the socket option.
    if ((optv = NNI_ALLOC_STRUCT(optv)) == NULL) {
        return (NNG_ENOMEM);
    }
    if ((optv->data = nni_alloc(sz)) == NULL) {
        NNI_FREE_STRUCT(optv);
        return (NNG_ENOMEM);
    }
    if ((optv->name = nni_strdup(name)) == NULL) {
        nni_free(optv->data, sz);
        NNI_FREE_STRUCT(optv);
        return (NNG_ENOMEM);
    }
    memcpy(optv->data, v, sz);
    optv->sz  = sz;
    optv->typ = t;
    NNI_LIST_NODE_INIT(&optv->node);
 
    nni_mtx_lock(&s->s_mx);
    NNI_LIST_FOREACH (&s->s_options, oldv) {
        if (strcmp(oldv->name, name) == 0) {
            if ((oldv->sz != sz) ||
                (memcmp(oldv->data, v, sz) != 0)) {
                break;
            }
 
            // The values are the same.  This is a no-op.
            nni_mtx_unlock(&s->s_mx);
            nni_free_opt(optv);
            return (0);
        }
    }
 
#ifndef NNG_ELIDE_DEPRCATED
    nni_dialer *  d;
    nni_listener *l;
 
    // Apply the options.  Failure to set any option on any
    // transport (other than ENOTSUP) stops the operation
    // altogether.  Its important that transport wide checks
    // properly pre-validate.
    NNI_LIST_FOREACH (&s->s_listeners, l) {
        int x;
        x = nni_listener_setopt(l, optv->name, optv->data, sz, t);
        if (x != NNG_ENOTSUP) {
            if ((rv = x) != 0) {
                nni_mtx_unlock(&s->s_mx);
                nni_free_opt(optv);
                return (rv);
            }
        }
    }
    NNI_LIST_FOREACH (&s->s_dialers, d) {
        int x;
        x = nni_dialer_setopt(d, optv->name, optv->data, sz, t);
        if (x != NNG_ENOTSUP) {
            if ((rv = x) != 0) {
                nni_mtx_unlock(&s->s_mx);
                nni_free_opt(optv);
                return (rv);
            }
        }
    }
#endif
 
    if (rv == 0) {
        // Remove and toss the old value; we are using a new one.
        if (oldv != NULL) {
            nni_list_remove(&s->s_options, oldv);
            nni_free_opt(oldv);
        }
 
        // Insert our new value.  This permits it to be
        // compared against later, and for new endpoints to
        // automatically receive these values,
        nni_list_append(&s->s_options, optv);
    } else {
        nni_free_opt(optv);
    }
 
    nni_mtx_unlock(&s->s_mx);
    return (rv);
}
 
int
nni_sock_getopt(
    nni_sock *s, const char *name, void *val, size_t *szp, nni_type t)
{
    int          rv;
    nni_sockopt *sopt;
 
    nni_mtx_lock(&s->s_mx);
    if (s->s_closing) {
        nni_mtx_unlock(&s->s_mx);
        return (NNG_ECLOSED);
    }
 
    // Protocol specific options.  The protocol can override
    // options like the send buffer or notification descriptors
    // this way.
    rv = nni_getopt(
        s->s_sock_ops.sock_options, name, s->s_data, val, szp, t);
    if (rv != NNG_ENOTSUP) {
        nni_mtx_unlock(&s->s_mx);
        return (rv);
    }
 
    // Socket generic options.
    rv = nni_getopt(sock_options, name, s, val, szp, t);
    if (rv != NNG_ENOTSUP) {
        nni_mtx_unlock(&s->s_mx);
        return (rv);
    }
 
    NNI_LIST_FOREACH (&s->s_options, sopt) {
        if (strcmp(sopt->name, name) == 0) {
            size_t sz = sopt->sz;
 
            if ((sopt->typ != NNI_TYPE_OPAQUE) &&
                (t != sopt->typ)) {
 
                if (t != NNI_TYPE_OPAQUE) {
                    nni_mtx_unlock(&s->s_mx);
                    return (NNG_EBADTYPE);
                }
                if (*szp != sopt->sz) {
                    nni_mtx_unlock(&s->s_mx);
                    return (NNG_EINVAL);
                }
            }
 
            if (szp != NULL) {
                if (sopt->sz > *szp) {
                    sz = *szp;
                }
                *szp = sopt->sz;
            }
            memcpy(val, sopt->data, sz);
            rv = 0;
            break;
        }
    }
 
    nni_mtx_unlock(&s->s_mx);
    return (rv);
}
 
uint32_t
nni_sock_flags(nni_sock *sock)
{
    return (sock->s_flags);
}
 
void
nni_sock_set_pipe_cb(nni_sock *s, int ev, nng_pipe_cb cb, void *arg)
{
    if ((ev >= 0) && (ev < NNG_PIPE_EV_NUM)) {
        nni_mtx_lock(&s->s_pipe_cbs_mtx);
        s->s_pipe_cbs[ev].cb_fn  = cb;
        s->s_pipe_cbs[ev].cb_arg = arg;
        nni_mtx_unlock(&s->s_pipe_cbs_mtx);
    }
}
 
int
nni_ctx_find(nni_ctx **cp, uint32_t id, bool closing)
{
    int      rv;
    nni_ctx *ctx;
 
    if ((rv = nni_init()) != 0) {
        return (rv);
    }
    nni_mtx_lock(&sock_lk);
    if ((ctx = nni_id_get(&ctx_ids, id)) != NULL) {
        // We refuse a reference if either the socket is
        // closed, or the context is closed.  (If the socket
        // is closed, and we are only getting the reference so
        // we can close it, then we still allow.  In the case
        // the only valid operation will be to close the
        // socket.)
        if (ctx->c_closed || ((!closing) && ctx->c_sock->s_closed)) {
            rv = NNG_ECLOSED;
        } else {
            ctx->c_ref++;
            *cp = ctx;
        }
    } else {
        rv = NNG_ECLOSED;
    }
    nni_mtx_unlock(&sock_lk);
 
    return (rv);
}
 
static void
nni_ctx_destroy(nni_ctx *ctx)
{
    if (ctx->c_data != NULL) {
        ctx->c_ops.ctx_fini(ctx->c_data);
    }
 
    // Let the socket go, our hold on it is done.
    nni_free(ctx, ctx->c_size);
}
 
void
nni_ctx_rele(nni_ctx *ctx)
{
    nni_sock *sock = ctx->c_sock;
    nni_mtx_lock(&sock_lk);
    ctx->c_ref--;
    if ((ctx->c_ref > 0) || (!ctx->c_closed)) {
        // Either still have an active reference, or not
        // actually closing yet.
        nni_mtx_unlock(&sock_lk);
        return;
    }
 
    // Remove us from the hash, so we can't be found any more.
    // This allows our ID to be reused later, although the system
    // tries to avoid ID reuse.
    nni_id_remove(&ctx_ids, ctx->c_id);
    nni_list_remove(&sock->s_ctxs, ctx);
    if (sock->s_closed || sock->s_ctxwait) {
        nni_cv_wake(&sock->s_close_cv);
    }
    nni_mtx_unlock(&sock_lk);
 
    nni_ctx_destroy(ctx);
}
 
int
nni_ctx_open(nni_ctx **ctxp, nni_sock *sock)
{
    nni_ctx *ctx;
    int      rv;
    size_t   sz;
 
    if (sock->s_ctx_ops.ctx_init == NULL) {
        return (NNG_ENOTSUP);
    }
 
    sz = NNI_ALIGN_UP(sizeof(*ctx)) + sock->s_ctx_ops.ctx_size;
    if ((ctx = nni_zalloc(sz)) == NULL) {
        return (NNG_ENOMEM);
    }
    ctx->c_size     = sz;
    ctx->c_data     = ctx + 1;
    ctx->c_closed   = false;
    ctx->c_ref      = 1; // Caller implicitly gets a reference.
    ctx->c_sock     = sock;
    ctx->c_ops      = sock->s_ctx_ops;
    ctx->c_rcvtimeo = sock->s_rcvtimeo;
    ctx->c_sndtimeo = sock->s_sndtimeo;
 
    nni_mtx_lock(&sock_lk);
    if (sock->s_closed) {
        nni_mtx_unlock(&sock_lk);
        nni_free(ctx, ctx->c_size);
        return (NNG_ECLOSED);
    }
    if ((rv = nni_id_alloc(&ctx_ids, &ctx->c_id, ctx)) != 0) {
        nni_mtx_unlock(&sock_lk);
        nni_free(ctx, ctx->c_size);
        return (rv);
    }
 
    if ((rv = sock->s_ctx_ops.ctx_init(ctx->c_data, sock->s_data)) != 0) {
        nni_id_remove(&ctx_ids, ctx->c_id);
        nni_mtx_unlock(&sock_lk);
        nni_free(ctx, ctx->c_size);
        return (rv);
    }
 
    nni_list_append(&sock->s_ctxs, ctx);
    nni_mtx_unlock(&sock_lk);
 
    // Paranoia, fixing a possible race in close.  Don't let us
    // give back a context if the socket is being shutdown (it
    // might not have reached the "closed" state yet.)
    nni_mtx_lock(&sock->s_mx);
    if (sock->s_closing) {
        nni_mtx_unlock(&sock->s_mx);
        nni_ctx_rele(ctx);
        return (NNG_ECLOSED);
    }
    nni_mtx_unlock(&sock->s_mx);
    *ctxp = ctx;
 
    return (0);
}
 
void
nni_ctx_close(nni_ctx *ctx)
{
    nni_mtx_lock(&sock_lk);
    ctx->c_closed = true;
    nni_mtx_unlock(&sock_lk);
 
    nni_ctx_rele(ctx);
}
 
uint32_t
nni_ctx_id(nni_ctx *ctx)
{
    return (ctx->c_id);
}
 
void
nni_ctx_send(nni_ctx *ctx, nni_aio *aio)
{
    nni_aio_normalize_timeout(aio, ctx->c_sndtimeo);
    ctx->c_ops.ctx_send(ctx->c_data, aio);
}
 
void
nni_ctx_recv(nni_ctx *ctx, nni_aio *aio)
{
    nni_aio_normalize_timeout(aio, ctx->c_rcvtimeo);
    ctx->c_ops.ctx_recv(ctx->c_data, aio);
}
 
int
nni_ctx_getopt(nni_ctx *ctx, const char *opt, void *v, size_t *szp, nni_type t)
{
    nni_sock *  sock = ctx->c_sock;
    nni_option *o;
    int         rv = NNG_ENOTSUP;
 
    nni_mtx_lock(&sock->s_mx);
    if (strcmp(opt, NNG_OPT_RECVTIMEO) == 0) {
        rv = nni_copyout_ms(ctx->c_rcvtimeo, v, szp, t);
    } else if (strcmp(opt, NNG_OPT_SENDTIMEO) == 0) {
        rv = nni_copyout_ms(ctx->c_sndtimeo, v, szp, t);
    } else if (ctx->c_ops.ctx_options != NULL) {
        for (o = ctx->c_ops.ctx_options; o->o_name != NULL; o++) {
            if (strcmp(opt, o->o_name) != 0) {
                continue;
            }
            if (o->o_get == NULL) {
                rv = NNG_EWRITEONLY;
                break;
            }
            rv = o->o_get(ctx->c_data, v, szp, t);
            break;
        }
    }
    nni_mtx_unlock(&sock->s_mx);
    return (rv);
}
 
int
nni_ctx_setopt(
    nni_ctx *ctx, const char *opt, const void *v, size_t sz, nni_type t)
{
    nni_sock *  sock = ctx->c_sock;
    nni_option *o;
    int         rv = NNG_ENOTSUP;
 
    nni_mtx_lock(&sock->s_mx);
    if (strcmp(opt, NNG_OPT_RECVTIMEO) == 0) {
        rv = nni_copyin_ms(&ctx->c_rcvtimeo, v, sz, t);
    } else if (strcmp(opt, NNG_OPT_SENDTIMEO) == 0) {
        rv = nni_copyin_ms(&ctx->c_sndtimeo, v, sz, t);
    } else if (ctx->c_ops.ctx_options != NULL) {
        for (o = ctx->c_ops.ctx_options; o->o_name != NULL; o++) {
            if (strcmp(opt, o->o_name) != 0) {
                continue;
            }
            if (o->o_set == NULL) {
                rv = NNG_EREADONLY;
                break;
            }
            rv = o->o_set(ctx->c_data, v, sz, t);
            break;
        }
    }
 
    nni_mtx_unlock(&sock->s_mx);
    return (rv);
}
 
static void
dialer_timer_start_locked(nni_dialer *d)
{
    nni_duration back_off;
    nni_sock *   sock = d->d_sock;
 
    if (d->d_closing || sock->s_closed) {
        return;
    }
    back_off = d->d_currtime;
    if (d->d_maxrtime > 0) {
        d->d_currtime *= 2;
        if (d->d_currtime > d->d_maxrtime) {
            d->d_currtime = d->d_maxrtime;
        }
    }
 
    // To minimize damage from storms, etc., we select a back-off
    // value randomly, in the range of [0, back_off-1]; this is
    // pretty similar to 802 style back-off, except that we have a
    // nearly uniform time period instead of discrete slot times.
    // This algorithm may lead to slight biases because we don't
    // have a statistically perfect distribution with the modulo of
    // the random number, but this really doesn't matter.
    nni_sleep_aio(
        back_off ? (int) nni_random() % back_off : 0, &d->d_tmo_aio);
}
 
void
nni_dialer_timer_start(nni_dialer *d)
{
    nni_sock *s = d->d_sock;
    nni_mtx_lock(&s->s_mx);
    dialer_timer_start_locked(d);
    nni_mtx_unlock(&s->s_mx);
}
 
void
nni_dialer_add_pipe(nni_dialer *d, void *tpipe)
{
    nni_sock *s = d->d_sock;
    nni_pipe *p;
 
    nni_mtx_lock(&s->s_mx);
 
    if (s->s_closed || d->d_closing) {
        d->d_tran->tran_pipe->p_fini(tpipe);
        nni_mtx_unlock(&s->s_mx);
        return;
    }
    if (nni_pipe_create_dialer(&p, d, tpipe) != 0) {
        nni_mtx_unlock(&s->s_mx);
        return;
    }
 
    nni_list_append(&d->d_pipes, p);
    nni_list_append(&s->s_pipes, p);
    d->d_pipe     = p;
    d->d_currtime = d->d_inirtime;
    nni_mtx_unlock(&s->s_mx);
#ifdef NNG_ENABLE_STATS
    nni_stat_inc(&s->st_pipes, 1);
    nni_stat_inc(&d->st_pipes, 1);
#endif
 
    nni_pipe_run_cb(p, NNG_PIPE_EV_ADD_PRE);
 
    nni_mtx_lock(&s->s_mx);
    if (p->p_closed) {
        nni_mtx_unlock(&s->s_mx);
#ifdef NNG_ENABLE_STATS
        nni_stat_inc(&d->st_reject, 1);
        nni_stat_inc(&s->st_rejects, 1);
#endif
        nni_pipe_rele(p);
        return;
    }
    if (p->p_proto_ops.pipe_start(p->p_proto_data) != 0) {
        nni_mtx_unlock(&s->s_mx);
#ifdef NNG_ENABLE_STATS
        nni_stat_inc(&d->st_reject, 1);
        nni_stat_inc(&s->st_rejects, 1);
#endif
        nni_pipe_close(p);
        nni_pipe_rele(p);
        return;
    }
    nni_mtx_unlock(&s->s_mx);
#ifdef NNG_ENABLE_STATS
    nni_stat_register(&p->st_root);
#endif
    nni_pipe_run_cb(p, NNG_PIPE_EV_ADD_POST);
    nni_pipe_rele(p);
}
 
static void
dialer_shutdown_impl(nni_dialer *d)
{
    nni_pipe *p;
 
    // Abort any remaining in-flight operations.
    nni_aio_close(&d->d_con_aio);
    nni_aio_close(&d->d_tmo_aio);
 
    // Stop the underlying transport.
    d->d_ops.d_close(d->d_data);
 
    NNI_LIST_FOREACH (&d->d_pipes, p) {
        nni_pipe_close(p);
    }
}
 
static void
dialer_shutdown_locked(nni_dialer *d)
{
    if (!d->d_closing) {
        d->d_closing = true;
        dialer_shutdown_impl(d);
    }
}
 
void
nni_dialer_shutdown(nni_dialer *d)
{
    nni_sock *s = d->d_sock;
    nni_mtx_lock(&s->s_mx);
    dialer_shutdown_locked(d);
    nni_mtx_unlock(&s->s_mx);
}
 
static void dialer_reap(void *);
 
static nni_reap_list dialer_reap_list = {
    .rl_offset = offsetof(nni_dialer, d_reap),
    .rl_func   = dialer_reap,
};
 
static void
dialer_reap(void *arg)
{
    nni_dialer *d = arg;
    nni_sock *  s = d->d_sock;
 
    nni_aio_stop(&d->d_tmo_aio);
    nni_aio_stop(&d->d_con_aio);
 
#ifdef NNG_ENABLE_STATS
    nni_stat_unregister(&d->st_root);
#endif
 
    nni_mtx_lock(&s->s_mx);
    if (!nni_list_empty(&d->d_pipes)) {
        nni_pipe *p;
        // This should already have been done, but be certain!
        NNI_LIST_FOREACH (&d->d_pipes, p) {
            nni_pipe_close(p);
        }
        nni_mtx_unlock(&s->s_mx);
        // Go back to the end of reap list.
        nni_dialer_reap(d);
        return;
    }
 
    nni_list_remove(&s->s_dialers, d);
    if ((s->s_closing) && (nni_list_empty(&s->s_dialers))) {
        nni_cv_wake(&s->s_cv);
    }
 
    nni_mtx_unlock(&s->s_mx);
 
    nni_dialer_destroy(d);
}
 
void
nni_dialer_reap(nni_dialer *d)
{
    nni_reap(&dialer_reap_list, d);
}
 
void
nni_listener_add_pipe(nni_listener *l, void *tpipe)
{
    nni_sock *s = l->l_sock;
    nni_pipe *p;
 
    nni_mtx_lock(&s->s_mx);
    if (s->s_closed || l->l_closing) {
        l->l_tran->tran_pipe->p_fini(tpipe);
        nni_mtx_unlock(&s->s_mx);
        return;
    }
 
    if (nni_pipe_create_listener(&p, l, tpipe) != 0) {
        nni_mtx_unlock(&s->s_mx);
        return;
    }
 
    nni_list_append(&l->l_pipes, p);
    nni_list_append(&s->s_pipes, p);
    nni_mtx_unlock(&s->s_mx);
#ifdef NNG_ENABLE_STATS
    nni_stat_inc(&l->st_pipes, 1);
    nni_stat_inc(&s->st_pipes, 1);
#endif
 
    nni_pipe_run_cb(p, NNG_PIPE_EV_ADD_PRE);
 
    nni_mtx_lock(&s->s_mx);
    if (p->p_closed) {
        nni_mtx_unlock(&s->s_mx);
#ifdef NNG_ENABLE_STATS
        nni_stat_inc(&l->st_reject, 1);
        nni_stat_inc(&s->st_rejects, 1);
#endif
        nni_pipe_rele(p);
        return;
    }
    if (p->p_proto_ops.pipe_start(p->p_proto_data) != 0) {
        nni_mtx_unlock(&s->s_mx);
#ifdef NNG_ENABLE_STATS
        nni_stat_inc(&l->st_reject, 1);
        nni_stat_inc(&s->st_rejects, 1);
#endif
        nni_pipe_close(p);
        nni_pipe_rele(p);
        return;
    }
    nni_mtx_unlock(&s->s_mx);
#ifdef NNG_ENABLE_STATS
    nni_stat_register(&p->st_root);
#endif
    nni_pipe_run_cb(p, NNG_PIPE_EV_ADD_POST);
    nni_pipe_rele(p);
}
 
static void
listener_shutdown_impl(nni_listener *l)
{
    nni_pipe *p;
 
    // Abort any remaining in-flight accepts.
    nni_aio_close(&l->l_acc_aio);
    nni_aio_close(&l->l_tmo_aio);
 
    // Stop the underlying transport.
    l->l_ops.l_close(l->l_data);
 
    NNI_LIST_FOREACH (&l->l_pipes, p) {
        nni_pipe_close(p);
    }
}
 
static void
listener_shutdown_locked(nni_listener *l)
{
    if (!l->l_closing) {
        l->l_closing = true;
        listener_shutdown_impl(l);
    }
}
 
void
nni_listener_shutdown(nni_listener *l)
{
    nni_sock *s = l->l_sock;
 
    nni_mtx_lock(&s->s_mx);
    listener_shutdown_locked(l);
    nni_mtx_unlock(&s->s_mx);
}
 
static void listener_reap(void *);
 
static nni_reap_list listener_reap_list = {
    .rl_offset = offsetof(nni_listener, l_reap),
    .rl_func   = listener_reap,
};
 
static void
listener_reap(void *arg)
{
    nni_listener *l = arg;
    nni_sock *    s = l->l_sock;
 
    nni_aio_stop(&l->l_tmo_aio);
    nni_aio_stop(&l->l_acc_aio);
 
#ifdef NNG_ENABLE_STATS
    nni_stat_unregister(&l->st_root);
#endif
 
    nni_mtx_lock(&s->s_mx);
    if (!nni_list_empty(&l->l_pipes)) {
        nni_pipe *p;
        // This should already have been done, but be certain!
        NNI_LIST_FOREACH (&l->l_pipes, p) {
            nni_pipe_close(p);
        }
        nni_mtx_unlock(&s->s_mx);
        // Go back to the end of reap list.
        nni_reap(&listener_reap_list, l);
        return;
    }
 
    nni_list_remove(&s->s_listeners, l);
    if ((s->s_closing) && (nni_list_empty(&s->s_listeners))) {
        nni_cv_wake(&s->s_cv);
    }
 
    nni_mtx_unlock(&s->s_mx);
 
    nni_listener_destroy(l);
}
 
void
nni_listener_reap(nni_listener *l)
{
    nni_reap(&listener_reap_list, l);
}
 
void
nni_pipe_run_cb(nni_pipe *p, nng_pipe_ev ev)
{
    nni_sock *  s = p->p_sock;
    nng_pipe_cb cb;
    void *      arg;
 
    nni_mtx_lock(&s->s_pipe_cbs_mtx);
    if (!p->p_cbs) {
        if (ev == NNG_PIPE_EV_ADD_PRE) {
            // First event, after this we want all other events.
            p->p_cbs = true;
        } else {
            nni_mtx_unlock(&s->s_pipe_cbs_mtx);
            return;
        }
    }
    cb  = s->s_pipe_cbs[ev].cb_fn;
    arg = s->s_pipe_cbs[ev].cb_arg;
    nni_mtx_unlock(&s->s_pipe_cbs_mtx);
 
    if (cb != NULL) {
        nng_pipe pid;
        pid.id = p->p_id;
        cb(pid, ev, arg);
    }
}
 
void
nni_pipe_remove(nni_pipe *p)
{
    nni_sock *  s = p->p_sock;
    nni_dialer *d = p->p_dialer;
 
    nni_mtx_lock(&s->s_mx);
#ifdef NNG_ENABLE_STATS
    if (nni_list_node_active(&p->p_sock_node)) {
        nni_stat_dec(&s->st_pipes, 1);
    }
    if (p->p_listener != NULL) {
        nni_stat_dec(&p->p_listener->st_pipes, 1);
    }
    if (p->p_dialer != NULL) {
        nni_stat_dec(&p->p_dialer->st_pipes, 1);
    }
#endif
    nni_list_node_remove(&p->p_sock_node);
    nni_list_node_remove(&p->p_ep_node);
    p->p_listener = NULL;
    p->p_dialer   = NULL;
    if ((d != NULL) && (d->d_pipe == p)) {
        d->d_pipe = NULL;
        dialer_timer_start_locked(d); // Kick the timer to redial.
    }
    if (s->s_closing) {
        nni_cv_wake(&s->s_cv);
    }
    nni_mtx_unlock(&s->s_mx);
}
 
void
nni_sock_add_stat(nni_sock *s, nni_stat_item *stat)
{
#ifdef NNG_ENABLE_STATS
    nni_stat_add(&s->st_root, stat);
#else
    NNI_ARG_UNUSED(s);
    NNI_ARG_UNUSED(stat);
#endif
}
 
void
nni_sock_bump_tx(nni_sock *s, uint64_t sz)
{
#ifdef NNG_ENABLE_STATS
    nni_stat_inc(&s->st_tx_msgs, 1);
    nni_stat_inc(&s->st_tx_bytes, sz);
#else
    NNI_ARG_UNUSED(s);
    NNI_ARG_UNUSED(sz);
#endif
}
 
void
nni_sock_bump_rx(nni_sock *s, uint64_t sz)
{
#ifdef NNG_ENABLE_STATS
    nni_stat_inc(&s->st_rx_msgs, 1);
    nni_stat_inc(&s->st_rx_bytes, sz);
#else
    NNI_ARG_UNUSED(s);
    NNI_ARG_UNUSED(sz);
#endif
}