Subversion Repositories Kolibri OS

Rev

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

Rev 1249 Rev 1254
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 16... Line 16...
16
$Revision: 1249 $
16
$Revision: 1254 $
17
 
17
 
18
struct	SOCKET_head
18
struct	SOCKET_head
19
	 .PrevPtr		dd ? ; pointer to previous socket in list
19
	 .PrevPtr		dd ? ; pointer to previous socket in list
Line 41... Line 41...
41
 
41
 
42
	 .LocalPort		dw ? ; In INET byte order
42
	 .LocalPort		dw ? ; In INET byte order
Line 43... Line 43...
43
	 .RemotePort		dw ? ; In INET byte order
43
	 .RemotePort		dw ? ; In INET byte order
44
 
44
 
45
	 .backlog		dw ? ; Backlog
45
	 .backlog		dw ? ; Backlog
-
 
46
;         .OrigRemoteIP          dd ? ; original remote IP address (used to reset to LISTEN state)
-
 
47
;         .OrigRemotePort        dw ? ; original remote port (used to reset to LISTEN state)
-
 
48
	 .wndsizeTimer		dd ? ; window size timer
46
	 .OrigRemoteIP		dd ? ; original remote IP address (used to reset to LISTEN state)
49
 
47
	 .OrigRemotePort	dw ? ; original remote port (used to reset to LISTEN state)
50
	; Transmission control block
48
	 .TCBState		dd ? ; TCB state
51
	 .state 		dd ? ; TCB state
49
	 .TCBTimer		dd ? ; TCB timer (seconds)
52
	 .timer 		dd ? ; TCB timer (seconds)
50
	 .ISS			dd ? ; initial send sequence
53
	 .ISS			dd ? ; initial send sequence number
51
	 .IRS			dd ? ; initial receive sequence
54
	 .IRS			dd ? ; initial receive sequence number
52
	 .SND_UNA		dd ? ; sequence number of unack'ed sent Packets
55
	 .SND_UNA		dd ? ; sequence number of unack'ed sent Packets
53
	 .SND_NXT		dd ? ; next send sequence number to use
56
	 .SND_NXT		dd ? ; next send sequence number to use
54
	 .SND_WND		dd ? ; send window
57
	 .SND_WND		dd ? ; send window
55
	 .RCV_NXT		dd ? ; next receive sequence number to use
58
	 .RCV_NXT		dd ? ; next receive sequence number to use
56
	 .RCV_WND		dd ? ; receive window
59
	 .RCV_WND		dd ? ; receive window
57
	 .SEG_LEN		dd ? ; segment length
-
 
Line 58... Line 60...
58
	 .SEG_WND		dd ? ; segment window
60
	 .SEG_LEN		dd ? ; segment length
Line 59... Line 61...
59
	 .wndsizeTimer		dd ? ; window size timer
61
	 .SEG_WND		dd ? ; segment window
60
 
62
 
Line 119... Line 121...
119
	mov	[last_TCP_port], MIN_EPHEMERAL_PORT
121
	mov	[last_TCP_port], MIN_EPHEMERAL_PORT
Line 120... Line 122...
120
 
122
 
Line 121... Line 123...
121
	ret
123
	ret
122
 
124
 
123
 
125
 
124
;-----------------------------------------------------------------------------
126
;-----------------------------------------------
125
;
127
;
Line 126... Line 128...
126
; Socket API (function 74)
128
; Socket API (function 74)
127
;
129
;
-
 
130
;-----------------------------------------------
-
 
131
 
-
 
132
align 4
-
 
133
sys_socket:
-
 
134
	and	ebx, 0x000000FF ; should i remove this line ?
-
 
135
	cmp	bl , 7		; highest possible number
-
 
136
	jg	s_error
-
 
137
	lea	ebx, [.table + 4*ebx]
-
 
138
	jmp	dword [ebx]
-
 
139
 
-
 
140
.table:
-
 
141
	dd	socket_open	; 0
-
 
142
	dd	socket_close	; 1
-
 
143
	dd	socket_bind	; 2
-
 
144
	dd	socket_listen	; 3
-
 
145
	dd	socket_connect	; 4
-
 
146
	dd	socket_accept	; 5
Line 128... Line -...
128
;-----------------------------------------------------------------------------
-
 
129
 
-
 
130
align 4
-
 
131
sys_socket:
-
 
132
 
-
 
133
	test	bl, bl
-
 
134
	jz	socket_open	; 0
-
 
135
	dec	bl
-
 
136
	jz	socket_close	; 1
-
 
137
	dec	bl
-
 
138
	jz	socket_bind	; 2
-
 
139
	dec	bl
-
 
140
	jz	socket_listen	; 3
-
 
141
	dec	bl
-
 
142
	jz	socket_connect	; 4
-
 
143
	dec	bl
-
 
144
	jz	socket_accept	; 5
-
 
145
	dec	bl
-
 
146
	jz	socket_send	; 6
-
 
147
	dec	bl
-
 
Line 148... Line 147...
148
	jz	socket_recv	; 7
147
	dd	socket_send	; 6
149
	dec	bl
148
	dd	socket_recv	; 7
Line 150... Line 149...
150
;        jz      socket_get_opt  ; 8
149
;        dd      socket_get_opt  ; 8
Line 151... Line -...
151
	dec	bl
-
 
152
;        jz      socket_set_opt  ; 9
-
 
153
 
150
;        dd      socket_set_opt  ; 9
154
s_error:
151
 
155
	mov	dword [esp+32],-1
152
 
156
 
153
s_error:
157
	ret
154
	mov	dword [esp+32],-1
Line 184... Line 181...
184
	mov	[eax + SOCKET_head.Protocol], esi
181
	mov	[eax + SOCKET_head.Protocol], esi
Line 185... Line 182...
185
 
182
 
186
	stdcall net_socket_addr_to_num, eax
183
	stdcall net_socket_addr_to_num, eax
Line -... Line 184...
-
 
184
	DEBUGF	1,", socketnumber: %u\n", eax
-
 
185
 
187
	DEBUGF	1,", socketnumber: %u\n", eax
186
	; TODO: if it is txcp socket, set state to TCB_CLOSED
Line 188... Line 187...
188
 
187
 
Line 229... Line 228...
229
 
228
 
Line 230... Line 229...
230
  .af_unix:
229
  .af_unix:
Line 231... Line -...
231
 
-
 
232
	; TODO: write code here
230
 
233
 
231
	; TODO: write code here
Line 234... Line 232...
234
 
232
 
Line 293... Line 291...
293
 
291
 
294
	stdcall net_socket_num_to_addr, ecx
292
	stdcall net_socket_num_to_addr, ecx
295
	cmp	eax, -1
293
	cmp	eax, -1
Line 296... Line 294...
296
	jz	s_error
294
	jz	s_error
297
 
295
 
Line 298... Line 296...
298
	cmp	esi, 2
296
	cmp	esi, 8
299
	jl	s_error
297
	jl	s_error
Line 300... Line 298...
300
 
298
 
Line 301... Line 299...
301
	cmp	word [edx], AF_INET4
299
	cmp	word [edx], AF_INET4
Line 302... Line -...
302
	je	.af_inet4
-
 
303
 
-
 
304
	jmp	s_error
-
 
305
 
300
	je	.af_inet4
306
  .af_inet4:
301
 
Line 307... Line -...
307
 
-
 
308
	cmp	esi, 8
-
 
309
	jl	s_error
-
 
310
 
302
	jmp	s_error
311
	cmp	[eax + SOCKET_head.Type], IP_PROTO_UDP
303
 
Line 312... Line 304...
312
	je	.udp
304
  .af_inet4:
Line 330... Line 322...
330
	DEBUGF	1,"remote ip: %u.%u.%u.%u\n",[edx+4]:1,[edx+5]:1,[edx+6]:1,[edx+7]:1
322
	DEBUGF	1,"remote ip: %u.%u.%u.%u\n",[edx+4]:1,[edx+5]:1,[edx+6]:1,[edx+7]:1
Line 331... Line 323...
331
 
323
 
332
	mov	dword [esp+32],0
324
	mov	dword [esp+32],0
Line 333... Line -...
333
	ret
-
 
Line -... Line 325...
-
 
325
	ret
334
 
326
 
Line 335... Line -...
335
    .icmp:
-
 
Line -... Line 327...
-
 
327
 
Line -... Line 328...
-
 
328
  .tcp:
-
 
329
	; TODO: set sequence number to random value
-
 
330
 
-
 
331
 
-
 
332
	; fill in remote port and IP
Line 336... Line 333...
336
 
333
 
-
 
334
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.wndsizeTimer], 0	   ; Reset the window timer.
Line 337... Line 335...
337
	; TODO: write code here
335
											   ; TODO: figure out WTF this is
Line 338... Line 336...
338
 
336
	mov	bx , word [edx + 2]
339
	ret
-
 
340
 
-
 
341
 
-
 
342
 
-
 
343
  .tcp:
-
 
344
 
-
 
345
;local sockAddr dd ?
-
 
346
 
337
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RemotePort], bx
347
;        cmp     esi, SOCKET_PASSIVE
338
	DEBUGF	1,"remote port: %x ",bx
348
;        jne     .skip_port_check
-
 
349
;
-
 
350
;        push    ebx
-
 
351
;        mov     eax, ebx
-
 
352
;        xchg    al, ah
339
 
353
;        mov     ebx, net_sockets
-
 
354
;
-
 
355
;  .next_socket:
-
 
356
;        mov     ebx, [ebx + SOCKET.NextPtr]
-
 
357
;        or      ebx, ebx
-
 
358
;        jz      .last_socket
-
 
359
;        cmp     [ebx + SOCKET.TCBState], TCB_LISTEN
-
 
360
;        jne     .next_socket
-
 
361
;        cmp     [ebx + SOCKET.LocalPort], ax
340
	mov	ebx, dword [edx + 4]
362
;        jne     .next_socket
-
 
363
;
-
 
364
;        xchg    al, ah
341
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.RemoteIP], ebx
365
;        DEBUGF  1, "K : error: port %u is listened by 0x%x\n", ax, ebx
342
 
366
;        pop     ebx
343
	; check if local port and IP is ok
367
;        jmp     .error
-
 
368
;
-
 
369
;  .last_socket:
-
 
370
;        pop     ebx
-
 
371
;
-
 
372
;  .skip_port_check:
-
 
373
 
-
 
374
;        mov     [eax + SOCKET.wndsizeTimer], 0     ; Reset the window timer.
-
 
375
;
-
 
376
;        xchg    bh, bl
344
 
377
;        mov     [eax + SOCKET.LocalPort], bx
-
 
378
;        xchg    ch, cl
345
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.LocalIP], 0
379
;        mov     [eax + SOCKET.RemotePort], cx
346
	jne	@f
380
;        mov     [eax + SOCKET.OrigRemotePort], cx
-
 
381
;        mov     ebx, [IP_LIST]
-
 
382
;        mov     [eax + SOCKET.LocalIP], ebx
-
 
383
;        mov     [eax + SOCKET.RemoteIP], edx
347
	push	[IP_LIST]	    ; device zero = default
384
;        mov     [eax + SOCKET.OrigRemoteIP], edx
348
	pop	[eax + SOCKET_head.end + IPv4_SOCKET.LocalIP]
385
 
349
       @@:
386
;        mov     ebx, TCB_LISTEN
350
 
387
;        cmp     esi, SOCKET_PASSIVE
-
 
388
;        je      @f
-
 
389
;        mov     ebx, TCB_SYN_SENT
-
 
390
;    @@: mov     [eax + SOCKET.TCBState], ebx            ; Indicate the state of the TCB
-
 
391
 
-
 
392
;        cmp     ebx, TCB_LISTEN
351
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort], 0
393
;        je      .exit
352
	jne	@f
394
 
-
 
395
	; Now, if we are in active mode, then we have to send a SYN to the specified remote port
-
 
396
;        mov     eax, EMPTY_QUEUE
-
 
397
;        call    dequeue
353
 
398
;        cmp     ax, NO_BUFFER
-
 
399
;        je      .exit
-
 
400
 
-
 
401
;        push    eax
354
	mov	ecx, [eax + SOCKET_head.Type]
402
 
-
 
403
;        mov     bl, TH_SYN
355
	call	socket_find_port
404
;        xor     ecx, ecx
-
 
405
;        stdcall build_tcp_Packet, [sockAddr]
-
 
406
 
-
 
407
;        mov     eax, NET1OUT_QUEUE
-
 
408
;        mov     edx, [IP_LIST]
-
 
Line 409... Line 356...
409
;        mov     ecx, [sockAddr]
356
	test	bx, bx
410
;        cmp     edx, [ecx + SOCKET.RemoteIP]
357
	jz	s_error
-
 
358
 
-
 
359
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort], bx
411
;        jne     .not_local
360
       @@:
Line 440... Line 389...
440
 
389
 
441
	stdcall net_socket_num_to_addr, ecx
390
	stdcall net_socket_num_to_addr, ecx
442
	cmp	eax, -1
391
	cmp	eax, -1
Line -... Line 392...
-
 
392
	jz	s_error
-
 
393
 
-
 
394
	cmp	word [eax + SOCKET_head.Domain], AF_INET4
-
 
395
	jne	s_error
-
 
396
 
-
 
397
	cmp	[eax + SOCKET_head.Type], IP_PROTO_TCP
443
	jz	s_error
398
	jne	s_error
444
 
399
 
445
	cmp	edx, MAX_backlog
400
	cmp	edx, MAX_backlog
446
	jl	.ok
401
	jl	.ok
Line 447... Line 402...
447
	mov	dx , 20
402
	mov	dx , 20
448
  .ok:
-
 
449
 
403
  .ok:
Line 450... Line 404...
450
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog], dx
404
 
451
 
405
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog], dx
Line 567... Line 521...
567
 
521
 
Line 568... Line 522...
568
 
522
 
-
 
523
	ret
Line 569... Line -...
569
	ret
-
 
570
 
-
 
571
  .tcp:
-
 
572
 
-
 
573
if 1 = 0
-
 
574
;local sockAddr dd ?
-
 
575
 
-
 
576
;        DEBUGF  1, "K : socket_close_tcp (0x%x)\n", ebx
-
 
577
	; first, remove any resend entries
-
 
578
	pusha
-
 
579
 
-
 
580
	mov	esi, resendQ
-
 
581
	mov	ecx, 0
-
 
582
 
-
 
583
  .next_resendq:
-
 
584
	cmp	ecx, NUMRESENDENTRIES
-
 
585
	je	.last_resendq	    ; None left
-
 
586
	cmp	[esi + 4], ebx
-
 
587
	je	@f		    ; found one
-
 
588
	inc	ecx
-
 
589
	add	esi, 8
-
 
590
	jmp	.next_resendq
-
 
591
 
-
 
592
    @@: mov	dword[esi + 4], 0
-
 
593
	inc	ecx
-
 
594
	add	esi, 8
-
 
595
	jmp	.next_resendq
-
 
596
 
-
 
597
  .last_resendq:
-
 
598
	popa
-
 
599
 
524
 
600
	mov	ebx, eax
525
  .tcp:
601
;        mov     [sockAddr], eax
526
	; first, remove all resend entries for this socket
602
 
527
 
Line 603... Line 528...
603
	cmp	[eax + SOCKET.TCBState], TCB_LISTEN
528
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_LISTEN
604
	je	.destroy_tcb
-
 
605
	cmp	[eax + SOCKET.TCBState], TCB_SYN_SENT
-
 
606
	je	.destroy_tcb
-
 
607
 
-
 
608
; Now construct the response, and queue for sending by IP
-
 
609
	mov	eax, EMPTY_QUEUE
-
 
Line 610... Line 529...
610
	call	dequeue
529
	je	.destroy_tcb
611
	cmp	ax, NO_BUFFER
530
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_SYN_SENT
612
	je	.error
-
 
613
 
-
 
Line 614... Line -...
614
	push	eax
-
 
615
 
531
	je	.destroy_tcb
616
	mov	bl, TH_FIN
532
 
617
	xor	ecx, ecx
533
	; Now construct the response, and queue for sending by IP
Line 618... Line 534...
618
	xor	esi, esi
534
 
619
	stdcall build_tcp_Packet, [sockAddr]
535
	mov	bl, TH_FIN
620
 
-
 
621
	mov	 ebx, [sockAddr]
536
	call	TCP_send_ack
622
	; increament SND.NXT in socket
537
 
623
	lea	esi, [ebx + SOCKET.SND_NXT]
538
	; increament SND.NXT in socket
Line 624... Line 539...
624
	call	inc_inet_esi
539
	lea	esi, [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT]
625
 
540
	inc_INET esi
626
	; Get the socket state
541
 
627
	mov	eax, [ebx + SOCKET.TCBState]
542
	; Get the socket state
Line 628... Line 543...
628
	cmp	eax, TCB_SYN_RECEIVED
543
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_SYN_RECEIVED
629
	je	.fin_wait_1
544
	je	.fin_wait_1
630
	cmp	eax, TCB_ESTABLISHED
545
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_ESTABLISHED
Line 631... Line 546...
631
	je	.fin_wait_1
546
	je	.fin_wait_1
632
 
-
 
633
	; assume CLOSE WAIT
-
 
634
	; Send a fin, then enter last-ack state
-
 
635
	mov	[ebx + SOCKET.TCBState], TCB_LAST_ACK
-
 
636
	jmp	.send
-
 
637
 
-
 
638
  .fin_wait_1:
-
 
639
	; Send a fin, then enter finwait2 state
-
 
640
	mov	[ebx + SOCKET.TCBState], TCB_FIN_WAIT_1
-
 
641
 
-
 
642
  .send:
-
 
643
	mov	eax, NET1OUT_QUEUE
-
 
Line -... Line 547...
-
 
547
 
Line 644... Line 548...
644
	mov	edx, [IP_LIST]
548
	; assume CLOSE WAIT
Line 645... Line 549...
645
;        mov     ecx, [sockAddr]
549
	; Send a fin, then enter last-ack state
646
	cmp	edx, [ecx + SOCKET.RemoteIP]
-
 
647
	jne	.not_local
-
 
648
	mov	eax, IPIN_QUEUE
-
 
649
 
-
 
650
  .not_local:
550
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_LAST_ACK
651
	; Send it.
551
	jmp	.send
Line 733... Line 633...
733
;       datalength in esi
633
;       datalength in esi
734
;       flags in edi
634
;       flags in edi
735
;  OUT: -1 on error
635
;  OUT: -1 on error
736
;
636
;
737
;-----------------------------------------------
637
;-----------------------------------------------
-
 
638
 
738
align 4
639
align 4
739
socket_send:
640
socket_send:
Line 740... Line 641...
740
 
641
 
Line 747... Line 648...
747
	cmp	word [eax + SOCKET_head.Domain], AF_INET4
648
	cmp	word [eax + SOCKET_head.Domain], AF_INET4
748
	je	.af_inet4
649
	je	.af_inet4
Line 749... Line 650...
749
 
650
 
Line 750... Line -...
750
	jmp	s_error
-
 
751
 
651
	jmp	s_error
752
;---------
652
 
Line 753... Line 653...
753
  .af_inet4:
653
  .af_inet4:
754
	DEBUGF	1,"Socket type:%u\n", [eax + SOCKET_head.Type]:4
654
	DEBUGF	1,"Socket type:%u\n", [eax + SOCKET_head.Type]:4
Line 761... Line 661...
761
 
661
 
762
	cmp	[eax + SOCKET_head.Type], SOCK_RAW
662
	cmp	[eax + SOCKET_head.Type], SOCK_RAW
Line 763... Line 663...
763
	je	.raw
663
	je	.raw
764
 
-
 
Line 765... Line 664...
765
	jmp	s_error
664
 
Line 766... Line 665...
766
;--------
665
	jmp	s_error
Line 790... Line 689...
790
	mov	[esp+32], eax
689
	mov	[esp+32], eax
791
	ret
690
	ret
Line 792... Line 691...
792
 
691
 
Line -... Line 692...
-
 
692
  .tcp:
-
 
693
 
-
 
694
	cmp	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort],0
-
 
695
	jne	@f
-
 
696
 
-
 
697
	push	esi
-
 
698
	mov	ecx, [eax + SOCKET_head.Type]
-
 
699
	call	socket_find_port
-
 
700
	test	bx, bx
-
 
701
	pop	esi
-
 
702
	je	s_error
-
 
703
	mov	[eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort], bx
-
 
704
 
-
 
705
     @@:
-
 
706
 
-
 
707
	mov	ecx, esi
-
 
708
	mov	esi, edx
-
 
709
 
793
  .tcp:
710
	call	TCP_socket_send
794
 
711
 
Line 795... Line -...
795
	mov	[esp+32], eax
-
 
796
	ret
712
	mov	[esp+32], eax
797
 
713
	ret
798
;--------
714
 
Line 799... Line 715...
799
  .raw:
715
  .raw:
800
	cmp	[eax + SOCKET_head.Protocol], IP_PROTO_IP
716
	cmp	[eax + SOCKET_head.Protocol], IP_PROTO_IP
Line 801... Line 717...
801
	je	.raw_ip
717
	je	.raw_ip
802
 
-
 
Line 803... Line 718...
803
	cmp	[eax + SOCKET_head.Protocol], IP_PROTO_ICMP
718
 
Line -... Line 719...
-
 
719
	cmp	[eax + SOCKET_head.Protocol], IP_PROTO_ICMP
-
 
720
	je	.raw_icmp
804
	je	.raw_icmp
721
 
805
 
722
	jmp	s_error
Line 806... Line 723...
806
	jmp	s_error
723