-
Notifications
You must be signed in to change notification settings - Fork 678
/
socket.c
2036 lines (1694 loc) · 51.2 KB
/
socket.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
#include "uwsgi.h"
extern struct uwsgi_server uwsgi;
static int connect_to_unix(char *, int, int);
static int connect_to_tcp(char *, int, int, int);
static int connect_to_udp(char *, int);
void uwsgi_socket_setup_protocol(struct uwsgi_socket *uwsgi_sock, char *protocol) {
if (!protocol) protocol = "uwsgi";
struct uwsgi_protocol *up = uwsgi.protocols;
while(up) {
if (!strcmp(protocol, up->name)) {
up->func(uwsgi_sock);
return;
}
up = up->next;
}
uwsgi_log("unable to find protocol %s\n", protocol);
exit(1);
}
static int uwsgi_socket_strcmp(char *sock1, char *sock2) {
size_t sock1_len = strlen(sock1);
size_t sock2_len = strlen(sock2);
if (!uwsgi_starts_with(sock1, sock1_len, "0.0.0.0:", 8)) {
sock1 += 7;
sock1_len = strlen(sock1);
}
if (!uwsgi_starts_with(sock2, sock2_len, "0.0.0.0:", 8)) {
sock2 += 7;
sock2_len = strlen(sock2);
}
return uwsgi_strncmp(sock1, sock1_len, sock2, sock2_len);
}
char *uwsgi_getsockname(int fd) {
socklen_t socket_type_len = sizeof(struct sockaddr_un);
union uwsgi_sockaddr usa;
union uwsgi_sockaddr_ptr gsa;
char computed_port[6];
char ipv4a[INET_ADDRSTRLEN + 1];
gsa.sa = (struct sockaddr *) &usa;
if (!getsockname(fd, gsa.sa, &socket_type_len)) {
if (gsa.sa->sa_family == AF_UNIX) {
// unnamed socket ?
if (socket_type_len == sizeof(sa_family_t)) return "";
if (usa.sa_un.sun_path[0] == 0) {
return uwsgi_concat2("@", usa.sa_un.sun_path + 1);
}
else {
return uwsgi_str(usa.sa_un.sun_path);
}
}
else {
memset(ipv4a, 0, INET_ADDRSTRLEN + 1);
memset(computed_port, 0, 6);
if (snprintf(computed_port, 6, "%d", ntohs(gsa.sa_in->sin_port)) > 0) {
if (inet_ntop(AF_INET, (const void *) &gsa.sa_in->sin_addr.s_addr, ipv4a, INET_ADDRSTRLEN)) {
if (!strcmp("0.0.0.0", ipv4a)) {
return uwsgi_concat2(":", computed_port);
}
else {
return uwsgi_concat3(ipv4a, ":", computed_port);
}
}
}
}
}
return NULL;
}
static int create_server_socket(int domain, int type) {
int serverfd = socket(domain, type, 0);
if (serverfd < 0) {
uwsgi_error("socket()");
uwsgi_nuclear_blast();
return -1;
}
if (uwsgi.close_on_exec2 && fcntl(serverfd, F_SETFD, FD_CLOEXEC) < 0)
uwsgi_error("fcntl()");
if (domain != AF_UNIX) {
int reuse = 1;
if (setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse, sizeof(int)) < 0) {
uwsgi_error("SO_REUSEADDR setsockopt()");
uwsgi_nuclear_blast();
return -1;
}
}
if (type == SOCK_STREAM || type == SOCK_DGRAM) {
if (uwsgi.so_sndbuf) {
socklen_t sndbuf = (socklen_t) uwsgi.so_sndbuf;
if (setsockopt(serverfd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(socklen_t)) < 0) {
uwsgi_error("SO_SNDBUF setsockopt()");
uwsgi_nuclear_blast();
return -1;
}
else {
uwsgi_log("successfully set SO_SNDBUF to %u for socket fd %d\n", sndbuf, serverfd);
}
}
if (uwsgi.so_rcvbuf) {
socklen_t rcvbuf = (socklen_t) uwsgi.so_rcvbuf;
if (setsockopt(serverfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(socklen_t)) < 0) {
uwsgi_error("SO_RCVBUF setsockopt()");
uwsgi_nuclear_blast();
return -1;
}
else {
uwsgi_log("successfully set SO_RCVBUF to %u for socket fd %d\n", rcvbuf, serverfd);
}
}
#ifdef __linux__
long somaxconn = uwsgi_num_from_file("/proc/sys/net/core/somaxconn", 1);
if (somaxconn > 0 && uwsgi.listen_queue > somaxconn) {
uwsgi_log("Listen queue size is greater than the system max net.core.somaxconn (%li).\n", somaxconn);
uwsgi_nuclear_blast();
return -1;
}
#endif
}
return serverfd;
}
int bind_to_unix_dgram(char *socket_name) {
int serverfd;
struct sockaddr_un *uws_addr;
socklen_t len;
serverfd = create_server_socket(AF_UNIX, SOCK_DGRAM);
if (serverfd < 0) return -1;
if (unlink(socket_name) != 0 && errno != ENOENT) {
uwsgi_error("error removing unix socket, unlink()");
}
uws_addr = uwsgi_calloc(sizeof(struct sockaddr_un));
uws_addr->sun_family = AF_UNIX;
memcpy(uws_addr->sun_path, socket_name, UMIN(strlen(socket_name), 102));
len = strlen(socket_name);
#ifdef __HAIKU__
if (bind(serverfd, (struct sockaddr *) uws_addr, sizeof(struct sockaddr_un))) {
#else
if (bind(serverfd, (struct sockaddr *) uws_addr, len + ((void *) uws_addr->sun_path - (void *) uws_addr)) != 0) {
#endif
uwsgi_error("bind()");
uwsgi_nuclear_blast();
return -1;
}
return serverfd;
}
int bind_to_unix(char *socket_name, int listen_queue, int chmod_socket, int abstract_socket) {
int serverfd;
struct sockaddr_un *uws_addr;
socklen_t len;
// leave 1 byte for abstract namespace (108 linux -> 104 bsd/mac)
if (strlen(socket_name) > 102) {
uwsgi_log("invalid socket name\n");
uwsgi_nuclear_blast();
return -1;
}
if (socket_name[0] == '@') {
abstract_socket = 1;
}
else if (strlen(socket_name) > 1 && socket_name[0] == '\\' && socket_name[1] == '0') {
abstract_socket = 1;
}
uws_addr = malloc(sizeof(struct sockaddr_un));
if (uws_addr == NULL) {
uwsgi_error("malloc()");
uwsgi_nuclear_blast();
return -1;
}
memset(uws_addr, 0, sizeof(struct sockaddr_un));
serverfd = create_server_socket(AF_UNIX, SOCK_STREAM);
if (serverfd < 0) {
free(uws_addr);
return -1;
}
if (abstract_socket == 0) {
if (unlink(socket_name) != 0 && errno != ENOENT) {
uwsgi_error("error removing unix socket, unlink()");
}
}
if (abstract_socket == 1) {
uwsgi_log("setting abstract socket mode (warning: only Linux supports this)\n");
}
uws_addr->sun_family = AF_UNIX;
if (socket_name[0] == '@') {
memcpy(uws_addr->sun_path + abstract_socket, socket_name + 1, UMIN(strlen(socket_name + 1), 101));
len = strlen(socket_name) + 1;
}
else if (strlen(socket_name) > 1 && socket_name[0] == '\\' && socket_name[1] == '0') {
memcpy(uws_addr->sun_path + abstract_socket, socket_name + 2, UMIN(strlen(socket_name + 2), 101));
len = strlen(socket_name + 1) + 1;
}
else if (abstract_socket) {
memcpy(uws_addr->sun_path + 1, socket_name, UMIN(strlen(socket_name), 101));
len = strlen(socket_name) + 1;
}
else {
memcpy(uws_addr->sun_path + abstract_socket, socket_name, UMIN(strlen(socket_name), 102));
len = strlen(socket_name);
}
#ifdef __HAIKU__
if (bind(serverfd, (struct sockaddr *) uws_addr, sizeof(struct sockaddr_un))) {
#else
if (bind(serverfd, (struct sockaddr *) uws_addr, len + ((void *) uws_addr->sun_path - (void *) uws_addr)) != 0) {
#endif
uwsgi_error("bind()");
uwsgi_nuclear_blast();
return -1;
}
if (listen(serverfd, listen_queue) != 0) {
uwsgi_error("listen()");
uwsgi_nuclear_blast();
return -1;
}
// chmod unix socket for lazy users
if (chmod_socket == 1 && abstract_socket == 0) {
if (uwsgi.chmod_socket_value) {
if (chmod(socket_name, uwsgi.chmod_socket_value) != 0) {
uwsgi_error("chmod()");
}
}
else {
uwsgi_log("chmod() socket to 666 for lazy and brave users\n");
if (chmod(socket_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) != 0) {
uwsgi_error("chmod()");
}
}
}
free(uws_addr);
return serverfd;
}
int bind_to_udp(char *socket_name, int multicast, int broadcast) {
int serverfd;
struct sockaddr_in uws_addr;
char *udp_port;
int bcast = 1;
struct ip_mreq mc;
udp_port = strchr(socket_name, ':');
if (udp_port == NULL) {
return -1;
}
udp_port[0] = 0;
if (socket_name[0] == 0 && multicast) {
uwsgi_log("invalid multicast address\n");
return -1;
}
memset(&uws_addr, 0, sizeof(struct sockaddr_in));
uws_addr.sin_family = AF_INET;
uws_addr.sin_port = htons(atoi(udp_port + 1));
if (!broadcast && !multicast) {
char quad[4];
char *first_part = strchr(socket_name, '.');
if (first_part && first_part - socket_name < 4) {
memset(quad, 0, 4);
memcpy(quad, socket_name, first_part - socket_name);
if (atoi(quad) >= 224 && atoi(quad) <= 239) {
multicast = 1;
}
}
if (!strcmp(socket_name, "255.255.255.255")) {
broadcast = 1;
}
}
if (broadcast) {
uws_addr.sin_addr.s_addr = INADDR_BROADCAST;
}
else if (socket_name[0] != 0) {
uws_addr.sin_addr.s_addr = inet_addr(socket_name);
}
else {
uws_addr.sin_addr.s_addr = INADDR_ANY;
}
serverfd = create_server_socket(AF_INET, SOCK_DGRAM);
if (serverfd < 0) return -1;
if (multicast) {
// if multicast is enabled remember to bind to INADDR_ANY
uws_addr.sin_addr.s_addr = INADDR_ANY;
mc.imr_multiaddr.s_addr = inet_addr(socket_name);
mc.imr_interface.s_addr = INADDR_ANY;
}
if (broadcast) {
if (setsockopt(serverfd, SOL_SOCKET, SO_BROADCAST, &bcast, sizeof(bcast))) {
perror("setsockopt");
close(serverfd);
return -1;
}
}
if (bind(serverfd, (struct sockaddr *) &uws_addr, sizeof(uws_addr)) != 0) {
uwsgi_error("bind()");
close(serverfd);
return -1;
}
if (multicast) {
uwsgi_log("[uwsgi-mcast] joining multicast group: %s:%d\n", socket_name, ntohs(uws_addr.sin_port));
if (setsockopt(serverfd, IPPROTO_IP, IP_MULTICAST_LOOP, &uwsgi.multicast_loop, sizeof(uwsgi.multicast_loop))) {
uwsgi_error("setsockopt()");
}
if (setsockopt(serverfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mc, sizeof(mc))) {
uwsgi_error("setsockopt()");
}
if (setsockopt(serverfd, IPPROTO_IP, IP_MULTICAST_TTL, &uwsgi.multicast_ttl, sizeof(uwsgi.multicast_ttl))) {
uwsgi_error("setsockopt()");
}
}
udp_port[0] = ':';
return serverfd;
}
static int uwsgi_connect_do(char *socket_name, int timeout, int async) {
char *tcp_port = strchr(socket_name, ':');
if (tcp_port) {
tcp_port[0] = 0;
tcp_port++;
return connect_to_tcp(socket_name, atoi(tcp_port), timeout, async);
}
return connect_to_unix(socket_name, timeout, async);
}
int uwsgi_connectn(char *socket_name, uint16_t len, int timeout, int async) {
char *zeroed_socket_name = uwsgi_concat2n(socket_name, len, "", 0);
int fd = uwsgi_connect_do(zeroed_socket_name, timeout, async);
free(zeroed_socket_name);
return fd;
}
int uwsgi_connect(char *socket_name, int timeout, int async) {
char *zeroed_socket_name = uwsgi_str(socket_name);
int fd = uwsgi_connect_do(zeroed_socket_name, timeout, async);
free(zeroed_socket_name);
return fd;
}
int uwsgi_connect_udp(char *socket_name) {
int fd = -1;
char *zeroed_socket_name = uwsgi_str(socket_name);
char *udp_port = strchr(zeroed_socket_name, ':');
if (!udp_port) goto end;
*udp_port = 0;
udp_port++;
fd = connect_to_udp(zeroed_socket_name, atoi(udp_port));
end:
free(zeroed_socket_name);
return fd;
}
static int connect_to_unix(char *socket_name, int timeout, int async) {
struct pollfd uwsgi_poll;
struct sockaddr_un uws_addr;
socklen_t un_size = sizeof(struct sockaddr_un);
memset(&uws_addr, 0, sizeof(struct sockaddr_un));
uws_addr.sun_family = AF_UNIX;
if (socket_name[0] == '@') {
un_size = sizeof(uws_addr.sun_family) + strlen(socket_name) + 1;
memcpy(uws_addr.sun_path + 1, socket_name + 1, UMIN(strlen(socket_name + 1), 101));
}
else if (strlen(socket_name) > 1 && socket_name[0] == '\\' && socket_name[1] == '0') {
un_size = sizeof(uws_addr.sun_family) + strlen(socket_name + 1) + 1;
memcpy(uws_addr.sun_path + 1, socket_name + 2, UMIN(strlen(socket_name + 2), 101));
}
else {
memcpy(uws_addr.sun_path, socket_name, UMIN(strlen(socket_name), 102));
}
#if defined(__linux__) && defined(SOCK_NONBLOCK) && !defined(OBSOLETE_LINUX_KERNEL)
uwsgi_poll.fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
#else
uwsgi_poll.fd = socket(AF_UNIX, SOCK_STREAM, 0);
#endif
if (uwsgi_poll.fd < 0) {
uwsgi_error("socket()");
return -1;
}
uwsgi_poll.events = POLLIN;
if (timed_connect(&uwsgi_poll, (const struct sockaddr *) &uws_addr, un_size, timeout, async)) {
// avoid error storm
//uwsgi_error("connect()");
close(uwsgi_poll.fd);
return -1;
}
return uwsgi_poll.fd;
}
static int connect_to_tcp(char *socket_name, int port, int timeout, int async) {
struct pollfd uwsgi_poll;
struct sockaddr_in uws_addr;
memset(&uws_addr, 0, sizeof(struct sockaddr_in));
uws_addr.sin_family = AF_INET;
uws_addr.sin_port = htons(port);
if (socket_name[0] == 0) {
uws_addr.sin_addr.s_addr = INADDR_ANY;
}
else {
uws_addr.sin_addr.s_addr = inet_addr(socket_name);
}
#if defined(__linux__) && defined(SOCK_NONBLOCK) && !defined(OBSOLETE_LINUX_KERNEL)
uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
#else
uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM, 0);
#endif
if (uwsgi_poll.fd < 0) {
uwsgi_error("connect_to_tcp()/socket()");
return -1;
}
uwsgi_poll.events = POLLIN;
if (timed_connect(&uwsgi_poll, (const struct sockaddr *) &uws_addr, sizeof(struct sockaddr_in), timeout, async)) {
//uwsgi_error("connect()");
close(uwsgi_poll.fd);
return -1;
}
return uwsgi_poll.fd;
}
static int connect_to_udp(char *socket_name, int port) {
struct sockaddr_in uws_addr;
memset(&uws_addr, 0, sizeof(struct sockaddr_in));
uws_addr.sin_family = AF_INET;
uws_addr.sin_port = htons(port);
if (socket_name[0] == 0) {
uws_addr.sin_addr.s_addr = INADDR_ANY;
}
else {
uws_addr.sin_addr.s_addr = inet_addr(socket_name);
}
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
uwsgi_error("connect_to_udp()/socket()");
return -1;
}
if (connect(fd, (const struct sockaddr *) &uws_addr, sizeof(struct sockaddr_in))) {
close(fd);
return -1;
}
return fd;
}
char *generate_socket_name(char *socket_name) {
char *asterisk = strchr(socket_name, '*');
char *tcp_port;
int i;
char *ptr = socket_name;
// ltrim spaces
for (i = 0; i < (int) strlen(socket_name); i++) {
if (isspace((int) socket_name[i])) {
ptr++;
}
else {
break;
}
}
socket_name = ptr;
if (socket_name[0] == 0) {
uwsgi_log("invalid/empty uwsgi socket name\n");
exit(1);
}
tcp_port = strchr(socket_name, ':');
if (!tcp_port)
return socket_name;
if (asterisk) {
#ifndef UWSGI_HAS_IFADDRS
uwsgi_log("your system does not support ifaddrs subsystem\n");
#else
char *new_socket;
#ifdef UWSGI_DEBUG
uwsgi_log("generate_socket_name(%s)\n", socket_name);
#endif
// get all the AF_INET addresses available
struct ifaddrs *ifap = NULL, *ifa, *ifaf;
if (getifaddrs(&ifap)) {
uwsgi_error("getifaddrs()");
uwsgi_nuclear_blast();
}
// here socket_name will be truncated
asterisk[0] = 0;
#ifdef UWSGI_DEBUG
uwsgi_log("asterisk found\n");
#endif
char new_addr[16];
struct sockaddr_in *sin;
ifa = ifap;
while (ifa) {
memset(new_addr, 0, 16);
if (!ifa->ifa_addr) goto next;
sin = (struct sockaddr_in *) ifa->ifa_addr;
if (inet_ntop(AF_INET, (void *) &sin->sin_addr.s_addr, new_addr, 16)) {
if (!strncmp(socket_name, new_addr, strlen(socket_name))) {
asterisk[0] = '*';
new_socket = uwsgi_concat3(new_addr, ":", tcp_port + 1);
uwsgi_log("[uwsgi-autoip] found %s for %s on interface %s\n", new_socket, socket_name, ifa->ifa_name);
freeifaddrs(ifap);
return new_socket;
}
}
next:
ifaf = ifa;
ifa = ifaf->ifa_next;
}
uwsgi_log("unable to find a valid socket address\n");
#endif
uwsgi_nuclear_blast();
}
return socket_name;
}
socklen_t socket_to_un_addr(char *socket_name, struct sockaddr_un * sun_addr) {
size_t len = strlen(socket_name);
if (len > 102) {
uwsgi_log("invalid UNIX socket address: %s\n", socket_name);
uwsgi_nuclear_blast();
}
memset(sun_addr, 0, sizeof(struct sockaddr_un));
sun_addr->sun_family = AF_UNIX;
// abstract socket
if (socket_name[0] == '@') {
memcpy(sun_addr->sun_path + 1, socket_name + 1, UMIN(len - 1, 101));
len = strlen(socket_name) + 1;
}
else if (len > 1 && socket_name[0] == '\\' && socket_name[1] == '0') {
memcpy(sun_addr->sun_path + 1, socket_name + 2, UMIN(len - 2, 101));
len = strlen(socket_name + 1) + 1;
}
else {
memcpy(sun_addr->sun_path, socket_name, UMIN(len, 102));
}
return sizeof(sun_addr->sun_family) + len;
}
socklen_t socket_to_in_addr(char *socket_name, char *port, int portn, struct sockaddr_in *sin_addr) {
memset(sin_addr, 0, sizeof(struct sockaddr_in));
sin_addr->sin_family = AF_INET;
if (port) {
*port = 0;
sin_addr->sin_port = htons(atoi(port + 1));
}
else {
sin_addr->sin_port = htons(portn);
}
if (socket_name[0] == 0) {
sin_addr->sin_addr.s_addr = INADDR_ANY;
}
else {
char *resolved = uwsgi_resolve_ip(socket_name);
if (resolved) {
sin_addr->sin_addr.s_addr = inet_addr(resolved);
}
else {
sin_addr->sin_addr.s_addr = inet_addr(socket_name);
}
}
if (port) {
*port = ':';
}
return sizeof(struct sockaddr_in);
}
int bind_to_tcp(char *socket_name, int listen_queue, char *tcp_port) {
int serverfd;
#ifdef AF_INET6
struct sockaddr_in6 uws_addr;
#else
struct sockaddr_in uws_addr;
#endif
int family = AF_INET;
socklen_t addr_len = sizeof(struct sockaddr_in);
#ifdef AF_INET6
if (socket_name[0] == '[' && tcp_port[-1] == ']') {
family = AF_INET6;
socket_to_in_addr6(socket_name, tcp_port, 0, &uws_addr);
addr_len = sizeof(struct sockaddr_in6);
}
else {
#endif
socket_to_in_addr(socket_name, tcp_port, 0, (struct sockaddr_in *) &uws_addr);
#ifdef AF_INET6
}
#endif
serverfd = create_server_socket(family, SOCK_STREAM);
if (serverfd < 0) return -1;
#ifdef __linux__
#ifndef IP_FREEBIND
#define IP_FREEBIND 15
#endif
if (uwsgi.freebind) {
if (setsockopt(serverfd, SOL_IP, IP_FREEBIND, (const void *) &uwsgi.freebind, sizeof(int)) < 0) {
uwsgi_error("IP_FREEBIND setsockopt()");
uwsgi_nuclear_blast();
return -1;
}
}
#endif
if (uwsgi.reuse_port) {
#ifdef SO_REUSEPORT
if (setsockopt(serverfd, SOL_SOCKET, SO_REUSEPORT, (const void *) &uwsgi.reuse_port, sizeof(int)) < 0) {
uwsgi_error("SO_REUSEPORT setsockopt()");
uwsgi_nuclear_blast();
return -1;
}
#else
uwsgi_log("!!! your system does not support SO_REUSEPORT !!!\n");
#endif
}
if (uwsgi.tcp_fast_open) {
#ifdef TCP_FASTOPEN
#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif
if (setsockopt(serverfd, SOL_TCP, TCP_FASTOPEN, (const void *) &uwsgi.tcp_fast_open, sizeof(int)) < 0) {
uwsgi_error("TCP_FASTOPEN setsockopt()");
}
else {
uwsgi_log("TCP_FASTOPEN enabled on %s\n", socket_name);
}
#else
uwsgi_log("!!! your system does not support TCP_FASTOPEN !!!\n");
#endif
}
if (uwsgi.so_send_timeout) {
struct timeval tv;
tv.tv_sec = uwsgi.so_send_timeout;
tv.tv_usec = 0;
if (setsockopt(serverfd, SOL_SOCKET, SO_SNDTIMEO, (const void *) &tv, sizeof(struct timeval)) < 0) {
uwsgi_error("SO_SNDTIMEO setsockopt()");
uwsgi_nuclear_blast();
return -1;
}
}
if (!uwsgi.no_defer_accept) {
#ifdef __linux__
if (setsockopt(serverfd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &uwsgi.socket_timeout, sizeof(int))) {
uwsgi_error("TCP_DEFER_ACCEPT setsockopt()");
}
// OSX has no SO_ACCEPTFILTER !!!
#elif defined(__freebsd__)
struct accept_filter_arg afa;
strcpy(afa.af_name, "dataready");
afa.af_arg[0] = 0;
if (setsockopt(serverfd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(struct accept_filter_arg))) {
uwsgi_error("SO_ACCEPTFILTER setsockopt()");
}
#endif
}
if (uwsgi.so_keepalive) {
if (setsockopt(serverfd, SOL_SOCKET, SO_KEEPALIVE, &uwsgi.so_keepalive, sizeof(int))) {
uwsgi_error("SO_KEEPALIVE setsockopt()");
}
}
if (bind(serverfd, (struct sockaddr *) &uws_addr, addr_len) != 0) {
if (errno == EADDRINUSE) {
uwsgi_log("probably another instance of uWSGI is running on the same address (%s).\n", socket_name);
}
uwsgi_error("bind()");
uwsgi_nuclear_blast();
return -1;
}
if (listen(serverfd, listen_queue) != 0) {
uwsgi_error("listen()");
uwsgi_nuclear_blast();
return -1;
}
if (tcp_port)
tcp_port[0] = ':';
return serverfd;
}
// set non-blocking socket
void uwsgi_socket_nb(int fd) {
int arg;
arg = fcntl(fd, F_GETFL, NULL);
if (arg < 0) {
uwsgi_error("fcntl()");
return;
}
arg |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, arg) < 0) {
uwsgi_error("fcntl()");
return;
}
}
// set blocking socket
void uwsgi_socket_b(int fd) {
int arg;
arg = fcntl(fd, F_GETFL, NULL);
if (arg < 0) {
uwsgi_error("fcntl()");
return;
}
arg &= (~O_NONBLOCK);
if (fcntl(fd, F_SETFL, arg) < 0) {
uwsgi_error("fcntl()");
return;
}
}
int timed_connect(struct pollfd *fdpoll, const struct sockaddr *addr, int addr_size, int timeout, int async) {
int ret;
int soopt = 0;
socklen_t solen = sizeof(int);
int cnt;
/* set non-blocking socket */
#if defined(__linux__) && defined(SOCK_NONBLOCK) && !defined(OBSOLETE_LINUX_KERNEL)
// hmm, nothing to do, as we are already non-blocking
#else
int arg = fcntl(fdpoll->fd, F_GETFL, NULL);
if (arg < 0) {
uwsgi_error("fcntl()");
return -1;
}
arg |= O_NONBLOCK;
if (fcntl(fdpoll->fd, F_SETFL, arg) < 0) {
uwsgi_error("fcntl()");
return -1;
}
#endif
#ifdef MSG_FASTOPEN
if (addr->sa_family == AF_INET && uwsgi.tcp_fast_open_client) {
ret = sendto(fdpoll->fd, "", 0, MSG_FASTOPEN, addr, addr_size);
}
else {
#endif
ret = connect(fdpoll->fd, addr, addr_size);
#ifdef MSG_FASTOPEN
}
#endif
if (async) {
if (ret < 0 && errno != EINPROGRESS) {
return -1;
}
return 0;
}
#if defined(__linux__) && defined(SOCK_NONBLOCK) && !defined(OBSOLETE_LINUX_KERNEL)
uwsgi_socket_b(fdpoll->fd);
#else
/* re-set blocking socket */
arg &= (~O_NONBLOCK);
if (fcntl(fdpoll->fd, F_SETFL, arg) < 0) {
uwsgi_error("fcntl()");
return -1;
}
#endif
if (ret < 0) {
/* check what happened */
// in progress ?
if (errno == EINPROGRESS) {
if (timeout < 1)
timeout = 3;
fdpoll->events = POLLOUT;
cnt = poll(fdpoll, 1, timeout * 1000);
/* check for errors */
if (cnt < 0 && errno != EINTR) {
uwsgi_error("poll()");
return -1;
}
/* something happened on the socket ... */
else if (cnt > 0) {
if (getsockopt(fdpoll->fd, SOL_SOCKET, SO_ERROR, (void *) (&soopt), &solen) < 0) {
uwsgi_error("getsockopt()");
return -1;
}
/* is something bad ? */
if (soopt) {
return -1;
}
}
/* timeout */
else {
return -1;
}
}
else {
return -1;
}
}
return 0;
}
int uwsgi_count_sockets(struct uwsgi_socket *uwsgi_sock) {
int count = 0;
while (uwsgi_sock) {
count++;
uwsgi_sock = uwsgi_sock->next;
}
return count;
}
int uwsgi_get_socket_num(struct uwsgi_socket *uwsgi_sock) {
int count = 0;
struct uwsgi_socket *current_sock = uwsgi.sockets;
while (current_sock) {
if (uwsgi_sock == current_sock) {
return count;
}
count++;
current_sock = current_sock->next;
}
return -1;
}
int uwsgi_get_shared_socket_num(struct uwsgi_socket *uwsgi_sock) {
int count = 0;
struct uwsgi_socket *current_sock = uwsgi.shared_sockets;
while (current_sock) {
if (uwsgi_sock == current_sock) {
return count;
}
count++;
current_sock = current_sock->next;
}
return -1;
}
struct uwsgi_socket *uwsgi_new_shared_socket(char *name) {
struct uwsgi_socket *uwsgi_sock = uwsgi.shared_sockets, *old_uwsgi_sock;
if (!uwsgi_sock) {
uwsgi.shared_sockets = uwsgi_malloc(sizeof(struct uwsgi_socket));
uwsgi_sock = uwsgi.shared_sockets;
}
else {
while (uwsgi_sock) {
old_uwsgi_sock = uwsgi_sock;
uwsgi_sock = uwsgi_sock->next;
}
uwsgi_sock = uwsgi_malloc(sizeof(struct uwsgi_socket));
old_uwsgi_sock->next = uwsgi_sock;
}
memset(uwsgi_sock, 0, sizeof(struct uwsgi_socket));
uwsgi_sock->name = name;
uwsgi_sock->fd = -1;
return uwsgi_sock;
}
struct uwsgi_socket *uwsgi_new_socket(char *name) {
struct uwsgi_socket *uwsgi_sock = uwsgi.sockets, *old_uwsgi_sock;
struct sockaddr_in sin;
socklen_t socket_type_len;
if (!uwsgi_sock) {
uwsgi.sockets = uwsgi_malloc(sizeof(struct uwsgi_socket));