Rev 1183 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
431 | serge | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
983 | diamond | 3 | ;; Copyright (C) KolibriOS team 2004-2008. All rights reserved. ;; |
431 | serge | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;; TCP.INC ;; |
||
7 | ;; ;; |
||
8 | ;; TCP Processes for Menuet OS TCP/IP stack ;; |
||
9 | ;; ;; |
||
10 | ;; Copyright 2002 Mike Hibbett, mikeh@oceanfree.net ;; |
||
11 | ;; ;; |
||
12 | ;; See file COPYING for details ;; |
||
13 | ;; v0.6 : Added reset handling in the established state ;; |
||
14 | ;; Added a timer per socket to allow delays when ;; |
||
15 | ;; rx window gets below 1KB ;; |
||
907 | mikedld | 16 | ;; ;; |
431 | serge | 17 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ha | 18 | |
593 | mikedld | 19 | $Revision: 1288 $ |
20 | |||
21 | |||
261 | hidnplayr | 22 | ; TCP TCB states |
907 | mikedld | 23 | TCB_LISTEN equ 1 |
24 | TCB_SYN_SENT equ 2 |
||
25 | TCB_SYN_RECEIVED equ 3 |
||
26 | TCB_ESTABLISHED equ 4 |
||
27 | TCB_FIN_WAIT_1 equ 5 |
||
28 | TCB_FIN_WAIT_2 equ 6 |
||
29 | TCB_CLOSE_WAIT equ 7 |
||
30 | TCB_CLOSING equ 8 |
||
31 | TCB_LAST_ACK equ 9 |
||
32 | TCB_TIMED_WAIT equ 10 |
||
33 | TCB_CLOSED equ 11 |
||
1 | ha | 34 | |
907 | mikedld | 35 | TH_FIN = 0x01 |
36 | TH_SYN = 0x02 |
||
37 | TH_RST = 0x04 |
||
38 | TH_PUSH = 0x08 |
||
39 | TH_ACK = 0x10 |
||
40 | TH_URG = 0x20 |
||
261 | hidnplayr | 41 | |
907 | mikedld | 42 | TWOMSL equ 10 ; # of secs to wait before closing socket |
261 | hidnplayr | 43 | |
907 | mikedld | 44 | TCP_RETRIES equ 5 ; Number of times to resend a packet |
1183 | clevermous | 45 | TCP_TIMEOUT equ 20 ; resend if not replied to in x hs |
907 | mikedld | 46 | |
1 | ha | 47 | ;******************************************************************* |
48 | ; Interface |
||
49 | ; |
||
50 | ; tcp_tx_handler Handles the TCP transmit queue |
||
51 | ; tcp_rx The protocol handler for received data |
||
52 | ; buildTCPPacket fills in the packet headers and data |
||
53 | ; tcpStateMachine Main state machine for received TCP packets |
||
54 | ; tcp_tcb_handler 1s timer, to erase tcb's in TIME_WAIT state |
||
55 | ; |
||
56 | ;******************************************************************* |
||
57 | |||
58 | |||
261 | hidnplayr | 59 | ; TCP Payload ( Data field in IP datagram ) |
60 | ; |
||
61 | ; 0 1 2 3 |
||
62 | ; 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
||
63 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
64 | ;20 | Source Port | Destination Port | |
||
65 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
66 | ;24 | Sequence Number | |
||
67 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
68 | ;28 | Acknowledgment Number | |
||
69 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
70 | ;32 | Data | |U|A|P|R|S|F| | |
||
71 | ; | Offset| Reserved |R|C|S|S|Y|I| Window | |
||
72 | ; | | |G|K|H|T|N|N| | |
||
73 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
74 | ;36 | Checksum | Urgent Pointer | |
||
75 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
76 | ;40 | Options | Padding | |
||
77 | ; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
78 | ; | data |
||
1 | ha | 79 | |
261 | hidnplayr | 80 | |
81 | struc TCP_PACKET |
||
907 | mikedld | 82 | { .SourcePort dw ? ;+00 |
261 | hidnplayr | 83 | .DestinationPort dw ? ;+02 |
84 | .SequenceNumber dd ? ;+04 |
||
907 | mikedld | 85 | .AckNumber dd ? ;+08 |
86 | .DataOffset db ? ;+12 - DataOffset[0-3 bits] and Reserved[4-7] |
||
87 | .Flags db ? ;+13 - Reserved[0-1 bits]|URG|ACK|PSH|RST|SYN|FIN |
||
88 | .Window dw ? ;+14 |
||
89 | .Checksum dw ? ;+16 |
||
261 | hidnplayr | 90 | .UrgentPointer dw ? ;+18 |
907 | mikedld | 91 | .Options rb 3 ;+20 |
92 | .Padding db ? ;+23 |
||
93 | .Data db ? ;+24 |
||
261 | hidnplayr | 94 | } |
95 | |||
96 | virtual at 0 |
||
97 | TCP_PACKET TCP_PACKET |
||
98 | end virtual |
||
99 | |||
100 | |||
101 | |||
1 | ha | 102 | ;*************************************************************************** |
103 | ; Function |
||
104 | ; tcp_tcb_handler |
||
105 | ; |
||
106 | ; Description |
||
107 | ; Handles sockets in the timewait state, closing them |
||
108 | ; when the TCB timer expires |
||
109 | ; |
||
110 | ;*************************************************************************** |
||
111 | |||
907 | mikedld | 112 | proc tcp_tcb_handler stdcall uses ebx |
113 | ; scan through all the sockets, decrementing active timers |
||
1 | ha | 114 | |
907 | mikedld | 115 | mov ebx, net_sockets |
1 | ha | 116 | |
907 | mikedld | 117 | cmp [ebx + SOCKET.NextPtr], 0 |
118 | je .exit |
||
1095 | diamond | 119 | ;DEBUGF 1, "K : sockets:\n" |
1 | ha | 120 | |
907 | mikedld | 121 | .next_socket: |
122 | mov ebx, [ebx + SOCKET.NextPtr] |
||
123 | or ebx, ebx |
||
124 | jz .exit |
||
1 | ha | 125 | |
1095 | diamond | 126 | ;DEBUGF 1, "K : %x-%x: %x-%x-%x-%u\n", [ebx + SOCKET.PID]:2, [ebx + SOCKET.Number]:2, [ebx + SOCKET.LocalPort]:4, [ebx + SOCKET.RemoteIP], [ebx + SOCKET.RemotePort]:4, [ebx + SOCKET.TCBState] |
1 | ha | 127 | |
907 | mikedld | 128 | cmp [ebx + SOCKET.TCBTimer], 0 |
129 | jne .decrement_tcb |
||
130 | cmp [ebx + SOCKET.wndsizeTimer], 0 |
||
131 | jne .decrement_wnd |
||
132 | jmp .next_socket |
||
1 | ha | 133 | |
907 | mikedld | 134 | .decrement_tcb: |
135 | ; decrement it, delete socket if TCB timer = 0 & socket in timewait state |
||
136 | dec [ebx + SOCKET.TCBTimer] |
||
137 | jnz .next_socket |
||
1 | ha | 138 | |
907 | mikedld | 139 | cmp [ebx + SOCKET.TCBState], TCB_TIMED_WAIT |
140 | jne .next_socket |
||
1 | ha | 141 | |
907 | mikedld | 142 | push [ebx + SOCKET.PrevPtr] |
143 | stdcall net_socket_free, ebx |
||
144 | pop ebx |
||
145 | jmp .next_socket |
||
1 | ha | 146 | |
907 | mikedld | 147 | .decrement_wnd: |
148 | ; TODO - prove it works! |
||
149 | dec [ebx + SOCKET.wndsizeTimer] |
||
150 | jmp .next_socket |
||
1 | ha | 151 | |
907 | mikedld | 152 | .exit: |
153 | ret |
||
154 | endp |
||
1 | ha | 155 | |
156 | |||
157 | ;*************************************************************************** |
||
158 | ; Function |
||
159 | ; tcp_tx_handler |
||
160 | ; |
||
161 | ; Description |
||
162 | ; Handles queued TCP data |
||
163 | ; This is a kernel function, called by stack_handler |
||
164 | ; |
||
165 | ;*************************************************************************** |
||
907 | mikedld | 166 | |
167 | proc tcp_tx_handler stdcall |
||
1 | ha | 168 | ; decrement all resend buffers timers. If they |
169 | ; expire, queue them for sending, and restart the timer. |
||
170 | ; If the retries counter reach 0, delete the entry |
||
171 | |||
907 | mikedld | 172 | mov esi, resendQ |
173 | mov ecx, 0 |
||
1 | ha | 174 | |
907 | mikedld | 175 | .next_resendq: |
176 | cmp ecx, NUMRESENDENTRIES |
||
177 | je .exit ; None left |
||
178 | cmp dword[esi + 4], 0 |
||
179 | jne @f ; found one |
||
180 | inc ecx |
||
181 | add esi, 8 |
||
182 | jmp .next_resendq |
||
1 | ha | 183 | |
907 | mikedld | 184 | @@: ; we have one. decrement it's timer by 1 |
185 | dec word[esi + 2] |
||
186 | jz @f |
||
187 | inc ecx |
||
188 | add esi, 8 |
||
189 | jmp .next_resendq ; Timer not zero, so move on |
||
1 | ha | 190 | |
907 | mikedld | 191 | @@: |
192 | xor ebx, ebx |
||
193 | ; restart timer, and decrement retries |
||
194 | ; After the first resend, back of on next, by a factor of 5 |
||
195 | mov [esi + 2], word TCP_TIMEOUT * 5 |
||
196 | dec byte[esi + 1] |
||
197 | jnz @f |
||
1 | ha | 198 | |
907 | mikedld | 199 | ; retries now 0, so delete from queue |
200 | xchg [esi + 4], ebx |
||
1 | ha | 201 | |
907 | mikedld | 202 | @@: ; resend packet |
203 | pushad |
||
1 | ha | 204 | |
907 | mikedld | 205 | mov eax, EMPTY_QUEUE |
206 | call dequeue |
||
207 | cmp ax, NO_BUFFER |
||
208 | jne .tth004z |
||
1 | ha | 209 | |
907 | mikedld | 210 | ; TODO - try again in 10ms. |
211 | test ebx, ebx |
||
212 | jnz @f |
||
213 | mov [esi + 4], ebx |
||
1 | ha | 214 | |