Subversion Repositories Kolibri OS

Rev

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

Rev 1519 Rev 1529
Line 3... Line 3...
3
;; Copyright (C) KolibriOS team 2004-2010. 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,                          ;;
-
 
9
;;     and Clevermouse.                                            ;;
-
 
10
;;                                                                 ;;
9
;;     based on code by mike.dld                                   ;;
11
;;       Based on code by mike.dld                                 ;;
10
;;                                                                 ;;
12
;;                                                                 ;;
11
;;          GNU GENERAL PUBLIC LICENSE                             ;;
13
;;         GNU GENERAL PUBLIC LICENSE                              ;;
12
;;             Version 2, June 1991                                ;;
14
;;          Version 2, June 1991                                   ;;
13
;;                                                                 ;;
15
;;                                                                 ;;
14
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 15... Line 17...
15
 
17
 
Line 16... Line 18...
16
$Revision: 1519 $
18
$Revision: 1529 $
Line 17... Line 19...
17
 
19
 
18
virtual at 0
20
virtual at 0
Line 29... Line 31...
29
	.Type			dd ? ; RAW/UDP/TCP/...
31
	.Type			dd ? ; RAW/UDP/TCP/...
30
	.Protocol		dd ? ; ICMP/IPv4/ARP/
32
	.Protocol		dd ? ; ICMP/IPv4/ARP/
31
	.errorcode		dd ?
33
	.errorcode		dd ?
Line 32... Line 34...
32
 
34
 
33
	.options		dd ?
-
 
34
	.SO_SND.SB_CC		dd ?  ;;;;; socket options: number of bytes in socket
-
 
35
	.SO_RCV.SB_CC		dd ?
35
	.options		dd ?
Line 36... Line 36...
36
	.state			dd ?  ;;;;;;;;;
36
	.state			dd ?
37
 
37
 
Line 38... Line 38...
38
	.end:
38
	.end:
Line 49... Line 49...
49
				rd 3 ; for IPv6 addresses
49
				rd 3 ; for IPv6 addresses
Line 50... Line 50...
50
 
50
 
51
	.end:
51
	.end:
Line 52... Line -...
52
end virtual
-
 
53
 
-
 
54
virtual at SOCKET.end
-
 
55
 
-
 
56
	SOCKET_virtual:
-
 
57
 
-
 
58
	.ConnectedTo		dd ? ; Socket number of other socket this one is connected to
-
 
59
 
-
 
60
	.end:
-
 
61
end virtual
52
end virtual
Line 62... Line 53...
62
 
53
 
Line 63... Line 54...
63
virtual at IP_SOCKET.end
54
virtual at IP_SOCKET.end
Line 112... Line 103...
112
	.SND_CWND		dd ?
103
	.SND_CWND		dd ?
113
	.SND_SSTHRESH		dd ?
104
	.SND_SSTHRESH		dd ?
Line 114... Line 105...
114
 
105
 
115
;----------------------
106
;----------------------
116
; Transmit timing stuff
-
 
117
 
107
; Transmit timing stuff
118
	.t_idle 		dd ?
108
	.t_idle 		dd ?
119
	.t_rtt			dd ?
109
	.t_rtt			dd ?
120
	.t_rtseq		dd ?
110
	.t_rtseq		dd ?
121
	.t_srtt 		dd ?
111
	.t_srtt 		dd ?
122
	.t_rttvar		dd ?
112
	.t_rttvar		dd ?
123
	.t_rttmin		dd ?
113
	.t_rttmin		dd ?
Line 124... Line 114...
124
	.max_sndwnd		dd ?
114
	.max_sndwnd		dd ?
125
 
115
 
126
;-----------------
-
 
127
; Out-of-band data
116
;-----------------
128
 
117
; Out-of-band data
129
	.t_oobflags		dd ?
118
	.t_oobflags		dd ?
Line 130... Line 119...
130
	.t_iobc 		dd ?
119
	.t_iobc 		dd ?
131
	.t_softerror		dd ?
120
	.t_softerror		dd ?
132
 
-
 
133
 
121
 
134
;---------
122
 
135
; RFC 1323
123
;---------
136
 
124
; RFC 1323
Line 144... Line 132...
144
	.last_ack_sent		dd ?
132
	.last_ack_sent		dd ?
Line 145... Line 133...
145
 
133
 
146
 
134
 
147
;-------
-
 
148
; Timers
135
;-------
149
 
136
; Timers
150
	.timer_retransmission	dw ?	; rexmt
137
	.timer_retransmission	dw ? ; rexmt
151
	.timer_ack		dw ?
138
	.timer_ack		dw ?
152
	.timer_persist		dw ?
139
	.timer_persist		dw ?
Line 174... Line 161...
174
	.Identifier		dw ? ;
161
	.Identifier		dw ? ;
Line 175... Line 162...
175
 
162
 
176
	.end:
163
	.end:
Line -... Line 164...
-
 
164
end virtual
-
 
165
 
-
 
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
-
 
172
}
-
 
173
 
-
 
174
virtual at 0
-
 
175
 
-
 
176
	RING_BUFFER	RING_BUFFER
-
 
177
 
-
 
178
end virtual
-
 
179
 
-
 
180
virtual at TCP_SOCKET.end
-
 
181
 
-
 
182
	rcv	RING_BUFFER
-
 
183
	snd	RING_BUFFER
-
 
184
 
-
 
185
 STREAM_SOCKET:
-
 
186
	.end:
-
 
187
 
Line 177... Line 188...
177
end virtual
188
end virtual
178
 
-
 
179
 
189
 
180
struct	socket_queue_entry
190
 
181
;        .owner          dd ?
191
struct	socket_queue_entry
182
	.data_ptr	dd ?
192
	.data_ptr	dd ?
183
	.buf_ptr	dd ?
193
	.buf_ptr	dd ?
Line 184... Line -...
184
	.data_size	dd ?
-
 
185
	.size:
194
	.data_size	dd ?
Line 186... Line 195...
186
ends
195
	.size:
187
 
196
ends
188
 
197
 
Line 202... Line 211...
202
 
211
 
203
;-----------------------------------------------------------------
212
;-----------------------------------------------------------------
204
;
213
;
205
; SOCKET_init
214
; SOCKET_init
206
;
-
 
207
;  -
-
 
208
;
-
 
209
;  IN:  /
-
 
210
;  OUT: /
-
 
211
;
215
;
212
;-----------------------------------------------------------------
-
 
213
align 4
216
;-----------------------------------------------------------------
Line 214... Line 217...
214
socket_init:
217
macro	SOCKET_init {
215
 
218
 
216
	xor	eax, eax
219
	xor	eax, eax
217
	mov	edi, net_sockets
220
	mov	edi, net_sockets
Line 218... Line -...
218
	mov	ecx, 4
-
 
219
	rep	stosd
221
	mov	ecx, 4
Line -... Line 222...
-
 
222
	rep	stosd
-
 
223
 
-
 
224
;--- for random port --
-
 
225
 
-
 
226
	mov	al, 0x0 		; set up 1s timer
-
 
227
	out	0x70, al
-
 
228
	in	al, 0x71
-
 
229
 
-
 
230
;----------------------
-
 
231
 
-
 
232
       @@:
220
 
233
	pseudo_random eax
-
 
234
	cmp	ax, MIN_EPHEMERAL_PORT
-
 
235
	jl	@r
-
 
236
	cmp	ax, MAX_EPHEMERAL_PORT
-
 
237
	jg	@r
-
 
238
 
-
 
239
	mov	[last_UDP_port], ax
-
 
240
 
-
 
241
       @@:
-
 
242
	pseudo_random eax
-
 
243
	cmp	ax, MIN_EPHEMERAL_PORT
-
 
244
	jl	@r
-
 
245
	cmp	ax, MAX_EPHEMERAL_PORT
-
 
246
	jg	@r
Line 221... Line 247...
221
	mov	[last_UDP_port], MIN_EPHEMERAL_PORT
247
 
222
	mov	[last_TCP_port], MIN_EPHEMERAL_PORT
248
	mov	[last_TCP_port], ax
223
 
249
 
224
	ret
250
}
225
 
251
 
226
 
252
 
227
;-----------------------------------------------------------------
253
;-----------------------------------------------------------------
228
;
254
;
229
; Socket API (function 74)
255
; Socket API (function 74)
230
;
256
;
231
;-----------------------------------------------------------------
257
;-----------------------------------------------------------------
-
 
258
align 4
-
 
259
sys_socket:
-
 
260
	cmp	ebx, 8		; highest possible number
Line 232... Line 261...
232
align 4
261
	jg	@f
-
 
262
	lea	ebx, [sock_sysfn_table + 4*ebx]
-
 
263
	jmp	dword [ebx]
-
 
264
       @@:
-
 
265
	cmp	ebx, 255
-
 
266
	jz	SOCKET_debug
-
 
267
 
233
sys_socket:
268
s_error:
234
	cmp	ebx, 8		; highest possible number
269
	DEBUGF	1,"socket error\n"
235
	jg	s_error
270
	mov	dword [esp+32], -1
236
	lea	ebx, [.table + 4*ebx]
271
 
237
	jmp	dword [ebx]
272
	ret
Line 247... Line 282...
247
	dd	SOCKET_receive	; 7
282
	dd	SOCKET_receive	; 7
248
	dd	SOCKET_get_opt	; 8
283
	dd	SOCKET_get_opt	; 8
249
;        dd      SOCKET_set_opt  ; 9
284
;        dd      SOCKET_set_opt  ; 9
Line 250... Line -...
250
 
-
 
251
 
-
 
252
s_error:
-
 
253
	DEBUGF	1,"socket error\n"
-
 
254
	mov	dword [esp+32], -1
-
 
255
 
-
 
Line 256... Line 285...
256
	ret
285
 
257
 
286
 
258
 
287
 
259
;-----------------------------------------------------------------
288
;-----------------------------------------------------------------
Line 278... Line 307...
278
	mov	[eax + SOCKET.Type], edx
307
	mov	[eax + SOCKET.Type], edx
279
	mov	[eax + SOCKET.Protocol], esi
308
	mov	[eax + SOCKET.Protocol], esi
Line 280... Line 309...
280
 
309
 
Line -... Line 310...
-
 
310
	mov	[esp+32], edi
-
 
311
 
-
 
312
	cmp	ecx, AF_INET4
-
 
313
	jnz	.no_stream
-
 
314
 
-
 
315
	push	[IP_LIST]		  ;;;;
-
 
316
	pop	[eax + IP_SOCKET.LocalIP] ;;;;
-
 
317
 
-
 
318
	cmp	edx, IP_PROTO_TCP
-
 
319
	jnz	.no_stream
-
 
320
 
-
 
321
	mov	esi, eax
-
 
322
	stdcall kernel_alloc, SOCKET_MAXDATA
-
 
323
	mov	[esi + rcv.start_ptr], eax
-
 
324
	mov	[esi + rcv.write_ptr], eax
-
 
325
	mov	[esi + rcv.read_ptr], eax
-
 
326
	mov	[esi + rcv.size], 0
-
 
327
	add	eax,  SOCKET_MAXDATA
-
 
328
	mov	[esi + rcv.end_ptr], eax
-
 
329
 
-
 
330
	stdcall kernel_alloc, SOCKET_MAXDATA
-
 
331
	mov	[esi + snd.start_ptr], eax
-
 
332
	mov	[esi + snd.write_ptr], eax
-
 
333
	mov	[esi + snd.read_ptr], eax
-
 
334
	mov	[esi + snd.size], 0
-
 
335
	add	eax,  SOCKET_MAXDATA
-
 
336
	mov	[esi + snd.end_ptr], eax
-
 
337
 
-
 
338
	ret
-
 
339
 
-
 
340
  .no_stream:
-
 
341
 
-
 
342
	push	edi
-
 
343
	init_queue (eax + SOCKET_QUEUE_LOCATION)
281
	mov	[esp+32], edi
344
	pop	edi
Line 282... Line 345...
282
 
345
 
Line 345... Line 408...
345
 
408
 
346
    .got_port:
409
    .got_port:
347
	DEBUGF	1,"using local port: %u\n", bx
410
	DEBUGF	1,"using local port: %u\n", bx
Line 348... Line -...
348
	mov	word [eax + UDP_SOCKET.LocalPort], bx
-
 
349
 
-
 
350
	mov	ebx, dword [edx + 4]
-
 
351
	mov	dword [eax + IP_SOCKET.LocalIP], ebx
411
	mov	word [eax + UDP_SOCKET.LocalPort], bx
352
 
412
 
353
	DEBUGF	1,"local ip: %u.%u.%u.%u\n",\
413
	DEBUGF	1,"local ip: %u.%u.%u.%u\n",\
Line 354... Line 414...
354
	[eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\
414
	[eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\
Line 409... Line 469...
409
	mov	dword [esp+32], 0
469
	mov	dword [esp+32], 0
410
	ret
470
	ret
Line 411... Line 471...
411
 
471
 
412
 
-
 
413
  .tcp:
-
 
414
	; set sequence number
-
 
415
 
-
 
416
	mov	ebx, [TCP_sequence_num]
-
 
417
	add	[TCP_sequence_num], 6400
-
 
418
	mov	[eax + TCP_SOCKET.ISS], ebx
-
 
419
 
-
 
420
	mov	[eax + TCP_SOCKET.timer_keepalive], 120 	; 120*630ms => 75,6 seconds
472
 
421
 
473
  .tcp:
Line 422... Line 474...
422
	lea	ebx, [eax + SOCKET.lock]
474
	lea	ebx, [eax + SOCKET.lock]
Line 423... Line -...
423
	call	wait_mutex
-
 
424
 
-
 
425
	; fill in remote port and IP
475
	call	wait_mutex
426
 
476
 
427
;;;;;;        mov     [eax + TCP_SOCKET.wndsizeTimer], 0     ; Reset the window timer.
477
	; fill in remote port and IP
Line 428... Line 478...
428
 
478
 
429
	mov	bx , word [edx + 2]
479
	mov	bx , word [edx + 2]
Line 430... Line 480...
430
	mov	[eax + TCP_SOCKET.RemotePort], bx
480
	mov	[eax + TCP_SOCKET.RemotePort], bx
Line 431... Line -...
431
	DEBUGF	1,"remote port: %u\n", bx
-
 
432
 
-
 
433
	mov	ebx, dword [edx + 4]
-
 
434
	mov	[eax + IP_SOCKET.RemoteIP], ebx
-
 
435
 
-
 
436
	; check if local port and IP is ok
-
 
437
 
481
	DEBUGF	1,"remote port: %u\n", bx
438
	cmp	[eax + IP_SOCKET.LocalIP], 0
482
 
439
	jne	@f
483
	mov	ebx, dword [edx + 4]
440
	push	[IP_LIST]	    ;;;;; device zero = default
484
	mov	[eax + IP_SOCKET.RemoteIP], ebx
-
 
485
 
-
 
486
	; check if local port and IP is ok
-
 
487
 
-
 
488
	cmp	[eax + TCP_SOCKET.LocalPort], 0
-
 
489
	jne	@f
-
 
490
	call	SOCKET_find_port
-
 
491
       @@:
-
 
492
	DEBUGF	1,"local port: %u\n", [eax + TCP_SOCKET.LocalPort]:2
-
 
493
 
Line 441... Line 494...
441
	pop	[eax + IP_SOCKET.LocalIP]
494
;;;;;
Line 442... Line 495...
442
       @@:
495
	mov	[eax + TCP_SOCKET.timer_persist], 0
443
 
496
	mov	[eax + TCP_SOCKET.t_state], TCB_SYN_SENT
-
 
497
	mov	ebx, [TCP_sequence_num]
Line 444... Line 498...
444
	cmp	[eax + TCP_SOCKET.LocalPort], 0
498
	add	[TCP_sequence_num], 6400
Line 445... Line 499...
445
	jne	@f
499
	mov	[eax + TCP_SOCKET.ISS], ebx
446
	call	SOCKET_find_port
500
	mov	[eax + TCP_SOCKET.timer_keepalive], 120 	; 120*640ms => 75,6 seconds
Line 581... Line 635...
581
 
635
 
582
  .tcp:
636
  .tcp:
583
	test	[eax + TCP_SOCKET.t_state], TCB_SYN_RECEIVED  ;;;;;;
637
	test	[eax + TCP_SOCKET.t_state], TCB_SYN_RECEIVED  ;;;;;;
Line 584... Line 638...
584
	jz	.free
638
	jz	.free
Line 585... Line 639...
585
 
639
 
Line 586... Line 640...
586
	call	TCP_output
640
  ;;;      call    TCP_output
Line 587... Line 641...
587
 
641
 
588
	mov	dword [esp+32], 0
642
  ;;;      mov     dword [esp+32], 0
589
 
643
 
590
	ret
644
  ;;;      ret
Line 686... Line 740...
686
	jmp	s_error
740
	jmp	s_error
Line 687... Line 741...
687
 
741
 
688
  .af_inet4:
742
  .af_inet4:
Line -... Line 743...
-
 
743
	DEBUGF	1,"af_inet4\n"
-
 
744
 
-
 
745
	cmp	[eax + IP_SOCKET.LocalIP], 0
-
 
746
	jne	@f
-
 
747
	mov	ebx, [IP_LIST]				;;;;
-
 
748
	mov	dword [eax + IP_SOCKET.LocalIP], ebx
689
	DEBUGF	1,"af_inet4\n"
749
       @@:
690
 
750
 
Line 691... Line 751...
691
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
751
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
692
	je	.tcp
752
	je	.tcp
Line 788... Line 848...
788
@@:
848
@@:
789
	mov	dword [esp+32], 0
849
	mov	dword [esp+32], 0
790
	ret
850
	ret
Line -... Line 851...
-
 
851
 
-
 
852
 
-
 
853
 
-
 
854
;-----------------------------------------------------------------
-
 
855
;
-
 
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
791
 
880
 
792
 
881
 
793
;-----------------------------------------------------------------
882
;-----------------------------------------------------------------
794
;
883
;
795
; SOCKET_find_port
884
; SOCKET_find_port
Line 939... Line 1028...
939
 
1028
 
Line 940... Line 1029...
940
	ret
1029
	ret
941
 
1030
 
-
 
1031
;-----------------------------------------------------------------
-
 
1032
;
-
 
1033
; SOCKET_ring_add
-
 
1034
;
-
 
1035
; Adds data to a stream socket
-
 
1036
;
-
 
1037
;  IN:  eax = ptr to ring struct
-
 
1038
;       ecx = data size
-
 
1039
;       esi = ptr to data
-
 
1040
;
-
 
1041
;  OUT: eax = number of bytes stored
-
 
1042
;
-
 
1043
;-----------------------------------------------------------------
-
 
1044
align 4
-
 
1045
SOCKET_ring_add:
-
 
1046
 
-
 
1047
	DEBUGF	1,"SOCKET_ring_add: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
-
 
1048
 
-
 
1049
	mov	edi, [eax + RING_BUFFER.size]
-
 
1050
	add	edi, ecx
-
 
1051
	cmp	edi, SOCKET_MAXDATA
-
 
1052
	jg	.too_large
-
 
1053
 
-
 
1054
	mov	[eax + RING_BUFFER.size], edi	     ; update size
-
 
1055
  .copy:
-
 
1056
	push	ecx	;<<<< 1
-
 
1057
	mov	edi, [eax + RING_BUFFER.write_ptr]   ; set write ptr in edi
-
 
1058
	add	[eax + RING_BUFFER.write_ptr], ecx   ; update write pointer
-
 
1059
	mov	edx, [eax + RING_BUFFER.end_ptr]
-
 
1060
	cmp	edx, [eax + RING_BUFFER.write_ptr]
-
 
1061
	jg	.copy_in_2
-
 
1062
	je	.wrap_write_ptr
-
 
1063
 
-
 
1064
  .copy_more:
-
 
1065
	push	ecx
-
 
1066
	and	ecx, 3
-
 
1067
	rep	movsb
-
 
1068
	pop	ecx
-
 
1069
	shr	ecx, 2
-
 
1070
	rep	movsd
-
 
1071
	pop	ecx	; >>>> 1/2
-
 
1072
	DEBUGF	2,"Copied %u bytes\n", ecx
-
 
1073
 
-
 
1074
	ret
-
 
1075
 
-
 
1076
  .too_large:
-
 
1077
	mov	ecx, SOCKET_MAXDATA				; calculate number of bytes available in buffer
-
 
1078
	sub	ecx, [eax + RING_BUFFER.size]
-
 
1079
	jz	.full
-
 
1080
 
-
 
1081
	mov	[eax + RING_BUFFER.size], SOCKET_MAXDATA	; update size, we will fill buffer completely
-
 
1082
 
-
 
1083
	jmp	.copy
-
 
1084
 
-
 
1085
  .full:
-
 
1086
	DEBUGF	2,"Ring buffer is full!\n"
-
 
1087
	xor	ecx, ecx
-
 
1088
	ret
-
 
1089
 
-
 
1090
  .copy_in_2:
-
 
1091
	DEBUGF	1,"Copying in 2 passes\n"
-
 
1092
 
-
 
1093
	mov	edx, ecx
-
 
1094
	mov	ecx, [eax + RING_BUFFER.end_ptr]    ; find number of bytes till end of buffer
-
 
1095
	sub	ecx, edi
-
 
1096
	sub	edx, ecx
-
 
1097
	push	edx	; <<<< 2
-
 
1098
 
-
 
1099
	mov	edi, [eax + RING_BUFFER.start_ptr]
-
 
1100
	call	.copy_more
-
 
1101
 
-
 
1102
  .wrap_write_ptr:
-
 
1103
	sub	[eax + RING_BUFFER.write_ptr], SOCKET_MAXDATA	; update write pointer
-
 
1104
	jmp	.copy_more
-
 
1105
 
-
 
1106
 
-
 
1107
 
-
 
1108
 
-
 
1109
;-----------------------------------------------------------------
-
 
1110
;
-
 
1111
; SOCKET_ring_read
-
 
1112
;
-
 
1113
; reads the data, but let the data remain in the buffer
-
 
1114
;
-
 
1115
;  IN:  eax = ptr to ring struct
-
 
1116
;       ecx = buffer size
-
 
1117
;       edi = ptr to buffer
-
 
1118
;
-
 
1119
;  OUT: eax = number of bytes read
-
 
1120
;
-
 
1121
;-----------------------------------------------------------------
-
 
1122
align 4
-
 
1123
SOCKET_ring_read:
-
 
1124
 
-
 
1125
	DEBUGF	1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
-
 
1126
 
-
 
1127
	cmp	[eax + RING_BUFFER.size], ecx	     ; update size
-
 
1128
	jl	.too_large
-
 
1129
 
-
 
1130
	mov	esi, [eax + RING_BUFFER.read_ptr]    ; update read ptr
-
 
1131
  .copy:
-
 
1132
	push	ecx	;<<<< 1
-
 
1133
	mov	edx, [eax + RING_BUFFER.read_ptr]
-
 
1134
	add	edx, ecx
-
 
1135
	cmp	edx, [eax + RING_BUFFER.end_ptr]
-
 
1136
	jg	.copy_in_2
-
 
1137
 
-
 
1138
  .copy_more:
-
 
1139
	push	ecx
-
 
1140
	and	ecx, 3
-
 
1141
	rep	movsb
-
 
1142
	pop	ecx
-
 
1143
	shr	ecx, 2
-
 
1144
	rep	movsd
-
 
1145
	pop	ecx	; >>>> 1/2
-
 
1146
	DEBUGF	2,"Copied %u bytes\n", ecx
-
 
1147
 
-
 
1148
	ret
-
 
1149
 
-
 
1150
  .too_large:
-
 
1151
	mov	ecx, [eax + RING_BUFFER.size]
-
 
1152
	jmp	.copy
-
 
1153
 
-
 
1154
  .full:
-
 
1155
	DEBUGF	2,"Ring buffer is full!\n"
-
 
1156
	xor	ecx, ecx
-
 
1157
	ret
-
 
1158
 
-
 
1159
  .copy_in_2:
-
 
1160
	DEBUGF	1,"Copying in 2 passes\n"
-
 
1161
 
-
 
1162
	mov	edx, ecx
-
 
1163
	mov	ecx, [eax + RING_BUFFER.end_ptr]    ; find number of bytes till end of buffer
-
 
1164
	sub	ecx, edi
-
 
1165
	sub	edx, ecx
-
 
1166
	push	edx	; <<<< 2
-
 
1167
 
-
 
1168
	mov	esi, [eax + RING_BUFFER.start_ptr]
-
 
1169
	call	.copy_more
-
 
1170
 
-
 
1171
 
-
 
1172
;-----------------------------------------------------------------
-
 
1173
;
-
 
1174
; SOCKET_ring_free
-
 
1175
;
-
 
1176
; Free's some bytes from the ringbuffer
-
 
1177
;
-
 
1178
;  IN:  eax = ptr to ring struct
-
 
1179
;       ecx = data size
-
 
1180
;
-
 
1181
;  OUT: ecx = number of bytes free-ed
-
 
1182
;
-
 
1183
;-----------------------------------------------------------------
-
 
1184
align 4
-
 
1185
SOCKET_ring_free:
-
 
1186
 
-
 
1187
	DEBUGF	1,"Trying to free %u bytes of data from ring %x\n", ecx, eax
-
 
1188
 
-
 
1189
	cmp	ecx, [eax + RING_BUFFER.size]
-
 
1190
	jle	.go_for_it
-
 
1191
 
-
 
1192
	cmp	ecx, SOCKET_MAXDATA				;;;;
-
 
1193
	jg	.moron_input
-
 
1194
 
-
 
1195
	mov	ecx, [eax + RING_BUFFER.size]
-
 
1196
 
-
 
1197
  .go_for_it:
-
 
1198
	sub	[eax + RING_BUFFER.size], ecx
-
 
1199
	add	[eax + RING_BUFFER.read_ptr], ecx
-
 
1200
 
-
 
1201
	mov	edx, [eax + RING_BUFFER.end_ptr]
-
 
1202
	cmp	[eax + RING_BUFFER.read_ptr], edx
-
 
1203
	jl	@f
-
 
1204
	sub	[eax + RING_BUFFER.read_ptr], SOCKET_MAXDATA	;;;;;
-
 
1205
       @@:
-
 
1206
	ret
-
 
1207
 
-
 
1208
  .moron_input:
-
 
1209
	xor	ecx, ecx
-
 
1210
	ret
-
 
1211
 
-
 
1212
 
942
;-----------------------------------------------------------------
1213
;-----------------------------------------------------------------
943
;
1214
;
944
; SOCKET_notify_owner
1215
; SOCKET_notify_owner
945
;
1216
;
946
; notify's the owner of a socket that something happened
1217
; notify's the owner of a socket that something happened
Line 1009... Line 1280...
1009
SOCKET_alloc:
1280
SOCKET_alloc:
Line 1010... Line 1281...
1010
 
1281
 
Line 1011... Line 1282...
1011
	push	ecx ebx
1282
	push	ecx ebx
1012
 
1283
 
1013
	stdcall kernel_alloc, SOCKETBUFFSIZE
1284
	stdcall kernel_alloc, SOCKETBUFFSIZE
1014
	DEBUGF	1, "socket_alloc: %x ", eax
1285
	DEBUGF	1, "SOCKET_alloc: ptr=%x\n", eax
Line 1015... Line 1286...
1015
	or	eax, eax
1286
	or	eax, eax
1016
	jz	.exit
1287
	jz	.exit
Line 1021... Line 1292...
1021
	mov	ecx, SOCKETBUFFSIZE / 4
1292
	mov	ecx, SOCKETBUFFSIZE / 4
1022
	xor	eax, eax
1293
	xor	eax, eax
1023
	rep	stosd
1294
	rep	stosd
1024
	pop	edi eax
1295
	pop	edi eax
Line 1025... Line -...
1025
 
-
 
1026
	init_queue (eax + SOCKET_QUEUE_LOCATION)
-
 
1027
 
1296
 
1028
; find first free socket number and use it
-
 
1029
 
1297
; find first free socket number and use it
1030
	mov	ebx, net_sockets
1298
	mov	ebx, net_sockets
1031
	xor	ecx, ecx
1299
	xor	ecx, ecx
1032
  .next_socket_number:
1300
  .next_socket_number:
1033
	inc	ecx
1301
	inc	ecx
1034
  .next_socket:
1302
  .next_socket:
1035
	mov	ebx, [ebx + SOCKET.NextPtr]
1303
	mov	ebx, [ebx + SOCKET.NextPtr]
1036
	or	ebx, ebx
1304
	test	ebx, ebx
1037
	jz	.last_socket
1305
	jz	.last_socket
1038
	cmp	[ebx + SOCKET.Number], ecx
1306
	cmp	[ebx + SOCKET.Number], ecx
1039
	jne	.next_socket
1307
	jne	.next_socket
1040
	mov	ebx, net_sockets
1308
	mov	ebx, net_sockets
Line 1041... Line 1309...
1041
	jmp	.next_socket_number
1309
	jmp	.next_socket_number
1042
 
1310
 
1043
  .last_socket:
-
 
1044
	mov	[eax + SOCKET.Number], ecx
1311
  .last_socket:
-
 
1312
	mov	[eax + SOCKET.Number], ecx
Line 1045... Line 1313...
1045
 
1313
	DEBUGF	1, "SOCKET_alloc: number=%u\n", ecx
1046
	DEBUGF	1, "(number: %u)\n", ecx
1314
	mov	edi, ecx
1047
 
1315
 
1048
; Fill in PID
-
 
1049
	mov	ebx, [TASK_BASE]
1316
; Fill in PID
Line 1050... Line 1317...
1050
	mov	ebx, [ebx + TASKDATA.pid]
1317
	mov	ebx, [TASK_BASE]
1051
 
-
 
1052
	mov	[eax + SOCKET.PID], ebx
1318
	mov	ebx, [ebx + TASKDATA.pid]
Line 1053... Line 1319...
1053
 
1319
	mov	[eax + SOCKET.PID], ebx
1054
; add socket to the list by changing pointers
1320
 
Line 1055... Line 1321...
1055
 
1321
; add socket to the list by re-arranging some pointers
1056
	mov	ebx, [net_sockets + SOCKET.NextPtr]
1322
	mov	ebx, [net_sockets + SOCKET.NextPtr]
1057
 
1323
 
1058
	mov	[eax + SOCKET.PrevPtr], net_sockets
1324
	mov	[eax + SOCKET.PrevPtr], net_sockets
1059
	mov	[eax + SOCKET.NextPtr], ebx
1325
	mov	[eax + SOCKET.NextPtr], ebx
1060
 
1326
 
1061
	or	ebx, ebx
1327
	test	ebx, ebx
1062
	jz	@f
1328
	jz	@f
Line 1063... Line 1329...
1063
	add	ebx, SOCKET.lock	; lock the next socket
1329
	add	ebx, SOCKET.lock	; lock the next socket
1064
	call	wait_mutex
-
 
1065
	sub	ebx, SOCKET.lock
-
 
1066
	mov	[ebx + SOCKET.PrevPtr], eax
1330
	call	wait_mutex
1067
	mov	[ebx + SOCKET.lock], 0
1331
	sub	ebx, SOCKET.lock
1068
       @@:
1332
	mov	[ebx + SOCKET.PrevPtr], eax
Line 1069... Line 1333...
1069
 
1333
	mov	[ebx + SOCKET.lock], 0
Line 1099... Line 1363...
1099
	lea	ebx, [eax + SOCKET.lock]
1363
	lea	ebx, [eax + SOCKET.lock]
1100
	call	wait_mutex
1364
	call	wait_mutex
Line 1101... Line 1365...
1101
 
1365
 
Line -... Line 1366...
-
 
1366
	DEBUGF	1, "freeing socket..\n"
-
 
1367
 
-
 
1368
	cmp	[eax + SOCKET.Domain], AF_INET4
-
 
1369
	jnz	.no_stream
-
 
1370
 
-
 
1371
	cmp	[eax + SOCKET.Type], IP_PROTO_TCP
-
 
1372
	jnz	.no_stream
-
 
1373
 
-
 
1374
	stdcall kernel_free, [eax + rcv.start_ptr]
-
 
1375
	stdcall kernel_free, [eax + snd.start_ptr]
1102
	DEBUGF	1, "freeing socket..\n"
1376
  .no_stream:
1103
 
1377
 
1104
	push	eax				; this will be passed to kernel_free
1378
	push	eax				; this will be passed to kernel_free
Line 1105... Line 1379...
1105
	mov	ebx, [eax + SOCKET.NextPtr]
1379
	mov	ebx, [eax + SOCKET.NextPtr]
Line 1124... Line 1398...
1124
 
1398
 
1125
  .error:
1399
  .error:
Line -... Line 1400...
-
 
1400
	ret
-
 
1401
 
-
 
1402
 
-
 
1403
; socket nr in ebx
-
 
1404
; new socket nr in eax
-
 
1405
; preserver edx
-
 
1406
 
-
 
1407
align 4
-
 
1408
SOCKET_fork:
-
 
1409
 
-
 
1410
;; Exit if backlog queue is full
-
 
1411
;        mov     ax, [ebx + TCP_SOCKET.backlog_cur]
-
 
1412
;        cmp     ax, [ebx + TCP_SOCKET.backlog]
-
 
1413
;        jae     .exit
-
 
1414
 
-
 
1415
; Allocate new socket
-
 
1416
	call	SOCKET_alloc
-
 
1417
     ;;;   jz      .fail
-
 
1418
 
-
 
1419
; Copy structure from current socket to new, (including lock!)
-
 
1420
; We start at PID to reserve the socket num, and the 2 pointers at beginning of socket
-
 
1421
	lea	esi, [edx + SOCKET.PID]
-
 
1422
	lea	edi, [eax + SOCKET.PID]
-
 
1423
	mov	ecx, (TCP_SOCKET.end - SOCKET.PID + 3)/4
-
 
1424
	rep	movsd
-
 
1425
 
-
 
1426
;; Push pointer to new socket to queue
-
 
1427
;        movzx   ecx, [ebx + TCP_SOCKET.backlog_cur]
-
 
1428
;        inc     [ebx + TCP_SOCKET.backlog_cur]
-
 
1429
;        mov     [ebx + TCP_SOCKET.end + ecx*4], eax
-
 
1430
 
-
 
1431
;;;;        mov     [eax + IP_SOCKET.RemoteIP], esi ; IP source address
-
 
1432
 
1126
	ret
1433
	ret
1127
 
1434
 
1128
 
1435
 
1129
;---------------------------------------------------
1436
;---------------------------------------------------
1130
;
1437
;