-
Notifications
You must be signed in to change notification settings - Fork 909
/
tcp_main.c
5379 lines (5048 loc) · 157 KB
/
tcp_main.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
/*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/** Kamailio core: tcp main/dispatcher and tcp send functions.
* @file tcp_main.c
* @ingroup core
* Module: @ref core
*/
#ifdef USE_TCP
#define HANDLE_IO_INLINE
#include "io_wait.h" /* include first to make sure the needed features are
turned on (e.g. _GNU_SOURCE for POLLRDHUP) */
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#ifdef HAVE_FILIO_H
#include <sys/filio.h> /* needed on solaris 2.x for FIONREAD */
#elif defined __OS_solaris
#define BSD_COMP /* needed on older solaris for FIONREAD */
#endif /* HAVE_FILIO_H / __OS_solaris */
#include <sys/ioctl.h> /* ioctl() used on write error */
#include <arpa/inet.h> /* for inet_pton() */
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <sys/uio.h> /* writev*/
#include <netdb.h>
#include <stdlib.h> /*exit() */
#include <stdint.h> /* UINT32_MAX */
#include <unistd.h>
#include <errno.h>
#include <string.h>
#ifdef HAVE_SELECT
#include <sys/select.h>
#endif
#include <poll.h>
#include "ip_addr.h"
#include "pass_fd.h"
#include "tcp_conn.h"
#include "globals.h"
#include "pt.h"
#include "locking.h"
#include "mem/mem.h"
#include "mem/shm_mem.h"
#include "timer.h"
#include "sr_module.h"
#include "tcp_server.h"
#include "tcp_init.h"
#include "tcp_int_send.h"
#include "tcp_stats.h"
#include "tcp_ev.h"
#include "tsend.h"
#include "events.h"
#include "timer_ticks.h"
#include "local_timer.h"
#ifdef CORE_TLS
#include "tls/tls_server.h"
#define tls_loaded() 1
#else
#include "tls_hooks_init.h"
#include "tls_hooks.h"
#endif /* CORE_TLS*/
#ifdef USE_DST_BLOCKLIST
#include "dst_blocklist.h"
#endif /* USE_DST_BLOCKLIST */
#include "tcp_info.h"
#include "tcp_options.h"
#include "ut.h"
#include "events.h"
#include "cfg/cfg_struct.h"
#include <fcntl.h> /* must be included after io_wait.h if SIGIO_RT is used */
#ifdef NO_MSG_DONTWAIT
#ifndef MSG_DONTWAIT
/* should work inside tcp_main */
#define MSG_DONTWAIT 0
#endif
#endif /*NO_MSG_DONTWAIT */
#define TCP_PASS_NEW_CONNECTION_ON_DATA /* don't pass a new connection
immediately to a child, wait for
some data on it first */
#define TCP_LISTEN_BACKLOG 1024
#define SEND_FD_QUEUE /* queue send fd requests on EAGAIN, instead of sending
them immediately */
#define TCP_CHILD_NON_BLOCKING
#ifdef SEND_FD_QUEUE
#ifndef TCP_CHILD_NON_BLOCKING
#define TCP_CHILD_NON_BLOCKING
#endif
#define MAX_SEND_FD_QUEUE_SIZE tcp_main_max_fd_no
#define SEND_FD_QUEUE_SIZE 128 /* initial size */
#define SEND_FD_QUEUE_TIMEOUT MS_TO_TICKS(2000) /* 2 s */
#endif
/* minimum interval local_timer_run() is allowed to run, in ticks */
#define TCPCONN_TIMEOUT_MIN_RUN 1 /* once per tick */
#define TCPCONN_WAIT_TIMEOUT 1 /* 1 tick */
#ifdef TCP_ASYNC
static unsigned int *tcp_total_wq = 0;
#endif
enum fd_types
{
F_NONE,
F_SOCKINFO /* a tcp_listen fd */,
F_TCPCONN,
F_TCPCHILD,
F_PROC
};
#ifdef TCP_FD_CACHE
#define TCP_FD_CACHE_SIZE 8
struct fd_cache_entry
{
struct tcp_connection *con;
int id;
int fd;
};
static struct fd_cache_entry fd_cache[TCP_FD_CACHE_SIZE];
#endif /* TCP_FD_CACHE */
static int is_tcp_main = 0;
enum poll_types tcp_poll_method = 0; /* by default choose the best method */
int tcp_main_max_fd_no = 0;
int tcp_max_connections = DEFAULT_TCP_MAX_CONNECTIONS;
int tls_max_connections = DEFAULT_TLS_MAX_CONNECTIONS;
int tcp_accept_unique = 0;
int tcp_connection_match = TCPCONN_MATCH_DEFAULT;
static union sockaddr_union tcp_source_ipv4_addr; /* saved bind/srv v4 addr. */
static union sockaddr_union *tcp_source_ipv4 = 0;
static union sockaddr_union tcp_source_ipv6_addr; /* saved bind/src v6 addr. */
static union sockaddr_union *tcp_source_ipv6 = 0;
static int *tcp_connections_no = 0; /* current tcp (+tls) open connections */
static int *tls_connections_no = 0; /* current tls open connections */
/* connection hash table (after ip&port) , includes also aliases */
struct tcp_conn_alias **tcpconn_aliases_hash = 0;
/* connection hash table (after connection id) */
struct tcp_connection **tcpconn_id_hash = 0;
gen_lock_t *tcpconn_lock = 0;
struct tcp_child *tcp_children = 0;
static int *connection_id = 0; /* unique for each connection, used for
quickly finding the corresponding connection
for a reply */
int unix_tcp_sock;
static int tcp_proto_no = -1; /* tcp protocol number as returned by
getprotobyname */
static io_wait_h io_h;
static struct local_timer tcp_main_ltimer;
static ticks_t tcp_main_prev_ticks;
/* tell if there are tcp workers that should handle only specific socket
* - used to optimize the search of least loaded worker for a tcp socket
* - 0 - no workers per tcp sockets have been set
* - 1 + generic_workers - when there are workers per tcp sockets
*/
static int tcp_sockets_gworkers = 0;
static ticks_t tcpconn_main_timeout(ticks_t, struct timer_ln *, void *);
inline static int _tcpconn_add_alias_unsafe(struct tcp_connection *c, int port,
struct ip_addr *l_ip, int l_port, int flags);
/* sets source address used when opening new sockets and no source is specified
* (by default the address is choosen by the kernel)
* Should be used only on init.
* returns -1 on error */
int tcp_set_src_addr(struct ip_addr *ip)
{
switch(ip->af) {
case AF_INET:
ip_addr2su(&tcp_source_ipv4_addr, ip, 0);
tcp_source_ipv4 = &tcp_source_ipv4_addr;
break;
case AF_INET6:
ip_addr2su(&tcp_source_ipv6_addr, ip, 0);
tcp_source_ipv6 = &tcp_source_ipv6_addr;
break;
default:
return -1;
}
return 0;
}
static inline int init_sock_keepalive(int s)
{
int optval;
#ifdef HAVE_SO_KEEPALIVE
if(cfg_get(tcp, tcp_cfg, keepalive)) {
optval = 1;
if(setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval))
< 0) {
LM_WARN("failed to enable SO_KEEPALIVE: %s\n", strerror(errno));
return -1;
}
}
#endif
#ifdef HAVE_TCP_KEEPINTVL
if((optval = cfg_get(tcp, tcp_cfg, keepintvl))) {
if(setsockopt(s, IPPROTO_TCP, TCP_KEEPINTVL, &optval, sizeof(optval))
< 0) {
LM_WARN("failed to set keepalive probes interval: %s\n",
strerror(errno));
}
}
#endif
#ifdef HAVE_TCP_KEEPIDLE
if((optval = cfg_get(tcp, tcp_cfg, keepidle))) {
if(setsockopt(s, IPPROTO_TCP, TCP_KEEPIDLE, &optval, sizeof(optval))
< 0) {
LM_WARN("failed to set keepalive idle interval: %s\n",
strerror(errno));
}
}
#endif
#ifdef HAVE_TCP_KEEPCNT
if((optval = cfg_get(tcp, tcp_cfg, keepcnt))) {
if(setsockopt(s, IPPROTO_TCP, TCP_KEEPCNT, &optval, sizeof(optval))
< 0) {
LM_WARN("failed to set maximum keepalive count: %s\n",
strerror(errno));
}
}
#endif
return 0;
}
/* set all socket/fd options for new sockets (e.g. before connect):
* disable nagle, tos lowdelay, reuseaddr, non-blocking
*
* return -1 on error */
static int init_sock_opt(int s, int af)
{
int flags;
int optval;
#ifdef DISABLE_NAGLE
flags = 1;
if((tcp_proto_no != -1)
&& (setsockopt(s, tcp_proto_no, TCP_NODELAY, &flags, sizeof(flags))
< 0)) {
LM_WARN("could not disable Nagle: %s\n", strerror(errno));
}
#endif
/* tos*/
optval = tos;
if(af == AF_INET) {
if(setsockopt(s, IPPROTO_IP, IP_TOS, (void *)&optval, sizeof(optval))
== -1) {
LM_WARN("setsockopt tos: %s\n", strerror(errno));
/* continue since this is not critical */
}
} else if(af == AF_INET6) {
if(setsockopt(s, IPPROTO_IPV6, IPV6_TCLASS, (void *)&optval,
sizeof(optval))
== -1) {
LM_WARN("setsockopt v6 tos: %s\n", strerror(errno));
/* continue since this is not critical */
}
}
#if !defined(TCP_DONT_REUSEADDR)
optval = 1;
if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&optval, sizeof(optval))
== -1) {
LM_ERR("setsockopt SO_REUSEADDR %s\n", strerror(errno));
/* continue, not critical */
}
#endif /* !TCP_DONT_REUSEADDR */
#ifdef SO_REUSEPORT
if((optval = cfg_get(tcp, tcp_cfg, reuse_port))) {
if(setsockopt(
s, SOL_SOCKET, SO_REUSEPORT, (void *)&optval, sizeof(optval))
== -1) {
LM_ERR("setsockopt %s\n", strerror(errno));
}
}
#endif
#ifdef HAVE_TCP_SYNCNT
if((optval = cfg_get(tcp, tcp_cfg, syncnt))) {
if(setsockopt(s, IPPROTO_TCP, TCP_SYNCNT, &optval, sizeof(optval))
< 0) {
LM_WARN("failed to set maximum SYN retr. count: %s\n",
strerror(errno));
}
}
#endif
#ifdef HAVE_TCP_LINGER2
if((optval = cfg_get(tcp, tcp_cfg, linger2))) {
if(setsockopt(s, IPPROTO_TCP, TCP_LINGER2, &optval, sizeof(optval))
< 0) {
LM_WARN("failed to set maximum LINGER2 timeout: %s\n",
strerror(errno));
}
}
#endif
#ifdef HAVE_TCP_QUICKACK
if(cfg_get(tcp, tcp_cfg, delayed_ack)) {
optval = 0; /* reset quick ack => delayed ack */
if(setsockopt(s, IPPROTO_TCP, TCP_QUICKACK, &optval, sizeof(optval))
< 0) {
LM_WARN("failed to reset TCP_QUICKACK: %s\n", strerror(errno));
}
}
#endif /* HAVE_TCP_QUICKACK */
init_sock_keepalive(s);
/* non-blocking */
flags = fcntl(s, F_GETFL);
if(flags == -1) {
LM_ERR("fnctl failed: (%d) %s\n", errno, strerror(errno));
goto error;
}
if(fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1) {
LM_ERR("fcntl: set non-blocking failed: (%d) %s\n", errno,
strerror(errno));
goto error;
}
return 0;
error:
return -1;
}
/* set all socket/fd options for "accepted" sockets
* only nonblocking is set since the rest is inherited from the
* "parent" (listening) socket
* Note: setting O_NONBLOCK is required on linux but it's not needed on
* BSD and possibly solaris (where the flag is inherited from the
* parent socket). However since there is no standard document
* requiring a specific behaviour in this case it's safer to always set
* it (at least for now) --andrei
* TODO: check on which OSes O_NONBLOCK is inherited and make this
* function a nop.
*
* return -1 on error */
static int init_sock_opt_accept(int s)
{
int flags;
/* non-blocking */
flags = fcntl(s, F_GETFL);
if(flags == -1) {
LM_ERR("fnctl failed: (%d) %s\n", errno, strerror(errno));
goto error;
}
if(fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1) {
LM_ERR("fcntl: set non-blocking failed: (%d) %s\n", errno,
strerror(errno));
goto error;
}
return 0;
error:
return -1;
}
/** close a socket, handling errno.
* On EINTR, repeat the close().
* Filter expected errors (return success if close() failed because
* EPIPE, ECONNRST a.s.o). Note that this happens on *BSDs (on linux close()
* does not fail for socket level errors).
* @param s - open valid socket.
* @return - 0 on success, < 0 on error (whatever close() returns). On error
* errno is set.
*/
static int tcp_safe_close(int s)
{
int ret;
if(s < 0)
return 0;
retry:
if(unlikely((ret = close(s)) < 0)) {
switch(errno) {
case EINTR:
goto retry;
case EPIPE:
case ENOTCONN:
case ECONNRESET:
case ECONNREFUSED:
case ENETUNREACH:
case EHOSTUNREACH:
/* on *BSD we really get these errors at close() time
=> ignore them */
ret = 0;
break;
default:
break;
}
}
return ret;
}
/* blocking connect on a non-blocking fd; it will timeout after
* tcp_connect_timeout
* if BLOCKING_USE_SELECT and HAVE_SELECT are defined it will internally
* use select() instead of poll (bad if fd > FD_SET_SIZE, poll is preferred)
*/
static int tcp_blocking_connect(int fd, int type, snd_flags_t *send_flags,
const struct sockaddr *servaddr, socklen_t addrlen)
{
int n;
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
fd_set sel_set;
fd_set orig_set;
struct timeval timeout;
#else
struct pollfd pf;
#endif
int elapsed;
int to;
int ticks;
int err;
unsigned int err_len;
int poll_err;
poll_err = 0;
to = cfg_get(tcp, tcp_cfg, connect_timeout_s);
ticks = get_ticks();
again:
n = connect(fd, servaddr, addrlen);
if(n == -1) {
if(errno == EINTR) {
elapsed = (get_ticks() - ticks) * TIMER_TICK;
if(elapsed < to)
goto again;
else
goto error_timeout;
}
if(errno != EINPROGRESS && errno != EALREADY) {
goto error_errno;
}
} else
goto end;
/* poll/select loop */
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
FD_ZERO(&orig_set);
FD_SET(fd, &orig_set);
#else
pf.fd = fd;
pf.events = POLLOUT;
#endif
while(1) {
elapsed = (get_ticks() - ticks) * TIMER_TICK;
if(elapsed >= to)
goto error_timeout;
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
sel_set = orig_set;
timeout.tv_sec = to - elapsed;
timeout.tv_usec = 0;
n = select(fd + 1, 0, &sel_set, 0, &timeout);
#else
n = poll(&pf, 1, (to - elapsed) * 1000);
#endif
if(n < 0) {
if(errno == EINTR)
continue;
LM_ERR("%s: poll/select failed: (%d) %s\n",
su2a((union sockaddr_union *)servaddr, addrlen), errno,
strerror(errno));
goto error;
} else if(n == 0) /* timeout */
continue;
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
if(FD_ISSET(fd, &sel_set))
#else
if(pf.revents & (POLLERR | POLLHUP | POLLNVAL)) {
LM_ERR("%s: poll error: flags %x\n",
su2a((union sockaddr_union *)servaddr, addrlen),
pf.revents);
poll_err = 1;
}
#endif
{
err_len = sizeof(err);
getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
if((err == 0) && (poll_err == 0))
goto end;
if(err != EINPROGRESS && err != EALREADY) {
LM_ERR("%s: SO_ERROR (%d) %s\n",
su2a((union sockaddr_union *)servaddr, addrlen), err,
strerror(err));
errno = err;
goto error_errno;
}
}
}
error_errno:
switch(errno) {
case ENETUNREACH:
case EHOSTUNREACH:
#ifdef USE_DST_BLOCKLIST
dst_blocklist_su(BLST_ERR_CONNECT, type,
(union sockaddr_union *)servaddr, send_flags, 0);
#endif /* USE_DST_BLOCKLIST */
TCP_EV_CONNECT_UNREACHABLE(
errno, 0, 0, (union sockaddr_union *)servaddr, type);
break;
case ETIMEDOUT:
#ifdef USE_DST_BLOCKLIST
dst_blocklist_su(BLST_ERR_CONNECT, type,
(union sockaddr_union *)servaddr, send_flags, 0);
#endif /* USE_DST_BLOCKLIST */
TCP_EV_CONNECT_TIMEOUT(
errno, 0, 0, (union sockaddr_union *)servaddr, type);
break;
case ECONNREFUSED:
case ECONNRESET:
#ifdef USE_DST_BLOCKLIST
dst_blocklist_su(BLST_ERR_CONNECT, type,
(union sockaddr_union *)servaddr, send_flags, 0);
#endif /* USE_DST_BLOCKLIST */
TCP_EV_CONNECT_RST(
errno, 0, 0, (union sockaddr_union *)servaddr, type);
break;
case EAGAIN: /* not posix, but supported on linux and bsd */
TCP_EV_CONNECT_NO_MORE_PORTS(
errno, 0, 0, (union sockaddr_union *)servaddr, type);
break;
default:
TCP_EV_CONNECT_ERR(
errno, 0, 0, (union sockaddr_union *)servaddr, type);
}
LM_ERR("%s: (%d) %s\n", su2a((union sockaddr_union *)servaddr, addrlen),
errno, strerror(errno));
goto error;
error_timeout:
/* timeout */
#ifdef USE_DST_BLOCKLIST
dst_blocklist_su(BLST_ERR_CONNECT, type, (union sockaddr_union *)servaddr,
send_flags, 0);
#endif /* USE_DST_BLOCKLIST */
TCP_EV_CONNECT_TIMEOUT(0, 0, 0, (union sockaddr_union *)servaddr, type);
LM_ERR("%s: timeout %d s elapsed from %d s\n",
su2a((union sockaddr_union *)servaddr, addrlen), elapsed,
cfg_get(tcp, tcp_cfg, connect_timeout_s));
error:
TCP_STATS_CONNECT_FAILED();
return -1;
end:
return 0;
}
#ifdef TCP_ASYNC
/* unsafe version */
#define _wbufq_empty(con) ((con)->wbuf_q.first == 0)
/* unsafe version */
#define _wbufq_non_empty(con) ((con)->wbuf_q.first != 0)
/* unsafe version, call while holding the connection write lock */
inline static int _wbufq_add(
struct tcp_connection *c, const char *data, unsigned int size)
{
struct tcp_wbuffer_queue *q;
struct tcp_wbuffer *wb;
unsigned int last_free;
unsigned int wb_size;
unsigned int crt_size;
ticks_t t;
q = &c->wbuf_q;
t = get_ticks_raw();
if(unlikely(((q->queued + size) > cfg_get(tcp, tcp_cfg, tcpconn_wq_max))
|| ((*tcp_total_wq + size) > cfg_get(tcp, tcp_cfg, tcp_wq_max))
|| (q->first && TICKS_LT(q->wr_timeout, t)))) {
LM_ERR("(%u bytes): write queue full or timeout "
" (%u, total %u, last write %d s ago)\n",
size, q->queued, *tcp_total_wq,
TICKS_TO_S(t
- (q->wr_timeout
- cfg_get(tcp, tcp_cfg, send_timeout))));
if(q->first && TICKS_LT(q->wr_timeout, t)) {
if(unlikely(c->state == S_CONN_CONNECT)) {
#ifdef USE_DST_BLOCKLIST
(void)dst_blocklist_su(BLST_ERR_CONNECT, c->rcv.proto,
&c->rcv.src_su, &c->send_flags, 0);
#endif /* USE_DST_BLOCKLIST */
TCP_EV_CONNECT_TIMEOUT(0, TCP_LADDR(c), TCP_LPORT(c),
TCP_PSU(c), TCP_PROTO(c));
TCP_STATS_CONNECT_FAILED();
} else {
#ifdef USE_DST_BLOCKLIST
(void)dst_blocklist_su(BLST_ERR_SEND, c->rcv.proto,
&c->rcv.src_su, &c->send_flags, 0);
#endif /* USE_DST_BLOCKLIST */
TCP_EV_SEND_TIMEOUT(0, &c->rcv);
TCP_STATS_SEND_TIMEOUT();
}
} else {
/* if it's not a timeout => queue full */
TCP_EV_SENDQ_FULL(0, &c->rcv);
TCP_STATS_SENDQ_FULL();
}
goto error;
}
if(unlikely(q->last == 0)) {
wb_size = MAX_unsigned(cfg_get(tcp, tcp_cfg, wq_blk_size), size);
wb = shm_malloc(sizeof(*wb) + wb_size - 1);
if(unlikely(wb == 0)) {
SHM_MEM_ERROR;
goto error;
}
wb->b_size = wb_size;
wb->next = 0;
q->last = wb;
q->first = wb;
q->last_used = 0;
q->offset = 0;
q->wr_timeout = get_ticks_raw()
+ ((c->state == S_CONN_CONNECT)
? S_TO_TICKS(cfg_get(tcp, tcp_cfg,
connect_timeout_s))
: cfg_get(tcp, tcp_cfg, send_timeout));
} else {
wb = q->last;
}
while(size) {
last_free = wb->b_size - q->last_used;
if(last_free == 0) {
wb_size = MAX_unsigned(cfg_get(tcp, tcp_cfg, wq_blk_size), size);
wb = shm_malloc(sizeof(*wb) + wb_size - 1);
if(unlikely(wb == 0)) {
SHM_MEM_ERROR;
goto error;
}
wb->b_size = wb_size;
wb->next = 0;
q->last->next = wb;
q->last = wb;
q->last_used = 0;
last_free = wb->b_size;
}
crt_size = MIN_unsigned(last_free, size);
memcpy(wb->buf + q->last_used, data, crt_size);
q->last_used += crt_size;
size -= crt_size;
data += crt_size;
q->queued += crt_size;
atomic_add_int((int *)tcp_total_wq, crt_size);
}
return 0;
error:
return -1;
}
/* unsafe version, call while holding the connection write lock
* inserts data at the beginning, it ignores the max queue size checks and
* the timeout (use sparingly)
* Note: it should never be called on a write buffer after wbufq_run() */
inline static int _wbufq_insert(
struct tcp_connection *c, const char *data, unsigned int size)
{
struct tcp_wbuffer_queue *q;
struct tcp_wbuffer *wb;
q = &c->wbuf_q;
if(likely(q->first == 0)) /* if empty, use wbufq_add */
return _wbufq_add(c, data, size);
if(unlikely((*tcp_total_wq + size) > cfg_get(tcp, tcp_cfg, tcp_wq_max))) {
LM_ERR("(%d bytes): write queue full"
" (%d, total %d, last write %d s ago)\n",
size, q->queued, *tcp_total_wq,
TICKS_TO_S(get_ticks_raw() - q->wr_timeout
- cfg_get(tcp, tcp_cfg, send_timeout)));
goto error;
}
if(unlikely(q->offset)) {
LM_CRIT("non-null offset %d (bad call, should"
"never be called after the wbufq_run())\n",
q->offset);
goto error;
}
if((q->first == q->last) && ((q->last->b_size - q->last_used) >= size)) {
/* one block with enough space in it for size bytes */
memmove(q->first->buf + size, q->first->buf, q->last_used);
memcpy(q->first->buf, data, size);
q->last_used += size;
} else {
/* create a size bytes block directly */
wb = shm_malloc(sizeof(*wb) + size - 1);
if(unlikely(wb == 0)) {
SHM_MEM_ERROR;
goto error;
}
wb->b_size = size;
/* insert it */
wb->next = q->first;
q->first = wb;
memcpy(wb->buf, data, size);
}
q->queued += size;
atomic_add_int((int *)tcp_total_wq, size);
return 0;
error:
return -1;
}
/* unsafe version, call while holding the connection write lock */
inline static void _wbufq_destroy(struct tcp_wbuffer_queue *q)
{
struct tcp_wbuffer *wb;
struct tcp_wbuffer *next_wb;
int unqueued;
unqueued = 0;
if(likely(q->first)) {
wb = q->first;
do {
next_wb = wb->next;
unqueued += (wb == q->last) ? q->last_used : wb->b_size;
if(wb == q->first)
unqueued -= q->offset;
shm_free(wb);
wb = next_wb;
} while(wb);
}
memset(q, 0, sizeof(*q));
atomic_add_int((int *)tcp_total_wq, -unqueued);
}
/* tries to empty the queue (safe version, c->write_lock must not be hold)
* returns -1 on error, bytes written on success (>=0)
* if the whole queue is emptied => sets *empty*/
inline static int wbufq_run(int fd, struct tcp_connection *c, int *empty)
{
struct tcp_wbuffer_queue *q;
struct tcp_wbuffer *wb;
int n;
int ret;
int block_size;
char *buf;
*empty = 0;
ret = 0;
lock_get(&c->write_lock);
q = &c->wbuf_q;
while(q->first) {
block_size = ((q->first == q->last) ? q->last_used : q->first->b_size)
- q->offset;
buf = q->first->buf + q->offset;
n = _tcpconn_write_nb(fd, c, buf, block_size);
if(likely(n > 0)) {
ret += n;
if(likely(n == block_size)) {
wb = q->first;
q->first = q->first->next;
shm_free(wb);
q->offset = 0;
q->queued -= block_size;
atomic_add_int((int *)tcp_total_wq, -block_size);
} else {
q->offset += n;
q->queued -= n;
atomic_add_int((int *)tcp_total_wq, -n);
break;
}
} else {
if(n < 0) {
/* EINTR is handled inside _tcpconn_write_nb */
if(!(errno == EAGAIN || errno == EWOULDBLOCK)) {
if(unlikely(c->state == S_CONN_CONNECT)) {
switch(errno) {
case ENETUNREACH:
case EHOSTUNREACH: /* not posix for send() */
#ifdef USE_DST_BLOCKLIST
dst_blocklist_su(BLST_ERR_CONNECT, c->rcv.proto,
&c->rcv.src_su, &c->send_flags, 0);
#endif /* USE_DST_BLOCKLIST */
TCP_EV_CONNECT_UNREACHABLE(errno, TCP_LADDR(c),
TCP_LPORT(c), TCP_PSU(c), TCP_PROTO(c));
break;
case ECONNREFUSED:
case ECONNRESET:
#ifdef USE_DST_BLOCKLIST
dst_blocklist_su(BLST_ERR_CONNECT, c->rcv.proto,
&c->rcv.src_su, &c->send_flags, 0);
#endif /* USE_DST_BLOCKLIST */
TCP_EV_CONNECT_RST(0, TCP_LADDR(c),
TCP_LPORT(c), TCP_PSU(c), TCP_PROTO(c));
break;
default:
TCP_EV_CONNECT_ERR(errno, TCP_LADDR(c),
TCP_LPORT(c), TCP_PSU(c), TCP_PROTO(c));
}
TCP_STATS_CONNECT_FAILED();
} else {
switch(errno) {
case ECONNREFUSED:
case ECONNRESET:
TCP_STATS_CON_RESET();
/* no break */
case ENETUNREACH:
case EHOSTUNREACH: /* not posix for send() */
#ifdef USE_DST_BLOCKLIST
dst_blocklist_su(BLST_ERR_SEND, c->rcv.proto,
&c->rcv.src_su, &c->send_flags, 0);
#endif /* USE_DST_BLOCKLIST */
break;
}
}
ret = -1;
LM_ERR("%s [%d]\n", strerror(errno), errno);
}
}
break;
}
}
if(likely(q->first == 0)) {
q->last = 0;
q->last_used = 0;
q->offset = 0;
*empty = 1;
}
lock_release(&c->write_lock);
if(likely(ret > 0)) {
q->wr_timeout = get_ticks_raw() + cfg_get(tcp, tcp_cfg, send_timeout);
if(unlikely(c->state == S_CONN_CONNECT || c->state == S_CONN_ACCEPT)) {
TCP_STATS_ESTABLISHED(c->state);
c->state = S_CONN_OK;
}
}
return ret;
}
#endif /* TCP_ASYNC */
/* Attempt to extract real connection information from an upstream load
* balancer or reverse proxy. This should be called right after accept()ing the
* connection, and before TLS negotiation.
*
* Returns:
* -1 on parsing error (connection should be closed)
* 0 on parser success, and connection information was extracted
* 1 on parser success, but no connection information was provided by the
* upstream load balancer or reverse proxy.
*/
int tcpconn_read_haproxy(struct tcp_connection *c)
{
int bytes, retval = 0;
uint32_t size, port;
char *p, *end;
struct ip_addr *src_ip, *dst_ip;
int twaitms = 0;
int tsleepus = 0;
const char v2sig[12] = "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A";
// proxy header union
union
{
// v1 struct
struct
{
char line[108];
} v1;
// v2 struct
struct
{
uint8_t sig[12];
uint8_t ver_cmd;
uint8_t fam;
uint16_t len;
union
{
struct
{ /* for TCP/UDP over IPv4, len = 12 */
uint32_t src_addr;
uint32_t dst_addr;
uint16_t src_port;
uint16_t dst_port;
} ip4;
struct
{ /* for TCP/UDP over IPv6, len = 36 */
uint8_t src_addr[16];
uint8_t dst_addr[16];
uint16_t src_port;
uint16_t dst_port;
} ip6;
struct
{ /* for AF_UNIX sockets, len = 216 */
uint8_t src_addr[108];
uint8_t dst_addr[108];
} unx;
} addr;
} v2;
} hdr;
if(cfg_get(tcp, tcp_cfg, wait_data_ms) > 10000) {
tsleepus = 100000;
} else if(cfg_get(tcp, tcp_cfg, wait_data_ms) < 1000) {
tsleepus = 50000;
} else {
tsleepus = 10 * cfg_get(tcp, tcp_cfg, wait_data_ms);
}
twaitms = 0;
do {
bytes = recv(c->s, &hdr, sizeof(hdr), MSG_PEEK);
if(bytes == -1 && (errno == EINTR || errno == EAGAIN)) {
if(twaitms <= cfg_get(tcp, tcp_cfg, wait_data_ms)) {
/* LM_DBG("bytes: %d - errno: %d (%d/%d) - twait: %dms\n", bytes,
errno, EINTR, EAGAIN, twaitms); */
sleep_us(tsleepus);
twaitms += tsleepus / 1000;
} else {
break;
}
} else {
break;
}
} while(1);
if(bytes == -1) {
/* no data received during tcp_wait_data */
return -1;
}
/* copy original tunnel address details */
memcpy(&c->cinfo.src_ip, &c->rcv.src_ip, sizeof(ip_addr_t));
memcpy(&c->cinfo.dst_ip, &c->rcv.dst_ip, sizeof(ip_addr_t));
c->cinfo.src_port = c->rcv.src_port;
c->cinfo.dst_port = c->rcv.dst_port;
c->cinfo.proto = (int)c->rcv.proto;
c->cinfo.csocket = c->rcv.bind_address;