forked from sezero/watt32
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpctcp.c
3233 lines (2812 loc) · 80.3 KB
/
pctcp.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 pctcp.c
* Main TCP handler.
*
* PCTCP, the true worker of Waterloo TCP.
* - contains all opens, closes, major read/write routines and
* basic IP handler for incomming packets
* - Note: much of the TCP/UDP/IP layering is done at the data
* structure level, not in separate routines or tasks
*
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "copyrigh.h"
#include "wattcp.h"
#include "chksum.h"
#include "language.h"
#include "pcdns.h"
#include "bsdname.h"
#include "pcconfig.h"
#include "pcqueue.h"
#include "pcsed.h"
#include "pcstat.h"
#include "pcpkt.h"
#include "pcicmp.h"
#include "pcicmp6.h"
#include "pcigmp.h"
#include "pcdbug.h"
#include "pcdhcp.h"
#include "pcarp.h"
#include "pcbuf.h"
#include "netaddr.h"
#include "ip4_frag.h"
#include "ip4_in.h"
#include "ip4_out.h"
#include "misc.h"
#include "misc_str.h"
#include "run.h"
#include "timer.h"
#include "split.h"
#include "pppoe.h"
#include "pctcp.h"
#if defined(USE_BSD_API) || defined(USE_IPV6)
#include "socket.h"
#endif
#ifndef __inline /**< normally in <sys/cdefs.h> */
#define __inline
#endif
/** Our configured hostname. Not the FQDN.
*/
char hostname [MAX_HOSTLEN+1] = "random-pc";
unsigned _mss = ETH_MAX_DATA - TCP_OVERHEAD;
unsigned _mtu = ETH_MAX_DATA;
BOOL mtu_discover = 0; /**< \todo Add PMTU discovery method */
BOOL mtu_blackhole = 0; /**< \todo Add PMTU blackhole detection */
BOOL block_tcp = 0; /**< when application handles TCP itself */
BOOL block_udp = 0; /**< when application handles UDP itself */
BOOL block_icmp = 0; /**< when application handles ICMP itself */
BOOL block_ip = 0; /**< when application handles IP itself */
DWORD my_ip_addr = 0L; /**< our IP address */
DWORD sin_mask = 0xFFFFFF00L; /**< our net-mask, 255.255.255.0 */
_udp_Socket *_udp_allsocs = NULL; /**< list of udp-sockets */
W32_CLANG_PACK_WARN_OFF()
#include <sys/pack_on.h>
struct ip4_packet {
in_Header in;
udp_Header udp;
/* BYTE data[]; */
};
struct ip6_packet {
in6_Header in;
udp_Header udp;
/* BYTE data[]; */
};
struct tcp_pkt {
in_Header in;
tcp_Header tcp;
};
struct tcp6_pkt {
in6_Header in;
tcp_Header tcp;
};
#include <sys/pack_off.h>
W32_CLANG_PACK_WARN_DEF()
#if defined(USE_BSD_API)
/**
* This hook is to prevent the BSD-socket API being linked in
* by default. It is only set from the BSD functions when needed.
*/
void *(MS_CDECL *_bsd_socket_hook) (enum BSD_SOCKET_OPS op, ...) = NULL;
#endif
#if !defined(USE_UDP_ONLY)
/** TCP timer values.
*/
unsigned tcp_OPEN_TO = DEF_OPEN_TO; /**< Default open timeout */
unsigned tcp_CLOSE_TO = DEF_CLOSE_TO; /**< Default close timeout */
unsigned tcp_RTO_ADD = DEF_RTO_ADD; /**< Time added in RTO calculation */
unsigned tcp_RTO_BASE = DEF_RTO_BASE; /**< Base time for RTO calculation */
unsigned tcp_RTO_SCALE = DEF_RTO_SCALE; /**< Scaling used in RTO calculation */
unsigned tcp_RST_TIME = DEF_RST_TIME; /**< Don't generate RST too often */
unsigned tcp_RETRAN_TIME = DEF_RETRAN_TIME; /**< Default retransmission time */
unsigned tcp_MAX_VJSA = DEF_MAX_VJSA; /**< Default max VJ std. average */
unsigned tcp_MAX_VJSD = DEF_MAX_VJSD; /**< Default max VJ std. deviation */
/** TCP option config flags (RFC 1323).
*/
BOOL tcp_opt_ts = FALSE;
BOOL tcp_opt_wscale = FALSE;
BOOL tcp_opt_sack = FALSE;
/** Misc TCP values.
*/
BOOL tcp_nagle = TRUE; /**< Nagle algo. is on globally */
unsigned tcp_keep_idle = 2*60; /**< idle time before sending keepalive */
unsigned tcp_keep_intvl = 30; /**< time between keepalive probes */
unsigned tcp_max_idle = 60; /**< max idle time before kill */
DWORD tcp_recv_win = DEF_RECV_WIN; /**< RWIN for BSD sockets only */
_tcp_Socket *_tcp_allsocs = NULL; /**< list of tcp-sockets */
static _tcp_Socket *tcp_findseq (const in_Header *ip, const tcp_Header *tcp);
static void tcp_sockreset (_tcp_Socket *s, BOOL proxy);
static void tcp_no_arp (_tcp_Socket *s);
static void tcp_rtt_win (_tcp_Socket *s);
static void tcp_upd_win (_tcp_Socket *s, unsigned line);
static BOOL tcp_checksum (const in_Header *ip, const tcp_Header *tcp, int len);
#endif
static void udp_close (const _udp_Socket *s);
static void (W32_CALL *system_yield)(void) = NULL;
/**
* Implementation for udp_listen() and udp_open().
*/
int _udp_listen (_udp_Socket *s, WORD lport, DWORD ip, WORD port)
{
if (!_eth_is_init) /* GvB 2002-09, Lets us survive without a working eth */
{
SOCK_ERRNO (ENETDOWN);
s->err_msg = _LANG (_eth_not_init);
return (0);
}
s->ip_type = UDP_PROTO;
s->myaddr = my_ip_addr;
s->myport = lport;
s->hisaddr = ip;
s->hisport = port;
if (!s->rx_data)
{
/* This is a new socket, set default options and insert it in the chain.
*/
s->sockmode = SOCK_MODE_BINARY | SOCK_MODE_UDPCHK;
s->ttl = _default_ttl;
s->usr_yield = system_yield;
s->safetysig = SAFETY_UDP;
s->next = _udp_allsocs;
_udp_allsocs = s;
}
return (1);
}
/**
* UDP passive open. Listen for a connection on a particular port.
*/
int W32_CALL udp_listen (_udp_Socket *s, WORD lport, DWORD ip,
WORD port, ProtoHandler handler)
{
udp_close (s);
WATT_LARGE_CHECK (s, sizeof(*s));
memset (s, 0, sizeof(*s));
lport = find_free_port (lport, 0);
if (!_udp_listen (s, lport, ip, port))
{
reuse_localport (lport);
return (0);
}
s->rx_data = &s->rx_buf[0];
s->max_rx_data = sizeof(s->rx_buf) - 1;
s->protoHandler = handler;
return (1);
}
/**
* Implementation for udp_open(), also used by BSD connect().
*/
int _udp_open (_udp_Socket *s, WORD lport, DWORD ip, WORD port)
{
BOOL bcast;
if (ip && _ip4_is_multihome_addr(ip)) /* 0.0.0.0 is legal */
{
SOCK_ERRNO (EINVAL);
strcpy (s->err_buf, _LANG("Illegal destination "));
strcat (s->err_buf, _inet_ntoa(NULL,ip));
s->err_msg = s->err_buf;
return (0);
}
if (!_udp_listen(s, lport, ip, port))
return (0);
bcast = ((ip == IP_BCAST_ADDR) || (~ip & ~sin_mask) == 0);
if (bcast || !ip) /* check for broadcast */
{
memset (s->his_ethaddr, 0xFF, sizeof(eth_address));
if (!ip)
s->hisaddr = IP_BCAST_ADDR; /* 255.255.255.255 (this network) */
}
#if defined(USE_MULTICAST)
else if (_ip4_is_multicast(ip)) /* check for multicast */
{
multi_to_eth (ip, &s->his_ethaddr);
s->ttl = 1; /* so we don't send worldwide as default */
}
#endif
else if (!_arp_resolve(ip,&s->his_ethaddr))
{
SOCK_ERRNO (EHOSTUNREACH);
strcpy (s->err_buf, _LANG("ARP failed "));
strcat (s->err_buf, _inet_ntoa(NULL,ip));
s->err_msg = s->err_buf;
STAT (ip4stats.ips_noroute++);
udp_close (s);
return (0);
}
return (1);
}
/**
* UDP active open. Open a connection on a particular port.
*/
int W32_CALL udp_open (_udp_Socket *s, WORD lport, DWORD ip,
WORD port, ProtoHandler handler)
{
udp_close (s);
WATT_LARGE_CHECK (s, sizeof(*s));
memset (s, 0, sizeof(*s));
lport = find_free_port (lport, 0);
if (!_udp_open (s, lport, ip, port))
{
reuse_localport (lport);
return (0);
}
s->rx_data = &s->rx_buf[0];
s->max_rx_data = sizeof(s->rx_buf) - 1;
s->protoHandler = handler;
return (1);
}
/**
* Since UDP is stateless, simply reclaim the local-port and
* unthread the socket from the list.
*/
static void udp_close (const _udp_Socket *udp)
{
_udp_Socket *s, *prev;
for (s = prev = _udp_allsocs; s; prev = s, s = s->next)
{
if (udp != s)
continue;
reuse_localport (s->myport);
if (s == _udp_allsocs)
_udp_allsocs = s->next;
else prev->next = s->next;
SET_ERR_MSG (s, _LANG("UDP Close called"));
break;
}
}
/**
* Set the TTL on an outgoing UDP datagram.
*/
int W32_CALL udp_SetTTL (_udp_Socket *s, BYTE ttl)
{
s->ttl = ttl;
return (0);
}
#if !defined(USE_UDP_ONLY)
/**
* Actively open a TCP connection.
*
* Make connection to a particular destination.
* Not used for IPv6 (see _TCP6_open()).
* \retval 0 on error. 's->err_msg' filled.
*
* \note 'lport' is local port to associate with the connection.
* \note 'rport' is remote port for same connection.
*/
int W32_CALL tcp_open (_tcp_Socket *s, WORD lport, DWORD ip,
WORD rport, ProtoHandler handler)
{
WATT_LARGE_CHECK (s, sizeof(*s));
_tcp_unthread (s, FALSE); /* just in case not totally closed */
memset (s, 0, sizeof(*s));
s->state = tcp_StateCLOSED; /* otherwise is tcp_StateLISTEN on failure */
STAT (tcpstats.tcps_connattempt++);
if (!_eth_is_init) /* GvB 2002-09, Lets us survive without a working eth */
{
SOCK_ERRNO (ENETDOWN);
s->err_msg = _LANG (_eth_not_init);
return (0);
}
if (!ip || _ip4_is_multihome_addr(ip) || _ip4_is_multicast(ip))
{
SOCK_ERRNO (EINVAL);
strcpy (s->err_buf, _LANG("Illegal destination "));
strcat (s->err_buf, _inet_ntoa(NULL,ip));
s->err_msg = s->err_buf;
return (0);
}
if (!_arp_start_lookup(ip)) /* GvB 2002-09, now non-blocking */
{
SOCK_ERRNO (EHOSTUNREACH);
strcpy (s->err_buf, _LANG("Failed to start ARP lookup "));
strcat (s->err_buf, _inet_ntoa(NULL,ip));
s->err_msg = s->err_buf;
return (0);
}
s->rx_data = &s->rx_buf[0];
s->max_rx_data = sizeof(s->rx_buf) - 1;
s->tx_data = &s->tx_buf[0];
s->max_tx_data = sizeof(s->tx_buf) - 1;
s->ip_type = TCP_PROTO;
s->max_seg = _mss; /**< \todo use \b mss from setsockopt() */
s->state = tcp_StateRESOLVE;
s->cwindow = 1;
s->wwindow = 0; /* slow start VJ algorithm */
s->vj_sa = INIT_VJSA;
s->rto = tcp_OPEN_TO; /* added 14-Dec 1999, GV */
s->myaddr = my_ip_addr;
s->myport = find_free_port (lport,1); /* get a nonzero port val */
s->locflags = LF_LINGER; /* close via TIMEWT state */
s->ttl = _default_ttl;
s->hisaddr = ip;
s->hisport = rport;
s->send_next = INIT_SEQ();
s->flags = tcp_FlagSYN;
s->unhappy = TRUE;
s->protoHandler = handler;
s->usr_yield = system_yield;
s->safetysig = SAFETY_TCP; /* marker signatures */
s->safetytcp = SAFETY_TCP;
s->next = _tcp_allsocs; /* insert into chain */
_tcp_allsocs = s;
/** \todo use \b TCP_NODELAY set in setsockopt()
*/
if (tcp_nagle)
s->sockmode = SOCK_MODE_NAGLE;
s->sockmode |= SOCK_MODE_BINARY;
return (1);
}
/**
* Passively opens TCP a connection.
*
* Listen for a connection on a particular port
*/
int W32_CALL tcp_listen (_tcp_Socket *s, WORD lport, DWORD ip,
WORD port, ProtoHandler handler, WORD timeout)
{
WATT_LARGE_CHECK (s, sizeof(*s));
_tcp_unthread (s, FALSE); /* just in case not totally closed */
memset (s, 0, sizeof(*s));
s->state = tcp_StateCLOSED; /* otherwise is tcp_StateLISTEN if success */
if (!_eth_is_init) /* GvB 2002-09, Lets us survive without a working eth */
{
SOCK_ERRNO (ENETDOWN);
s->err_msg = _LANG (_eth_not_init);
return (0);
}
if (_ip4_is_multicast(ip)) /* 0.0.0.0 is legal */
{
SOCK_ERRNO (EINVAL);
strcpy (s->err_buf, _LANG("Illegal destination "));
strcat (s->err_buf, _inet_ntoa(NULL,ip));
s->err_msg = s->err_buf;
return (0);
}
s->rx_data = &s->rx_buf[0];
s->max_rx_data = sizeof(s->rx_buf) - 1;
s->tx_data = &s->tx_buf[0];
s->max_tx_data = sizeof(s->tx_buf) - 1;
s->ip_type = TCP_PROTO;
s->max_seg = _mss; /**< \todo use \b mss from setsockopt() */
s->cwindow = 1;
s->wwindow = 0; /* slow start VJ algorithm */
s->vj_sa = INIT_VJSA;
s->state = tcp_StateLISTEN;
s->locflags = LF_LINGER | LF_IS_SERVER;
s->myport = find_free_port (lport, 0);
s->hisport = port;
s->hisaddr = ip;
s->send_next = INIT_SEQ();
s->unhappy = FALSE;
s->ttl = _default_ttl;
s->protoHandler = handler;
s->usr_yield = system_yield;
s->safetysig = SAFETY_TCP; /* marker signatures */
s->safetytcp = SAFETY_TCP;
s->next = _tcp_allsocs; /* insert into chain */
_tcp_allsocs = s;
if (tcp_nagle)
s->sockmode = SOCK_MODE_NAGLE;
s->sockmode |= SOCK_MODE_BINARY;
if (timeout != 0)
s->timeout = set_timeout (1000 * timeout);
return (1);
}
/**
* Close a TCP connection.
*
* Send a FIN on a particular port -- only works if it is open.
* Must still allow receives.
*/
void _tcp_close (_tcp_Socket *s)
{
if (s->ip_type != TCP_PROTO)
return;
if (s->state == tcp_StateESTAB ||
s->state == tcp_StateESTCL ||
s->state == tcp_StateSYNREC)
{
if (s->tx_datalen) /* must first flush all Tx data */
{
s->flags |= (tcp_FlagPUSH | tcp_FlagACK);
if (s->state < tcp_StateESTCL)
{
s->state = tcp_StateESTCL;
TCP_SENDSOON (s);
}
}
else /* really closing */
{
SET_ERR_MSG (s, _LANG("Connection closed normally"));
s->flags = (tcp_FlagACK | tcp_FlagFIN);
s->state = tcp_StateFINWT1;
s->timeout = set_timeout (tcp_TIMEOUT);
s->rtt_time = 0UL; /* stop RTT timer */
TCP_SEND (s);
}
s->unhappy = TRUE;
}
else if (s->state == tcp_StateCLOSWT)
{
/* need to ACK the FIN and get on with it
*/
s->timeout = set_timeout (tcp_LASTACK_TIME); /* Added AGW 6 Jan 2001 */
s->state = tcp_StateLASTACK;
s->flags |= tcp_FlagFIN;
TCP_SEND (s);
s->unhappy = TRUE;
}
else if (s->state == tcp_StateRESOLVE || /* unlink failed connection */
s->state == tcp_StateSYNSENT)
{
s->state = tcp_StateCLOSED;
maybe_reuse_localport (s);
_tcp_unthread (s, FALSE);
}
}
/**
* Abort a TCP connection.
*/
_tcp_Socket *_tcp_abort (_tcp_Socket *s, const char *file, unsigned line)
{
SET_ERR_MSG (s, _LANG("TCP Abort"));
TRACE_FILE ("_tcp_abort(%" ADDR_FMT ") called from %s (%u). State %s\n",
ADDR_CAST(s), file, line, tcpStateName(s->state));
if (s->state >= tcp_StateSYNSENT &&
s->state <= tcp_StateLASTACK)
{
s->flags = (tcp_FlagRST | tcp_FlagACK);
s->unhappy = TRUE;
if (s->state <= tcp_StateSYNREC)
{
s->rtt_time = 0UL; /* Stop RTT timer */
tcp_rtt_clr (s); /* Clear cached RTT */
}
s->tx_datalen = 0; /* RST should not carry any data */
TCP_SEND (s);
}
s->unhappy = FALSE;
s->ip_type = 0;
s->tx_datalen = 0; /* discard Tx buffer, but not Rx buffer */
maybe_reuse_localport (s);
ARGSUSED (file);
ARGSUSED (line);
return _tcp_unthread (s, TRUE);
}
/**
* Schedule a transmission pretty soon.
*
* This one has an imperfection at midnight, but it
* is not significant to the connection performance.
*
* \note gv: Added - 5 May 2000: Relax retransmission period to
* \b tcp_CLOSE_TO when \b CLOSEWT state is entered.\n
* Relax retransmission period to \b tcp_OPEN_TO in
* \b SYNSENT state.
*/
int _tcp_sendsoon (_tcp_Socket *s, const char *file, unsigned line)
{
DWORD timeout;
if (s->ip_type != TCP_PROTO)
return (0);
if (s->state >= tcp_StateCLOSWT)
timeout = set_timeout (tcp_CLOSE_TO);
else timeout = set_timeout (tcp_RTO_BASE);
if (s->rto <= tcp_RTO_BASE && s->recent == 0 &&
cmp_timers(s->rtt_time,timeout) <= 0)
{ /* !! was == */
int rc;
s->karn_count = 0;
rc = _tcp_send (s, file, line);
s->recent = 1;
return (rc);
}
if ((s->unhappy || s->tx_datalen > 0 || s->karn_count == 1) &&
(s->rtt_time && cmp_timers(s->rtt_time,timeout) < 0))
return (0);
if (s->state == tcp_StateSYNSENT) /* relaxed in SYNSENT state */
s->rtt_time = set_timeout (tcp_OPEN_TO);
else s->rtt_time = set_timeout (s->rto / tcp_RTO_SCALE);
s->karn_count = 1;
return (0);
}
/**
* Unthread a socket from the tcp socket list, if it's there.
* Free Tx-buffer if set in tcp_set_window().
*/
_tcp_Socket *_tcp_unthread (_tcp_Socket *ds, BOOL free_tx)
{
_tcp_Socket *s, *prev;
_tcp_Socket *next = NULL;
if (ds == NULL)
return (NULL);
for (s = prev = _tcp_allsocs; s; prev = s, s = s->next)
{
if (ds != s)
continue;
if (s == _tcp_allsocs)
_tcp_allsocs = s->next;
else prev->next = s->next;
next = s->next;
break;
}
if (ds->rx_datalen == 0 || (ds->state > tcp_StateESTCL))
ds->ip_type = 0; /* fail further I/O */
ds->state = tcp_StateCLOSED; /* tcp_tick needs this */
if (free_tx && ds->tx_data != NULL && ds->tx_data != &ds->tx_buf[0] &&
*(DWORD*)(ds->tx_data-4) == SAFETY_TCP)
{
free (ds->tx_data-4);
ds->tx_data = NULL;
ds->tx_datalen = 0; /* should already be 0 */
}
return (next);
}
/**
* Returns 1 if TCP connection is established.
*/
int W32_CALL tcp_established (const _tcp_Socket *s)
{
return (s->state >= tcp_StateESTAB);
}
/**
* The main TCP input handler.
*
* All TCP input processing is done from here.
*/
_tcp_Socket *_tcp_handler (const in_Header *ip, BOOL broadcast)
{
#if defined(USE_IPV6)
const in6_Header *ip6 = (const in6_Header*)ip;
ip6_address ip6_src, ip6_dst;
#endif
tcp_Header *tcp;
_tcp_Socket *s;
int len;
BYTE flags;
DWORD source = intel (ip->source);
DWORD destin = intel (ip->destination);
DWORD seq;
WORD dstPort, srcPort;
BOOL is_ip4 = (ip->ver == 4);
if (is_ip4)
{
if (broadcast || block_tcp ||
!_ip4_is_local_addr(destin) || _ip4_is_multicast(source))
{
DEBUG_RX (NULL, ip);
return (NULL);
}
len = in_GetHdrLen (ip); /* len of IP header */
tcp = (tcp_Header*) ((BYTE*)ip + len); /* tcp frame pointer */
len = intel16 (ip->length) - len; /* len of tcp+data */
flags = tcp->flags & tcp_FlagMASK; /* get TCP flags */
if (!tcp_checksum(ip,tcp,len))
{
DEBUG_RX (NULL, ip);
return (NULL);
}
}
#if defined(USE_IPV6)
else
{
if (broadcast || block_tcp ||
!IN6_IS_ADDR_LINKLOCAL(&ip6->destination) ||
IN6_IS_ADDR_MULTICAST(&ip6->source))
{
DEBUG_RX (NULL, ip);
return (NULL);
}
len = intel16 (ip6->len);
tcp = (tcp_Header*) pkt_get_type_in (TYPE_TCP_HEAD)->data;
flags = tcp->flags & tcp_FlagMASK;
memcpy (&ip6_dst, &ip6->destination, sizeof(ip6_dst));
memcpy (&ip6_src, &ip6->source, sizeof(ip6_src));
if (!_ip6_tcp_checksum(ip6,tcp,len))
{
DEBUG_RX (NULL, ip6);
return (NULL);
}
}
#else
else
{
DEBUG_RX (NULL, ip);
return (NULL);
}
#endif
dstPort = intel16 (tcp->dstPort);
srcPort = intel16 (tcp->srcPort);
/* demux to active sockets
*/
for (s = _tcp_allsocs; s; s = s->next)
{
if (s->safetysig != SAFETY_TCP || s->safetytcp != SAFETY_TCP)
{
outsnl (_LANG("Error in _tcp_handler()"));
DEBUG_RX (s, ip);
return (NULL);
}
if (s->is_ip6 && !is_ip4)
{
#if defined(USE_IPV6)
if (s->hisport &&
!memcmp(&ip6_dst, &s->my6addr, sizeof(ip6_dst)) &&
!memcmp(&ip6_src, &s->his6addr, sizeof(ip6_src)) &&
dstPort == s->myport &&
srcPort == s->hisport)
break;
#endif
}
else if (is_ip4)
{
if (s->hisport && /* IP4: not a listening socket */
destin == s->myaddr && /* addressed to my IP */
source == s->hisaddr && /* and from my peer address */
dstPort == s->myport && /* addressed to my local port */
srcPort == s->hisport) /* and from correct remote port */
break;
}
}
if (!s && (flags & tcp_FlagSYN))
{
/* demux to passive (listening) sockets, must be a new session
*/
for (s = _tcp_allsocs; s; s = s->next)
if (s->hisport == 0 && /* =0, listening socket */
s->myport == dstPort) /* addressed to my local port */
{
if (s->is_ip6)
{
#if defined(USE_IPV6)
if (!is_ip4)
{
s->hisaddr = source;
s->hisport = srcPort;
memcpy (&s->my6addr, ip6_dst, sizeof(s->my6addr));
}
#endif
}
else if (is_ip4)
{
s->hisaddr = source; /* remember his IP-address */
s->hisport = srcPort; /* and src-port */
s->myaddr = destin; /* socket is now active (should be same) */
}
break;
}
}
DEBUG_RX (s, ip);
if (!s)
{
if (!(flags & tcp_FlagRST)) /* don't answer RST */
{
TCP_SEND_RESET (NULL, ip, tcp);
}
else if ((flags & tcp_FlagACK) && /* got ACK,RS */
(s = tcp_findseq(ip,tcp)) != NULL) /* ACK = SEQ + 1 */
{
/* e.g. a firewall is sending RST for host on inside.
*/
tcp_sockreset (s, TRUE);
}
return (NULL);
}
#if defined(USE_TCP_MD5)
if (is_ip4)
{
/* \todo: check in a src-host database and drop if not found
* and MD5 fails.
*/
check_md5_signature (s, ip);
}
#endif
/* Restart idle-timer unless overridden by tcp_connect().
*/
if (sock_inactive && !(s->locflags & LF_RCVTIMEO))
s->inactive_to = set_timeout (1000 * sock_inactive);
/* Got a RST with correct SEQ (RCV.NXT - RCV.NXT-RCV.RWIN+1).
* Reset socket (if socket is a server, go back to listening).
*/
seq = intel (tcp->seqnum);
if ((flags & tcp_FlagRST) &&
SEQ_GEQ(seq, s->recv_next) &&
SEQ_LEQ(seq, s->recv_next+s->adv_win+1))
{
TRACE_FILE ("_tcp_handler(): got RST, SEQ %lu\n", (u_long)seq);
tcp_sockreset (s, FALSE);
return (NULL);
}
if (flags & tcp_FlagPUSH) /* EE 2002.2.28 */
s->locflags |= LF_GOT_PUSH;
else s->locflags &= ~LF_GOT_PUSH;
tcp_rtt_win (s); /* update retrans timer, windows etc. */
if (_tcp_fsm(&s,ip) && /* do input tcp state-machine */
s->unhappy) /* if "unhappy", retransmit soon */
TCP_SENDSOON (s);
#if defined(USE_DEBUG)
_sock_check_tcp_buffers (s);
#endif
return (s);
}
#endif /* !USE_UDP_ONLY */
/**
* Verify checksum for an UDP packet.
*/
static BOOL udp_checksum (const in_Header *ip, const udp_Header *udp, int len)
{
tcp_PseudoHeader ph = { 0,0,0,0,0,0 };
ph.src = ip->source; /* already network order */
ph.dst = ip->destination;
ph.protocol = UDP_PROTO;
ph.length = udp->length;
ph.checksum = CHECKSUM (udp, len);
if (CHECKSUM(&ph,sizeof(ph)) != 0xFFFF)
{
TRACE_CONSOLE (1, "Bad udp checksum. ");
TRACE_FILE ("Bad udp checksum\n");
STAT (udpstats.udps_badsum++);
return (FALSE);
}
return (TRUE);
}
/**
* Demultiplexer for incoming UDP packets.
* Don't debug packet if no match was found (except if '*udp_err').
*/
static _udp_Socket *udp_demux (const in_Header *ip, BOOL ip_bcast,
DWORD destin, WORD srcPort, WORD dstPort,
BOOL *udp_err)
{
_udp_Socket *s;
BOOL is_ip4 = (ip->ver == 4);
#if defined(USE_IPV6)
const in6_Header *ip6 = (const in6_Header*)ip;
ip6_address ip6_src, ip6_dst;
if (!is_ip4)
{
memcpy (&ip6_dst, &ip6->destination, sizeof(ip6_dst));
memcpy (&ip6_src, &ip6->source, sizeof(ip6_src));
}
else
{
/*
* To supress GCC 5.x warning:
* pctcp.c:951:14: warning: 'ip6_dst' may be used uninitialized in this function [-Wmaybe-uninitialized]
* memcpy (&s->my6addr, &ip6_dst, sizeof(s->my6addr));
*/
memset (&ip6_dst, 0, sizeof(ip6_dst));
}
#endif
*udp_err = FALSE; /* assume socket-list is OK */
/** Demux to active sockets.
* \todo use some kind of hashing to speed up the search.
*/
for (s = _udp_allsocs; s; s = s->next)
{
if (s->safetysig != SAFETY_UDP)
{
outsnl (_LANG("Error in udp_demux()"));
s->safetysig = SAFETY_UDP;
DEBUG_RX (s, ip);
*udp_err = TRUE;
return (NULL);
}
if (!ip_bcast &&
(s->hisport != 0) &&
(dstPort == s->myport) &&
(srcPort == s->hisport))
{
if (s->is_ip6)
{
#if defined(USE_IPV6)
if (!is_ip4 &&
IN6_ARE_ADDR_EQUAL(&ip6_src, &s->his6addr)) /* !!mask */
{
DEBUG_RX (s, ip);
break;
}
#endif
}
else if (is_ip4 &&
((destin & sin_mask) == (s->myaddr & sin_mask)) &&
(intel(ip->source) == s->hisaddr))
{
DEBUG_RX (s, ip);
break;
}
}
}
if (!s)
{
/* demux to passive sockets
*/
for (s = _udp_allsocs; s; s = s->next)
{
if (s->is_ip6)
{
#if defined(USE_IPV6)
if (!is_ip4 && dstPort == s->myport &&
IN6_IS_ADDR_UNSPECIFIED(&s->his6addr))
{
DEBUG_RX (s, ip);
memcpy (&s->his6addr, &ip6_src, sizeof(s->his6addr));
s->hisport = srcPort;
SET_PEER_MAC_ADDR (s, ip6);
if (!ip_bcast)
memcpy (&s->my6addr, &ip6_dst, sizeof(s->my6addr));
break;
}
#endif
}
else if (is_ip4 && (s->hisaddr == 0 || s->hisaddr == IP_BCAST_ADDR) &&
dstPort == s->myport)
{
DEBUG_RX (s, ip);
if (s->hisaddr == 0)
{
s->hisaddr = intel (ip->source); /* socket now active */
s->hisport = srcPort;
SET_PEER_MAC_ADDR (s, ip);
/* take on value of expected destination
* unless it is broadcast
*/
if (!ip_bcast)
s->myaddr = destin;
}
break;
}
}
}
#if defined(USE_MULTICAST)
if (is_ip4 && !s)
{
/* demux to multicast sockets
*/
for (s = _udp_allsocs; s; s = s->next)
{
if (s->hisport != 0 &&
s->hisaddr == destin &&
dstPort == s->myport &&
_ip4_is_multicast(destin))