Subversion Repositories Kolibri OS

Rev

Rev 1533 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1159 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
1514 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2010. All rights reserved.    ;;
1159 hidnplayr 4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  SOCKET.INC                                                     ;;
7
;;                                                                 ;;
1529 hidnplayr 8
;;    Written by hidnplayr@kolibrios.org,                          ;;
9
;;     and Clevermouse.                                            ;;
1159 hidnplayr 10
;;                                                                 ;;
1529 hidnplayr 11
;;       Based on code by mike.dld                                 ;;
1159 hidnplayr 12
;;                                                                 ;;
1529 hidnplayr 13
;;         GNU GENERAL PUBLIC LICENSE                              ;;
14
;;          Version 2, June 1991                                   ;;
15
;;                                                                 ;;
1159 hidnplayr 16
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17
 
1206 hidnplayr 18
$Revision: 1534 $
1159 hidnplayr 19
 
1514 hidnplayr 20
virtual at 0
1249 hidnplayr 21
 
1514 hidnplayr 22
	SOCKET:
23
	.NextPtr		dd ? ; pointer to next socket in list
24
	.PrevPtr		dd ? ; pointer to previous socket in list
25
	.Number 		dd ? ; socket number
1249 hidnplayr 26
 
1514 hidnplayr 27
	.lock			dd ? ; lock mutex
1249 hidnplayr 28
 
1514 hidnplayr 29
	.PID			dd ? ; application process id
30
	.Domain 		dd ? ; INET/UNIX/..
31
	.Type			dd ? ; RAW/UDP/TCP/...
32
	.Protocol		dd ? ; ICMP/IPv4/ARP/
33
	.errorcode		dd ?
1249 hidnplayr 34
 
1514 hidnplayr 35
	.options		dd ?
1529 hidnplayr 36
	.state			dd ?
1249 hidnplayr 37
 
1514 hidnplayr 38
	.end:
39
end virtual
1249 hidnplayr 40
 
1514 hidnplayr 41
virtual at SOCKET.end
1254 hidnplayr 42
 
1514 hidnplayr 43
	IP_SOCKET:
1318 hidnplayr 44
 
1514 hidnplayr 45
	.LocalIP		dd ?
46
				rd 3 ; for IPv6 addresses
1249 hidnplayr 47
 
1514 hidnplayr 48
	.RemoteIP		dd ?
49
				rd 3 ; for IPv6 addresses
1249 hidnplayr 50
 
1514 hidnplayr 51
	.end:
52
end virtual
1159 hidnplayr 53
 
1514 hidnplayr 54
virtual at IP_SOCKET.end
1249 hidnplayr 55
 
1514 hidnplayr 56
	TCP_SOCKET:
57
 
58
	.LocalPort		dw ? ; In INET byte order
59
	.RemotePort		dw ? ; In INET byte order
60
 
61
	.backlog		dw ? ; Backlog
62
	.backlog_cur		dw ? ; current size of queue for un-accept-ed connections
63
 
64
	.OrigRemoteIP		dd ? ; original remote IP address (used to reset to LISTEN state)
65
	.OrigRemotePort 	dw ? ; original remote port (used to reset to LISTEN state)
66
 
67
	.t_state		dd ? ; TCB state
68
	.t_rxtshift		dd ?
69
	.t_rxtcur		dd ?
70
	.t_dupacks		dd ?
71
	.t_maxseg		dd ?
72
	.t_force		dd ?
73
	.t_flags		dd ?
74
 
75
;---------------
76
; RFC783 page 21
77
 
78
; send sequence
79
	.SND_UNA		dd ? ; sequence number of unack'ed sent Packets
80
	.SND_NXT		dd ? ; next send sequence number to use
81
	.SND_UP 		dd ?
82
	.SND_WL1		dd ? ; window minus one
83
	.SND_WL2		dd ? ;
84
	.ISS			dd ? ; initial send sequence number
85
	.SND_WND		dd ? ; send window
86
 
87
; receive sequence
88
	.RCV_WND		dw ? ; receive window
89
	.RCV_NXT		dd ? ; next receive sequence number to use
90
	.RCV_UP 		dd ?
91
	.IRS			dd ? ; initial receive sequence number
92
 
93
;---------------------
94
; Additional variables
95
 
96
; receive variables
97
	.RCV_ADV		dd ?
98
 
99
; retransmit variables
100
	.SND_MAX		dd ?
101
 
102
; congestion control
103
	.SND_CWND		dd ?
104
	.SND_SSTHRESH		dd ?
105
 
106
;----------------------
107
; Transmit timing stuff
108
	.t_idle 		dd ?
109
	.t_rtt			dd ?
110
	.t_rtseq		dd ?
111
	.t_srtt 		dd ?
112
	.t_rttvar		dd ?
113
	.t_rttmin		dd ?
114
	.max_sndwnd		dd ?
115
 
116
;-----------------
117
; Out-of-band data
118
	.t_oobflags		dd ?
119
	.t_iobc 		dd ?
120
	.t_softerror		dd ?
121
 
122
 
123
;---------
124
; RFC 1323
125
	.SND_SCALE		db ? ; Scale factor
126
	.RCV_SCALE		db ?
127
	.request_r_scale	db ?
128
	.requested_s_scale	dd ?
129
 
130
	.ts_recent		dd ?
131
	.ts_recent_age		dd ?
132
	.last_ack_sent		dd ?
133
 
1519 hidnplayr 134
 
135
;-------
136
; Timers
1529 hidnplayr 137
	.timer_retransmission	dw ? ; rexmt
1519 hidnplayr 138
	.timer_ack		dw ?
139
	.timer_persist		dw ?
1529 hidnplayr 140
	.timer_keepalive	dw ? ; keepalive/syn timeout
141
	.timer_timed_wait	dw ? ; also used as 2msl timer
1519 hidnplayr 142
 
1249 hidnplayr 143
	.end:
1514 hidnplayr 144
end virtual
1249 hidnplayr 145
 
1514 hidnplayr 146
virtual at IP_SOCKET.end
1249 hidnplayr 147
 
1514 hidnplayr 148
	UDP_SOCKET:
1249 hidnplayr 149
 
1514 hidnplayr 150
	.LocalPort		dw ? ; In INET byte order
151
	.RemotePort		dw ? ; In INET byte order
152
	.firstpacket		db ?
1249 hidnplayr 153
 
154
	.end:
1514 hidnplayr 155
end virtual
1249 hidnplayr 156
 
1514 hidnplayr 157
virtual at IP_SOCKET.end
1249 hidnplayr 158
 
1514 hidnplayr 159
	ICMP_SOCKET:
160
 
161
	.Identifier		dw ? ;
162
 
163
	.end:
164
end virtual
165
 
1529 hidnplayr 166
struc	RING_BUFFER {
167
	.start_ptr		dd ? ; Pointer to start of buffer
168
	.end_ptr		dd ? ; pointer to end of buffer
169
	.read_ptr		dd ? ; Read pointer
170
	.write_ptr		dd ? ; Write pointer
171
	.size			dd ? ; Number of bytes buffered
1533 hidnplayr 172
	.end:
1529 hidnplayr 173
}
1514 hidnplayr 174
 
1529 hidnplayr 175
virtual at 0
176
 
177
	RING_BUFFER	RING_BUFFER
178
 
179
end virtual
180
 
181
virtual at TCP_SOCKET.end
182
 
183
 STREAM_SOCKET:
1533 hidnplayr 184
	.rcv	rd     RING_BUFFER.end/4
185
	.snd	rd     RING_BUFFER.end/4
1529 hidnplayr 186
	.end:
187
 
188
end virtual
189
 
190
 
1274 hidnplayr 191
struct	socket_queue_entry
192
	.data_ptr	dd ?
1514 hidnplayr 193
	.buf_ptr	dd ?
1274 hidnplayr 194
	.data_size	dd ?
195
	.size:
196
ends
197
 
1514 hidnplayr 198
 
1249 hidnplayr 199
SOCKETBUFFSIZE		equ 4096     ; in bytes
1514 hidnplayr 200
 
1249 hidnplayr 201
SOCKET_QUEUE_SIZE	equ 10	     ; maximum number ofincoming packets queued for 1 socket
1514 hidnplayr 202
; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start
203
SOCKET_QUEUE_LOCATION	equ SOCKETBUFFSIZE - SOCKET_QUEUE_SIZE*socket_queue_entry.size - queue.data
1249 hidnplayr 204
 
1159 hidnplayr 205
uglobal
1514 hidnplayr 206
	net_sockets	rd 4
1159 hidnplayr 207
	last_UDP_port	dw ? ; These values give the number of the last used ephemeral port
208
	last_TCP_port	dw ? ;
209
endg
210
 
211
 
1257 hidnplayr 212
;-----------------------------------------------------------------
1159 hidnplayr 213
;
214
; SOCKET_init
215
;
1257 hidnplayr 216
;-----------------------------------------------------------------
1529 hidnplayr 217
macro	SOCKET_init {
1159 hidnplayr 218
 
1514 hidnplayr 219
	xor	eax, eax
220
	mov	edi, net_sockets
221
	mov	ecx, 4
222
	rep	stosd
1159 hidnplayr 223
 
1529 hidnplayr 224
       @@:
225
	pseudo_random eax
226
	cmp	ax, MIN_EPHEMERAL_PORT
227
	jl	@r
228
	cmp	ax, MAX_EPHEMERAL_PORT
229
	jg	@r
230
	mov	[last_UDP_port], ax
231
 
232
       @@:
233
	pseudo_random eax
234
	cmp	ax, MIN_EPHEMERAL_PORT
235
	jl	@r
236
	cmp	ax, MAX_EPHEMERAL_PORT
237
	jg	@r
238
	mov	[last_TCP_port], ax
239
 
240
}
241
 
242
 
1257 hidnplayr 243
;-----------------------------------------------------------------
1159 hidnplayr 244
;
245
; Socket API (function 74)
246
;
1257 hidnplayr 247
;-----------------------------------------------------------------
1159 hidnplayr 248
align 4
249
sys_socket:
1514 hidnplayr 250
	cmp	ebx, 8		; highest possible number
1529 hidnplayr 251
	jg	@f
252
	lea	ebx, [sock_sysfn_table + 4*ebx]
1254 hidnplayr 253
	jmp	dword [ebx]
1529 hidnplayr 254
       @@:
255
	cmp	ebx, 255
256
	jz	SOCKET_debug
1159 hidnplayr 257
 
1529 hidnplayr 258
s_error:
259
	DEBUGF	1,"socket error\n"
260
	mov	dword [esp+32], -1
261
 
262
	ret
263
 
264
sock_sysfn_table:
1514 hidnplayr 265
	dd	SOCKET_open	; 0
266
	dd	SOCKET_close	; 1
267
	dd	SOCKET_bind	; 2
268
	dd	SOCKET_listen	; 3
269
	dd	SOCKET_connect	; 4
270
	dd	SOCKET_accept	; 5
271
	dd	SOCKET_send	; 6
272
	dd	SOCKET_receive	; 7
273
	dd	SOCKET_get_opt	; 8
274
;        dd      SOCKET_set_opt  ; 9
1159 hidnplayr 275
 
1254 hidnplayr 276
 
1159 hidnplayr 277
 
1257 hidnplayr 278
;-----------------------------------------------------------------
1159 hidnplayr 279
;
280
; SOCKET_open
281
;
282
;  IN:  domain in ecx
283
;       type in edx
1196 hidnplayr 284
;       protocol in esi
1159 hidnplayr 285
;  OUT: eax is socket num, -1 on error
286
;
1257 hidnplayr 287
;-----------------------------------------------------------------
1206 hidnplayr 288
align 4
1514 hidnplayr 289
SOCKET_open:
1159 hidnplayr 290
 
1514 hidnplayr 291
	DEBUGF	1,"socket_open: domain: %u, type: %u protocol: %x\n", ecx, edx, esi
1159 hidnplayr 292
 
1514 hidnplayr 293
	call	SOCKET_alloc
1185 hidnplayr 294
	jz	s_error
1159 hidnplayr 295
 
1514 hidnplayr 296
	mov	[eax + SOCKET.Domain], ecx
297
	mov	[eax + SOCKET.Type], edx
298
	mov	[eax + SOCKET.Protocol], esi
1159 hidnplayr 299
 
1514 hidnplayr 300
	mov	[esp+32], edi
1318 hidnplayr 301
 
1529 hidnplayr 302
	cmp	ecx, AF_INET4
303
	jnz	.no_stream
304
 
305
	push	[IP_LIST]		  ;;;;
306
	pop	[eax + IP_SOCKET.LocalIP] ;;;;
307
 
308
	cmp	edx, IP_PROTO_TCP
309
	jnz	.no_stream
310
 
1533 hidnplayr 311
	mov	ebx, eax
1529 hidnplayr 312
 
1533 hidnplayr 313
	lea	eax, [ebx + STREAM_SOCKET.snd]
314
	call	SOCKET_ring_create
1529 hidnplayr 315
 
1533 hidnplayr 316
	lea	eax, [ebx + STREAM_SOCKET.rcv]
317
	call	SOCKET_ring_create
318
 
1159 hidnplayr 319
	ret
320
 
1529 hidnplayr 321
  .no_stream:
1159 hidnplayr 322
 
1529 hidnplayr 323
	push	edi
324
	init_queue (eax + SOCKET_QUEUE_LOCATION)
325
	pop	edi
1159 hidnplayr 326
 
1529 hidnplayr 327
	ret
328
 
329
 
330
 
1257 hidnplayr 331
;-----------------------------------------------------------------
1159 hidnplayr 332
;
333
; SOCKET_bind
334
;
335
;  IN:  socket number in ecx
336
;       pointer to sockaddr struct in edx
337
;       length of that struct in esi
338
;  OUT: 0 on success
339
;
1257 hidnplayr 340
;-----------------------------------------------------------------
1206 hidnplayr 341
align 4
1514 hidnplayr 342
SOCKET_bind:
1159 hidnplayr 343
 
1514 hidnplayr 344
	DEBUGF	1,"socket_bind: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
1159 hidnplayr 345
 
1514 hidnplayr 346
	call	SOCKET_num_to_ptr
1185 hidnplayr 347
	jz	s_error
1159 hidnplayr 348
 
349
	cmp	esi, 2
1185 hidnplayr 350
	jl	s_error
1159 hidnplayr 351
 
352
	cmp	word [edx], AF_INET4
1249 hidnplayr 353
	je	.af_inet4
1159 hidnplayr 354
 
1249 hidnplayr 355
	cmp	word [edx], AF_UNIX
356
	je	.af_unix
357
 
358
	jmp	s_error
359
 
360
  .af_unix:
361
 
362
	; TODO: write code here
363
 
1514 hidnplayr 364
	mov	dword [esp+32], 0
1249 hidnplayr 365
	ret
366
 
1159 hidnplayr 367
  .af_inet4:
368
 
1514 hidnplayr 369
	DEBUGF	1,"af_inet4\n"
370
 
1159 hidnplayr 371
	cmp	esi, 6
1185 hidnplayr 372
	jl	s_error
1159 hidnplayr 373
 
1514 hidnplayr 374
	mov	ecx, [eax + SOCKET.Type]
1249 hidnplayr 375
 
1159 hidnplayr 376
	mov	bx, word [edx + 2]
377
	test	bx, bx
1206 hidnplayr 378
	jz	.find_free
1159 hidnplayr 379
 
1514 hidnplayr 380
	call	SOCKET_check_port
381
;        test    bx, bx
382
	jz	s_error
1206 hidnplayr 383
	jmp	.got_port
1159 hidnplayr 384
 
1249 hidnplayr 385
    .find_free:
1514 hidnplayr 386
	call	SOCKET_find_port
387
;        test    bx, bx
388
	jz	s_error
1159 hidnplayr 389
 
1249 hidnplayr 390
    .got_port:
1519 hidnplayr 391
	DEBUGF	1,"using local port: %u\n", bx
1514 hidnplayr 392
	mov	word [eax + UDP_SOCKET.LocalPort], bx
1159 hidnplayr 393
 
394
	DEBUGF	1,"local ip: %u.%u.%u.%u\n",\
1514 hidnplayr 395
	[eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\
396
	[eax + IP_SOCKET.LocalIP + 2]:1,[eax + IP_SOCKET.LocalIP + 3]:1
1159 hidnplayr 397
 
1514 hidnplayr 398
	mov	dword [esp+32], 0
1159 hidnplayr 399
	ret
400
 
401
 
402
 
403
 
1257 hidnplayr 404
;-----------------------------------------------------------------
1159 hidnplayr 405
;
406
; SOCKET_connect
407
;
408
;  IN:  socket number in ecx
409
;       pointer to sockaddr struct in edx
410
;       length of that struct in esi
411
;  OUT: 0 on success
412
;
1257 hidnplayr 413
;-----------------------------------------------------------------
1159 hidnplayr 414
align 4
1514 hidnplayr 415
SOCKET_connect:
1159 hidnplayr 416
 
1514 hidnplayr 417
	DEBUGF	1,"socket_connect: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
1159 hidnplayr 418
 
1514 hidnplayr 419
	call	SOCKET_num_to_ptr
1185 hidnplayr 420
	jz	s_error
1159 hidnplayr 421
 
1254 hidnplayr 422
	cmp	esi, 8
1185 hidnplayr 423
	jl	s_error
1159 hidnplayr 424
 
425
	cmp	word [edx], AF_INET4
426
	je	.af_inet4
427
 
1185 hidnplayr 428
	jmp	s_error
1159 hidnplayr 429
 
430
  .af_inet4:
431
 
1514 hidnplayr 432
	cmp	[eax + SOCKET.Type], IP_PROTO_UDP
1159 hidnplayr 433
	je	.udp
434
 
1514 hidnplayr 435
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
1206 hidnplayr 436
	je	.tcp
1159 hidnplayr 437
 
1185 hidnplayr 438
	jmp	s_error
1159 hidnplayr 439
 
1254 hidnplayr 440
  .udp:
1159 hidnplayr 441
	mov	bx , word [edx + 2]
1514 hidnplayr 442
	mov	word [eax + UDP_SOCKET.RemotePort], bx
443
	mov	[eax + UDP_SOCKET.firstpacket], 0
1519 hidnplayr 444
	DEBUGF	1,"remote port: %u\n",bx
1159 hidnplayr 445
 
446
	mov	ebx, dword [edx + 4]
1514 hidnplayr 447
	mov	dword [eax + IP_SOCKET.RemoteIP], ebx
1159 hidnplayr 448
	DEBUGF	1,"remote ip: %u.%u.%u.%u\n",[edx+4]:1,[edx+5]:1,[edx+6]:1,[edx+7]:1
449
 
1514 hidnplayr 450
	mov	dword [esp+32], 0
1159 hidnplayr 451
	ret
452
 
453
 
1254 hidnplayr 454
  .tcp:
1514 hidnplayr 455
	lea	ebx, [eax + SOCKET.lock]
1281 hidnplayr 456
	call	wait_mutex
1159 hidnplayr 457
 
1254 hidnplayr 458
	; fill in remote port and IP
1159 hidnplayr 459
 
1254 hidnplayr 460
	mov	bx , word [edx + 2]
1514 hidnplayr 461
	mov	[eax + TCP_SOCKET.RemotePort], bx
1519 hidnplayr 462
	DEBUGF	1,"remote port: %u\n", bx
1159 hidnplayr 463
 
1254 hidnplayr 464
	mov	ebx, dword [edx + 4]
1514 hidnplayr 465
	mov	[eax + IP_SOCKET.RemoteIP], ebx
1159 hidnplayr 466
 
1254 hidnplayr 467
	; check if local port and IP is ok
1159 hidnplayr 468
 
1514 hidnplayr 469
	cmp	[eax + TCP_SOCKET.LocalPort], 0
1254 hidnplayr 470
	jne	@f
1514 hidnplayr 471
	call	SOCKET_find_port
1254 hidnplayr 472
       @@:
1529 hidnplayr 473
	DEBUGF	1,"local port: %u\n", [eax + TCP_SOCKET.LocalPort]:2
1159 hidnplayr 474
 
1529 hidnplayr 475
;;;;;
476
	mov	[eax + TCP_SOCKET.timer_persist], 0
477
	mov	[eax + TCP_SOCKET.t_state], TCB_SYN_SENT
478
	mov	ebx, [TCP_sequence_num]
479
	add	[TCP_sequence_num], 6400
480
	mov	[eax + TCP_SOCKET.ISS], ebx
1533 hidnplayr 481
	mov	[eax + TCP_SOCKET.timer_keepalive], TCP_time_keep_init
1519 hidnplayr 482
 
1533 hidnplayr 483
	TCP_sendseqinit eax
1529 hidnplayr 484
 
1533 hidnplayr 485
;;;;            mov     [ebx + TCP_SOCKET.timer_retransmission],   ;; todo: create macro to set retransmission timer
486
 
1529 hidnplayr 487
	push	eax
1514 hidnplayr 488
	call	TCP_output
1529 hidnplayr 489
	pop	eax
1159 hidnplayr 490
 
1514 hidnplayr 491
	mov	[eax + SOCKET.lock], 0
1159 hidnplayr 492
 
1514 hidnplayr 493
	mov	dword [esp+32], 0	; success!
1159 hidnplayr 494
	ret
495
 
496
 
1257 hidnplayr 497
;-----------------------------------------------------------------
1159 hidnplayr 498
;
499
; SOCKET_listen
500
;
501
;  IN:  socket number in ecx
502
;       backlog in edx
503
;  OUT: eax is socket num, -1 on error
504
;
1257 hidnplayr 505
;-----------------------------------------------------------------
1206 hidnplayr 506
align 4
1514 hidnplayr 507
SOCKET_listen:
1159 hidnplayr 508
 
1514 hidnplayr 509
	DEBUGF	1,"Socket_listen: socknum: %u backlog: %u\n", ecx, edx
1159 hidnplayr 510
 
1514 hidnplayr 511
	call	SOCKET_num_to_ptr
1185 hidnplayr 512
	jz	s_error
1159 hidnplayr 513
 
1514 hidnplayr 514
	cmp	word [eax + SOCKET.Domain], AF_INET4
1254 hidnplayr 515
	jne	s_error
516
 
1514 hidnplayr 517
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
1254 hidnplayr 518
	jne	s_error
519
 
1514 hidnplayr 520
	; TODO: check local port number
521
 
1159 hidnplayr 522
	cmp	edx, MAX_backlog
1514 hidnplayr 523
	jle	.ok
524
	mov	edx, MAX_backlog
1159 hidnplayr 525
  .ok:
526
 
1514 hidnplayr 527
	mov	[eax + TCP_SOCKET.backlog], dx
528
	mov	[eax + TCP_SOCKET.t_state], TCB_LISTEN
529
	 or	[eax + SOCKET.options], SO_ACCEPTCON
1159 hidnplayr 530
 
531
	mov	dword [esp+32], 0
1514 hidnplayr 532
 
1159 hidnplayr 533
	ret
534
 
535
 
1257 hidnplayr 536
;-----------------------------------------------------------------
1159 hidnplayr 537
;
538
; SOCKET_accept
539
;
540
;  IN:  socket number in ecx
541
;       addr in edx
542
;       addrlen in esi
543
;  OUT: eax is socket num, -1 on error
544
;
1257 hidnplayr 545
;-----------------------------------------------------------------
1206 hidnplayr 546
align 4
1514 hidnplayr 547
SOCKET_accept:
1159 hidnplayr 548
 
1514 hidnplayr 549
	DEBUGF	1,"Socket_accept: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
1159 hidnplayr 550
 
1514 hidnplayr 551
	call	SOCKET_num_to_ptr
1185 hidnplayr 552
	jz	s_error
1159 hidnplayr 553
 
1514 hidnplayr 554
	cmp	word [eax + SOCKET.Domain], AF_INET4
1249 hidnplayr 555
	je	.af_inet4
556
 
557
	jmp	s_error
558
 
559
  .af_inet4:
560
 
1514 hidnplayr 561
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
1249 hidnplayr 562
	je	.tcp
563
 
564
	jmp	s_error
565
 
566
  .tcp:
567
 
1514 hidnplayr 568
	lea	ebx, [eax + SOCKET.lock]
1256 clevermous 569
	call	wait_mutex
1514 hidnplayr 570
 
571
	movzx	ebx, [eax + TCP_SOCKET.backlog_cur]
572
	test	ebx, ebx
1256 clevermous 573
	jz	.unlock_err
1514 hidnplayr 574
 
575
	dec	[eax + TCP_SOCKET.backlog_cur]
576
	mov	eax, [eax + TCP_SOCKET.end + (ebx-1)*4]
577
	mov	[eax + SOCKET.lock], 0
578
	mov	dword [esp+32], 0
579
 
580
	call	TCP_output	;;;;;
581
 
1159 hidnplayr 582
	ret
1514 hidnplayr 583
 
1256 clevermous 584
  .unlock_err:
1514 hidnplayr 585
	mov	[eax + SOCKET.lock], 0
1256 clevermous 586
	jmp	s_error
1159 hidnplayr 587
 
588
 
1257 hidnplayr 589
;-----------------------------------------------------------------
1159 hidnplayr 590
;
591
; SOCKET_close
592
;
593
;  IN:  socket number in ecx
594
;  OUT: eax is socket num, -1 on error
595
;
1257 hidnplayr 596
;-----------------------------------------------------------------
1206 hidnplayr 597
align 4
1514 hidnplayr 598
SOCKET_close:
1159 hidnplayr 599
 
1514 hidnplayr 600
	DEBUGF	1,"socket_close: socknum: %u\n", ecx
1159 hidnplayr 601
 
1514 hidnplayr 602
	call	SOCKET_num_to_ptr
1185 hidnplayr 603
	jz	s_error
1159 hidnplayr 604
 
1514 hidnplayr 605
	cmp	[eax + SOCKET.Domain], AF_INET4
1249 hidnplayr 606
	jne	s_error
1159 hidnplayr 607
 
1514 hidnplayr 608
	cmp	[eax + SOCKET.Type], IP_PROTO_UDP
609
	je	.free
1159 hidnplayr 610
 
1514 hidnplayr 611
	cmp	[eax + SOCKET.Type], IP_PROTO_ICMP
612
	je	.free
1159 hidnplayr 613
 
1514 hidnplayr 614
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
1206 hidnplayr 615
	je	.tcp
1159 hidnplayr 616
 
1185 hidnplayr 617
	jmp	s_error
1159 hidnplayr 618
 
619
  .tcp:
1514 hidnplayr 620
	test	[eax + TCP_SOCKET.t_state], TCB_SYN_RECEIVED  ;;;;;;
621
	jz	.free
1318 hidnplayr 622
 
1529 hidnplayr 623
  ;;;      call    TCP_output
1159 hidnplayr 624
 
1529 hidnplayr 625
  ;;;      mov     dword [esp+32], 0
1159 hidnplayr 626
 
1529 hidnplayr 627
  ;;;      ret
1159 hidnplayr 628
 
1514 hidnplayr 629
;       state must be LISTEN, SYN_SENT, CLOSED or maybe even invalid
630
;       so, we may destroy the socket
631
  .free:
632
	call	SOCKET_free
633
	mov	dword [esp+32], 0
1159 hidnplayr 634
 
635
	ret
636
 
637
 
1257 hidnplayr 638
;-----------------------------------------------------------------
1159 hidnplayr 639
;
640
; SOCKET_receive
641
;
642
;  IN:  socket number in ecx
1249 hidnplayr 643
;       addr to buffer in edx
644
;       length of buffer in esi
1159 hidnplayr 645
;       flags in edi
646
;  OUT: eax is number of bytes copied, -1 on error
647
;
1257 hidnplayr 648
;-----------------------------------------------------------------
1206 hidnplayr 649
align 4
1514 hidnplayr 650
SOCKET_receive:
1159 hidnplayr 651
 
1514 hidnplayr 652
	DEBUGF	1,"socket_receive: socknum: %u bufaddr: %x, buflength: %u, flags: %x\n", ecx, edx, esi, edi
653
 
654
	call	SOCKET_num_to_ptr
1185 hidnplayr 655
	jz	s_error
1159 hidnplayr 656
 
1533 hidnplayr 657
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP	;;;;;;;;
658
	je	.tcp
659
 
1281 hidnplayr 660
	mov	ebx, esi
1514 hidnplayr 661
	get_from_queue (eax + SOCKET_QUEUE_LOCATION),\
662
		       SOCKET_QUEUE_SIZE,\
663
		       socket_queue_entry.size,\
664
		       s_error
665
		       ; destroys esi and ecx
1281 hidnplayr 666
 
1514 hidnplayr 667
	mov	edi, edx					; addr to buffer
1159 hidnplayr 668
 
1249 hidnplayr 669
	mov	ecx, [esi + socket_queue_entry.data_size]
670
	DEBUGF	1,"Got %u bytes of data\n", ecx
1159 hidnplayr 671
 
1281 hidnplayr 672
	cmp	ecx, ebx
1249 hidnplayr 673
	jle	.large_enough
674
	DEBUGF	1,"Buffer too small...\n"
675
	jmp	s_error
1514 hidnplayr 676
 
1249 hidnplayr 677
  .large_enough:
1514 hidnplayr 678
	push	[esi + socket_queue_entry.buf_ptr]	       ; save the buffer addr so we can clear it later
679
	mov	esi, [esi + socket_queue_entry.data_ptr]
1249 hidnplayr 680
	DEBUGF	1,"Source buffer: %x, real addr: %x\n", [esp], esi
1514 hidnplayr 681
	mov	dword[esp+32+4], ecx				; return number of bytes copied in ebx
1159 hidnplayr 682
 
1514 hidnplayr 683
; copy the data
1249 hidnplayr 684
	shr	ecx, 1
685
	jnc	.nb
686
	movsb
687
.nb:	shr	ecx, 1
688
	jnc	.nw
689
	movsw
1274 hidnplayr 690
.nw:	test	ecx, ecx
691
	jz	.nd
692
	rep	movsd
693
.nd:
1514 hidnplayr 694
; remove the packet     ;;; TODO: only if it is empty!!
1159 hidnplayr 695
 
1514 hidnplayr 696
	call	kernel_free
1533 hidnplayr 697
	ret
1514 hidnplayr 698
 
1533 hidnplayr 699
  .tcp:
700
 
701
	mov	ecx, esi
702
	mov	edi, edx
703
	add	eax, STREAM_SOCKET.rcv
704
	call	SOCKET_ring_read
1534 hidnplayr 705
	call	SOCKET_ring_free
1533 hidnplayr 706
 
707
	mov	dword[esp+32], ecx			      ; return number of bytes copied in ebx
708
 
1159 hidnplayr 709
	ret
710
 
711
 
1257 hidnplayr 712
;-----------------------------------------------------------------
1159 hidnplayr 713
;
714
; SOCKET_send
715
;
716
;
717
;  IN:  socket number in ecx
1206 hidnplayr 718
;       pointer to data in edx
719
;       datalength in esi
1159 hidnplayr 720
;       flags in edi
721
;  OUT: -1 on error
722
;
1257 hidnplayr 723
;-----------------------------------------------------------------
1206 hidnplayr 724
align 4
1514 hidnplayr 725
SOCKET_send:
1159 hidnplayr 726
 
1514 hidnplayr 727
	DEBUGF	1,"socket_send: socknum: %u sockaddr: %x, length: %u, flags: %x\n", ecx, edx, esi, edi
1159 hidnplayr 728
 
1514 hidnplayr 729
	call	SOCKET_num_to_ptr
1185 hidnplayr 730
	jz	s_error
1159 hidnplayr 731
 
1514 hidnplayr 732
	cmp	word [eax + SOCKET.Domain], AF_INET4
1206 hidnplayr 733
	je	.af_inet4
734
 
735
	jmp	s_error
736
 
737
  .af_inet4:
1514 hidnplayr 738
	DEBUGF	1,"af_inet4\n"
1206 hidnplayr 739
 
1529 hidnplayr 740
	cmp	[eax + IP_SOCKET.LocalIP], 0
741
	jne	@f
742
	mov	ebx, [IP_LIST]				;;;;
743
	mov	dword [eax + IP_SOCKET.LocalIP], ebx
744
       @@:
745
 
1514 hidnplayr 746
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
1249 hidnplayr 747
	je	.tcp
1159 hidnplayr 748
 
1514 hidnplayr 749
	cmp	[eax + SOCKET.Type], IP_PROTO_UDP
1159 hidnplayr 750
	je	.udp
751
 
1185 hidnplayr 752
	jmp	s_error
1159 hidnplayr 753
 
754
  .udp:
1514 hidnplayr 755
	DEBUGF	1,"type: UDP\n"
1159 hidnplayr 756
 
1514 hidnplayr 757
; check if local port is valid
758
	cmp	[eax + UDP_SOCKET.LocalPort], 0
1249 hidnplayr 759
	jne	@f
1206 hidnplayr 760
 
1514 hidnplayr 761
	call	SOCKET_find_port
762
	jz	s_error
1206 hidnplayr 763
 
1514 hidnplayr 764
; Now, send the packet
1249 hidnplayr 765
     @@:
1159 hidnplayr 766
	mov	ecx, esi
767
	mov	esi, edx
768
 
1514 hidnplayr 769
	call	UDP_output
1159 hidnplayr 770
 
1514 hidnplayr 771
	mov	dword [esp+32], 0
1159 hidnplayr 772
	ret
773
 
1249 hidnplayr 774
  .tcp:
1514 hidnplayr 775
	DEBUGF	1,"type: TCP\n"
1159 hidnplayr 776
 
1514 hidnplayr 777
; check if local port is valid
778
	cmp	[eax + TCP_SOCKET.LocalPort], 0
1254 hidnplayr 779
	jne	@f
780
 
1514 hidnplayr 781
	call	SOCKET_find_port
782
	jz	s_error
1254 hidnplayr 783
 
784
     @@:
785
 
1533 hidnplayr 786
	push	eax
787
	mov	ecx, esi
788
	mov	esi, edx
789
	add	eax, STREAM_SOCKET.snd
790
	call	SOCKET_ring_write
791
	pop	eax
1514 hidnplayr 792
	call	TCP_output
1254 hidnplayr 793
 
1249 hidnplayr 794
	mov	[esp+32], eax
795
	ret
1159 hidnplayr 796
 
1249 hidnplayr 797
 
798
 
799
 
1257 hidnplayr 800
;-----------------------------------------------------------------
1256 clevermous 801
;
1257 hidnplayr 802
; SOCKET_get_options
1256 clevermous 803
;
1514 hidnplayr 804
;  IN:  ecx = socket number
805
;       edx = pointer to the options:
1257 hidnplayr 806
;               dd      level, optname, optval, optlen
1256 clevermous 807
;  OUT: -1 on error
808
;
809
; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
810
; TODO: find best way to notify that send()'ed data were acknowledged
1299 clevermous 811
; Also pseudo-optname -3 is valid and returns socket state, one of TCB_*.
1256 clevermous 812
;
1257 hidnplayr 813
;-----------------------------------------------------------------
814
align 4
1514 hidnplayr 815
SOCKET_get_opt:
1257 hidnplayr 816
 
1514 hidnplayr 817
	DEBUGF	1,"socket_get_opt\n"
818
 
819
	call	SOCKET_num_to_ptr
820
	jz	s_error
821
 
1256 clevermous 822
	cmp	dword [edx], IP_PROTO_TCP
1514 hidnplayr 823
	jnz	s_error
1256 clevermous 824
	cmp	dword [edx+4], -2
1299 clevermous 825
	jz	@f
826
	cmp	dword [edx+4], -3
1514 hidnplayr 827
	jnz	s_error
1299 clevermous 828
@@:
1514 hidnplayr 829
;        mov     eax, [edx+12]
830
;        test    eax, eax
831
;        jz      .fail
832
;        cmp     dword [eax], 4
833
;        mov     dword [eax], 4
834
;        jb      .fail
835
;        stdcall net_socket_num_to_addr, ecx
836
;        test    eax, eax
837
;        jz      .fail
838
;        ; todo: check that eax is really TCP socket
839
;        mov     ecx, [eax + TCP_SOCKET.last_ack_number]
840
;        cmp     dword [edx+4], -2
841
;        jz      @f
842
;        mov     ecx, [eax + TCP_SOCKET.state]
1299 clevermous 843
@@:
1256 clevermous 844
	mov	eax, [edx+8]
845
	test	eax, eax
846
	jz	@f
847
	mov	[eax], ecx
848
@@:
1257 hidnplayr 849
	mov	dword [esp+32], 0
1256 clevermous 850
	ret
1159 hidnplayr 851
 
852
 
1529 hidnplayr 853
 
1257 hidnplayr 854
;-----------------------------------------------------------------
1206 hidnplayr 855
;
1529 hidnplayr 856
; SOCKET_debug
857
;
858
;  Copies socket variables to application buffer
859
;
860
;  IN:  ecx = socket number
861
;       edx = pointer to buffer
862
;
863
;  OUT: -1 on error
864
;-----------------------------------------------------------------
865
align 4
866
SOCKET_debug:
867
 
868
	DEBUGF	1,"socket_debug\n"
869
 
870
	call	SOCKET_num_to_ptr
871
	jz	s_error
872
 
873
	mov	esi, eax
874
	mov	edi, edx
875
	mov	ecx, SOCKETBUFFSIZE/4
876
	rep	movsd
877
 
878
	mov	dword [esp+32], 0
879
	ret
880
 
881
 
882
;-----------------------------------------------------------------
883
;
1514 hidnplayr 884
; SOCKET_find_port
1206 hidnplayr 885
;
1514 hidnplayr 886
; Fills in the local port number for TCP and UDP sockets
887
; This procedure always works because the number of sockets is
888
; limited to a smaller number then the number of possible ports
1206 hidnplayr 889
;
1514 hidnplayr 890
;  IN:  eax = socket pointer
891
;  OUT: /
1206 hidnplayr 892
;
1257 hidnplayr 893
;-----------------------------------------------------------------
1206 hidnplayr 894
align 4
1514 hidnplayr 895
SOCKET_find_port:
1159 hidnplayr 896
 
1514 hidnplayr 897
	DEBUGF	1,"socket_find_free_port\n"
1159 hidnplayr 898
 
1514 hidnplayr 899
	push	ebx esi ecx
900
 
901
	cmp	[eax + SOCKET.Type], IP_PROTO_UDP
1206 hidnplayr 902
	je	.udp
1159 hidnplayr 903
 
1514 hidnplayr 904
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
1206 hidnplayr 905
	je	.tcp
1159 hidnplayr 906
 
1514 hidnplayr 907
	jmp	.error
908
 
909
  .done:
910
	mov	[eax + UDP_SOCKET.LocalPort], bx
911
  .error:
912
	pop	ecx esi ebx
913
	ret
914
 
1206 hidnplayr 915
  .udp:
916
	mov	bx, [last_UDP_port]
1514 hidnplayr 917
	call	.findit
918
	mov	[last_UDP_port], bx
919
	jmp	.done
1206 hidnplayr 920
 
921
  .tcp:
922
	mov	bx, [last_TCP_port]
1514 hidnplayr 923
	call	.findit
924
	mov	[last_TCP_port], bx
925
	jmp	.done
1206 hidnplayr 926
 
927
 
1514 hidnplayr 928
  .restart:
929
	mov	bx, MIN_EPHEMERAL_PORT
930
  .findit:
1206 hidnplayr 931
	inc	bx
932
 
933
	cmp	bx, MAX_EPHEMERAL_PORT
1514 hidnplayr 934
	jz	.restart
1206 hidnplayr 935
 
1514 hidnplayr 936
	call	SOCKET_check_port
937
	jz	.findit
1206 hidnplayr 938
 
939
	ret
940
 
1257 hidnplayr 941
 
942
 
943
;-----------------------------------------------------------------
1206 hidnplayr 944
;
1514 hidnplayr 945
; SOCKET_check_port
1206 hidnplayr 946
;
1514 hidnplayr 947
; Checks if a local port number is unused
948
; If the proposed port number is unused, it is filled in in the socket structure
1206 hidnplayr 949
;
1514 hidnplayr 950
;  IN:  eax = socket ptr (to find out if its a TCP/UDP socket)
951
;        bx = proposed socket number
1206 hidnplayr 952
;
1514 hidnplayr 953
;  OUT:  ZF = cleared on error
954
;
1257 hidnplayr 955
;-----------------------------------------------------------------
1206 hidnplayr 956
align 4
1514 hidnplayr 957
SOCKET_check_port:
958
 
959
	DEBUGF	1,"socket_check_port\n"
960
 
961
	mov	ecx, [eax + SOCKET.Type]
1206 hidnplayr 962
	mov	esi, net_sockets
963
 
964
  .next_socket:
1514 hidnplayr 965
	mov	esi, [esi + SOCKET.NextPtr]
1206 hidnplayr 966
	or	esi, esi
967
	jz	.port_ok
968
 
1514 hidnplayr 969
	cmp	[esi + SOCKET.Type], ecx
1206 hidnplayr 970
	jne	.next_socket
971
 
1514 hidnplayr 972
	cmp	[esi + UDP_SOCKET.LocalPort], bx
1206 hidnplayr 973
	jne	.next_socket
974
 
1514 hidnplayr 975
	DEBUGF	1,"local port %u already in use\n", bx
976
	ret
1206 hidnplayr 977
 
978
  .port_ok:
1514 hidnplayr 979
	mov	[eax + UDP_SOCKET.LocalPort], bx
980
	or	bx, bx					; set the zero-flag
981
 
1206 hidnplayr 982
	ret
983
 
984
 
1257 hidnplayr 985
 
986
;-----------------------------------------------------------------
1206 hidnplayr 987
;
1514 hidnplayr 988
; SOCKET_input
1206 hidnplayr 989
;
1249 hidnplayr 990
; Updates a socket with received data
1206 hidnplayr 991
;
1514 hidnplayr 992
; Note: the mutex should already be set !
1206 hidnplayr 993
;
1249 hidnplayr 994
;  IN:  eax = socket ptr
1514 hidnplayr 995
;       ebx = pointer to device struct
996
;       ecx = data size
997
;       esi = ptr to data
998
;       [esp] = ptr to buf
999
;       [esp + 4] = buf size
1249 hidnplayr 1000
;
1514 hidnplayr 1001
;  OUT: /
1206 hidnplayr 1002
;
1257 hidnplayr 1003
;-----------------------------------------------------------------
1206 hidnplayr 1004
align 4
1514 hidnplayr 1005
SOCKET_input:
1206 hidnplayr 1006
 
1514 hidnplayr 1007
	DEBUGF	1,"socket_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
1206 hidnplayr 1008
 
1514 hidnplayr 1009
	mov	dword[esp+4], ecx
1010
	push	esi
1249 hidnplayr 1011
	mov	esi, esp
1514 hidnplayr 1012
 
1013
	add_to_queue (eax + SOCKET_QUEUE_LOCATION),\
1014
		     SOCKET_QUEUE_SIZE,\
1015
		     socket_queue_entry.size,\
1016
		     SOCKET_input.full
1017
 
1249 hidnplayr 1018
	DEBUGF	1,"Queued packet successfully\n"
1257 hidnplayr 1019
	add	esp, socket_queue_entry.size
1514 hidnplayr 1020
	mov	[eax + SOCKET.lock], 0
1021
	jmp	SOCKET_notify_owner
1206 hidnplayr 1022
 
1514 hidnplayr 1023
  .full:
1024
	DEBUGF	2,"Socket %x is full!\n", eax
1025
	mov	[eax + SOCKET.lock], 0
1026
	call	kernel_free
1027
	add	esp, 8
1206 hidnplayr 1028
 
1514 hidnplayr 1029
	ret
1030
 
1533 hidnplayr 1031
 
1032
;--------------------------
1033
;
1034
; eax = ptr to ring struct (just a buffer of the right size)
1035
;
1036
align 4
1037
SOCKET_ring_create:
1038
 
1039
	mov	esi, eax
1040
	stdcall create_ring_buffer, SOCKET_MAXDATA, PG_SW
1041
 
1042
	DEBUGF	1,"SOCKET_ring_created: %x\n", eax
1043
	mov	[esi + RING_BUFFER.start_ptr], eax
1044
	mov	[esi + RING_BUFFER.write_ptr], eax
1045
	mov	[esi + RING_BUFFER.read_ptr], eax
1046
	mov	[esi + RING_BUFFER.size], 0
1047
	add	eax,  SOCKET_MAXDATA
1048
	mov	[esi + RING_BUFFER.end_ptr], eax
1049
 
1050
	ret
1051
 
1514 hidnplayr 1052
;-----------------------------------------------------------------
1053
;
1533 hidnplayr 1054
; SOCKET_ring_write
1529 hidnplayr 1055
;
1533 hidnplayr 1056
; Adds data to a stream socket, and updates write pointer and size
1529 hidnplayr 1057
;
1058
;  IN:  eax = ptr to ring struct
1059
;       ecx = data size
1060
;       esi = ptr to data
1061
;
1533 hidnplayr 1062
;  OUT: ecx = number of bytes stored
1529 hidnplayr 1063
;
1064
;-----------------------------------------------------------------
1065
align 4
1533 hidnplayr 1066
SOCKET_ring_write:
1529 hidnplayr 1067
 
1533 hidnplayr 1068
	DEBUGF	1,"SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
1529 hidnplayr 1069
 
1533 hidnplayr 1070
	add	[eax + RING_BUFFER.size], ecx
1071
	cmp	[eax + RING_BUFFER.size], SOCKET_MAXDATA
1529 hidnplayr 1072
	jg	.too_large
1073
 
1074
  .copy:
1533 hidnplayr 1075
	mov	edi, [eax + RING_BUFFER.write_ptr]
1076
	DEBUGF	2,"Copying %u bytes from %x to %x\n", ecx, esi, edi
1529 hidnplayr 1077
 
1078
	push	ecx
1533 hidnplayr 1079
	shr	ecx, 1
1080
	jnc	.nb
1081
	movsb
1082
.nb:
1083
	shr	ecx, 1
1084
	jnc	.nw
1085
	movsw
1086
.nw:
1087
	test	ecx, ecx
1088
	jz	.nd
1089
	rep	movsd
1090
.nd:
1529 hidnplayr 1091
	pop	ecx
1092
 
1533 hidnplayr 1093
	cmp	edi, [eax + RING_BUFFER.end_ptr]
1094
	jge	.wrap
1095
	mov	[eax + RING_BUFFER.write_ptr], edi
1096
 
1529 hidnplayr 1097
	ret
1098
 
1533 hidnplayr 1099
  .wrap:
1100
	sub	edi, SOCKET_MAXDATA
1101
	mov	[eax + RING_BUFFER.write_ptr], edi
1102
 
1103
	ret
1104
 
1529 hidnplayr 1105
  .too_large:
1106
	mov	ecx, SOCKET_MAXDATA				; calculate number of bytes available in buffer
1107
	sub	ecx, [eax + RING_BUFFER.size]
1533 hidnplayr 1108
	jge	.full
1529 hidnplayr 1109
 
1110
	mov	[eax + RING_BUFFER.size], SOCKET_MAXDATA	; update size, we will fill buffer completely
1111
	jmp	.copy
1112
 
1113
  .full:
1114
	DEBUGF	2,"Ring buffer is full!\n"
1115
	xor	ecx, ecx
1116
	ret
1117
 
1118
 
1119
;-----------------------------------------------------------------
1120
;
1121
; SOCKET_ring_read
1122
;
1533 hidnplayr 1123
; reads the data, BUT DOES NOT CLEAR IT FROM MEMORY YET
1529 hidnplayr 1124
;
1125
;  IN:  eax = ptr to ring struct
1126
;       ecx = buffer size
1127
;       edi = ptr to buffer
1128
;
1533 hidnplayr 1129
;  OUT: ecx = number of bytes read
1529 hidnplayr 1130
;
1131
;-----------------------------------------------------------------
1132
align 4
1133
SOCKET_ring_read:
1134
 
1533 hidnplayr 1135
	DEBUGF	1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u\n", eax, edi, ecx
1529 hidnplayr 1136
 
1533 hidnplayr 1137
	cmp	ecx, [eax + RING_BUFFER.size]
1138
	jg	.less_data
1529 hidnplayr 1139
 
1140
  .copy:
1533 hidnplayr 1141
	mov	esi, [eax + RING_BUFFER.read_ptr]
1529 hidnplayr 1142
 
1533 hidnplayr 1143
	DEBUGF	2,"Copying %u bytes from %x to %x\n", ecx, esi, edi
1529 hidnplayr 1144
	push	ecx
1533 hidnplayr 1145
	shr	ecx, 1
1146
	jnc	.nb
1147
	movsb
1148
.nb:
1149
	shr	ecx, 1
1150
	jnc	.nw
1151
	movsw
1152
.nw:
1153
	test	ecx, ecx
1154
	jz	.nd
1155
	rep	movsd
1156
.nd:
1529 hidnplayr 1157
	pop	ecx
1158
 
1533 hidnplayr 1159
;  .no_data_at_all:
1529 hidnplayr 1160
	ret
1161
 
1533 hidnplayr 1162
  .less_data:
1529 hidnplayr 1163
	mov	ecx, [eax + RING_BUFFER.size]
1533 hidnplayr 1164
	test	ecx, ecx
1165
;        jz      .no_data_at_all
1529 hidnplayr 1166
	jmp	.copy
1167
 
1168
 
1169
;-----------------------------------------------------------------
1170
;
1171
; SOCKET_ring_free
1172
;
1173
; Free's some bytes from the ringbuffer
1174
;
1175
;  IN:  eax = ptr to ring struct
1176
;       ecx = data size
1177
;
1178
;  OUT: ecx = number of bytes free-ed
1179
;
1180
;-----------------------------------------------------------------
1181
align 4
1182
SOCKET_ring_free:
1183
 
1184
	DEBUGF	1,"Trying to free %u bytes of data from ring %x\n", ecx, eax
1185
 
1186
	sub	[eax + RING_BUFFER.size], ecx
1533 hidnplayr 1187
	jl	.sumthinwong
1529 hidnplayr 1188
	add	[eax + RING_BUFFER.read_ptr], ecx
1189
 
1190
	mov	edx, [eax + RING_BUFFER.end_ptr]
1191
	cmp	[eax + RING_BUFFER.read_ptr], edx
1192
	jl	@f
1533 hidnplayr 1193
	sub	[eax + RING_BUFFER.read_ptr], SOCKET_MAXDATA
1529 hidnplayr 1194
       @@:
1195
	ret
1196
 
1533 hidnplayr 1197
  .sumthinwong: 	       ; we could free all available bytes, but that would be stupid, i guess..
1198
	add	[eax + RING_BUFFER.size], ecx
1529 hidnplayr 1199
	xor	ecx, ecx
1200
	ret
1201
 
1202
 
1203
;-----------------------------------------------------------------
1204
;
1514 hidnplayr 1205
; SOCKET_notify_owner
1206
;
1207
; notify's the owner of a socket that something happened
1208
;
1209
;  IN:  eax = socket ptr
1210
;  OUT: /
1211
;
1212
;-----------------------------------------------------------------
1213
align 4
1214
SOCKET_notify_owner:
1215
 
1216
	DEBUGF	1,"socket_notify_owner\n"
1217
 
1218
	call	SOCKET_check
1219
	jz	.error
1220
 
1221
	push	ecx eax esi
1222
 
1223
; socket exists, now try to flag an event to the application
1224
 
1225
	mov	eax, [eax + SOCKET.PID]
1206 hidnplayr 1226
	mov	ecx, 1
1227
	mov	esi, TASK_DATA + TASKDATA.pid
1228
 
1229
       .next_pid:
1514 hidnplayr 1230
	cmp	[esi], eax
1206 hidnplayr 1231
	je	.found_pid
1232
	inc	ecx
1233
	add	esi, 0x20
1234
	cmp	ecx, [TASK_COUNT]
1235
	jbe	.next_pid
1236
 
1514 hidnplayr 1237
; PID not found, TODO: close socket!
1238
 
1239
	jmp	.error2
1240
 
1206 hidnplayr 1241
       .found_pid:
1242
	shl	ecx, 8
1514 hidnplayr 1243
	or	[ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK
1206 hidnplayr 1244
	mov	[check_idle_semaphore], 200
1245
 
1514 hidnplayr 1246
	DEBUGF	1,"owner notified\n"
1247
 
1248
  .error2:
1249
	pop	esi eax ecx
1250
  .error:
1251
 
1206 hidnplayr 1252
	ret
1253
 
1254
 
1514 hidnplayr 1255
;--------------------------------------------------------------------
1256
;
1257
; SOCKET_alloc
1258
;
1159 hidnplayr 1259
; Allocate memory for socket data and put new socket into the list
1260
; Newly created socket is initialized with calling PID and number and
1261
; put into beginning of list (which is a fastest way).
1262
;
1514 hidnplayr 1263
; IN:  /
1264
; OUT: eax = 0 on error, socket ptr otherwise
1265
;      edi = socket number
1266
;       ZF = cleared on error
1159 hidnplayr 1267
;
1514 hidnplayr 1268
;--------------------------------------------------------------------
1269
align 4
1270
SOCKET_alloc:
1271
 
1272
	push	ecx ebx
1273
 
1159 hidnplayr 1274
	stdcall kernel_alloc, SOCKETBUFFSIZE
1529 hidnplayr 1275
	DEBUGF	1, "SOCKET_alloc: ptr=%x\n", eax
1159 hidnplayr 1276
	or	eax, eax
1277
	jz	.exit
1278
 
1514 hidnplayr 1279
; zero-initialize allocated memory
1280
	push	eax edi
1159 hidnplayr 1281
	mov	edi, eax
1282
	mov	ecx, SOCKETBUFFSIZE / 4
1283
	xor	eax, eax
1284
	rep	stosd
1514 hidnplayr 1285
	pop	edi eax
1159 hidnplayr 1286
 
1514 hidnplayr 1287
; find first free socket number and use it
1159 hidnplayr 1288
	mov	ebx, net_sockets
1289
	xor	ecx, ecx
1290
  .next_socket_number:
1291
	inc	ecx
1292
  .next_socket:
1514 hidnplayr 1293
	mov	ebx, [ebx + SOCKET.NextPtr]
1529 hidnplayr 1294
	test	ebx, ebx
1514 hidnplayr 1295
	jz	.last_socket
1296
	cmp	[ebx + SOCKET.Number], ecx
1159 hidnplayr 1297
	jne	.next_socket
1298
	mov	ebx, net_sockets
1299
	jmp	.next_socket_number
1300
 
1514 hidnplayr 1301
  .last_socket:
1302
	mov	[eax + SOCKET.Number], ecx
1529 hidnplayr 1303
	DEBUGF	1, "SOCKET_alloc: number=%u\n", ecx
1304
	mov	edi, ecx
1159 hidnplayr 1305
 
1514 hidnplayr 1306
; Fill in PID
1307
	mov	ebx, [TASK_BASE]
1308
	mov	ebx, [ebx + TASKDATA.pid]
1309
	mov	[eax + SOCKET.PID], ebx
1310
 
1529 hidnplayr 1311
; add socket to the list by re-arranging some pointers
1514 hidnplayr 1312
	mov	ebx, [net_sockets + SOCKET.NextPtr]
1313
 
1314
	mov	[eax + SOCKET.PrevPtr], net_sockets
1315
	mov	[eax + SOCKET.NextPtr], ebx
1316
 
1529 hidnplayr 1317
	test	ebx, ebx
1514 hidnplayr 1318
	jz	@f
1319
	add	ebx, SOCKET.lock	; lock the next socket
1320
	call	wait_mutex
1321
	sub	ebx, SOCKET.lock
1322
	mov	[ebx + SOCKET.PrevPtr], eax
1323
	mov	[ebx + SOCKET.lock], 0
1324
       @@:
1325
 
1326
	mov	[net_sockets + SOCKET.NextPtr], eax
1529 hidnplayr 1327
	or	eax, eax		; used to clear zero flag
1159 hidnplayr 1328
  .exit:
1514 hidnplayr 1329
	pop	ebx ecx
1330
 
1159 hidnplayr 1331
	ret
1332
 
1514 hidnplayr 1333
 
1334
;----------------------------------------------------
1159 hidnplayr 1335
;
1514 hidnplayr 1336
; SOCKET_free
1159 hidnplayr 1337
;
1514 hidnplayr 1338
; Free socket data memory and remove socket from the list
1339
;
1340
; IN:  eax = socket ptr
1341
; OUT: /
1342
;
1343
;----------------------------------------------------
1344
align 4
1345
SOCKET_free:
1159 hidnplayr 1346
 
1514 hidnplayr 1347
	DEBUGF	1, "socket_free: %x\n", eax
1348
 
1349
	call	SOCKET_check
1159 hidnplayr 1350
	jz	.error
1351
 
1514 hidnplayr 1352
	push	ebx
1353
	lea	ebx, [eax + SOCKET.lock]
1354
	call	wait_mutex
1355
 
1356
	DEBUGF	1, "freeing socket..\n"
1357
 
1529 hidnplayr 1358
	cmp	[eax + SOCKET.Domain], AF_INET4
1359
	jnz	.no_stream
1360
 
1361
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
1362
	jnz	.no_stream
1363
 
1533 hidnplayr 1364
	stdcall kernel_free, [eax + STREAM_SOCKET.rcv + RING_BUFFER.start_ptr]
1365
	stdcall kernel_free, [eax + STREAM_SOCKET.snd + RING_BUFFER.start_ptr]
1529 hidnplayr 1366
  .no_stream:
1367
 
1514 hidnplayr 1368
	push	eax				; this will be passed to kernel_free
1369
	mov	ebx, [eax + SOCKET.NextPtr]
1370
	mov	eax, [eax + SOCKET.PrevPtr]
1371
 
1372
	DEBUGF	1, "linking socket %x to socket %x\n", eax, ebx
1373
 
1374
	test	eax, eax
1159 hidnplayr 1375
	jz	@f
1514 hidnplayr 1376
	mov	[eax + SOCKET.NextPtr], ebx
1377
       @@:
1159 hidnplayr 1378
 
1514 hidnplayr 1379
	test	ebx, ebx
1380
	jz	@f
1381
	mov	[ebx + SOCKET.PrevPtr], eax
1382
       @@:
1249 hidnplayr 1383
 
1514 hidnplayr 1384
	call	kernel_free
1385
	pop	ebx
1159 hidnplayr 1386
 
1514 hidnplayr 1387
	DEBUGF	1, "socket is gone!\n"
1388
 
1159 hidnplayr 1389
  .error:
1390
	ret
1391
 
1514 hidnplayr 1392
 
1533 hidnplayr 1393
; IN:  socket nr in ebx
1394
; OUT: socket nr in eax
1395
; preserves edx
1529 hidnplayr 1396
 
1397
align 4
1398
SOCKET_fork:
1399
 
1400
;; Exit if backlog queue is full
1401
;        mov     ax, [ebx + TCP_SOCKET.backlog_cur]
1402
;        cmp     ax, [ebx + TCP_SOCKET.backlog]
1403
;        jae     .exit
1404
 
1405
; Allocate new socket
1406
	call	SOCKET_alloc
1407
     ;;;   jz      .fail
1408
 
1409
; Copy structure from current socket to new, (including lock!)
1410
; We start at PID to reserve the socket num, and the 2 pointers at beginning of socket
1411
	lea	esi, [edx + SOCKET.PID]
1412
	lea	edi, [eax + SOCKET.PID]
1413
	mov	ecx, (TCP_SOCKET.end - SOCKET.PID + 3)/4
1414
	rep	movsd
1415
 
1416
;; Push pointer to new socket to queue
1417
;        movzx   ecx, [ebx + TCP_SOCKET.backlog_cur]
1418
;        inc     [ebx + TCP_SOCKET.backlog_cur]
1419
;        mov     [ebx + TCP_SOCKET.end + ecx*4], eax
1420
 
1421
;;;;        mov     [eax + IP_SOCKET.RemoteIP], esi ; IP source address
1422
 
1423
	ret
1424
 
1425
 
1514 hidnplayr 1426
;---------------------------------------------------
1427
;
1428
; SOCKET_num_to_ptr
1429
;
1159 hidnplayr 1430
; Get socket structure address by its number
1431
;
1514 hidnplayr 1432
; IN:  ecx = socket number
1533 hidnplayr 1433
; OUT: eax = 0 on error, socket ptr otherwise
1514 hidnplayr 1434
;       ZF = set on error
1159 hidnplayr 1435
;
1514 hidnplayr 1436
;---------------------------------------------------
1437
align 4
1438
SOCKET_num_to_ptr:
1159 hidnplayr 1439
 
1514 hidnplayr 1440
	DEBUGF	1,"socket_num_to_ptr: %u ", ecx
1441
 
1442
	mov	eax, net_sockets
1443
 
1159 hidnplayr 1444
  .next_socket:
1514 hidnplayr 1445
	mov	eax, [eax + SOCKET.NextPtr]
1446
	or	eax, eax
1159 hidnplayr 1447
	jz	.error
1514 hidnplayr 1448
	cmp	[eax + SOCKET.Number], ecx
1159 hidnplayr 1449
	jne	.next_socket
1450
 
1514 hidnplayr 1451
	test	eax, eax
1159 hidnplayr 1452
 
1514 hidnplayr 1453
	DEBUGF	1,"(%x)\n", eax
1159 hidnplayr 1454
  .error:
1455
	ret
1456
 
1514 hidnplayr 1457
 
1458
;---------------------------------------------------
1159 hidnplayr 1459
;
1514 hidnplayr 1460
; SOCKET_ptr_to_num
1159 hidnplayr 1461
;
1514 hidnplayr 1462
; Get socket number by its address
1463
;
1464
; IN:  eax = socket ptr
1465
; OUT: eax = 0 on error, socket num otherwise
1466
;       ZF = set on error
1467
;
1468
;---------------------------------------------------
1469
align 4
1470
SOCKET_ptr_to_num:
1471
 
1472
	DEBUGF	1,"socket_ptr_to_num: %x ", eax
1473
 
1474
	call	SOCKET_check
1159 hidnplayr 1475
	jz	.error
1476
 
1514 hidnplayr 1477
	mov	eax, [eax + SOCKET.Number]
1478
 
1479
	DEBUGF	1,"(%u)\n", eax
1480
 
1481
  .error:
1482
	ret
1483
 
1484
 
1485
;---------------------------------------------------
1486
;
1487
; SOCKET_check
1488
;
1489
; checks if the given value is really a socket ptr
1490
;
1491
; IN:  eax = socket ptr
1492
; OUT: eax = 0 on error, unchanged otherwise
1493
;       ZF = set on error
1494
;
1495
;---------------------------------------------------
1496
align 4
1497
SOCKET_check:
1498
 
1499
	DEBUGF	1,"socket_check\n"
1500
 
1501
	push	ebx
1159 hidnplayr 1502
	mov	ebx, net_sockets
1514 hidnplayr 1503
 
1159 hidnplayr 1504
  .next_socket:
1514 hidnplayr 1505
	mov	ebx, [ebx + SOCKET.NextPtr]
1159 hidnplayr 1506
	or	ebx, ebx
1514 hidnplayr 1507
	jz	.done
1159 hidnplayr 1508
	cmp	ebx, eax
1514 hidnplayr 1509
	jnz	.next_socket
1159 hidnplayr 1510
 
1514 hidnplayr 1511
  .done:
1512
	mov	eax, ebx
1513
	test	eax, eax
1514
	pop	ebx
1515
 
1159 hidnplayr 1516
	ret
1517
 
1514 hidnplayr 1518
 
1519
 
1520
;---------------------------------------------------
1521
;
1522
; SOCKET_check_owner
1523
;
1524
; checks if the caller application owns the socket
1525
;
1526
; IN:  eax = socket ptr
1527
; OUT:  ZF = true/false
1528
;
1529
;---------------------------------------------------
1530
align 4
1531
SOCKET_check_owner:
1532
 
1533
	DEBUGF	1,"socket_check_owner\n"
1534
 
1535
	push	ebx
1536
	mov	ebx, [TASK_BASE]
1537
	mov	ebx, [ecx + TASKDATA.pid]
1538
	cmp	[eax + SOCKET.PID], ebx
1539
	pop	 ebx
1540
 
1159 hidnplayr 1541
	ret
1514 hidnplayr 1542
 
1543
 
1544
 
1545
 
1546
;---------------------------------------------------
1547
;
1548
; SOCKET_process_end
1549
;
1550
; Kernel calls this function when a certain process ends
1551
; This function will check if the process had any open sockets
1552
; And update them accordingly
1553
;
1554
; IN:  eax = pid
1555
; OUT: /
1556
;
1557
;------------------------------------------------------
1558
align 4
1559
SOCKET_process_end:
1560
 
1561
	DEBUGF	1,"socket_process_end: %x\n", eax
1562
 
1563
	push	ebx
1564
	mov	ebx, net_sockets
1565
 
1566
  .next_socket:
1567
 
1568
	mov	ebx, [ebx + SOCKET.NextPtr]
1569
  .test_socket:
1570
	test	ebx, ebx
1571
	jz	.done
1572
 
1573
	cmp	[ebx + SOCKET.PID], eax
1574
	jne	.next_socket
1575
 
1576
	DEBUGF	1,"closing socket %x", eax, ebx
1577
 
1578
	mov	[ebx + SOCKET.PID], 0
1579
 
1580
	cmp	[ebx + SOCKET.Type], IP_PROTO_UDP
1581
	je	.udp
1582
 
1583
	cmp	[ebx + SOCKET.Type], IP_PROTO_TCP
1584
	je	.tcp
1585
 
1586
	jmp	.next_socket	; kill all sockets for given PID
1587
 
1588
  .udp:
1589
	mov	eax, ebx
1590
	mov	ebx, [ebx + SOCKET.NextPtr]
1591
	call	SOCKET_free
1592
	jmp	.test_socket
1593
 
1594
  .tcp:
1595
 
1596
	jmp	.next_socket
1597
 
1598
  .done:
1599
	pop	ebx
1600
 
1601
	ret