-
Notifications
You must be signed in to change notification settings - Fork 83
/
tcp.c
1486 lines (1172 loc) · 27.4 KB
/
tcp.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
/**
* @file tcp.c Transport Control Protocol
*
* Copyright (C) 2010 Creytiv.com
*/
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_IO_H
#include <io.h>
#endif
#if !defined(WIN32)
#include <netdb.h>
#endif
#include <string.h>
#include <re_types.h>
#include <re_fmt.h>
#include <re_mem.h>
#include <re_mbuf.h>
#include <re_list.h>
#include <re_net.h>
#include <re_main.h>
#include <re_sa.h>
#include <re_tcp.h>
#define DEBUG_MODULE "tcp"
#define DEBUG_LEVEL 5
#include <re_dbg.h>
/** Platform independent buffer type cast */
#ifdef WIN32
#define BUF_CAST (char *)
#define SIZ_CAST (int)
#define close closesocket
#else
#define BUF_CAST
#define SIZ_CAST
#endif
enum {
TCP_TXQSZ_DEFAULT = 524288,
TCP_RXSZ_DEFAULT = 8192
};
/** Defines a listening TCP socket */
struct tcp_sock {
struct re_fhs *fhs;
re_sock_t fd; /**< Listening file descriptor */
re_sock_t fdc; /**< Cached connection file descriptor */
tcp_conn_h *connh; /**< TCP Connect handler */
void *arg; /**< Handler argument */
uint8_t tos; /**< Type-of-service field */
};
/** Defines a TCP connection */
struct tcp_conn {
struct list helpers; /**< List of TCP-helpers */
struct list sendq; /**< Sending queue */
struct re_fhs *fhs;
re_sock_t fdc; /**< Connection file descriptor */
tcp_estab_h *estabh; /**< Connection established handler */
tcp_send_h *sendh; /**< Data send handler */
tcp_recv_h *recvh; /**< Data receive handler */
tcp_close_h *closeh; /**< Connection close handler */
void *arg; /**< Handler argument */
size_t rxsz; /**< Maximum receive chunk size */
size_t txqsz;
size_t txqsz_max;
bool active; /**< We are connecting flag */
bool connected; /**< Connection is connected flag */
uint8_t tos; /**< Type-of-service field */
};
/** Defines a TCP-Connection Helper */
struct tcp_helper {
struct le le;
int layer;
tcp_helper_estab_h *estabh;
tcp_helper_send_h *sendh;
tcp_helper_recv_h *recvh;
void *arg;
};
struct tcp_qent {
struct le le;
struct mbuf mb;
};
static void tcp_recv_handler(int flags, void *arg);
static bool helper_estab_handler(int *err, bool active, void *arg)
{
(void)err;
(void)active;
(void)arg;
return false;
}
static bool helper_send_handler(int *err, struct mbuf *mb, void *arg)
{
(void)err;
(void)mb;
(void)arg;
return false;
}
static bool helper_recv_handler(int *err, struct mbuf *mb, bool *estab,
void *arg)
{
(void)err;
(void)mb;
(void)estab;
(void)arg;
return false;
}
static void sock_destructor(void *data)
{
struct tcp_sock *ts = data;
if (ts->fd != RE_BAD_SOCK) {
ts->fhs = fd_close(ts->fhs);
(void)close(ts->fd);
}
if (ts->fdc != RE_BAD_SOCK)
(void)close(ts->fdc);
}
static struct tcp_sock *sock_constructor(void)
{
struct tcp_sock *ts;
ts = mem_zalloc(sizeof(*ts), sock_destructor);
if (!ts)
return NULL;
ts->fhs = NULL;
ts->fd = RE_BAD_SOCK;
ts->fdc = RE_BAD_SOCK;
return ts;
}
static void conn_destructor(void *data)
{
struct tcp_conn *tc = data;
list_flush(&tc->helpers);
list_flush(&tc->sendq);
if (tc->fdc != RE_BAD_SOCK) {
tc->fhs = fd_close(tc->fhs);
(void)close(tc->fdc);
}
}
static void helper_destructor(void *data)
{
struct tcp_helper *th = data;
list_unlink(&th->le);
}
static void qent_destructor(void *arg)
{
struct tcp_qent *qe = arg;
list_unlink(&qe->le);
mem_deref(qe->mb.buf);
}
static int enqueue(struct tcp_conn *tc, struct mbuf *mb)
{
const size_t n = mbuf_get_left(mb);
struct tcp_qent *qe;
int err;
if (tc->txqsz + n > tc->txqsz_max)
return ENOSPC;
if (!tc->sendq.head && !tc->sendh) {
err = fd_listen(&tc->fhs, tc->fdc, FD_READ | FD_WRITE,
tcp_recv_handler, tc);
if (err)
return err;
}
qe = mem_zalloc(sizeof(*qe), qent_destructor);
if (!qe)
return ENOMEM;
list_append(&tc->sendq, &qe->le, qe);
mbuf_init(&qe->mb);
err = mbuf_write_mem(&qe->mb, mbuf_buf(mb), n);
qe->mb.pos = 0;
if (err)
mem_deref(qe);
else
tc->txqsz += qe->mb.end;
return err;
}
static int dequeue(struct tcp_conn *tc)
{
struct tcp_qent *qe = list_ledata(tc->sendq.head);
ssize_t n;
int err;
#ifdef MSG_NOSIGNAL
const int flags = MSG_NOSIGNAL; /* disable SIGPIPE signal */
#else
const int flags = 0;
#endif
if (!qe) {
if (tc->sendh)
tc->sendh(tc->arg);
return 0;
}
n = send(tc->fdc, BUF_CAST mbuf_buf(&qe->mb),
SIZ_CAST (qe->mb.end - qe->mb.pos), flags);
if (n < 0) {
err = RE_ERRNO_SOCK;
if (err == EAGAIN)
return 0;
#ifdef WIN32
if (err == WSAEWOULDBLOCK)
return 0;
#endif
return err;
}
tc->txqsz -= n;
qe->mb.pos += n;
if (qe->mb.pos >= qe->mb.end)
mem_deref(qe);
return 0;
}
static void conn_close(struct tcp_conn *tc, int err)
{
list_flush(&tc->sendq);
tc->txqsz = 0;
/* Stop polling */
if (tc->fdc != RE_BAD_SOCK) {
tc->fhs = fd_close(tc->fhs);
(void)close(tc->fdc);
tc->fdc = RE_BAD_SOCK;
}
if (tc->closeh)
tc->closeh(err, tc->arg);
}
static void tcp_recv_handler(int flags, void *arg)
{
struct tcp_conn *tc = arg;
struct mbuf *mb = NULL;
bool hlp_estab = false;
struct le *le;
ssize_t n;
int err = 0;
socklen_t err_len = sizeof(err);
if (flags & FD_EXCEPT) {
DEBUG_INFO("recv handler: got FD_EXCEPT on fd=%d\n", tc->fdc);
}
/* check for connection errors */
if (tc->active && !tc->connected) {
if (-1 == getsockopt(tc->fdc, SOL_SOCKET, SO_ERROR,
BUF_CAST &err, &err_len)) {
DEBUG_WARNING("recv handler: getsockopt: (%m)\n",
RE_ERRNO_SOCK);
return;
}
}
#if 0
if (EINPROGRESS != err && EALREADY != err) {
DEBUG_WARNING("recv handler: Socket error (%m)\n", err);
return;
}
#endif
if (err) {
conn_close(tc, err);
return;
}
if (flags & FD_WRITE) {
if (tc->connected) {
uint32_t nrefs;
mem_ref(tc);
err = dequeue(tc);
nrefs = mem_nrefs(tc);
mem_deref(tc);
/* check if connection was deref'd from send handler */
if (nrefs == 1)
return;
if (err) {
conn_close(tc, err);
return;
}
if (!tc->sendq.head && !tc->sendh) {
err = fd_listen(&tc->fhs, tc->fdc, FD_READ,
tcp_recv_handler, tc);
if (err) {
conn_close(tc, err);
return;
}
}
if (flags & FD_READ)
goto read;
return;
}
tc->connected = true;
err = fd_listen(&tc->fhs, tc->fdc, FD_READ, tcp_recv_handler,
tc);
if (err) {
DEBUG_WARNING("recv handler: fd_listen(): %m\n", err);
conn_close(tc, err);
return;
}
le = tc->helpers.head;
while (le) {
struct tcp_helper *th = le->data;
le = le->next;
if (th->estabh(&err, tc->active, th->arg) || err) {
if (err)
conn_close(tc, err);
return;
}
}
if (tc->estabh)
tc->estabh(tc->arg);
return;
}
read:
mb = mbuf_alloc(tc->rxsz);
if (!mb)
return;
n = recv(tc->fdc, BUF_CAST mb->buf, SIZ_CAST mb->size, 0);
if (0 == n) {
mem_deref(mb);
conn_close(tc, 0);
return;
}
else if (n < 0) {
err = RE_ERRNO_SOCK;
DEBUG_WARNING("recv handler: recv(): %m\n", err);
#ifdef WIN32
if (err == WSAECONNRESET || err == WSAECONNABORTED) {
mem_deref(mb);
conn_close(tc, err);
return;
}
#endif
goto out;
}
mb->end = n;
le = tc->helpers.head;
while (le) {
struct tcp_helper *th = le->data;
bool hdld = false;
le = le->next;
if (hlp_estab) {
hdld |= th->estabh(&err, tc->active, th->arg);
if (err) {
conn_close(tc, err);
goto out;
}
}
if (mb->pos < mb->end) {
hdld |= th->recvh(&err, mb, &hlp_estab, th->arg);
if (err) {
conn_close(tc, err);
goto out;
}
}
if (hdld)
goto out;
}
mbuf_trim(mb);
if (hlp_estab && tc->estabh) {
uint32_t nrefs;
mem_ref(tc);
tc->estabh(tc->arg);
nrefs = mem_nrefs(tc);
mem_deref(tc);
/* check if connection was deref'ed from establish handler */
if (nrefs == 1)
goto out;
}
if (mb->pos < mb->end && tc->recvh) {
tc->recvh(mb, tc->arg);
}
out:
mem_deref(mb);
}
static struct tcp_conn *conn_alloc(tcp_estab_h *eh, tcp_recv_h *rh,
tcp_close_h *ch, void *arg)
{
struct tcp_conn *tc;
tc = mem_zalloc(sizeof(*tc), conn_destructor);
if (!tc)
return NULL;
list_init(&tc->helpers);
tc->fhs = NULL;
tc->fdc = RE_BAD_SOCK;
tc->rxsz = TCP_RXSZ_DEFAULT;
tc->txqsz_max = TCP_TXQSZ_DEFAULT;
tc->estabh = eh;
tc->recvh = rh;
tc->closeh = ch;
tc->arg = arg;
return tc;
}
static int tcp_sock_setopt(struct tcp_sock *ts, int level, int optname,
const void *optval, uint32_t optlen)
{
int err = 0;
if (!ts)
return EINVAL;
if (ts->fdc != RE_BAD_SOCK) {
if (0 != setsockopt(ts->fdc, level, optname,
BUF_CAST optval, optlen))
err |= RE_ERRNO_SOCK;
}
if (ts->fd != RE_BAD_SOCK) {
if (0 != setsockopt(ts->fd, level, optname,
BUF_CAST optval, optlen))
err |= RE_ERRNO_SOCK;
}
return err;
}
/**
* Handler for incoming TCP connections.
*
* @param flags Event flags.
* @param arg Handler argument.
*/
static void tcp_conn_handler(int flags, void *arg)
{
struct sa peer;
struct tcp_sock *ts = arg;
(void)flags;
sa_init(&peer, AF_UNSPEC);
if (ts->fdc != RE_BAD_SOCK)
(void)close(ts->fdc);
#ifdef HAVE_ACCEPT4
ts->fdc = accept4(ts->fd, &peer.u.sa, &peer.len, SOCK_NONBLOCK);
if (ts->fdc == RE_BAD_SOCK) {
return;
}
#else
ts->fdc = accept(ts->fd, &peer.u.sa, &peer.len);
if (ts->fdc == RE_BAD_SOCK) {
return;
}
int err = net_sockopt_blocking_set(ts->fdc, false);
if (err) {
DEBUG_WARNING("conn handler: nonblock set: %m\n", err);
(void)close(ts->fdc);
ts->fdc = RE_BAD_SOCK;
return;
}
#endif
if (ts->connh)
ts->connh(&peer, ts->arg);
}
/**
* Create a TCP Socket with fd
*
* @param tsp Pointer to returned TCP Socket
* @param fd File descriptor
* @param ch Incoming connection handler
* @param arg Handler argument
*
* @return 0 if success, otherwise errorcode
*/
int tcp_sock_alloc_fd(struct tcp_sock **tsp, re_sock_t fd, tcp_conn_h *ch,
void *arg)
{
struct tcp_sock *ts = NULL;
if (!tsp || fd == RE_BAD_SOCK)
return EINVAL;
ts = sock_constructor();
if (!ts)
return ENOMEM;
ts->fd = fd;
ts->fdc = RE_BAD_SOCK;
ts->connh = ch;
ts->arg = arg;
*tsp = ts;
return fd_listen(&ts->fhs, ts->fd, FD_READ, tcp_conn_handler, ts);
}
/**
* Create a TCP Socket
*
* @param tsp Pointer to returned TCP Socket
* @param local Local listen address (NULL for any)
* @param ch Incoming connection handler
* @param arg Handler argument
*
* @return 0 if success, otherwise errorcode
*/
int tcp_sock_alloc(struct tcp_sock **tsp, const struct sa *local,
tcp_conn_h *ch, void *arg)
{
struct addrinfo hints, *res = NULL, *r;
char addr[64] = "";
char serv[6] = "0";
struct tcp_sock *ts = NULL;
int error, err;
if (!tsp)
return EINVAL;
ts = sock_constructor();
if (!ts)
return ENOMEM;
ts->fd = RE_BAD_SOCK;
ts->fdc = RE_BAD_SOCK;
if (local) {
(void)re_snprintf(addr, sizeof(addr), "%H",
sa_print_addr, local);
(void)re_snprintf(serv, sizeof(serv), "%u", sa_port(local));
}
memset(&hints, 0, sizeof(hints));
/* set-up hints structure */
hints.ai_family = PF_UNSPEC;
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
error = getaddrinfo(addr[0] ? addr : NULL, serv, &hints, &res);
if (error) {
#ifdef WIN32
DEBUG_WARNING("listen: getaddrinfo: wsaerr=%d\n",
WSAGetLastError());
#endif
DEBUG_WARNING("listen: getaddrinfo: %s:%s error=%d (%s)\n",
addr, serv, error, gai_strerror(error));
err = EADDRNOTAVAIL;
goto out;
}
err = EINVAL;
for (r = res; r; r = r->ai_next) {
re_sock_t fd = RE_BAD_SOCK;
if (ts->fd != RE_BAD_SOCK)
continue;
fd = socket(r->ai_family, SOCK_STREAM, IPPROTO_TCP);
if (fd == RE_BAD_SOCK) {
err = RE_ERRNO_SOCK;
continue;
}
(void)net_sockopt_reuse_set(fd, true);
err = net_sockopt_blocking_set(fd, false);
if (err) {
DEBUG_WARNING("listen: nonblock set: %m\n", err);
(void)close(fd);
continue;
}
/* OK */
ts->fd = fd;
err = 0;
break;
}
freeaddrinfo(res);
if (ts->fd == RE_BAD_SOCK)
goto out;
ts->connh = ch;
ts->arg = arg;
out:
if (err)
mem_deref(ts);
else
*tsp = ts;
return err;
}
/**
* Duplicate TCP socket
*
* @param tso TCP Socket to duplicate
*
* @return Duplicated TCP Socket if success, otherwise NULL
*/
struct tcp_sock *tcp_sock_dup(struct tcp_sock *tso)
{
struct tcp_sock *ts;
if (!tso)
return NULL;
ts = sock_constructor();
if (!ts)
return NULL;
ts->fd = RE_BAD_SOCK;
ts->fdc = tso->fdc;
tso->fdc = RE_BAD_SOCK;
return ts;
}
/**
* Bind to a TCP Socket
*
* @param ts TCP Socket
* @param local Local bind address
*
* @return 0 if success, otherwise errorcode
*/
int tcp_sock_bind(struct tcp_sock *ts, const struct sa *local)
{
struct addrinfo hints, *res = NULL, *r;
char addr[64] = "";
char serv[NI_MAXSERV] = "0";
int error, err;
if (!ts || ts->fd == RE_BAD_SOCK)
return EINVAL;
if (local) {
(void)re_snprintf(addr, sizeof(addr), "%H",
sa_print_addr, local);
(void)re_snprintf(serv, sizeof(serv), "%u", sa_port(local));
}
memset(&hints, 0, sizeof(hints));
/* set-up hints structure */
hints.ai_family = PF_UNSPEC;
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
error = getaddrinfo(addr[0] ? addr : NULL, serv, &hints, &res);
if (error) {
#ifdef WIN32
DEBUG_WARNING("sock_bind: getaddrinfo: wsaerr=%d\n",
WSAGetLastError());
#endif
DEBUG_WARNING("sock_bind: getaddrinfo: %s:%s error=%d (%s)\n",
addr, serv, error, gai_strerror(error));
return EADDRNOTAVAIL;
}
err = EINVAL;
for (r = res; r; r = r->ai_next) {
/* use dual socket */
if (r->ai_family == AF_INET6)
(void)net_sockopt_v6only(ts->fd, false);
if (bind(ts->fd, r->ai_addr, SIZ_CAST r->ai_addrlen) < 0) {
err = RE_ERRNO_SOCK;
DEBUG_WARNING("sock_bind: bind: %m (af=%d, %J)\n",
err, r->ai_family, local);
continue;
}
/* OK */
err = 0;
break;
}
freeaddrinfo(res);
return err;
}
/**
* Listen on a TCP Socket
*
* @param ts TCP Socket
* @param backlog Maximum length the queue of pending connections
*
* @return 0 if success, otherwise errorcode
*/
int tcp_sock_listen(struct tcp_sock *ts, int backlog)
{
int err;
if (!ts)
return EINVAL;
if (ts->fd == RE_BAD_SOCK) {
DEBUG_WARNING("sock_listen: invalid fd\n");
return EBADF;
}
if (listen(ts->fd, backlog) < 0) {
err = RE_ERRNO_SOCK;
DEBUG_WARNING("sock_listen: listen(): %m\n", err);
return err;
}
return fd_listen(&ts->fhs, ts->fd, FD_READ, tcp_conn_handler, ts);
}
/**
* Accept an incoming TCP Connection
*
* @param tcp Returned TCP Connection object
* @param ts Corresponding TCP Socket
* @param eh TCP Connection Established handler
* @param rh TCP Connection Receive data handler
* @param ch TCP Connection close handler
* @param arg Handler argument
*
* @return 0 if success, otherwise errorcode
*/
int tcp_accept(struct tcp_conn **tcp, struct tcp_sock *ts, tcp_estab_h *eh,
tcp_recv_h *rh, tcp_close_h *ch, void *arg)
{
struct tcp_conn *tc;
int err;
if (!tcp || !ts || ts->fdc == RE_BAD_SOCK)
return EINVAL;
tc = conn_alloc(eh, rh, ch, arg);
if (!tc)
return ENOMEM;
/* Transfer ownership to TCP connection */
tc->fdc = ts->fdc;
ts->fdc = RE_BAD_SOCK;
err = fd_listen(&tc->fhs, tc->fdc, FD_READ | FD_WRITE | FD_EXCEPT,
tcp_recv_handler, tc);
if (err) {
DEBUG_WARNING("accept: fd_listen(): %m\n", err);
}
if (err)
mem_deref(tc);
else
*tcp = tc;
return err;
}
/**
* Reject an incoming TCP Connection
*
* @param ts Corresponding TCP Socket
*/
void tcp_reject(struct tcp_sock *ts)
{
if (!ts)
return;
if (ts->fdc != RE_BAD_SOCK) {
(void)close(ts->fdc);
ts->fdc = RE_BAD_SOCK;
}
}
/**
* Allocate a TCP Connection
*
* @param tcp Returned TCP Connection object
* @param peer Network address of peer
* @param eh TCP Connection Established handler
* @param rh TCP Connection Receive data handler
* @param ch TCP Connection close handler
* @param arg Handler argument
*
* @return 0 if success, otherwise errorcode
*/
int tcp_conn_alloc(struct tcp_conn **tcp,
const struct sa *peer, tcp_estab_h *eh,
tcp_recv_h *rh, tcp_close_h *ch, void *arg)
{
struct tcp_conn *tc;
struct addrinfo hints, *res = NULL, *r;
char addr[64];
char serv[NI_MAXSERV] = "0";
int error, err;
if (!tcp || !sa_isset(peer, SA_ALL))
return EINVAL;
tc = conn_alloc(eh, rh, ch, arg);
if (!tc)
return ENOMEM;
memset(&hints, 0, sizeof(hints));
/* set-up hints structure */
hints.ai_family = PF_UNSPEC;
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
(void)re_snprintf(addr, sizeof(addr), "%H",
sa_print_addr, peer);
(void)re_snprintf(serv, sizeof(serv), "%u", sa_port(peer));
error = getaddrinfo(addr, serv, &hints, &res);
if (error) {
DEBUG_WARNING("connect: getaddrinfo(): (%s)\n",
gai_strerror(error));
err = EADDRNOTAVAIL;
goto out;
}
err = EINVAL;
for (r = res; r; r = r->ai_next) {
tc->fdc = socket(r->ai_family, SOCK_STREAM,
IPPROTO_TCP);
if (tc->fdc == RE_BAD_SOCK) {
err = RE_ERRNO_SOCK;
continue;
}
err = net_sockopt_blocking_set(tc->fdc, false);
if (err) {
DEBUG_WARNING("connect: nonblock set: %m\n", err);
(void)close(tc->fdc);
tc->fdc = RE_BAD_SOCK;
continue;
}
err = 0;
break;
}
freeaddrinfo(res);
out:
if (err)
mem_deref(tc);
else
*tcp = tc;
return err;
}
/**
* Bind a TCP Connection to a local address
*
* @param tc TCP Connection object
* @param local Local bind address
*
* @return 0 if success, otherwise errorcode
*/
int tcp_conn_bind(struct tcp_conn *tc, const struct sa *local)
{
struct addrinfo hints, *res = NULL, *r;
char addr[64] = "";
char serv[NI_MAXSERV] = "0";
int error, err;
if (!tc)
return EINVAL;
if (local) {
(void)re_snprintf(addr, sizeof(addr), "%H",
sa_print_addr, local);
(void)re_snprintf(serv, sizeof(serv), "%u", sa_port(local));
}
memset(&hints, 0, sizeof(hints));
/* set-up hints structure */
hints.ai_family = PF_UNSPEC;
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
error = getaddrinfo(addr[0] ? addr : NULL, serv, &hints, &res);
if (error) {
DEBUG_WARNING("conn_bind: getaddrinfo(): (%s)\n",
gai_strerror(error));
return EADDRNOTAVAIL;
}
err = EINVAL;
for (r = res; r; r = r->ai_next) {
(void)net_sockopt_reuse_set(tc->fdc, true);