aboutsummaryrefslogtreecommitdiff
path: root/src/core/socket.c
blob: c9b70ccbd4694716e354767aa3a37b30027b71ba (plain)
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
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
// Copyright 2017 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 <stdio.h>
#include <string.h>

// Socket implementation.

static nni_list    nni_sock_list;
static nni_idhash *nni_sock_hash;
static nni_mtx     nni_sock_lk;

typedef struct nni_socket_option {
	const char *so_name;
	int (*so_getopt)(nni_sock *, void *, size_t *);
	int (*so_setopt)(nni_sock *, const void *, size_t);
} nni_socket_option;

typedef struct nni_sockopt {
	nni_list_node node;
	char *        name;
	size_t        sz;
	void *        data;
} nni_sockopt;

struct nni_socket {
	nni_list_node s_node;
	nni_mtx       s_mx;
	nni_cv        s_cv;
	nni_cv        s_close_cv;
	int           s_raw;

	uint64_t s_id;
	uint32_t s_flags;
	unsigned s_refcnt; // protected by global lock
	void *   s_data;   // Protocol private

	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;

	// options
	nni_duration s_linger;    // linger time
	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_eps;   // active endpoints
	nni_list s_pipes; // active pipes

	int s_ep_pend; // EP dial/listen in progress
	int s_closing; // Socket is closing
	int s_closed;  // Socket closed, protected by global lock

	nni_notifyfd s_send_fd;
	nni_notifyfd s_recv_fd;
};

static void
nni_sock_can_send_cb(void *arg, int flags)
{
	nni_notifyfd *fd = arg;

	if ((flags & nni_msgq_f_can_put) == 0) {
		nni_plat_pipe_clear(fd->sn_rfd);
	} else {
		nni_plat_pipe_raise(fd->sn_wfd);
	}
}

static void
nni_sock_can_recv_cb(void *arg, int flags)
{
	nni_notifyfd *fd = arg;

	if ((flags & nni_msgq_f_can_get) == 0) {
		nni_plat_pipe_clear(fd->sn_rfd);
	} else {
		nni_plat_pipe_raise(fd->sn_wfd);
	}
}

static int
nni_sock_getopt_fd(nni_sock *s, int flag, void *val, size_t *szp)
{
	int           rv;
	uint32_t      flags;
	nni_notifyfd *fd;
	nni_msgq *    mq;
	nni_msgq_cb   cb;

	if ((*szp < sizeof(int))) {
		return (NNG_EINVAL);
	}

	if ((flag & nni_sock_flags(s)) == 0) {
		return (NNG_ENOTSUP);
	}

	switch (flag) {
	case NNI_PROTO_FLAG_SND:
		fd = &s->s_send_fd;
		mq = s->s_uwq;
		cb = nni_sock_can_send_cb;
		break;
	case NNI_PROTO_FLAG_RCV:
		fd = &s->s_recv_fd;
		mq = s->s_urq;
		cb = nni_sock_can_recv_cb;
		break;
	default:
		nni_panic("default case!");
	}

	// If we already inited this, just give back the same file descriptor.
	if (fd->sn_init) {
		memcpy(val, &fd->sn_rfd, sizeof(int));
		*szp = sizeof(int);
		return (0);
	}

	if ((rv = nni_plat_pipe_open(&fd->sn_wfd, &fd->sn_rfd)) != 0) {
		return (rv);
	}

	nni_msgq_set_cb(mq, cb, fd);

	fd->sn_init = 1;
	*szp        = sizeof(int);
	memcpy(val, &fd->sn_rfd, sizeof(int));
	return (0);
}

static int
nni_sock_getopt_sendfd(nni_sock *s, void *buf, size_t *szp)
{
	return (nni_sock_getopt_fd(s, NNI_PROTO_FLAG_SND, buf, szp));
}

static int
nni_sock_getopt_recvfd(nni_sock *s, void *buf, size_t *szp)
{
	return (nni_sock_getopt_fd(s, NNI_PROTO_FLAG_RCV, buf, szp));
}

static int
nni_sock_setopt_recvtimeo(nni_sock *s, const void *buf, size_t sz)
{
	return (nni_setopt_ms(&s->s_rcvtimeo, buf, sz));
}

static int
nni_sock_getopt_recvtimeo(nni_sock *s, void *buf, size_t *szp)
{
	return (nni_getopt_ms(s->s_rcvtimeo, buf, szp));
}

static int
nni_sock_setopt_sendtimeo(nni_sock *s, const void *buf, size_t sz)
{
	return (nni_setopt_ms(&s->s_sndtimeo, buf, sz));
}

static int
nni_sock_getopt_sendtimeo(nni_sock *s, void *buf, size_t *szp)
{
	return (nni_getopt_ms(s->s_sndtimeo, buf, szp));
}

static int
nni_sock_setopt_reconnmint(nni_sock *s, const void *buf, size_t sz)
{
	return (nni_setopt_ms(&s->s_reconn, buf, sz));
}

static int
nni_sock_getopt_reconnmint(nni_sock *s, void *buf, size_t *szp)
{
	return (nni_getopt_ms(s->s_reconn, buf, szp));
}

static int
nni_sock_setopt_reconnmaxt(nni_sock *s, const void *buf, size_t sz)
{
	return (nni_setopt_ms(&s->s_reconnmax, buf, sz));
}

static int
nni_sock_getopt_reconnmaxt(nni_sock *s, void *buf, size_t *szp)
{
	return (nni_getopt_ms(s->s_reconnmax, buf, szp));
}

static int
nni_sock_setopt_recvbuf(nni_sock *s, const void *buf, size_t sz)
{
	return (nni_setopt_buf(s->s_urq, buf, sz));
}

static int
nni_sock_getopt_recvbuf(nni_sock *s, void *buf, size_t *szp)
{
	return (nni_getopt_buf(s->s_urq, buf, szp));
}

static int
nni_sock_setopt_sendbuf(nni_sock *s, const void *buf, size_t sz)
{
	return (nni_setopt_buf(s->s_uwq, buf, sz));
}

static int
nni_sock_getopt_sendbuf(nni_sock *s, void *buf, size_t *szp)
{
	return (nni_getopt_buf(s->s_uwq, buf, szp));
}

static int
nni_sock_getopt_sockname(nni_sock *s, void *buf, size_t *szp)
{
	return (nni_getopt_str(s->s_name, buf, szp));
}

static int
nni_sock_setopt_sockname(nni_sock *s, const void *buf, size_t sz)
{
	if (nni_strnlen(buf, sz) > sizeof(s->s_name) - 1) {
		return (NNG_EINVAL);
	}
	nni_strlcpy(s->s_name, buf, sizeof(s->s_name));
	return (0);
}

static int
nni_sock_getopt_domain(nni_sock *s, void *buf, size_t *szp)
{
	return (nni_getopt_int(s->s_raw + 1, buf, szp));
}

static const nni_socket_option nni_sock_options[] = {
	{
	    .so_name   = NNG_OPT_RECVTIMEO,
	    .so_getopt = nni_sock_getopt_recvtimeo,
	    .so_setopt = nni_sock_setopt_recvtimeo,
	},
	{
	    .so_name   = NNG_OPT_SENDTIMEO,
	    .so_getopt = nni_sock_getopt_sendtimeo,
	    .so_setopt = nni_sock_setopt_sendtimeo,
	},
	{
	    .so_name   = NNG_OPT_RECVFD,
	    .so_getopt = nni_sock_getopt_recvfd,
	    .so_setopt = NULL,
	},
	{
	    .so_name   = NNG_OPT_SENDFD,
	    .so_getopt = nni_sock_getopt_sendfd,
	    .so_setopt = NULL,
	},
	{
	    .so_name   = NNG_OPT_RECVBUF,
	    .so_getopt = nni_sock_getopt_recvbuf,
	    .so_setopt = nni_sock_setopt_recvbuf,
	},
	{
	    .so_name   = NNG_OPT_SENDBUF,
	    .so_getopt = nni_sock_getopt_sendbuf,
	    .so_setopt = nni_sock_setopt_sendbuf,
	},
	{
	    .so_name   = NNG_OPT_RECONNMINT,
	    .so_getopt = nni_sock_getopt_reconnmint,
	    .so_setopt = nni_sock_setopt_reconnmint,
	},
	{
	    .so_name   = NNG_OPT_RECONNMAXT,
	    .so_getopt = nni_sock_getopt_reconnmaxt,
	    .so_setopt = nni_sock_setopt_reconnmaxt,
	},
	{
	    .so_name   = NNG_OPT_SOCKNAME,
	    .so_getopt = nni_sock_getopt_sockname,
	    .so_setopt = nni_sock_setopt_sockname,
	},
	{
	    .so_name   = NNG_OPT_DOMAIN,
	    .so_getopt = nni_sock_getopt_domain,
	    .so_setopt = NULL,
	},
	// terminate list
	{ NULL, NULL, 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 ((uint32_t) 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(&nni_sock_lk);
	if ((rv = nni_idhash_find(nni_sock_hash, id, (void **) &s)) == 0) {
		if (s->s_closed) {
			rv = NNG_ECLOSED;
		} else {
			s->s_refcnt++;
			*sockp = s;
		}
	}
	nni_mtx_unlock(&nni_sock_lk);

	return (rv);
}

void
nni_sock_rele(nni_sock *s)
{
	nni_mtx_lock(&nni_sock_lk);
	s->s_refcnt--;
	if (s->s_closed && (s->s_refcnt < 2)) {
		nni_cv_wake(&s->s_close_cv);
	}
	nni_mtx_unlock(&nni_sock_lk);
}

int
nni_sock_pipe_start(nni_sock *s, nni_pipe *pipe)
{
	void *pdata = nni_pipe_get_proto_data(pipe);
	int   rv;

	NNI_ASSERT(s != NULL);
	nni_mtx_lock(&s->s_mx);
	if (s->s_closing) {
		// We're closing, bail out.
		rv = NNG_ECLOSED;
	} else if (nni_pipe_peer(pipe) != s->s_peer_id.p_id) {
		// Peer protocol mismatch.
		rv = NNG_EPROTO;
	} else {
		// Protocol can reject for other reasons.
		rv = s->s_pipe_ops.pipe_start(pdata);
	}
	nni_mtx_unlock(&s->s_mx);
	return (rv);
}

int
nni_sock_pipe_add(nni_sock *s, nni_pipe *p)
{
	int   rv;
	void *pdata;

	if ((rv = s->s_pipe_ops.pipe_init(&pdata, p, s->s_data)) != 0) {
		return (rv);
	}
	nni_pipe_set_proto_data(p, pdata);

	// Initialize protocol pipe data.
	nni_mtx_lock(&s->s_mx);
	if (s->s_closing) {
		nni_mtx_unlock(&s->s_mx);
		return (NNG_ECLOSED);
	}

	nni_list_append(&s->s_pipes, p);

	// Start the initial negotiation I/O...
	nni_pipe_start(p);

	nni_mtx_unlock(&s->s_mx);
	return (0);
}

void
nni_sock_pipe_remove(nni_sock *sock, nni_pipe *pipe)
{
	void *pdata;

	nni_mtx_lock(&sock->s_mx);
	pdata = nni_pipe_get_proto_data(pipe);
	if (pdata != NULL) {
		sock->s_pipe_ops.pipe_stop(pdata);
		nni_pipe_set_proto_data(pipe, NULL);
		if (nni_list_active(&sock->s_pipes, pipe)) {
			nni_list_remove(&sock->s_pipes, pipe);
		}
		sock->s_pipe_ops.pipe_fini(pdata);
	}
	if (sock->s_closing && nni_list_empty(&sock->s_pipes)) {
		nni_cv_wake(&sock->s_cv);
	}
	nni_mtx_unlock(&sock->s_mx);
}

static void
nni_sock_destroy(nni_sock *s)
{
	nni_sockopt *sopt;

	// Close any open notification pipes.
	if (s->s_recv_fd.sn_init) {
		nni_plat_pipe_close(s->s_recv_fd.sn_wfd, s->s_recv_fd.sn_rfd);
	}
	if (s->s_send_fd.sn_init) {
		nni_plat_pipe_close(s->s_send_fd.sn_wfd, s->s_send_fd.sn_rfd);
	}

	// The protocol needs to clean up its state.
	if (s->s_data != NULL) {
		s->s_sock_ops.sock_fini(s->s_data);
	}

	while ((sopt = nni_list_first(&s->s_options)) != NULL) {
		nni_list_remove(&s->s_options, sopt);
		nni_free_opt(sopt);
	}

	// This exists to silence a false positive in helgrind.
	nni_mtx_lock(&s->s_mx);
	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_FREE_STRUCT(s);
}

static int
nni_sock_create(nni_sock **sp, const nni_proto *proto)
{
	int       rv;
	nni_sock *s;

	if ((s = NNI_ALLOC_STRUCT(s)) == NULL) {
		return (NNG_ENOMEM);
	}
	s->s_linger          = 0;
	s->s_sndtimeo        = -1;
	s->s_rcvtimeo        = -1;
	s->s_closing         = 0;
	s->s_reconn          = NNI_SECOND;
	s->s_reconnmax       = 0;
	s->s_rcvmaxsz        = 1024 * 1024; // 1 MB by default
	s->s_id              = 0;
	s->s_refcnt          = 0;
	s->s_send_fd.sn_init = 0;
	s->s_recv_fd.sn_init = 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;

	NNI_ASSERT(s->s_sock_ops.sock_open != NULL);
	NNI_ASSERT(s->s_sock_ops.sock_close != NULL);

	NNI_ASSERT(s->s_pipe_ops.pipe_start != NULL);
	NNI_ASSERT(s->s_pipe_ops.pipe_stop != NULL);

	NNI_LIST_NODE_INIT(&s->s_node);
	NNI_LIST_INIT(&s->s_options, nni_sockopt, node);
	nni_pipe_sock_list_init(&s->s_pipes);
	nni_ep_list_init(&s->s_eps);
	nni_mtx_init(&s->s_mx);
	nni_cv_init(&s->s_cv, &s->s_mx);
	nni_cv_init(&s->s_close_cv, &nni_sock_lk);

	if (((rv = nni_msgq_init(&s->s_uwq, 0)) != 0) ||
	    ((rv = nni_msgq_init(&s->s_urq, 0)) != 0) ||
	    ((rv = s->s_sock_ops.sock_init(&s->s_data, s)) != 0) ||
	    ((rv = nni_sock_setopt(s, NNG_OPT_LINGER, &s->s_linger,
	          sizeof(nni_duration))) != 0) ||
	    ((rv = nni_sock_setopt(s, NNG_OPT_SENDTIMEO, &s->s_sndtimeo,
	          sizeof(nni_duration))) != 0) ||
	    ((rv = nni_sock_setopt(s, NNG_OPT_RECVTIMEO, &s->s_rcvtimeo,
	          sizeof(nni_duration))) != 0) ||
	    ((rv = nni_sock_setopt(s, NNG_OPT_RECONNMINT, &s->s_reconn,
	          sizeof(nni_duration))) != 0) ||
	    ((rv = nni_sock_setopt(s, NNG_OPT_RECONNMAXT, &s->s_reconnmax,
	          sizeof(nni_duration))) != 0) ||
	    ((rv = nni_sock_setopt(s, NNG_OPT_RECVMAXSZ, &s->s_rcvmaxsz,
	          sizeof(size_t))) != 0)) {
		nni_sock_destroy(s);
		return (rv);
	}

	if (s->s_sock_ops.sock_filter != NULL) {
		nni_msgq_set_filter(
		    s->s_urq, s->s_sock_ops.sock_filter, s->s_data);
	}

	*sp = s;
	return (rv);
}

int
nni_sock_sys_init(void)
{
	int rv;

	NNI_LIST_INIT(&nni_sock_list, nni_sock, s_node);
	nni_mtx_init(&nni_sock_lk);

	if ((rv = nni_idhash_init(&nni_sock_hash)) != 0) {
		nni_sock_sys_fini();
	} else {
		nni_idhash_set_limits(nni_sock_hash, 1, 0x7fffffff, 1);
	}
	return (rv);
}

void
nni_sock_sys_fini(void)
{
	nni_idhash_fini(nni_sock_hash);
	nni_sock_hash = NULL;
	nni_mtx_fini(&nni_sock_lk);
}

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(&nni_sock_lk);
	if ((rv = nni_idhash_alloc(nni_sock_hash, &s->s_id, s)) != 0) {
		nni_sock_destroy(s);
	} else {
		nni_list_append(&nni_sock_list, s);
		s->s_sock_ops.sock_open(s->s_data);
		*sockp = s;
	}
	// Set the sockname.
	(void) snprintf(
	    s->s_name, sizeof(s->s_name), "%u", (unsigned) s->s_id);
	nni_mtx_unlock(&nni_sock_lk);

	return (rv);
}

// 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_ep *  ep;
	nni_ep *  nep;
	nni_time  linger;

	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 = 1;

	// Special optimization; if there are no pipes connected,
	// then there is no reason to linger since there's nothing that
	// could possibly send this data out.
	if (nni_list_first(&sock->s_pipes) == NULL) {
		linger = NNI_TIME_ZERO;
	} else {
		linger = nni_clock() + sock->s_linger;
	}

	// Close the EPs. This prevents new connections from forming but
	// but allows existing ones to drain.
	NNI_LIST_FOREACH (&sock->s_eps, ep) {
		nni_ep_shutdown(ep);
	}
	nni_mtx_unlock(&sock->s_mx);

	// We drain the upper write queue.  This is just like closing it,
	// except that the protocol gets a chance to get the messages and
	// push them down to the transport.  This operation can *block*
	// until the linger time has expired.
	nni_msgq_drain(sock->s_uwq, linger);

	// 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->s_mx);
	while (nni_list_first(&sock->s_pipes) != NULL) {
		if (nni_cv_until(&sock->s_cv, linger) == NNG_ETIMEDOUT) {
			break;
		}
	}

	// 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 endpoint list, 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.
	nep = nni_list_first(&sock->s_eps);
	while ((ep = nep) != NULL) {
		nep = nni_list_next(&sock->s_eps, nep);

		if (nni_ep_hold(ep) == 0) {
			nni_mtx_unlock(&sock->s_mx);
			nni_ep_close(ep);
			nni_mtx_lock(&sock->s_mx);
		}
	}

	// For each pipe, arrange for it to teardown hard.
	NNI_LIST_FOREACH (&sock->s_pipes, pipe) {
		nni_pipe_stop(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_eps))) {
		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(&nni_sock_lk);
	if (s->s_closed) {
		// Some other thread called close.  All we need to do is
		// drop our reference count.
		nni_mtx_unlock(&nni_sock_lk);
		nni_sock_rele(s);
		return;
	}
	s->s_closed = 1;
	nni_idhash_remove(nni_sock_hash, 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).
	while (s->s_refcnt > 1) {
		nni_cv_wait(&s->s_close_cv);
	}
	nni_mtx_unlock(&nni_sock_lk);

	// Wait for pipe and eps to finish closing.
	nni_mtx_lock(&s->s_mx);
	while (
	    (!nni_list_empty(&s->s_pipes)) || (!nni_list_empty(&s->s_eps))) {
		nni_cv_wait(&s->s_cv);
	}
	nni_mtx_unlock(&s->s_mx);

	nni_sock_destroy(s);
}

void
nni_sock_closeall(void)
{
	nni_sock *s;

	if (nni_sock_hash == NULL) {
		return;
	}
	for (;;) {
		nni_mtx_lock(&nni_sock_lk);
		if ((s = nni_list_first(&nni_sock_list)) == NULL) {
			nni_mtx_unlock(&nni_sock_lk);
			return;
		}
		// Bump the reference count.  The close call below will
		// drop it.
		s->s_refcnt++;
		nni_list_node_remove(&s->s_node);
		nni_mtx_unlock(&nni_sock_lk);
		nni_sock_close(s);
	}
}

static void
nni_sock_normalize_expiration(nni_aio *aio, nni_duration def)
{
	if (aio->a_reltime) {
		if (aio->a_expire == (nni_time) -2) {
			aio->a_expire = def;
		}
		switch (aio->a_expire) {
		case (nni_time) 0:
			aio->a_expire = NNI_TIME_ZERO;
			break;
		case (nni_time) -1:
			aio->a_expire = NNI_TIME_NEVER;
			break;
		default:
			aio->a_expire = nni_clock() + aio->a_expire;
			break;
		}
		aio->a_reltime = 0;
	}
}

void
nni_sock_send(nni_sock *sock, nni_aio *aio)
{
	nni_sock_normalize_expiration(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_sock_normalize_expiration(aio, sock->s_rcvtimeo);
	sock->s_sock_ops.sock_recv(sock->s_data, aio);
}

// nni_sock_protocol returns the socket's 16-bit protocol number.
uint16_t
nni_sock_proto(nni_sock *sock)
{
	return (sock->s_self_id.p_id);
}

uint16_t
nni_sock_peer(nni_sock *sock)
{
	return (sock->s_peer_id.p_id);
}

void
nni_sock_reconntimes(nni_sock *sock, nni_duration *rcur, nni_duration *rmax)
{
	// These two values are linked, so get them atomically.
	nni_mtx_lock(&sock->s_mx);
	*rcur = sock->s_reconn;
	*rmax = sock->s_reconnmax ? sock->s_reconnmax : sock->s_reconn;
	nni_mtx_unlock(&sock->s_mx);
}

int
nni_sock_ep_add(nni_sock *s, nni_ep *ep)
{
	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_ep_setopt(ep, sopt->name, sopt->data, sopt->sz);
		if ((rv != 0) && (rv != NNG_ENOTSUP)) {
			nni_mtx_unlock(&s->s_mx);
			return (rv);
		}
	}

	nni_list_append(&s->s_eps, ep);
	nni_mtx_unlock(&s->s_mx);
	return (0);
}

void
nni_sock_ep_remove(nni_sock *sock, nni_ep *ep)
{
	nni_mtx_lock(&sock->s_mx);
	if (nni_list_active(&sock->s_eps, ep)) {
		nni_list_remove(&sock->s_eps, ep);
		if ((sock->s_closing) && (nni_list_empty(&sock->s_eps))) {
			nni_cv_wake(&sock->s_cv);
		}
	}
	nni_mtx_unlock(&sock->s_mx);
}

int
nni_sock_setopt(nni_sock *s, const char *name, const void *val, size_t size)
{
	int                          rv = NNG_ENOTSUP;
	nni_ep *                     ep;
	int                          commits = 0;
	nni_sockopt *                optv;
	nni_sockopt *                oldv = NULL;
	const nni_socket_option *    sso;
	const nni_proto_sock_option *pso;

	nni_mtx_lock(&s->s_mx);
	if (s->s_closing) {
		nni_mtx_unlock(&s->s_mx);
		return (NNG_ECLOSED);
	}

	// Protocol options.
	for (pso = s->s_sock_ops.sock_options; pso->pso_name != NULL; pso++) {
		if (strcmp(pso->pso_name, name) != 0) {
			continue;
		}
		if (pso->pso_setopt == NULL) {
			nni_mtx_unlock(&s->s_mx);
			return (NNG_EREADONLY);
		}
		rv = pso->pso_setopt(s->s_data, val, size);
		if ((rv == 0) && (strcmp(name, NNG_OPT_RAW) == 0) &&
		    (size >= sizeof(int))) {
			// Save the raw option -- we use this for the DOMAIN.
			memcpy(&s->s_raw, val, sizeof(int));
		}
		nni_mtx_unlock(&s->s_mx);
		return (rv);
	}

	// Some options do not go down to transports.  Handle them directly.
	for (sso = nni_sock_options; sso->so_name != NULL; sso++) {
		if (strcmp(sso->so_name, name) != 0) {
			continue;
		}
		if (sso->so_setopt == NULL) {
			nni_mtx_unlock(&s->s_mx);
			return (NNG_EREADONLY);
		}
		rv = sso->so_setopt(s, val, size);
		nni_mtx_unlock(&s->s_mx);
		return (rv);
	}

	nni_mtx_unlock(&s->s_mx);

	// If the option was already handled one way or the other,
	if (rv != NNG_ENOTSUP) {
		return (rv);
	}

	// Validation of transport options.  This is stateless, so transports
	// should not fail to set an option later if they passed it here.
	rv = nni_tran_chkopt(name, val, size);

	// Also check a few generic things.  We do this if no transport
	// was found, or even if a transport rejected one of the settings.
	if ((rv == NNG_ENOTSUP) || (rv == 0)) {
		if ((strcmp(name, NNG_OPT_LINGER) == 0)) {
			rv = nni_chkopt_ms(val, size);
		} else if (strcmp(name, NNG_OPT_RECVMAXSZ) == 0) {
			// just a sanity test on the size; it also ensures that
			// a size can be set even with no transport configured.
			rv = nni_chkopt_size(val, size, 0, NNI_MAXSZ);
		}
	}

	if (rv != 0) {
		return (rv);
	}

	// Prepare a copy of the sockoption.
	if ((optv = NNI_ALLOC_STRUCT(optv)) == NULL) {
		return (NNG_ENOMEM);
	}
	if ((optv->data = nni_alloc(size)) == NULL) {
		NNI_FREE_STRUCT(optv);
		return (NNG_ENOMEM);
	}
	if ((optv->name = nni_strdup(name)) == NULL) {
		nni_free(optv->data, size);
		NNI_FREE_STRUCT(optv);
		return (NNG_ENOMEM);
	}
	memcpy(optv->data, val, size);
	optv->sz = size;
	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 != size) ||
			    (memcmp(oldv->data, val, size) != 0)) {
				break;
			}

			// The values are the same.  This is a no-op.
			nni_mtx_unlock(&s->s_mx);
			nni_free_opt(optv);
			return (0);
		}
	}

	// 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_eps, ep) {
		int x;
		x = nni_ep_setopt(ep, optv->name, optv->data, size);
		if (x != NNG_ENOTSUP) {
			if ((rv = x) != 0) {
				nni_mtx_unlock(&s->s_mx);
				nni_free_opt(optv);
				return (rv);
			}
		}
	}

	// For some options, which also have an impact on the socket
	// behavior, we save a local value.  Note that the transport
	// will already have had a chance to veto this.

	if (strcmp(name, NNG_OPT_LINGER) == 0) {
		rv = nni_setopt_ms(&s->s_linger, val, size);
	}

	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)
{
	int                          rv = NNG_ENOTSUP;
	nni_sockopt *                sopt;
	const nni_socket_option *    sso;
	const nni_proto_sock_option *pso;

	nni_mtx_lock(&s->s_mx);
	if (s->s_closing) {
		nni_mtx_unlock(&s->s_mx);
		return (NNG_ECLOSED);
	}

	// Protocol specific options.
	for (pso = s->s_sock_ops.sock_options; pso->pso_name != NULL; pso++) {
		if (strcmp(name, pso->pso_name) != 0) {
			continue;
		}
		if (pso->pso_getopt == NULL) {
			nni_mtx_unlock(&s->s_mx);
			return (NNG_EWRITEONLY);
		}
		rv = pso->pso_getopt(s->s_data, val, szp);
		nni_mtx_unlock(&s->s_mx);
		return (rv);
	}

	// Options that are handled by socket core, and never passed down.
	for (sso = nni_sock_options; sso->so_name != NULL; sso++) {
		if (strcmp(name, sso->so_name) != 0) {
			continue;
		}
		if (sso->so_getopt == NULL) {
			nni_mtx_unlock(&s->s_mx);
			return (NNG_EWRITEONLY);
		}
		rv = sso->so_getopt(s, val, szp);
		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->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);
}