Rev 6476 | Rev 6908 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 6476 | Rev 6907 | ||
---|---|---|---|
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2016. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2016. All rights reserved. ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
5 | ;; ;; |
6 | ;; Part of the TCP/IP network stack for KolibriOS ;; |
6 | ;; Part of the TCP/IP network stack for KolibriOS ;; |
7 | ;; ;; |
7 | ;; ;; |
8 | ;; Written by hidnplayr@kolibrios.org, ;; |
8 | ;; Written by hidnplayr@kolibrios.org, ;; |
9 | ;; and Clevermouse. ;; |
9 | ;; and Clevermouse. ;; |
10 | ;; ;; |
10 | ;; ;; |
11 | ;; Based on code by mike.dld ;; |
11 | ;; Based on code by mike.dld ;; |
12 | ;; ;; |
12 | ;; ;; |
13 | ;; GNU GENERAL PUBLIC LICENSE ;; |
13 | ;; GNU GENERAL PUBLIC LICENSE ;; |
14 | ;; Version 2, June 1991 ;; |
14 | ;; Version 2, June 1991 ;; |
15 | ;; ;; |
15 | ;; ;; |
16 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
16 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
17 | 17 | ||
18 | $Revision: 6476 $ |
18 | $Revision: 6907 $ |
19 | 19 | ||
20 | struct SOCKET |
20 | struct SOCKET |
21 | 21 | ||
22 | NextPtr dd ? ; pointer to next socket in list |
22 | NextPtr dd ? ; pointer to next socket in list |
23 | PrevPtr dd ? ; pointer to previous socket in list |
23 | PrevPtr dd ? ; pointer to previous socket in list |
24 | Number dd ? ; socket number |
24 | Number dd ? ; socket number |
25 | 25 | ||
26 | mutex MUTEX |
26 | mutex MUTEX |
27 | 27 | ||
28 | PID dd ? ; process ID |
28 | PID dd ? ; process ID |
29 | TID dd ? ; thread ID |
29 | TID dd ? ; thread ID |
30 | Domain dd ? ; INET4/INET6/LOCAL/.. |
30 | Domain dd ? ; INET4/INET6/LOCAL/.. |
31 | Type dd ? ; RAW/STREAM/DGRAM |
31 | Type dd ? ; RAW/STREAM/DGRAM |
32 | Protocol dd ? ; UDP/TCP/ARP/ICMP |
32 | Protocol dd ? ; UDP/TCP/ARP/ICMP |
33 | errorcode dd ? |
33 | errorcode dd ? |
34 | device dd ? ; device pointer, paired socket pointer if it's a local socket |
34 | device dd ? ; device pointer, paired socket pointer if it's a local socket |
35 | 35 | ||
36 | options dd ? |
36 | options dd ? |
37 | state dd ? |
37 | state dd ? |
38 | backlog dw ? ; number of incoming connections that can be queued |
38 | backlog dw ? ; number of incoming connections that can be queued |
39 | 39 | ||
40 | snd_proc dd ? |
40 | snd_proc dd ? |
41 | rcv_proc dd ? |
41 | rcv_proc dd ? |
42 | connect_proc dd ? |
42 | connect_proc dd ? |
43 | 43 | ||
44 | ends |
44 | ends |
45 | 45 | ||
46 | struct IP_SOCKET SOCKET |
46 | struct IP_SOCKET SOCKET |
47 | 47 | ||
48 | LocalIP rd 4 ; network byte order |
48 | LocalIP rd 4 ; network byte order |
49 | RemoteIP rd 4 ; network byte order |
49 | RemoteIP rd 4 ; network byte order |
50 | ttl db ? |
50 | ttl db ? |
51 | rb 3 ; align |
51 | rb 3 ; align |
52 | 52 | ||
53 | ends |
53 | ends |
54 | 54 | ||
55 | struct TCP_SOCKET IP_SOCKET |
55 | struct TCP_SOCKET IP_SOCKET |
56 | 56 | ||
57 | LocalPort dw ? ; network byte order |
57 | LocalPort dw ? ; network byte order |
58 | RemotePort dw ? ; network byte order |
58 | RemotePort dw ? ; network byte order |
59 | 59 | ||
60 | t_state dd ? ; TCB state |
60 | t_state dd ? ; TCB state |
61 | t_rxtshift db ? |
61 | t_rxtshift db ? |
62 | rb 3 ; align |
62 | rb 3 ; align |
63 | t_rxtcur dd ? |
63 | t_rxtcur dd ? |
64 | t_dupacks dd ? |
64 | t_dupacks dd ? |
65 | t_maxseg dd ? |
65 | t_maxseg dd ? |
66 | t_force dd ? |
66 | t_force dd ? |
67 | t_flags dd ? |
67 | t_flags dd ? |
68 | 68 | ||
69 | ;--------------- |
69 | ;--------------- |
70 | ; RFC783 page 21 |
70 | ; RFC783 page 21 |
71 | 71 | ||
72 | ; send sequence |
72 | ; send sequence |
73 | SND_UNA dd ? ; sequence number of unack'ed sent Packets |
73 | SND_UNA dd ? ; sequence number of unack'ed sent Packets |
74 | SND_NXT dd ? ; next send sequence number to use |
74 | SND_NXT dd ? ; next send sequence number to use |
75 | SND_UP dd ? ; urgent pointer |
75 | SND_UP dd ? ; urgent pointer |
76 | SND_WL1 dd ? ; the sequence number of the last segment used to update the send window |
76 | SND_WL1 dd ? ; the sequence number of the last segment used to update the send window |
77 | SND_WL2 dd ? ; the acknowledgment number of the last segment used to update the send window |
77 | SND_WL2 dd ? ; the acknowledgment number of the last segment used to update the send window |
78 | ISS dd ? ; initial send sequence number |
78 | ISS dd ? ; initial send sequence number |
79 | SND_WND dd ? ; send window |
79 | SND_WND dd ? ; send window |
80 | 80 | ||
81 | ; receive sequence |
81 | ; receive sequence |
82 | RCV_WND dd ? ; receive window |
82 | RCV_WND dd ? ; receive window |
83 | RCV_NXT dd ? ; next receive sequence number to use |
83 | RCV_NXT dd ? ; next receive sequence number to use |
84 | RCV_UP dd ? ; urgent pointer |
84 | RCV_UP dd ? ; urgent pointer |
85 | IRS dd ? ; initial receive sequence number |
85 | IRS dd ? ; initial receive sequence number |
86 | 86 | ||
87 | ;--------------------- |
87 | ;--------------------- |
88 | ; Additional variables |
88 | ; Additional variables |
89 | 89 | ||
90 | ; receive variables |
90 | ; receive variables |
91 | RCV_ADV dd ? |
91 | RCV_ADV dd ? |
92 | 92 | ||
93 | ; retransmit variables |
93 | ; retransmit variables |
94 | SND_MAX dd ? |
94 | SND_MAX dd ? |
95 | 95 | ||
96 | ; congestion control |
96 | ; congestion control |
97 | SND_CWND dd ? ; congestion window |
97 | SND_CWND dd ? ; congestion window |
98 | SND_SSTHRESH dd ? ; slow start threshold |
98 | SND_SSTHRESH dd ? ; slow start threshold |
99 | 99 | ||
100 | ;---------------------- |
100 | ;---------------------- |
101 | ; Transmit timing stuff |
101 | ; Transmit timing stuff |
102 | t_idle dd ? |
102 | t_idle dd ? |
103 | t_rtt dd ? ; round trip time |
103 | t_rtt dd ? ; round trip time |
104 | t_rtseq dd ? |
104 | t_rtseq dd ? |
105 | t_srtt dd ? ; smoothed round trip time |
105 | t_srtt dd ? ; smoothed round trip time |
106 | t_rttvar dd ? |
106 | t_rttvar dd ? |
107 | t_rttmin dd ? |
107 | t_rttmin dd ? |
108 | max_sndwnd dd ? |
108 | max_sndwnd dd ? |
109 | 109 | ||
110 | ;----------------- |
110 | ;----------------- |
111 | ; Out-of-band data |
111 | ; Out-of-band data |
112 | t_oobflags dd ? |
112 | t_oobflags dd ? |
113 | t_iobc dd ? |
113 | t_iobc dd ? |
114 | t_softerror dd ? |
114 | t_softerror dd ? |
115 | 115 | ||
116 | 116 | ||
117 | ;--------- |
117 | ;--------- |
118 | ; RFC 1323 ; the order of next 4 elements may not change |
118 | ; RFC 1323 ; the order of next 4 elements may not change |
119 | 119 | ||
120 | SND_SCALE db ? |
120 | SND_SCALE db ? |
121 | RCV_SCALE db ? |
121 | RCV_SCALE db ? |
122 | requested_s_scale db ? |
122 | requested_s_scale db ? |
123 | request_r_scale db ? |
123 | request_r_scale db ? |
124 | 124 | ||
125 | ts_recent dd ? ; a copy of the most-recent valid timestamp from the other end |
125 | ts_recent dd ? ; a copy of the most-recent valid timestamp from the other end |
126 | ts_recent_age dd ? |
126 | ts_recent_age dd ? |
127 | last_ack_sent dd ? |
127 | last_ack_sent dd ? |
128 | 128 | ||
129 | 129 | ||
130 | ;------- |
130 | ;------- |
131 | ; Timers |
131 | ; Timers |
132 | timer_flags dd ? |
132 | timer_flags dd ? |
133 | timer_retransmission dd ? ; rexmt |
133 | timer_retransmission dd ? ; rexmt |
134 | timer_persist dd ? |
134 | timer_persist dd ? |
135 | timer_keepalive dd ? ; keepalive/syn timeout |
135 | timer_keepalive dd ? ; keepalive/syn timeout |
136 | timer_timed_wait dd ? ; also used as 2msl timer |
136 | timer_timed_wait dd ? ; also used as 2msl timer |
137 | timer_connect dd ? |
137 | timer_connect dd ? |
138 | 138 | ||
139 | ; extra |
139 | ; extra |
140 | 140 | ||
141 | ts_ecr dd ? ; timestamp echo reply |
141 | ts_ecr dd ? ; timestamp echo reply |
142 | ts_val dd ? |
142 | ts_val dd ? |
143 | 143 | ||
144 | seg_next dd ? ; re-assembly queue |
144 | seg_next dd ? ; re-assembly queue |
145 | 145 | ||
146 | ends |
146 | ends |
147 | 147 | ||
148 | struct UDP_SOCKET IP_SOCKET |
148 | struct UDP_SOCKET IP_SOCKET |
149 | 149 | ||
150 | LocalPort dw ? ; in network byte order |
150 | LocalPort dw ? ; in network byte order |
151 | RemotePort dw ? ; in network byte order |
151 | RemotePort dw ? ; in network byte order |
152 | 152 | ||
153 | ends |
153 | ends |
154 | 154 | ||
155 | struct RING_BUFFER |
155 | struct RING_BUFFER |
156 | 156 | ||
157 | mutex MUTEX |
157 | mutex MUTEX |
158 | start_ptr dd ? ; Pointer to start of buffer |
158 | start_ptr dd ? ; Pointer to start of buffer |
159 | end_ptr dd ? ; pointer to end of buffer |
159 | end_ptr dd ? ; pointer to end of buffer |
160 | read_ptr dd ? ; Read pointer |
160 | read_ptr dd ? ; Read pointer |
161 | write_ptr dd ? ; Write pointer |
161 | write_ptr dd ? ; Write pointer |
162 | size dd ? ; Number of bytes buffered |
162 | size dd ? ; Number of bytes buffered |
163 | 163 | ||
164 | ends |
164 | ends |
165 | 165 | ||
166 | struct STREAM_SOCKET TCP_SOCKET |
166 | struct STREAM_SOCKET TCP_SOCKET |
167 | 167 | ||
168 | rcv RING_BUFFER |
168 | rcv RING_BUFFER |
169 | snd RING_BUFFER |
169 | snd RING_BUFFER |
170 | 170 | ||
171 | ends |
171 | ends |
172 | 172 | ||
173 | struct socket_queue_entry |
173 | struct socket_queue_entry |
174 | 174 | ||
175 | data_ptr dd ? |
175 | data_ptr dd ? |
176 | data_size dd ? |
176 | data_size dd ? |
177 | buf_ptr dd ? |
177 | buf_ptr dd ? |
178 | 178 | ||
179 | ends |
179 | ends |
180 | 180 | ||
181 | struct socket_options |
181 | struct socket_options |
182 | 182 | ||
183 | level dd ? |
183 | level dd ? |
184 | optname dd ? |
184 | optname dd ? |
185 | optlen dd ? |
185 | optlen dd ? |
186 | optval dd ? |
186 | optval dd ? |
187 | 187 | ||
188 | ends |
188 | ends |
189 | 189 | ||
190 | SOCKET_STRUCT_SIZE = 4096 ; in bytes |
190 | SOCKET_STRUCT_SIZE = 4096 ; in bytes |
191 | 191 | ||
192 | SOCKET_QUEUE_SIZE = 10 ; maximum number of incoming packets queued for 1 socket |
192 | SOCKET_QUEUE_SIZE = 10 ; maximum number of incoming packets queued for 1 socket |
193 | ; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start |
193 | ; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start |
194 | SOCKET_QUEUE_LOCATION = (SOCKET_STRUCT_SIZE - SOCKET_QUEUE_SIZE*sizeof.socket_queue_entry - sizeof.queue) |
194 | SOCKET_QUEUE_LOCATION = (SOCKET_STRUCT_SIZE - SOCKET_QUEUE_SIZE*sizeof.socket_queue_entry - sizeof.queue) |
195 | 195 | ||
196 | uglobal |
196 | uglobal |
197 | align 4 |
197 | align 4 |
198 | 198 | ||
199 | net_sockets rd 4 |
199 | net_sockets rd 4 |
200 | last_socket_num dd ? |
200 | last_socket_num dd ? |
201 | last_UDP_port dw ? ; last used ephemeral port |
201 | last_UDP_port dw ? ; last used ephemeral port |
202 | last_TCP_port dw ? ; |
202 | last_TCP_port dw ? ; |
203 | socket_mutex MUTEX |
203 | socket_mutex MUTEX |
204 | 204 | ||
205 | endg |
205 | endg |
206 | 206 | ||
207 | 207 | ||
208 | ;-----------------------------------------------------------------; |
208 | ;-----------------------------------------------------------------; |
209 | ; ; |
209 | ; ; |
210 | ; socket_init ; |
210 | ; socket_init ; |
211 | ; ; |
211 | ; ; |
212 | ;-----------------------------------------------------------------; |
212 | ;-----------------------------------------------------------------; |
213 | macro socket_init { |
213 | macro socket_init { |
214 | 214 | ||
215 | xor eax, eax |
215 | xor eax, eax |
216 | mov edi, net_sockets |
216 | mov edi, net_sockets |
217 | mov ecx, 5 |
217 | mov ecx, 5 |
218 | rep stosd |
218 | rep stosd |
219 | 219 | ||
220 | @@: |
220 | @@: |
221 | pseudo_random eax |
221 | pseudo_random eax |
222 | cmp ax, EPHEMERAL_PORT_MIN |
222 | cmp ax, EPHEMERAL_PORT_MIN |
223 | jb @r |
223 | jb @r |
224 | cmp ax, EPHEMERAL_PORT_MAX |
224 | cmp ax, EPHEMERAL_PORT_MAX |
225 | ja @r |
225 | ja @r |
226 | xchg al, ah |
226 | xchg al, ah |
227 | mov [last_UDP_port], ax |
227 | mov [last_UDP_port], ax |
228 | 228 | ||
229 | @@: |
229 | @@: |
230 | pseudo_random eax |
230 | pseudo_random eax |
231 | cmp ax, EPHEMERAL_PORT_MIN |
231 | cmp ax, EPHEMERAL_PORT_MIN |
232 | jb @r |
232 | jb @r |
233 | cmp ax, EPHEMERAL_PORT_MAX |
233 | cmp ax, EPHEMERAL_PORT_MAX |
234 | ja @r |
234 | ja @r |
235 | xchg al, ah |
235 | xchg al, ah |
236 | mov [last_TCP_port], ax |
236 | mov [last_TCP_port], ax |
237 | 237 | ||
238 | mov ecx, socket_mutex |
238 | mov ecx, socket_mutex |
239 | call mutex_init |
239 | call mutex_init |
240 | 240 | ||
241 | } |
241 | } |
242 | 242 | ||
243 | ;-----------------------------------------------------------------; |
243 | ;-----------------------------------------------------------------; |
244 | ; ; |
244 | ; ; |
245 | ; Sockets API (system function 75) ; |
245 | ; Sockets API (system function 75) ; |
246 | ; ; |
246 | ; ; |
247 | ;-----------------------------------------------------------------; |
247 | ;-----------------------------------------------------------------; |
248 | align 4 |
248 | align 4 |
249 | sys_socket: |
249 | sys_socket: |
250 | 250 | ||
251 | mov dword[esp+20], 0 ; Set error code to 0 |
251 | mov dword[esp+20], 0 ; Set error code to 0 |
252 | 252 | ||
253 | cmp ebx, 255 |
253 | cmp ebx, 255 |
254 | jz socket_debug |
254 | jz socket_debug |
255 | 255 | ||
256 | cmp ebx, .number |
256 | cmp ebx, .number |
257 | ja .error |
257 | ja .error |
258 | jmp dword [.table + 4*ebx] |
258 | jmp dword [.table + 4*ebx] |
259 | 259 | ||
260 | .table: |
260 | .table: |
261 | dd socket_open ; 0 |
261 | dd socket_open ; 0 |
262 | dd socket_close ; 1 |
262 | dd socket_close ; 1 |
263 | dd socket_bind ; 2 |
263 | dd socket_bind ; 2 |
264 | dd socket_listen ; 3 |
264 | dd socket_listen ; 3 |
265 | dd socket_connect ; 4 |
265 | dd socket_connect ; 4 |
266 | dd socket_accept ; 5 |
266 | dd socket_accept ; 5 |
267 | dd socket_send ; 6 |
267 | dd socket_send ; 6 |
268 | dd socket_receive ; 7 |
268 | dd socket_receive ; 7 |
269 | dd socket_set_opt ; 8 |
269 | dd socket_set_opt ; 8 |
270 | dd socket_get_opt ; 9 |
270 | dd socket_get_opt ; 9 |
271 | dd socket_pair ; 10 |
271 | dd socket_pair ; 10 |
272 | .number = ($ - .table) / 4 - 1 |
272 | .number = ($ - .table) / 4 - 1 |
273 | 273 | ||
274 | .error: |
274 | .error: |
275 | mov dword[esp+32], -1 |
275 | mov dword[esp+32], -1 |
276 | mov dword[esp+20], EINVAL |
276 | mov dword[esp+20], EINVAL |
277 | 277 | ||
278 | ret |
278 | ret |
279 | 279 | ||
280 | ;-----------------------------------------------------------------; |
280 | ;-----------------------------------------------------------------; |
281 | ; ; |
281 | ; ; |
282 | ; socket_open: Create a new socket. ; |
282 | ; socket_open: Create a new socket. ; |
283 | ; ; |
283 | ; ; |
284 | ; IN: ecx = domain ; |
284 | ; IN: ecx = domain ; |
285 | ; edx = type ; |
285 | ; edx = type ; |
286 | ; esi = protocol ; |
286 | ; esi = protocol ; |
287 | ; ; |
287 | ; ; |
288 | ; OUT: eax = socket number ; |
288 | ; OUT: eax = socket number ; |
289 | ; eax = -1 on error ; |
289 | ; eax = -1 on error ; |
290 | ; ebx = errorcode on error ; |
290 | ; ebx = errorcode on error ; |
291 | ; ; |
291 | ; ; |
292 | ;-----------------------------------------------------------------; |
292 | ;-----------------------------------------------------------------; |
293 | align 4 |
293 | align 4 |
294 | socket_open: |
294 | socket_open: |
295 | 295 | ||
296 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_open: domain=%u type=%u protocol=%x ", ecx, edx, esi |
296 | DEBUGF 1, "SOCKET_open: domain=%u type=%u protocol=%x\n ", ecx, edx, esi |
297 | 297 | ||
298 | push ecx edx esi |
298 | push ecx edx esi |
299 | call socket_alloc |
299 | call socket_alloc |
300 | pop esi edx ecx |
300 | pop esi edx ecx |
301 | test eax, eax |
301 | test eax, eax |
302 | jz .nobuffs |
302 | jz .nobuffs |
303 | 303 | ||
304 | mov [esp+32], edi ; return socketnumber |
304 | mov [esp+32], edi ; return socketnumber |
305 | DEBUGF DEBUG_NETWORK_VERBOSE, "socknum=%u\n", edi |
305 | DEBUGF DEBUG_NETWORK_VERBOSE, "socknum=%u\n", edi |
306 | 306 | ||
307 | test edx, SO_NONBLOCK |
307 | test edx, SO_NONBLOCK |
308 | jz @f |
308 | jz @f |
309 | or [eax + SOCKET.options], SO_NONBLOCK |
309 | or [eax + SOCKET.options], SO_NONBLOCK |
310 | and edx, not SO_NONBLOCK |
310 | and edx, not SO_NONBLOCK |
311 | @@: |
311 | @@: |
312 | 312 | ||
313 | mov [eax + SOCKET.Domain], ecx |
313 | mov [eax + SOCKET.Domain], ecx |
314 | mov [eax + SOCKET.Type], edx |
314 | mov [eax + SOCKET.Type], edx |
315 | mov [eax + SOCKET.Protocol], esi |
315 | mov [eax + SOCKET.Protocol], esi |
316 | mov [eax + SOCKET.connect_proc], connect_notsupp |
316 | mov [eax + SOCKET.connect_proc], connect_notsupp |
317 | 317 | ||
318 | cmp ecx, AF_INET4 |
318 | cmp ecx, AF_INET4 |
319 | jne .no_inet4 |
319 | jne .no_inet4 |
320 | 320 | ||
321 | mov [eax + IP_SOCKET.ttl], 128 |
321 | mov [eax + IP_SOCKET.ttl], 128 |
322 | 322 | ||
323 | cmp edx, SOCK_DGRAM |
323 | cmp edx, SOCK_DGRAM |
324 | je .udp |
324 | je .udp |
325 | 325 | ||
326 | cmp edx, SOCK_STREAM |
326 | cmp edx, SOCK_STREAM |
327 | je .tcp |
327 | je .tcp |
328 | 328 | ||
329 | cmp edx, SOCK_RAW |
329 | cmp edx, SOCK_RAW |
330 | je .raw |
330 | je .raw |
331 | 331 | ||
332 | .no_inet4: |
332 | .no_inet4: |
333 | cmp ecx, AF_PPP |
333 | cmp ecx, AF_PPP |
334 | jne .no_ppp |
334 | jne .no_ppp |
335 | 335 | ||
336 | cmp esi, PPP_PROTO_ETHERNET |
336 | cmp esi, PPP_PROTO_ETHERNET |
337 | je .pppoe |
337 | je .pppoe |
338 | 338 | ||
339 | .no_ppp: |
339 | .no_ppp: |
340 | .unsupported: |
340 | .unsupported: |
341 | push eax |
341 | push eax |
342 | call socket_free |
342 | call socket_free |
343 | pop eax |
343 | pop eax |
344 | mov dword[esp+20], EOPNOTSUPP |
344 | mov dword[esp+20], EOPNOTSUPP |
345 | mov dword[esp+32], -1 |
345 | mov dword[esp+32], -1 |
346 | ret |
346 | ret |
347 | 347 | ||
348 | .nobuffs: |
348 | .nobuffs: |
349 | mov dword[esp+20], ENOBUFS |
349 | mov dword[esp+20], ENOBUFS |
350 | mov dword[esp+32], -1 |
350 | mov dword[esp+32], -1 |
351 | ret |
351 | ret |
352 | 352 | ||
353 | .raw: |
353 | .raw: |
354 | test esi, esi ; IP_PROTO_IP |
354 | test esi, esi ; IP_PROTO_IP |
355 | jz .raw_ip |
355 | jz .raw_ip |
356 | 356 | ||
357 | cmp esi, IP_PROTO_ICMP |
357 | cmp esi, IP_PROTO_ICMP |
358 | je .raw_icmp |
358 | je .raw_icmp |
359 | 359 | ||
360 | jmp .unsupported |
360 | jmp .unsupported |
361 | 361 | ||
362 | align 4 |
362 | align 4 |
363 | .udp: |
363 | .udp: |
364 | push eax |
364 | push eax |
365 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue |
365 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue |
366 | pop eax |
366 | pop eax |
367 | 367 | ||
368 | mov [eax + SOCKET.Protocol], IP_PROTO_UDP |
368 | mov [eax + SOCKET.Protocol], IP_PROTO_UDP |
369 | mov [eax + SOCKET.snd_proc], socket_send_udp |
369 | mov [eax + SOCKET.snd_proc], socket_send_udp |
370 | mov [eax + SOCKET.rcv_proc], socket_receive_dgram |
370 | mov [eax + SOCKET.rcv_proc], socket_receive_dgram |
371 | mov [eax + SOCKET.connect_proc], udp_connect |
371 | mov [eax + SOCKET.connect_proc], udp_connect |
372 | ret |
372 | ret |
373 | 373 | ||
374 | align 4 |
374 | align 4 |
375 | .tcp: |
375 | .tcp: |
376 | mov [eax + SOCKET.Protocol], IP_PROTO_TCP |
376 | mov [eax + SOCKET.Protocol], IP_PROTO_TCP |
377 | mov [eax + SOCKET.snd_proc], socket_send_tcp |
377 | mov [eax + SOCKET.snd_proc], socket_send_tcp |
378 | mov [eax + SOCKET.rcv_proc], socket_receive_stream |
378 | mov [eax + SOCKET.rcv_proc], socket_receive_stream |
379 | mov [eax + SOCKET.connect_proc], tcp_connect |
379 | mov [eax + SOCKET.connect_proc], tcp_connect |
380 | 380 | ||
381 | tcp_init_socket eax |
381 | tcp_init_socket eax |
382 | ret |
382 | ret |
383 | 383 | ||
384 | 384 | ||
385 | align 4 |
385 | align 4 |
386 | .raw_ip: |
386 | .raw_ip: |
387 | push eax |
387 | push eax |
388 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue |
388 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue |
389 | pop eax |
389 | pop eax |
390 | 390 | ||
391 | mov [eax + SOCKET.snd_proc], socket_send_ip |
391 | mov [eax + SOCKET.snd_proc], socket_send_ip |
392 | mov [eax + SOCKET.rcv_proc], socket_receive_dgram |
392 | mov [eax + SOCKET.rcv_proc], socket_receive_dgram |
393 | mov [eax + SOCKET.connect_proc], ipv4_connect |
393 | mov [eax + SOCKET.connect_proc], ipv4_connect |
394 | ret |
394 | ret |
395 | 395 | ||
396 | 396 | ||
397 | align 4 |
397 | align 4 |
398 | .raw_icmp: |
398 | .raw_icmp: |
399 | push eax |
399 | push eax |
400 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue |
400 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue |
401 | pop eax |
401 | pop eax |
402 | 402 | ||
403 | mov [eax + SOCKET.snd_proc], socket_send_icmp |
403 | mov [eax + SOCKET.snd_proc], socket_send_icmp |
404 | mov [eax + SOCKET.rcv_proc], socket_receive_dgram |
404 | mov [eax + SOCKET.rcv_proc], socket_receive_dgram |
405 | mov [eax + SOCKET.connect_proc], ipv4_connect |
405 | mov [eax + SOCKET.connect_proc], ipv4_connect |
406 | ret |
406 | ret |
407 | 407 | ||
408 | align 4 |
408 | align 4 |
409 | .pppoe: |
409 | .pppoe: |
410 | push eax |
410 | push eax |
411 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue |
411 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue |
412 | pop eax |
412 | pop eax |
413 | 413 | ||
414 | mov [eax + SOCKET.snd_proc], socket_send_pppoe |
414 | mov [eax + SOCKET.snd_proc], socket_send_pppoe |
415 | mov [eax + SOCKET.rcv_proc], socket_receive_dgram |
415 | mov [eax + SOCKET.rcv_proc], socket_receive_dgram |
416 | ret |
416 | ret |
417 | 417 | ||
418 | 418 | ||
419 | ;-----------------------------------------------------------------; |
419 | ;-----------------------------------------------------------------; |
420 | ; ; |
420 | ; ; |
421 | ; socket_bind: Bind to a local port. ; |
421 | ; socket_bind: Bind to a local port. ; |
422 | ; ; |
422 | ; ; |
423 | ; IN: ecx = socket number ; |
423 | ; IN: ecx = socket number ; |
424 | ; edx = pointer to sockaddr struct ; |
424 | ; edx = pointer to sockaddr struct ; |
425 | ; esi = length of sockaddr struct ; |
425 | ; esi = length of sockaddr struct ; |
426 | ; ; |
426 | ; ; |
427 | ; OUT: eax = 0 on success ; |
427 | ; OUT: eax = 0 on success ; |
428 | ; eax = -1 on error ; |
428 | ; eax = -1 on error ; |
429 | ; ebx = errorcode on error ; |
429 | ; ebx = errorcode on error ; |
430 | ; ; |
430 | ; ; |
431 | ;-----------------------------------------------------------------; |
431 | ;-----------------------------------------------------------------; |
432 | align 4 |
432 | align 4 |
433 | socket_bind: |
433 | socket_bind: |
434 | 434 | ||
435 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_bind: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi |
435 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_bind: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi |
436 | 436 | ||
437 | call socket_num_to_ptr |
437 | call socket_num_to_ptr |
438 | test eax, eax |
438 | test eax, eax |
439 | jz .invalid |
439 | jz .invalid |
440 | 440 | ||
441 | cmp esi, 2 |
441 | cmp esi, 2 |
442 | jb .invalid |
442 | jb .invalid |
443 | 443 | ||
444 | cmp [eax + UDP_SOCKET.LocalPort], 0 ; Socket can only be bound once |
444 | cmp [eax + UDP_SOCKET.LocalPort], 0 ; Socket can only be bound once |
445 | jnz .invalid |
445 | jnz .invalid |
446 | 446 | ||
447 | cmp word[edx], AF_INET4 |
447 | cmp word[edx], AF_INET4 |
448 | je .af_inet4 |
448 | je .af_inet4 |
449 | 449 | ||
450 | cmp word[edx], AF_LOCAL |
450 | cmp word[edx], AF_LOCAL |
451 | je .af_local |
451 | je .af_local |
452 | 452 | ||
453 | .notsupp: |
453 | .notsupp: |
454 | mov dword[esp+20], EOPNOTSUPP |
454 | mov dword[esp+20], EOPNOTSUPP |
455 | mov dword[esp+32], -1 |
455 | mov dword[esp+32], -1 |
456 | ret |
456 | ret |
457 | 457 | ||
458 | .invalid: |
458 | .invalid: |
459 | mov dword[esp+20], EINVAL |
459 | mov dword[esp+20], EINVAL |
460 | mov dword[esp+32], -1 |
460 | mov dword[esp+32], -1 |
461 | ret |
461 | ret |
462 | 462 | ||
463 | .af_local: |
463 | .af_local: |
464 | ; TODO: write code here |
464 | ; TODO: write code here |
465 | mov dword[esp+32], 0 |
465 | mov dword[esp+32], 0 |
466 | ret |
466 | ret |
467 | 467 | ||
468 | .af_inet4: |
468 | .af_inet4: |
469 | cmp esi, 6 |
469 | cmp esi, 6 |
470 | jb .invalid |
470 | jb .invalid |
471 | 471 | ||
472 | cmp [eax + SOCKET.Protocol], IP_PROTO_UDP |
472 | cmp [eax + SOCKET.Protocol], IP_PROTO_UDP |
473 | je .udp |
473 | je .udp |
474 | 474 | ||
475 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
475 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
476 | je .tcp |
476 | je .tcp |
477 | 477 | ||
478 | jmp .notsupp |
478 | jmp .notsupp |
479 | 479 | ||
480 | .tcp: |
480 | .tcp: |
481 | .udp: |
481 | .udp: |
482 | pushd [edx + 4] ; First, fill in the IP |
482 | pushd [edx + 4] ; First, fill in the IP |
483 | popd [eax + IP_SOCKET.LocalIP] |
483 | popd [eax + IP_SOCKET.LocalIP] |
484 | 484 | ||
485 | mov bx, [edx + 2] ; Did caller specify a local port? |
485 | mov bx, [edx + 2] ; Did caller specify a local port? |
486 | test bx, bx |
486 | test bx, bx |
487 | jnz .just_check |
487 | jnz .just_check |
488 | call socket_find_port ; Nope, find an ephemeral one |
488 | call socket_find_port ; Nope, find an ephemeral one |
489 | jmp .done |
489 | jmp .done |
490 | 490 | ||
491 | .just_check: |
491 | .just_check: |
492 | call socket_check_port ; Yes, check if it's still available |
492 | call socket_check_port ; Yes, check if it's still available |
493 | jz .addrinuse ; ZF is set by socket_check_port on error |
493 | jz .addrinuse ; ZF is set by socket_check_port on error |
494 | 494 | ||
495 | .done: |
495 | .done: |
496 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_bind: local ip=%u.%u.%u.%u\n",\ |
496 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_bind: local ip=%u.%u.%u.%u\n",\ |
497 | [eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\ |
497 | [eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\ |
498 | [eax + IP_SOCKET.LocalIP + 2]:1,[eax + IP_SOCKET.LocalIP + 3]:1 |
498 | [eax + IP_SOCKET.LocalIP + 2]:1,[eax + IP_SOCKET.LocalIP + 3]:1 |
499 | 499 | ||
500 | mov dword[esp+32], 0 |
500 | mov dword[esp+32], 0 |
501 | ret |
501 | ret |
502 | 502 | ||
503 | .addrinuse: |
503 | .addrinuse: |
504 | mov dword[esp+32], -1 |
504 | mov dword[esp+32], -1 |
505 | mov dword[esp+20], EADDRINUSE |
505 | mov dword[esp+20], EADDRINUSE |
506 | ret |
506 | ret |
507 | 507 | ||
508 | 508 | ||
509 | 509 | ||
510 | 510 | ||
511 | ;-----------------------------------------------------------------; |
511 | ;-----------------------------------------------------------------; |
512 | ; ; |
512 | ; ; |
513 | ; socket_connect: Connect to the remote host. ; |
513 | ; socket_connect: Connect to the remote host. ; |
514 | ; ; |
514 | ; ; |
515 | ; IN: ecx = socket number ; |
515 | ; IN: ecx = socket number ; |
516 | ; edx = pointer to sockaddr struct ; |
516 | ; edx = pointer to sockaddr struct ; |
517 | ; esi = length of sockaddr struct ; |
517 | ; esi = length of sockaddr struct ; |
518 | ; ; |
518 | ; ; |
519 | ; OUT: eax = 0 on success ; |
519 | ; OUT: eax = 0 on success ; |
520 | ; eax = -1 on error ; |
520 | ; eax = -1 on error ; |
521 | ; ebx = errorcode on error ; |
521 | ; ebx = errorcode on error ; |
522 | ; ; |
522 | ; ; |
523 | ;-----------------------------------------------------------------; |
523 | ;-----------------------------------------------------------------; |
524 | align 4 |
524 | align 4 |
525 | socket_connect: |
525 | socket_connect: |
526 | 526 | ||
527 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_connect: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi |
527 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_connect: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi |
528 | 528 | ||
529 | call socket_num_to_ptr |
529 | call socket_num_to_ptr |
530 | test eax, eax |
530 | test eax, eax |
531 | jz .invalid |
531 | jz .invalid |
532 | 532 | ||
533 | cmp esi, 8 |
533 | cmp esi, 8 |
534 | jb .invalid |
534 | jb .invalid |
535 | 535 | ||
536 | cmp [eax + SOCKET.state], SS_ISCONNECTING |
536 | cmp [eax + SOCKET.state], SS_ISCONNECTING |
537 | je .already |
537 | je .already |
538 | 538 | ||
539 | test [eax + SOCKET.options], SO_ACCEPTCON |
539 | test [eax + SOCKET.options], SO_ACCEPTCON |
540 | jnz .notsupp |
540 | jnz .notsupp |
541 | 541 | ||
542 | call [eax + SOCKET.connect_proc] |
542 | call [eax + SOCKET.connect_proc] |
543 | 543 | ||
544 | mov dword[esp+20], ebx |
544 | mov dword[esp+20], ebx |
545 | mov dword[esp+32], eax |
545 | mov dword[esp+32], eax |
546 | ret |
546 | ret |
547 | 547 | ||
548 | 548 | ||
549 | .notsupp: |
549 | .notsupp: |
550 | mov dword[esp+20], EOPNOTSUPP |
550 | mov dword[esp+20], EOPNOTSUPP |
551 | mov dword[esp+32], -1 |
551 | mov dword[esp+32], -1 |
552 | ret |
552 | ret |
553 | 553 | ||
554 | .invalid: |
554 | .invalid: |
555 | mov dword[esp+20], EINVAL |
555 | mov dword[esp+20], EINVAL |
556 | mov dword[esp+32], -1 |
556 | mov dword[esp+32], -1 |
557 | ret |
557 | ret |
558 | 558 | ||
559 | .already: |
559 | .already: |
560 | mov dword[esp+20], EALREADY |
560 | mov dword[esp+20], EALREADY |
561 | mov dword[esp+32], -1 |
561 | mov dword[esp+32], -1 |
562 | ret |
562 | ret |
563 | 563 | ||
564 | 564 | ||
565 | connect_notsupp: |
565 | connect_notsupp: |
566 | xor eax, eax |
566 | xor eax, eax |
567 | dec eax |
567 | dec eax |
568 | mov ebx, EOPNOTSUPP |
568 | mov ebx, EOPNOTSUPP |
569 | ret |
569 | ret |
570 | 570 | ||
571 | 571 | ||
572 | ;-----------------------------------------------------------------; |
572 | ;-----------------------------------------------------------------; |
573 | ; ; |
573 | ; ; |
574 | ; socket_listen: Listen for incoming connections. ; |
574 | ; socket_listen: Listen for incoming connections. ; |
575 | ; ; |
575 | ; ; |
576 | ; IN: ecx = socket number ; |
576 | ; IN: ecx = socket number ; |
577 | ; edx = backlog in edx ; |
577 | ; edx = backlog in edx ; |
578 | ; ; |
578 | ; ; |
579 | ; OUT: eax = 0 on success ; |
579 | ; OUT: eax = 0 on success ; |
580 | ; eax = -1 on error ; |
580 | ; eax = -1 on error ; |
581 | ; ebx = errorcode on error ; |
581 | ; ebx = errorcode on error ; |
582 | ; ; |
582 | ; ; |
583 | ;-----------------------------------------------------------------; |
583 | ;-----------------------------------------------------------------; |
584 | align 4 |
584 | align 4 |
585 | socket_listen: |
585 | socket_listen: |
586 | 586 | ||
587 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_listen: socknum=%u backlog=%u\n", ecx, edx |
587 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_listen: socknum=%u backlog=%u\n", ecx, edx |
588 | 588 | ||
589 | call socket_num_to_ptr |
589 | call socket_num_to_ptr |
590 | test eax, eax |
590 | test eax, eax |
591 | jz .invalid |
591 | jz .invalid |
592 | 592 | ||
593 | cmp [eax + SOCKET.Domain], AF_INET4 |
593 | cmp [eax + SOCKET.Domain], AF_INET4 |
594 | jne .notsupp |
594 | jne .notsupp |
595 | 595 | ||
596 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
596 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
597 | jne .invalid |
597 | jne .invalid |
598 | 598 | ||
599 | cmp [eax + TCP_SOCKET.LocalPort], 0 |
599 | cmp [eax + TCP_SOCKET.LocalPort], 0 |
600 | je .already |
600 | je .already |
601 | 601 | ||
602 | cmp [eax + IP_SOCKET.LocalIP], 0 |
602 | cmp [eax + IP_SOCKET.LocalIP], 0 |
603 | jne @f |
603 | jne @f |
604 | push [IP_LIST + 4] ;;; fixme!!!! |
604 | push [IP_LIST + 4] ;;; fixme!!!! |
605 | pop [eax + IP_SOCKET.LocalIP] |
605 | pop [eax + IP_SOCKET.LocalIP] |
606 | @@: |
606 | @@: |
607 | 607 | ||
608 | cmp edx, MAX_backlog |
608 | cmp edx, MAX_backlog |
609 | jbe @f |
609 | jbe @f |
610 | mov edx, MAX_backlog |
610 | mov edx, MAX_backlog |
611 | @@: |
611 | @@: |
612 | 612 | ||
613 | mov [eax + SOCKET.backlog], dx |
613 | mov [eax + SOCKET.backlog], dx |
614 | or [eax + SOCKET.options], SO_ACCEPTCON |
614 | or [eax + SOCKET.options], SO_ACCEPTCON |
615 | mov [eax + TCP_SOCKET.t_state], TCPS_LISTEN |
615 | mov [eax + TCP_SOCKET.t_state], TCPS_LISTEN |
616 | mov [eax + TCP_SOCKET.timer_keepalive], 0 ; disable keepalive timer |
616 | mov [eax + TCP_SOCKET.timer_keepalive], 0 ; disable keepalive timer |
617 | 617 | ||
618 | push eax |
618 | push eax |
619 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up sockets queue |
619 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up sockets queue |
620 | pop eax |
620 | pop eax |
621 | 621 | ||
622 | mov dword[esp+32], 0 |
622 | mov dword[esp+32], 0 |
623 | ret |
623 | ret |
624 | 624 | ||
625 | .notsupp: |
625 | .notsupp: |
626 | mov dword[esp+20], EOPNOTSUPP |
626 | mov dword[esp+20], EOPNOTSUPP |
627 | mov dword[esp+32], -1 |
627 | mov dword[esp+32], -1 |
628 | ret |
628 | ret |
629 | 629 | ||
630 | .invalid: |
630 | .invalid: |
631 | mov dword[esp+20], EINVAL |
631 | mov dword[esp+20], EINVAL |
632 | mov dword[esp+32], -1 |
632 | mov dword[esp+32], -1 |
633 | ret |
633 | ret |
634 | 634 | ||
635 | .already: |
635 | .already: |
636 | mov dword[esp+20], EALREADY |
636 | mov dword[esp+20], EALREADY |
637 | mov dword[esp+32], -1 |
637 | mov dword[esp+32], -1 |
638 | ret |
638 | ret |
639 | 639 | ||
640 | 640 | ||
641 | ;-----------------------------------------------------------------; |
641 | ;-----------------------------------------------------------------; |
642 | ; ; |
642 | ; ; |
643 | ; socket_accept: Accept an incoming connection. ; |
643 | ; socket_accept: Accept an incoming connection. ; |
644 | ; ; |
644 | ; ; |
645 | ; IN: ecx = socket number (of listening socket) ; |
645 | ; IN: ecx = socket number (of listening socket) ; |
646 | ; edx = ptr to sockaddr struct ; |
646 | ; edx = ptr to sockaddr struct ; |
647 | ; esi = length of sockaddr struct ; |
647 | ; esi = length of sockaddr struct ; |
648 | ; ; |
648 | ; ; |
649 | ; OUT: eax = newly created socket num ; |
649 | ; OUT: eax = newly created socket num ; |
650 | ; eax = -1 on error ; |
650 | ; eax = -1 on error ; |
651 | ; ebx = errorcode on error ; |
651 | ; ebx = errorcode on error ; |
652 | ; ; |
652 | ; ; |
653 | ;-----------------------------------------------------------------; |
653 | ;-----------------------------------------------------------------; |
654 | align 4 |
654 | align 4 |
655 | socket_accept: |
655 | socket_accept: |
656 | 656 | ||
657 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_accept: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi |
657 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_accept: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi |
658 | 658 | ||
659 | call socket_num_to_ptr |
659 | call socket_num_to_ptr |
660 | test eax, eax |
660 | test eax, eax |
661 | jz .invalid |
661 | jz .invalid |
662 | 662 | ||
663 | test [eax + SOCKET.options], SO_ACCEPTCON |
663 | test [eax + SOCKET.options], SO_ACCEPTCON |
664 | jz .invalid |
664 | jz .invalid |
665 | 665 | ||
666 | cmp [eax + SOCKET.Domain], AF_INET4 |
666 | cmp [eax + SOCKET.Domain], AF_INET4 |
667 | jne .notsupp |
667 | jne .notsupp |
668 | 668 | ||
669 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
669 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
670 | jne .invalid |
670 | jne .invalid |
671 | 671 | ||
672 | .loop: |
672 | .loop: |
673 | get_from_queue (eax + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .block |
673 | get_from_queue (eax + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .block |
674 | 674 | ||
675 | ; Ok, we got a socket ptr |
675 | ; Ok, we got a socket ptr |
676 | mov eax, [esi] |
676 | mov eax, [esi] |
677 | 677 | ||
678 | ; Verify that it is (still) a valid socket |
678 | ; Verify that it is (still) a valid socket |
679 | call socket_check |
679 | call socket_check |
680 | jz .invalid |
680 | jz .invalid |
681 | 681 | ||
682 | ; Change sockets thread owner ID to that of the current thread |
682 | ; Change sockets thread owner ID to that of the current thread |
683 | mov ebx, [TASK_BASE] |
683 | mov ebx, [TASK_BASE] |
684 | mov ebx, [ebx + TASKDATA.pid] |
684 | mov ebx, [ebx + TASKDATA.pid] |
685 | mov [eax + SOCKET.TID], ebx |
685 | mov [eax + SOCKET.TID], ebx |
686 | 686 | ||
687 | ; Return socket number to caller |
687 | ; Return socket number to caller |
688 | mov eax, [eax + SOCKET.Number] |
688 | mov eax, [eax + SOCKET.Number] |
689 | mov [esp+32], eax |
689 | mov [esp+32], eax |
690 | ret |
690 | ret |
691 | 691 | ||
692 | .block: |
692 | .block: |
693 | test [eax + SOCKET.options], SO_NONBLOCK |
693 | test [eax + SOCKET.options], SO_NONBLOCK |
694 | jnz .wouldblock |
694 | jnz .wouldblock |
695 | - | ||
- | 695 | DEBUGF 1, "Calling socket_block at 695\n" |
|
696 | call socket_block |
696 | call socket_block |
697 | jmp .loop |
697 | jmp .loop |
698 | 698 | ||
699 | .wouldblock: |
699 | .wouldblock: |
700 | mov dword[esp+20], EWOULDBLOCK |
700 | mov dword[esp+20], EWOULDBLOCK |
701 | mov dword[esp+32], -1 |
701 | mov dword[esp+32], -1 |
702 | ret |
702 | ret |
703 | 703 | ||
704 | .invalid: |
704 | .invalid: |
705 | mov dword[esp+20], EINVAL |
705 | mov dword[esp+20], EINVAL |
706 | mov dword[esp+32], -1 |
706 | mov dword[esp+32], -1 |
707 | ret |
707 | ret |
708 | 708 | ||
709 | .notsupp: |
709 | .notsupp: |
710 | mov dword[esp+20], EOPNOTSUPP |
710 | mov dword[esp+20], EOPNOTSUPP |
711 | mov dword[esp+32], -1 |
711 | mov dword[esp+32], -1 |
712 | ret |
712 | ret |
713 | 713 | ||
714 | ;-----------------------------------------------------------------; |
714 | ;-----------------------------------------------------------------; |
715 | ; ; |
715 | ; ; |
716 | ; socket_close: Close the socket (and connection). ; |
716 | ; socket_close: Close the socket (and connection). ; |
717 | ; ; |
717 | ; ; |
718 | ; IN: ecx = socket number ; |
718 | ; IN: ecx = socket number ; |
719 | ; ; |
719 | ; ; |
720 | ; OUT: eax = 0 on success ; |
720 | ; OUT: eax = 0 on success ; |
721 | ; eax = -1 on error ; |
721 | ; eax = -1 on error ; |
722 | ; ebx = errorcode on error ; |
722 | ; ebx = errorcode on error ; |
723 | ; ; |
723 | ; ; |
724 | ;-----------------------------------------------------------------; |
724 | ;-----------------------------------------------------------------; |
725 | align 4 |
725 | align 4 |
726 | socket_close: |
726 | socket_close: |
727 | 727 | ||
728 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_close: socknum=%u\n", ecx |
728 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_close: socknum=%u\n", ecx |
729 | 729 | ||
730 | call socket_num_to_ptr |
730 | call socket_num_to_ptr |
731 | test eax, eax |
731 | test eax, eax |
732 | jz .invalid |
732 | jz .invalid |
- | 733 | DEBUGF 1, "SOCKET_close: socknum=%x\n", eax |
|
733 | 734 | ||
734 | mov dword[esp+32], 0 ; The socket exists, so we will succeed in closing it. |
735 | mov dword[esp+32], 0 ; The socket exists, so we will succeed in closing it. |
735 | 736 | ||
736 | or [eax + SOCKET.options], SO_NONBLOCK ; Mark the socket as non blocking, we dont want it to block any longer! |
737 | or [eax + SOCKET.options], SO_NONBLOCK ; Mark the socket as non blocking, we dont want it to block any longer! |
737 | 738 | ||
738 | test [eax + SOCKET.state], SS_BLOCKED ; Is the socket still in blocked state? |
739 | test [eax + SOCKET.state], SS_BLOCKED ; Is the socket still in blocked state? |
739 | jz @f |
740 | jz @f |
740 | call socket_notify ; Unblock it. |
741 | call socket_notify ; Unblock it. |
741 | @@: |
742 | @@: |
742 | 743 | ||
743 | cmp [eax + SOCKET.Domain], AF_INET4 |
744 | cmp [eax + SOCKET.Domain], AF_INET4 |
744 | jne .free |
745 | jne .free |
745 | 746 | ||
746 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
747 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
747 | je .tcp |
748 | je .tcp |
748 | 749 | ||
749 | .free: |
750 | .free: |
750 | call socket_free |
751 | call socket_free |
751 | ret |
752 | ret |
752 | 753 | ||
753 | .tcp: |
754 | .tcp: |
754 | call tcp_usrclosed |
755 | call tcp_usrclosed |
755 | 756 | ||
756 | test eax, eax |
757 | test eax, eax |
757 | jz @f |
758 | jz @f |
758 | call tcp_output ; If connection is not closed yet, send the FIN |
759 | call tcp_output ; If connection is not closed yet, send the FIN |
759 | @@: |
760 | @@: |
760 | ret |
761 | ret |
761 | 762 | ||
762 | 763 | ||
763 | .invalid: |
764 | .invalid: |
- | 765 | DEBUGF 1, "SOCKET_close: INVALID!\n" |
|
764 | mov dword[esp+20], EINVAL |
766 | mov dword[esp+20], EINVAL |
765 | mov dword[esp+32], -1 |
767 | mov dword[esp+32], -1 |
766 | ret |
768 | ret |
767 | 769 | ||
768 | 770 | ||
769 | ;-----------------------------------------------------------------; |
771 | ;-----------------------------------------------------------------; |
770 | ; ; |
772 | ; ; |
771 | ; socket_receive: Receive some data from the remote end. ; |
773 | ; socket_receive: Receive some data from the remote end. ; |
772 | ; ; |
774 | ; ; |
773 | ; IN: ecx = socket number ; |
775 | ; IN: ecx = socket number ; |
774 | ; edx = addr to application buffer ; |
776 | ; edx = addr to application buffer ; |
775 | ; edx = length of application buffer ; |
777 | ; edx = length of application buffer ; |
776 | ; edi = flags ; |
778 | ; edi = flags ; |
777 | ; ; |
779 | ; ; |
778 | ; OUT: eax = number of bytes copied ; |
780 | ; OUT: eax = number of bytes copied ; |
779 | ; eax = -1 on error ; |
781 | ; eax = -1 on error ; |
780 | ; eax = 0 when socket has been closed by the remote end ; |
782 | ; eax = 0 when socket has been closed by the remote end ; |
781 | ; ebx = errorcode on error ; |
783 | ; ebx = errorcode on error ; |
782 | ; ; |
784 | ; ; |
783 | ;-----------------------------------------------------------------; |
785 | ;-----------------------------------------------------------------; |
784 | align 4 |
786 | align 4 |
785 | socket_receive: |
787 | socket_receive: |
786 | 788 | ||
787 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: socknum=%u bufaddr=%x buflength=%u flags=%x\n", ecx, edx, esi, edi |
789 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: socknum=%u bufaddr=%x buflength=%u flags=%x\n", ecx, edx, esi, edi |
788 | 790 | ||
789 | call socket_num_to_ptr |
791 | call socket_num_to_ptr |
790 | test eax, eax |
792 | test eax, eax |
791 | jz .invalid |
793 | jz .invalid |
792 | 794 | ||
793 | .loop: |
795 | .loop: |
- | 796 | pushf |
|
- | 797 | cli |
|
794 | push edi |
798 | push edi |
795 | call [eax + SOCKET.rcv_proc] |
799 | call [eax + SOCKET.rcv_proc] |
796 | pop edi |
800 | pop edi |
797 | 801 | ||
798 | test [eax + SOCKET.state], SS_CANTRCVMORE |
802 | test [eax + SOCKET.state], SS_CANTRCVMORE |
799 | jnz .last_data |
803 | jnz .last_data |
800 | 804 | ||
801 | cmp ebx, EWOULDBLOCK |
805 | cmp ebx, EWOULDBLOCK |
802 | jne .return |
806 | jne .return |
803 | 807 | ||
804 | test edi, MSG_DONTWAIT |
808 | test edi, MSG_DONTWAIT |
805 | jnz .return_err |
809 | jnz .return_err |
806 | 810 | ||
807 | test [eax + SOCKET.options], SO_NONBLOCK |
811 | test [eax + SOCKET.options], SO_NONBLOCK |
808 | jnz .return_err |
812 | jnz .return_err |
809 | - | ||
- | 813 | DEBUGF 1, "Calling socket_block at 813\n" |
|
810 | call socket_block |
814 | call socket_block |
- | 815 | popf |
|
811 | jmp .loop |
816 | jmp .loop |
812 | 817 | ||
813 | 818 | ||
814 | .invalid: |
819 | .invalid: |
815 | push EINVAL |
820 | push EINVAL |
816 | pop ebx |
821 | pop ebx |
817 | .return_err: |
822 | .return_err: |
818 | mov ecx, -1 |
823 | mov ecx, -1 |
819 | .return: |
824 | .return: |
- | 825 | popf |
|
820 | mov [esp+20], ebx |
826 | mov [esp+20], ebx |
821 | mov [esp+32], ecx |
827 | mov [esp+32], ecx |
822 | ret |
828 | ret |
823 | 829 | ||
824 | .last_data: |
830 | .last_data: |
825 | test ecx, ecx |
831 | test ecx, ecx |
826 | jz .return |
832 | jz .return |
827 | call socket_notify ; Call me again! |
833 | call socket_notify ; Call me again! |
828 | jmp .return |
834 | jmp .return |
829 | 835 | ||
830 | 836 | ||
831 | 837 | ||
832 | 838 | ||
833 | align 4 |
839 | align 4 |
834 | socket_receive_dgram: |
840 | socket_receive_dgram: |
835 | 841 | ||
836 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: DGRAM\n" |
842 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: DGRAM\n" |
837 | 843 | ||
838 | test edi, MSG_PEEK |
844 | test edi, MSG_PEEK |
839 | jnz .peek |
845 | jnz .peek |
840 | 846 | ||
841 | mov ebx, esi ; buffer length |
847 | mov ebx, esi ; buffer length |
842 | 848 | ||
843 | get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .wouldblock ; sets esi only on success. |
849 | get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .wouldblock ; sets esi only on success. |
844 | mov ecx, [esi + socket_queue_entry.data_size] |
850 | mov ecx, [esi + socket_queue_entry.data_size] |
845 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: %u bytes data\n", ecx |
851 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: %u bytes data\n", ecx |
846 | 852 | ||
847 | cmp ecx, ebx ; If data segment does not fit in applications buffer, abort |
853 | cmp ecx, ebx ; If data segment does not fit in applications buffer, abort |
848 | ja .too_small |
854 | ja .too_small |
849 | 855 | ||
850 | push eax ecx |
856 | push eax ecx |
851 | push [esi + socket_queue_entry.buf_ptr] ; save the buffer addr so we can clear it later |
857 | push [esi + socket_queue_entry.buf_ptr] ; save the buffer addr so we can clear it later |
852 | mov esi, [esi + socket_queue_entry.data_ptr] |
858 | mov esi, [esi + socket_queue_entry.data_ptr] |
853 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: Source buffer=%x real addr=%x\n", [esp], esi |
859 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: Source buffer=%x real addr=%x\n", [esp], esi |
854 | 860 | ||
855 | ; copy the data from kernel buffer to application buffer |
861 | ; copy the data from kernel buffer to application buffer |
856 | mov edi, edx ; bufferaddr |
862 | mov edi, edx ; bufferaddr |
857 | shr ecx, 1 |
863 | shr ecx, 1 |
858 | jnc .nb |
864 | jnc .nb |
859 | movsb |
865 | movsb |
860 | .nb: |
866 | .nb: |
861 | shr ecx, 1 |
867 | shr ecx, 1 |
862 | jnc .nw |
868 | jnc .nw |
863 | movsw |
869 | movsw |
864 | .nw: |
870 | .nw: |
865 | test ecx, ecx |
871 | test ecx, ecx |
866 | jz .nd |
872 | jz .nd |
867 | rep movsd |
873 | rep movsd |
868 | .nd: |
874 | .nd: |
869 | 875 | ||
870 | call net_buff_free |
876 | call net_buff_free |
871 | pop ecx eax ; return number of bytes copied to application |
877 | pop ecx eax ; return number of bytes copied to application |
872 | cmp [eax + SOCKET_QUEUE_LOCATION + queue.size], 0 |
878 | cmp [eax + SOCKET_QUEUE_LOCATION + queue.size], 0 |
873 | je @f |
879 | je @f |
874 | call socket_notify ; Queue another network event |
880 | call socket_notify ; Queue another network event |
875 | @@: |
881 | @@: |
876 | xor ebx, ebx ; errorcode = 0 (no error) |
882 | xor ebx, ebx ; errorcode = 0 (no error) |
877 | ret |
883 | ret |
878 | 884 | ||
879 | .too_small: |
885 | .too_small: |
880 | mov ecx, -1 |
886 | mov ecx, -1 |
881 | push EMSGSIZE |
887 | push EMSGSIZE |
882 | pop ebx |
888 | pop ebx |
883 | ret |
889 | ret |
884 | 890 | ||
885 | .wouldblock: |
891 | .wouldblock: |
886 | push EWOULDBLOCK |
892 | push EWOULDBLOCK |
887 | pop ebx |
893 | pop ebx |
888 | ret |
894 | ret |
889 | 895 | ||
890 | .peek: |
896 | .peek: |
891 | xor ebx, ebx |
897 | xor ebx, ebx |
892 | xor ecx, ecx |
898 | xor ecx, ecx |
893 | cmp [eax + SOCKET_QUEUE_LOCATION + queue.size], 0 |
899 | cmp [eax + SOCKET_QUEUE_LOCATION + queue.size], 0 |
894 | je @f |
900 | je @f |
895 | mov esi, [eax + SOCKET_QUEUE_LOCATION + queue.r_ptr] |
901 | mov esi, [eax + SOCKET_QUEUE_LOCATION + queue.r_ptr] |
896 | mov ecx, [esi + socket_queue_entry.data_size] |
902 | mov ecx, [esi + socket_queue_entry.data_size] |
897 | @@: |
903 | @@: |
898 | ret |
904 | ret |
899 | 905 | ||
900 | 906 | ||
901 | align 4 |
907 | align 4 |
902 | socket_receive_local: |
908 | socket_receive_local: |
903 | 909 | ||
904 | ; does this socket have a PID yet? |
910 | ; does this socket have a PID yet? |
905 | cmp [eax + SOCKET.PID], 0 |
911 | cmp [eax + SOCKET.PID], 0 |
906 | jne @f |
912 | jne @f |
907 | 913 | ||
908 | ; Change PID to that of current process |
914 | ; Change PID to that of current process |
909 | mov ebx, [TASK_BASE] |
915 | mov ebx, [TASK_BASE] |
910 | mov ebx, [ebx + TASKDATA.pid] |
916 | mov ebx, [ebx + TASKDATA.pid] |
911 | mov [eax + SOCKET.PID], ebx |
917 | mov [eax + SOCKET.PID], ebx |
912 | mov [eax + SOCKET.TID], ebx ; currently TID = PID in kolibrios :( |
918 | mov [eax + SOCKET.TID], ebx ; currently TID = PID in kolibrios :( |
913 | @@: |
919 | @@: |
914 | 920 | ||
915 | mov [eax + SOCKET.rcv_proc], socket_receive_stream |
921 | mov [eax + SOCKET.rcv_proc], socket_receive_stream |
916 | 922 | ||
917 | ; ... continue to SOCKET_receive_stream |
923 | ; ... continue to SOCKET_receive_stream |
918 | 924 | ||
919 | align 4 |
925 | align 4 |
920 | socket_receive_stream: |
926 | socket_receive_stream: |
921 | 927 | ||
922 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: STREAM\n" |
928 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: STREAM\n" |
923 | 929 | ||
924 | cmp [eax + STREAM_SOCKET.rcv + RING_BUFFER.size], 0 |
930 | cmp [eax + STREAM_SOCKET.rcv + RING_BUFFER.size], 0 |
925 | je .wouldblock |
931 | je .wouldblock |
926 | 932 | ||
927 | test edi, MSG_PEEK |
933 | test edi, MSG_PEEK |
928 | jnz .peek |
934 | jnz .peek |
929 | 935 | ||
930 | mov ecx, esi |
936 | mov ecx, esi |
931 | mov edi, edx |
937 | mov edi, edx |
932 | xor edx, edx |
938 | xor edx, edx |
933 | 939 | ||
934 | push eax |
940 | push eax |
935 | add eax, STREAM_SOCKET.rcv |
941 | add eax, STREAM_SOCKET.rcv |
936 | call socket_ring_read ; copy data from kernel buffer to application buffer |
942 | call socket_ring_read ; copy data from kernel buffer to application buffer |
937 | call socket_ring_free ; free read memory |
943 | call socket_ring_free ; free read memory |
938 | pop eax |
944 | pop eax |
939 | 945 | ||
940 | cmp [eax + STREAM_SOCKET.rcv + RING_BUFFER.size], 0 |
946 | cmp [eax + STREAM_SOCKET.rcv + RING_BUFFER.size], 0 |
941 | jne .more_data |
947 | jne .more_data |
942 | xor ebx, ebx ; errorcode = 0 (no error) |
948 | xor ebx, ebx ; errorcode = 0 (no error) |
943 | ret |
949 | ret |
944 | 950 | ||
945 | .more_data: |
951 | .more_data: |
946 | call socket_notify ; Queue another network event |
952 | call socket_notify ; Queue another network event |
947 | xor ebx, ebx ; errorcode = 0 (no error) |
953 | xor ebx, ebx ; errorcode = 0 (no error) |
948 | ret |
954 | ret |
949 | 955 | ||
950 | .wouldblock: |
956 | .wouldblock: |
951 | push EWOULDBLOCK |
957 | push EWOULDBLOCK |
952 | pop ebx |
958 | pop ebx |
953 | xor ecx, ecx |
959 | xor ecx, ecx |
954 | ret |
960 | ret |
955 | 961 | ||
956 | .peek: |
962 | .peek: |
957 | mov ecx, [eax + STREAM_SOCKET.rcv + RING_BUFFER.size] |
963 | mov ecx, [eax + STREAM_SOCKET.rcv + RING_BUFFER.size] |
958 | xor ebx, ebx |
964 | xor ebx, ebx |
959 | ret |
965 | ret |
960 | 966 | ||
961 | 967 | ||
962 | ;-----------------------------------------------------------------; |
968 | ;-----------------------------------------------------------------; |
963 | ; ; |
969 | ; ; |
964 | ; socket_send: Send some data to the remote end. ; |
970 | ; socket_send: Send some data to the remote end. ; |
965 | ; ; |
971 | ; ; |
966 | ; IN: ecx = socket number ; |
972 | ; IN: ecx = socket number ; |
967 | ; edx = pointer to data ; |
973 | ; edx = pointer to data ; |
968 | ; esi = data length ; |
974 | ; esi = data length ; |
969 | ; edi = flags ; |
975 | ; edi = flags ; |
970 | ; ; |
976 | ; ; |
971 | ; OUT: eax = number of bytes sent ; |
977 | ; OUT: eax = number of bytes sent ; |
972 | ; eax = -1 on error ; |
978 | ; eax = -1 on error ; |
973 | ; ebx = errorcode on error ; |
979 | ; ebx = errorcode on error ; |
974 | ; ; |
980 | ; ; |
975 | ;-----------------------------------------------------------------; |
981 | ;-----------------------------------------------------------------; |
976 | align 4 |
982 | align 4 |
977 | socket_send: |
983 | socket_send: |
978 | 984 | ||
979 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: socknum=%u data ptr=%x length=%u flags=%x\n", ecx, edx, esi, edi |
985 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: socknum=%u data ptr=%x length=%u flags=%x\n", ecx, edx, esi, edi |
980 | 986 | ||
981 | call socket_num_to_ptr |
987 | call socket_num_to_ptr |
982 | test eax, eax |
988 | test eax, eax |
983 | jz .invalid |
989 | jz .invalid |
984 | 990 | ||
985 | mov ecx, esi |
991 | mov ecx, esi |
986 | mov esi, edx |
992 | mov esi, edx |
987 | 993 | ||
988 | jmp [eax + SOCKET.snd_proc] |
994 | jmp [eax + SOCKET.snd_proc] |
989 | 995 | ||
990 | .invalid: |
996 | .invalid: |
991 | mov dword[esp+20], EINVAL |
997 | mov dword[esp+20], EINVAL |
992 | mov dword[esp+32], -1 |
998 | mov dword[esp+32], -1 |
993 | ret |
999 | ret |
994 | 1000 | ||
995 | 1001 | ||
996 | align 4 |
1002 | align 4 |
997 | socket_send_udp: |
1003 | socket_send_udp: |
998 | 1004 | ||
999 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: UDP\n" |
1005 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: UDP\n" |
1000 | 1006 | ||
1001 | mov [esp+32], ecx |
1007 | mov [esp+32], ecx |
1002 | call udp_output |
1008 | call udp_output |
1003 | cmp eax, -1 |
1009 | cmp eax, -1 |
1004 | je .error |
1010 | je .error |
1005 | ret |
1011 | ret |
1006 | 1012 | ||
1007 | .error: |
1013 | .error: |
1008 | mov dword[esp+32], -1 |
1014 | mov dword[esp+32], -1 |
1009 | mov dword[esp+20], EMSGSIZE ; FIXME: UDP_output should return error codes! |
1015 | mov dword[esp+20], EMSGSIZE ; FIXME: UDP_output should return error codes! |
1010 | ret |
1016 | ret |
1011 | 1017 | ||
1012 | 1018 | ||
1013 | align 4 |
1019 | align 4 |
1014 | socket_send_tcp: |
1020 | socket_send_tcp: |
1015 | 1021 | ||
1016 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: TCP\n" |
1022 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: TCP\n" |
1017 | 1023 | ||
1018 | push eax |
1024 | push eax |
1019 | add eax, STREAM_SOCKET.snd |
1025 | add eax, STREAM_SOCKET.snd |
1020 | call socket_ring_write |
1026 | call socket_ring_write |
1021 | pop eax |
1027 | pop eax |
1022 | 1028 | ||
1023 | mov [esp+32], ecx |
1029 | mov [esp+32], ecx |
1024 | mov [eax + SOCKET.errorcode], 0 |
1030 | mov [eax + SOCKET.errorcode], 0 |
1025 | push eax |
1031 | push eax |
1026 | call tcp_output ; FIXME: this doesnt look pretty, does it? |
1032 | call tcp_output ; FIXME: this doesnt look pretty, does it? |
1027 | pop eax |
1033 | pop eax |
1028 | mov eax, [eax + SOCKET.errorcode] |
1034 | mov eax, [eax + SOCKET.errorcode] |
1029 | mov [esp+20], eax |
1035 | mov [esp+20], eax |
1030 | ret |
1036 | ret |
1031 | 1037 | ||
1032 | 1038 | ||
1033 | align 4 |
1039 | align 4 |
1034 | socket_send_ip: |
1040 | socket_send_ip: |
1035 | 1041 | ||
1036 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: IPv4\n" |
1042 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: IPv4\n" |
1037 | 1043 | ||
1038 | mov [esp+32], ecx |
1044 | mov [esp+32], ecx |
1039 | call ipv4_output_raw |
1045 | call ipv4_output_raw |
1040 | cmp eax, -1 |
1046 | cmp eax, -1 |
1041 | je .error |
1047 | je .error |
1042 | ret |
1048 | ret |
1043 | 1049 | ||
1044 | .error: |
1050 | .error: |
1045 | mov dword[esp+32], eax |
1051 | mov dword[esp+32], eax |
1046 | mov dword[esp+20], ebx |
1052 | mov dword[esp+20], ebx |
1047 | ret |
1053 | ret |
1048 | 1054 | ||
1049 | 1055 | ||
1050 | align 4 |
1056 | align 4 |
1051 | socket_send_icmp: |
1057 | socket_send_icmp: |
1052 | 1058 | ||
1053 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: ICMP\n" |
1059 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: ICMP\n" |
1054 | 1060 | ||
1055 | mov [esp+32], ecx |
1061 | mov [esp+32], ecx |
1056 | call icmp_output_raw |
1062 | call icmp_output_raw |
1057 | cmp eax, -1 |
1063 | cmp eax, -1 |
1058 | je .error |
1064 | je .error |
1059 | ret |
1065 | ret |
1060 | 1066 | ||
1061 | .error: |
1067 | .error: |
1062 | mov dword[esp+32], eax |
1068 | mov dword[esp+32], eax |
1063 | mov dword[esp+20], ebx |
1069 | mov dword[esp+20], ebx |
1064 | ret |
1070 | ret |
1065 | 1071 | ||
1066 | 1072 | ||
1067 | align 4 |
1073 | align 4 |
1068 | socket_send_pppoe: |
1074 | socket_send_pppoe: |
1069 | 1075 | ||
1070 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: PPPoE\n" |
1076 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: PPPoE\n" |
1071 | 1077 | ||
1072 | mov [esp+32], ecx |
1078 | mov [esp+32], ecx |
1073 | mov ebx, [eax + SOCKET.device] |
1079 | mov ebx, [eax + SOCKET.device] |
1074 | 1080 | ||
1075 | call pppoe_discovery_output ; FIXME: errorcodes |
1081 | call pppoe_discovery_output ; FIXME: errorcodes |
1076 | cmp eax, -1 |
1082 | cmp eax, -1 |
1077 | je .error |
1083 | je .error |
1078 | ret |
1084 | ret |
1079 | 1085 | ||
1080 | .error: |
1086 | .error: |
1081 | mov dword[esp+32], -1 |
1087 | mov dword[esp+32], -1 |
1082 | mov dword[esp+20], EMSGSIZE |
1088 | mov dword[esp+20], EMSGSIZE |
1083 | ret |
1089 | ret |
1084 | 1090 | ||
1085 | 1091 | ||
1086 | 1092 | ||
1087 | align 4 |
1093 | align 4 |
1088 | socket_send_local: |
1094 | socket_send_local: |
1089 | 1095 | ||
1090 | ; does this socket have a PID yet? |
1096 | ; does this socket have a PID yet? |
1091 | cmp [eax + SOCKET.PID], 0 |
1097 | cmp [eax + SOCKET.PID], 0 |
1092 | jne @f |
1098 | jne @f |
1093 | 1099 | ||
1094 | ; Change PID to that of current process |
1100 | ; Change PID to that of current process |
1095 | mov ebx, [TASK_BASE] |
1101 | mov ebx, [TASK_BASE] |
1096 | mov ebx, [ebx + TASKDATA.pid] |
1102 | mov ebx, [ebx + TASKDATA.pid] |
1097 | mov [eax + SOCKET.PID], ebx |
1103 | mov [eax + SOCKET.PID], ebx |
1098 | mov [eax + SOCKET.TID], ebx ; currently TID = PID in kolibrios :( |
1104 | mov [eax + SOCKET.TID], ebx ; currently TID = PID in kolibrios :( |
1099 | @@: |
1105 | @@: |
1100 | mov [eax + SOCKET.snd_proc], socket_send_local_initialized |
1106 | mov [eax + SOCKET.snd_proc], socket_send_local_initialized |
1101 | 1107 | ||
1102 | align 4 |
1108 | align 4 |
1103 | socket_send_local_initialized: |
1109 | socket_send_local_initialized: |
1104 | 1110 | ||
1105 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: LOCAL\n" |
1111 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: LOCAL\n" |
1106 | 1112 | ||
1107 | ; get the other side's socket and check if it still exists |
1113 | ; get the other side's socket and check if it still exists |
1108 | mov eax, [eax + SOCKET.device] |
1114 | mov eax, [eax + SOCKET.device] |
1109 | call socket_check |
1115 | call socket_check |
1110 | jz .invalid |
1116 | jz .invalid |
1111 | 1117 | ||
1112 | ; allright, shove in the data! |
1118 | ; allright, shove in the data! |
1113 | push eax |
1119 | push eax |
1114 | add eax, STREAM_SOCKET.rcv |
1120 | add eax, STREAM_SOCKET.rcv |
1115 | call socket_ring_write |
1121 | call socket_ring_write |
1116 | pop eax |
1122 | pop eax |
1117 | 1123 | ||
1118 | ; return the number of written bytes (or errorcode) to application |
1124 | ; return the number of written bytes (or errorcode) to application |
1119 | mov [esp+32], ecx |
1125 | mov [esp+32], ecx |
1120 | 1126 | ||
1121 | ; and notify the other end |
1127 | ; and notify the other end |
1122 | call socket_notify |
1128 | call socket_notify |
1123 | 1129 | ||
1124 | ret |
1130 | ret |
1125 | 1131 | ||
1126 | .invalid: |
1132 | .invalid: |
1127 | mov dword[esp+32], -1 |
1133 | mov dword[esp+32], -1 |
1128 | mov dword[esp+20], EINVAL |
1134 | mov dword[esp+20], EINVAL |
1129 | ret |
1135 | ret |
1130 | 1136 | ||
1131 | 1137 | ||
1132 | ;-----------------------------------------------------------------; |
1138 | ;-----------------------------------------------------------------; |
1133 | ; ; |
1139 | ; ; |
1134 | ; socket_get_opt: Read a socket option ; |
1140 | ; socket_get_opt: Read a socket option ; |
1135 | ; ; |
1141 | ; ; |
1136 | ; IN: ecx = socket number ; |
1142 | ; IN: ecx = socket number ; |
1137 | ; edx = pointer to socket options struct ; |
1143 | ; edx = pointer to socket options struct ; |
1138 | ; ; |
1144 | ; ; |
1139 | ; OUT: eax = 0 on success ; |
1145 | ; OUT: eax = 0 on success ; |
1140 | ; eax = -1 on error ; |
1146 | ; eax = -1 on error ; |
1141 | ; ebx = errorcode on error ; |
1147 | ; ebx = errorcode on error ; |
1142 | ; ; |
1148 | ; ; |
1143 | ;-----------------------------------------------------------------; |
1149 | ;-----------------------------------------------------------------; |
1144 | align 4 |
1150 | align 4 |
1145 | socket_get_opt: |
1151 | socket_get_opt: |
1146 | 1152 | ||
1147 | ; FIXME: |
1153 | ; FIXME: |
1148 | ; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP. |
1154 | ; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP. |
1149 | ; TODO: find best way to notify that send()'ed data were acknowledged |
1155 | ; TODO: find best way to notify that send()'ed data were acknowledged |
1150 | ; Also pseudo-optname -3 is valid and returns socket state, one of TCPS_*. |
1156 | ; Also pseudo-optname -3 is valid and returns socket state, one of TCPS_*. |
1151 | 1157 | ||
1152 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_get_opt\n" |
1158 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_get_opt\n" |
1153 | 1159 | ||
1154 | call socket_num_to_ptr |
1160 | call socket_num_to_ptr |
1155 | test eax, eax |
1161 | test eax, eax |
1156 | jz .invalid |
1162 | jz .invalid |
1157 | 1163 | ||
1158 | cmp dword [edx], IP_PROTO_TCP |
1164 | cmp dword [edx], IP_PROTO_TCP |
1159 | jne .invalid |
1165 | jne .invalid |
1160 | cmp dword [edx+4], -2 |
1166 | cmp dword [edx+4], -2 |
1161 | je @f |
1167 | je @f |
1162 | cmp dword [edx+4], -3 |
1168 | cmp dword [edx+4], -3 |
1163 | jne .invalid |
1169 | jne .invalid |
1164 | @@: |
1170 | @@: |
1165 | ; mov eax, [edx+12] |
1171 | ; mov eax, [edx+12] |
1166 | ; test eax, eax |
1172 | ; test eax, eax |
1167 | ; jz .fail |
1173 | ; jz .fail |
1168 | ; cmp dword [eax], 4 |
1174 | ; cmp dword [eax], 4 |
1169 | ; mov dword [eax], 4 |
1175 | ; mov dword [eax], 4 |
1170 | ; jb .fail |
1176 | ; jb .fail |
1171 | ; stdcall net_socket_num_to_addr, ecx |
1177 | ; stdcall net_socket_num_to_addr, ecx |
1172 | ; test eax, eax |
1178 | ; test eax, eax |
1173 | ; jz .fail |
1179 | ; jz .fail |
1174 | ; ; todo: check that eax is really TCP socket |
1180 | ; ; todo: check that eax is really TCP socket |
1175 | ; mov ecx, [eax + TCP_SOCKET.last_ack_number] |
1181 | ; mov ecx, [eax + TCP_SOCKET.last_ack_number] |
1176 | ; cmp dword [edx+4], -2 |
1182 | ; cmp dword [edx+4], -2 |
1177 | ; jz @f |
1183 | ; jz @f |
1178 | ; mov ecx, [eax + TCP_SOCKET.state] |
1184 | ; mov ecx, [eax + TCP_SOCKET.state] |
1179 | @@: |
1185 | @@: |
1180 | mov eax, [edx+8] |
1186 | mov eax, [edx+8] |
1181 | test eax, eax |
1187 | test eax, eax |
1182 | jz @f |
1188 | jz @f |
1183 | mov [eax], ecx |
1189 | mov [eax], ecx |
1184 | @@: |
1190 | @@: |
1185 | mov dword [esp+32], 0 |
1191 | mov dword [esp+32], 0 |
1186 | ret |
1192 | ret |
1187 | 1193 | ||
1188 | .invalid: |
1194 | .invalid: |
1189 | mov dword[esp+32], -1 |
1195 | mov dword[esp+32], -1 |
1190 | mov dword[esp+20], EINVAL |
1196 | mov dword[esp+20], EINVAL |
1191 | ret |
1197 | ret |
1192 | 1198 | ||
1193 | 1199 | ||
1194 | ;-----------------------------------------------------------------; |
1200 | ;-----------------------------------------------------------------; |
1195 | ; ; |
1201 | ; ; |
1196 | ; socket_set_options: Set a socket option. ; |
1202 | ; socket_set_options: Set a socket option. ; |
1197 | ; ; |
1203 | ; ; |
1198 | ; IN: ecx = socket number ; |
1204 | ; IN: ecx = socket number ; |
1199 | ; edx = pointer to socket options struct ; |
1205 | ; edx = pointer to socket options struct ; |
1200 | ; ; |
1206 | ; ; |
1201 | ; OUT: eax = 0 on success ; |
1207 | ; OUT: eax = 0 on success ; |
1202 | ; eax = -1 on error ; |
1208 | ; eax = -1 on error ; |
1203 | ; ebx = errorcode on error ; |
1209 | ; ebx = errorcode on error ; |
1204 | ; ; |
1210 | ; ; |
1205 | ;-----------------------------------------------------------------; |
1211 | ;-----------------------------------------------------------------; |
1206 | align 4 |
1212 | align 4 |
1207 | socket_set_opt: |
1213 | socket_set_opt: |
1208 | 1214 | ||
1209 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt\n" |
1215 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt\n" |
1210 | 1216 | ||
1211 | call socket_num_to_ptr |
1217 | call socket_num_to_ptr |
1212 | test eax, eax |
1218 | test eax, eax |
1213 | jz .invalid |
1219 | jz .invalid |
1214 | 1220 | ||
1215 | cmp [edx + socket_options.level], IP_PROTO_IP |
1221 | cmp [edx + socket_options.level], IP_PROTO_IP |
1216 | je .ip |
1222 | je .ip |
1217 | cmp [edx + socket_options.level], SOL_SOCKET |
1223 | cmp [edx + socket_options.level], SOL_SOCKET |
1218 | jne .invalid |
1224 | jne .invalid |
1219 | 1225 | ||
1220 | .socket: |
1226 | .socket: |
1221 | cmp [edx + socket_options.optname], SO_BINDTODEVICE |
1227 | cmp [edx + socket_options.optname], SO_BINDTODEVICE |
1222 | jne .invalid |
1228 | jne .invalid |
1223 | 1229 | ||
1224 | .bind: |
1230 | .bind: |
1225 | cmp [edx + socket_options.optlen], 0 |
1231 | cmp [edx + socket_options.optlen], 0 |
1226 | je .unbind |
1232 | je .unbind |
1227 | 1233 | ||
1228 | movzx edx, byte[edx + socket_options.optval] |
1234 | movzx edx, byte[edx + socket_options.optval] |
1229 | cmp edx, NET_DEVICES_MAX |
1235 | cmp edx, NET_DEVICES_MAX |
1230 | ja .invalid |
1236 | ja .invalid |
1231 | 1237 | ||
1232 | mov edx, [NET_DRV_LIST + 4*edx] |
1238 | mov edx, [NET_DRV_LIST + 4*edx] |
1233 | test edx, edx |
1239 | test edx, edx |
1234 | jz .already |
1240 | jz .already |
1235 | mov [eax + SOCKET.device], edx |
1241 | mov [eax + SOCKET.device], edx |
1236 | 1242 | ||
1237 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt: Bound socket %x to device %x\n", eax, edx |
1243 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt: Bound socket %x to device %x\n", eax, edx |
1238 | 1244 | ||
1239 | mov dword[esp+32], 0 ; success! |
1245 | mov dword[esp+32], 0 ; success! |
1240 | ret |
1246 | ret |
1241 | 1247 | ||
1242 | .unbind: |
1248 | .unbind: |
1243 | mov [eax + SOCKET.device], 0 |
1249 | mov [eax + SOCKET.device], 0 |
1244 | 1250 | ||
1245 | mov dword[esp+32], 0 ; success! |
1251 | mov dword[esp+32], 0 ; success! |
1246 | ret |
1252 | ret |
1247 | 1253 | ||
1248 | .ip: |
1254 | .ip: |
1249 | cmp [edx + socket_options.optname], IP_TTL |
1255 | cmp [edx + socket_options.optname], IP_TTL |
1250 | jne .invalid |
1256 | jne .invalid |
1251 | 1257 | ||
1252 | .ttl: |
1258 | .ttl: |
1253 | mov bl, byte[edx + socket_options.optval] |
1259 | mov bl, byte[edx + socket_options.optval] |
1254 | mov [eax + IP_SOCKET.ttl], bl |
1260 | mov [eax + IP_SOCKET.ttl], bl |
1255 | 1261 | ||
1256 | mov dword[esp+32], 0 ; success! |
1262 | mov dword[esp+32], 0 ; success! |
1257 | ret |
1263 | ret |
1258 | 1264 | ||
1259 | .already: |
1265 | .already: |
1260 | mov dword[esp+20], EALREADY |
1266 | mov dword[esp+20], EALREADY |
1261 | mov dword[esp+32], -1 |
1267 | mov dword[esp+32], -1 |
1262 | ret |
1268 | ret |
1263 | 1269 | ||
1264 | .invalid: |
1270 | .invalid: |
1265 | mov dword[esp+20], EINVAL |
1271 | mov dword[esp+20], EINVAL |
1266 | mov dword[esp+32], -1 |
1272 | mov dword[esp+32], -1 |
1267 | ret |
1273 | ret |
1268 | 1274 | ||
1269 | 1275 | ||
1270 | 1276 | ||
1271 | 1277 | ||
1272 | ;-----------------------------------------------------------------; |
1278 | ;-----------------------------------------------------------------; |
1273 | ; ; |
1279 | ; ; |
1274 | ; socket_pair: Allocate a pair of linked local sockets. ; |
1280 | ; socket_pair: Allocate a pair of linked local sockets. ; |
1275 | ; ; |
1281 | ; ; |
1276 | ; IN: / ; |
1282 | ; IN: / ; |
1277 | ; ; |
1283 | ; ; |
1278 | ; OUT: eax = socket1 num on success ; |
1284 | ; OUT: eax = socket1 num on success ; |
1279 | ; eax = -1 on error ; |
1285 | ; eax = -1 on error ; |
1280 | ; ebx = socket2 num on success ; |
1286 | ; ebx = socket2 num on success ; |
1281 | ; ebx = errorcode on error ; |
1287 | ; ebx = errorcode on error ; |
1282 | ; ; |
1288 | ; ; |
1283 | ;-----------------------------------------------------------------; |
1289 | ;-----------------------------------------------------------------; |
1284 | align 4 |
1290 | align 4 |
1285 | socket_pair: |
1291 | socket_pair: |
1286 | 1292 | ||
1287 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_pair\n" |
1293 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_pair\n" |
1288 | 1294 | ||
1289 | call socket_alloc |
1295 | call socket_alloc |
1290 | test eax, eax |
1296 | test eax, eax |
1291 | jz .nomem1 |
1297 | jz .nomem1 |
1292 | mov [esp+32], edi ; application's eax |
1298 | mov [esp+32], edi ; application's eax |
1293 | 1299 | ||
1294 | mov [eax + SOCKET.Domain], AF_LOCAL |
1300 | mov [eax + SOCKET.Domain], AF_LOCAL |
1295 | mov [eax + SOCKET.Type], SOCK_STREAM |
1301 | mov [eax + SOCKET.Type], SOCK_STREAM |
1296 | mov [eax + SOCKET.Protocol], 0 ;;; CHECKME |
1302 | mov [eax + SOCKET.Protocol], 0 ;;; CHECKME |
1297 | mov [eax + SOCKET.snd_proc], socket_send_local |
1303 | mov [eax + SOCKET.snd_proc], socket_send_local |
1298 | mov [eax + SOCKET.rcv_proc], socket_receive_local |
1304 | mov [eax + SOCKET.rcv_proc], socket_receive_local |
1299 | mov [eax + SOCKET.PID], 0 |
1305 | mov [eax + SOCKET.PID], 0 |
1300 | mov ebx, eax |
1306 | mov ebx, eax |
1301 | 1307 | ||
1302 | call socket_alloc |
1308 | call socket_alloc |
1303 | test eax, eax |
1309 | test eax, eax |
1304 | jz .nomem2 |
1310 | jz .nomem2 |
1305 | mov [esp+20], edi ; application's ebx |
1311 | mov [esp+20], edi ; application's ebx |
1306 | 1312 | ||
1307 | mov [eax + SOCKET.Domain], AF_LOCAL |
1313 | mov [eax + SOCKET.Domain], AF_LOCAL |
1308 | mov [eax + SOCKET.Type], SOCK_STREAM |
1314 | mov [eax + SOCKET.Type], SOCK_STREAM |
1309 | mov [eax + SOCKET.Protocol], 0 ;;; CHECKME |
1315 | mov [eax + SOCKET.Protocol], 0 ;;; CHECKME |
1310 | mov [eax + SOCKET.snd_proc], socket_send_local |
1316 | mov [eax + SOCKET.snd_proc], socket_send_local |
1311 | mov [eax + SOCKET.rcv_proc], socket_receive_local |
1317 | mov [eax + SOCKET.rcv_proc], socket_receive_local |
1312 | mov [eax + SOCKET.PID], 0 |
1318 | mov [eax + SOCKET.PID], 0 |
1313 | 1319 | ||
1314 | ; Link the two sockets to eachother |
1320 | ; Link the two sockets to eachother |
1315 | mov [eax + SOCKET.device], ebx |
1321 | mov [eax + SOCKET.device], ebx |
1316 | mov [ebx + SOCKET.device], eax |
1322 | mov [ebx + SOCKET.device], eax |
1317 | 1323 | ||
1318 | lea eax, [eax + STREAM_SOCKET.rcv] |
1324 | lea eax, [eax + STREAM_SOCKET.rcv] |
1319 | call socket_ring_create |
1325 | call socket_ring_create |
1320 | test eax, eax |
1326 | test eax, eax |
1321 | jz .nomem2 |
1327 | jz .nomem2 |
1322 | 1328 | ||
1323 | lea eax, [ebx + STREAM_SOCKET.rcv] |
1329 | lea eax, [ebx + STREAM_SOCKET.rcv] |
1324 | call socket_ring_create |
1330 | call socket_ring_create |
1325 | test eax, eax |
1331 | test eax, eax |
1326 | jz .nomem2 |
1332 | jz .nomem2 |
1327 | 1333 | ||
1328 | ret |
1334 | ret |
1329 | 1335 | ||
1330 | .nomem2: |
1336 | .nomem2: |
1331 | mov eax, [esp+20] |
1337 | mov eax, [esp+20] |
1332 | call socket_free |
1338 | call socket_free |
1333 | 1339 | ||
1334 | .nomem1: |
1340 | .nomem1: |
1335 | mov eax, [esp+32] |
1341 | mov eax, [esp+32] |
1336 | call socket_free |
1342 | call socket_free |
1337 | 1343 | ||
1338 | mov dword[esp+32], -1 |
1344 | mov dword[esp+32], -1 |
1339 | mov dword[esp+20], ENOMEM |
1345 | mov dword[esp+20], ENOMEM |
1340 | ret |
1346 | ret |
1341 | 1347 | ||
1342 | 1348 | ||
1343 | 1349 | ||
1344 | ;-----------------------------------------------------------------; |
1350 | ;-----------------------------------------------------------------; |
1345 | ; ; |
1351 | ; ; |
1346 | ; socket_debug: Copy socket variables to application buffer. ; |
1352 | ; socket_debug: Copy socket variables to application buffer. ; |
1347 | ; ; |
1353 | ; ; |
1348 | ; IN: ecx = socket number ; |
1354 | ; IN: ecx = socket number ; |
1349 | ; edx = pointer to application buffer ; |
1355 | ; edx = pointer to application buffer ; |
1350 | ; ; |
1356 | ; ; |
1351 | ; OUT: eax = 0 on success ; |
1357 | ; OUT: eax = 0 on success ; |
1352 | ; eax = -1 on error ; |
1358 | ; eax = -1 on error ; |
1353 | ; ebx = errorcode on error ; |
1359 | ; ebx = errorcode on error ; |
1354 | ; ; |
1360 | ; ; |
1355 | ;-----------------------------------------------------------------; |
1361 | ;-----------------------------------------------------------------; |
1356 | align 4 |
1362 | align 4 |
1357 | socket_debug: |
1363 | socket_debug: |
1358 | 1364 | ||
1359 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_debug\n" |
1365 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_debug\n" |
1360 | 1366 | ||
1361 | mov edi, edx |
1367 | mov edi, edx |
1362 | 1368 | ||
1363 | test ecx, ecx |
1369 | test ecx, ecx |
1364 | jz .returnall |
1370 | jz .returnall |
1365 | 1371 | ||
1366 | call socket_num_to_ptr |
1372 | call socket_num_to_ptr |
1367 | test eax, eax |
1373 | test eax, eax |
1368 | jz .invalid |
1374 | jz .invalid |
1369 | 1375 | ||
1370 | mov esi, eax |
1376 | mov esi, eax |
1371 | mov ecx, SOCKET_STRUCT_SIZE/4 |
1377 | mov ecx, SOCKET_STRUCT_SIZE/4 |
1372 | rep movsd |
1378 | rep movsd |
1373 | 1379 | ||
1374 | mov dword[esp+32], 0 |
1380 | mov dword[esp+32], 0 |
1375 | ret |
1381 | ret |
1376 | 1382 | ||
1377 | .returnall: |
1383 | .returnall: |
1378 | mov ebx, net_sockets |
1384 | mov ebx, net_sockets |
1379 | .next_socket: |
1385 | .next_socket: |
1380 | mov ebx, [ebx + SOCKET.NextPtr] |
1386 | mov ebx, [ebx + SOCKET.NextPtr] |
1381 | test ebx, ebx |
1387 | test ebx, ebx |
1382 | jz .done |
1388 | jz .done |
1383 | mov eax, [ebx + SOCKET.Number] |
1389 | mov eax, [ebx + SOCKET.Number] |
1384 | stosd |
1390 | stosd |
1385 | jmp .next_socket |
1391 | jmp .next_socket |
1386 | .done: |
1392 | .done: |
1387 | xor eax, eax |
1393 | xor eax, eax |
1388 | stosd |
1394 | stosd |
1389 | mov dword[esp+32], eax |
1395 | mov dword[esp+32], eax |
1390 | ret |
1396 | ret |
1391 | 1397 | ||
1392 | .invalid: |
1398 | .invalid: |
1393 | mov dword[esp+32], -1 |
1399 | mov dword[esp+32], -1 |
1394 | mov dword[esp+20], EINVAL |
1400 | mov dword[esp+20], EINVAL |
1395 | ret |
1401 | ret |
1396 | 1402 | ||
1397 | 1403 | ||
1398 | ;-----------------------------------------------------------------; |
1404 | ;-----------------------------------------------------------------; |
1399 | ; ____ ____ ; |
1405 | ; ____ ____ ; |
1400 | ; \ / End of sockets API \ / ; |
1406 | ; \ / End of sockets API \ / ; |
1401 | ; \/ \/ ; |
1407 | ; \/ \/ ; |
1402 | ; () Internally used functions follow () ; |
1408 | ; () Internally used functions follow () ; |
1403 | ; ; |
1409 | ; ; |
1404 | ;-----------------------------------------------------------------; |
1410 | ;-----------------------------------------------------------------; |
1405 | 1411 | ||
1406 | 1412 | ||
1407 | ;-----------------------------------------------------------------; |
1413 | ;-----------------------------------------------------------------; |
1408 | ; ; |
1414 | ; ; |
1409 | ; socket_find_port: ; |
1415 | ; socket_find_port: ; |
1410 | ; Fill in the local port number for TCP and UDP sockets ; |
1416 | ; Fill in the local port number for TCP and UDP sockets ; |
1411 | ; This procedure always works because the number of sockets is ; |
1417 | ; This procedure always works because the number of sockets is ; |
1412 | ; limited to a smaller number then the number of possible ports ; |
1418 | ; limited to a smaller number then the number of possible ports ; |
1413 | ; ; |
1419 | ; ; |
1414 | ; IN: eax = socket pointer ; |
1420 | ; IN: eax = socket pointer ; |
1415 | ; ; |
1421 | ; ; |
1416 | ; OUT: / ; |
1422 | ; OUT: / ; |
1417 | ; ; |
1423 | ; ; |
1418 | ;-----------------------------------------------------------------; |
1424 | ;-----------------------------------------------------------------; |
1419 | align 4 |
1425 | align 4 |
1420 | socket_find_port: |
1426 | socket_find_port: |
1421 | 1427 | ||
1422 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_find_port\n" |
1428 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_find_port\n" |
1423 | 1429 | ||
1424 | push ebx esi ecx |
1430 | push ebx esi ecx |
1425 | 1431 | ||
1426 | cmp [eax + SOCKET.Protocol], IP_PROTO_UDP |
1432 | cmp [eax + SOCKET.Protocol], IP_PROTO_UDP |
1427 | je .udp |
1433 | je .udp |
1428 | 1434 | ||
1429 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
1435 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
1430 | je .tcp |
1436 | je .tcp |
1431 | 1437 | ||
1432 | pop ecx esi ebx |
1438 | pop ecx esi ebx |
1433 | ret |
1439 | ret |
1434 | 1440 | ||
1435 | .udp: |
1441 | .udp: |
1436 | mov bx, [last_UDP_port] |
1442 | mov bx, [last_UDP_port] |
1437 | call .findit |
1443 | call .findit |
1438 | mov [last_UDP_port], bx |
1444 | mov [last_UDP_port], bx |
1439 | 1445 | ||
1440 | pop ecx esi ebx |
1446 | pop ecx esi ebx |
1441 | ret |
1447 | ret |
1442 | 1448 | ||
1443 | .tcp: |
1449 | .tcp: |
1444 | mov bx, [last_TCP_port] |
1450 | mov bx, [last_TCP_port] |
1445 | call .findit |
1451 | call .findit |
1446 | mov [last_TCP_port], bx |
1452 | mov [last_TCP_port], bx |
1447 | 1453 | ||
1448 | pop ecx esi ebx |
1454 | pop ecx esi ebx |
1449 | ret |
1455 | ret |
1450 | 1456 | ||
1451 | 1457 | ||
1452 | .restart: |
1458 | .restart: |
1453 | mov bx, MIN_EPHEMERAL_PORT_N |
1459 | mov bx, MIN_EPHEMERAL_PORT_N |
1454 | .findit: |
1460 | .findit: |
1455 | cmp bx, MAX_EPHEMERAL_PORT_N |
1461 | cmp bx, MAX_EPHEMERAL_PORT_N |
1456 | je .restart |
1462 | je .restart |
1457 | 1463 | ||
1458 | add bh, 1 |
1464 | add bh, 1 |
1459 | adc bl, 0 |
1465 | adc bl, 0 |
1460 | 1466 | ||
1461 | call socket_check_port |
1467 | call socket_check_port |
1462 | jz .findit |
1468 | jz .findit |
1463 | ret |
1469 | ret |
1464 | 1470 | ||
1465 | 1471 | ||
1466 | 1472 | ||
1467 | ;-----------------------------------------------------------------; |
1473 | ;-----------------------------------------------------------------; |
1468 | ; ; |
1474 | ; ; |
1469 | ; socket_check_port: (to be used with AF_INET only!) ; |
1475 | ; socket_check_port: (to be used with AF_INET only!) ; |
1470 | ; Checks if a local port number is unused ; |
1476 | ; Checks if a local port number is unused ; |
1471 | ; If the proposed port number is unused, it is filled in in the ; |
1477 | ; If the proposed port number is unused, it is filled in in the ; |
1472 | ; socket structure. ; |
1478 | ; socket structure. ; |
1473 | ; ; |
1479 | ; ; |
1474 | ; IN: eax = socket ptr ; |
1480 | ; IN: eax = socket ptr ; |
1475 | ; bx = proposed socket number (network byte order) ; |
1481 | ; bx = proposed socket number (network byte order) ; |
1476 | ; ; |
1482 | ; ; |
1477 | ; OUT: ZF = set on error ; |
1483 | ; OUT: ZF = set on error ; |
1478 | ; ; |
1484 | ; ; |
1479 | ;-----------------------------------------------------------------; |
1485 | ;-----------------------------------------------------------------; |
1480 | align 4 |
1486 | align 4 |
1481 | socket_check_port: |
1487 | socket_check_port: |
1482 | 1488 | ||
1483 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check_port: " |
1489 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check_port: " |
1484 | 1490 | ||
1485 | pusha |
1491 | pusha |
1486 | mov ecx, socket_mutex |
1492 | mov ecx, socket_mutex |
1487 | call mutex_lock |
1493 | call mutex_lock |
1488 | popa |
1494 | popa |
1489 | 1495 | ||
1490 | mov ecx, [eax + SOCKET.Protocol] |
1496 | mov ecx, [eax + SOCKET.Protocol] |
1491 | mov edx, [eax + IP_SOCKET.LocalIP] |
1497 | mov edx, [eax + IP_SOCKET.LocalIP] |
1492 | mov esi, net_sockets |
1498 | mov esi, net_sockets |
1493 | 1499 | ||
1494 | .next_socket: |
1500 | .next_socket: |
1495 | mov esi, [esi + SOCKET.NextPtr] |
1501 | mov esi, [esi + SOCKET.NextPtr] |
1496 | or esi, esi |
1502 | or esi, esi |
1497 | jz .port_ok |
1503 | jz .port_ok |
1498 | 1504 | ||
1499 | cmp [esi + SOCKET.Protocol], ecx |
1505 | cmp [esi + SOCKET.Protocol], ecx |
1500 | jne .next_socket |
1506 | jne .next_socket |
1501 | 1507 | ||
1502 | cmp [esi + IP_SOCKET.LocalIP], edx |
1508 | cmp [esi + IP_SOCKET.LocalIP], edx |
1503 | jne .next_socket |
1509 | jne .next_socket |
1504 | 1510 | ||
1505 | cmp [esi + UDP_SOCKET.LocalPort], bx |
1511 | cmp [esi + UDP_SOCKET.LocalPort], bx |
1506 | jne .next_socket |
1512 | jne .next_socket |
1507 | 1513 | ||
1508 | pusha |
1514 | pusha |
1509 | mov ecx, socket_mutex |
1515 | mov ecx, socket_mutex |
1510 | call mutex_unlock |
1516 | call mutex_unlock |
1511 | popa |
1517 | popa |
1512 | 1518 | ||
1513 | DEBUGF DEBUG_NETWORK_VERBOSE, "local port %x already in use\n", bx ; FIXME: find a way to print big endian values with debugf |
1519 | DEBUGF DEBUG_NETWORK_VERBOSE, "local port %x already in use\n", bx ; FIXME: find a way to print big endian values with debugf |
1514 | ret |
1520 | ret |
1515 | 1521 | ||
1516 | .port_ok: |
1522 | .port_ok: |
1517 | pusha |
1523 | pusha |
1518 | mov ecx, socket_mutex |
1524 | mov ecx, socket_mutex |
1519 | call mutex_unlock |
1525 | call mutex_unlock |
1520 | popa |
1526 | popa |
1521 | 1527 | ||
1522 | DEBUGF DEBUG_NETWORK_VERBOSE, "local port %x is free\n", bx ; FIXME: find a way to print big endian values with debugf |
1528 | DEBUGF DEBUG_NETWORK_VERBOSE, "local port %x is free\n", bx ; FIXME: find a way to print big endian values with debugf |
1523 | mov [eax + UDP_SOCKET.LocalPort], bx |
1529 | mov [eax + UDP_SOCKET.LocalPort], bx |
1524 | or bx, bx ; clear the zero-flag |
1530 | or bx, bx ; clear the zero-flag |
1525 | ret |
1531 | ret |
1526 | 1532 | ||
1527 | 1533 | ||
1528 | 1534 | ||
1529 | ;-----------------------------------------------------------------; |
1535 | ;-----------------------------------------------------------------; |
1530 | ; ; |
1536 | ; ; |
1531 | ; socket_input: Update a (stateless) socket with received data. ; |
1537 | ; socket_input: Update a (stateless) socket with received data. ; |
1532 | ; ; |
1538 | ; ; |
1533 | ; Note: The socket's mutex should already be set ! ; |
1539 | ; Note: The socket's mutex should already be set ! ; |
1534 | ; ; |
1540 | ; ; |
1535 | ; IN: eax = socket ptr ; |
1541 | ; IN: eax = socket ptr ; |
1536 | ; ecx = data size ; |
1542 | ; ecx = data size ; |
1537 | ; esi = ptr to data ; |
1543 | ; esi = ptr to data ; |
1538 | ; [esp] = ptr to buf ; |
1544 | ; [esp] = ptr to buf ; |
1539 | ; ; |
1545 | ; ; |
1540 | ; OUT: / ; |
1546 | ; OUT: / ; |
1541 | ; ; |
1547 | ; ; |
1542 | ;-----------------------------------------------------------------; |
1548 | ;-----------------------------------------------------------------; |
1543 | align 4 |
1549 | align 4 |
1544 | socket_input: |
1550 | socket_input: |
1545 | 1551 | ||
1546 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx |
1552 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx |
1547 | 1553 | ||
1548 | push ecx |
1554 | push ecx |
1549 | push esi |
1555 | push esi |
1550 | mov esi, esp |
1556 | mov esi, esp |
1551 | 1557 | ||
1552 | add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .full |
1558 | add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .full |
1553 | 1559 | ||
1554 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_input: success\n" |
1560 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_input: success\n" |
1555 | add esp, sizeof.socket_queue_entry |
1561 | add esp, sizeof.socket_queue_entry |
1556 | 1562 | ||
1557 | pusha |
1563 | pusha |
1558 | lea ecx, [eax + SOCKET.mutex] |
1564 | lea ecx, [eax + SOCKET.mutex] |
1559 | call mutex_unlock |
1565 | call mutex_unlock |
1560 | popa |
1566 | popa |
1561 | 1567 | ||
1562 | jmp socket_notify |
1568 | jmp socket_notify |
1563 | 1569 | ||
1564 | .full: |
1570 | .full: |
1565 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_input: socket %x is full!\n", eax |
1571 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_input: socket %x is full!\n", eax |
1566 | 1572 | ||
1567 | pusha |
1573 | pusha |
1568 | lea ecx, [eax + SOCKET.mutex] |
1574 | lea ecx, [eax + SOCKET.mutex] |
1569 | call mutex_unlock |
1575 | call mutex_unlock |
1570 | popa |
1576 | popa |
1571 | 1577 | ||
1572 | add esp, 8 |
1578 | add esp, 8 |
1573 | call net_buff_free |
1579 | call net_buff_free |
1574 | ret |
1580 | ret |
1575 | 1581 | ||
1576 | 1582 | ||
1577 | ;-----------------------------------------------------------------; |
1583 | ;-----------------------------------------------------------------; |
1578 | ; ; |
1584 | ; ; |
1579 | ; socket_ring_create: Create a ringbuffer for sockets. ; |
1585 | ; socket_ring_create: Create a ringbuffer for sockets. ; |
1580 | ; ; |
1586 | ; ; |
1581 | ; IN: eax = ptr to ring struct ; |
1587 | ; IN: eax = ptr to ring struct ; |
1582 | ; ; |
1588 | ; ; |
1583 | ; OUT: eax = 0 on error ; |
1589 | ; OUT: eax = 0 on error ; |
1584 | ; eax = start ptr ; |
1590 | ; eax = start ptr ; |
1585 | ; ; |
1591 | ; ; |
1586 | ;-----------------------------------------------------------------; |
1592 | ;-----------------------------------------------------------------; |
1587 | align 4 |
1593 | align 4 |
1588 | socket_ring_create: |
1594 | socket_ring_create: |
1589 | 1595 | ||
1590 | push esi |
1596 | push esi |
1591 | mov esi, eax |
1597 | mov esi, eax |
1592 | 1598 | ||
1593 | push edx |
1599 | push edx |
1594 | stdcall create_ring_buffer, SOCKET_BUFFER_SIZE, PG_SWR |
1600 | stdcall create_ring_buffer, SOCKET_BUFFER_SIZE, PG_SWR |
1595 | pop edx |
1601 | pop edx |
1596 | test eax, eax |
1602 | test eax, eax |
1597 | jz .fail |
1603 | jz .fail |
1598 | 1604 | ||
1599 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_create: %x\n", eax |
1605 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_create: %x\n", eax |
1600 | 1606 | ||
1601 | pusha |
1607 | pusha |
1602 | lea ecx, [esi + RING_BUFFER.mutex] |
1608 | lea ecx, [esi + RING_BUFFER.mutex] |
1603 | call mutex_init |
1609 | call mutex_init |
1604 | popa |
1610 | popa |
1605 | 1611 | ||
1606 | mov [esi + RING_BUFFER.start_ptr], eax |
1612 | mov [esi + RING_BUFFER.start_ptr], eax |
1607 | mov [esi + RING_BUFFER.write_ptr], eax |
1613 | mov [esi + RING_BUFFER.write_ptr], eax |
1608 | mov [esi + RING_BUFFER.read_ptr], eax |
1614 | mov [esi + RING_BUFFER.read_ptr], eax |
1609 | mov [esi + RING_BUFFER.size], 0 |
1615 | mov [esi + RING_BUFFER.size], 0 |
1610 | add eax, SOCKET_BUFFER_SIZE |
1616 | add eax, SOCKET_BUFFER_SIZE |
1611 | mov [esi + RING_BUFFER.end_ptr], eax |
1617 | mov [esi + RING_BUFFER.end_ptr], eax |
1612 | mov eax, esi |
1618 | mov eax, esi |
1613 | 1619 | ||
1614 | pop esi |
1620 | pop esi |
1615 | ret |
1621 | ret |
1616 | 1622 | ||
1617 | .fail: |
1623 | .fail: |
1618 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_ring_create: Out of memory!\n" |
1624 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_ring_create: Out of memory!\n" |
1619 | pop esi |
1625 | pop esi |
1620 | ret |
1626 | ret |
1621 | 1627 | ||
1622 | ;-----------------------------------------------------------------; |
1628 | ;-----------------------------------------------------------------; |
1623 | ; ; |
1629 | ; ; |
1624 | ; socket_ring_write: Write data to ring buffer. ; |
1630 | ; socket_ring_write: Write data to ring buffer. ; |
1625 | ; ; |
1631 | ; ; |
1626 | ; IN: eax = ptr to ring struct ; |
1632 | ; IN: eax = ptr to ring struct ; |
1627 | ; ecx = data size ; |
1633 | ; ecx = data size ; |
1628 | ; esi = ptr to data ; |
1634 | ; esi = ptr to data ; |
1629 | ; ; |
1635 | ; ; |
1630 | ; OUT: ecx = number of bytes stored ; |
1636 | ; OUT: ecx = number of bytes stored ; |
1631 | ; ; |
1637 | ; ; |
1632 | ;-----------------------------------------------------------------; |
1638 | ;-----------------------------------------------------------------; |
1633 | align 4 |
1639 | align 4 |
1634 | socket_ring_write: |
1640 | socket_ring_write: |
1635 | 1641 | ||
1636 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx |
1642 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx |
1637 | 1643 | ||
1638 | ; lock mutex |
1644 | ; lock mutex |
1639 | pusha |
1645 | pusha |
1640 | lea ecx, [eax + RING_BUFFER.mutex] |
1646 | lea ecx, [eax + RING_BUFFER.mutex] |
1641 | call mutex_lock ; TODO: check what registers this function actually destroys |
1647 | call mutex_lock ; TODO: check what registers this function actually destroys |
1642 | popa |
1648 | popa |
1643 | 1649 | ||
1644 | ; calculate available size |
1650 | ; calculate available size |
1645 | mov edi, SOCKET_BUFFER_SIZE |
1651 | mov edi, SOCKET_BUFFER_SIZE |
1646 | sub edi, [eax + RING_BUFFER.size] ; available buffer size in edi |
1652 | sub edi, [eax + RING_BUFFER.size] ; available buffer size in edi |
1647 | cmp ecx, edi |
1653 | cmp ecx, edi |
1648 | jbe .copy |
1654 | jbe .copy |
1649 | mov ecx, edi |
1655 | mov ecx, edi |
1650 | .copy: |
1656 | .copy: |
1651 | mov edi, [eax + RING_BUFFER.write_ptr] |
1657 | mov edi, [eax + RING_BUFFER.write_ptr] |
1652 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi |
1658 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi |
1653 | 1659 | ||
1654 | ; update write ptr |
1660 | ; update write ptr |
1655 | push edi |
1661 | push edi |
1656 | add edi, ecx |
1662 | add edi, ecx |
1657 | cmp edi, [eax + RING_BUFFER.end_ptr] |
1663 | cmp edi, [eax + RING_BUFFER.end_ptr] |
1658 | jb @f |
1664 | jb @f |
1659 | sub edi, SOCKET_BUFFER_SIZE ; WRAP |
1665 | sub edi, SOCKET_BUFFER_SIZE ; WRAP |
1660 | @@: |
1666 | @@: |
1661 | 1667 | ||
1662 | mov [eax + RING_BUFFER.write_ptr], edi |
1668 | mov [eax + RING_BUFFER.write_ptr], edi |
1663 | pop edi |
1669 | pop edi |
1664 | 1670 | ||
1665 | ; update size |
1671 | ; update size |
1666 | add [eax + RING_BUFFER.size], ecx |
1672 | add [eax + RING_BUFFER.size], ecx |
1667 | 1673 | ||
1668 | ; copy the data |
1674 | ; copy the data |
1669 | push ecx |
1675 | push ecx |
1670 | shr ecx, 1 |
1676 | shr ecx, 1 |
1671 | jnc .nb |
1677 | jnc .nb |
1672 | movsb |
1678 | movsb |
1673 | .nb: |
1679 | .nb: |
1674 | shr ecx, 1 |
1680 | shr ecx, 1 |
1675 | jnc .nw |
1681 | jnc .nw |
1676 | movsw |
1682 | movsw |
1677 | .nw: |
1683 | .nw: |
1678 | test ecx, ecx |
1684 | test ecx, ecx |
1679 | jz .nd |
1685 | jz .nd |
1680 | rep movsd |
1686 | rep movsd |
1681 | .nd: |
1687 | .nd: |
1682 | pop ecx |
1688 | pop ecx |
1683 | 1689 | ||
1684 | ; unlock mutex |
1690 | ; unlock mutex |
1685 | pusha |
1691 | pusha |
1686 | lea ecx, [eax + RING_BUFFER.mutex] |
1692 | lea ecx, [eax + RING_BUFFER.mutex] |
1687 | call mutex_unlock ; TODO: check what registers this function actually destroys |
1693 | call mutex_unlock ; TODO: check what registers this function actually destroys |
1688 | popa |
1694 | popa |
1689 | 1695 | ||
1690 | ret |
1696 | ret |
1691 | 1697 | ||
1692 | ;-----------------------------------------------------------------; |
1698 | ;-----------------------------------------------------------------; |
1693 | ; ; |
1699 | ; ; |
1694 | ; socket_ring_read: Read from ring buffer ; |
1700 | ; socket_ring_read: Read from ring buffer ; |
1695 | ; ; |
1701 | ; ; |
1696 | ; IN: eax = ring struct ptr ; |
1702 | ; IN: eax = ring struct ptr ; |
1697 | ; ecx = bytes to read ; |
1703 | ; ecx = bytes to read ; |
1698 | ; edx = offset ; |
1704 | ; edx = offset ; |
1699 | ; edi = ptr to buffer start ; |
1705 | ; edi = ptr to buffer start ; |
1700 | ; ; |
1706 | ; ; |
1701 | ; OUT: eax = unchanged ; |
1707 | ; OUT: eax = unchanged ; |
1702 | ; ecx = number of bytes read (0 on error) ; |
1708 | ; ecx = number of bytes read (0 on error) ; |
1703 | ; edx = destroyed ; |
1709 | ; edx = destroyed ; |
1704 | ; esi = destroyed ; |
1710 | ; esi = destroyed ; |
1705 | ; edi = ptr to buffer end ; |
1711 | ; edi = ptr to buffer end ; |
1706 | ; ; |
1712 | ; ; |
1707 | ;-----------------------------------------------------------------; |
1713 | ;-----------------------------------------------------------------; |
1708 | align 4 |
1714 | align 4 |
1709 | socket_ring_read: |
1715 | socket_ring_read: |
1710 | 1716 | ||
1711 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: ringbuff=%x ptr=%x size=%u offset=%x\n", eax, edi, ecx, edx |
1717 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: ringbuff=%x ptr=%x size=%u offset=%x\n", eax, edi, ecx, edx |
1712 | 1718 | ||
1713 | pusha |
1719 | pusha |
1714 | lea ecx, [eax + RING_BUFFER.mutex] |
1720 | lea ecx, [eax + RING_BUFFER.mutex] |
1715 | call mutex_lock ; TODO: check what registers this function actually destroys |
1721 | call mutex_lock ; TODO: check what registers this function actually destroys |
1716 | popa |
1722 | popa |
1717 | 1723 | ||
1718 | mov esi, [eax + RING_BUFFER.read_ptr] |
1724 | mov esi, [eax + RING_BUFFER.read_ptr] |
1719 | add esi, edx ; esi = start_ptr + offset |
1725 | add esi, edx ; esi = start_ptr + offset |
1720 | 1726 | ||
1721 | neg edx |
1727 | neg edx |
1722 | add edx, [eax + RING_BUFFER.size] ; edx = snd.size - offset |
1728 | add edx, [eax + RING_BUFFER.size] ; edx = snd.size - offset |
1723 | jle .no_data_at_all |
1729 | jle .no_data_at_all |
1724 | 1730 | ||
1725 | pusha |
1731 | pusha |
1726 | lea ecx, [eax + RING_BUFFER.mutex] |
1732 | lea ecx, [eax + RING_BUFFER.mutex] |
1727 | call mutex_unlock ; TODO: check what registers this function actually destroys |
1733 | call mutex_unlock ; TODO: check what registers this function actually destroys |
1728 | popa |
1734 | popa |
1729 | 1735 | ||
1730 | cmp ecx, edx |
1736 | cmp ecx, edx |
1731 | ja .less_data |
1737 | ja .less_data |
1732 | 1738 | ||
1733 | .copy: |
1739 | .copy: |
1734 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi |
1740 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi |
1735 | push ecx |
1741 | push ecx |
1736 | shr ecx, 1 |
1742 | shr ecx, 1 |
1737 | jnc .nb |
1743 | jnc .nb |
1738 | movsb |
1744 | movsb |
1739 | .nb: |
1745 | .nb: |
1740 | shr ecx, 1 |
1746 | shr ecx, 1 |
1741 | jnc .nw |
1747 | jnc .nw |
1742 | movsw |
1748 | movsw |
1743 | .nw: |
1749 | .nw: |
1744 | test ecx, ecx |
1750 | test ecx, ecx |
1745 | jz .nd |
1751 | jz .nd |
1746 | rep movsd |
1752 | rep movsd |
1747 | .nd: |
1753 | .nd: |
1748 | pop ecx |
1754 | pop ecx |
1749 | ret |
1755 | ret |
1750 | 1756 | ||
1751 | .no_data_at_all: |
1757 | .no_data_at_all: |
1752 | pusha |
1758 | pusha |
1753 | lea ecx, [eax + RING_BUFFER.mutex] |
1759 | lea ecx, [eax + RING_BUFFER.mutex] |
1754 | call mutex_unlock ; TODO: check what registers this function actually destroys |
1760 | call mutex_unlock ; TODO: check what registers this function actually destroys |
1755 | popa |
1761 | popa |
1756 | 1762 | ||
1757 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: no data at all!\n" |
1763 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: no data at all!\n" |
1758 | xor ecx, ecx |
1764 | xor ecx, ecx |
1759 | ret |
1765 | ret |
1760 | 1766 | ||
1761 | .less_data: |
1767 | .less_data: |
1762 | mov ecx, edx |
1768 | mov ecx, edx |
1763 | jmp .copy |
1769 | jmp .copy |
1764 | 1770 | ||
1765 | 1771 | ||
1766 | ;-----------------------------------------------------------------; |
1772 | ;-----------------------------------------------------------------; |
1767 | ; ; |
1773 | ; ; |
1768 | ; socket_ring_free: Free data from a ringbuffer. ; |
1774 | ; socket_ring_free: Free data from a ringbuffer. ; |
1769 | ; ; |
1775 | ; ; |
1770 | ; IN: eax = ptr to ring struct ; |
1776 | ; IN: eax = ptr to ring struct ; |
1771 | ; ecx = data size ; |
1777 | ; ecx = data size ; |
1772 | ; ; |
1778 | ; ; |
1773 | ; OUT: ecx = number of freed bytes ; |
1779 | ; OUT: ecx = number of freed bytes ; |
1774 | ; ; |
1780 | ; ; |
1775 | ;-----------------------------------------------------------------; |
1781 | ;-----------------------------------------------------------------; |
1776 | align 4 |
1782 | align 4 |
1777 | socket_ring_free: |
1783 | socket_ring_free: |
1778 | 1784 | ||
1779 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax |
1785 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax |
1780 | 1786 | ||
1781 | push eax ecx |
1787 | push eax ecx |
1782 | lea ecx, [eax + RING_BUFFER.mutex] |
1788 | lea ecx, [eax + RING_BUFFER.mutex] |
1783 | call mutex_lock ; TODO: check what registers this function actually destroys |
1789 | call mutex_lock ; TODO: check what registers this function actually destroys |
1784 | pop ecx eax |
1790 | pop ecx eax |
1785 | 1791 | ||
1786 | sub [eax + RING_BUFFER.size], ecx |
1792 | sub [eax + RING_BUFFER.size], ecx |
1787 | jb .error |
1793 | jb .error |
1788 | add [eax + RING_BUFFER.read_ptr], ecx |
1794 | add [eax + RING_BUFFER.read_ptr], ecx |
1789 | 1795 | ||
1790 | mov edx, [eax + RING_BUFFER.end_ptr] |
1796 | mov edx, [eax + RING_BUFFER.end_ptr] |
1791 | cmp [eax + RING_BUFFER.read_ptr], edx |
1797 | cmp [eax + RING_BUFFER.read_ptr], edx |
1792 | jb @f |
1798 | jb @f |
1793 | sub [eax + RING_BUFFER.read_ptr], SOCKET_BUFFER_SIZE |
1799 | sub [eax + RING_BUFFER.read_ptr], SOCKET_BUFFER_SIZE |
1794 | @@: |
1800 | @@: |
1795 | 1801 | ||
1796 | push eax ecx |
1802 | push eax ecx |
1797 | lea ecx, [eax + RING_BUFFER.mutex] ; TODO: check what registers this function actually destroys |
1803 | lea ecx, [eax + RING_BUFFER.mutex] ; TODO: check what registers this function actually destroys |
1798 | call mutex_unlock |
1804 | call mutex_unlock |
1799 | pop ecx eax |
1805 | pop ecx eax |
1800 | 1806 | ||
1801 | ret |
1807 | ret |
1802 | 1808 | ||
1803 | .error: ; we could free all available bytes, but that would be stupid, i guess.. |
1809 | .error: ; we could free all available bytes, but that would be stupid, i guess.. |
1804 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_free: buffer=%x error!\n", eax |
1810 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_free: buffer=%x error!\n", eax |
1805 | add [eax + RING_BUFFER.size], ecx |
1811 | add [eax + RING_BUFFER.size], ecx |
1806 | 1812 | ||
1807 | push eax |
1813 | push eax |
1808 | lea ecx, [eax + RING_BUFFER.mutex] |
1814 | lea ecx, [eax + RING_BUFFER.mutex] |
1809 | call mutex_unlock ; TODO: check what registers this function actually destroys |
1815 | call mutex_unlock ; TODO: check what registers this function actually destroys |
1810 | pop eax |
1816 | pop eax |
1811 | 1817 | ||
1812 | xor ecx, ecx |
1818 | xor ecx, ecx |
1813 | ret |
1819 | ret |
1814 | 1820 | ||
1815 | 1821 | ||
1816 | ;-----------------------------------------------------------------; |
1822 | ;-----------------------------------------------------------------; |
1817 | ; ; |
1823 | ; ; |
1818 | ; socket_block: Suspend the thread attached to a socket. ; |
1824 | ; socket_block: Suspend the thread attached to a socket. ; |
1819 | ; ; |
1825 | ; ; |
1820 | ; IN: eax = socket ptr ; |
1826 | ; IN: eax = socket ptr ; |
1821 | ; ; |
1827 | ; ; |
1822 | ; OUT: eax = unchanged ; |
1828 | ; OUT: eax = unchanged ; |
1823 | ; ; |
1829 | ; ; |
1824 | ;-----------------------------------------------------------------; |
1830 | ;-----------------------------------------------------------------; |
1825 | align 4 |
1831 | align 4 |
1826 | socket_block: |
1832 | socket_block: |
1827 | 1833 | ||
1828 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: %x\n", eax |
1834 | DEBUGF 1, "SOCKET_block: %x\n", eax |
1829 | 1835 | ||
1830 | push eax |
1836 | push eax |
1831 | 1837 | ||
1832 | pushf |
1838 | pushf |
1833 | cli |
1839 | cli |
1834 | 1840 | ||
1835 | ; Set the 'socket is blocked' flag |
1841 | ; Set the 'socket is blocked' flag |
1836 | or [eax + SOCKET.state], SS_BLOCKED |
1842 | or [eax + SOCKET.state], SS_BLOCKED |
1837 | 1843 | ||
1838 | ; Suspend the thread |
1844 | ; Suspend the thread |
1839 | push edx |
1845 | push edx |
1840 | mov edx, [TASK_BASE] |
1846 | mov edx, [TASK_BASE] |
1841 | mov [edx + TASKDATA.state], 1 ; Suspended |
1847 | mov [edx + TASKDATA.state], 1 ; Suspended |
1842 | 1848 | ||
1843 | ; Remember the thread ID so we can wake it up again |
1849 | ; Remember the thread ID so we can wake it up again |
1844 | mov edx, [edx + TASKDATA.pid] |
1850 | mov edx, [edx + TASKDATA.pid] |
1845 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: suspending thread: %u\n", edx |
1851 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: suspending thread: %u\n", edx |
1846 | mov [eax + SOCKET.TID], edx |
1852 | mov [eax + SOCKET.TID], edx |
1847 | pop edx |
1853 | pop edx |
1848 | popf |
1854 | popf |
1849 | 1855 | ||
1850 | call change_task |
1856 | call change_task |
1851 | pop eax |
1857 | pop eax |
1852 | 1858 | ||
1853 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: continuing\n" |
1859 | DEBUGF 1, "SOCKET_block: continuing: %x\n", eax |
1854 | 1860 | ||
1855 | ret |
1861 | ret |
1856 | 1862 | ||
1857 | 1863 | ||
1858 | ;-----------------------------------------------------------------; |
1864 | ;-----------------------------------------------------------------; |
1859 | ; ; |
1865 | ; ; |
1860 | ; socket_notify: Wake up socket owner thread. ; |
1866 | ; socket_notify: Wake up socket owner thread. ; |
1861 | ; ; |
1867 | ; ; |
1862 | ; IN: eax = socket ptr ; |
1868 | ; IN: eax = socket ptr ; |
1863 | ; ; |
1869 | ; ; |
1864 | ; OUT: eax = unchanged ; |
1870 | ; OUT: eax = unchanged ; |
1865 | ; ; |
1871 | ; ; |
1866 | ;-----------------------------------------------------------------; |
1872 | ;-----------------------------------------------------------------; |
1867 | align 4 |
1873 | align 4 |
1868 | socket_notify: |
1874 | socket_notify: |
1869 | 1875 | ||
1870 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: %x\n", eax |
1876 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: %x\n", eax |
1871 | 1877 | ||
1872 | call socket_check |
1878 | call socket_check |
1873 | jz .error |
1879 | jz .error |
1874 | 1880 | ||
1875 | ; Find the associated thread's TASK_DATA |
1881 | ; Find the associated thread's TASK_DATA |
1876 | push ebx ecx esi |
1882 | push ebx ecx esi |
1877 | mov ebx, [eax + SOCKET.TID] |
1883 | mov ebx, [eax + SOCKET.TID] |
1878 | test ebx, ebx |
1884 | test ebx, ebx |
1879 | jz .error2 |
1885 | jz .error2 |
1880 | xor ecx, ecx |
1886 | xor ecx, ecx |
1881 | inc ecx |
1887 | inc ecx |
1882 | mov esi, TASK_DATA |
1888 | mov esi, TASK_DATA |
1883 | .next: |
1889 | .next: |
1884 | cmp [esi + TASKDATA.pid], ebx |
1890 | cmp [esi + TASKDATA.pid], ebx |
1885 | je .found |
1891 | je .found |
1886 | inc ecx |
1892 | inc ecx |
1887 | add esi, 0x20 |
1893 | add esi, 0x20 |
1888 | cmp ecx, [TASK_COUNT] |
1894 | cmp ecx, [TASK_COUNT] |
1889 | jbe .next |
1895 | jbe .next |
1890 | 1896 | ||
1891 | .error2: |
1897 | .error2: |
1892 | ; PID not found, TODO: close socket! |
1898 | ; PID not found, TODO: close socket! |
1893 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_notify: error finding thread 0x%x !\n", ebx |
1899 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_notify: error finding thread 0x%x !\n", ebx |
1894 | pop esi ecx ebx |
1900 | pop esi ecx ebx |
1895 | ret |
1901 | ret |
1896 | 1902 | ||
1897 | .error: |
1903 | .error: |
1898 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_notify: invalid socket ptr: 0x%x !\n", eax |
1904 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_notify: invalid socket ptr: 0x%x !\n", eax |
1899 | ret |
1905 | ret |
1900 | 1906 | ||
1901 | .found: |
1907 | .found: |
1902 | test [eax + SOCKET.state], SS_BLOCKED |
1908 | test [eax + SOCKET.state], SS_BLOCKED |
1903 | jnz .un_block |
1909 | jnz .un_block |
1904 | 1910 | ||
1905 | ; Socket and thread exists and socket is of non blocking type. |
1911 | ; Socket and thread exists and socket is of non blocking type. |
1906 | ; We'll try to flag an event to the thread. |
1912 | ; We'll try to flag an event to the thread. |
1907 | shl ecx, 8 |
1913 | shl ecx, 8 |
1908 | or [SLOT_BASE + ecx + APPDATA.event_mask], EVENT_NETWORK |
1914 | or [SLOT_BASE + ecx + APPDATA.event_mask], EVENT_NETWORK |
1909 | 1915 | ||
1910 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: poking thread %u!\n", ebx |
1916 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: poking thread %u!\n", ebx |
1911 | pop esi ecx ebx |
1917 | pop esi ecx ebx |
1912 | ret |
1918 | ret |
1913 | 1919 | ||
1914 | 1920 | ||
1915 | .un_block: |
1921 | .un_block: |
1916 | ; Socket and thread exists and socket is of blocking type |
1922 | ; Socket and thread exists and socket is of blocking type |
1917 | ; We'll try to unblock it. |
1923 | ; We'll try to unblock it. |
1918 | and [eax + SOCKET.state], not SS_BLOCKED ; Clear the 'socket is blocked' flag |
1924 | and [eax + SOCKET.state], not SS_BLOCKED ; Clear the 'socket is blocked' flag |
1919 | mov [esi + TASKDATA.state], 0 ; Run the thread |
1925 | mov [esi + TASKDATA.state], 0 ; Run the thread |
1920 | 1926 | ||
1921 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: Unblocked socket!\n" |
1927 | DEBUGF 1, "SOCKET_notify: Unblocked socket!\n" |
1922 | pop esi ecx ebx |
1928 | pop esi ecx ebx |
1923 | ret |
1929 | ret |
1924 | 1930 | ||
1925 | 1931 | ||
1926 | ;-----------------------------------------------------------------; |
1932 | ;-----------------------------------------------------------------; |
1927 | ; ; |
1933 | ; ; |
1928 | ; socket_alloc: Allocate memory for socket and put new socket ; |
1934 | ; socket_alloc: Allocate memory for socket and put new socket ; |
1929 | ; into the list. Newly created socket is initialized with calling ; |
1935 | ; into the list. Newly created socket is initialized with calling ; |
1930 | ; PID and given a socket number. ; |
1936 | ; PID and given a socket number. ; |
1931 | ; ; |
1937 | ; ; |
1932 | ; IN: / ; |
1938 | ; IN: / ; |
1933 | ; ; |
1939 | ; ; |
1934 | ; OUT: eax = socket ptr on success ; |
1940 | ; OUT: eax = socket ptr on success ; |
1935 | ; eax = 0 on error ; |
1941 | ; eax = 0 on error ; |
1936 | ; edi = socket number on success ; |
1942 | ; edi = socket number on success ; |
1937 | ; ; |
1943 | ; ; |
1938 | ;-----------------------------------------------------------------; |
1944 | ;-----------------------------------------------------------------; |
1939 | align 4 |
1945 | align 4 |
1940 | socket_alloc: |
1946 | socket_alloc: |
1941 | 1947 | ||
1942 | push ebx |
1948 | push ebx |
1943 | 1949 | ||
1944 | stdcall kernel_alloc, SOCKET_STRUCT_SIZE |
1950 | stdcall kernel_alloc, SOCKET_STRUCT_SIZE |
1945 | or eax, eax |
1951 | or eax, eax |
1946 | jz .nomem |
1952 | jz .nomem |
1947 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: ptr=%x\n", eax |
1953 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: ptr=%x\n", eax |
1948 | 1954 | ||
1949 | ; zero-initialize allocated memory |
1955 | ; zero-initialize allocated memory |
1950 | push eax |
1956 | push eax |
1951 | mov edi, eax |
1957 | mov edi, eax |
1952 | mov ecx, SOCKET_STRUCT_SIZE / 4 |
1958 | mov ecx, SOCKET_STRUCT_SIZE / 4 |
1953 | xor eax, eax |
1959 | xor eax, eax |
1954 | rep stosd |
1960 | rep stosd |
1955 | pop eax |
1961 | pop eax |
1956 | 1962 | ||
1957 | ; set send-and receive procedures to return -1 |
1963 | ; set send-and receive procedures to return -1 |
1958 | mov [eax + SOCKET.snd_proc], .not_yet |
1964 | mov [eax + SOCKET.snd_proc], .not_yet |
1959 | mov [eax + SOCKET.rcv_proc], .not_yet |
1965 | mov [eax + SOCKET.rcv_proc], .not_yet |
1960 | 1966 | ||
1961 | pusha |
1967 | pusha |
1962 | mov ecx, socket_mutex |
1968 | mov ecx, socket_mutex |
1963 | call mutex_lock |
1969 | call mutex_lock |
1964 | popa |
1970 | popa |
1965 | 1971 | ||
1966 | ; find first free socket number and use it |
1972 | ; find first free socket number and use it |
1967 | mov edi, [last_socket_num] |
1973 | mov edi, [last_socket_num] |
1968 | .next_socket_number: |
1974 | .next_socket_number: |
1969 | inc edi |
1975 | inc edi |
1970 | jz .next_socket_number ; avoid socket nr 0 |
1976 | jz .next_socket_number ; avoid socket nr 0 |
1971 | cmp edi, -1 |
1977 | cmp edi, -1 |
1972 | je .next_socket_number ; avoid socket nr -1 |
1978 | je .next_socket_number ; avoid socket nr -1 |
1973 | mov ebx, net_sockets |
1979 | mov ebx, net_sockets |
1974 | .next_socket: |
1980 | .next_socket: |
1975 | mov ebx, [ebx + SOCKET.NextPtr] |
1981 | mov ebx, [ebx + SOCKET.NextPtr] |
1976 | test ebx, ebx |
1982 | test ebx, ebx |
1977 | jz .last_socket |
1983 | jz .last_socket |
1978 | 1984 | ||
1979 | cmp [ebx + SOCKET.Number], edi |
1985 | cmp [ebx + SOCKET.Number], edi |
1980 | jne .next_socket |
1986 | jne .next_socket |
1981 | jmp .next_socket_number |
1987 | jmp .next_socket_number |
1982 | 1988 | ||
1983 | .last_socket: |
1989 | .last_socket: |
1984 | mov [last_socket_num], edi |
1990 | mov [last_socket_num], edi |
1985 | mov [eax + SOCKET.Number], edi |
1991 | mov [eax + SOCKET.Number], edi |
1986 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: number=%u\n", edi |
1992 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: number=%u\n", edi |
1987 | 1993 | ||
1988 | ; Fill in PID |
1994 | ; Fill in PID |
1989 | mov ebx, [TASK_BASE] |
1995 | mov ebx, [TASK_BASE] |
1990 | mov ebx, [ebx + TASKDATA.pid] |
1996 | mov ebx, [ebx + TASKDATA.pid] |
1991 | mov [eax + SOCKET.PID], ebx |
1997 | mov [eax + SOCKET.PID], ebx |
1992 | mov [eax + SOCKET.TID], ebx ; currently TID = PID in kolibrios :( |
1998 | mov [eax + SOCKET.TID], ebx ; currently TID = PID in kolibrios :( |
1993 | 1999 | ||
1994 | ; init mutex |
2000 | ; init mutex |
1995 | pusha |
2001 | pusha |
1996 | lea ecx, [eax + SOCKET.mutex] |
2002 | lea ecx, [eax + SOCKET.mutex] |
1997 | call mutex_init |
2003 | call mutex_init |
1998 | popa |
2004 | popa |
1999 | 2005 | ||
2000 | ; add socket to the list by re-arranging some pointers |
2006 | ; add socket to the list by re-arranging some pointers |
2001 | mov ebx, [net_sockets + SOCKET.NextPtr] |
2007 | mov ebx, [net_sockets + SOCKET.NextPtr] |
2002 | 2008 | ||
2003 | mov [eax + SOCKET.PrevPtr], net_sockets |
2009 | mov [eax + SOCKET.PrevPtr], net_sockets |
2004 | mov [eax + SOCKET.NextPtr], ebx |
2010 | mov [eax + SOCKET.NextPtr], ebx |
2005 | 2011 | ||
2006 | test ebx, ebx |
2012 | test ebx, ebx |
2007 | jz @f |
2013 | jz @f |
2008 | 2014 | ||
2009 | pusha |
2015 | pusha |
2010 | lea ecx, [ebx + SOCKET.mutex] |
2016 | lea ecx, [ebx + SOCKET.mutex] |
2011 | call mutex_lock |
2017 | call mutex_lock |
2012 | popa |
2018 | popa |
2013 | 2019 | ||
2014 | mov [ebx + SOCKET.PrevPtr], eax |
2020 | mov [ebx + SOCKET.PrevPtr], eax |
2015 | 2021 | ||
2016 | pusha |
2022 | pusha |
2017 | lea ecx, [ebx + SOCKET.mutex] |
2023 | lea ecx, [ebx + SOCKET.mutex] |
2018 | call mutex_unlock |
2024 | call mutex_unlock |
2019 | popa |
2025 | popa |
2020 | @@: |
2026 | @@: |
2021 | 2027 | ||
2022 | mov [net_sockets + SOCKET.NextPtr], eax |
2028 | mov [net_sockets + SOCKET.NextPtr], eax |
2023 | 2029 | ||
2024 | pusha |
2030 | pusha |
2025 | mov ecx, socket_mutex |
2031 | mov ecx, socket_mutex |
2026 | call mutex_unlock |
2032 | call mutex_unlock |
2027 | popa |
2033 | popa |
2028 | pop ebx |
2034 | pop ebx |
2029 | 2035 | ||
2030 | ret |
2036 | ret |
2031 | 2037 | ||
2032 | .nomem: |
2038 | .nomem: |
2033 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_alloc: Out of memory!\n" |
2039 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_alloc: Out of memory!\n" |
2034 | pop ebx |
2040 | pop ebx |
2035 | ret |
2041 | ret |
2036 | 2042 | ||
2037 | .not_yet: |
2043 | .not_yet: |
2038 | mov dword[esp+20], ENOTCONN |
2044 | mov dword[esp+20], ENOTCONN |
2039 | mov dword[esp+32], -1 |
2045 | mov dword[esp+32], -1 |
2040 | ret |
2046 | ret |
2041 | 2047 | ||
2042 | 2048 | ||
2043 | ;-----------------------------------------------------------------; |
2049 | ;-----------------------------------------------------------------; |
2044 | ; ; |
2050 | ; ; |
2045 | ; socket_free: Free socket data memory and remove socket from ; |
2051 | ; socket_free: Free socket data memory and remove socket from ; |
2046 | ; the list. Caller should lock and unlock socket_mutex. ; |
2052 | ; the list. Caller should lock and unlock socket_mutex. ; |
2047 | ; ; |
2053 | ; ; |
2048 | ; IN: eax = socket ptr ; |
2054 | ; IN: eax = socket ptr ; |
2049 | ; ; |
2055 | ; ; |
2050 | ; OUT: / ; |
2056 | ; OUT: / ; |
2051 | ; ; |
2057 | ; ; |
2052 | ;-----------------------------------------------------------------; |
2058 | ;-----------------------------------------------------------------; |
2053 | align 4 |
2059 | align 4 |
2054 | socket_free: |
2060 | socket_free: |
2055 | 2061 | ||
2056 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: %x\n", eax |
2062 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: %x\n", eax |
2057 | 2063 | ||
2058 | call socket_check |
2064 | call socket_check |
2059 | jz .error |
2065 | jz .error |
2060 | 2066 | ||
2061 | push ebx |
2067 | push ebx |
2062 | 2068 | ||
2063 | pusha |
2069 | pusha |
2064 | lea ecx, [eax + SOCKET.mutex] |
2070 | lea ecx, [eax + SOCKET.mutex] |
2065 | call mutex_lock |
2071 | call mutex_lock |
2066 | popa |
2072 | popa |
2067 | 2073 | ||
2068 | cmp [eax + SOCKET.Type], SOCK_STREAM |
2074 | cmp [eax + SOCKET.Type], SOCK_STREAM |
2069 | jne .no_stream |
2075 | jne .no_stream |
2070 | 2076 | ||
2071 | mov ebx, eax |
2077 | mov ebx, eax |
2072 | cmp [eax + STREAM_SOCKET.rcv.start_ptr], 0 |
2078 | cmp [eax + STREAM_SOCKET.rcv.start_ptr], 0 |
2073 | je @f |
2079 | je @f |
2074 | stdcall free_kernel_space, [eax + STREAM_SOCKET.rcv.start_ptr] |
2080 | stdcall free_kernel_space, [eax + STREAM_SOCKET.rcv.start_ptr] |
2075 | @@: |
2081 | @@: |
2076 | cmp [ebx + STREAM_SOCKET.snd.start_ptr], 0 |
2082 | cmp [ebx + STREAM_SOCKET.snd.start_ptr], 0 |
2077 | je @f |
2083 | je @f |
2078 | stdcall free_kernel_space, [ebx + STREAM_SOCKET.snd.start_ptr] |
2084 | stdcall free_kernel_space, [ebx + STREAM_SOCKET.snd.start_ptr] |
2079 | @@: |
2085 | @@: |
2080 | mov eax, ebx |
2086 | mov eax, ebx |
2081 | .no_stream: |
2087 | .no_stream: |
2082 | 2088 | ||
2083 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: freeing socket %x\n", eax |
2089 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: freeing socket %x\n", eax |
2084 | push eax ; this will be passed to kernel_free |
2090 | push eax ; this will be passed to kernel_free |
2085 | mov ebx, [eax + SOCKET.NextPtr] |
2091 | mov ebx, [eax + SOCKET.NextPtr] |
2086 | mov eax, [eax + SOCKET.PrevPtr] |
2092 | mov eax, [eax + SOCKET.PrevPtr] |
2087 | 2093 | ||
2088 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx |
2094 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx |
2089 | 2095 | ||
2090 | test eax, eax |
2096 | test eax, eax |
2091 | jz @f |
2097 | jz @f |
2092 | mov [eax + SOCKET.NextPtr], ebx |
2098 | mov [eax + SOCKET.NextPtr], ebx |
2093 | @@: |
2099 | @@: |
2094 | 2100 | ||
2095 | test ebx, ebx |
2101 | test ebx, ebx |
2096 | jz @f |
2102 | jz @f |
2097 | mov [ebx + SOCKET.PrevPtr], eax |
2103 | mov [ebx + SOCKET.PrevPtr], eax |
2098 | @@: |
2104 | @@: |
2099 | 2105 | ||
2100 | call kernel_free |
2106 | call kernel_free |
2101 | pop ebx |
2107 | pop ebx |
2102 | 2108 | ||
2103 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: success!\n" |
2109 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: success!\n" |
2104 | 2110 | ||
2105 | .error: |
2111 | .error: |
2106 | ret |
2112 | ret |
2107 | 2113 | ||
2108 | .error1: |
2114 | .error1: |
2109 | pop ebx |
2115 | pop ebx |
2110 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_free: error!\n" |
2116 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_free: error!\n" |
2111 | DEBUGF DEBUG_NETWORK_ERROR, "socket ptr=0x%x caller=0x%x\n", eax, [esp] |
2117 | DEBUGF DEBUG_NETWORK_ERROR, "socket ptr=0x%x caller=0x%x\n", eax, [esp] |
2112 | ret |
2118 | ret |
2113 | 2119 | ||
2114 | ;-----------------------------------------------------------------; |
2120 | ;-----------------------------------------------------------------; |
2115 | ; ; |
2121 | ; ; |
2116 | ; socket_fork: Create a child socket. ; |
2122 | ; socket_fork: Create a child socket. ; |
2117 | ; ; |
2123 | ; ; |
2118 | ; IN: ebx = socket number ; |
2124 | ; IN: ebx = socket number ; |
2119 | ; ; |
2125 | ; ; |
2120 | ; OUT: eax = child socket number on success ; |
2126 | ; OUT: eax = child socket number on success ; |
2121 | ; eax = 0 on error ; |
2127 | ; eax = 0 on error ; |
2122 | ; ; |
2128 | ; ; |
2123 | ;-----------------------------------------------------------------; |
2129 | ;-----------------------------------------------------------------; |
2124 | align 4 |
2130 | align 4 |
2125 | socket_fork: |
2131 | socket_fork: |
2126 | 2132 | ||
2127 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_fork: %x\n", ebx |
2133 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_fork: %x\n", ebx |
2128 | 2134 | ||
2129 | ; Exit if backlog queue is full |
2135 | ; Exit if backlog queue is full |
2130 | mov eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size] |
2136 | mov eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size] |
2131 | cmp ax, [ebx + SOCKET.backlog] |
2137 | cmp ax, [ebx + SOCKET.backlog] |
2132 | jae .fail |
2138 | jae .fail |
2133 | 2139 | ||
2134 | ; Allocate new socket |
2140 | ; Allocate new socket |
2135 | push ebx |
2141 | push ebx |
2136 | call socket_alloc |
2142 | call socket_alloc |
2137 | pop ebx |
2143 | pop ebx |
2138 | test eax, eax |
2144 | test eax, eax |
2139 | jz .fail |
2145 | jz .fail |
2140 | 2146 | ||
2141 | push eax |
2147 | push eax |
2142 | mov esi, esp |
2148 | mov esi, esp |
2143 | add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2 |
2149 | add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2 |
2144 | pop eax |
2150 | pop eax |
2145 | 2151 | ||
2146 | ; Copy structure from current socket to new |
2152 | ; Copy structure from current socket to new |
2147 | ; We start at PID to preserve the socket num, 2 pointers and mutex |
2153 | ; We start at PID to preserve the socket num, 2 pointers and mutex |
2148 | ; TID will be filled in later |
2154 | ; TID will be filled in later |
2149 | lea esi, [ebx + SOCKET.PID] |
2155 | lea esi, [ebx + SOCKET.PID] |
2150 | lea edi, [eax + SOCKET.PID] |
2156 | lea edi, [eax + SOCKET.PID] |
2151 | mov ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4 |
2157 | mov ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4 |
2152 | rep movsd |
2158 | rep movsd |
2153 | 2159 | ||
2154 | and [eax + SOCKET.options], not SO_ACCEPTCON |
2160 | and [eax + SOCKET.options], not SO_ACCEPTCON |
2155 | 2161 | ||
2156 | ; Notify owner of parent socket |
2162 | ; Notify owner of parent socket |
2157 | push eax |
2163 | push eax |
2158 | mov eax, ebx |
2164 | mov eax, ebx |
2159 | call socket_notify |
2165 | call socket_notify |
2160 | pop eax |
2166 | pop eax |
2161 | 2167 | ||
2162 | ret |
2168 | ret |
2163 | 2169 | ||
2164 | .fail2: |
2170 | .fail2: |
2165 | add esp, 4+4+4 |
2171 | add esp, 4+4+4 |
2166 | .fail: |
2172 | .fail: |
2167 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_fork: failed\n" |
2173 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_fork: failed\n" |
2168 | xor eax, eax |
2174 | xor eax, eax |
2169 | ret |
2175 | ret |
2170 | 2176 | ||
2171 | 2177 | ||
2172 | ;-----------------------------------------------------------------; |
2178 | ;-----------------------------------------------------------------; |
2173 | ; ; |
2179 | ; ; |
2174 | ; socket_num_to_ptr: Get socket structure address by its number. ; |
2180 | ; socket_num_to_ptr: Get socket structure address by its number. ; |
2175 | ; ; |
2181 | ; ; |
2176 | ; IN: ecx = socket number ; |
2182 | ; IN: ecx = socket number ; |
2177 | ; ; |
2183 | ; ; |
2178 | ; OUT: eax = socket ptr ; |
2184 | ; OUT: eax = socket ptr ; |
2179 | ; eax = 0 on error ; |
2185 | ; eax = 0 on error ; |
2180 | ; ; |
2186 | ; ; |
2181 | ;-----------------------------------------------------------------; |
2187 | ;-----------------------------------------------------------------; |
2182 | align 4 |
2188 | align 4 |
2183 | socket_num_to_ptr: |
2189 | socket_num_to_ptr: |
2184 | 2190 | ||
2185 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_num_to_ptr: num=%u ", ecx |
2191 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_num_to_ptr: num=%u ", ecx |
2186 | 2192 | ||
2187 | pusha |
2193 | pusha |
2188 | mov ecx, socket_mutex |
2194 | mov ecx, socket_mutex |
2189 | call mutex_lock |
2195 | call mutex_lock |
2190 | popa |
2196 | popa |
2191 | 2197 | ||
2192 | mov eax, net_sockets |
2198 | mov eax, net_sockets |
2193 | .next_socket: |
2199 | .next_socket: |
2194 | mov eax, [eax + SOCKET.NextPtr] |
2200 | mov eax, [eax + SOCKET.NextPtr] |
2195 | test eax, eax |
2201 | test eax, eax |
2196 | jz .error |
2202 | jz .error |
2197 | cmp [eax + SOCKET.Number], ecx |
2203 | cmp [eax + SOCKET.Number], ecx |
2198 | jne .next_socket |
2204 | jne .next_socket |
2199 | 2205 | ||
2200 | pusha |
2206 | pusha |
2201 | mov ecx, socket_mutex |
2207 | mov ecx, socket_mutex |
2202 | call mutex_unlock |
2208 | call mutex_unlock |
2203 | popa |
2209 | popa |
2204 | 2210 | ||
2205 | DEBUGF DEBUG_NETWORK_VERBOSE, "ptr=%x\n", eax |
2211 | DEBUGF DEBUG_NETWORK_VERBOSE, "ptr=%x\n", eax |
2206 | ret |
2212 | ret |
2207 | 2213 | ||
2208 | .error: |
2214 | .error: |
2209 | pusha |
2215 | pusha |
2210 | mov ecx, socket_mutex |
2216 | mov ecx, socket_mutex |
2211 | call mutex_unlock |
2217 | call mutex_unlock |
2212 | popa |
2218 | popa |
2213 | 2219 | ||
2214 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: socket %u not found!\n", eax |
2220 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: socket %u not found!\n", eax |
2215 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: caller = 0x%x\n", [esp] |
2221 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: caller = 0x%x\n", [esp] |
2216 | ret |
2222 | ret |
2217 | 2223 | ||
2218 | 2224 | ||
2219 | ;-----------------------------------------------------------------; |
2225 | ;-----------------------------------------------------------------; |
2220 | ; ; |
2226 | ; ; |
2221 | ; socket_ptr_to_num: Get socket number by its address. ; |
2227 | ; socket_ptr_to_num: Get socket number by its address. ; |
2222 | ; ; |
2228 | ; ; |
2223 | ; IN: eax = socket ptr ; |
2229 | ; IN: eax = socket ptr ; |
2224 | ; ; |
2230 | ; ; |
2225 | ; OUT: eax = socket number ; |
2231 | ; OUT: eax = socket number ; |
2226 | ; eax = 0 on error ; |
2232 | ; eax = 0 on error ; |
2227 | ; ZF = set on error ; |
2233 | ; ZF = set on error ; |
2228 | ; ; |
2234 | ; ; |
2229 | ;-----------------------------------------------------------------; |
2235 | ;-----------------------------------------------------------------; |
2230 | align 4 |
2236 | align 4 |
2231 | socket_ptr_to_num: |
2237 | socket_ptr_to_num: |
2232 | 2238 | ||
2233 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ptr_to_num: ptr=%x ", eax |
2239 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ptr_to_num: ptr=%x ", eax |
2234 | 2240 | ||
2235 | call socket_check |
2241 | call socket_check |
2236 | jz .error |
2242 | jz .error |
2237 | 2243 | ||
2238 | mov eax, [eax + SOCKET.Number] |
2244 | mov eax, [eax + SOCKET.Number] |
2239 | 2245 | ||
2240 | DEBUGF DEBUG_NETWORK_VERBOSE, "num=%u\n", eax |
2246 | DEBUGF DEBUG_NETWORK_VERBOSE, "num=%u\n", eax |
2241 | ret |
2247 | ret |
2242 | 2248 | ||
2243 | .error: |
2249 | .error: |
2244 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_ptr_to_num: not found\n", eax |
2250 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_ptr_to_num: not found\n", eax |
2245 | ret |
2251 | ret |
2246 | 2252 | ||
2247 | 2253 | ||
2248 | ;-----------------------------------------------------------------; |
2254 | ;-----------------------------------------------------------------; |
2249 | ; ; |
2255 | ; ; |
2250 | ; socket_check: Checks if the given ptr is really a socket ptr. ; |
2256 | ; socket_check: Checks if the given ptr is really a socket ptr. ; |
2251 | ; ; |
2257 | ; ; |
2252 | ; IN: eax = socket ptr ; |
2258 | ; IN: eax = socket ptr ; |
2253 | ; ; |
2259 | ; ; |
2254 | ; OUT: eax = 0 on error ; |
2260 | ; OUT: eax = 0 on error ; |
2255 | ; ZF = set on error ; |
2261 | ; ZF = set on error ; |
2256 | ; ; |
2262 | ; ; |
2257 | ;-----------------------------------------------------------------; |
2263 | ;-----------------------------------------------------------------; |
2258 | align 4 |
2264 | align 4 |
2259 | socket_check: |
2265 | socket_check: |
2260 | 2266 | ||
2261 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check: %x\n", eax |
2267 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check: %x\n", eax |
2262 | 2268 | ||
2263 | test eax, eax |
2269 | test eax, eax |
2264 | jz .error |
2270 | jz .error |
2265 | push ebx |
2271 | push ebx |
2266 | mov ebx, net_sockets |
2272 | mov ebx, net_sockets |
2267 | 2273 | ||
2268 | .next_socket: |
2274 | .next_socket: |
2269 | mov ebx, [ebx + SOCKET.NextPtr] |
2275 | mov ebx, [ebx + SOCKET.NextPtr] |
2270 | or ebx, ebx |
2276 | or ebx, ebx |
2271 | jz .done |
2277 | jz .done |
2272 | cmp ebx, eax |
2278 | cmp ebx, eax |
2273 | jnz .next_socket |
2279 | jnz .next_socket |
2274 | 2280 | ||
2275 | .done: |
2281 | .done: |
2276 | mov eax, ebx |
2282 | mov eax, ebx |
2277 | test eax, eax |
2283 | test eax, eax |
2278 | pop ebx |
2284 | pop ebx |
2279 | ret |
2285 | ret |
2280 | 2286 | ||
2281 | .error: |
2287 | .error: |
2282 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_check: called with argument 0\n" |
2288 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_check: called with argument 0\n" |
2283 | DEBUGF DEBUG_NETWORK_ERROR, "stack: 0x%x, 0x%x, 0x%x\n", [esp], [esp+4], [esp+8] |
2289 | DEBUGF DEBUG_NETWORK_ERROR, "stack: 0x%x, 0x%x, 0x%x\n", [esp], [esp+4], [esp+8] |
2284 | ret |
2290 | ret |
2285 | 2291 | ||
2286 | 2292 | ||
2287 | 2293 | ||
2288 | ;-----------------------------------------------------------------; |
2294 | ;-----------------------------------------------------------------; |
2289 | ; ; |
2295 | ; ; |
2290 | ; socket_check_owner: Check if the caller app owns the socket. ; |
2296 | ; socket_check_owner: Check if the caller app owns the socket. ; |
2291 | ; ; |
2297 | ; ; |
2292 | ; IN: eax = socket ptr ; |
2298 | ; IN: eax = socket ptr ; |
2293 | ; ; |
2299 | ; ; |
2294 | ; OUT: ZF = true/false ; |
2300 | ; OUT: ZF = true/false ; |
2295 | ; ; |
2301 | ; ; |
2296 | ;-----------------------------------------------------------------; |
2302 | ;-----------------------------------------------------------------; |
2297 | align 4 |
2303 | align 4 |
2298 | socket_check_owner: |
2304 | socket_check_owner: |
2299 | 2305 | ||
2300 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check_owner: %x\n", eax |
2306 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check_owner: %x\n", eax |
2301 | 2307 | ||
2302 | push ebx |
2308 | push ebx |
2303 | mov ebx, [TASK_BASE] |
2309 | mov ebx, [TASK_BASE] |
2304 | mov ebx, [ebx + TASKDATA.pid] |
2310 | mov ebx, [ebx + TASKDATA.pid] |
2305 | cmp [eax + SOCKET.PID], ebx |
2311 | cmp [eax + SOCKET.PID], ebx |
2306 | pop ebx |
2312 | pop ebx |
2307 | 2313 | ||
2308 | ret |
2314 | ret |
2309 | 2315 | ||
2310 | 2316 | ||
2311 | 2317 | ||
2312 | 2318 | ||
2313 | ;-----------------------------------------------------------------; |
2319 | ;-----------------------------------------------------------------; |
2314 | ; ; |
2320 | ; ; |
2315 | ; socket_process_end: Kernel calls this function when a certain ; |
2321 | ; socket_process_end: Kernel calls this function when a certain ; |
2316 | ; process ends. This function will check if the process had any ; |
2322 | ; process ends. This function will check if the process had any ; |
2317 | ; open sockets and update them accordingly (clean up). ; |
2323 | ; open sockets and update them accordingly (clean up). ; |
2318 | ; ; |
2324 | ; ; |
2319 | ; IN: edx = pid ; |
2325 | ; IN: edx = pid ; |
2320 | ; ; |
2326 | ; ; |
2321 | ; OUT: / ; |
2327 | ; OUT: / ; |
2322 | ; ; |
2328 | ; ; |
2323 | ;-----------------------------------------------------------------; |
2329 | ;-----------------------------------------------------------------; |
2324 | align 4 |
2330 | align 4 |
2325 | socket_process_end: |
2331 | socket_process_end: |
2326 | 2332 | ||
2327 | ret ; FIXME |
2333 | ret ; FIXME |
2328 | 2334 | ||
2329 | cmp [net_sockets + SOCKET.NextPtr], 0 ; Are there any active sockets at all? |
2335 | cmp [net_sockets + SOCKET.NextPtr], 0 ; Are there any active sockets at all? |
2330 | je .quickret ; nope, exit immediately |
2336 | je .quickret ; nope, exit immediately |
2331 | 2337 | ||
2332 | ; TODO: run the following code in another thread, to avoid deadlock |
2338 | ; TODO: run the following code in another thread, to avoid deadlock |
2333 | 2339 | ||
2334 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: %x\n", edx |
2340 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: %x\n", edx |
2335 | 2341 | ||
2336 | pusha |
2342 | pusha |
2337 | mov ecx, socket_mutex |
2343 | mov ecx, socket_mutex |
2338 | call mutex_lock |
2344 | call mutex_lock |
2339 | popa |
2345 | popa |
2340 | 2346 | ||
2341 | push ebx |
2347 | push ebx |
2342 | mov ebx, net_sockets |
2348 | mov ebx, net_sockets |
2343 | 2349 | ||
2344 | .next_socket: |
2350 | .next_socket: |
2345 | mov ebx, [ebx + SOCKET.NextPtr] |
2351 | mov ebx, [ebx + SOCKET.NextPtr] |
2346 | .next_socket_test: |
2352 | .next_socket_test: |
2347 | test ebx, ebx |
2353 | test ebx, ebx |
2348 | jz .done |
2354 | jz .done |
2349 | 2355 | ||
2350 | cmp [ebx + SOCKET.PID], edx |
2356 | cmp [ebx + SOCKET.PID], edx |
2351 | jne .next_socket |
2357 | jne .next_socket |
2352 | 2358 | ||
2353 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: killing socket %x\n", ebx |
2359 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: killing socket %x\n", ebx |
2354 | 2360 | ||
2355 | mov [ebx + SOCKET.PID], 0 |
2361 | mov [ebx + SOCKET.PID], 0 |
2356 | mov eax, ebx |
2362 | mov eax, ebx |
2357 | mov ebx, [ebx + SOCKET.NextPtr] |
2363 | mov ebx, [ebx + SOCKET.NextPtr] |
2358 | 2364 | ||
2359 | pusha |
2365 | pusha |
2360 | cmp [eax + SOCKET.Domain], AF_INET4 |
2366 | cmp [eax + SOCKET.Domain], AF_INET4 |
2361 | jne .free |
2367 | jne .free |
2362 | 2368 | ||
2363 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
2369 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
2364 | jne .free |
2370 | jne .free |
2365 | 2371 | ||
2366 | call tcp_disconnect |
2372 | call tcp_disconnect |
2367 | jmp .closed |
2373 | jmp .closed |
2368 | 2374 | ||
2369 | .free: |
2375 | .free: |
2370 | call socket_free |
2376 | call socket_free |
2371 | 2377 | ||
2372 | .closed: |
2378 | .closed: |
2373 | popa |
2379 | popa |
2374 | jmp .next_socket_test |
2380 | jmp .next_socket_test |
2375 | 2381 | ||
2376 | .done: |
2382 | .done: |
2377 | pop ebx |
2383 | pop ebx |
2378 | 2384 | ||
2379 | pusha |
2385 | pusha |
2380 | mov ecx, socket_mutex |
2386 | mov ecx, socket_mutex |
2381 | call mutex_unlock |
2387 | call mutex_unlock |
2382 | popa |
2388 | popa |
2383 | 2389 | ||
2384 | .quickret: |
2390 | .quickret: |
2385 | ret |
2391 | ret |
2386 | 2392 | ||
2387 | 2393 | ||
2388 | 2394 | ||
2389 | 2395 | ||
2390 | ;-----------------------------------------------------------------; |
2396 | ;-----------------------------------------------------------------; |
2391 | ; ; |
2397 | ; ; |
2392 | ; socket_is_connecting: Update socket state. ; |
2398 | ; socket_is_connecting: Update socket state. ; |
2393 | ; ; |
2399 | ; ; |
2394 | ; IN: eax = socket ptr ; |
2400 | ; IN: eax = socket ptr ; |
2395 | ; ; |
2401 | ; ; |
2396 | ; OUT: / ; |
2402 | ; OUT: / ; |
2397 | ; ; |
2403 | ; ; |
2398 | ;-----------------------------------------------------------------; |
2404 | ;-----------------------------------------------------------------; |
2399 | align 4 |
2405 | align 4 |
2400 | socket_is_connecting: |
2406 | socket_is_connecting: |
2401 | 2407 | ||
2402 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_is_connecting: %x\n", eax |
2408 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_is_connecting: %x\n", eax |
2403 | 2409 | ||
2404 | and [eax + SOCKET.state], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING) |
2410 | and [eax + SOCKET.state], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING) |
2405 | or [eax + SOCKET.state], SS_ISCONNECTING |
2411 | or [eax + SOCKET.state], SS_ISCONNECTING |
2406 | ret |
2412 | ret |
2407 | 2413 | ||
2408 | 2414 | ||
2409 | 2415 | ||
2410 | ;-----------------------------------------------------------------; |
2416 | ;-----------------------------------------------------------------; |
2411 | ; ; |
2417 | ; ; |
2412 | ; socket_is_connected: Update socket state. ; |
2418 | ; socket_is_connected: Update socket state. ; |
2413 | ; ; |
2419 | ; ; |
2414 | ; IN: eax = socket ptr ; |
2420 | ; IN: eax = socket ptr ; |
2415 | ; ; |
2421 | ; ; |
2416 | ; OUT: / ; |
2422 | ; OUT: / ; |
2417 | ; ; |
2423 | ; ; |
2418 | ;-----------------------------------------------------------------; |
2424 | ;-----------------------------------------------------------------; |
2419 | align 4 |
2425 | align 4 |
2420 | socket_is_connected: |
2426 | socket_is_connected: |
2421 | 2427 | ||
2422 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_is_connected: %x\n", eax |
2428 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_is_connected: %x\n", eax |
2423 | 2429 | ||
2424 | and [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING) |
2430 | and [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING) |
2425 | or [eax + SOCKET.state], SS_ISCONNECTED |
2431 | or [eax + SOCKET.state], SS_ISCONNECTED |
2426 | jmp socket_notify |
2432 | jmp socket_notify |
2427 | 2433 | ||
2428 | 2434 | ||
2429 | 2435 | ||
2430 | 2436 | ||
2431 | ;-----------------------------------------------------------------; |
2437 | ;-----------------------------------------------------------------; |
2432 | ; ; |
2438 | ; ; |
2433 | ; socket_is_disconnecting: Update socket state. ; |
2439 | ; socket_is_disconnecting: Update socket state. ; |
2434 | ; ; |
2440 | ; ; |
2435 | ; IN: eax = socket ptr ; |
2441 | ; IN: eax = socket ptr ; |
2436 | ; ; |
2442 | ; ; |
2437 | ; OUT: / ; |
2443 | ; OUT: / ; |
2438 | ; ; |
2444 | ; ; |
2439 | ;-----------------------------------------------------------------; |
2445 | ;-----------------------------------------------------------------; |
2440 | align 4 |
2446 | align 4 |
2441 | socket_is_disconnecting: |
2447 | socket_is_disconnecting: |
2442 | 2448 | ||
2443 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnecting: %x\n", eax |
2449 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnecting: %x\n", eax |
2444 | 2450 | ||
2445 | and [eax + SOCKET.state], not (SS_ISCONNECTING) |
2451 | and [eax + SOCKET.state], not (SS_ISCONNECTING) |
2446 | or [eax + SOCKET.state], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE |
2452 | or [eax + SOCKET.state], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE |
2447 | jmp socket_notify |
2453 | jmp socket_notify |
2448 | 2454 | ||
2449 | 2455 | ||
2450 | 2456 | ||
2451 | ;-----------------------------------------------------------------; |
2457 | ;-----------------------------------------------------------------; |
2452 | ; ; |
2458 | ; ; |
2453 | ; socket_is_disconnected: Update socket state. ; |
2459 | ; socket_is_disconnected: Update socket state. ; |
2454 | ; ; |
2460 | ; ; |
2455 | ; IN: eax = socket ptr ; |
2461 | ; IN: eax = socket ptr ; |
2456 | ; ; |
2462 | ; ; |
2457 | ; OUT: / ; |
2463 | ; OUT: / ; |
2458 | ; ; |
2464 | ; ; |
2459 | ;-----------------------------------------------------------------; |
2465 | ;-----------------------------------------------------------------; |
2460 | align 4 |
2466 | align 4 |
2461 | socket_is_disconnected: |
2467 | socket_is_disconnected: |
2462 | 2468 | ||
2463 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnected: %x\n", eax |
2469 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnected: %x\n", eax |
2464 | 2470 | ||
2465 | and [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING) |
2471 | and [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING) |
2466 | or [eax + SOCKET.state], SS_CANTRCVMORE + SS_CANTSENDMORE |
2472 | or [eax + SOCKET.state], SS_CANTRCVMORE + SS_CANTSENDMORE |
2467 | jmp socket_notify |
2473 | jmp socket_notify |
2468 | 2474 | ||
2469 | 2475 | ||
2470 | 2476 | ||
2471 | ;-----------------------------------------------------------------; |
2477 | ;-----------------------------------------------------------------; |
2472 | ; ; |
2478 | ; ; |
2473 | ; socket_cant_recv_more: Update socket state. ; |
2479 | ; socket_cant_recv_more: Update socket state. ; |
2474 | ; ; |
2480 | ; ; |
2475 | ; IN: eax = socket ptr ; |
2481 | ; IN: eax = socket ptr ; |
2476 | ; ; |
2482 | ; ; |
2477 | ; OUT: / ; |
2483 | ; OUT: / ; |
2478 | ; ; |
2484 | ; ; |
2479 | ;-----------------------------------------------------------------; |
2485 | ;-----------------------------------------------------------------; |
2480 | align 4 |
2486 | align 4 |
2481 | socket_cant_recv_more: |
2487 | socket_cant_recv_more: |
2482 | 2488 | ||
2483 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_cant_recv_more: %x\n", eax |
2489 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_cant_recv_more: %x\n", eax |
2484 | 2490 | ||
2485 | or [eax + SOCKET.state], SS_CANTRCVMORE |
2491 | or [eax + SOCKET.state], SS_CANTRCVMORE |
2486 | jmp socket_notify |
2492 | jmp socket_notify |
2487 | 2493 | ||
2488 | 2494 | ||
2489 | 2495 | ||
2490 | ;-----------------------------------------------------------------; |
2496 | ;-----------------------------------------------------------------; |
2491 | ; ; |
2497 | ; ; |
2492 | ; socket_cant_send_more: Update socket state. ; |
2498 | ; socket_cant_send_more: Update socket state. ; |
2493 | ; ; |
2499 | ; ; |
2494 | ; IN: eax = socket ptr ; |
2500 | ; IN: eax = socket ptr ; |
2495 | ; ; |
2501 | ; ; |
2496 | ; OUT: / ; |
2502 | ; OUT: / ; |
2497 | ; ; |
2503 | ; ; |
2498 | ;-----------------------------------------------------------------; |
2504 | ;-----------------------------------------------------------------; |
2499 | align 4 |
2505 | align 4 |
2500 | socket_cant_send_more: |
2506 | socket_cant_send_more: |
2501 | 2507 | ||
2502 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_cant_send_more: %x\n", eax |
2508 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_cant_send_more: %x\n", eax |
2503 | 2509 | ||
2504 | or [eax + SOCKET.state], SS_CANTSENDMORE |
2510 | or [eax + SOCKET.state], SS_CANTSENDMORE |
2505 | mov [eax + SOCKET.snd_proc], .notconn |
2511 | mov [eax + SOCKET.snd_proc], .notconn |
2506 | jmp socket_notify |
2512 | jmp socket_notify |
2507 | 2513 | ||
2508 | .notconn: |
2514 | .notconn: |
2509 | mov dword[esp+20], ENOTCONN |
2515 | mov dword[esp+20], ENOTCONN |
2510 | mov dword[esp+32], -1 |
2516 | mov dword[esp+32], -1 |
2511 | ret |
2517 | ret |