2009-09-17 13:55:38 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2009-10-05 22:47:27 +02:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;;
|
2009-09-17 13:55:38 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; SOCKET.INC ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; based on code by mike.dld ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
$Revision$
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
struct SOCKET_head
|
2009-09-17 13:55:38 +02:00
|
|
|
.PrevPtr dd ? ; pointer to previous socket in list
|
|
|
|
.NextPtr dd ? ; pointer to next socket in list
|
|
|
|
.Number dd ? ; socket number (unique within single process)
|
|
|
|
.PID dd ? ; application process id
|
|
|
|
.Domain dd ? ; INET/UNIX/..
|
|
|
|
.Type dd ? ; RAW/UDP/TCP/...
|
2009-10-05 22:47:27 +02:00
|
|
|
.Protocol dd ? ; ICMP/IPv4/ARP/
|
2009-11-06 21:45:08 +01:00
|
|
|
.lock dd ? ; lock mutex
|
|
|
|
.end:
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct IPv4_SOCKET
|
|
|
|
.LocalIP dd ?
|
|
|
|
.RemoteIP dd ?
|
|
|
|
.SequenceNumber dd ?
|
|
|
|
|
|
|
|
; todo: add options (for func 8 and 9)
|
|
|
|
|
|
|
|
.end:
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct TCP_SOCKET
|
|
|
|
|
|
|
|
.LocalPort dw ? ; In INET byte order
|
|
|
|
.RemotePort dw ? ; In INET byte order
|
|
|
|
|
|
|
|
.backlog dw ? ; Backlog
|
2009-11-08 19:00:01 +01:00
|
|
|
; .OrigRemoteIP dd ? ; original remote IP address (used to reset to LISTEN state)
|
|
|
|
; .OrigRemotePort dw ? ; original remote port (used to reset to LISTEN state)
|
|
|
|
.wndsizeTimer dd ? ; window size timer
|
|
|
|
|
|
|
|
; Transmission control block
|
|
|
|
.state dd ? ; TCB state
|
|
|
|
.timer dd ? ; TCB timer (seconds)
|
|
|
|
.ISS dd ? ; initial send sequence number
|
|
|
|
.IRS dd ? ; initial receive sequence number
|
2009-10-05 22:47:27 +02:00
|
|
|
.SND_UNA dd ? ; sequence number of unack'ed sent Packets
|
2009-11-06 21:45:08 +01:00
|
|
|
.SND_NXT dd ? ; next send sequence number to use
|
2009-10-05 22:47:27 +02:00
|
|
|
.SND_WND dd ? ; send window
|
|
|
|
.RCV_NXT dd ? ; next receive sequence number to use
|
|
|
|
.RCV_WND dd ? ; receive window
|
|
|
|
.SEG_LEN dd ? ; segment length
|
|
|
|
.SEG_WND dd ? ; segment window
|
2009-11-06 21:45:08 +01:00
|
|
|
|
|
|
|
.flags db ? ; packet flags
|
|
|
|
|
|
|
|
.end:
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct UDP_SOCKET
|
|
|
|
|
|
|
|
.LocalPort dw ? ; In INET byte order
|
|
|
|
.RemotePort dw ? ; In INET byte order
|
|
|
|
|
|
|
|
.end:
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct ICMP_SOCKET
|
|
|
|
|
|
|
|
.Identifier dw ? ;
|
|
|
|
|
|
|
|
.end:
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
ends
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
struct IPC_SOCKET
|
|
|
|
|
|
|
|
.ConnectedTo dd ? ; Socket number of other socket this one is connected to
|
|
|
|
|
|
|
|
.end:
|
|
|
|
|
|
|
|
ends
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
MAX_backlog equ 20 ; backlog for stream sockets
|
|
|
|
SOCKETBUFFSIZE equ 4096 ; in bytes
|
|
|
|
SOCKET_QUEUE_SIZE equ 10 ; maximum number ofincoming packets queued for 1 socket
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
uglobal
|
|
|
|
net_sockets rd 2
|
|
|
|
last_UDP_port dw ? ; These values give the number of the last used ephemeral port
|
|
|
|
last_TCP_port dw ? ;
|
|
|
|
endg
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_init
|
|
|
|
;
|
|
|
|
; -
|
|
|
|
;
|
|
|
|
; IN: /
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
|
|
|
|
|
|
|
align 4
|
|
|
|
socket_init:
|
|
|
|
|
|
|
|
mov [net_sockets], 0
|
|
|
|
mov [net_sockets + 4], 0
|
|
|
|
|
|
|
|
mov [last_UDP_port], MIN_EPHEMERAL_PORT
|
|
|
|
mov [last_TCP_port], MIN_EPHEMERAL_PORT
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
;-----------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; Socket API (function 74)
|
|
|
|
;
|
2009-11-08 19:00:01 +01:00
|
|
|
;-----------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
align 4
|
|
|
|
sys_socket:
|
2009-11-08 19:00:01 +01:00
|
|
|
and ebx, 0x000000FF ; should i remove this line ?
|
|
|
|
cmp bl , 7 ; highest possible number
|
|
|
|
jg s_error
|
|
|
|
lea ebx, [.table + 4*ebx]
|
|
|
|
jmp dword [ebx]
|
|
|
|
|
|
|
|
.table:
|
|
|
|
dd socket_open ; 0
|
|
|
|
dd socket_close ; 1
|
|
|
|
dd socket_bind ; 2
|
|
|
|
dd socket_listen ; 3
|
|
|
|
dd socket_connect ; 4
|
|
|
|
dd socket_accept ; 5
|
|
|
|
dd socket_send ; 6
|
|
|
|
dd socket_recv ; 7
|
|
|
|
; dd socket_get_opt ; 8
|
|
|
|
; dd socket_set_opt ; 9
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2009-09-28 21:48:32 +02:00
|
|
|
s_error:
|
2009-09-17 13:55:38 +02:00
|
|
|
mov dword [esp+32],-1
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_open
|
|
|
|
;
|
|
|
|
;
|
|
|
|
; IN: domain in ecx
|
|
|
|
; type in edx
|
2009-10-05 22:47:27 +02:00
|
|
|
; protocol in esi
|
2009-09-17 13:55:38 +02:00
|
|
|
; OUT: eax is socket num, -1 on error
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2009-09-17 13:55:38 +02:00
|
|
|
socket_open:
|
|
|
|
|
|
|
|
DEBUGF 1,"socket_open: domain: %u, type: %u",ecx, edx
|
|
|
|
|
|
|
|
call net_socket_alloc
|
|
|
|
or eax, eax
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [eax + SOCKET_head.Domain], ecx
|
|
|
|
mov [eax + SOCKET_head.Type], edx
|
|
|
|
mov [eax + SOCKET_head.Protocol], esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
stdcall net_socket_addr_to_num, eax
|
|
|
|
DEBUGF 1,", socketnumber: %u\n", eax
|
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
; TODO: if it is txcp socket, set state to TCB_CLOSED
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
mov [esp+32], eax
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_bind
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
|
|
|
; pointer to sockaddr struct in edx
|
|
|
|
; length of that struct in esi
|
|
|
|
; OUT: 0 on success
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2009-09-17 13:55:38 +02:00
|
|
|
socket_bind:
|
|
|
|
|
|
|
|
DEBUGF 1,"Socket_bind: socknum: %u sockaddr: %x, length: %u, ",ecx,edx,esi
|
|
|
|
|
|
|
|
stdcall net_socket_num_to_addr, ecx
|
|
|
|
cmp eax, -1
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
cmp esi, 2
|
2009-09-28 21:48:32 +02:00
|
|
|
jl s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
cmp word [edx], AF_INET4
|
2009-11-06 21:45:08 +01:00
|
|
|
je .af_inet4
|
|
|
|
|
|
|
|
cmp word [edx], AF_UNIX
|
|
|
|
je .af_unix
|
|
|
|
|
|
|
|
jmp s_error
|
|
|
|
|
|
|
|
.af_unix:
|
|
|
|
|
|
|
|
; TODO: write code here
|
|
|
|
|
|
|
|
mov dword [esp+32],0
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.af_inet4:
|
|
|
|
|
|
|
|
cmp esi, 6
|
2009-09-28 21:48:32 +02:00
|
|
|
jl s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ecx, [eax + SOCKET_head.Type]
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
mov bx, word [edx + 2]
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"local port: %x ",bx
|
2009-09-17 13:55:38 +02:00
|
|
|
test bx, bx
|
2009-10-12 16:39:59 +02:00
|
|
|
jz .find_free
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
call socket_check_port
|
|
|
|
test bx, bx
|
|
|
|
je s_error
|
|
|
|
jmp .got_port
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.find_free:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
call socket_find_port
|
|
|
|
test bx, bx
|
|
|
|
je s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.got_port:
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"using port: %x ",bx
|
2009-11-06 21:45:08 +01:00
|
|
|
mov word [eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort], bx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
mov ebx, dword [edx + 4]
|
2009-11-06 21:45:08 +01:00
|
|
|
mov dword [eax + SOCKET_head.end + IPv4_SOCKET.LocalIP], ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
DEBUGF 1,"local ip: %u.%u.%u.%u\n",\
|
2009-11-06 21:45:08 +01:00
|
|
|
[eax + SOCKET_head.end + IPv4_SOCKET.LocalIP + 0]:1,[eax + SOCKET_head.end + IPv4_SOCKET.LocalIP + 1]:1,\
|
|
|
|
[eax + SOCKET_head.end + IPv4_SOCKET.LocalIP + 2]:1,[eax + SOCKET_head.end + IPv4_SOCKET.LocalIP + 3]:1
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
mov dword [esp+32],0
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_connect
|
|
|
|
;
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
|
|
|
; pointer to sockaddr struct in edx
|
|
|
|
; length of that struct in esi
|
|
|
|
; OUT: 0 on success
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
|
|
|
align 4
|
|
|
|
socket_connect:
|
|
|
|
|
|
|
|
DEBUGF 1,"Socket_connect: socknum: %u sockaddr: %x, length: %u,",ecx,edx,esi
|
|
|
|
|
|
|
|
stdcall net_socket_num_to_addr, ecx
|
|
|
|
cmp eax, -1
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
cmp esi, 8
|
2009-09-28 21:48:32 +02:00
|
|
|
jl s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
cmp word [edx], AF_INET4
|
|
|
|
je .af_inet4
|
|
|
|
|
2009-09-28 21:48:32 +02:00
|
|
|
jmp s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.af_inet4:
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [eax + SOCKET_head.Type], IP_PROTO_UDP
|
2009-09-17 13:55:38 +02:00
|
|
|
je .udp
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [eax + SOCKET_head.Type], IP_PROTO_TCP
|
2009-10-12 16:39:59 +02:00
|
|
|
je .tcp
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-09-28 21:48:32 +02:00
|
|
|
jmp s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
.udp:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
mov bx , word [edx + 2]
|
2009-11-06 21:45:08 +01:00
|
|
|
mov word [eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.RemotePort], bx
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"remote port: %x ",bx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
mov ebx, dword [edx + 4]
|
2009-11-06 21:45:08 +01:00
|
|
|
mov dword [eax + SOCKET_head.end + IPv4_SOCKET.RemoteIP], ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
DEBUGF 1,"remote ip: %u.%u.%u.%u\n",[edx+4]:1,[edx+5]:1,[edx+6]:1,[edx+7]:1
|
|
|
|
|
|
|
|
mov dword [esp+32],0
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
.tcp:
|
|
|
|
; TODO: set sequence number to random value
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
; fill in remote port and IP
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.wndsizeTimer], 0 ; Reset the window timer.
|
|
|
|
; TODO: figure out WTF this is
|
|
|
|
mov bx , word [edx + 2]
|
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RemotePort], bx
|
|
|
|
DEBUGF 1,"remote port: %x ",bx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
mov ebx, dword [edx + 4]
|
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.RemoteIP], ebx
|
|
|
|
|
|
|
|
; check if local port and IP is ok
|
|
|
|
|
|
|
|
cmp [eax + SOCKET_head.end + IPv4_SOCKET.LocalIP], 0
|
|
|
|
jne @f
|
|
|
|
push [IP_LIST] ; device zero = default
|
|
|
|
pop [eax + SOCKET_head.end + IPv4_SOCKET.LocalIP]
|
|
|
|
@@:
|
|
|
|
|
|
|
|
cmp [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort], 0
|
|
|
|
jne @f
|
|
|
|
|
|
|
|
mov ecx, [eax + SOCKET_head.Type]
|
|
|
|
call socket_find_port
|
|
|
|
test bx, bx
|
|
|
|
jz s_error
|
|
|
|
|
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort], bx
|
|
|
|
@@:
|
|
|
|
|
|
|
|
|
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_SYN_SENT
|
|
|
|
; now say hello to the remote tcp socket
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
mov bl, TH_SYN
|
|
|
|
call TCP_send_ack
|
|
|
|
|
|
|
|
mov dword [esp+32],0
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_listen
|
|
|
|
;
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
|
|
|
; backlog in edx
|
|
|
|
; OUT: eax is socket num, -1 on error
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2009-09-17 13:55:38 +02:00
|
|
|
socket_listen:
|
|
|
|
|
|
|
|
DEBUGF 1,"Socket_listen: socknum: %u backlog: %u\n",ecx,edx
|
|
|
|
|
|
|
|
stdcall net_socket_num_to_addr, ecx
|
|
|
|
cmp eax, -1
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
cmp word [eax + SOCKET_head.Domain], AF_INET4
|
|
|
|
jne s_error
|
|
|
|
|
|
|
|
cmp [eax + SOCKET_head.Type], IP_PROTO_TCP
|
|
|
|
jne s_error
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
cmp edx, MAX_backlog
|
|
|
|
jl .ok
|
|
|
|
mov dx , 20
|
|
|
|
.ok:
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog], dx
|
2009-11-08 19:00:01 +01:00
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_LISTEN
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
mov dword [esp+32], 0
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_accept
|
|
|
|
;
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
|
|
|
; addr in edx
|
|
|
|
; addrlen in esi
|
|
|
|
; OUT: eax is socket num, -1 on error
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2009-09-17 13:55:38 +02:00
|
|
|
socket_accept:
|
|
|
|
|
|
|
|
DEBUGF 1,"Socket_accept: socknum: %u sockaddr: %x, length: %u\n",ecx,edx,esi
|
|
|
|
|
|
|
|
stdcall net_socket_num_to_addr, ecx
|
|
|
|
or eax, eax
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
mov esi, eax
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp word [esi + SOCKET_head.Domain], AF_INET4
|
|
|
|
je .af_inet4
|
|
|
|
|
|
|
|
jmp s_error
|
|
|
|
|
|
|
|
.af_inet4:
|
|
|
|
|
|
|
|
cmp [esi + SOCKET_head.Type], IP_PROTO_TCP
|
|
|
|
je .tcp
|
|
|
|
|
|
|
|
jmp s_error
|
|
|
|
|
|
|
|
.tcp:
|
|
|
|
|
|
|
|
cmp [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog], 0
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
call net_socket_alloc
|
|
|
|
or eax, eax
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
mov edi, eax
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
dec [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ecx, (SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.end+3)/4
|
|
|
|
push esi edi
|
2009-09-17 13:55:38 +02:00
|
|
|
rep movsd
|
2009-11-06 21:45:08 +01:00
|
|
|
pop edi esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [edi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.backlog], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
; TODO: fill in structure in ecx
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [esi + SOCKET_head.end + IPv4_SOCKET.RemoteIP], 0
|
|
|
|
mov [esi + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.RemotePort], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
stdcall net_socket_addr_to_num, edi
|
2009-09-17 13:55:38 +02:00
|
|
|
mov [esp+32], eax
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_close
|
|
|
|
;
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
|
|
|
; OUT: eax is socket num, -1 on error
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2009-09-17 13:55:38 +02:00
|
|
|
socket_close:
|
|
|
|
|
|
|
|
DEBUGF 1,"Socket_close: socknum: %u\n",ecx
|
|
|
|
|
|
|
|
stdcall net_socket_num_to_addr, ecx
|
|
|
|
or eax, eax
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [eax + SOCKET_head.Domain], AF_INET4
|
|
|
|
jne s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [eax + SOCKET_head.Type], IP_PROTO_UDP
|
2009-09-17 13:55:38 +02:00
|
|
|
je .udp
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [eax + SOCKET_head.Type], IP_PROTO_ICMP
|
2009-09-17 13:55:38 +02:00
|
|
|
je .icmp
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [eax + SOCKET_head.Type], IP_PROTO_TCP
|
2009-10-12 16:39:59 +02:00
|
|
|
je .tcp
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-09-28 21:48:32 +02:00
|
|
|
jmp s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.udp:
|
|
|
|
|
|
|
|
stdcall net_socket_free, eax
|
|
|
|
mov dword [esp+32],0
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
.icmp:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.tcp:
|
2009-11-08 19:00:01 +01:00
|
|
|
; first, remove all resend entries for this socket
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
cmp [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_LISTEN
|
2009-09-17 13:55:38 +02:00
|
|
|
je .destroy_tcb
|
2009-11-08 19:00:01 +01:00
|
|
|
cmp [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_SYN_SENT
|
2009-09-17 13:55:38 +02:00
|
|
|
je .destroy_tcb
|
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
; Now construct the response, and queue for sending by IP
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
mov bl, TH_FIN
|
2009-11-08 19:00:01 +01:00
|
|
|
call TCP_send_ack
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
; increament SND.NXT in socket
|
2009-11-08 19:00:01 +01:00
|
|
|
lea esi, [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.SND_NXT]
|
|
|
|
inc_INET esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
; Get the socket state
|
2009-11-08 19:00:01 +01:00
|
|
|
cmp [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_SYN_RECEIVED
|
2009-09-17 13:55:38 +02:00
|
|
|
je .fin_wait_1
|
2009-11-08 19:00:01 +01:00
|
|
|
cmp [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_ESTABLISHED
|
2009-09-17 13:55:38 +02:00
|
|
|
je .fin_wait_1
|
|
|
|
|
|
|
|
; assume CLOSE WAIT
|
|
|
|
; Send a fin, then enter last-ack state
|
2009-11-08 19:00:01 +01:00
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_LAST_ACK
|
2009-09-17 13:55:38 +02:00
|
|
|
jmp .send
|
|
|
|
|
|
|
|
.fin_wait_1:
|
|
|
|
; Send a fin, then enter finwait2 state
|
2009-11-08 19:00:01 +01:00
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.state], TCB_FIN_WAIT_1
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.send:
|
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
;;;;;
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.destroy_tcb:
|
|
|
|
|
|
|
|
stdcall net_socket_free, eax
|
|
|
|
mov dword [esp+32],0
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_receive
|
|
|
|
;
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
2009-11-06 21:45:08 +01:00
|
|
|
; addr to buffer in edx
|
|
|
|
; length of buffer in esi
|
2009-09-17 13:55:38 +02:00
|
|
|
; flags in edi
|
|
|
|
; OUT: eax is number of bytes copied, -1 on error
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2009-09-17 13:55:38 +02:00
|
|
|
socket_recv:
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
DEBUGF 1,"Socket_receive: socknum: %u bufferaddress: %x, length: %u, flags: %x\n",ecx,edx,esi,edi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
stdcall net_socket_num_to_addr, ecx ; get real socket address
|
|
|
|
or eax, eax
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
DEBUGF 1,"Socket pointer: %x\n", eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
get_from_queue (eax + 2048), SOCKET_QUEUE_SIZE, 4*3, s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
mov edi, edx
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ecx, [esi + socket_queue_entry.data_size]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
DEBUGF 1,"Got %u bytes of data\n", ecx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp ecx, edx
|
|
|
|
jle .large_enough
|
|
|
|
DEBUGF 1,"Buffer too small...\n"
|
|
|
|
jmp s_error
|
|
|
|
.large_enough:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push [esi + socket_queue_entry.data_ptr]
|
|
|
|
mov esi, [esi + socket_queue_entry.offset]
|
|
|
|
add esi, [esp]
|
|
|
|
DEBUGF 1,"Source buffer: %x, real addr: %x\n", [esp], esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov dword[esp+32+4], ecx ; return number of bytes copied
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
shr ecx, 1
|
|
|
|
jnc .nb
|
|
|
|
movsb
|
|
|
|
.nb: shr ecx, 1
|
|
|
|
jnc .nw
|
|
|
|
movsw
|
|
|
|
.nw: rep movsd
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
call kernel_free
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_send
|
|
|
|
;
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
2009-10-12 16:39:59 +02:00
|
|
|
; pointer to data in edx
|
|
|
|
; datalength in esi
|
2009-09-17 13:55:38 +02:00
|
|
|
; flags in edi
|
|
|
|
; OUT: -1 on error
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
2009-11-08 19:00:01 +01:00
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2009-09-17 13:55:38 +02:00
|
|
|
socket_send:
|
|
|
|
|
|
|
|
DEBUGF 1,"Socket_send: socknum: %u sockaddr: %x, length: %u, flags: %x, ",ecx,edx,esi,edi
|
|
|
|
|
|
|
|
stdcall net_socket_num_to_addr, ecx ; get real socket address
|
|
|
|
or eax, eax
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp word [eax + SOCKET_head.Domain], AF_INET4
|
2009-10-12 16:39:59 +02:00
|
|
|
je .af_inet4
|
|
|
|
|
|
|
|
jmp s_error
|
|
|
|
|
|
|
|
.af_inet4:
|
2009-11-06 21:45:08 +01:00
|
|
|
DEBUGF 1,"Socket type:%u\n", [eax + SOCKET_head.Type]:4
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [eax + SOCKET_head.Type], IP_PROTO_TCP
|
|
|
|
je .tcp
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [eax + SOCKET_head.Type], IP_PROTO_UDP
|
2009-09-17 13:55:38 +02:00
|
|
|
je .udp
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [eax + SOCKET_head.Type], SOCK_RAW
|
|
|
|
je .raw
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-09-28 21:48:32 +02:00
|
|
|
jmp s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.udp:
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
DEBUGF 1,"type: UDP, "
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort],0
|
|
|
|
jne @f
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2009-10-12 20:14:14 +02:00
|
|
|
push esi
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ecx, [eax + SOCKET_head.Type]
|
2009-10-12 16:39:59 +02:00
|
|
|
call socket_find_port
|
|
|
|
test bx, bx
|
2009-10-12 20:14:14 +02:00
|
|
|
pop esi
|
2009-10-12 16:39:59 +02:00
|
|
|
je s_error
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort], bx
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
@@:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
mov ecx, esi
|
|
|
|
mov esi, edx
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
call UDP_socket_send
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.tcp:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
cmp [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort],0
|
|
|
|
jne @f
|
|
|
|
|
|
|
|
push esi
|
|
|
|
mov ecx, [eax + SOCKET_head.Type]
|
|
|
|
call socket_find_port
|
|
|
|
test bx, bx
|
|
|
|
pop esi
|
|
|
|
je s_error
|
|
|
|
mov [eax + SOCKET_head.end + IPv4_SOCKET.end + TCP_SOCKET.LocalPort], bx
|
|
|
|
|
|
|
|
@@:
|
|
|
|
|
|
|
|
mov ecx, esi
|
|
|
|
mov esi, edx
|
|
|
|
|
|
|
|
call TCP_socket_send
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.raw:
|
|
|
|
cmp [eax + SOCKET_head.Protocol], IP_PROTO_IP
|
|
|
|
je .raw_ip
|
|
|
|
|
|
|
|
cmp [eax + SOCKET_head.Protocol], IP_PROTO_ICMP
|
|
|
|
je .raw_icmp
|
|
|
|
|
|
|
|
jmp s_error
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.raw_ip:
|
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
;;;;;;
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [esp+32], eax
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.raw_icmp:
|
|
|
|
|
|
|
|
; sub ecx, ICMP_Packet.Data
|
|
|
|
; mov esi, edx
|
|
|
|
; push ax
|
|
|
|
; call IPv4_get_frgmnt_num
|
|
|
|
; mov dx, ax
|
|
|
|
; pop ax
|
|
|
|
; shl edx, 16
|
|
|
|
; mov dh , [esi + ICMP_Packet.Type]
|
|
|
|
; mov dl , [esi + ICMP_Packet.Code]
|
|
|
|
; mov di , [esi + ICMP_Packet.Identifier]
|
|
|
|
; mov [eax + SOCKET.LocalPort], di ; Set localport to the identifier number, so we can receive reply's
|
|
|
|
; shl edi, 16
|
|
|
|
; mov di , [esi + ICMP_Packet.SequenceNumber]
|
|
|
|
; add esi, ICMP_Packet.Data
|
|
|
|
; mov ebx, [eax + SOCKET.LocalIP]
|
|
|
|
; mov eax, [eax + SOCKET.RemoteIP]
|
|
|
|
; call ICMP_create_packet
|
|
|
|
|
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_find_free_port (local port)
|
|
|
|
;
|
|
|
|
; works with INET byte order
|
|
|
|
;
|
|
|
|
; IN: type in ecx (TCP/UDP)
|
|
|
|
; OUT: bx = 0 on error, portnumber otherwise
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
|
|
|
align 4
|
|
|
|
socket_find_port:
|
|
|
|
|
2009-10-12 20:14:14 +02:00
|
|
|
DEBUGF 1,"Socket_find_free_port\n"
|
2009-10-12 16:39:59 +02:00
|
|
|
|
|
|
|
cmp ecx, IP_PROTO_UDP
|
|
|
|
je .udp
|
|
|
|
|
|
|
|
cmp ecx, IP_PROTO_TCP
|
|
|
|
je .tcp
|
|
|
|
|
|
|
|
.udp:
|
|
|
|
mov bx, [last_UDP_port]
|
|
|
|
je .continue
|
|
|
|
|
|
|
|
.tcp:
|
|
|
|
mov bx, [last_TCP_port]
|
|
|
|
|
|
|
|
|
|
|
|
.continue:
|
|
|
|
inc bx
|
|
|
|
|
|
|
|
.check_only:
|
|
|
|
mov esi, net_sockets
|
|
|
|
|
|
|
|
.next_socket:
|
2009-11-06 21:45:08 +01:00
|
|
|
mov esi, [esi + SOCKET_head.NextPtr]
|
2009-10-12 16:39:59 +02:00
|
|
|
or esi, esi
|
|
|
|
jz .port_ok
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [esi + SOCKET_head.Type], ecx
|
2009-10-12 16:39:59 +02:00
|
|
|
jne .next_socket
|
|
|
|
|
|
|
|
rol bx, 8
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [esi + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort], bx
|
2009-10-12 16:39:59 +02:00
|
|
|
rol bx, 8 ; this doesnt change the zero flag, does it ?
|
|
|
|
jne .next_socket
|
|
|
|
|
|
|
|
cmp bx, MAX_EPHEMERAL_PORT
|
|
|
|
jle .continue
|
|
|
|
|
|
|
|
; todo: WRAP!
|
|
|
|
; mov [last_UDP_port], MIN_EPHEMERAL_PORT
|
|
|
|
.exit:
|
|
|
|
xor ebx, ebx
|
|
|
|
|
|
|
|
.port_ok:
|
|
|
|
rol bx, 8
|
|
|
|
ret
|
|
|
|
|
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_check_port (local port)
|
|
|
|
;
|
|
|
|
; works with INET byte order
|
|
|
|
;
|
|
|
|
; IN: type in ecx (TCP/UDP)
|
|
|
|
; port to check in bx
|
|
|
|
; OUT: bx = 0 on error, unchanged otherwise
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
|
|
|
align 4
|
|
|
|
socket_check_port:
|
|
|
|
mov esi, net_sockets
|
|
|
|
|
|
|
|
.next_socket:
|
2009-11-06 21:45:08 +01:00
|
|
|
mov esi, [esi + SOCKET_head.NextPtr]
|
2009-10-12 16:39:59 +02:00
|
|
|
or esi, esi
|
|
|
|
jz .port_ok
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [esi + SOCKET_head.Type], ecx
|
2009-10-12 16:39:59 +02:00
|
|
|
jne .next_socket
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [esi + SOCKET_head.end + IPv4_SOCKET.end + UDP_SOCKET.LocalPort], bx
|
2009-10-12 16:39:59 +02:00
|
|
|
jne .next_socket
|
|
|
|
|
|
|
|
xor ebx, ebx
|
|
|
|
|
|
|
|
.port_ok:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_internal_receiver
|
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
; Updates a socket with received data
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2009-11-06 21:45:08 +01:00
|
|
|
; Note: the mutex must already be set !
|
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; ecx = size
|
|
|
|
; esi = pointer to buffer
|
|
|
|
; edi = offset
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
|
|
|
; OUT: xxx
|
|
|
|
;
|
|
|
|
;-----------------------------------------------
|
|
|
|
align 4
|
|
|
|
socket_internal_receiver:
|
|
|
|
|
2009-11-08 22:44:04 +01:00
|
|
|
DEBUGF 1,"Internal socket receiver: buffer %x, offset: %x size=%u socket: %x\n", esi, edi, ecx, eax
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
push edi ; offset
|
|
|
|
push ecx ; size
|
|
|
|
push esi ; data_ptr
|
|
|
|
mov esi, esp
|
|
|
|
add_to_queue (eax + 2048), SOCKET_QUEUE_SIZE, 3*4, .full
|
|
|
|
DEBUGF 1,"Queued packet successfully\n"
|
|
|
|
add esp, 4*3
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [eax + SOCKET_head.lock], 0
|
2009-10-12 16:39:59 +02:00
|
|
|
|
|
|
|
; flag an event to the application
|
2009-11-06 21:45:08 +01:00
|
|
|
mov edx, [eax + SOCKET_head.PID] ; get socket owner PID
|
2009-10-12 16:39:59 +02:00
|
|
|
mov ecx, 1
|
|
|
|
mov esi, TASK_DATA + TASKDATA.pid
|
|
|
|
|
|
|
|
.next_pid:
|
|
|
|
cmp [esi], edx
|
|
|
|
je .found_pid
|
|
|
|
inc ecx
|
|
|
|
add esi, 0x20
|
|
|
|
cmp ecx, [TASK_COUNT]
|
|
|
|
jbe .next_pid
|
|
|
|
ret
|
|
|
|
|
|
|
|
.found_pid:
|
|
|
|
shl ecx, 8
|
2009-11-06 21:45:08 +01:00
|
|
|
or [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK ; stack event
|
2009-10-12 16:39:59 +02:00
|
|
|
mov [check_idle_semaphore], 200
|
|
|
|
ret
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
.full:
|
|
|
|
DEBUGF 1,"Socket %x is full!\n",eax
|
|
|
|
mov [eax + SOCKET_head.lock], 0
|
|
|
|
call kernel_free
|
|
|
|
add esp, 8
|
2009-10-12 16:39:59 +02:00
|
|
|
ret
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; Allocate memory for socket data and put new socket into the list
|
|
|
|
; Newly created socket is initialized with calling PID and number and
|
|
|
|
; put into beginning of list (which is a fastest way).
|
|
|
|
;
|
|
|
|
; @return socket structure address in EAX
|
|
|
|
;
|
|
|
|
proc net_socket_alloc stdcall uses ebx ecx edx edi
|
|
|
|
stdcall kernel_alloc, SOCKETBUFFSIZE
|
|
|
|
DEBUGF 1, "K : net_socket_alloc (0x%x)\n", eax
|
|
|
|
; check if we can allocate needed amount of memory
|
|
|
|
or eax, eax
|
|
|
|
jz .exit
|
|
|
|
|
|
|
|
; zero-initialize allocated memory
|
|
|
|
push eax
|
|
|
|
mov edi, eax
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
mov ecx, SOCKETBUFFSIZE / 4
|
|
|
|
; cld
|
|
|
|
xor eax, eax
|
|
|
|
rep stosd
|
|
|
|
pop eax
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
init_queue (eax + 2048)
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
; add socket to the list by changing pointers
|
|
|
|
mov ebx, net_sockets
|
2009-11-06 21:45:08 +01:00
|
|
|
push [ebx + SOCKET_head.NextPtr]
|
|
|
|
mov [ebx + SOCKET_head.NextPtr], eax
|
|
|
|
mov [eax + SOCKET_head.PrevPtr], ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
pop ebx
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [eax + SOCKET_head.NextPtr], ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
or ebx, ebx
|
|
|
|
jz @f
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.PrevPtr], eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
@@: ; set socket owner PID to the one of calling process
|
|
|
|
mov ebx, [TASK_BASE]
|
|
|
|
mov ebx, [ebx + TASKDATA.pid]
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [eax + SOCKET_head.PID], ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
; find first free socket number and use it
|
|
|
|
;mov edx, ebx
|
|
|
|
mov ebx, net_sockets
|
|
|
|
xor ecx, ecx
|
|
|
|
.next_socket_number:
|
|
|
|
inc ecx
|
|
|
|
.next_socket:
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ebx, [ebx + SOCKET_head.NextPtr]
|
2009-09-17 13:55:38 +02:00
|
|
|
or ebx, ebx
|
|
|
|
jz .last_socket_number
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [ebx + SOCKET_head.Number], ecx
|
2009-09-17 13:55:38 +02:00
|
|
|
jne .next_socket
|
|
|
|
;cmp [ebx + SOCKET.PID], edx
|
|
|
|
;jne .next_socket
|
|
|
|
mov ebx, net_sockets
|
|
|
|
jmp .next_socket_number
|
|
|
|
|
|
|
|
.last_socket_number:
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [eax + SOCKET_head.Number], ecx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
; Free socket data memory and pop socket off the list
|
|
|
|
;
|
|
|
|
; @param sockAddr is a socket structure address
|
|
|
|
;
|
|
|
|
proc net_socket_free stdcall uses ebx ecx edx, sockAddr:DWORD
|
|
|
|
mov eax, [sockAddr]
|
|
|
|
DEBUGF 1, "K : net_socket_free (0x%x)\n", eax
|
|
|
|
; check if we got something similar to socket structure address
|
|
|
|
or eax, eax
|
|
|
|
jz .error
|
|
|
|
|
|
|
|
; make sure sockAddr is one of the socket addresses in the list
|
|
|
|
mov ebx, net_sockets
|
|
|
|
;mov ecx, [TASK_BASE]
|
|
|
|
;mov ecx, [ecx + TASKDATA.pid]
|
|
|
|
.next_socket:
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ebx, [ebx + SOCKET_head.NextPtr]
|
2009-09-17 13:55:38 +02:00
|
|
|
or ebx, ebx
|
|
|
|
jz .error
|
|
|
|
cmp ebx, eax
|
|
|
|
jne .next_socket
|
|
|
|
;cmp [ebx + SOCKET.PID], ecx
|
|
|
|
;jne .next_socket
|
|
|
|
|
|
|
|
; okay, we found the correct one
|
|
|
|
; remove it from the list first, changing pointers
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ebx, [eax + SOCKET_head.NextPtr]
|
|
|
|
mov eax, [eax + SOCKET_head.PrevPtr]
|
|
|
|
mov [eax + SOCKET_head.NextPtr], ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
or ebx, ebx
|
|
|
|
jz @f
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [ebx + SOCKET_head.PrevPtr], eax
|
|
|
|
|
|
|
|
lea ebx, [eax + SOCKET_head.lock]
|
|
|
|
call wait_mutex
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
@@: ; and finally free the memory structure used
|
|
|
|
stdcall kernel_free, [sockAddr]
|
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
|
|
|
DEBUGF 1, "K : failed\n"
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
; Get socket structure address by its number
|
|
|
|
; Scan through sockets list to find the socket with specified number.
|
|
|
|
; This proc uses SOCKET.PID indirectly to check if socket is owned by
|
|
|
|
; calling process.
|
|
|
|
;
|
|
|
|
; @param sockNum is a socket number
|
|
|
|
; @return socket structure address or 0 (not found) in EAX
|
|
|
|
;
|
|
|
|
proc net_socket_num_to_addr stdcall uses ebx ecx, sockNum:DWORD
|
|
|
|
mov eax, [sockNum]
|
|
|
|
; check if we got something similar to socket number
|
|
|
|
or eax, eax
|
|
|
|
jz .error
|
|
|
|
|
|
|
|
; scan through sockets list
|
|
|
|
mov ebx, net_sockets
|
|
|
|
;mov ecx, [TASK_BASE]
|
|
|
|
;mov ecx, [ecx + TASKDATA.pid]
|
|
|
|
.next_socket:
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ebx, [ebx + SOCKET_head.NextPtr]
|
2009-09-17 13:55:38 +02:00
|
|
|
or ebx, ebx
|
|
|
|
jz .error
|
2009-11-06 21:45:08 +01:00
|
|
|
cmp [ebx + SOCKET_head.Number], eax
|
2009-09-17 13:55:38 +02:00
|
|
|
jne .next_socket
|
|
|
|
;cmp [ebx + SOCKET.PID], ecx
|
|
|
|
;jne .next_socket
|
|
|
|
|
|
|
|
; okay, we found the correct one
|
|
|
|
mov eax, ebx
|
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
; Get socket number by its structure address
|
|
|
|
; Scan through sockets list to find the socket with specified address.
|
|
|
|
; This proc uses SOCKET.PID indirectly to check if socket is owned by
|
|
|
|
; calling process.
|
|
|
|
;
|
|
|
|
; @param sockAddr is a socket structure address
|
|
|
|
; @return socket number (SOCKET.Number) or 0 (not found) in EAX
|
|
|
|
;
|
|
|
|
proc net_socket_addr_to_num stdcall uses ebx ecx, sockAddr:DWORD
|
|
|
|
mov eax, [sockAddr]
|
|
|
|
; check if we got something similar to socket structure address
|
|
|
|
or eax, eax
|
|
|
|
jz .error
|
|
|
|
|
|
|
|
; scan through sockets list
|
|
|
|
mov ebx, net_sockets
|
|
|
|
;mov ecx, [TASK_BASE]
|
|
|
|
;mov ecx, [ecx + TASKDATA.pid]
|
|
|
|
.next_socket:
|
2009-11-06 21:45:08 +01:00
|
|
|
mov ebx, [ebx + SOCKET_head.NextPtr]
|
2009-09-17 13:55:38 +02:00
|
|
|
or ebx, ebx
|
|
|
|
jz .error
|
|
|
|
cmp ebx, eax
|
|
|
|
jne .next_socket
|
|
|
|
;cmp [ebx + SOCKET.PID], ecx
|
|
|
|
;jne .next_socket
|
|
|
|
|
|
|
|
; okay, we found the correct one
|
2009-11-06 21:45:08 +01:00
|
|
|
mov eax, [ebx + SOCKET_head.Number]
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
endp
|