Subversion Repositories Kolibri OS

Rev

Rev 1473 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1473 Rev 1514
Line 1... Line 1...
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2004-2009. All rights reserved.    ;;
3
;; Copyright (C) KolibriOS team 2004-2010. 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
;;  SOCKET.INC                                                     ;;
6
;;  SOCKET.INC                                                     ;;
7
;;                                                                 ;;
7
;;                                                                 ;;
8
;;    Written by hidnplayr@kolibrios.org                           ;;
8
;;    Written by hidnplayr@kolibrios.org                           ;;
Line 11... Line 11...
11
;;          GNU GENERAL PUBLIC LICENSE                             ;;
11
;;          GNU GENERAL PUBLIC LICENSE                             ;;
12
;;             Version 2, June 1991                                ;;
12
;;             Version 2, June 1991                                ;;
13
;;                                                                 ;;
13
;;                                                                 ;;
14
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 15... Line 15...
15
 
15
 
Line -... Line 16...
-
 
16
$Revision: 1514 $
-
 
17
 
16
$Revision: 1473 $
18
virtual at 0
17
 
19
 
18
struct	SOCKET_head
20
	SOCKET:
19
	 .NextPtr		dd ? ; pointer to next socket in list
21
	.NextPtr		dd ? ; pointer to next socket in list
-
 
22
	.PrevPtr		dd ? ; pointer to previous socket in list
-
 
23
	.Number 		dd ? ; socket number
-
 
24
 
20
	 .PrevPtr		dd ? ; pointer to previous socket in list
25
	.lock			dd ? ; lock mutex
21
	 .Number		dd ? ; socket number (unique within single process)
26
 
22
	 .PID			dd ? ; application process id
27
	.PID			dd ? ; application process id
23
	 .Domain		dd ? ; INET/UNIX/..
28
	.Domain 		dd ? ; INET/UNIX/..
24
	 .Type			dd ? ; RAW/UDP/TCP/...
-
 
25
	 .Protocol		dd ? ; ICMP/IPv4/ARP/
29
	.Type			dd ? ; RAW/UDP/TCP/...
-
 
30
	.Protocol		dd ? ; ICMP/IPv4/ARP/
-
 
31
	.errorcode		dd ?
-
 
32
 
-
 
33
	.options		dd ?
-
 
34
	.SO_SND.SB_CC		dd ?  ;;;;; socket options: number of bytes in socket
-
 
35
	.SO_RCV.SB_CC		dd ?
26
	 .lock			dd ? ; lock mutex
36
	.state			dd ?  ;;;;;;;;;
27
	 .errorcode		dd ?
37
 
-
 
38
	.end:
-
 
39
end virtual
-
 
40
 
-
 
41
virtual at SOCKET.end
Line 28... Line -...
28
	 .end:
-
 
29
ends
42
 
-
 
43
	IP_SOCKET:
-
 
44
 
30
 
45
	.LocalIP		dd ?
31
struct	IPv4_SOCKET
46
				rd 3 ; for IPv6 addresses
-
 
47
 
-
 
48
	.RemoteIP		dd ?
-
 
49
				rd 3 ; for IPv6 addresses
Line -... Line 50...
-
 
50
 
-
 
51
	.end:
-
 
52
end virtual
-
 
53
 
32
	 .LocalIP		dd ?
54
virtual at SOCKET.end
Line 33... Line 55...
33
	 .RemoteIP		dd ?
55
 
34
	 .SequenceNumber	dd ?
56
	SOCKET_virtual:
Line -... Line 57...
-
 
57
 
-
 
58
	.ConnectedTo		dd ? ; Socket number of other socket this one is connected to
35
 
59
 
Line 36... Line 60...
36
	; todo: add options (for func 8 and 9)
60
	.end:
37
 
61
end virtual
Line 38... Line 62...
38
	 .end:
62
 
39
ends
63
virtual at IP_SOCKET.end
40
 
-
 
41
struct	TCP_SOCKET
-
 
42
 
-
 
43
	 .LocalPort		dw ? ; In INET byte order
-
 
-
 
64
 
44
	 .RemotePort		dw ? ; In INET byte order
65
	TCP_SOCKET:
45
 
66
 
46
	 .backlog		dw ? ; Backlog
-
 
Line 47... Line -...
47
	 .backlog_cur		dw ? ; current size of queue for un-accept-ed connections
-
 
48
	 .last_ack_number	dd ? ; used only to let application know that ACK has been received
67
	.LocalPort		dw ? ; In INET byte order
49
					; todo: may be use SND_UNA instead
68
	.RemotePort		dw ? ; In INET byte order
-
 
69
 
-
 
70
	.backlog		dw ? ; Backlog
-
 
71
	.backlog_cur		dw ? ; current size of queue for un-accept-ed connections
-
 
72
 
-
 
73
	.OrigRemoteIP		dd ? ; original remote IP address (used to reset to LISTEN state)
-
 
74
	.OrigRemotePort 	dw ? ; original remote port (used to reset to LISTEN state)
Line -... Line 75...
-
 
75
 
50
					; todo: may be use events which allow additional information instead
76
	.t_state		dd ? ; TCB state
-
 
77
	.t_timer		dd ? ; TCB timer (seconds)
51
					; todo: may be count acknowledged bytes (at least it has obvious sense)
78
	.t_rxtshift		dd ?
52
	 .OrigRemoteIP		dd ? ; original remote IP address (used to reset to LISTEN state)
79
	.t_rxtcur		dd ?
53
	 .OrigRemotePort	dw ? ; original remote port (used to reset to LISTEN state)
80
	.t_dupacks		dd ?
-
 
81
	.t_maxseg		dd ?
-
 
82
	.t_force		dd ?
-
 
83
	.t_flags		dd ?
-
 
84
 
54
	 .wndsizeTimer		dd ? ; window size timer
85
;---------------
-
 
86
; RFC783 page 21
-
 
87
 
-
 
88
; send sequence
55
 
89
	.SND_UNA		dd ? ; sequence number of unack'ed sent Packets
56
	; Transmission control block
90
	.SND_NXT		dd ? ; next send sequence number to use
57
	 .state 		dd ? ; TCB state
-
 
58
	 .timer 		dd ? ; TCB timer (seconds)
91
	.SND_UP 		dd ?
Line -... Line 92...
-
 
92
	.SND_WL1		dd ? ; window minus one
59
 
93
	.SND_WL2		dd ? ;
Line 60... Line 94...
60
	 .ISS			dd ? ; initial send sequence number
94
	.ISS			dd ? ; initial send sequence number
61
	 .IRS			dd ? ; initial receive sequence number
95
	.SND_WND		dd ? ; send window
Line 62... Line 96...
62
	 .SND_UNA		dd ? ; sequence number of unack'ed sent Packets
96
 
-
 
97
; receive sequence
Line -... Line 98...
-
 
98
	.RCV_WND		dw ? ; receive window
-
 
99
	.RCV_NXT		dd ? ; next receive sequence number to use
-
 
100
	.RCV_UP 		dd ?
-
 
101
	.IRS			dd ? ; initial receive sequence number
-
 
102
 
-
 
103
;---------------------
-
 
104
; Additional variables
-
 
105
 
-
 
106
; receive variables
-
 
107
	.RCV_ADV		dd ?
-
 
108
 
-
 
109
; retransmit variables
-
 
110
	.SND_MAX		dd ?
-
 
111
 
-
 
112
; congestion control
-
 
113
	.SND_CWND		dd ?
-
 
114
	.SND_SSTHRESH		dd ?
-
 
115
 
-
 
116
;----------------------
-
 
117
; Transmit timing stuff
-
 
118
 
-
 
119
	.t_idle 		dd ?
-
 
120
	.t_rtt			dd ?
-
 
121
	.t_rtseq		dd ?
-
 
122
	.t_srtt 		dd ?
-
 
123
	.t_rttvar		dd ?
63
	 .SND_NXT		dd ? ; next send sequence number to use
124
	.t_rttmin		dd ?
-
 
125
	.max_sndwnd		dd ?
-
 
126
 
64
	 .SND_WND		dd ? ; send window
127
;-----------------
-
 
128
; Out-of-band data
-
 
129
 
-
 
130
	.t_oobflags		dd ?
65
	 .RCV_NXT		dd ? ; next receive sequence number to use
131
	.t_iobc 		dd ?
Line 66... Line 132...
66
	 .RCV_WND		dd ? ; receive window
132
	.t_softerror		dd ?
67
	 .SEG_LEN		dd ? ; segment length
133
 
Line 68... Line 134...
68
	 .SEG_WND		dd ? ; segment window
134
 
Line -... Line 135...
-
 
135
;---------
-
 
136
; RFC 1323
-
 
137
 
-
 
138
	.SND_SCALE		db ? ; Scale factor
69
 
139
	.RCV_SCALE		db ?
Line 70... Line 140...
70
	 .flags 		db ? ; packet flags
140
	.request_r_scale	db ?
-
 
141
	.requested_s_scale	dd ?
Line 71... Line 142...
71
 
142
 
Line 72... Line 143...
72
	 .end:
143
	.ts_recent		dd ?
Line 73... Line 144...
73
ends
144
	.ts_recent_age		dd ?
Line 74... Line 145...
74
 
145
	.last_ack_sent		dd ?
-
 
146
 
Line 75... Line -...
75
struct	UDP_SOCKET
-
 
Line 76... Line 147...
76
 
147
	.end:
-
 
148
end virtual
77
	 .LocalPort		dw ? ; In INET byte order
149
 
-
 
150
virtual at IP_SOCKET.end
78
	 .RemotePort		dw ? ; In INET byte order
151
 
79
	 .firstpacket		db ?
-
 
80
 
152
	UDP_SOCKET:
81
	 .end:
153
 
Line -... Line 154...
-
 
154
	.LocalPort		dw ? ; In INET byte order
82
ends
155
	.RemotePort		dw ? ; In INET byte order
83
 
156
	.firstpacket		db ?
-
 
157
 
84
struct	ICMP_SOCKET
158
	.end:
85
 
159
end virtual
-
 
160
 
Line 86... Line 161...
86
	.Identifier		dw ? ;
161
virtual at IP_SOCKET.end
87
 
162
 
88
	.end:
163
	ICMP_SOCKET:
89
 
164
 
90
ends
165
	.Identifier		dw ? ;
Line 127... Line 202...
127
;
202
;
128
;-----------------------------------------------------------------
203
;-----------------------------------------------------------------
129
align 4
204
align 4
130
socket_init:
205
socket_init:
Line -... Line 206...
-
 
206
 
131
 
207
	xor	eax, eax
132
	mov	[net_sockets], 0
208
	mov	edi, net_sockets
-
 
209
	mov	ecx, 4
Line 133... Line 210...
133
	mov	[net_sockets + 4], 0
210
	rep	stosd
134
 
211
 
Line 135... Line 212...
135
	mov	[last_UDP_port], MIN_EPHEMERAL_PORT
212
	mov	[last_UDP_port], MIN_EPHEMERAL_PORT
Line 143... Line 220...
143
; Socket API (function 74)
220
; Socket API (function 74)
144
;
221
;
145
;-----------------------------------------------------------------
222
;-----------------------------------------------------------------
146
align 4
223
align 4
147
sys_socket:
224
sys_socket:
148
	and	ebx, 0x000000FF ; should i remove this line ?
-
 
149
	cmp	bl , 8		; highest possible number
225
	cmp	ebx, 8		; highest possible number
150
	jg	s_error
226
	jg	s_error
151
	lea	ebx, [.table + 4*ebx]
227
	lea	ebx, [.table + 4*ebx]
152
	jmp	dword [ebx]
228
	jmp	dword [ebx]
Line 153... Line 229...
153
 
229
 
154
.table:
230
.table:
155
	dd	socket_open	; 0
231
	dd	SOCKET_open	; 0
156
	dd	socket_close	; 1
232
	dd	SOCKET_close	; 1
157
	dd	socket_bind	; 2
233
	dd	SOCKET_bind	; 2
158
	dd	socket_listen	; 3
234
	dd	SOCKET_listen	; 3
159
	dd	socket_connect	; 4
235
	dd	SOCKET_connect	; 4
160
	dd	socket_accept	; 5
236
	dd	SOCKET_accept	; 5
161
	dd	socket_send	; 6
237
	dd	SOCKET_send	; 6
162
	dd	socket_recv	; 7
238
	dd	SOCKET_receive	; 7
163
	dd	socket_get_opt	; 8
239
	dd	SOCKET_get_opt	; 8
Line 164... Line 240...
164
;        dd      socket_set_opt  ; 9
240
;        dd      SOCKET_set_opt  ; 9
-
 
241
 
165
 
242
 
Line 166... Line 243...
166
 
243
s_error:
Line 167... Line 244...
167
s_error:
244
	DEBUGF	1,"socket error\n"
168
	mov	dword [esp+32],-1
245
	mov	dword [esp+32], -1
169
 
246
 
170
	ret
247
	ret
171
 
-
 
172
 
248
 
173
;-----------------------------------------------------------------
249
 
174
;
250
;-----------------------------------------------------------------
175
; SOCKET_open
251
;
176
;
252
; SOCKET_open
177
;
253
;
178
;  IN:  domain in ecx
254
;  IN:  domain in ecx
179
;       type in edx
255
;       type in edx
Line 180... Line 256...
180
;       protocol in esi
256
;       protocol in esi
Line 181... Line 257...
181
;  OUT: eax is socket num, -1 on error
257
;  OUT: eax is socket num, -1 on error
182
;
-
 
183
;-----------------------------------------------------------------
258
;
Line 184... Line 259...
184
align 4
259
;-----------------------------------------------------------------
185
socket_open:
260
align 4
186
 
261
SOCKET_open:
187
	DEBUGF	1,"socket_open: domain: %u, type: %u",ecx, edx
-
 
188
 
-
 
189
	call	net_socket_alloc
-
 
190
	or	eax, eax
-
 
191
	jz	s_error
-
 
Line 192... Line -...
192
 
-
 
193
	mov	[eax + SOCKET_head.Domain], ecx
-
 
194
	mov	[eax + SOCKET_head.Type], edx
-
 
195
	mov	[eax + SOCKET_head.Protocol], esi
-
 
196
 
-
 
197
	cmp	ecx, AF_INET4
-
 
198
	je	.af_inet4
-
 
199
 
-
 
200
	jmp	.done
-
 
201
 
-
 
202
 
-
 
203
  .af_inet4:
-
 
204
 
-
 
205
	cmp	edx, IP_PROTO_TCP
-
 
206
	je	.tcp
-
 
207
 
-
 
208
	jmp	.done
-
 
209
 
-
 
210
  .tcp:
-
 
211
 
-
 
212
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_CLOSED
262
 
Line 213... Line 263...
213
 
263
	DEBUGF	1,"socket_open: domain: %u, type: %u protocol: %x\n", ecx, edx, esi
Line 234... Line 284...
234
;       length of that struct in esi
284
;       length of that struct in esi
235
;  OUT: 0 on success
285
;  OUT: 0 on success
236
;
286
;
237
;-----------------------------------------------------------------
287
;-----------------------------------------------------------------
238
align 4
288
align 4
239
socket_bind:
289
SOCKET_bind:
Line 240... Line 290...
240
 
290
 
Line 241... Line 291...
241
	DEBUGF	1,"Socket_bind: socknum: %u sockaddr: %x, length: %u, ",ecx,edx,esi
291
	DEBUGF	1,"socket_bind: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
242
 
-
 
243
	stdcall net_socket_num_to_addr, ecx
292
 
Line 244... Line 293...
244
	cmp	eax, -1
293
	call	SOCKET_num_to_ptr
245
	jz	s_error
294
	jz	s_error
Line 262... Line 311...
262
	mov	dword [esp+32],0
311
	mov	dword [esp+32], 0
263
	ret
312
	ret
Line 264... Line 313...
264
 
313
 
Line -... Line 314...
-
 
314
  .af_inet4:
-
 
315
 
265
  .af_inet4:
316
	DEBUGF	1,"af_inet4\n"
266
 
317
 
Line 267... Line 318...
267
	cmp	esi, 6
318
	cmp	esi, 6
Line 268... Line 319...
268
	jl	s_error
319
	jl	s_error
269
 
-
 
270
	mov	ecx, [eax + SOCKET_head.Type]
320
 
271
 
321
	mov	ecx, [eax + SOCKET.Type]
Line 272... Line 322...
272
	mov	bx, word [edx + 2]
322
 
273
	DEBUGF	1,"local port: %x ",bx
323
	mov	bx, word [edx + 2]
274
	test	bx, bx
324
	test	bx, bx
275
	jz	.find_free
325
	jz	.find_free
Line 276... Line 326...
276
 
326
 
277
	call	socket_check_port
-
 
278
	test	bx, bx
327
	call	SOCKET_check_port
279
	je	s_error
328
;        test    bx, bx
280
	jmp	.got_port
329
	jz	s_error
Line 281... Line 330...
281
 
330
	jmp	.got_port
282
    .find_free:
331
 
283
 
332
    .find_free:
Line 284... Line 333...
284
	call	socket_find_port
333
	call	SOCKET_find_port
285
	test	bx, bx
334
;        test    bx, bx
Line 286... Line 335...
286
	je	s_error
335
	jz	s_error
287
 
336
 
288
    .got_port:
337
    .got_port:
Line 289... Line 338...
289
	DEBUGF	1,"using port: %x ",bx
338
	DEBUGF	1,"using local port: %u", bx
290
	mov	word [eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort], bx
339
	mov	word [eax + UDP_SOCKET.LocalPort], bx
Line 304... Line 353...
304
 
353
 
305
;-----------------------------------------------------------------
354
;-----------------------------------------------------------------
306
;
355
;
307
; SOCKET_connect
356
; SOCKET_connect
308
;
-
 
309
;
357
;
310
;  IN:  socket number in ecx
358
;  IN:  socket number in ecx
311
;       pointer to sockaddr struct in edx
359
;       pointer to sockaddr struct in edx
312
;       length of that struct in esi
360
;       length of that struct in esi
313
;  OUT: 0 on success
361
;  OUT: 0 on success
314
;
362
;
315
;-----------------------------------------------------------------
363
;-----------------------------------------------------------------
316
align 4
364
align 4
Line 317... Line 365...
317
socket_connect:
365
SOCKET_connect:
Line 318... Line 366...
318
 
366
 
319
	DEBUGF	1,"Socket_connect: socknum: %u sockaddr: %x, length: %u,",ecx,edx,esi
-
 
320
 
367
	DEBUGF	1,"socket_connect: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
Line 321... Line 368...
321
	stdcall net_socket_num_to_addr, ecx
368
 
322
	cmp	eax, -1
369
	call	SOCKET_num_to_ptr
Line 330... Line 377...
330
 
377
 
Line 331... Line 378...
331
	jmp	s_error
378
	jmp	s_error
Line 332... Line 379...
332
 
379
 
333
  .af_inet4:
380
  .af_inet4:
Line 334... Line 381...
334
 
381
 
335
	cmp	[eax + SOCKET_head.Type], IP_PROTO_UDP
382
	cmp	[eax + SOCKET.Type], IP_PROTO_UDP
Line 336... Line 383...
336
	je	.udp
383
	je	.udp
Line 337... Line 384...
337
 
384
 
338
	cmp	[eax + SOCKET_head.Type], IP_PROTO_TCP
-
 
339
	je	.tcp
385
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
340
 
386
	je	.tcp
341
	jmp	s_error
387
 
342
 
388
	jmp	s_error
Line 343... Line 389...
343
  .udp:
389
 
344
 
390
  .udp:
345
	mov	bx , word [edx + 2]
391
	mov	bx , word [edx + 2]
Line 346... Line 392...
346
	mov	word [eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.RemotePort], bx
392
	mov	word [eax + UDP_SOCKET.RemotePort], bx
347
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.firstpacket], 0
393
	mov	[eax + UDP_SOCKET.firstpacket], 0
Line 348... Line 394...
348
	DEBUGF	1,"remote port: %x ",bx
394
	DEBUGF	1,"remote port: %u ",bx
349
 
395
 
Line -... Line 396...
-
 
396
	mov	ebx, dword [edx + 4]
-
 
397
	mov	dword [eax + IP_SOCKET.RemoteIP], ebx
-
 
398
	DEBUGF	1,"remote ip: %u.%u.%u.%u\n",[edx+4]:1,[edx+5]:1,[edx+6]:1,[edx+7]:1
-
 
399
 
350
	mov	ebx, dword [edx + 4]
400
	mov	dword [esp+32], 0
351
	mov	dword [eax + SOCKET_head.end + IPv4_SOCKET.RemoteIP], ebx
401
	ret
Line 352... Line 402...
352
	DEBUGF	1,"remote ip: %u.%u.%u.%u\n",[edx+4]:1,[edx+5]:1,[edx+6]:1,[edx+7]:1
402
 
Line 353... Line 403...
353
 
403
 
354
	mov	dword [esp+32],0
-
 
-
 
404
  .tcp:
355
	ret
405
	; set sequence number
356
 
406
 
357
 
407
	mov	ebx, [TCP_sequence_num]
Line 358... Line 408...
358
  .tcp:
408
	add	[TCP_sequence_num], 6400
359
	; TODO: set sequence number to random value
409
	mov	[eax + TCP_SOCKET.ISS], ebx
Line 360... Line 410...
360
 
410
 
Line 361... Line 411...
361
	lea	ebx, [eax + SOCKET_head.lock]
411
	lea	ebx, [eax + SOCKET.lock]
362
	call	wait_mutex
412
	call	wait_mutex
363
 
413
 
364
	; fill in remote port and IP
414
	; fill in remote port and IP
365
 
415
 
Line 366... Line 416...
366
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.wndsizeTimer], 0	   ; Reset the window timer.
416
;;;;;;        mov     [eax + TCP_SOCKET.wndsizeTimer], 0     ; Reset the window timer.
367
											   ; TODO: figure out WTF this is
417
 
368
	mov	bx , word [edx + 2]
-
 
369
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RemotePort], bx
-
 
370
	DEBUGF	1,"remote port: %x ",bx
418
	mov	bx , word [edx + 2]
371
 
-
 
372
	mov	ebx, dword [edx + 4]
-
 
373
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.RemoteIP], ebx
-
 
374
 
-
 
375
	; check if local port and IP is ok
419
	mov	[eax + TCP_SOCKET.RemotePort], bx
Line -... Line 420...
-
 
420
	DEBUGF	1,"remote port: %u ",bx
-
 
421
 
Line 376... Line -...
376
 
-
 
377
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.LocalIP], 0
422
	mov	ebx, dword [edx + 4]
Line 378... Line -...
378
	jne	@f
-
 
379
	push	[IP_LIST]	    ; device zero = default
-
 
380
	pop	[eax + SOCKET_head.end + IPv4_SOCKET.LocalIP]
-
 
381
       @@:
-
 
382
 
423
	mov	[eax + IP_SOCKET.RemoteIP], ebx
383
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort], 0
424
 
Line 384... Line 425...
384
	jne	@f
425
	; check if local port and IP is ok
385
 
426
 
386
	mov	ecx, [eax + SOCKET_head.Type]
427
	cmp	[eax + IP_SOCKET.LocalIP], 0
387
	call	socket_find_port
428
	jne	@f
388
	test	bx, bx
-
 
389
	jz	s_error
429
	push	[IP_LIST]	    ;;;;; device zero = default
390
 
430
	pop	[eax + IP_SOCKET.LocalIP]
391
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort], bx
431
       @@:
392
       @@:
432
 
393
 
433
	cmp	[eax + TCP_SOCKET.LocalPort], 0
394
 
434
	jne	@f
395
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_SYN_SENT
435
	call	SOCKET_find_port
Line 396... Line 436...
396
	; now say hello to the remote tcp socket
436
       @@:
Line 397... Line 437...
397
 
437
 
398
	mov	bl, TH_SYN
-
 
399
	xor	ecx, ecx
438
;        mov     [eax + TCP_SOCKET.t_state], TCB_SYN_SENT
Line 400... Line 439...
400
	call	TCP_send
439
	call	TCP_output
401
 
440
 
Line 402... Line 441...
402
	mov	dword [esp+32],0
441
	mov	[eax + SOCKET.lock], 0
403
	ret
442
 
Line -... Line 443...
-
 
443
	mov	dword [esp+32], 0	; success!
-
 
444
	ret
404
 
445
 
405
 
446
 
406
;-----------------------------------------------------------------
447
;-----------------------------------------------------------------
407
;
448
;
Line 408... Line 449...
408
; SOCKET_listen
449
; SOCKET_listen
409
;
450
;
-
 
451
;  IN:  socket number in ecx
Line 410... Line 452...
410
;
452
;       backlog in edx
-
 
453
;  OUT: eax is socket num, -1 on error
411
;  IN:  socket number in ecx
454
;
Line 412... Line 455...
412
;       backlog in edx
455
;-----------------------------------------------------------------
413
;  OUT: eax is socket num, -1 on error
456
align 4
414
;
457
SOCKET_listen:
415
;-----------------------------------------------------------------
458
 
416
align 4
-
 
417
socket_listen:
459
	DEBUGF	1,"Socket_listen: socknum: %u backlog: %u\n", ecx, edx
418
 
460
 
419
	DEBUGF	1,"Socket_listen: socknum: %u backlog: %u\n",ecx,edx
461
	call	SOCKET_num_to_ptr
420
 
462
	jz	s_error
421
	stdcall net_socket_num_to_addr, ecx
463
 
422
	cmp	eax, -1
464
	cmp	word [eax + SOCKET.Domain], AF_INET4
423
	jz	s_error
465
	jne	s_error
424
 
466
 
Line 425... Line 467...
425
	cmp	word [eax + SOCKET_head.Domain], AF_INET4
467
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
Line 426... Line 468...
426
	jne	s_error
468
	jne	s_error
427
 
-
 
428
	cmp	[eax + SOCKET_head.Type], IP_PROTO_TCP
469
 
429
	jne	s_error
-
 
Line 430... Line 470...
430
 
470
	; TODO: check local port number
431
	cmp	edx, MAX_backlog
471
 
Line 432... Line 472...
432
	jb	.ok
472
	cmp	edx, MAX_backlog
Line 433... Line 473...
433
	mov	dx , MAX_backlog
473
	jle	.ok
Line 434... Line 474...
434
  .ok:
474
	mov	edx, MAX_backlog
435
 
475
  .ok:
Line 436... Line 476...
436
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog], dx
476
 
Line 437... Line 477...
437
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_LISTEN
477
	mov	[eax + TCP_SOCKET.backlog], dx
Line 438... Line 478...
438
 
478
	mov	[eax + TCP_SOCKET.t_state], TCB_LISTEN
439
	mov	dword [esp+32], 0
479
	 or	[eax + SOCKET.options], SO_ACCEPTCON
-
 
480
 
440
	ret
481
	mov	dword [esp+32], 0
441
 
482
 
442
 
483
	ret
-
 
484
 
443
;-----------------------------------------------------------------
485
 
444
;
486
;-----------------------------------------------------------------
445
; SOCKET_accept
487
;
446
;
488
; SOCKET_accept
-
 
489
;
447
;
490
;  IN:  socket number in ecx
-
 
491
;       addr in edx
448
;  IN:  socket number in ecx
492
;       addrlen in esi
-
 
493
;  OUT: eax is socket num, -1 on error
449
;       addr in edx
494
;
450
;       addrlen in esi
495
;-----------------------------------------------------------------
451
;  OUT: eax is socket num, -1 on error
496
align 4
Line 452... Line 497...
452
;
497
SOCKET_accept:
453
;-----------------------------------------------------------------
498
 
454
align 4
499
	DEBUGF	1,"Socket_accept: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
455
socket_accept:
500
 
456
 
-
 
457
	DEBUGF	1,"Socket_accept: socknum: %u sockaddr: %x, length: %u\n",ecx,edx,esi
501
	call	SOCKET_num_to_ptr
458
 
502
	jz	s_error
459
	stdcall net_socket_num_to_addr, ecx
503
 
460
	or	eax, eax
504
	cmp	word [eax + SOCKET.Domain], AF_INET4
461
	jz	s_error
505
	je	.af_inet4
462
	mov	esi, eax
506
 
Line 463... Line 507...
463
 
507
	jmp	s_error
Line 464... Line 508...
464
	cmp	word [esi + SOCKET_head.Domain], AF_INET4
508
 
465
	je	.af_inet4
-
 
466
 
509
  .af_inet4:
Line 467... Line 510...
467
	jmp	s_error
510
 
468
 
511
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
Line 469... Line 512...
469
  .af_inet4:
512
	je	.tcp
470
 
513
 
Line 471... Line 514...
471
	cmp	[esi + SOCKET_head.Type], IP_PROTO_TCP
514
	jmp	s_error
472
	je	.tcp
515
 
Line 473... Line 516...
473
 
516
  .tcp:
474
	jmp	s_error
517
 
Line 475... Line 518...
475
 
518
	lea	ebx, [eax + SOCKET.lock]
Line 476... Line -...
476
  .tcp:
-
 
477
 
-
 
478
	lea	ebx, [esi + SOCKET_head.lock]
-
 
479
	call	wait_mutex
-
 
480
	movzx	eax, [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog_cur]
-
 
481
	test	eax, eax
-
 
482
	jz	.unlock_err
-
 
483
	dec	[esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog_cur]
-
 
484
	mov	eax, [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.end + (eax-1)*4]
-
 
485
	mov	[esi + SOCKET_head.lock], 0
-
 
486
	stdcall net_socket_addr_to_num, eax
-
 
487
	mov	[esp+32], eax
-
 
488
	ret
-
 
489
  .unlock_err:
519
	call	wait_mutex
-
 
520
 
490
	mov	[esi + SOCKET_head.lock], 0
521
	movzx	ebx, [eax + TCP_SOCKET.backlog_cur]
Line 491... Line 522...
491
	jmp	s_error
522
	test	ebx, ebx
Line 492... Line 523...
492
 
523
	jz	.unlock_err
493
 
-
 
494
;-----------------------------------------------------------------
-
 
495
;
-
 
496
; SOCKET_close
-
 
497
;
-
 
498
;
-
 
499
;  IN:  socket number in ecx
-
 
500
;  OUT: eax is socket num, -1 on error
-
 
501
;
-
 
502
;-----------------------------------------------------------------
-
 
503
align 4
-
 
504
socket_close:
-
 
505
 
-
 
506
	DEBUGF	1,"Socket_close: socknum: %u\n",ecx
-
 
507
 
-
 
Line 508... Line 524...
508
	stdcall net_socket_num_to_addr, ecx
524
 
Line 509... Line -...
509
	or	eax, eax
-
 
510
	jz	s_error
-
 
511
 
525
	dec	[eax + TCP_SOCKET.backlog_cur]
512
	cmp	[eax + SOCKET_head.Domain], AF_INET4
526
	mov	eax, [eax + TCP_SOCKET.end + (ebx-1)*4]
513
	jne	s_error
-
 
514
 
527
	mov	[eax + SOCKET.lock], 0
515
	cmp	[eax + SOCKET_head.Type], IP_PROTO_UDP
-
 
516
	je	.udp
-
 
517
 
528
	mov	dword [esp+32], 0
518
	cmp	[eax + SOCKET_head.Type], IP_PROTO_ICMP
529
 
519
	je	.icmp
-
 
520
 
-
 
Line 521... Line 530...
521
	cmp	[eax + SOCKET_head.Type], IP_PROTO_TCP
530
	call	TCP_output	;;;;;
Line 522... Line -...
522
	je	.tcp
-
 
523
 
-
 
524
	jmp	s_error
531
 
525
 
532
	ret
526
  .udp:
533
 
527
 
534
  .unlock_err:
528
	stdcall net_socket_free, eax
-
 
529
	mov	dword [esp+32],0
535
	mov	[eax + SOCKET.lock], 0
530
	ret
536
	jmp	s_error
531
 
537
 
532
 
538
 
533
  .icmp:
539
;-----------------------------------------------------------------
534
 
540
;
535
 
541
; SOCKET_close
536
 
542
;
537
	ret
543
;  IN:  socket number in ecx
Line 538... Line 544...
538
 
544
;  OUT: eax is socket num, -1 on error
539
  .tcp:
-
 
-
 
545
;
540
	mov	dword [esp+32],0
546
;-----------------------------------------------------------------
541
 
547
align 4
Line 542... Line 548...
542
	; first, remove all resend entries for this socket
548
SOCKET_close:
543
 
-
 
-
 
549
 
-
 
550
	DEBUGF	1,"socket_close: socknum: %u\n", ecx
544
	call	TCP_remove_socket
551
 
545
 
-
 
-
 
552
	call	SOCKET_num_to_ptr
546
;        cmp     [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_LISTEN
553
	jz	s_error
Line 547... Line 554...
547
;        je      .destroy_tcb
554
 
548
;        cmp     [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_SYN_SENT
-
 
Line -... Line 555...
-
 
555
	cmp	[eax + SOCKET.Domain], AF_INET4
549
;        je      .destroy_tcb
556
	jne	s_error
Line 550... Line 557...
550
;        cmp     [eac + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_CLOSED
557
 
551
;        je      .destroy_tcb
558
	cmp	[eax + SOCKET.Type], IP_PROTO_UDP
552
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_SYN_RECEIVED
559
	je	.free
553
	je	.fin_wait
560
 
554
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_ESTABLISHED
-
 
Line -... Line 561...
-
 
561
	cmp	[eax + SOCKET.Type], IP_PROTO_ICMP
555
	je	.fin_wait
562
	je	.free
556
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_CLOSE_WAIT
563
 
557
	je	.last_ack
-
 
558
 
564
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
-
 
565
	je	.tcp
Line 559... Line 566...
559
	stdcall net_socket_free, ebx
566
 
560
 
-
 
561
	ret
567
	jmp	s_error
562
 
568
 
563
 
569
  .tcp:
564
  .last_ack:
570
	test	[eax + TCP_SOCKET.t_state], TCB_SYN_RECEIVED  ;;;;;;
565
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_LAST_ACK
571
	jz	.free
566
	jmp	.send_fin
572
 
567
 
573
	call	TCP_output
568
  .fin_wait:
574
 
569
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_FIN_WAIT_1
575
	mov	dword [esp+32], 0
570
 
576
 
-
 
577
	ret
-
 
578
 
-
 
579
;       state must be LISTEN, SYN_SENT, CLOSED or maybe even invalid
Line 571... Line 580...
571
  .send_fin:
580
;       so, we may destroy the socket
Line 572... Line 581...
572
	mov	bl, TH_FIN + TH_ACK
581
  .free:
Line 573... Line 582...
573
	xor	ecx, ecx
582
	call	SOCKET_free
Line 649... Line 658...
649
;       flags in edi
658
;       flags in edi
650
;  OUT: -1 on error
659
;  OUT: -1 on error
651
;
660
;
652
;-----------------------------------------------------------------
661
;-----------------------------------------------------------------
653
align 4
662
align 4
654
socket_send:
663
SOCKET_send:
Line 655... Line 664...
655
 
664
 
Line 656... Line -...
656
	DEBUGF	1,"Socket_send: socknum: %u sockaddr: %x, length: %u, flags: %x, ",ecx,edx,esi,edi
-
 
657
 
665
	DEBUGF	1,"socket_send: socknum: %u sockaddr: %x, length: %u, flags: %x\n", ecx, edx, esi, edi
658
	stdcall net_socket_num_to_addr, ecx		   ; get real socket address
666
 
Line 659... Line 667...
659
	or	eax, eax
667
	call	SOCKET_num_to_ptr
660
	jz	s_error
668
	jz	s_error
Line 661... Line 669...
661
 
669
 
Line 662... Line 670...
662
	cmp	word [eax + SOCKET_head.Domain], AF_INET4
670
	cmp	word [eax + SOCKET.Domain], AF_INET4
663
	je	.af_inet4
671
	je	.af_inet4
Line 664... Line 672...
664
 
672
 
665
	jmp	s_error
673
	jmp	s_error
Line 666... Line 674...
666
 
674
 
667
  .af_inet4:
675
  .af_inet4:
Line 668... Line -...
668
	DEBUGF	1,"Socket type:%u\n", [eax + SOCKET_head.Type]:4
-
 
669
 
-
 
670
	cmp	[eax + SOCKET_head.Type], IP_PROTO_TCP
-
 
671
	je	.tcp
676
	DEBUGF	1,"af_inet4\n"
Line 672... Line 677...
672
 
677
 
-
 
678
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
Line 673... Line 679...
673
	cmp	[eax + SOCKET_head.Type], IP_PROTO_UDP
679
	je	.tcp
674
	je	.udp
-
 
675
 
680
 
676
	cmp	[eax + SOCKET_head.Type], SOCK_RAW
681
	cmp	[eax + SOCKET.Type], IP_PROTO_UDP
Line 677... Line -...
677
	je	.raw
-
 
678
 
-
 
679
	jmp	s_error
682
	je	.udp
680
 
-
 
681
  .udp:
-
 
682
 
683
 
683
	DEBUGF	1,"type: UDP, "
-
 
Line -... Line 684...
-
 
684
	jmp	s_error
684
 
685
 
685
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort],0
-
 
686
	jne	@f
686
  .udp:
687
 
687
	DEBUGF	1,"type: UDP\n"
Line 688... Line 688...
688
	push	esi
688
 
Line 689... Line 689...
689
	mov	ecx, [eax + SOCKET_head.Type]
689
; check if local port is valid
690
	call	socket_find_port
690
	cmp	[eax + UDP_SOCKET.LocalPort], 0
Line 691... Line 691...
691
	test	bx, bx
691
	jne	@f
-
 
692
 
Line -... Line 693...
-
 
693
	call	SOCKET_find_port
692
	pop	esi
694
	jz	s_error
693
	je	s_error
695
 
Line 694... Line -...
694
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort], bx
-
 
695
 
-
 
696
     @@:
696
; Now, send the packet
697
 
-
 
698
	mov	ecx, esi
-
 
699
	mov	esi, edx
697
     @@:
700
 
-
 
Line 701... Line 698...
701
	call	UDP_socket_send
698
	mov	ecx, esi
-
 
699
	mov	esi, edx
Line 702... Line -...
702
 
-
 
703
	and	dword [esp+32], 0
-
 
704
	ret
-
 
705
 
-
 
706
  .tcp:
700
 
Line 707... Line 701...
707
 
701
	call	UDP_output
708
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort],0
702
 
Line 709... Line -...
709
	jne	@f
-
 
710
 
-
 
711
	push	esi
-
 
Line 712... Line -...
712
	mov	ecx, [eax + SOCKET_head.Type]
-
 
713
	call	socket_find_port
-
 
714
	test	bx, bx
-
 
715
	pop	esi
-
 
Line 716... Line -...
716
	je	s_error
-
 
717
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort], bx
-
 
718
 
-
 
719
     @@:
-
 
720
 
-
 
721
	mov	ecx, esi
-
 
722
	mov	esi, edx
-
 
723
	mov	bl, TH_PUSH + TH_ACK
-
 
724
 
-
 
725
	call	TCP_send
-
 
726
 
-
 
727
	mov	[esp+32], eax
-
 
728
	ret
-
 
729
 
-
 
730
  .raw:
-
 
731
	cmp	[eax + SOCKET_head.Protocol], IP_PROTO_IP
-
 
732
	je	.raw_ip
-
 
733
 
-
 
734
	cmp	[eax + SOCKET_head.Protocol], IP_PROTO_ICMP
-
 
735
	je	.raw_icmp
-
 
736
 
-
 
737
	jmp	s_error
-
 
738
 
-
 
739
 
-
 
740
  .raw_ip:
-
 
741
 
-
 
742
	;;;;;;
-
 
743
 
-
 
744
	mov	[esp+32], eax
-
 
745
	ret
-
 
746
 
-
 
747
 
703
	mov	dword [esp+32], 0
748
  .raw_icmp:
704
	ret
749
 
705
 
750
;        sub     ecx, ICMP_Packet.Data
706
  .tcp:
751
;        mov     esi, edx
-
 
752
;        push    ax
707
	DEBUGF	1,"type: TCP\n"
753
;        call    IPv4_get_frgmnt_num
708
 
754
;        mov     dx, ax
709
; check if local port is valid
755
;        pop     ax
710
	cmp	[eax + TCP_SOCKET.LocalPort], 0
756
;        shl     edx, 16
711
	jne	@f
757
;        mov     dh , [esi + ICMP_Packet.Type]
712
 
758
;        mov     dl , [esi + ICMP_Packet.Code]
713
	call	SOCKET_find_port
759
;        mov     di , [esi + ICMP_Packet.Identifier]
714
	jz	s_error
760
;        mov     [eax + SOCKET.LocalPort], di            ; Set localport to the identifier number, so we can receive reply's
715
 
761
;        shl     edi, 16
716
     @@:
762
;        mov     di , [esi + ICMP_Packet.SequenceNumber]
717
;;;; TODO: queue the data
763
;        add     esi, ICMP_Packet.Data
718
 
-
 
719
	call	TCP_output
-
 
720
 
-
 
721
	mov	[esp+32], eax
-
 
722
	ret
-
 
723
 
Line 764... Line 724...
764
;        mov     ebx, [eax + SOCKET.LocalIP]
724
 
765
;        mov     eax, [eax + SOCKET.RemoteIP]
725
 
766
;        call    ICMP_create_packet
726
 
767
 
727
;-----------------------------------------------------------------
768
	mov	[esp+32], eax
728
;
769
	ret
729
; SOCKET_get_options
770
 
730
;
771
;-----------------------------------------------------------------
731
;  IN:  ecx = socket number
772
;
732
;       edx = pointer to the options:
773
; SOCKET_get_options
733
;               dd      level, optname, optval, optlen
774
;
734
;  OUT: -1 on error
775
;
735
;
776
;  IN:  socket number in ecx
736
; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
777
;       edx points to the options:
737
; TODO: find best way to notify that send()'ed data were acknowledged
778
;               dd      level, optname, optval, optlen
738
; Also pseudo-optname -3 is valid and returns socket state, one of TCB_*.
779
;  OUT: -1 on error
739
;
780
;
740
;-----------------------------------------------------------------
781
; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
741
align 4
782
; TODO: find best way to notify that send()'ed data were acknowledged
742
SOCKET_get_opt:
783
; Also pseudo-optname -3 is valid and returns socket state, one of TCB_*.
743
 
784
;
744
	DEBUGF	1,"socket_get_opt\n"
785
;-----------------------------------------------------------------
745
 
786
align 4
746
	call	SOCKET_num_to_ptr
787
socket_get_opt:
747
	jz	s_error
788
 
748
 
789
	cmp	dword [edx], IP_PROTO_TCP
749
	cmp	dword [edx], IP_PROTO_TCP
790
	jnz	.unknown
750
	jnz	s_error
791
	cmp	dword [edx+4], -2
751
	cmp	dword [edx+4], -2
792
	jz	@f
752
	jz	@f
793
	cmp	dword [edx+4], -3
-
 
794
	jnz	.unknown
-
 
795
@@:
-
 
796
	mov	eax, [edx+12]
-
 
Line 797... Line 753...
797
	test	eax, eax
753
	cmp	dword [edx+4], -3
798
	jz	.fail
754
	jnz	s_error
799
	cmp	dword [eax], 4
755
@@:
800
	mov	dword [eax], 4
756
;        mov     eax, [edx+12]
801
	jb	.fail
757
;        test    eax, eax
-
 
758
;        jz      .fail
-
 
759
;        cmp     dword [eax], 4
802
	stdcall net_socket_num_to_addr, ecx
760
;        mov     dword [eax], 4
803
	test	eax, eax
761
;        jb      .fail
804
	jz	.fail
762
;        stdcall net_socket_num_to_addr, ecx
805
	; todo: check that eax is really TCP socket
763
;        test    eax, eax
806
	mov	ecx, [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.last_ack_number]
764
;        jz      .fail
807
	cmp	dword [edx+4], -2
765
;        ; todo: check that eax is really TCP socket
808
	jz	@f
766
;        mov     ecx, [eax + TCP_SOCKET.last_ack_number]
Line 809... Line 767...
809
	mov	ecx, [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state]
767
;        cmp     dword [edx+4], -2
Line -... Line 768...
-
 
768
;        jz      @f
-
 
769
;        mov     ecx, [eax + TCP_SOCKET.state]
810
@@:
770
@@:
811
	mov	eax, [edx+8]
771
	mov	eax, [edx+8]
Line 812... Line 772...
812
	test	eax, eax
772
	test	eax, eax
813
	jz	@f
773
	jz	@f
Line -... Line 774...
-
 
774
	mov	[eax], ecx
-
 
775
@@:
-
 
776
	mov	dword [esp+32], 0
-
 
777
	ret
-
 
778
 
-
 
779
 
-
 
780
;-----------------------------------------------------------------
-
 
781
;
814
	mov	[eax], ecx
782
; SOCKET_find_port
815
@@:
783
;
-
 
784
; Fills in the local port number for TCP and UDP sockets
-
 
785
; This procedure always works because the number of sockets is
816
	mov	dword [esp+32], 0
786
; limited to a smaller number then the number of possible ports
Line 817... Line 787...
817
	ret
787
;
818
.fail:
788
;  IN:  eax = socket pointer
-
 
789
;  OUT: /
-
 
790
;
-
 
791
;-----------------------------------------------------------------
Line -... Line 792...
-
 
792
align 4
-
 
793
SOCKET_find_port:
819
.unknown:
794
 
820
	mov	dword [esp+32], -1
795
	DEBUGF	1,"socket_find_free_port\n"
Line 821... Line -...
821
	ret
-
 
822
 
-
 
823
 
-
 
824
;-----------------------------------------------------------------
-
 
825
;
-
 
826
; SOCKET_find_free_port (local port)
-
 
827
;
-
 
828
; works with INET byte order
-
 
829
;
-
 
830
;  IN:  type in ecx (TCP/UDP)
-
 
831
;  OUT: bx = 0 on error, portnumber otherwise
-
 
832
;
-
 
833
;-----------------------------------------------------------------
-
 
834
align 4
-
 
835
socket_find_port:
-
 
836
 
-
 
837
	DEBUGF	1,"Socket_find_free_port\n"
796
 
838
 
797
	push	ebx esi ecx
Line 839... Line 798...
839
	cmp	ecx, IP_PROTO_UDP
798
 
840
	je	.udp
-
 
841
 
799
	cmp	[eax + SOCKET.Type], IP_PROTO_UDP
842
	cmp	ecx, IP_PROTO_TCP
-
 
Line 843... Line -...
843
	je	.tcp
-
 
844
 
-
 
845
  .udp:
800
	je	.udp
Line 846... Line 801...
846
	mov	bx, [last_UDP_port]
801
 
847
	je	.continue
802
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
848
 
803
	je	.tcp
849
  .tcp:
804
 
850
	mov	bx, [last_TCP_port]
805
	jmp	.error
-
 
806
 
851
 
807
  .done:
852
 
808
	mov	[eax + UDP_SOCKET.LocalPort], bx
853
  .continue:
809
  .error:
-
 
810
	pop	ecx esi ebx
854
	inc	bx
811
	ret
855
 
812
 
856
  .check_only:
813
  .udp:
857
	mov	esi, net_sockets
814
	mov	bx, [last_UDP_port]
858
 
815
	call	.findit
-
 
816
	mov	[last_UDP_port], bx
-
 
817
	jmp	.done
-
 
818
 
-
 
819
  .tcp:
859
  .next_socket:
820
	mov	bx, [last_TCP_port]
Line 860... Line 821...
860
	mov	esi, [esi + SOCKET_head.NextPtr]
821
	call	.findit
861
	or	esi, esi
822
	mov	[last_TCP_port], bx
862
	jz	.port_ok
823
	jmp	.done
863
 
824
 
Line 864... Line 825...
864
	cmp	[esi + SOCKET_head.Type], ecx
825
 
865
	jne	.next_socket
826
  .restart:
Line 866... Line 827...
866
 
827
	mov	bx, MIN_EPHEMERAL_PORT
867
	rol	bx, 8
828
  .findit:
Line -... Line 829...
-
 
829
	inc	bx
868
	cmp	[esi + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort], bx
830
 
Line 869... Line 831...
869
	rol	bx, 8				; this doesnt change the zero flag, does it ?
831
	cmp	bx, MAX_EPHEMERAL_PORT
-
 
832
	jz	.restart
-
 
833
 
-
 
834
	call	SOCKET_check_port
870
	jne	.next_socket
835
	jz	.findit
Line 871... Line 836...
871
 
836
 
872
	cmp	bx, MAX_EPHEMERAL_PORT
837
	ret
873
	jle	.continue
838
 
874
 
839
 
875
	; todo: WRAP!
840
 
876
;        mov     [last_UDP_port], MIN_EPHEMERAL_PORT
841
;-----------------------------------------------------------------
877
  .exit:
842
;
878
	xor	ebx, ebx
843
; SOCKET_check_port
879
 
844
;
-
 
845
; Checks if a local port number is unused
880
  .port_ok:
846
; If the proposed port number is unused, it is filled in in the socket structure
-
 
847
;
881
	rol	bx, 8
848
;  IN:  eax = socket ptr (to find out if its a TCP/UDP socket)
882
	ret
849
;        bx = proposed socket number
883
 
850
;
884
 
851
;  OUT:  ZF = cleared on error
885
 
852
;
886
;-----------------------------------------------------------------
853
;-----------------------------------------------------------------
887
;
854
align 4
888
; SOCKET_check_port (local port)
855
SOCKET_check_port:
Line 889... Line 856...
889
;
856
 
Line 890... Line 857...
890
; works with INET byte order
857
	DEBUGF	1,"socket_check_port\n"
891
;
858
 
892
;  IN:  type in ecx (TCP/UDP)
-
 
893
;       port to check in bx
859
	mov	ecx, [eax + SOCKET.Type]
-
 
860
	mov	esi, net_sockets
894
;  OUT: bx = 0 on error, unchanged otherwise
861
 
-
 
862
  .next_socket:
-
 
863
	mov	esi, [esi + SOCKET.NextPtr]
-
 
864
	or	esi, esi
-
 
865
	jz	.port_ok
895
;
866
 
896
;-----------------------------------------------------------------
867
	cmp	[esi + SOCKET.Type], ecx
-
 
868
	jne	.next_socket
-
 
869
 
-
 
870
	cmp	[esi + UDP_SOCKET.LocalPort], bx
-
 
871
	jne	.next_socket
-
 
872
 
-
 
873
	DEBUGF	1,"local port %u already in use\n", bx
-
 
874
	ret
-
 
875
 
-
 
876
  .port_ok:
-
 
877
	mov	[eax + UDP_SOCKET.LocalPort], bx
-
 
878
	or	bx, bx					; set the zero-flag
-
 
879
 
-
 
880
	ret
-
 
881
 
-
 
882
 
-
 
883
 
-
 
884
;-----------------------------------------------------------------
-
 
885
;
-
 
886
; SOCKET_input
-
 
887
;
-
 
888
; Updates a socket with received data
-
 
889
;
-
 
890
; Note: the mutex should already be set !
-
 
891
;
-
 
892
;  IN:  eax = socket ptr
-
 
893
;       ebx = pointer to device struct
-
 
894
;       ecx = data size
-
 
895
;       esi = ptr to data
Line 897... Line 896...
897
align 4
896
;       [esp] = ptr to buf
Line 898... Line -...
898
socket_check_port:
-
 
899
	mov	esi, net_sockets
897
;       [esp + 4] = buf size
-
 
898
;
900
 
899
;  OUT: /
901
  .next_socket:
900
;
902
	mov	esi, [esi + SOCKET_head.NextPtr]
901
;-----------------------------------------------------------------
Line 903... Line 902...
903
	or	esi, esi
902
align 4
904
	jz	.port_ok
903
SOCKET_input:
905
 
904
 
906
	cmp	[esi + SOCKET_head.Type], ecx
905
	DEBUGF	1,"socket_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
907
	jne	.next_socket
906
 
908
 
907
	mov	dword[esp+4], ecx
909
	cmp	[esi + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort], bx
908
	push	esi
-
 
909
	mov	esi, esp
-
 
910
 
-
 
911
	add_to_queue (eax + SOCKET_QUEUE_LOCATION),\
910
	jne	.next_socket
912
		     SOCKET_QUEUE_SIZE,\
Line 911... Line 913...
911
 
913
		     socket_queue_entry.size,\
912
	xor	ebx, ebx
914
		     SOCKET_input.full
913
 
915
 
914
  .port_ok:
916
	DEBUGF	1,"Queued packet successfully\n"
915
	ret
-
 
Line 916... Line -...
916
 
-
 
917
 
917
	add	esp, socket_queue_entry.size
918
 
-
 
919
;-----------------------------------------------------------------
-
 
920
;
-
 
921
; SOCKET_internal_receiver
-
 
Line -... Line 918...
-
 
918
	mov	[eax + SOCKET.lock], 0
-
 
919
	jmp	SOCKET_notify_owner
-
 
920
 
-
 
921
  .full:
-
 
922
	DEBUGF	2,"Socket %x is full!\n", eax
Line -... Line 923...
-
 
923
	mov	[eax + SOCKET.lock], 0
-
 
924
	call	kernel_free
-
 
925
	add	esp, 8
-
 
926
 
922
;
927
	ret
923
; Updates a socket with received data
928
 
924
;
929
;-----------------------------------------------------------------
925
; Note: the mutex must already be set !
930
;
-
 
931
; SOCKET_notify_owner
926
;
932
;
-
 
933
; notify's the owner of a socket that something happened
-
 
934
;
927
;  IN:  eax = socket ptr
935
;  IN:  eax = socket ptr
928
;       ecx = size
936
;  OUT: /
-
 
937
;
-
 
938
;-----------------------------------------------------------------
-
 
939
align 4
-
 
940
SOCKET_notify_owner:
-
 
941
 
929
;       esi = pointer to buffer
942
	DEBUGF	1,"socket_notify_owner\n"
930
;       edi = offset
943
 
931
;
-
 
932
;  OUT: xxx
944
	call	SOCKET_check
933
;
945
	jz	.error
Line 934... Line 946...
934
;-----------------------------------------------------------------
946
 
935
align 4
947
	push	ecx eax esi
936
socket_internal_receiver:
948
 
937
 
-
 
938
	DEBUGF	1,"Internal socket receiver: buffer %x, offset: %x size=%u socket: %x\n", esi, edi, ecx, eax
949
; socket exists, now try to flag an event to the application
939
 
-
 
940
	push	edi	; offset
950
 
941
	push	ecx	; size
951
	mov	eax, [eax + SOCKET.PID]
942
	push	esi	; data_ptr
952
	mov	ecx, 1
Line 943... Line 953...
943
	mov	esi, esp
953
	mov	esi, TASK_DATA + TASKDATA.pid
Line 944... Line -...
944
	add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, socket_queue_entry.size, notify_network_event.full
-
 
945
	DEBUGF	1,"Queued packet successfully\n"
-
 
946
	add	esp, socket_queue_entry.size
-
 
947
 
-
 
948
	mov	[eax + SOCKET_head.lock], 0
-
 
949
 
-
 
950
notify_network_event:
-
 
951
	; flag an event to the application
-
 
952
	mov	edx, [eax + SOCKET_head.PID]				; get socket owner PID
-
 
953
	mov	ecx, 1
-
 
954
	mov	esi, TASK_DATA + TASKDATA.pid
-
 
955
 
-
 
956
       .next_pid:
-
 
957
	cmp	[esi], edx
-
 
958
	je	.found_pid
-
 
959
	inc	ecx
-
 
960
	add	esi, 0x20
954
 
961
	cmp	ecx, [TASK_COUNT]
-
 
-
 
955
       .next_pid:
962
	jbe	.next_pid
956
	cmp	[esi], eax
963
	ret
957
	je	.found_pid
964
 
958
	inc	ecx
965
       .found_pid:
959
	add	esi, 0x20
966
	shl	ecx, 8
960
	cmp	ecx, [TASK_COUNT]
967
	or	[ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK	; stack event
961
	jbe	.next_pid
968
	mov	[check_idle_semaphore], 200
962
 
969
	ret
963
; PID not found, TODO: close socket!
970
 
964
 
971
  .full:
965
	jmp	.error2
972
	DEBUGF	2,"Socket %x is full!\n",eax
-
 
973
	mov	[eax + SOCKET_head.lock], 0
-
 
974
	call	kernel_free
966
 
975
	add	esp, 8
967
       .found_pid:
Line 976... Line 968...
976
	ret
968
	shl	ecx, 8
977
 
969
	or	[ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK
-
 
970
	mov	[check_idle_semaphore], 200
-
 
971
 
-
 
972
	DEBUGF	1,"owner notified\n"
-
 
973
 
-
 
974
  .error2:
-
 
975
	pop	esi eax ecx
-
 
976
  .error:
-
 
977
 
-
 
978
	ret
-
 
979
 
-
 
980
 
-
 
981
;--------------------------------------------------------------------
Line -... Line 982...
-
 
982
;
-
 
983
; SOCKET_alloc
-
 
984
;
-
 
985
; Allocate memory for socket data and put new socket into the list
-
 
986
; Newly created socket is initialized with calling PID and number and
-
 
987
; put into beginning of list (which is a fastest way).
-
 
988
;
-
 
989
; IN:  /
-
 
990
; OUT: eax = 0 on error, socket ptr otherwise
-
 
991
;      edi = socket number
-
 
992
;       ZF = cleared on error
-
 
993
;
-
 
994
;--------------------------------------------------------------------
-
 
995
align 4
-
 
996
SOCKET_alloc:
-
 
997
 
978
 
998
	push	ecx ebx
-
 
999
 
-
 
1000
	stdcall kernel_alloc, SOCKETBUFFSIZE
979
 
1001
	DEBUGF	1, "socket_alloc: %x ", eax
980
; Allocate memory for socket data and put new socket into the list
-
 
Line -... Line 1002...
-
 
1002
	or	eax, eax
981
; Newly created socket is initialized with calling PID and number and
1003
	jz	.exit
982
; put into beginning of list (which is a fastest way).
1004
 
983
;
1005
; zero-initialize allocated memory
984
; @return socket structure address in EAX
1006
	push	eax edi
985
;
1007
	mov	edi, eax
-
 
1008
	mov	ecx, SOCKETBUFFSIZE / 4
986
proc net_socket_alloc stdcall uses ebx ecx edx edi
1009
	xor	eax, eax
987
	stdcall kernel_alloc, SOCKETBUFFSIZE
1010
	rep	stosd
-
 
1011
	pop	edi eax
988
	DEBUGF	1, "K : net_socket_alloc (0x%x)\n", eax
1012
 
989
	; check if we can allocate needed amount of memory
1013
	init_queue (eax + SOCKET_QUEUE_LOCATION)
990
	or	eax, eax
1014
 
Line 991... Line -...
991
	jz	.exit
-
 
992
 
-
 
993
	; zero-initialize allocated memory
-
 
994
	push	eax
1015
; find first free socket number and use it
995
	mov	edi, eax
1016
 
996
 
-
 
997
	mov	ecx, SOCKETBUFFSIZE / 4
1017
	mov	ebx, net_sockets
998
;        cld
1018
	xor	ecx, ecx
999
	xor	eax, eax
-
 
1000
	rep	stosd
-
 
1001
	pop	eax
-
 
1002
 
-
 
Line -... Line 1019...
-
 
1019
  .next_socket_number:
-
 
1020
	inc	ecx
-
 
1021
  .next_socket:
-
 
1022
	mov	ebx, [ebx + SOCKET.NextPtr]
1003
	init_queue (eax + SOCKET_QUEUE_LOCATION)
1023
	or	ebx, ebx
-
 
1024
	jz	.last_socket
1004
 
1025
	cmp	[ebx + SOCKET.Number], ecx
1005
	; add socket to the list by changing pointers
1026
	jne	.next_socket
1006
	mov	ebx, net_sockets
1027
	mov	ebx, net_sockets
-
 
1028
	jmp	.next_socket_number
1007
	push	[ebx + SOCKET_head.NextPtr]
1029
 
-
 
1030
  .last_socket:
1008
	mov	[ebx + SOCKET_head.NextPtr], eax
1031
	mov	[eax + SOCKET.Number], ecx
1009
	mov	[eax + SOCKET_head.PrevPtr], ebx
1032
 
1010
	pop	ebx
1033
	DEBUGF	1, "(number: %u)\n", ecx
-
 
1034
 
Line -... Line 1035...
-
 
1035
; Fill in PID
-
 
1036
	mov	ebx, [TASK_BASE]
1011
	mov	[eax + SOCKET_head.NextPtr], ebx
1037
	mov	ebx, [ebx + TASKDATA.pid]
1012
	or	ebx, ebx
1038
 
Line 1013... Line -...
1013
	jz	@f
-
 
1014
	mov	[ebx + SOCKET_head.PrevPtr], eax
1039
	mov	[eax + SOCKET.PID], ebx
1015
 
1040
 
-
 
1041
; add socket to the list by changing pointers
-
 
1042
 
Line 1016... Line 1043...
1016
    @@: ; set socket owner PID to the one of calling process
1043
	mov	ebx, [net_sockets + SOCKET.NextPtr]
1017
	mov	ebx, [TASK_BASE]
-
 
1018
	mov	ebx, [ebx + TASKDATA.pid]
1044
 
1019
	mov	[eax + SOCKET_head.PID], ebx
-
 
Line -... Line 1045...
-
 
1045
	mov	[eax + SOCKET.PrevPtr], net_sockets
-
 
1046
	mov	[eax + SOCKET.NextPtr], ebx
-
 
1047
 
-
 
1048
	or	ebx, ebx
-
 
1049
	jz	@f
1020
 
1050
	add	ebx, SOCKET.lock	; lock the next socket
1021
	; find first free socket number and use it
-
 
1022
	;mov     edx, ebx
-
 
1023
	mov	ebx, net_sockets
-
 
1024
	xor	ecx, ecx
1051
	call	wait_mutex
1025
  .next_socket_number:
1052
	sub	ebx, SOCKET.lock
1026
	inc	ecx
1053
	mov	[ebx + SOCKET.PrevPtr], eax
-
 
1054
	mov	[ebx + SOCKET.lock], 0
1027
  .next_socket:
1055
       @@:
1028
	mov	ebx, [ebx + SOCKET_head.NextPtr]
1056
 
-
 
1057
	mov	[net_sockets + SOCKET.NextPtr], eax
1029
	or	ebx, ebx
1058
 
-
 
1059
	mov	edi, ecx
1030
	jz	.last_socket_number
1060
	or	eax, eax	; used to clear zero flag
-
 
1061
  .exit:
1031
	cmp	[ebx + SOCKET_head.Number], ecx
1062
	pop	ebx ecx
1032
	jne	.next_socket
-
 
Line 1033... Line -...
1033
	;cmp     [ebx + SOCKET.PID], edx
-
 
1034
	;jne     .next_socket
-
 
1035
	mov	ebx, net_sockets
-
 
1036
	jmp	.next_socket_number
-
 
1037
 
1063
 
1038
  .last_socket_number:
1064
	ret
1039
	mov	[eax + SOCKET_head.Number], ecx
1065
 
1040
 
1066
 
1041
  .exit:
1067
;----------------------------------------------------
1042
	ret
1068
;
1043
endp
-
 
1044
 
-
 
Line 1045... Line -...
1045
; Free socket data memory and pop socket off the list
-
 
1046
;
1069
; SOCKET_free
1047
; @param sockAddr is a socket structure address
-
 
Line -... Line 1070...
-
 
1070
;
1048
;
1071
; Free socket data memory and remove socket from the list
1049
proc net_socket_free stdcall uses ebx ecx edx, sockAddr:DWORD
-
 
1050
	mov	eax, [sockAddr]
1072
;
1051
	DEBUGF	1, "K : net_socket_free (0x%x)\n", eax
-
 
Line -... Line 1073...
-
 
1073
; IN:  eax = socket ptr
1052
	; check if we got something similar to socket structure address
1074
; OUT: /
1053
	or	eax, eax
-
 
1054
	jz	.error
-
 
-
 
1075
;
1055
 
1076
;----------------------------------------------------
1056
	; make sure sockAddr is one of the socket addresses in the list
1077
align 4
1057
	mov	ebx, net_sockets
1078
SOCKET_free:
-
 
1079
 
-
 
1080
	DEBUGF	1, "socket_free: %x\n", eax
1058
	;mov     ecx, [TASK_BASE]
1081
 
-
 
1082
	call	SOCKET_check
1059
	;mov     ecx, [ecx + TASKDATA.pid]
1083
	jz	.error
1060
  .next_socket:
1084
 
-
 
1085
	push	ebx
1061
	mov	ebx, [ebx + SOCKET_head.NextPtr]
1086
	lea	ebx, [eax + SOCKET.lock]
-
 
1087
	call	wait_mutex
1062
	or	ebx, ebx
1088
 
-
 
1089
	DEBUGF	1, "freeing socket..\n"
1063
	jz	.error
1090
 
1064
	cmp	ebx, eax
1091
	push	eax				; this will be passed to kernel_free
Line -... Line 1092...
-
 
1092
	mov	ebx, [eax + SOCKET.NextPtr]
-
 
1093
	mov	eax, [eax + SOCKET.PrevPtr]
-
 
1094
 
-
 
1095
	DEBUGF	1, "linking socket %x to socket %x\n", eax, ebx
-
 
1096
 
-
 
1097
	test	eax, eax
-
 
1098
	jz	@f
-
 
1099
	mov	[eax + SOCKET.NextPtr], ebx
-
 
1100
       @@:
-
 
1101
 
-
 
1102
	test	ebx, ebx
-
 
1103
	jz	@f
-
 
1104
	mov	[ebx + SOCKET.PrevPtr], eax
-
 
1105
       @@:
1065
	jne	.next_socket
1106
 
-
 
1107
	call	kernel_free
-
 
1108
	pop	ebx
-
 
1109
 
-
 
1110
	DEBUGF	1, "socket is gone!\n"
-
 
1111
 
-
 
1112
  .error:
-
 
1113
	ret
-
 
1114
 
-
 
1115
 
-
 
1116
;---------------------------------------------------
1066
	;cmp     [ebx + SOCKET.PID], ecx
1117
;
1067
	;jne     .next_socket
-
 
1068
 
-
 
-
 
1118
; SOCKET_num_to_ptr
1069
	; okay, we found the correct one
1119
;
1070
	; remove it from the list first, changing pointers
1120
; Get socket structure address by its number
1071
	mov	ebx, [eax + SOCKET_head.NextPtr]
1121
;
1072
	mov	eax, [eax + SOCKET_head.PrevPtr]
1122
; IN:  ecx = socket number
1073
	mov	[eax + SOCKET_head.NextPtr], ebx
1123
; OUT: ecx = 0 on error, socket ptr otherwise
1074
	or	ebx, ebx
1124
;       ZF = set on error
-
 
1125
;
-
 
1126
;---------------------------------------------------
1075
	jz	@f
1127
align 4
1076
	mov	[ebx + SOCKET_head.PrevPtr], eax
1128
SOCKET_num_to_ptr:
-
 
1129
 
Line 1077... Line -...
1077
 
-
 
1078
	lea	ebx, [eax + SOCKET_head.lock]
-
 
1079
	call	wait_mutex
1130
	DEBUGF	1,"socket_num_to_ptr: %u ", ecx
Line -... Line 1131...
-
 
1131
 
-
 
1132
	mov	eax, net_sockets
-
 
1133
 
-
 
1134
  .next_socket:
-
 
1135
	mov	eax, [eax + SOCKET.NextPtr]
-
 
1136
	or	eax, eax
-
 
1137
	jz	.error
-
 
1138
	cmp	[eax + SOCKET.Number], ecx
-
 
1139
	jne	.next_socket
-
 
1140
 
-
 
1141
	test	eax, eax
-
 
1142
 
-
 
1143
	DEBUGF	1,"(%x)\n", eax
-
 
1144
  .error:
-
 
1145
	ret
-
 
1146
 
-
 
1147
 
-
 
1148
;---------------------------------------------------
-
 
1149
;
-
 
1150
; SOCKET_ptr_to_num
-
 
1151
;
-
 
1152
; Get socket number by its address
-
 
1153
;
-
 
1154
; IN:  eax = socket ptr
-
 
1155
; OUT: eax = 0 on error, socket num otherwise
-
 
1156
;       ZF = set on error
-
 
1157
;
-
 
1158
;---------------------------------------------------
-
 
1159
align 4
-
 
1160
SOCKET_ptr_to_num:
-
 
1161
 
-
 
1162
	DEBUGF	1,"socket_ptr_to_num: %x ", eax
-
 
1163
 
-
 
1164
	call	SOCKET_check
-
 
1165
	jz	.error
-
 
1166
 
-
 
1167
	mov	eax, [eax + SOCKET.Number]
-
 
1168
 
-
 
1169
	DEBUGF	1,"(%u)\n", eax
-
 
1170
 
-
 
1171
  .error:
-
 
1172
	ret
-
 
1173
 
-
 
1174
 
-
 
1175
;---------------------------------------------------
-
 
1176
;
-
 
1177
; SOCKET_check
-
 
1178
;
-
 
1179
; checks if the given value is really a socket ptr
-
 
1180
;
-
 
1181
; IN:  eax = socket ptr
-
 
1182
; OUT: eax = 0 on error, unchanged otherwise
-
 
1183
;       ZF = set on error
-
 
1184
;
-
 
1185
;---------------------------------------------------
-
 
1186
align 4
-
 
1187
SOCKET_check:
-
 
1188
 
-
 
1189
	DEBUGF	1,"socket_check\n"
-
 
1190
 
-
 
1191
	push	ebx
-
 
1192
	mov	ebx, net_sockets
-
 
1193
 
-
 
1194
  .next_socket:
-
 
1195
	mov	ebx, [ebx + SOCKET.NextPtr]
-
 
1196
	or	ebx, ebx
-
 
1197
	jz	.done
-
 
1198
	cmp	ebx, eax
-
 
1199
	jnz	.next_socket
-
 
1200
 
1080
 
1201
  .done:
1081
    @@: ; and finally free the memory structure used
1202
	mov	eax, ebx
-
 
1203
	test	eax, eax
-
 
1204
	pop	ebx
-
 
1205
 
-
 
1206
	ret
-
 
1207
 
-
 
1208
 
-
 
1209
 
-
 
1210
;---------------------------------------------------
-
 
1211
;
-
 
1212
; SOCKET_check_owner
-
 
1213
;
1082
	stdcall kernel_free, [sockAddr]
1214
; checks if the caller application owns the socket
1083
	ret
-