forked from stripydog/kplex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.c
1144 lines (1049 loc) · 37 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
/* tcp.c
* This file is part of kplex
* Copyright Keith Young 2012-2016
* For copying information see the file COPYING distributed with this software
*/
#include "kplex.h"
#include "tcp.h"
#include <netdb.h>
#include <fcntl.h>
#include <netinet/tcp.h>
#include <signal.h>
#include <sys/uio.h>
#include <arpa/inet.h>
/*
* Duplicate struct if_tcp
* Args: if_tcp to be duplicated
* Returns: pointer to new if_tcp
*/
void *ifdup_tcp(void *ift)
{
struct if_tcp *oldif,*newif;
if ((newif = (struct if_tcp *) malloc(sizeof(struct if_tcp)))
== (struct if_tcp *) NULL)
return(NULL);
oldif = (struct if_tcp *) ift;
memcpy((void *)newif,(void *)oldif,sizeof(struct if_tcp));
if (newif->shared)
newif->shared->donewith=0;
return ((void *) newif);
}
/*
* Clean up a tcp interface on exit
* Args: iface_t *
* Returns: Nothing
* Side effects: de-allocates memory and performs other cleanup tasks
*/
void cleanup_tcp(iface_t *ifa)
{
struct if_tcp *ift = (struct if_tcp *)ifa->info;
if (ift->shared) {
/* io_mutex is held in cleanup routines to serialize this */
/* unlock shared mutex in case we were interupted whilst holding it */
(void) pthread_mutex_unlock(&ift->shared->t_mutex);
if (!(ift->shared->donewith)) {
ift->shared->donewith++;
return;
}
if (ift->shared->port)
free(ift->shared->port);
if (ift->shared->host)
free(ift->shared->host);
if (ift->shared->preamble) {
free((void *) ift->shared->preamble->string);
free((void *) ift->shared->preamble);
}
pthread_mutex_destroy(&ift->shared->t_mutex);
pthread_cond_destroy(&ift->shared->fv);
free(ift->shared);
}
close(ift->fd);
}
/*
* Send a preamble string (if defined)
* Args: Pointer to an if_tcp structure, pointer to tcp_preamble structure
* Returns: 0 on success, -1 on error
* Side effects: Preamble string written to the interface's file descriptor
*/
int do_preamble(struct if_tcp *ift, struct tcp_preamble *preamble)
{
size_t towrite;
ssize_t n;
if (preamble == NULL) {
if (ift->shared == NULL || ift->shared->preamble == NULL)
return (-1);
preamble=ift->shared->preamble;
}
for (towrite=preamble->len;towrite;towrite-=n) {
if ((n=write(ift->fd,preamble->string,towrite)) < 0)
return(-1);
}
return(0);
}
/*
* Set socket options to enable keepalives as required
* Args: Pointer to an if_tcp structure
* Returns: 0 on success, -1 if any errors occur
* Side effects: Sets keepalive options on interface socket
*/
int establish_keepalive(struct if_tcp *ift)
{
int on=1;
int err=0;
if (ift->shared->keepalive) {
if (setsockopt(ift->fd,SOL_SOCKET,SO_KEEPALIVE,&on,sizeof(on)) < 0) {
logerr(errno,"Could not enable keepalives on tcp socket");
return(-1);
}
if (ift->shared->keepidle)
#ifdef __APPLE__
if (setsockopt(ift->fd,IPPROTO_TCP,TCP_KEEPALIVE,
&ift->shared->keepidle, sizeof(unsigned)) < 0) {
logerr(errno,"Could not set tcp keepidle");
err=-1;
}
#else
if (setsockopt(ift->fd,IPPROTO_TCP,TCP_KEEPIDLE,
&ift->shared->keepidle,sizeof(unsigned)) < 0) {
logerr(errno,"Could not set tcp keepidle");
err=-1;
}
#endif
#if !defined (__APPLE__) || __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
if (ift->shared->keepintvl)
if (setsockopt(ift->fd,IPPROTO_TCP,TCP_KEEPINTVL,
&ift->shared->keepintvl,sizeof(unsigned)) < 0) {
logerr(errno,"Could not set tcp keepintvl");
err=-1;
}
if (ift->shared->keepcnt)
if (setsockopt(ift->fd,IPPROTO_TCP,TCP_KEEPCNT,
&ift->shared->keepcnt,sizeof(unsigned)) < 0) {
logerr(errno,"Could not set tcp keepcnt");
err=-1;
}
#endif
}
if (ift->shared->tv.tv_sec) {
if ((setsockopt(ift->fd,SOL_SOCKET,SO_SNDTIMEO,&ift->shared->tv,
sizeof(ift->shared->tv)) < 0) || (setsockopt(ift->fd,
SOL_SOCKET,SO_SNDBUF,&ift->shared->sndbuf,
sizeof(ift->shared->sndbuf)) <0))
logerr(errno,"Could not set tcp send timeout");
err=-1;
}
return(err);
}
/*
* Reconnect a lost connection in persist mode
* Args: Pointer to interface and error raised by onnection failure
* Returns: 0 on success, -1 in the case of an unrecoverable error
* Side effects: Connection should be re-established on exit
*/
int reconnect(iface_t *ifa, int err)
{
struct if_tcp *ift = (struct if_tcp *) ifa->info;
struct if_tcp *iftp;
int retval=0;
int on=1;
DEBUG(3,"%s: Reconnecting (write) interface",ifa->name);
/* ift->shared_t_mutex should be locked by the calling routine */
/* If the write timed out, we don't need to sleep before retrying */
switch (err) {
case EAGAIN:
break;
default:
mysleep(ift->shared->retry);
}
/* Loop retrying until we reconnect or encounter an error which doesn't
* look like one we are going to recover from */
for(retval=0;retval == 0;) {
/* For most re-connections, closing and re-opening the socket is
* unnecessary, but we do it here for consistency */
close(ift->fd);
if ((ift->fd=socket(ift->shared->sa.ss_family,SOCK_STREAM,
ift->shared->protocol)) < 0) {
logerr(errno,"Failed to create socket");
retval=-1;
break;
}
DEBUG(6,"%s: Reconnecting...",ifa->name);
if (connect(ift->fd,(const struct sockaddr *)
&ift->shared->sa,ift->shared->sa_len) == 0) {
break;
}
switch (errno) {
case ECONNREFUSED:
case EHOSTUNREACH:
case ENETDOWN:
case ENETUNREACH:
mysleep(ift->shared->retry);
case ETIMEDOUT:
continue;
default:
retval = -1;
}
}
DEBUG(3,"%s: Reconnected (write) interface",ifa->name);
if (retval == 0) {
if (ifa->pair) {
iftp = (struct if_tcp *) ifa->pair->info;
iftp->fd = ift->fd;
}
if (ift->shared->nodelay &&
(setsockopt(ift->fd,IPPROTO_TCP,TCP_NODELAY,&on,sizeof(on))
< 0))
logerr(errno,"Could not disable Nagle on new tcp connection");
(void) establish_keepalive(ift);
if (ift->shared->preamble){
do_preamble(ift,NULL);
}
}
DEBUG(7,"Flushing queue interface %s",ifa->name);
flush_queue(ifa->q);
return(retval);
}
/*
* Reconnect a lost read connection in persist mode
* Args: Pointer to interface
* Pointer to a buffer for newly read data
* Buffer size
* Returns: Amount of data newly read on success, -1 on error
* Side effects: Connection should be re-established on exit
*/
ssize_t reread(iface_t *ifa, char *buf, int bsize)
{
struct if_tcp *ift = (struct if_tcp *) ifa->info;
struct if_tcp *iftp;
ssize_t nread;
int fflags;
int on=1;
DEBUG(3,"%s: Reconnecting (read) interface",ifa->name);
/* ift->shared->t_mutex should be held by the calling routine */
/* Make socket non-blocking so we don't hold the mutex longer
* than necessary */
if ((fflags=fcntl(ift->fd,F_GETFL)) < 0) {
logerr(errno,"Failed to get socket flags");
return(-1);
}
if (fcntl(ift->fd,F_SETFL,fflags | O_NONBLOCK) < 0) {
logerr(errno,"Failed to make tcp socket non-blocking");
return(-1);
}
if ((nread=read(ift->fd,buf,bsize)) <= 0) {
if (nread == 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
/* An actual error as opposed to success but would block */
for (nread=-1;nread!=0;) {
close(ift->fd);
if ((ift->fd=socket(ift->shared->sa.ss_family,SOCK_STREAM,
ift->shared->protocol)) < 0) {
logerr(errno,"Failed to create socket");
nread=-1;
break;
}
mysleep(ift->shared->retry);
DEBUG(7,"%s: Retrying connection...",ifa->name);
if ((nread=connect(ift->fd,
(const struct sockaddr *)&ift->shared->sa,
ift->shared->sa_len)) == 0)
DEBUG(3,"%s: Reconnected (read) interface",ifa->name);
}
} else {
nread=0;
}
}
if (nread >= 0) {
if (fcntl(ift->fd,F_SETFL,fflags) < 0) {
logerr(errno,"Failed to make tcp socket blocking");
nread=-1;
}
}
if (nread == 0) {
(void) establish_keepalive(ift);
if (ifa->pair) {
if (!(iftp = (struct if_tcp *) ifa->pair->info)) {
logerr(errno,"No pair information found for bi-directional tcp connection!");
nread=-1;
} else {
if (ift->shared->nodelay && (setsockopt(ift->fd,IPPROTO_TCP,
TCP_NODELAY,&on,sizeof(on)) < 0))
logerr(errno,
"Could not disable Nagle on new tcp connection");
iftp->fd = ift->fd;
if (iftp->shared->preamble)
do_preamble(iftp,NULL);
}
}
}
return(nread);
}
ssize_t read_tcp(struct iface *ifa, char *buf)
{
struct if_tcp *ift = (struct if_tcp *) ifa->info;
ssize_t nread;
int done=0;
/* Wow this is ugly and due a complete re-write. In persist mode we first
check if a pair thread (ie writer to this thread's reader on a bi-
directional interface) has tried and failed to reconnect and if yes
then we exit. If not we increment a counter before entering blocking
read. This is mainly for consistency with the write side. If we don't
read something (EOF or error) we exit if persist is not set but if it
is set we check if there's another thread in the critical region
(critical == 2). If there is we do a shutdown on the socket and wait for
the partner thread. Once it catches up it waits, we fix the problem,
then signal to the other thread to re-start.
If there's no error on read we first check if another thread is waiting
for this thread to fix a problem. If it is we tell it to go ahead and
fix it because we're just leaving the critical region.
*/
for(nread=0;nread<=0;) {
if (flag_test(ifa,F_PERSIST)) {
pthread_mutex_lock(&ift->shared->t_mutex);
if (ift->fd == -1)
done++;
else
ift->shared->critical++;
pthread_mutex_unlock(&ift->shared->t_mutex);
if (done) {
nread=-1;
break;
}
}
/* Man pages lie! On FreeBSD, Linux and OS X, SIGPIPE is NOT delivered
* to a process reading from socket which times out due to unreplied to
* keepalives. Instead the read exits with ETIMEDOUT
*/
nread=read(ift->fd,buf,BUFSIZ);
if (nread <= 0) {
if (nread) {
DEBUG(3,"%s: %s",ifa->name,"Read Failed");
} else {
DEBUG(3,"%s: EOF",ifa->name);
}
if (!flag_test(ifa,F_PERSIST))
break;
pthread_mutex_lock(&ift->shared->t_mutex);
if (ift->shared->fixing) {
pthread_cond_signal(&ift->shared->fv);
pthread_cond_wait(&ift->shared->fv,&ift->shared->t_mutex);
} else {
if (ift->shared->critical == 2) {
ift->shared->fixing++;
(void) shutdown(ift->fd,SHUT_RDWR);
pthread_cond_wait(&ift->shared->fv,&ift->shared->t_mutex);
}
if ((nread=reread(ifa,buf,BUFSIZ)) < 0) {
if (ifa->pair)
((struct if_tcp *)ifa->pair->info)->fd=-1;
logerr(errno,"failed to reconnect tcp connection");
}
if (ift->shared->fixing) {
ift->shared->fixing=0;
pthread_cond_signal(&ift->shared->fv);
}
}
ift->shared->critical--;
pthread_mutex_unlock(&ift->shared->t_mutex);
} else if (flag_test(ifa,F_PERSIST)) {
pthread_mutex_lock(&ift->shared->t_mutex);
ift->shared->critical--;
if (ift->shared->fixing)
pthread_cond_signal(&ift->shared->fv);
pthread_mutex_unlock(&ift->shared->t_mutex);
}
}
return nread;
}
void write_tcp(struct iface *ifa)
{
struct if_tcp *ift = (struct if_tcp *) ifa->info;
senblk_t *sptr;
int status=0;
int err=0;
int data=0;
int cnt=1;
int done = 0;
struct iovec iov[2];
if (ifa->tagflags) {
if ((iov[0].iov_base=malloc(TAGMAX)) == NULL) {
logerr(errno,"Disabing tag output on interface id %x (%s)",
ifa->id,ifa->name);
ifa->tagflags=0;
} else {
cnt=2;
data=1;
}
}
for(;(!done);) {
if ((sptr = next_senblk(ifa->q)) == NULL)
break;
if (senfilter(sptr,ifa->ofilter)) {
senblk_free(sptr,ifa->q);
continue;
}
if (ifa->tagflags)
if ((iov[0].iov_len = gettag(ifa,iov[0].iov_base,sptr)) == 0) {
logerr(errno,"Disabing tag output on interface id %x (%s)",
ifa->id,ifa->name);
ifa->tagflags=0;
cnt=1;
data=0;
free(iov[0].iov_base);
}
/* SIGPIPE is blocked here so we can avoid using the (non-portable)
* MSG_NOSIGNAL
*/
iov[data].iov_base=sptr->data;
iov[data].iov_len=sptr->len;
if (flag_test(ifa,F_PERSIST)) {
pthread_mutex_lock(&ift->shared->t_mutex);
if (ift->fd == -1)
done++;
else
ift->shared->critical++;
pthread_mutex_unlock(&ift->shared->t_mutex);
if (done) {
senblk_free(sptr,ifa->q);
break;
}
}
if (writev(ift->fd,iov,cnt) <0) {
DEBUG2(3,"%s id %x: write failed",ifa->name,ifa->id);
err=errno;
if (!flag_test(ifa,F_PERSIST)) {
senblk_free(sptr,ifa->q);
break;
}
pthread_mutex_lock(&ift->shared->t_mutex);
if (ift->shared->fixing) {
pthread_cond_signal(&ift->shared->fv);
pthread_cond_wait(&ift->shared->fv,&ift->shared->t_mutex);
} else {
if (ift->shared->critical == 2) {
ift->shared->fixing++;
(void) shutdown(ift->fd,SHUT_RDWR);
pthread_cond_wait(&ift->shared->fv,&ift->shared->t_mutex);
}
if ((status=reconnect(ifa,err)) < 0) {
if (ifa->pair)
((struct if_tcp *) ifa->pair->info)->fd=-1;
logerr(errno,"failed to reconnect tcp connection");
done++;
}
if (ift->shared->fixing) {
ift->shared->fixing=0;
pthread_cond_signal(&ift->shared->fv);
}
}
ift->shared->critical--;
pthread_mutex_unlock(&ift->shared->t_mutex);
} else if (flag_test(ifa,F_PERSIST)) {
pthread_mutex_lock(&ift->shared->t_mutex);
ift->shared->critical--;
if (ift->shared->fixing)
pthread_cond_signal(&ift->shared->fv);
pthread_mutex_unlock(&ift->shared->t_mutex);
}
senblk_free(sptr,ifa->q);
}
if (cnt == 2)
free(iov[0].iov_base);
iface_thread_exit(errno);
}
void delayed_connect(iface_t *ifa)
{
struct if_tcp *ift = (struct if_tcp *) ifa->info;
struct if_tcp *iftp;
struct addrinfo hints,*abase,*aptr;
int err;
int on=1;
memset((void *)&hints,0,sizeof(hints));
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
pthread_mutex_lock(&ift->shared->t_mutex);
while (ift->shared->host) {
if ((err=getaddrinfo(ift->shared->host,ift->shared->port,&hints,&abase))) {
if ((err != EAI_AGAIN && err != EAI_FAIL)) {
logerr(0,"Lookup failed for host %s/service %s: %s",ift->shared->host,ift->shared->port,gai_strerror(err));
iface_thread_exit(errno);
}
abase=NULL;
}
for (aptr=abase;aptr;aptr=aptr->ai_next) {
if ((ift->fd=socket(aptr->ai_family,aptr->ai_socktype,aptr->ai_protocol)) < 0)
continue;
if (connect(ift->fd,aptr->ai_addr,aptr->ai_addrlen) == 0)
break;
close(ift->fd);
}
if (aptr) {
ift->shared->sa_len=aptr->ai_addrlen;
(void) memcpy(&ift->shared->sa,aptr->ai_addr,aptr->ai_addrlen);
ift->shared->protocol=aptr->ai_protocol;
freeaddrinfo(abase);
free(ift->shared->host);
free(ift->shared->port);
ift->shared->host=ift->shared->port=NULL;
if (ift->shared->nodelay &&
(setsockopt(ift->fd,IPPROTO_TCP,TCP_NODELAY,&on,sizeof(on))
< 0))
logerr(errno,"Could not disable Nagle on new tcp connection");
(void) establish_keepalive(ift);
if (ifa->pair) {
iftp = (struct if_tcp *) ifa->pair->info;
iftp->fd = ift->fd;
}
/* do preamble */
if (ift->shared->preamble)
do_preamble(ift,NULL);
DEBUG(3,"%s: Completed delayed connect",ifa->name);
} else {
DEBUG(4,"%s: Delayed connect failed (sleeping)",ifa->name);
mysleep(ift->shared->retry);
}
}
pthread_mutex_unlock(&ift->shared->t_mutex);
if (ifa->direction == IN)
do_read(ifa);
else {
write_tcp(ifa);
}
}
iface_t *new_tcp_conn(int fd, iface_t *ifa)
{
iface_t *newifa;
struct if_tcp *oldift=(struct if_tcp *) ifa->info;
struct if_tcp *newift=NULL;
pthread_t tid;
int on=1;
sigset_t set,saved;
if ((newifa = malloc(sizeof(iface_t))) == NULL) {
logerr(errno,"malloc failed for %s",ifa->name);
return(NULL);
}
memset(newifa,0,sizeof(iface_t));
if (((newift = (struct if_tcp *) malloc(sizeof(struct if_tcp))) == NULL) ||
((ifa->direction != IN) &&
(init_q(newifa, oldift->qsize) < 0))) {
logerr(errno,"Failed to set up new connection");
if (newifa && newifa->q)
free(newifa->q);
if (newift)
free(newift);
free(newifa);
return(NULL);
}
memset(newift,0,sizeof(struct if_tcp));
newift->fd=fd;
newift->shared=NULL;
newifa->id=ifa->id+(fd&IDMINORMASK);
newifa->direction=ifa->direction;
newifa->type=TCP;
newifa->name=ifa->name;
newifa->info=newift;
newifa->cleanup=cleanup_tcp;
newifa->write=write_tcp;
newifa->read=do_read;
newifa->tagflags=ifa->tagflags;
newifa->flags=ifa->flags;
newifa->readbuf=read_tcp;
newifa->lists=ifa->lists;
newifa->ifilter=addfilter(ifa->ifilter);
newifa->ofilter=addfilter(ifa->ofilter);
newifa->checksum=ifa->checksum;
newifa->strict=ifa->strict;
if (ifa->direction == IN)
newifa->q=ifa->lists->engine->q;
else {
if (setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,&on,sizeof(on)) < 0)
logerr(errno,"Could not disable Nagle on new tcp connection");
if (ifa->direction == BOTH) {
if ((newifa->next=ifdup(newifa)) == NULL) {
logwarn("Interface duplication failed");
free(newifa->q);
free(newift);
free(newifa);
return(NULL);
}
newifa->direction=OUT;
newifa->pair->direction=IN;
newifa->pair->q=ifa->lists->engine->q;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
pthread_sigmask(SIG_BLOCK, &set, &saved);
link_to_initialized(newifa->pair);
pthread_create(&tid,NULL,(void *)start_interface,(void *) newifa->pair);
pthread_sigmask(SIG_SETMASK,&saved,NULL);
}
}
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
pthread_sigmask(SIG_BLOCK, &set, &saved);
link_to_initialized(newifa);
pthread_create(&tid,NULL,(void *)start_interface,(void *) newifa);
pthread_sigmask(SIG_SETMASK,&saved,NULL);
return(newifa);
}
void tcp_server(iface_t *ifa)
{
int afd;
socklen_t slen;
struct if_tcp *ift=(struct if_tcp *)ifa->info;
iface_t * newifa;
struct sockaddr_storage sad;
char addrs[INET6_ADDRSTRLEN];
if (listen(ift->fd,5) == 0) {
while(ifa->direction != NONE) {
slen = sizeof(struct sockaddr_storage);
if ((afd = accept(ift->fd,(struct sockaddr *) &sad,&slen)) < 0) {
afd=errno;
logerr(errno,"accept failed for connection to %s",ifa->name);
continue;
}
if ((newifa = new_tcp_conn(afd,ifa)) == NULL) {
close(afd);
afd=-1;
}
DEBUG(3,"%s: New connection id %x %ssuccessfully received from %s",
ifa->name,newifa->id,(afd<0)?"un":"",
inet_ntop(sad.ss_family,(sad.ss_family == AF_INET)?
(const void *) &((struct sockaddr_in *)&sad)->sin_addr:
(const void *) &((struct sockaddr_in6 *)&sad)->sin6_addr,
addrs,INET6_ADDRSTRLEN));
}
}
iface_thread_exit(errno);
}
struct tcp_preamble *parse_preamble(const char * val)
{
const unsigned char *optr = (unsigned char *) val;
unsigned char *ptr;
unsigned char prebuf[MAXPREAMBLE];
int count,tval,i;
struct tcp_preamble *preamble;
for (count=0,ptr=prebuf;*optr && count < MAXPREAMBLE;count++,optr++) {
if (*optr == '\\') {
switch (*(++optr)) {
case 'a':
*ptr++ = '\a';
break;
case 'b':
*ptr++ = '\b';
break;
case 'f':
*ptr++ = '\f';
break;
case 'n':
*ptr++ = '\n';
break;
case 'r':
*ptr++ = '\r';
break;
case 't':
*ptr++ ='\t';
break;
case 'v':
*ptr++ = '\v';
break;
case '\'':
*ptr++ = '\'';
break;
case '\"':
*ptr++ = '\"';
break;
case '?':
*ptr++ = '?';
break;
case 'x':
for (i=0,tval=0;i<2;i++) {
if (*++optr == '\0')
return(NULL);
tval<<=4;
if (*optr >= '0' && *optr <= '9')
tval += *optr - '0';
else if (*optr >= 'a' && *optr <= 'f')
tval += (*optr - 'a' + 10);
else if (*optr >= 'A' && *optr <= 'F')
tval += (*optr - 'A' + 10);
else
return(NULL);
}
*ptr++=(unsigned char) tval;
break;
case '\0':
return(NULL);
default:
for (i=0,tval=0;i<3;i++) {
if (i) {
++optr;
tval <<=3;
}
if (*optr >= '0' && *optr <= '7')
tval += (*optr - '0');
else if (i == 0) {
*ptr++ = *optr;
break;
} else
return(NULL);
}
if (i == 3) {
if (tval < 512)
*ptr++ = (unsigned char) tval;
else
return(NULL);
}
break;
}
} else
*ptr++=*optr;
}
if (count == MAXPREAMBLE) {
logerr(0,"Specified preamble is too long: Max %d chars",MAXPREAMBLE);
return(0);
}
if ((preamble = (struct tcp_preamble *)
malloc(sizeof(struct tcp_preamble))) == NULL) {
logerr(errno,"Failed to allocate memory for preamble");
return(NULL);
}
if ((preamble->string=(unsigned char *) malloc(count)) == NULL) {
logerr(errno,"Failed to allocate memory for preamble string");
if (preamble)
free(preamble);
return(NULL);
}
memcpy(preamble->string,prebuf,count);
preamble->len=(size_t) count;
return(preamble);
}
iface_t *init_tcp(iface_t *ifa)
{
struct if_tcp *ift;
char *host,*port,*eptr=NULL;
struct addrinfo hints,*connection,*abase;
struct servent *svent;
struct tcp_preamble *preamble=NULL;
int err;
int on=1,off=0;
char *conntype = "c";
unsigned char *ptr;
int i;
struct kopts *opt;
long retry=5;
int keepalive=-1;
unsigned keepidle=0;
unsigned keepintvl=0;
unsigned keepcnt=0;
unsigned sndbuf=DEFSNDBUF;
int nodelay=1;
long timeout=-1;
int gpsd=0;
host=port=NULL;
if ((ift = malloc(sizeof(struct if_tcp))) == NULL) {
logerr(errno,"Could not allocate memory");
return(NULL);
}
ift->qsize=DEFQSIZE;
ift->shared=NULL;
preamble=NULL;
for(opt=ifa->options;opt;opt=opt->next) {
if (!strcasecmp(opt->var,"address"))
host=opt->val;
else if (!strcasecmp(opt->var,"mode")) {
if (strcasecmp(opt->val,"client") && strcasecmp(opt->val,"server")){
logerr(0,"Unknown tcp mode %s (must be \'client\' or \'server\')",opt->val);
return(NULL);
}
conntype=opt->val;
} else if (!strcasecmp(opt->var,"port")) {
port=opt->val;
} else if (!strcasecmp(opt->var,"retry")) {
if (!flag_test(ifa,F_PERSIST)) {
logerr(0,"retry valid only valid with persist option");
return(NULL);
}
errno=0;
if ((retry=strtol(opt->val,&eptr,0)) == 0 || (errno)) {
logerr(0,"retry value %s out of range",opt->val);
return(NULL);
}
if (*eptr != '\0') {
logerr(0,"Invalid retry value %s",opt->val);
return(NULL);
}
} else if (!strcasecmp(opt->var,"qsize")) {
if (!(ift->qsize=atoi(opt->val))) {
logerr(0,"Invalid queue size specified: %s",opt->val);
return(NULL);
}
} else if (!strcasecmp(opt->var,"keepalive")) {
if (!flag_test(ifa,F_PERSIST)) {
logerr(0,"keepalive valid only valid with persist option");
return(NULL);
}
if (!strcasecmp(opt->val,"yes"))
keepalive=1;
else if (!strcasecmp(opt->val,"no"))
keepalive=0;
else {
logerr(0,"keepalive must be \"yes\" or \"no\"");
return(NULL);
}
} else if (!strcasecmp(opt->var,"keepcnt")) {
if ((keepcnt=atoi(opt->val)) <= 0) {
logerr(0,"Invalid keepcnt value specified: %s",opt->val);
return(NULL);
}
} else if (!strcasecmp(opt->var,"keepintvl")) {
if ((keepintvl=atoi(opt->val)) <= 0) {
logerr(0,"Invalid keepintvl value specified: %s",opt->val);
return(NULL);
}
} else if (!strcasecmp(opt->var,"keepidle")) {
if ((keepidle=atoi(opt->val)) <= 0) {
logerr(0,"Invalid keepidle value specified: %s",opt->val);
return(NULL);
}
} else if (!strcasecmp(opt->var,"timeout")) {
if (!flag_test(ifa,F_PERSIST)) {
logerr(0,"timeout valid only valid with persist option");
return(NULL);
}
if (ifa->direction == IN) {
logerr(0,"Timout option is for sending tcp data only (not receiving)");
return(NULL);
}
if ((timeout=atoi(opt->val)) <= 0) {
logerr(0,"Invalid timeout value specified: %s",opt->val);
return(NULL);
}
} else if (!strcasecmp(opt->var,"sndbuf")) {
if (!flag_test(ifa,F_PERSIST)) {
logerr(0,"sndbuf valid only valid with persist option");
return(NULL);
}
if (ifa->direction == IN) {
logerr(0,"sndbuf option is for sending tcp data only (not receiving)");
return(NULL);
}
if ((sndbuf=atoi(opt->val)) <= 0) {
logerr(0,"Invalid sndbuf size value specified: %s",opt->val);
return(NULL);
}
} else if (!strcasecmp(opt->var,"gpsd")) {
if (!strcasecmp(opt->val,"yes")) {
gpsd=1;
if (!port)
port="2947";
} else if (!strcasecmp(opt->val,"no")) {
gpsd=0;
} else {
logerr(0,"Invalid option \"gpsd=%s\"",opt->val);
return(NULL);
}
} else if (!strcasecmp(opt->var,"preamble")) {
if (preamble) {
logerr(0,"Can only specify preamble once");
return(NULL);
}
if ((preamble=parse_preamble(opt->val)) == NULL) {
logerr(0,"Could not parse preamble %s",opt->val);
return(NULL);
}
} else if (!strcasecmp(opt->var,"nodelay")) {
if (!strcasecmp(opt->val,"no")) {
nodelay=0;
} else if (!strcasecmp(opt->val,"yes")) {
nodelay=1;
} else {
logerr(0,"Invalid option \"nodelay=%s\"",opt->val);
return(NULL);
}
} else {
logerr(0,"unknown interface option %s\n",opt->var);
return(NULL);
}
}
if (flag_test(ifa,F_PERSIST)){
if (keepalive == -1) {
keepalive=1;
if (!keepidle)
keepidle=DEFKEEPIDLE;
#if !defined (__APPLE__) || __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
if (!keepintvl)
keepintvl=DEFKEEPINTVL;
if (!keepcnt)
keepcnt=DEFKEEPCNT;
#endif
}
if (timeout == -1)
timeout=DEFSNDTIMEO;
}
if (*conntype == 'c') {
if (!host) {
logerr(0,"Must specify address for tcp client mode\n");
return(NULL);
}
if (gpsd) {
if (preamble) {
logerr(0,"Can't specify preamble with proto=gpsd");
return(NULL);
}
preamble=parse_preamble("?WATCH={\"enable\":true,\"nmea\":true}");
}
} else {
if (flag_test(ifa,F_PERSIST)) {
logerr(0,"persist option not valid for tcp servers");
return(NULL);
}
if (preamble) {
logerr(0,"preamble option not valid for servers");
free(preamble->string);
free(preamble);
return(NULL);
}
if (gpsd) {
logerr(0,"proto=gpsd not valid for servers");
return(NULL);
}
}
if (!port) {
if ((svent=getservbyname("nmea-0183","tcp")) != NULL)
port=svent->s_name;
else
port = DEFPORTSTRING;
}
memset((void *)&hints,0,sizeof(hints));