Rev 1206 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1196 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3 | ;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;; |
||
4 | ;; Distributed under terms of the GNU General Public License ;; |
||
5 | ;; ;; |
||
6 | ;; TCP.INC ;; |
||
7 | ;; ;; |
||
8 | ;; Part of the tcp/ip network stack for KolibriOS ;; |
||
9 | ;; ;; |
||
10 | ;; Written by hidnplayr@kolibrios.org ;; |
||
11 | ;; ;; |
||
12 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
13 | ;; Version 2, June 1991 ;; |
||
14 | ;; ;; |
||
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
1159 | hidnplayr | 16 | |
1196 | hidnplayr | 17 | |
1206 | hidnplayr | 18 | $Revision: 1249 $ |
1159 | hidnplayr | 19 | |
20 | |||
21 | TCB_LISTEN equ 1 |
||
22 | TCB_SYN_SENT equ 2 |
||
23 | TCB_SYN_RECEIVED equ 3 |
||
24 | TCB_ESTABLISHED equ 4 |
||
25 | TCB_FIN_WAIT_1 equ 5 |
||
26 | TCB_FIN_WAIT_2 equ 6 |
||
27 | TCB_CLOSE_WAIT equ 7 |
||
28 | TCB_CLOSING equ 8 |
||
29 | TCB_LAST_ACK equ 9 |
||
30 | TCB_TIMED_WAIT equ 10 |
||
31 | TCB_CLOSED equ 11 |
||
32 | |||
1249 | hidnplayr | 33 | TH_FIN equ 1 shl 0 |
34 | TH_SYN equ 1 shl 1 |
||
35 | TH_RST equ 1 shl 2 |
||
36 | TH_PUSH equ 1 shl 3 |
||
37 | TH_ACK equ 1 shl 4 |
||
38 | TH_URG equ 1 shl 5 |
||
1159 | hidnplayr | 39 | |
1196 | hidnplayr | 40 | TWOMSL equ 10 ; # of secs to wait before closing socket |
1159 | hidnplayr | 41 | |
1196 | hidnplayr | 42 | TCP_RETRIES equ 5 ; Number of times to resend a Packet |
1249 | hidnplayr | 43 | TCP_TIMEOUT equ 10 ; resend if not replied to in 1/100 s |
1159 | hidnplayr | 44 | |
1196 | hidnplayr | 45 | TCP_QUEUE_SIZE equ 16 |
46 | |||
1249 | hidnplayr | 47 | |
1159 | hidnplayr | 48 | struct TCP_Packet |
49 | .SourcePort dw ? |
||
50 | .DestinationPort dw ? |
||
51 | .SequenceNumber dd ? |
||
52 | .AckNumber dd ? |
||
1196 | hidnplayr | 53 | .DataOffset db ? ; DataOffset[0-3 bits] and Reserved[4-7] |
54 | .Flags db ? ; Reserved[0-1 bits]|URG|ACK|PSH|RST|SYN|FIN |
||
1159 | hidnplayr | 55 | .Window dw ? |
56 | .Checksum dw ? |
||
57 | .UrgentPointer dw ? |
||
58 | .Options rb 3 |
||
59 | .Padding db ? |
||
60 | .Data: |
||
61 | ends |
||
62 | |||
1196 | hidnplayr | 63 | align 4 |
64 | uglobal |
||
65 | TCP_PACKETS_TX rd MAX_IP |
||
66 | TCP_PACKETS_RX rd MAX_IP |
||
1159 | hidnplayr | 67 | |
1249 | hidnplayr | 68 | TCP_IN_QUEUE rd (tcp_in_queue_entry.size*TCP_QUEUE_SIZE+queue.data)/4 |
69 | TCP_OUT_QUEUE dd ? |
||
70 | rd (tcp_out_queue_entry.size*TCP_QUEUE_SIZE)/4 |
||
1196 | hidnplayr | 71 | endg |
72 | |||
1249 | hidnplayr | 73 | align 4 |
74 | iglobal |
||
1196 | hidnplayr | 75 | |
1249 | hidnplayr | 76 | TCBStateHandler: |
1196 | hidnplayr | 77 | |
1249 | hidnplayr | 78 | dd stateTCB_LISTEN |
79 | dd stateTCB_SYN_SENT |
||
80 | dd stateTCB_SYN_RECEIVED |
||
81 | dd stateTCB_ESTABLISHED |
||
82 | dd stateTCB_FIN_WAIT_1 |
||
83 | dd stateTCB_FIN_WAIT_2 |
||
84 | dd stateTCB_CLOSE_WAIT |
||
85 | dd stateTCB_CLOSING |
||
86 | dd stateTCB_LAST_ACK |
||
87 | dd stateTCB_TIME_WAIT |
||
88 | dd stateTCB_CLOSED |
||
1196 | hidnplayr | 89 | |
1249 | hidnplayr | 90 | endg |
1196 | hidnplayr | 91 | |
1249 | hidnplayr | 92 | |
93 | macro inc_INET reg { |
||
94 | |||
95 | inc byte [reg + 0] |
||
96 | adc byte [reg + 1], 0 |
||
97 | adc byte [reg + 2], 0 |
||
98 | adc byte [reg + 3], 0 |
||
99 | |||
100 | } |
||
101 | |||
102 | |||
103 | macro add_INET reg { |
||
104 | |||
105 | rol ecx, 16 |
||
106 | adc byte [reg + 0], ch |
||
107 | adc byte [reg + 1], cl |
||
108 | rol ecx, 16 |
||
109 | adc byte [reg + 2], ch |
||
110 | adc byte [reg + 3], cl |
||
111 | |||
112 | } |
||
113 | |||
114 | |||
115 | |||
116 | |||
1196 | hidnplayr | 117 | ;----------------------------------------------------------------- |
118 | ; |
||
119 | ; TCP_init |
||
120 | ; |
||
121 | ; This function resets all TCP variables |
||
122 | ; |
||
123 | ; IN: / |
||
124 | ; OUT: / |
||
125 | ; |
||
126 | ;----------------------------------------------------------------- |
||
127 | |||
128 | align 4 |
||
129 | TCP_init: |
||
130 | |||
131 | xor eax, eax |
||
132 | mov edi, TCP_PACKETS_TX |
||
133 | mov ecx, 2*MAX_IP |
||
134 | rep stosd |
||
135 | |||
1249 | hidnplayr | 136 | init_queue TCP_IN_QUEUE |
137 | init_queue TCP_OUT_QUEUE |
||
1196 | hidnplayr | 138 | |
139 | ret |
||
140 | |||
141 | |||
142 | ;----------------------------------------------------------------- |
||
143 | ; |
||
1249 | hidnplayr | 144 | ; TCP_decrease_socket_ttls |
1159 | hidnplayr | 145 | ; |
1249 | hidnplayr | 146 | ; IN: / |
147 | ; OUT: / |
||
1159 | hidnplayr | 148 | ; |
1196 | hidnplayr | 149 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 150 | |
1196 | hidnplayr | 151 | align 4 |
1249 | hidnplayr | 152 | TCP_decrease_socket_ttls: |
1159 | hidnplayr | 153 | ; scan through all the sockets, decrementing active timers |
154 | |||
155 | mov ebx, net_sockets |
||
156 | |||
1249 | hidnplayr | 157 | cmp [ebx + SOCKET_head.NextPtr], 0 |
1159 | hidnplayr | 158 | je .exit |
159 | |||
160 | .next_socket: |
||
1249 | hidnplayr | 161 | mov ebx, [ebx + SOCKET_head.NextPtr] |
1159 | hidnplayr | 162 | or ebx, ebx |
163 | jz .exit |
||
164 | |||
1249 | hidnplayr | 165 | cmp [ebx + SOCKET_head.Type], IP_PROTO_TCP |
166 | jne .next_socket |
||
167 | |||
1196 | hidnplayr | 168 | ; 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] |
1159 | hidnplayr | 169 | |
1249 | hidnplayr | 170 | cmp [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBTimer], 0 |
1159 | hidnplayr | 171 | jne .decrement_tcb |
1249 | hidnplayr | 172 | cmp [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.wndsizeTimer], 0 |
1159 | hidnplayr | 173 | jne .decrement_wnd |
174 | jmp .next_socket |
||
175 | |||
176 | .decrement_tcb: |
||
177 | ; decrement it, delete socket if TCB timer = 0 & socket in timewait state |
||
1249 | hidnplayr | 178 | dec [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBTimer] |
1159 | hidnplayr | 179 | jnz .next_socket |
180 | |||
1249 | hidnplayr | 181 | cmp [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.TCBState], TCB_TIMED_WAIT |
1159 | hidnplayr | 182 | jne .next_socket |
183 | |||
1249 | hidnplayr | 184 | push [ebx + SOCKET_head.PrevPtr] |
1159 | hidnplayr | 185 | stdcall net_socket_free, ebx |
186 | pop ebx |
||
187 | jmp .next_socket |
||
188 | |||
189 | .decrement_wnd: |
||
190 | ; TODO - prove it works! |
||
1249 | hidnplayr | 191 | dec [ebx + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.wndsizeTimer] |
1159 | hidnplayr | 192 | jmp .next_socket |
193 | |||
194 | .exit: |
||
195 | ret |
||
196 | |||
197 | |||
1249 | hidnplayr | 198 | |
199 | ;----------------------------------------------------------------- |
||
1159 | hidnplayr | 200 | ; |
1249 | hidnplayr | 201 | ; TCP_send_queued: |
1159 | hidnplayr | 202 | ; |
1249 | hidnplayr | 203 | ; Decreases 'ttl' of tcp packets queued. |
204 | ; if 'ttl' reaches 0, resend the packet and decrease 'retries' |
||
205 | ; if 'retries' reaches zero, remove the queued packet |
||
206 | ; |
||
207 | ; IN: / |
||
208 | ; OUT: / |
||
209 | ; |
||
210 | ;----------------------------------------------------------------- |
||
1159 | hidnplayr | 211 | |
1196 | hidnplayr | 212 | align 4 |
1249 | hidnplayr | 213 | TCP_send_queued: |
1159 | hidnplayr | 214 | |