Rev 5363 | Rev 6011 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3545 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
5363 | yogev_ezra | 3 | ;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;; |
3545 | hidnplayr | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;; Part of the TCP/IP network stack for KolibriOS ;; |
||
7 | ;; ;; |
||
8 | ;; Written by hidnplayr@kolibrios.org ;; |
||
9 | ;; ;; |
||
10 | ;; Based on the code of 4.4BSD ;; |
||
11 | ;; ;; |
||
12 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
13 | ;; Version 2, June 1991 ;; |
||
14 | ;; ;; |
||
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
16 | |||
4850 | mario79 | 17 | $Revision: 5442 $ |
3545 | hidnplayr | 18 | |
4850 | mario79 | 19 | |
3545 | hidnplayr | 20 | ;------------------------- |
21 | ; |
||
22 | ; TCP_usrclose |
||
23 | ; |
||
24 | ; Move connection to next state, based on process close. |
||
25 | ; |
||
26 | ; IN: eax = socket ptr |
||
27 | ; |
||
28 | ;------------------------- |
||
29 | align 4 |
||
30 | TCP_usrclosed: |
||
31 | |||
3556 | hidnplayr | 32 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_usrclosed: %x\n", eax |
3545 | hidnplayr | 33 | |
34 | push ebx |
||
35 | mov ebx, [eax + TCP_SOCKET.t_state] |
||
36 | mov ebx, dword [.switch + ebx*4] |
||
37 | jmp ebx |
||
38 | |||
39 | .switch: |
||
40 | |||
41 | dd .close ; TCPS_CLOSED |
||
42 | dd .close ; TCPS_LISTEN |
||
43 | dd .close ; TCPS_SYN_SENT |
||
44 | dd .wait1 ; TCPS_SYN_RECEIVED |
||
45 | dd .wait1 ; TCPS_ESTABLISHED |
||
46 | dd .last_ack ; TCPS_CLOSE_WAIT |
||
47 | dd .ret ; TCPS_FIN_WAIT_1 |
||
48 | dd .ret ; TCPS_CLOSING |
||
49 | dd .ret ; TCPS_LAST_ACK |
||
50 | dd .disc ; TCPS_FIN_WAIT_2 |
||
51 | dd .disc ; TCPS_TIMED_WAIT |
||
52 | |||
53 | |||
54 | .close: |
||
55 | mov [eax + TCP_SOCKET.t_state], TCPS_CLOSED |
||
56 | call TCP_close |
||
57 | pop ebx |
||
58 | ret |
||
59 | |||
60 | .wait1: |
||
61 | mov [eax + TCP_SOCKET.t_state], TCPS_FIN_WAIT_1 |
||
62 | pop ebx |
||
63 | ret |
||
64 | |||
65 | .last_ack: |
||
66 | mov [eax + TCP_SOCKET.t_state], TCPS_LAST_ACK |
||
67 | pop ebx |
||
68 | ret |
||
69 | |||
70 | .disc: |
||
71 | call SOCKET_is_disconnected |
||
72 | .ret: |
||
73 | pop ebx |
||
74 | ret |
||
75 | |||
76 | |||
4030 | hidnplayr | 77 | ;------------------------- |
78 | ; |
||
79 | ; TCP_connect |
||
80 | ; |
||
81 | ; IN: eax = socket ptr |
||
82 | ; OUT: eax = 0 ok / -1 error |
||
83 | ; ebx = error code |
||
84 | ; |
||
85 | ;------------------------- |
||
86 | align 4 |
||
87 | TCP_connect: |
||
3545 | hidnplayr | 88 | |
4030 | hidnplayr | 89 | test [eax + SOCKET.state], SS_ISCONNECTED |
90 | jnz .eisconn |
||
3545 | hidnplayr | 91 | |
5442 | hidnplayr | 92 | inc [TCPS_connattempt] ; update stats |
93 | |||
4035 | hidnplayr | 94 | push eax edx |
4030 | hidnplayr | 95 | lea ecx, [eax + SOCKET.mutex] |
96 | call mutex_lock |
||
4035 | hidnplayr | 97 | pop edx eax |
4030 | hidnplayr | 98 | |
99 | ; Fill in local IP |
||
100 | cmp [eax + IP_SOCKET.LocalIP], 0 |
||
101 | jne @f |
||
102 | push [IP_LIST + 4] ; FIXME: use correct local IP |
||
103 | pop [eax + IP_SOCKET.LocalIP] |
||
104 | |||
105 | ; Fill in remote port and IP |
||
106 | pushw [edx + 2] |
||
107 | pop [eax + TCP_SOCKET.RemotePort] |
||
108 | |||
109 | pushd [edx + 4] |
||
110 | pop [eax + IP_SOCKET.RemoteIP] |
||
111 | |||
112 | ; Find a local port, if user didnt define one |
||
113 | cmp [eax + TCP_SOCKET.LocalPort], 0 |
||
114 | jne @f |
||
115 | call SOCKET_find_port |
||
116 | @@: |
||
117 | |||
118 | ; Start the TCP sequence |
||
119 | mov [eax + TCP_SOCKET.timer_persist], 0 |
||
120 | mov [eax + TCP_SOCKET.t_state], TCPS_SYN_SENT |
||
121 | |||
122 | push [TCP_sequence_num] |
||
123 | add [TCP_sequence_num], 6400 |
||
124 | pop [eax + TCP_SOCKET.ISS] |
||
125 | mov [eax + TCP_SOCKET.timer_keepalive], TCP_time_keep_init |
||
126 | |||
127 | TCP_sendseqinit eax |
||
128 | |||
129 | mov ebx, eax |
||
130 | lea eax, [ebx + STREAM_SOCKET.snd] |
||
131 | call SOCKET_ring_create |
||
132 | test eax, eax |
||
133 | jz .nomem |
||
134 | |||
135 | lea eax, [ebx + STREAM_SOCKET.rcv] |
||
136 | call SOCKET_ring_create |
||
137 | test eax, eax |
||
138 | jz .nomem |
||
139 | |||
140 | push ebx |
||
141 | lea ecx, [ebx + SOCKET.mutex] |
||
142 | call mutex_unlock |
||
143 | pop eax |
||
144 | |||
145 | call SOCKET_is_connecting |
||
146 | |||
147 | ; Now send the SYN packet to remote end |
||
148 | push eax |
||
149 | call TCP_output |
||
150 | pop eax |
||
151 | |||
152 | .block: |
||
153 | test [eax + SOCKET.options], SO_NONBLOCK |
||
154 | jz .waitforit |
||
155 | |||
156 | xor eax, eax |
||
157 | dec eax |
||
158 | mov ebx, EINPROGRESS |
||
159 | ret |
||
160 | |||
161 | .nomem: |
||
162 | xor eax, eax |
||
163 | dec eax |
||
164 | mov ebx, ENOMEM |
||
165 | ret |
||
166 | |||
167 | .eisconn: |
||
168 | xor eax, eax |
||
169 | dec eax |
||
170 | mov ebx, EISCONN |
||
171 | ret |
||
172 | |||
173 | .waitforit: |
||
174 | push eax |
||
175 | stdcall timer_hs, TCP_time_connect, 0, .timeout, eax |
||
176 | pop ebx |
||
177 | mov [ebx + TCP_SOCKET.timer_connect], eax |
||
178 | mov eax, ebx |
||
179 | |||
180 | .loop: |
||
181 | cmp [eax + SOCKET.errorcode], 0 |
||
182 | jne .fail |
||
183 | cmp [eax + TCP_SOCKET.t_state], TCPS_ESTABLISHED |
||
184 | je .established |
||
185 | |||
186 | call SOCKET_block |
||
187 | jmp .loop |
||
188 | |||
189 | .timeout: |
||
190 | mov eax, [esp+4] |
||
191 | mov [eax + SOCKET.errorcode], ETIMEDOUT |
||
192 | and [eax + SOCKET.state], not SS_ISCONNECTING |
||
4528 | hidnplayr | 193 | call SOCKET_notify |
4030 | hidnplayr | 194 | ret 4 |
195 | |||
196 | .fail: |
||
197 | mov ebx, [eax + SOCKET.errorcode] |
||
198 | mov [eax + SOCKET.errorcode], 0 ; Clear the error, we only need to send it to the caller once |
||
199 | xor eax, eax |
||
200 | dec eax |
||
201 | ret |
||
202 | |||
203 | .established: |
||
204 | stdcall cancel_timer_hs, [eax + TCP_SOCKET.timer_connect] |
||
205 | |||
206 | xor eax, eax |
||
3545 | hidnplayr | 207 | ret |