-
Notifications
You must be signed in to change notification settings - Fork 244
/
sc_sock.c
2143 lines (1704 loc) · 43.6 KB
/
sc_sock.c
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
/*
* BSD-3-Clause
*
* Copyright 2021 Ozan Tezcan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
#include "sc_sock.h"
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef SC_SIZE_MAX
#define SC_SIZE_MAX INT32_MAX
#endif
#ifndef SC_SOCK_POLL_MAX_EVENTS
#define SC_SOCK_POLL_MAX_EVENTS 1024
#endif
#if defined(_WIN32) || defined(_WIN64)
#include <afunix.h>
#include <assert.h>
#include <ws2tcpip.h>
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif
#define sc_close(n) closesocket(n)
#define sc_unlink(n) DeleteFileA(n)
#define SC_ERR SOCKET_ERROR
#define SC_INVALID INVALID_SOCKET
#define SC_EAGAIN WSAEWOULDBLOCK
#define SC_EINPROGRESS WSAEINPROGRESS
#define SC_EINTR WSAEINTR
typedef int socklen_t;
static int sc_sock_err(void)
{
return WSAGetLastError();
}
static void sc_sock_errstr(struct sc_sock *s, int gai_err)
{
(void)gai_err;
DWORD rc;
DWORD err = (DWORD)WSAGetLastError();
LPSTR str = 0;
rc = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, err, 0, (LPSTR) &str, 0, NULL);
if (rc != 0) {
strncpy(s->err, str, sizeof(s->err) - 1);
LocalFree(str);
}
}
int sc_sock_set_blocking(struct sc_sock *s, bool blocking)
{
u_long mode = blocking ? 0 : 1;
int rc = ioctlsocket(s->fdt.fd, (long)FIONBIO, &mode);
return rc == 0 ? 0 : -1;
}
int sc_sock_startup(void)
{
int rc;
WSADATA data;
rc = WSAStartup(MAKEWORD(2, 2), &data);
if (rc != 0 ||
(LOBYTE(data.wVersion) != 2 || HIBYTE(data.wVersion) != 2)) {
return -1;
}
return 0;
}
int sc_sock_cleanup(void)
{
int rc;
rc = WSACleanup();
return rc != 0 ? -1 : 0;
}
int sc_sock_notify_systemd(const char *msg)
{
(void)msg;
return -1;
}
#else
#include <arpa/inet.h>
#include <assert.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/time.h>
#include <sys/un.h>
#include <unistd.h>
#define sc_close(n) close(n)
#define sc_unlink(n) unlink(n)
#define SC_ERR (-1)
#define SC_INVALID (-1)
#define SC_EAGAIN EAGAIN
#define SC_EINPROGRESS EINPROGRESS
#define SC_EINTR EINTR
int sc_sock_notify_systemd(const char *msg)
{
assert(msg != NULL);
int fd, rc;
const char *s;
struct sockaddr_un addr = {
.sun_family = AF_UNIX,
};
struct iovec iovec = {
.iov_base = (char *) msg,
.iov_len = strlen(msg),
};
struct msghdr msghdr = {
.msg_name = &addr,
.msg_iov = &iovec,
.msg_iovlen = 1,
};
s = getenv("NOTIFY_SOCKET");
if (!s) {
errno = EINVAL;
return -1;
}
if ((s[0] != '@' && s[0] != '/') || s[1] == '\0') {
errno = EINVAL;
return -1;
}
fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (fd < 0) {
return -1;
}
strncpy(addr.sun_path, s, sizeof(addr.sun_path) - 1);
if (addr.sun_path[0] == '@') {
addr.sun_path[0] = '\0';
}
msghdr.msg_namelen = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + strlen(s));
if (msghdr.msg_namelen > sizeof(struct sockaddr_un)) {
msghdr.msg_namelen = sizeof(struct sockaddr_un);
}
rc = (int) sendmsg(fd, &msghdr, MSG_NOSIGNAL);
close(fd);
return rc < 0 ? -1 : 0;
}
int sc_sock_startup(void)
{
return 0;
}
int sc_sock_cleanup(void)
{
return 0;
}
static int sc_sock_err(void)
{
return errno;
}
static void sc_sock_errstr(struct sc_sock *s, int gai_err)
{
const char *str;
str = gai_err ? gai_strerror(gai_err) : strerror(errno);
strncpy(s->err, str, sizeof(s->err) - 1);
}
int sc_sock_set_blocking(struct sc_sock *s, bool blocking)
{
int flags;
flags = fcntl(s->fdt.fd, F_GETFL, 0);
if (flags == -1) {
return -1;
}
flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
return (fcntl(s->fdt.fd, F_SETFL, flags) == 0) ? 0 : -1;
}
#endif
void sc_sock_init(struct sc_sock *s, int type, bool blocking, int family)
{
*s = (struct sc_sock){
.fdt.fd = SC_INVALID,
.fdt.type = type,
.blocking = blocking,
.family = family
};
#if defined(_WIN32) || defined(_WIN64)
s->fdt.op_index = -1;
#endif
}
static int sc_sock_close(struct sc_sock *s)
{
int rc = 0;
if (s->fdt.fd != SC_INVALID) {
rc = sc_close(s->fdt.fd);
s->fdt.fd = SC_INVALID;
}
return (rc == 0) ? 0 : -1;
}
int sc_sock_term(struct sc_sock *s)
{
int rc;
rc = sc_sock_close(s);
if (rc != 0) {
sc_sock_errstr(s, 0);
}
return rc;
}
int sc_sock_set_rcvtimeo(struct sc_sock *s, int ms)
{
int rc;
void *p;
struct timeval tv = {
.tv_usec = ms % 1000,
.tv_sec = ms / 1000,
};
p = (void *) &tv;
rc = setsockopt(s->fdt.fd, SOL_SOCKET, SO_RCVTIMEO, p, sizeof(tv));
if (rc != 0) {
sc_sock_errstr(s, 0);
}
return rc;
}
int sc_sock_set_sndtimeo(struct sc_sock *s, int ms)
{
int rc;
void *p;
struct timeval tv = {
.tv_usec = ms % 1000,
.tv_sec = ms / 1000,
};
p = (void *) &tv;
rc = setsockopt(s->fdt.fd, SOL_SOCKET, SO_SNDTIMEO, p, sizeof(tv));
if (rc != 0) {
sc_sock_errstr(s, 0);
}
return rc;
}
static int sc_sock_bind_unix(struct sc_sock *s, const char *host)
{
int rc;
struct sockaddr_un addr = {
.sun_family = AF_UNIX,
};
strncpy(addr.sun_path, host, sizeof(addr.sun_path) - 1);
sc_unlink(host);
rc = bind(s->fdt.fd, (struct sockaddr *) &addr, sizeof(addr));
return rc == 0 ? 0 : -1;
}
static int sc_sock_bind(struct sc_sock *s, const char *host, const char *port)
{
int rc, rv = 0;
struct addrinfo *servinfo = NULL;
struct addrinfo hints = {
.ai_family = s->family,
.ai_socktype = SOCK_STREAM,
};
*s->err = '\0';
if (s->family == AF_UNIX) {
sc_sock_int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == SC_INVALID) {
goto error_unix;
}
s->fdt.fd = fd;
rc = sc_sock_bind_unix(s, host);
if (rc != 0) {
goto error_unix;
}
return 0;
error_unix:
sc_sock_errstr(s, 0);
sc_sock_close(s);
return -1;
}
rc = getaddrinfo(host, port, &hints, &servinfo);
if (rc != 0) {
sc_sock_errstr(s, rc);
return -1;
}
for (struct addrinfo *p = servinfo; p != NULL; p = p->ai_next) {
const int tsz = sizeof(int);
sc_sock_int fd;
void *tmp;
fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (fd == SC_INVALID) {
continue;
}
s->fdt.fd = fd;
if (s->family == AF_INET6) {
rc = setsockopt(s->fdt.fd, IPPROTO_IPV6, IPV6_V6ONLY,
(void *) &(int){1}, tsz);
if (rc != 0) {
goto error;
}
}
rc = sc_sock_set_blocking(s, s->blocking);
if (rc != 0) {
goto error;
}
tmp = (void *) &(int){1};
rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, tmp, sizeof(int));
if (rc != 0) {
goto error;
}
rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, tmp, sizeof(int));
if (rc != 0) {
goto error;
}
rc = bind(s->fdt.fd, p->ai_addr, (socklen_t) p->ai_addrlen);
if (rc == -1) {
goto error;
}
goto out;
}
error:
sc_sock_errstr(s, 0);
sc_sock_close(s);
rv = -1;
out:
freeaddrinfo(servinfo);
return rv;
}
int sc_sock_finish_connect(struct sc_sock *s)
{
int ret, rc;
socklen_t len = sizeof(ret);
rc = getsockopt(s->fdt.fd, SOL_SOCKET, SO_ERROR, (void *) &ret, &len);
if (rc == 0 && ret != 0) {
errno = ret;
rc = -1;
}
if (rc != 0) {
sc_sock_errstr(s, 0);
return -1;
}
return 0;
}
static int sc_sock_connect_unix(struct sc_sock *s, const char *addr)
{
const size_t len = strlen(addr);
int rc;
sc_sock_int fd;
struct sockaddr_un un = {
.sun_family = AF_UNIX,
};
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == SC_INVALID) {
goto err;
}
s->fdt.fd = fd;
if (len >= sizeof(un.sun_path)) {
errno = EINVAL;
goto err;
}
strcpy(un.sun_path, addr);
rc = connect(s->fdt.fd, (struct sockaddr *) &un, sizeof(un));
if (rc != 0) {
goto err;
}
return 0;
err:
sc_sock_errstr(s, 0);
sc_sock_close(s);
return -1;
}
static int sc_sock_bind_src(struct sc_sock *s, const char *addr,
const char *port)
{
int rc;
struct addrinfo *p = NULL, *i;
struct addrinfo inf = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
};
rc = getaddrinfo(addr, port, &inf, &p);
if (rc != 0) {
goto gai_err;
}
for (i = p; i != NULL; i = i->ai_next) {
rc = bind(s->fdt.fd, i->ai_addr, (socklen_t) i->ai_addrlen);
if (rc != -1) {
break;
}
}
freeaddrinfo(p);
if (rc == -1) {
goto err;
}
return 0;
gai_err:
sc_sock_errstr(s, rc);
return -1;
err:
sc_sock_errstr(s, 0);
return -1;
}
int sc_sock_connect(struct sc_sock *s, const char *dst_addr,
const char *dst_port, const char *src_addr,
const char *src_port)
{
int family = s->family;
int rc, rv = 0;
sc_sock_int fd;
void *tmp;
struct addrinfo *sinfo = NULL, *p;
struct addrinfo inf = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
};
if (family == AF_UNIX) {
return sc_sock_connect_unix(s, dst_addr);
}
rc = getaddrinfo(dst_addr, dst_port, &inf, &sinfo);
if (rc != 0) {
sc_sock_errstr(s, rc);
return -1;
}
for (int i = 0; i < 2; i++) {
for (p = sinfo; p != NULL; p = p->ai_next) {
// Try same family addresses in the first iteration.
if ((i == 0) ^ (p->ai_family == family)) {
continue;
}
fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (fd == SC_INVALID) {
continue;
}
s->family = p->ai_family;
s->fdt.fd = fd;
rc = sc_sock_set_blocking(s, s->blocking);
if (rc != 0) {
goto error;
}
tmp = (void *) &(int){1};
rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, tmp, sizeof(int));
if (rc != 0) {
goto error;
}
rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, tmp, sizeof(int));
if (rc != 0) {
goto error;
}
if (src_addr || src_port) {
rc = sc_sock_bind_src(s, src_addr, src_port);
if (rc != 0) {
goto bind_error;
}
}
rc = connect(fd, p->ai_addr, (socklen_t) p->ai_addrlen);
if (rc != 0) {
if (!s->blocking && (sc_sock_err() == SC_EINPROGRESS ||
sc_sock_err() == SC_EAGAIN)) {
errno = EAGAIN;
rv = -1;
goto end;
}
sc_sock_close(s);
continue;
}
goto end;
}
}
error:
sc_sock_errstr(s, 0);
bind_error:
rv = -1;
sc_sock_close(s);
end:
freeaddrinfo(sinfo);
return rv;
}
#if defined(_WIN32) || defined(_WIN64)
#define sc_send_recv_size_t int
#else
#define sc_send_recv_size_t size_t
#endif
int sc_sock_send(struct sc_sock *s, char *buf, int len, int flags)
{
int n, err;
if (len <= 0) {
return len;
}
retry:
n = (int) send(s->fdt.fd, buf, (sc_send_recv_size_t) len, flags);
if (n == SC_ERR) {
err = sc_sock_err();
if (err == SC_EINTR) {
goto retry;
}
if (err == SC_EAGAIN) {
#if defined(_WIN32) || defined(_WIN64)
// Stop masking WRITE event.
struct sc_sock_poll_data *pd = s->fdt.poll_data;
if (pd != NULL && (pd->edge_mask & SC_SOCK_WRITE)) {
InterlockedAnd(&pd->edge_mask, ~SC_SOCK_WRITE);
}
#endif
errno = EAGAIN;
return -1;
}
sc_sock_errstr(s, 0);
n = -1;
}
return n;
}
int sc_sock_recv(struct sc_sock *s, char *buf, int len, int flags)
{
int n, err;
if (len <= 0) {
return len;
}
retry:
n = (int) recv(s->fdt.fd, buf, (sc_send_recv_size_t) len, flags);
if (n == 0) {
errno = EOF;
return -1;
} else if (n == SC_ERR) {
err = sc_sock_err();
if (err == SC_EINTR) {
goto retry;
}
if (err == SC_EAGAIN) {
#if defined(_WIN32) || defined(_WIN64)
// Stop masking READ event.
struct sc_sock_poll_data *pd = s->fdt.poll_data;
if (pd != NULL && (pd->edge_mask & SC_SOCK_READ)) {
InterlockedAnd(&pd->edge_mask, ~SC_SOCK_READ);
}
#endif
errno = EAGAIN;
return -1;
}
sc_sock_errstr(s, 0);
n = -1;
}
return n;
}
int sc_sock_accept(struct sc_sock *s, struct sc_sock *in)
{
const void *tmp = (void *) &(int){1};
int rc;
sc_sock_int fd;
fd = accept(s->fdt.fd, NULL, NULL);
if (fd == SC_INVALID) {
if (!s->blocking && sc_sock_err() == SC_EAGAIN) {
errno = EAGAIN;
} else {
sc_sock_errstr(s, 0);
}
return -1;
}
in->fdt.fd = fd;
in->fdt.op = SC_SOCK_NONE;
in->family = s->family;
if (in->family != AF_UNIX) {
rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, tmp, sizeof(int));
if (rc != 0) {
goto error;
}
}
rc = sc_sock_set_blocking(in, s->blocking);
if (rc != 0) {
goto error;
}
return 0;
error:
sc_sock_errstr(s, 0);
sc_sock_close(in);
return -1;
}
int sc_sock_listen(struct sc_sock *s, const char *host, const char *port)
{
int rc;
rc = sc_sock_bind(s, host, port);
if (rc != 0) {
return rc;
}
rc = listen(s->fdt.fd, 4096);
if (rc != 0) {
goto err;
}
return 0;
err:
sc_sock_errstr(s, 0);
sc_sock_close(s);
return -1;
}
const char *sc_sock_error(struct sc_sock *s)
{
s->err[sizeof(s->err) - 1] = '\0';
return s->err;
}
#if defined(_WIN32) || defined(_WIN64)
#define sc_sock_addr_size_t size_t
#else
#define sc_sock_addr_size_t socklen_t
#endif
static const char *sc_sock_addr(struct sc_sock *sock, int af, void *cp,
char *buf, size_t len)
{
const char *dest;
dest = inet_ntop(af, cp, buf, (sc_sock_addr_size_t) len);
if (dest == NULL) {
sc_sock_errstr(sock, 0);
*buf = '\0';
}
return dest;
}
static const char *sc_sock_print_storage(struct sc_sock *sock,
struct sockaddr_storage *storage,
char *buf, size_t len)
{
const char *dst;
struct sockaddr_in *addr;
struct sockaddr_in6 *addr6;
struct sockaddr_un *addr_un;
char tmp[INET6_ADDRSTRLEN];
*buf = '\0';
switch (storage->ss_family) {
case AF_INET:
addr = (struct sockaddr_in *) storage;
dst = sc_sock_addr(sock, AF_INET, &addr->sin_addr, tmp,
sizeof(tmp));
snprintf(buf, len, "%s:%d", dst, ntohs(addr->sin_port));
break;
case AF_INET6:
addr6 = (struct sockaddr_in6 *) storage;
dst = sc_sock_addr(sock, AF_INET6, &addr6->sin6_addr, tmp,
sizeof(tmp));
snprintf(buf, len, "%s:%d", dst, ntohs(addr6->sin6_port));
break;
case AF_UNIX:
addr_un = (struct sockaddr_un *) storage;
snprintf(buf, len, "%s", addr_un->sun_path);
break;
}
return buf;
}
const char *sc_sock_local_str(struct sc_sock *sock, char *buf, size_t len)
{
int rc;
struct sockaddr_storage st;
socklen_t storage_len = sizeof(st);
rc = getsockname(sock->fdt.fd, (struct sockaddr *) &st, &storage_len);
if (rc != 0) {
sc_sock_errstr(sock, 0);
*buf = '\0';
return NULL;
}
return sc_sock_print_storage(sock, &st, buf, len);
}
const char *sc_sock_remote_str(struct sc_sock *sock, char *buf, size_t len)
{
int rc;
struct sockaddr_storage st;
socklen_t storage_len = sizeof(st);
rc = getpeername(sock->fdt.fd, (struct sockaddr *) &st, &storage_len);
if (rc != 0) {
sc_sock_errstr(sock, 0);
*buf = '\0';
return NULL;
}
return sc_sock_print_storage(sock, &st, buf, len);
}
void sc_sock_print(struct sc_sock *sock, char *buf, size_t len)
{
char l[128];
char r[128];
sc_sock_local_str(sock, l, sizeof(l));
sc_sock_remote_str(sock, r, sizeof(r));
snprintf(buf, len, "Local(%s), Remote(%s) ", l, r);
}
const char *sc_sock_pipe_err(struct sc_sock_pipe *pipe)
{
pipe->err[sizeof(pipe->err) - 1] = '\0';
return pipe->err;
}
static void sc_sock_pipe_set_err(struct sc_sock_pipe *pipe, const char *fmt,
...)
{
va_list args;
va_start(args, fmt);
vsnprintf(pipe->err, sizeof(pipe->err), fmt, args);
va_end(args);
pipe->err[sizeof(pipe->err) - 1] = '\0';
}
#if defined(_WIN32) || defined(_WIN64)
int sc_sock_pipe_init(struct sc_sock_pipe *p, int type)
{
SOCKET listener;
int rc;
struct sockaddr_in addr;
int addrlen = sizeof(addr);
int val = 1;
BOOL nodelay = 1;
*p = (struct sc_sock_pipe){
.fds = {SC_INVALID, SC_INVALID},
};
p->fdt.type = type;
/* Create listening socket. */
listener = socket(AF_INET, SOCK_STREAM, 0);
if (listener == INVALID_SOCKET) {
goto wsafail;
}
rc = setsockopt(listener, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
(char *) &val, sizeof(val));
if (rc == SOCKET_ERROR) {
goto wsafail;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = 0;
rc = bind(listener, (const struct sockaddr *) &addr, sizeof(addr));
if (rc == SOCKET_ERROR) {
goto wsafail;
}
rc = getsockname(listener, (struct sockaddr *) &addr, &addrlen);
if (rc == SOCKET_ERROR) {
goto wsafail;
}
rc = listen(listener, 1);
if (rc == SOCKET_ERROR) {
goto wsafail;
}
p->fds[1] = socket(AF_INET, SOCK_STREAM, 0);
if (p->fds[1] == INVALID_SOCKET) {
goto wsafail;
}
rc = setsockopt(p->fds[1], IPPROTO_TCP, TCP_NODELAY, (char *) &nodelay,
sizeof(nodelay));
if (rc == SOCKET_ERROR) {
goto wsafail;
}
rc = connect(p->fds[1], (struct sockaddr *) &addr, sizeof(addr));
if (rc == SOCKET_ERROR) {
goto wsafail;
}
p->fds[0] = accept(listener, (struct sockaddr *) &addr, &addrlen);
if (p->fds[0] == INVALID_SOCKET) {
goto wsafail;
}
p->fdt.fd = p->fds[0];
closesocket(listener);
return 0;
wsafail:
sc_sock_pipe_set_err(p, "sc_sock_pipe_init() : %d ", WSAGetLastError());
return -1;
}
int sc_sock_pipe_term(struct sc_sock_pipe *p)
{
int rc = 0, rv;
if (p->fds[0] == SC_INVALID) {
return 0;
}
rv = closesocket(p->fds[0]);
if (rv != 0) {
rc = -1;
sc_sock_pipe_set_err(p, "closesocket() : err(%d) ",
WSAGetLastError());
}
rv = closesocket(p->fds[1]);
if (rv != 0) {
rc = -1;
sc_sock_pipe_set_err(p, "closesocket() : err(%d) ",
WSAGetLastError());
}
p->fds[0] = SC_INVALID;
p->fds[1] = SC_INVALID;
return rc;
}
int sc_sock_pipe_write(struct sc_sock_pipe *p, void *data, unsigned int len)
{
int rc;
rc = send(p->fds[1], data, (int)len, 0);
if (rc == SOCKET_ERROR) {
sc_sock_pipe_set_err(p, "pipe send() : err(%d) ",
WSAGetLastError());
}
return rc;
}
int sc_sock_pipe_read(struct sc_sock_pipe *p, void *data, unsigned int len)
{
int rc;