2013-05-28 21:34:26 +04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2015-01-08 23:10:22 +03:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;;
|
2013-05-28 21:34:26 +04:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; Part of the TCP/IP network stack for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org, ;;
|
|
|
|
;; and Clevermouse. ;;
|
|
|
|
;; ;;
|
|
|
|
;; Based on code by mike.dld ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-04-18 01:19:45 +04:00
|
|
|
$Revision$
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2012-02-22 19:46:09 +04:00
|
|
|
struct SOCKET
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
NextPtr dd ? ; pointer to next socket in list
|
|
|
|
PrevPtr dd ? ; pointer to previous socket in list
|
|
|
|
Number dd ? ; socket number
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mutex MUTEX
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
PID dd ? ; process ID
|
|
|
|
TID dd ? ; thread ID
|
|
|
|
Domain dd ? ; INET4/INET6/LOCAL/..
|
|
|
|
Type dd ? ; RAW/STREAM/DGRAM
|
|
|
|
Protocol dd ? ; UDP/TCP/ARP/ICMP
|
2013-05-28 21:34:26 +04:00
|
|
|
errorcode dd ?
|
2015-12-17 00:20:53 +03:00
|
|
|
device dd ? ; device pointer, paired socket pointer if it's a local socket
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
options dd ?
|
|
|
|
state dd ?
|
2015-12-17 00:20:53 +03:00
|
|
|
backlog dw ? ; number of incoming connections that can be queued
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
snd_proc dd ?
|
|
|
|
rcv_proc dd ?
|
2013-10-15 14:35:08 +04:00
|
|
|
connect_proc dd ?
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct IP_SOCKET SOCKET
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
LocalIP rd 4 ; network byte order
|
|
|
|
RemoteIP rd 4 ; network byte order
|
2015-10-11 21:23:40 +03:00
|
|
|
ttl db ?
|
2015-12-17 00:20:53 +03:00
|
|
|
rb 3 ; align
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct TCP_SOCKET IP_SOCKET
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
LocalPort dw ? ; network byte order
|
|
|
|
RemotePort dw ? ; network byte order
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
t_state dd ? ; TCB state
|
2013-05-28 21:34:26 +04:00
|
|
|
t_rxtshift db ?
|
2015-12-17 00:20:53 +03:00
|
|
|
rb 3 ; align
|
2013-05-28 21:34:26 +04:00
|
|
|
t_rxtcur dd ?
|
|
|
|
t_dupacks dd ?
|
|
|
|
t_maxseg dd ?
|
|
|
|
t_force dd ?
|
|
|
|
t_flags dd ?
|
|
|
|
|
|
|
|
;---------------
|
|
|
|
; RFC783 page 21
|
|
|
|
|
|
|
|
; send sequence
|
2015-12-17 00:20:53 +03:00
|
|
|
SND_UNA dd ? ; sequence number of unack'ed sent Packets
|
|
|
|
SND_NXT dd ? ; next send sequence number to use
|
|
|
|
SND_UP dd ? ; urgent pointer
|
|
|
|
SND_WL1 dd ? ; window minus one
|
|
|
|
SND_WL2 dd ? ;
|
|
|
|
ISS dd ? ; initial send sequence number
|
|
|
|
SND_WND dd ? ; send window
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; receive sequence
|
2015-12-17 00:20:53 +03:00
|
|
|
RCV_WND dd ? ; receive window
|
|
|
|
RCV_NXT dd ? ; next receive sequence number to use
|
|
|
|
RCV_UP dd ? ; urgent pointer
|
|
|
|
IRS dd ? ; initial receive sequence number
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
;---------------------
|
|
|
|
; Additional variables
|
|
|
|
|
|
|
|
; receive variables
|
|
|
|
RCV_ADV dd ?
|
|
|
|
|
|
|
|
; retransmit variables
|
|
|
|
SND_MAX dd ?
|
|
|
|
|
|
|
|
; congestion control
|
2013-12-11 01:08:29 +04:00
|
|
|
SND_CWND dd ? ; congestion window
|
|
|
|
SND_SSTHRESH dd ? ; slow start threshold
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
;----------------------
|
|
|
|
; Transmit timing stuff
|
|
|
|
t_idle dd ?
|
2015-02-21 18:07:30 +03:00
|
|
|
t_rtt dd ? ; round trip time
|
2013-05-28 21:34:26 +04:00
|
|
|
t_rtseq dd ?
|
2015-02-21 18:07:30 +03:00
|
|
|
t_srtt dd ? ; smoothed round trip time
|
2013-05-28 21:34:26 +04:00
|
|
|
t_rttvar dd ?
|
|
|
|
t_rttmin dd ?
|
|
|
|
max_sndwnd dd ?
|
|
|
|
|
|
|
|
;-----------------
|
|
|
|
; Out-of-band data
|
|
|
|
t_oobflags dd ?
|
|
|
|
t_iobc dd ?
|
|
|
|
t_softerror dd ?
|
|
|
|
|
|
|
|
|
|
|
|
;---------
|
|
|
|
; RFC 1323 ; the order of next 4 elements may not change
|
|
|
|
|
|
|
|
SND_SCALE db ?
|
|
|
|
RCV_SCALE db ?
|
|
|
|
requested_s_scale db ?
|
|
|
|
request_r_scale db ?
|
|
|
|
|
|
|
|
ts_recent dd ? ; a copy of the most-recent valid timestamp from the other end
|
|
|
|
ts_recent_age dd ?
|
|
|
|
last_ack_sent dd ?
|
|
|
|
|
|
|
|
|
|
|
|
;-------
|
|
|
|
; Timers
|
2013-06-04 18:12:37 +04:00
|
|
|
timer_flags dd ?
|
2015-12-17 00:20:53 +03:00
|
|
|
timer_retransmission dd ? ; rexmt
|
2013-05-28 21:34:26 +04:00
|
|
|
timer_persist dd ?
|
2015-12-17 00:20:53 +03:00
|
|
|
timer_keepalive dd ? ; keepalive/syn timeout
|
|
|
|
timer_timed_wait dd ? ; also used as 2msl timer
|
2013-10-14 01:59:31 +04:00
|
|
|
timer_connect dd ?
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; extra
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
ts_ecr dd ? ; timestamp echo reply
|
2013-05-28 21:34:26 +04:00
|
|
|
ts_val dd ?
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
seg_next dd ? ; re-assembly queue
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct UDP_SOCKET IP_SOCKET
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
LocalPort dw ? ; in network byte order
|
|
|
|
RemotePort dw ? ; in network byte order
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct RING_BUFFER
|
|
|
|
|
|
|
|
mutex MUTEX
|
2015-12-17 00:20:53 +03:00
|
|
|
start_ptr dd ? ; Pointer to start of buffer
|
|
|
|
end_ptr dd ? ; pointer to end of buffer
|
|
|
|
read_ptr dd ? ; Read pointer
|
|
|
|
write_ptr dd ? ; Write pointer
|
|
|
|
size dd ? ; Number of bytes buffered
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct STREAM_SOCKET TCP_SOCKET
|
|
|
|
|
|
|
|
rcv RING_BUFFER
|
|
|
|
snd RING_BUFFER
|
|
|
|
|
|
|
|
ends
|
|
|
|
|
|
|
|
struct socket_queue_entry
|
|
|
|
|
|
|
|
data_ptr dd ?
|
|
|
|
data_size dd ?
|
2015-03-18 00:50:29 +03:00
|
|
|
buf_ptr dd ?
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2011-10-15 01:38:50 +04:00
|
|
|
ends
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
struct socket_options
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
level dd ?
|
|
|
|
optname dd ?
|
|
|
|
optlen dd ?
|
|
|
|
optval dd ?
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
ends
|
|
|
|
|
2016-05-01 00:15:55 +03:00
|
|
|
SOCKET_STRUCT_SIZE = 4096 ; in bytes
|
2015-12-17 00:20:53 +03:00
|
|
|
|
|
|
|
SOCKET_QUEUE_SIZE = 10 ; maximum number of incoming packets queued for 1 socket
|
2013-05-28 21:34:26 +04:00
|
|
|
; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start
|
2016-05-01 00:15:55 +03:00
|
|
|
SOCKET_QUEUE_LOCATION = (SOCKET_STRUCT_SIZE - SOCKET_QUEUE_SIZE*sizeof.socket_queue_entry - sizeof.queue)
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
uglobal
|
2013-06-24 15:39:05 +04:00
|
|
|
align 4
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
net_sockets rd 4
|
|
|
|
last_socket_num dd ?
|
2015-12-17 00:20:53 +03:00
|
|
|
last_UDP_port dw ? ; last used ephemeral port
|
|
|
|
last_TCP_port dw ? ;
|
2013-06-12 14:21:41 +04:00
|
|
|
socket_mutex MUTEX
|
2013-06-24 15:39:05 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
endg
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_init ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2015-12-27 18:37:31 +03:00
|
|
|
macro socket_init {
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
mov edi, net_sockets
|
|
|
|
mov ecx, 5
|
2013-06-27 03:35:43 +04:00
|
|
|
rep stosd
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
@@:
|
|
|
|
pseudo_random eax
|
2013-06-04 18:12:37 +04:00
|
|
|
cmp ax, EPHEMERAL_PORT_MIN
|
2013-05-28 21:34:26 +04:00
|
|
|
jb @r
|
2013-06-04 18:12:37 +04:00
|
|
|
cmp ax, EPHEMERAL_PORT_MAX
|
2013-05-28 21:34:26 +04:00
|
|
|
ja @r
|
|
|
|
xchg al, ah
|
|
|
|
mov [last_UDP_port], ax
|
|
|
|
|
|
|
|
@@:
|
|
|
|
pseudo_random eax
|
2013-06-04 18:12:37 +04:00
|
|
|
cmp ax, EPHEMERAL_PORT_MIN
|
2013-05-28 21:34:26 +04:00
|
|
|
jb @r
|
2013-06-04 18:12:37 +04:00
|
|
|
cmp ax, EPHEMERAL_PORT_MAX
|
2013-05-28 21:34:26 +04:00
|
|
|
ja @r
|
|
|
|
xchg al, ah
|
|
|
|
mov [last_TCP_port], ax
|
|
|
|
|
2013-06-12 14:21:41 +04:00
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_init
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
}
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
|
|
|
; Sockets API (system function 75) ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
|
|
|
sys_socket:
|
|
|
|
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], 0 ; Set error code to 0
|
2013-06-15 00:26:05 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
cmp ebx, 255
|
2015-12-27 18:37:31 +03:00
|
|
|
jz socket_debug
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp ebx, .number
|
2013-06-15 00:14:35 +04:00
|
|
|
ja .error
|
2013-05-28 21:34:26 +04:00
|
|
|
jmp dword [.table + 4*ebx]
|
|
|
|
|
|
|
|
.table:
|
2015-12-27 18:37:31 +03:00
|
|
|
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_receive ; 7
|
|
|
|
dd socket_set_opt ; 8
|
|
|
|
dd socket_get_opt ; 9
|
|
|
|
dd socket_pair ; 10
|
2013-05-28 21:34:26 +04:00
|
|
|
.number = ($ - .table) / 4 - 1
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.error:
|
|
|
|
mov dword[esp+32], -1
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EINVAL
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_open: Create a new socket. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ecx = domain ;
|
|
|
|
; edx = type ;
|
|
|
|
; esi = protocol ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = socket number ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ebx = errorcode on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_open:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_open: domain=%u type=%u protocol=%x ", ecx, edx, esi
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
push ecx edx esi
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_alloc
|
2013-05-28 21:34:26 +04:00
|
|
|
pop esi edx ecx
|
2014-01-27 14:27:38 +04:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .nobuffs
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
mov [esp+32], edi ; return socketnumber
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "socknum=%u\n", edi
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-26 00:11:09 +04:00
|
|
|
test edx, SO_NONBLOCK
|
|
|
|
jz @f
|
|
|
|
or [eax + SOCKET.options], SO_NONBLOCK
|
|
|
|
and edx, not SO_NONBLOCK
|
|
|
|
@@:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov [eax + SOCKET.Domain], ecx
|
|
|
|
mov [eax + SOCKET.Type], edx
|
|
|
|
mov [eax + SOCKET.Protocol], esi
|
2013-10-15 14:35:08 +04:00
|
|
|
mov [eax + SOCKET.connect_proc], connect_notsupp
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp ecx, AF_INET4
|
|
|
|
jne .no_inet4
|
|
|
|
|
2015-10-11 21:23:40 +03:00
|
|
|
mov [eax + IP_SOCKET.ttl], 128
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
cmp edx, SOCK_DGRAM
|
|
|
|
je .udp
|
|
|
|
|
|
|
|
cmp edx, SOCK_STREAM
|
|
|
|
je .tcp
|
|
|
|
|
|
|
|
cmp edx, SOCK_RAW
|
|
|
|
je .raw
|
|
|
|
|
|
|
|
.no_inet4:
|
|
|
|
cmp ecx, AF_PPP
|
|
|
|
jne .no_ppp
|
|
|
|
|
|
|
|
cmp esi, PPP_PROTO_ETHERNET
|
|
|
|
je .pppoe
|
|
|
|
|
|
|
|
.no_ppp:
|
2013-06-15 00:14:35 +04:00
|
|
|
.unsupported:
|
|
|
|
push eax
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_free
|
2013-06-15 00:14:35 +04:00
|
|
|
pop eax
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EOPNOTSUPP
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
.nobuffs:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], ENOBUFS
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.raw:
|
2015-12-17 00:20:53 +03:00
|
|
|
test esi, esi ; IP_PROTO_IP
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .raw_ip
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp esi, IP_PROTO_ICMP
|
2013-06-15 00:14:35 +04:00
|
|
|
je .raw_icmp
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
jmp .unsupported
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
align 4
|
|
|
|
.udp:
|
2015-10-11 21:23:40 +03:00
|
|
|
push eax
|
|
|
|
init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue
|
|
|
|
pop eax
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [eax + SOCKET.Protocol], IP_PROTO_UDP
|
2015-12-27 18:37:31 +03:00
|
|
|
mov [eax + SOCKET.snd_proc], socket_send_udp
|
|
|
|
mov [eax + SOCKET.rcv_proc], socket_receive_dgram
|
|
|
|
mov [eax + SOCKET.connect_proc], udp_connect
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
.tcp:
|
|
|
|
mov [eax + SOCKET.Protocol], IP_PROTO_TCP
|
2015-12-27 18:37:31 +03:00
|
|
|
mov [eax + SOCKET.snd_proc], socket_send_tcp
|
|
|
|
mov [eax + SOCKET.rcv_proc], socket_receive_stream
|
|
|
|
mov [eax + SOCKET.connect_proc], tcp_connect
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
tcp_init_socket eax
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
2013-06-15 00:14:35 +04:00
|
|
|
.raw_ip:
|
2015-10-11 21:23:40 +03:00
|
|
|
push eax
|
|
|
|
init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue
|
|
|
|
pop eax
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
mov [eax + SOCKET.snd_proc], socket_send_ip
|
|
|
|
mov [eax + SOCKET.rcv_proc], socket_receive_dgram
|
|
|
|
mov [eax + SOCKET.connect_proc], ipv4_connect
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
2013-06-15 00:14:35 +04:00
|
|
|
.raw_icmp:
|
2015-10-11 21:23:40 +03:00
|
|
|
push eax
|
|
|
|
init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue
|
|
|
|
pop eax
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
mov [eax + SOCKET.snd_proc], socket_send_icmp
|
|
|
|
mov [eax + SOCKET.rcv_proc], socket_receive_dgram
|
|
|
|
mov [eax + SOCKET.connect_proc], ipv4_connect
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
.pppoe:
|
|
|
|
push eax
|
|
|
|
init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue
|
|
|
|
pop eax
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
mov [eax + SOCKET.snd_proc], socket_send_pppoe
|
|
|
|
mov [eax + SOCKET.rcv_proc], socket_receive_dgram
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_bind: Bind to a local port. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ecx = socket number ;
|
|
|
|
; edx = pointer to sockaddr struct ;
|
|
|
|
; esi = length of sockaddr struct ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = 0 on success ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ebx = errorcode on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_bind:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_bind: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_num_to_ptr
|
2015-12-16 00:54:06 +03:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp esi, 2
|
2013-06-15 00:14:35 +04:00
|
|
|
jb .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
cmp [eax + UDP_SOCKET.LocalPort], 0 ; Socket can only be bound once
|
2013-10-19 19:47:58 +04:00
|
|
|
jnz .invalid
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
cmp word[edx], AF_INET4
|
2013-05-28 21:34:26 +04:00
|
|
|
je .af_inet4
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
cmp word[edx], AF_LOCAL
|
2013-05-28 21:34:26 +04:00
|
|
|
je .af_local
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.notsupp:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EOPNOTSUPP
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
.invalid:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EINVAL
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
.af_local:
|
|
|
|
; TODO: write code here
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], 0
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.af_inet4:
|
|
|
|
cmp esi, 6
|
2013-06-15 00:14:35 +04:00
|
|
|
jb .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_UDP
|
|
|
|
je .udp
|
|
|
|
|
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
|
|
|
|
je .tcp
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
jmp .notsupp
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
.tcp:
|
|
|
|
.udp:
|
2013-10-19 19:47:58 +04:00
|
|
|
pushd [edx + 4] ; First, fill in the IP
|
|
|
|
popd [eax + IP_SOCKET.LocalIP]
|
|
|
|
|
|
|
|
mov bx, [edx + 2] ; Did caller specify a local port?
|
|
|
|
test bx, bx
|
|
|
|
jnz .just_check
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_find_port ; Nope, find an ephemeral one
|
2013-10-19 19:47:58 +04:00
|
|
|
jmp .done
|
|
|
|
|
|
|
|
.just_check:
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_check_port ; Yes, check if it's still available
|
2013-10-19 19:47:58 +04:00
|
|
|
jz .addrinuse ; ZF is set by socket_check_port on error
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-10-19 19:47:58 +04:00
|
|
|
.done:
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_bind: local ip=%u.%u.%u.%u\n",\
|
2013-05-28 21:34:26 +04:00
|
|
|
[eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\
|
|
|
|
[eax + IP_SOCKET.LocalIP + 2]:1,[eax + IP_SOCKET.LocalIP + 3]:1
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], 0
|
|
|
|
ret
|
|
|
|
|
|
|
|
.addrinuse:
|
|
|
|
mov dword[esp+32], -1
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EADDRINUSE
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_connect: Connect to the remote host. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ecx = socket number ;
|
|
|
|
; edx = pointer to sockaddr struct ;
|
|
|
|
; esi = length of sockaddr struct ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = 0 on success ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ebx = errorcode on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_connect:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_connect: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_num_to_ptr
|
2015-12-16 00:54:06 +03:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp esi, 8
|
2013-06-15 00:14:35 +04:00
|
|
|
jb .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-10-14 11:08:51 +04:00
|
|
|
cmp [eax + SOCKET.state], SS_ISCONNECTING
|
|
|
|
je .already
|
|
|
|
|
|
|
|
test [eax + SOCKET.options], SO_ACCEPTCON
|
|
|
|
jnz .notsupp
|
|
|
|
|
2013-10-15 14:35:08 +04:00
|
|
|
call [eax + SOCKET.connect_proc]
|
|
|
|
|
|
|
|
mov dword[esp+20], ebx
|
|
|
|
mov dword[esp+32], eax
|
|
|
|
ret
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.notsupp:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EOPNOTSUPP
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
.invalid:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EINVAL
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-10-14 11:08:51 +04:00
|
|
|
.already:
|
|
|
|
mov dword[esp+20], EALREADY
|
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-10-15 14:35:08 +04:00
|
|
|
connect_notsupp:
|
|
|
|
xor eax, eax
|
|
|
|
dec eax
|
|
|
|
mov ebx, EOPNOTSUPP
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_listen: Listen for incoming connections. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ecx = socket number ;
|
|
|
|
; edx = backlog in edx ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = 0 on success ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ebx = errorcode on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_listen:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_listen: socknum=%u backlog=%u\n", ecx, edx
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_num_to_ptr
|
2015-12-16 00:54:06 +03:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp [eax + SOCKET.Domain], AF_INET4
|
2013-06-15 00:14:35 +04:00
|
|
|
jne .notsupp
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
|
2013-06-15 00:14:35 +04:00
|
|
|
jne .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp [eax + TCP_SOCKET.LocalPort], 0
|
2013-06-15 00:14:35 +04:00
|
|
|
je .already
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp [eax + IP_SOCKET.LocalIP], 0
|
|
|
|
jne @f
|
2013-06-05 04:21:20 +04:00
|
|
|
push [IP_LIST + 4] ;;; fixme!!!!
|
2013-05-28 21:34:26 +04:00
|
|
|
pop [eax + IP_SOCKET.LocalIP]
|
|
|
|
@@:
|
|
|
|
|
|
|
|
cmp edx, MAX_backlog
|
|
|
|
jbe @f
|
|
|
|
mov edx, MAX_backlog
|
|
|
|
@@:
|
|
|
|
|
|
|
|
mov [eax + SOCKET.backlog], dx
|
|
|
|
or [eax + SOCKET.options], SO_ACCEPTCON
|
|
|
|
mov [eax + TCP_SOCKET.t_state], TCPS_LISTEN
|
|
|
|
mov [eax + TCP_SOCKET.timer_keepalive], 0 ; disable keepalive timer
|
|
|
|
|
|
|
|
push eax
|
|
|
|
init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up sockets queue
|
|
|
|
pop eax
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], 0
|
|
|
|
ret
|
|
|
|
|
|
|
|
.notsupp:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EOPNOTSUPP
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.invalid:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EINVAL
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
.already:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EALREADY
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_accept: Accept an incoming connection. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ecx = socket number (of listening socket) ;
|
|
|
|
; edx = ptr to sockaddr struct ;
|
|
|
|
; esi = length of sockaddr struct ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = newly created socket num ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ebx = errorcode on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_accept:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_accept: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_num_to_ptr
|
2015-12-16 00:54:06 +03:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
test [eax + SOCKET.options], SO_ACCEPTCON
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp [eax + SOCKET.Domain], AF_INET4
|
2013-06-15 00:14:35 +04:00
|
|
|
jne .notsupp
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
|
2013-06-15 00:14:35 +04:00
|
|
|
jne .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
.loop:
|
|
|
|
get_from_queue (eax + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .block
|
|
|
|
|
|
|
|
; Ok, we got a socket ptr
|
|
|
|
mov eax, [esi]
|
|
|
|
|
2015-12-16 00:54:06 +03:00
|
|
|
; Verify that it is (still) a valid socket
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_check
|
2015-12-16 00:54:06 +03:00
|
|
|
jz .invalid
|
|
|
|
|
|
|
|
; Change sockets thread owner ID to that of the current thread
|
2013-05-28 21:34:26 +04:00
|
|
|
mov ebx, [TASK_BASE]
|
|
|
|
mov ebx, [ebx + TASKDATA.pid]
|
|
|
|
mov [eax + SOCKET.TID], ebx
|
|
|
|
|
2015-12-16 00:54:06 +03:00
|
|
|
; Return socket number to caller
|
|
|
|
mov eax, [eax + SOCKET.Number]
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [esp+32], eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.block:
|
|
|
|
test [eax + SOCKET.options], SO_NONBLOCK
|
2013-06-15 00:14:35 +04:00
|
|
|
jnz .wouldblock
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_block
|
2013-05-28 21:34:26 +04:00
|
|
|
jmp .loop
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.wouldblock:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EWOULDBLOCK
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
.invalid:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EINVAL
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
.notsupp:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EOPNOTSUPP
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_close: Close the socket (and connection). ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ecx = socket number ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = 0 on success ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ebx = errorcode on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_close:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_close: socknum=%u\n", ecx
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_num_to_ptr
|
2015-12-16 00:54:06 +03:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], 0 ; The socket exists, so we will succeed in closing it.
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
or [eax + SOCKET.options], SO_NONBLOCK ; Mark the socket as non blocking, we dont want it to block any longer!
|
|
|
|
|
|
|
|
test [eax + SOCKET.state], SS_BLOCKED ; Is the socket still in blocked state?
|
|
|
|
jz @f
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_notify ; Unblock it.
|
2013-05-28 21:34:26 +04:00
|
|
|
@@:
|
|
|
|
|
|
|
|
cmp [eax + SOCKET.Domain], AF_INET4
|
|
|
|
jne .free
|
|
|
|
|
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
|
|
|
|
je .tcp
|
|
|
|
|
|
|
|
.free:
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_free
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.tcp:
|
2015-12-27 18:37:31 +03:00
|
|
|
call tcp_usrclosed
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-12-16 01:27:22 +04:00
|
|
|
test eax, eax
|
|
|
|
jz @f
|
2015-12-27 18:37:31 +03:00
|
|
|
call tcp_output ; If connection is not closed yet, send the FIN
|
2013-12-16 01:27:22 +04:00
|
|
|
@@:
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.invalid:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EINVAL
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_receive: Receive some data from the remote end. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ecx = socket number ;
|
|
|
|
; edx = addr to application buffer ;
|
|
|
|
; edx = length of application buffer ;
|
|
|
|
; edi = flags ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = number of bytes copied ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; eax = 0 when socket has been closed by the remote end ;
|
|
|
|
; ebx = errorcode on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_receive:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: socknum=%u bufaddr=%x buflength=%u flags=%x\n", ecx, edx, esi, edi
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_num_to_ptr
|
2015-12-16 00:54:06 +03:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-26 00:02:56 +04:00
|
|
|
.loop:
|
|
|
|
push edi
|
2013-05-29 15:46:06 +04:00
|
|
|
call [eax + SOCKET.rcv_proc]
|
2013-06-26 00:02:56 +04:00
|
|
|
pop edi
|
|
|
|
|
2013-11-13 00:05:57 +04:00
|
|
|
test [eax + SOCKET.state], SS_CANTRCVMORE
|
2015-01-09 02:32:03 +03:00
|
|
|
jnz .last_data
|
2013-11-13 00:05:57 +04:00
|
|
|
|
2013-06-26 00:02:56 +04:00
|
|
|
cmp ebx, EWOULDBLOCK
|
|
|
|
jne .return
|
|
|
|
|
|
|
|
test edi, MSG_DONTWAIT
|
|
|
|
jnz .return_err
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
test [eax + SOCKET.options], SO_NONBLOCK
|
|
|
|
jnz .return_err
|
2013-06-26 00:02:56 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_block
|
2013-06-26 00:02:56 +04:00
|
|
|
jmp .loop
|
2013-05-29 15:46:06 +04:00
|
|
|
|
2013-06-26 00:02:56 +04:00
|
|
|
|
|
|
|
.invalid:
|
|
|
|
push EINVAL
|
|
|
|
pop ebx
|
|
|
|
.return_err:
|
2013-11-13 00:05:57 +04:00
|
|
|
mov ecx, -1
|
2013-06-26 00:02:56 +04:00
|
|
|
.return:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov [esp+20], ebx
|
2013-11-13 00:05:57 +04:00
|
|
|
mov [esp+32], ecx
|
2013-05-29 15:46:06 +04:00
|
|
|
ret
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-01-09 02:32:03 +03:00
|
|
|
.last_data:
|
|
|
|
test ecx, ecx
|
|
|
|
jz .return
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_notify ; Call me again!
|
2015-01-09 02:32:03 +03:00
|
|
|
jmp .return
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-26 00:02:56 +04:00
|
|
|
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_receive_dgram:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-26 00:02:56 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: DGRAM\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
test edi, MSG_PEEK
|
|
|
|
jnz .peek
|
|
|
|
|
|
|
|
mov ebx, esi ; buffer length
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-26 00:02:56 +04:00
|
|
|
get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .wouldblock ; sets esi only on success.
|
2013-05-28 21:34:26 +04:00
|
|
|
mov ecx, [esi + socket_queue_entry.data_size]
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: %u bytes data\n", ecx
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-26 00:02:56 +04:00
|
|
|
cmp ecx, ebx ; If data segment does not fit in applications buffer, abort
|
2013-05-28 21:34:26 +04:00
|
|
|
ja .too_small
|
|
|
|
|
2013-11-13 00:05:57 +04:00
|
|
|
push eax ecx
|
2013-05-28 21:34:26 +04:00
|
|
|
push [esi + socket_queue_entry.buf_ptr] ; save the buffer addr so we can clear it later
|
|
|
|
mov esi, [esi + socket_queue_entry.data_ptr]
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: Source buffer=%x real addr=%x\n", [esp], esi
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-26 00:02:56 +04:00
|
|
|
; copy the data from kernel buffer to application buffer
|
|
|
|
mov edi, edx ; bufferaddr
|
2013-05-28 21:34:26 +04:00
|
|
|
shr ecx, 1
|
|
|
|
jnc .nb
|
|
|
|
movsb
|
|
|
|
.nb:
|
|
|
|
shr ecx, 1
|
|
|
|
jnc .nw
|
|
|
|
movsw
|
|
|
|
.nw:
|
|
|
|
test ecx, ecx
|
|
|
|
jz .nd
|
2013-06-27 03:35:43 +04:00
|
|
|
rep movsd
|
2013-05-28 21:34:26 +04:00
|
|
|
.nd:
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call net_buff_free
|
2013-11-13 00:05:57 +04:00
|
|
|
pop ecx eax ; return number of bytes copied to application
|
2015-12-17 00:20:53 +03:00
|
|
|
cmp [eax + SOCKET_QUEUE_LOCATION + queue.size], 0
|
|
|
|
je @f
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_notify ; Queue another network event
|
2015-12-17 00:20:53 +03:00
|
|
|
@@:
|
|
|
|
xor ebx, ebx ; errorcode = 0 (no error)
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.too_small:
|
2013-11-13 00:05:57 +04:00
|
|
|
mov ecx, -1
|
2013-06-26 00:02:56 +04:00
|
|
|
push EMSGSIZE
|
|
|
|
pop ebx
|
2013-05-29 15:46:06 +04:00
|
|
|
ret
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.wouldblock:
|
2013-06-26 00:02:56 +04:00
|
|
|
push EWOULDBLOCK
|
|
|
|
pop ebx
|
2013-06-15 00:14:35 +04:00
|
|
|
ret
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
.peek:
|
|
|
|
xor ebx, ebx
|
|
|
|
xor ecx, ecx
|
|
|
|
cmp [eax + SOCKET_QUEUE_LOCATION + queue.size], 0
|
|
|
|
je @f
|
|
|
|
mov esi, [eax + SOCKET_QUEUE_LOCATION + queue.r_ptr]
|
|
|
|
mov ecx, [esi + socket_queue_entry.data_size]
|
|
|
|
@@:
|
|
|
|
ret
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-26 00:02:56 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_receive_local:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; does this socket have a PID yet?
|
|
|
|
cmp [eax + SOCKET.PID], 0
|
|
|
|
jne @f
|
|
|
|
|
|
|
|
; Change PID to that of current process
|
|
|
|
mov ebx, [TASK_BASE]
|
|
|
|
mov ebx, [ebx + TASKDATA.pid]
|
|
|
|
mov [eax + SOCKET.PID], ebx
|
2013-06-26 00:02:56 +04:00
|
|
|
mov [eax + SOCKET.TID], ebx ; currently TID = PID in kolibrios :(
|
2013-05-28 21:34:26 +04:00
|
|
|
@@:
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
mov [eax + SOCKET.rcv_proc], socket_receive_stream
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-26 00:02:56 +04:00
|
|
|
; ... continue to SOCKET_receive_stream
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_receive_stream:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: STREAM\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-26 00:02:56 +04:00
|
|
|
cmp [eax + STREAM_SOCKET.rcv + RING_BUFFER.size], 0
|
|
|
|
je .wouldblock
|
|
|
|
|
|
|
|
test edi, MSG_PEEK
|
|
|
|
jnz .peek
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov ecx, esi
|
|
|
|
mov edi, edx
|
|
|
|
xor edx, edx
|
|
|
|
|
2013-11-13 00:05:57 +04:00
|
|
|
push eax
|
2013-05-28 21:34:26 +04:00
|
|
|
add eax, STREAM_SOCKET.rcv
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_ring_read ; copy data from kernel buffer to application buffer
|
|
|
|
call socket_ring_free ; free read memory
|
2013-11-13 00:05:57 +04:00
|
|
|
pop eax
|
2013-05-29 15:46:06 +04:00
|
|
|
|
2015-01-09 02:32:03 +03:00
|
|
|
cmp [eax + STREAM_SOCKET.rcv + RING_BUFFER.size], 0
|
|
|
|
jne .more_data
|
|
|
|
xor ebx, ebx ; errorcode = 0 (no error)
|
|
|
|
ret
|
|
|
|
|
|
|
|
.more_data:
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_notify ; Queue another network event
|
2013-06-26 00:02:56 +04:00
|
|
|
xor ebx, ebx ; errorcode = 0 (no error)
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.wouldblock:
|
2013-06-26 00:02:56 +04:00
|
|
|
push EWOULDBLOCK
|
|
|
|
pop ebx
|
2013-11-13 00:05:57 +04:00
|
|
|
xor ecx, ecx
|
2013-06-15 00:14:35 +04:00
|
|
|
ret
|
|
|
|
|
2013-06-26 00:02:56 +04:00
|
|
|
.peek:
|
2013-11-13 00:05:57 +04:00
|
|
|
mov ecx, [eax + STREAM_SOCKET.rcv + RING_BUFFER.size]
|
2013-06-26 00:02:56 +04:00
|
|
|
xor ebx, ebx
|
|
|
|
ret
|
2013-06-15 00:14:35 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_send: Send some data to the remote end. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ecx = socket number ;
|
|
|
|
; edx = pointer to data ;
|
|
|
|
; esi = data length ;
|
|
|
|
; edi = flags ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = number of bytes sent ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ebx = errorcode on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_send:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: socknum=%u data ptr=%x length=%u flags=%x\n", ecx, edx, esi, edi
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_num_to_ptr
|
2015-12-16 00:54:06 +03:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov ecx, esi
|
|
|
|
mov esi, edx
|
|
|
|
|
|
|
|
jmp [eax + SOCKET.snd_proc]
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.invalid:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EINVAL
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_send_udp:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: UDP\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov [esp+32], ecx
|
2015-12-27 18:37:31 +03:00
|
|
|
call udp_output
|
2013-05-28 21:34:26 +04:00
|
|
|
cmp eax, -1
|
2013-06-15 00:14:35 +04:00
|
|
|
je .error
|
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
|
|
|
mov dword[esp+32], -1
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EMSGSIZE ; FIXME: UDP_output should return error codes!
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_send_tcp:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: TCP\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
push eax
|
|
|
|
add eax, STREAM_SOCKET.snd
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_ring_write
|
2013-05-28 21:34:26 +04:00
|
|
|
pop eax
|
|
|
|
|
|
|
|
mov [esp+32], ecx
|
2013-06-15 00:14:35 +04:00
|
|
|
mov [eax + SOCKET.errorcode], 0
|
|
|
|
push eax
|
2015-12-27 18:37:31 +03:00
|
|
|
call tcp_output ; FIXME: this doesnt look pretty, does it?
|
2013-06-15 00:14:35 +04:00
|
|
|
pop eax
|
|
|
|
mov eax, [eax + SOCKET.errorcode]
|
2013-06-17 17:59:20 +04:00
|
|
|
mov [esp+20], eax
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_send_ip:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: IPv4\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov [esp+32], ecx
|
2015-12-27 18:37:31 +03:00
|
|
|
call ipv4_output_raw
|
2013-05-28 21:34:26 +04:00
|
|
|
cmp eax, -1
|
2013-06-15 00:14:35 +04:00
|
|
|
je .error
|
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
2015-10-11 21:23:40 +03:00
|
|
|
mov dword[esp+32], eax
|
|
|
|
mov dword[esp+20], ebx
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_send_icmp:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: ICMP\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov [esp+32], ecx
|
2015-12-27 18:37:31 +03:00
|
|
|
call icmp_output_raw
|
2013-05-28 21:34:26 +04:00
|
|
|
cmp eax, -1
|
2013-06-15 00:14:35 +04:00
|
|
|
je .error
|
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
2015-10-11 21:23:40 +03:00
|
|
|
mov dword[esp+32], eax
|
|
|
|
mov dword[esp+20], ebx
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_send_pppoe:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: PPPoE\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov [esp+32], ecx
|
|
|
|
mov ebx, [eax + SOCKET.device]
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call pppoe_discovery_output ; FIXME: errorcodes
|
2013-05-28 21:34:26 +04:00
|
|
|
cmp eax, -1
|
2013-06-15 00:14:35 +04:00
|
|
|
je .error
|
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
|
|
|
mov dword[esp+32], -1
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EMSGSIZE
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_send_local:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; does this socket have a PID yet?
|
|
|
|
cmp [eax + SOCKET.PID], 0
|
|
|
|
jne @f
|
|
|
|
|
|
|
|
; Change PID to that of current process
|
|
|
|
mov ebx, [TASK_BASE]
|
|
|
|
mov ebx, [ebx + TASKDATA.pid]
|
|
|
|
mov [eax + SOCKET.PID], ebx
|
|
|
|
mov [eax + SOCKET.TID], ebx ; currently TID = PID in kolibrios :(
|
|
|
|
@@:
|
2015-12-27 18:37:31 +03:00
|
|
|
mov [eax + SOCKET.snd_proc], socket_send_local_initialized
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_send_local_initialized:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: LOCAL\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; get the other side's socket and check if it still exists
|
|
|
|
mov eax, [eax + SOCKET.device]
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_check
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; allright, shove in the data!
|
|
|
|
push eax
|
|
|
|
add eax, STREAM_SOCKET.rcv
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_ring_write
|
2013-05-28 21:34:26 +04:00
|
|
|
pop eax
|
|
|
|
|
|
|
|
; return the number of written bytes (or errorcode) to application
|
|
|
|
mov [esp+32], ecx
|
|
|
|
|
|
|
|
; and notify the other end
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_notify
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.invalid:
|
|
|
|
mov dword[esp+32], -1
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EINVAL
|
2013-06-15 00:14:35 +04:00
|
|
|
ret
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_get_opt: Read a socket option ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ecx = socket number ;
|
|
|
|
; edx = pointer to socket options struct ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = 0 on success ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ebx = errorcode on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_get_opt:
|
2015-12-17 00:20:53 +03:00
|
|
|
|
|
|
|
; FIXME:
|
2013-05-28 21:34:26 +04:00
|
|
|
; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
|
|
|
|
; TODO: find best way to notify that send()'ed data were acknowledged
|
|
|
|
; Also pseudo-optname -3 is valid and returns socket state, one of TCPS_*.
|
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_get_opt\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_num_to_ptr
|
2015-12-16 00:54:06 +03:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
cmp dword [edx], IP_PROTO_TCP
|
2013-06-15 00:14:35 +04:00
|
|
|
jne .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
cmp dword [edx+4], -2
|
|
|
|
je @f
|
|
|
|
cmp dword [edx+4], -3
|
2013-06-15 00:14:35 +04:00
|
|
|
jne .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
@@:
|
|
|
|
; mov eax, [edx+12]
|
|
|
|
; test eax, eax
|
|
|
|
; jz .fail
|
|
|
|
; cmp dword [eax], 4
|
|
|
|
; mov dword [eax], 4
|
|
|
|
; jb .fail
|
|
|
|
; stdcall net_socket_num_to_addr, ecx
|
|
|
|
; test eax, eax
|
|
|
|
; jz .fail
|
|
|
|
; ; todo: check that eax is really TCP socket
|
|
|
|
; mov ecx, [eax + TCP_SOCKET.last_ack_number]
|
|
|
|
; cmp dword [edx+4], -2
|
|
|
|
; jz @f
|
|
|
|
; mov ecx, [eax + TCP_SOCKET.state]
|
|
|
|
@@:
|
|
|
|
mov eax, [edx+8]
|
|
|
|
test eax, eax
|
|
|
|
jz @f
|
|
|
|
mov [eax], ecx
|
|
|
|
@@:
|
|
|
|
mov dword [esp+32], 0
|
|
|
|
ret
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.invalid:
|
|
|
|
mov dword[esp+32], -1
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EINVAL
|
2013-06-15 00:14:35 +04:00
|
|
|
ret
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_set_options: Set a socket option. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ecx = socket number ;
|
|
|
|
; edx = pointer to socket options struct ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = 0 on success ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ebx = errorcode on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_set_opt:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_num_to_ptr
|
2015-12-16 00:54:06 +03:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-10-11 21:23:40 +03:00
|
|
|
cmp [edx + socket_options.level], IP_PROTO_IP
|
|
|
|
je .ip
|
|
|
|
cmp [edx + socket_options.level], SOL_SOCKET
|
2013-06-15 00:14:35 +04:00
|
|
|
jne .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-10-11 21:23:40 +03:00
|
|
|
.socket:
|
|
|
|
cmp [edx + socket_options.optname], SO_BINDTODEVICE
|
|
|
|
jne .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
.bind:
|
2015-10-11 21:23:40 +03:00
|
|
|
cmp [edx + socket_options.optlen], 0
|
2013-05-28 21:34:26 +04:00
|
|
|
je .unbind
|
|
|
|
|
2015-10-11 21:23:40 +03:00
|
|
|
movzx edx, byte[edx + socket_options.optval]
|
2013-06-04 18:12:37 +04:00
|
|
|
cmp edx, NET_DEVICES_MAX
|
2013-06-15 00:14:35 +04:00
|
|
|
ja .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov edx, [NET_DRV_LIST + 4*edx]
|
|
|
|
test edx, edx
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .already
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [eax + SOCKET.device], edx
|
|
|
|
|
2015-07-20 14:13:42 +03:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt: Bound socket %x to device %x\n", eax, edx
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], 0 ; success!
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.unbind:
|
|
|
|
mov [eax + SOCKET.device], 0
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], 0 ; success!
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
2015-10-11 21:23:40 +03:00
|
|
|
.ip:
|
|
|
|
cmp [edx + socket_options.optname], IP_TTL
|
|
|
|
jne .invalid
|
|
|
|
|
|
|
|
.ttl:
|
|
|
|
mov bl, byte[edx + socket_options.optval]
|
|
|
|
mov [eax + IP_SOCKET.ttl], bl
|
|
|
|
|
|
|
|
mov dword[esp+32], 0 ; success!
|
|
|
|
ret
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.already:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], EALREADY
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
2015-10-11 21:23:40 +03:00
|
|
|
.invalid:
|
|
|
|
mov dword[esp+20], EINVAL
|
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_pair: Allocate a pair of linked local sockets. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: / ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = socket1 num on success ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ebx = socket2 num on success ;
|
|
|
|
; ebx = errorcode on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_pair:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_pair\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_alloc
|
2014-01-27 14:27:38 +04:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .nomem1
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [esp+32], edi ; application's eax
|
|
|
|
|
|
|
|
mov [eax + SOCKET.Domain], AF_LOCAL
|
|
|
|
mov [eax + SOCKET.Type], SOCK_STREAM
|
|
|
|
mov [eax + SOCKET.Protocol], 0 ;;; CHECKME
|
2015-12-27 18:37:31 +03:00
|
|
|
mov [eax + SOCKET.snd_proc], socket_send_local
|
|
|
|
mov [eax + SOCKET.rcv_proc], socket_receive_local
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [eax + SOCKET.PID], 0
|
|
|
|
mov ebx, eax
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_alloc
|
2014-01-27 14:27:38 +04:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .nomem2
|
2013-06-17 17:59:20 +04:00
|
|
|
mov [esp+20], edi ; application's ebx
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov [eax + SOCKET.Domain], AF_LOCAL
|
|
|
|
mov [eax + SOCKET.Type], SOCK_STREAM
|
|
|
|
mov [eax + SOCKET.Protocol], 0 ;;; CHECKME
|
2015-12-27 18:37:31 +03:00
|
|
|
mov [eax + SOCKET.snd_proc], socket_send_local
|
|
|
|
mov [eax + SOCKET.rcv_proc], socket_receive_local
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [eax + SOCKET.PID], 0
|
|
|
|
|
|
|
|
; Link the two sockets to eachother
|
|
|
|
mov [eax + SOCKET.device], ebx
|
|
|
|
mov [ebx + SOCKET.device], eax
|
|
|
|
|
|
|
|
lea eax, [eax + STREAM_SOCKET.rcv]
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_ring_create
|
2014-11-01 21:27:28 +03:00
|
|
|
test eax, eax
|
2015-12-16 00:54:06 +03:00
|
|
|
jz .nomem2
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
lea eax, [ebx + STREAM_SOCKET.rcv]
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_ring_create
|
2014-11-01 21:27:28 +03:00
|
|
|
test eax, eax
|
|
|
|
jz .nomem2
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.nomem2:
|
2015-12-16 00:54:06 +03:00
|
|
|
mov eax, [esp+20]
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_free
|
2015-12-16 00:54:06 +03:00
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.nomem1:
|
2015-12-16 00:54:06 +03:00
|
|
|
mov eax, [esp+32]
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_free
|
2015-12-16 00:54:06 +03:00
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
2015-12-16 00:54:06 +03:00
|
|
|
mov dword[esp+20], ENOMEM
|
2013-06-15 00:14:35 +04:00
|
|
|
ret
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_debug: Copy socket variables to application buffer. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ecx = socket number ;
|
|
|
|
; edx = pointer to application buffer ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = 0 on success ;
|
|
|
|
; eax = -1 on error ;
|
|
|
|
; ebx = errorcode on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_debug:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_debug\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov edi, edx
|
|
|
|
|
|
|
|
test ecx, ecx
|
|
|
|
jz .returnall
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_num_to_ptr
|
2015-12-16 00:54:06 +03:00
|
|
|
test eax, eax
|
2013-06-15 00:14:35 +04:00
|
|
|
jz .invalid
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
mov esi, eax
|
2016-05-01 00:15:55 +03:00
|
|
|
mov ecx, SOCKET_STRUCT_SIZE/4
|
2013-06-27 03:35:43 +04:00
|
|
|
rep movsd
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], 0
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.returnall:
|
|
|
|
mov ebx, net_sockets
|
|
|
|
.next_socket:
|
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
|
|
|
test ebx, ebx
|
|
|
|
jz .done
|
|
|
|
mov eax, [ebx + SOCKET.Number]
|
|
|
|
stosd
|
|
|
|
jmp .next_socket
|
|
|
|
.done:
|
|
|
|
xor eax, eax
|
|
|
|
stosd
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], eax
|
|
|
|
ret
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.invalid:
|
|
|
|
mov dword[esp+32], -1
|
2015-12-16 00:54:06 +03:00
|
|
|
mov dword[esp+20], EINVAL
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ____ ____ ;
|
|
|
|
; \ / End of sockets API \ / ;
|
|
|
|
; \/ \/ ;
|
|
|
|
; () Internally used functions follow () ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_find_port: ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; Fill in the local port number for TCP and UDP sockets ;
|
|
|
|
; This procedure always works because the number of sockets is ;
|
|
|
|
; limited to a smaller number then the number of possible ports ;
|
|
|
|
; ;
|
|
|
|
; IN: eax = socket pointer ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_find_port:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_find_port\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
push ebx esi ecx
|
|
|
|
|
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_UDP
|
|
|
|
je .udp
|
|
|
|
|
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
|
|
|
|
je .tcp
|
|
|
|
|
|
|
|
pop ecx esi ebx
|
|
|
|
ret
|
|
|
|
|
|
|
|
.udp:
|
|
|
|
mov bx, [last_UDP_port]
|
|
|
|
call .findit
|
|
|
|
mov [last_UDP_port], bx
|
|
|
|
|
|
|
|
pop ecx esi ebx
|
|
|
|
ret
|
|
|
|
|
|
|
|
.tcp:
|
|
|
|
mov bx, [last_TCP_port]
|
|
|
|
call .findit
|
|
|
|
mov [last_TCP_port], bx
|
|
|
|
|
|
|
|
pop ecx esi ebx
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
.restart:
|
|
|
|
mov bx, MIN_EPHEMERAL_PORT_N
|
|
|
|
.findit:
|
|
|
|
cmp bx, MAX_EPHEMERAL_PORT_N
|
|
|
|
je .restart
|
|
|
|
|
|
|
|
add bh, 1
|
|
|
|
adc bl, 0
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_check_port
|
2013-05-28 21:34:26 +04:00
|
|
|
jz .findit
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_check_port: (to be used with AF_INET only!) ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; Checks if a local port number is unused ;
|
|
|
|
; If the proposed port number is unused, it is filled in in the ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket structure. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; bx = proposed socket number (network byte order) ;
|
|
|
|
; ;
|
|
|
|
; OUT: ZF = set on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_check_port:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check_port: "
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-12 14:21:41 +04:00
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov ecx, [eax + SOCKET.Protocol]
|
|
|
|
mov edx, [eax + IP_SOCKET.LocalIP]
|
|
|
|
mov esi, net_sockets
|
|
|
|
|
|
|
|
.next_socket:
|
|
|
|
mov esi, [esi + SOCKET.NextPtr]
|
|
|
|
or esi, esi
|
|
|
|
jz .port_ok
|
|
|
|
|
|
|
|
cmp [esi + SOCKET.Protocol], ecx
|
|
|
|
jne .next_socket
|
|
|
|
|
|
|
|
cmp [esi + IP_SOCKET.LocalIP], edx
|
|
|
|
jne .next_socket
|
|
|
|
|
|
|
|
cmp [esi + UDP_SOCKET.LocalPort], bx
|
|
|
|
jne .next_socket
|
|
|
|
|
2013-06-12 14:21:41 +04:00
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "local port %x already in use\n", bx ; FIXME: find a way to print big endian values with debugf
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.port_ok:
|
2013-06-12 14:21:41 +04:00
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "local port %x is free\n", bx ; FIXME: find a way to print big endian values with debugf
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [eax + UDP_SOCKET.LocalPort], bx
|
|
|
|
or bx, bx ; clear the zero-flag
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_input: Update a (stateless) socket with received data. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; Note: The socket's mutex should already be set ! ;
|
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ecx = data size ;
|
|
|
|
; esi = ptr to data ;
|
|
|
|
; [esp] = ptr to buf ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_input:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-03-18 00:50:29 +03:00
|
|
|
push ecx
|
2013-05-28 21:34:26 +04:00
|
|
|
push esi
|
|
|
|
mov esi, esp
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .full
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_input: success\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
add esp, sizeof.socket_queue_entry
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
pusha
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
jmp socket_notify
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
.full:
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_input: socket %x is full!\n", eax
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
pusha
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2015-10-11 21:23:40 +03:00
|
|
|
add esp, 8
|
2015-12-27 18:37:31 +03:00
|
|
|
call net_buff_free
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_ring_create: Create a ringbuffer for sockets. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = ptr to ring struct ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = 0 on error ;
|
|
|
|
; eax = start ptr ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_ring_create:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
push esi
|
|
|
|
mov esi, eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
push edx
|
2016-05-01 00:15:55 +03:00
|
|
|
stdcall create_ring_buffer, SOCKET_BUFFER_SIZE, PG_SWR
|
2013-05-28 21:34:26 +04:00
|
|
|
pop edx
|
2014-11-01 21:27:28 +03:00
|
|
|
test eax, eax
|
|
|
|
jz .fail
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-16 00:54:06 +03:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_create: %x\n", eax
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
pusha
|
|
|
|
lea ecx, [esi + RING_BUFFER.mutex]
|
2011-10-15 01:38:50 +04:00
|
|
|
call mutex_init
|
2013-05-28 21:34:26 +04:00
|
|
|
popa
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [esi + RING_BUFFER.start_ptr], eax
|
|
|
|
mov [esi + RING_BUFFER.write_ptr], eax
|
|
|
|
mov [esi + RING_BUFFER.read_ptr], eax
|
|
|
|
mov [esi + RING_BUFFER.size], 0
|
2016-05-01 00:15:55 +03:00
|
|
|
add eax, SOCKET_BUFFER_SIZE
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [esi + RING_BUFFER.end_ptr], eax
|
|
|
|
mov eax, esi
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2015-12-16 00:54:06 +03:00
|
|
|
pop esi
|
|
|
|
ret
|
|
|
|
|
2014-11-01 21:27:28 +03:00
|
|
|
.fail:
|
2015-12-16 00:54:06 +03:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_ring_create: Out of memory!\n"
|
2015-06-02 00:44:10 +03:00
|
|
|
pop esi
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_ring_write: Write data to ring buffer. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = ptr to ring struct ;
|
|
|
|
; ecx = data size ;
|
|
|
|
; esi = ptr to data ;
|
|
|
|
; ;
|
|
|
|
; OUT: ecx = number of bytes stored ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_ring_write:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; lock mutex
|
|
|
|
pusha
|
|
|
|
lea ecx, [eax + RING_BUFFER.mutex]
|
|
|
|
call mutex_lock ; TODO: check what registers this function actually destroys
|
|
|
|
popa
|
|
|
|
|
|
|
|
; calculate available size
|
2016-05-01 00:15:55 +03:00
|
|
|
mov edi, SOCKET_BUFFER_SIZE
|
2013-05-28 21:34:26 +04:00
|
|
|
sub edi, [eax + RING_BUFFER.size] ; available buffer size in edi
|
|
|
|
cmp ecx, edi
|
|
|
|
jbe .copy
|
|
|
|
mov ecx, edi
|
|
|
|
.copy:
|
|
|
|
mov edi, [eax + RING_BUFFER.write_ptr]
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; update write ptr
|
|
|
|
push edi
|
|
|
|
add edi, ecx
|
|
|
|
cmp edi, [eax + RING_BUFFER.end_ptr]
|
|
|
|
jb @f
|
2016-05-01 00:15:55 +03:00
|
|
|
sub edi, SOCKET_BUFFER_SIZE ; WRAP
|
2013-05-28 21:34:26 +04:00
|
|
|
@@:
|
2014-11-01 21:27:28 +03:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [eax + RING_BUFFER.write_ptr], edi
|
|
|
|
pop edi
|
|
|
|
|
|
|
|
; update size
|
|
|
|
add [eax + RING_BUFFER.size], ecx
|
|
|
|
|
|
|
|
; copy the data
|
|
|
|
push ecx
|
|
|
|
shr ecx, 1
|
|
|
|
jnc .nb
|
|
|
|
movsb
|
|
|
|
.nb:
|
|
|
|
shr ecx, 1
|
|
|
|
jnc .nw
|
|
|
|
movsw
|
|
|
|
.nw:
|
|
|
|
test ecx, ecx
|
|
|
|
jz .nd
|
2013-06-27 03:35:43 +04:00
|
|
|
rep movsd
|
2013-05-28 21:34:26 +04:00
|
|
|
.nd:
|
|
|
|
pop ecx
|
|
|
|
|
|
|
|
; unlock mutex
|
2013-12-12 15:32:03 +04:00
|
|
|
pusha
|
2013-05-28 21:34:26 +04:00
|
|
|
lea ecx, [eax + RING_BUFFER.mutex]
|
|
|
|
call mutex_unlock ; TODO: check what registers this function actually destroys
|
2013-12-12 15:32:03 +04:00
|
|
|
popa
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_ring_read: Read from ring buffer ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = ring struct ptr ;
|
|
|
|
; ecx = bytes to read ;
|
|
|
|
; edx = offset ;
|
|
|
|
; edi = ptr to buffer start ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = unchanged ;
|
|
|
|
; ecx = number of bytes read (0 on error) ;
|
|
|
|
; edx = destroyed ;
|
|
|
|
; esi = destroyed ;
|
|
|
|
; edi = ptr to buffer end ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_ring_read:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: ringbuff=%x ptr=%x size=%u offset=%x\n", eax, edi, ecx, edx
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
pusha
|
|
|
|
lea ecx, [eax + RING_BUFFER.mutex]
|
|
|
|
call mutex_lock ; TODO: check what registers this function actually destroys
|
|
|
|
popa
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov esi, [eax + RING_BUFFER.read_ptr]
|
|
|
|
add esi, edx ; esi = start_ptr + offset
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
neg edx
|
|
|
|
add edx, [eax + RING_BUFFER.size] ; edx = snd.size - offset
|
|
|
|
jle .no_data_at_all
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
pusha
|
|
|
|
lea ecx, [eax + RING_BUFFER.mutex]
|
|
|
|
call mutex_unlock ; TODO: check what registers this function actually destroys
|
|
|
|
popa
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
cmp ecx, edx
|
|
|
|
ja .less_data
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
.copy:
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi
|
2013-05-28 21:34:26 +04:00
|
|
|
push ecx
|
|
|
|
shr ecx, 1
|
|
|
|
jnc .nb
|
|
|
|
movsb
|
|
|
|
.nb:
|
|
|
|
shr ecx, 1
|
|
|
|
jnc .nw
|
|
|
|
movsw
|
|
|
|
.nw:
|
|
|
|
test ecx, ecx
|
|
|
|
jz .nd
|
2013-06-27 03:35:43 +04:00
|
|
|
rep movsd
|
2013-05-28 21:34:26 +04:00
|
|
|
.nd:
|
|
|
|
pop ecx
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
.no_data_at_all:
|
|
|
|
pusha
|
|
|
|
lea ecx, [eax + RING_BUFFER.mutex]
|
|
|
|
call mutex_unlock ; TODO: check what registers this function actually destroys
|
|
|
|
popa
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: no data at all!\n"
|
2013-05-28 21:34:26 +04:00
|
|
|
xor ecx, ecx
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
.less_data:
|
|
|
|
mov ecx, edx
|
|
|
|
jmp .copy
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_ring_free: Free data from a ringbuffer. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = ptr to ring struct ;
|
|
|
|
; ecx = data size ;
|
|
|
|
; ;
|
|
|
|
; OUT: ecx = number of freed bytes ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_ring_free:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2011-10-15 01:38:50 +04:00
|
|
|
push eax ecx
|
2013-05-28 21:34:26 +04:00
|
|
|
lea ecx, [eax + RING_BUFFER.mutex]
|
|
|
|
call mutex_lock ; TODO: check what registers this function actually destroys
|
2011-10-15 01:38:50 +04:00
|
|
|
pop ecx eax
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
sub [eax + RING_BUFFER.size], ecx
|
|
|
|
jb .error
|
|
|
|
add [eax + RING_BUFFER.read_ptr], ecx
|
|
|
|
|
|
|
|
mov edx, [eax + RING_BUFFER.end_ptr]
|
|
|
|
cmp [eax + RING_BUFFER.read_ptr], edx
|
2011-10-15 01:38:50 +04:00
|
|
|
jb @f
|
2016-05-01 00:15:55 +03:00
|
|
|
sub [eax + RING_BUFFER.read_ptr], SOCKET_BUFFER_SIZE
|
2013-05-28 21:34:26 +04:00
|
|
|
@@:
|
|
|
|
|
|
|
|
push eax ecx
|
|
|
|
lea ecx, [eax + RING_BUFFER.mutex] ; TODO: check what registers this function actually destroys
|
|
|
|
call mutex_unlock
|
2011-10-15 01:38:50 +04:00
|
|
|
pop ecx eax
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.error: ; we could free all available bytes, but that would be stupid, i guess..
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_free: buffer=%x error!\n", eax
|
2013-05-28 21:34:26 +04:00
|
|
|
add [eax + RING_BUFFER.size], ecx
|
|
|
|
|
|
|
|
push eax
|
|
|
|
lea ecx, [eax + RING_BUFFER.mutex]
|
|
|
|
call mutex_unlock ; TODO: check what registers this function actually destroys
|
|
|
|
pop eax
|
|
|
|
|
|
|
|
xor ecx, ecx
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_block: Suspend the thread attached to a socket. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = unchanged ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_block:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: %x\n", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-06-17 22:17:17 +04:00
|
|
|
push eax
|
2014-01-23 18:37:18 +04:00
|
|
|
|
|
|
|
pushf
|
2013-05-28 21:34:26 +04:00
|
|
|
cli
|
|
|
|
|
|
|
|
; Set the 'socket is blocked' flag
|
|
|
|
or [eax + SOCKET.state], SS_BLOCKED
|
|
|
|
|
|
|
|
; Suspend the thread
|
|
|
|
push edx
|
|
|
|
mov edx, [TASK_BASE]
|
|
|
|
mov [edx + TASKDATA.state], 1 ; Suspended
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
; Remember the thread ID so we can wake it up again
|
|
|
|
mov edx, [edx + TASKDATA.pid]
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: suspending thread: %u\n", edx
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [eax + SOCKET.TID], edx
|
|
|
|
pop edx
|
2014-01-23 18:37:18 +04:00
|
|
|
popf
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
call change_task
|
2013-06-17 22:17:17 +04:00
|
|
|
pop eax
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2014-01-23 18:37:18 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: continuing\n"
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_notify: Wake up socket owner thread. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = unchanged ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_notify:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: %x\n", eax
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_check
|
2013-05-28 21:34:26 +04:00
|
|
|
jz .error
|
|
|
|
|
2014-01-25 16:48:59 +04:00
|
|
|
; Find the associated thread's TASK_DATA
|
|
|
|
push ebx ecx esi
|
|
|
|
mov ebx, [eax + SOCKET.TID]
|
|
|
|
test ebx, ebx
|
|
|
|
jz .error2
|
2013-05-28 21:34:26 +04:00
|
|
|
xor ecx, ecx
|
|
|
|
inc ecx
|
|
|
|
mov esi, TASK_DATA
|
|
|
|
.next:
|
2014-01-25 16:48:59 +04:00
|
|
|
cmp [esi + TASKDATA.pid], ebx
|
2013-05-28 21:34:26 +04:00
|
|
|
je .found
|
|
|
|
inc ecx
|
|
|
|
add esi, 0x20
|
|
|
|
cmp ecx, [TASK_COUNT]
|
|
|
|
jbe .next
|
2014-01-25 16:48:59 +04:00
|
|
|
|
|
|
|
.error2:
|
|
|
|
; PID not found, TODO: close socket!
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_notify: error finding thread 0x%x !\n", ebx
|
|
|
|
pop esi ecx ebx
|
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
2014-01-25 17:14:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_notify: invalid socket ptr: 0x%x !\n", eax
|
2014-01-25 16:48:59 +04:00
|
|
|
ret
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
.found:
|
2014-01-25 16:48:59 +04:00
|
|
|
test [eax + SOCKET.state], SS_BLOCKED
|
2014-01-25 17:14:23 +04:00
|
|
|
jnz .un_block
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
; Socket and thread exists and socket is of non blocking type.
|
2014-01-25 16:48:59 +04:00
|
|
|
; We'll try to flag an event to the thread.
|
|
|
|
shl ecx, 8
|
2016-05-01 00:15:55 +03:00
|
|
|
or [SLOT_BASE + ecx + APPDATA.event_mask], EVENT_NETWORK
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2016-05-01 00:15:55 +03:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: poking thread %u!\n", ebx
|
2014-01-25 16:48:59 +04:00
|
|
|
pop esi ecx ebx
|
|
|
|
ret
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2014-01-25 16:48:59 +04:00
|
|
|
|
2014-01-25 17:14:23 +04:00
|
|
|
.un_block:
|
2015-12-17 00:20:53 +03:00
|
|
|
; Socket and thread exists and socket is of blocking type
|
2014-01-25 16:48:59 +04:00
|
|
|
; We'll try to unblock it.
|
|
|
|
and [eax + SOCKET.state], not SS_BLOCKED ; Clear the 'socket is blocked' flag
|
|
|
|
mov [esi + TASKDATA.state], 0 ; Run the thread
|
|
|
|
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: Unblocked socket!\n"
|
|
|
|
pop esi ecx ebx
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_alloc: Allocate memory for socket and put new socket ;
|
|
|
|
; into the list. Newly created socket is initialized with calling ;
|
|
|
|
; PID and given a socket number. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: / ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = socket ptr on success ;
|
|
|
|
; eax = 0 on error ;
|
|
|
|
; edi = socket number on success ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_alloc:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
push ebx
|
|
|
|
|
2016-05-01 00:15:55 +03:00
|
|
|
stdcall kernel_alloc, SOCKET_STRUCT_SIZE
|
2013-05-28 21:34:26 +04:00
|
|
|
or eax, eax
|
2015-12-16 00:54:06 +03:00
|
|
|
jz .nomem
|
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: ptr=%x\n", eax
|
2013-05-28 21:34:26 +04:00
|
|
|
|
|
|
|
; zero-initialize allocated memory
|
|
|
|
push eax
|
|
|
|
mov edi, eax
|
2016-05-01 00:15:55 +03:00
|
|
|
mov ecx, SOCKET_STRUCT_SIZE / 4
|
2013-05-28 21:34:26 +04:00
|
|
|
xor eax, eax
|
2013-06-27 03:35:43 +04:00
|
|
|
rep stosd
|
2013-05-28 21:34:26 +04:00
|
|
|
pop eax
|
|
|
|
|
|
|
|
; set send-and receive procedures to return -1
|
2013-06-15 00:14:35 +04:00
|
|
|
mov [eax + SOCKET.snd_proc], .not_yet
|
|
|
|
mov [eax + SOCKET.rcv_proc], .not_yet
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-12 14:21:41 +04:00
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
; find first free socket number and use it
|
|
|
|
mov edi, [last_socket_num]
|
|
|
|
.next_socket_number:
|
|
|
|
inc edi
|
|
|
|
jz .next_socket_number ; avoid socket nr 0
|
|
|
|
cmp edi, -1
|
|
|
|
je .next_socket_number ; avoid socket nr -1
|
|
|
|
mov ebx, net_sockets
|
2011-10-15 01:38:50 +04:00
|
|
|
.next_socket:
|
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
2013-05-28 21:34:26 +04:00
|
|
|
test ebx, ebx
|
2011-10-15 01:38:50 +04:00
|
|
|
jz .last_socket
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
cmp [ebx + SOCKET.Number], edi
|
|
|
|
jne .next_socket
|
|
|
|
jmp .next_socket_number
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
.last_socket:
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [last_socket_num], edi
|
|
|
|
mov [eax + SOCKET.Number], edi
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: number=%u\n", edi
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
; Fill in PID
|
|
|
|
mov ebx, [TASK_BASE]
|
|
|
|
mov ebx, [ebx + TASKDATA.pid]
|
|
|
|
mov [eax + SOCKET.PID], ebx
|
|
|
|
mov [eax + SOCKET.TID], ebx ; currently TID = PID in kolibrios :(
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
; init mutex
|
|
|
|
pusha
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_init
|
|
|
|
popa
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
; add socket to the list by re-arranging some pointers
|
|
|
|
mov ebx, [net_sockets + SOCKET.NextPtr]
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [eax + SOCKET.PrevPtr], net_sockets
|
|
|
|
mov [eax + SOCKET.NextPtr], ebx
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
test ebx, ebx
|
|
|
|
jz @f
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
pusha
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [ebx + SOCKET.PrevPtr], eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
pusha
|
|
|
|
lea ecx, [ebx + SOCKET.mutex]
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
@@:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [net_sockets + SOCKET.NextPtr], eax
|
2013-06-12 14:21:41 +04:00
|
|
|
|
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
2013-05-28 21:34:26 +04:00
|
|
|
pop ebx
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2015-12-16 00:54:06 +03:00
|
|
|
.nomem:
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_alloc: Out of memory!\n"
|
|
|
|
pop ebx
|
|
|
|
ret
|
|
|
|
|
2013-06-15 00:14:35 +04:00
|
|
|
.not_yet:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], ENOTCONN
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
|
|
|
ret
|
|
|
|
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_free: Free socket data memory and remove socket from ;
|
|
|
|
; the list. Caller should lock and unlock socket_mutex. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_free:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: %x\n", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_check
|
2013-06-13 12:22:37 +04:00
|
|
|
jz .error
|
|
|
|
|
|
|
|
push ebx
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
pusha
|
|
|
|
lea ecx, [eax + SOCKET.mutex]
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2015-12-16 00:54:06 +03:00
|
|
|
cmp [eax + SOCKET.Type], SOCK_STREAM
|
|
|
|
jne .no_stream
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov ebx, eax
|
2015-12-16 00:54:06 +03:00
|
|
|
cmp [eax + STREAM_SOCKET.rcv.start_ptr], 0
|
|
|
|
je @f
|
|
|
|
stdcall free_kernel_space, [eax + STREAM_SOCKET.rcv.start_ptr]
|
|
|
|
@@:
|
|
|
|
cmp [ebx + STREAM_SOCKET.snd.start_ptr], 0
|
|
|
|
je @f
|
|
|
|
stdcall free_kernel_space, [ebx + STREAM_SOCKET.snd.start_ptr]
|
|
|
|
@@:
|
2013-05-28 21:34:26 +04:00
|
|
|
mov eax, ebx
|
2015-12-16 00:54:06 +03:00
|
|
|
.no_stream:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-06-13 12:22:37 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: freeing socket %x\n", eax
|
2013-05-28 21:34:26 +04:00
|
|
|
push eax ; this will be passed to kernel_free
|
|
|
|
mov ebx, [eax + SOCKET.NextPtr]
|
|
|
|
mov eax, [eax + SOCKET.PrevPtr]
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
test eax, eax
|
|
|
|
jz @f
|
|
|
|
mov [eax + SOCKET.NextPtr], ebx
|
|
|
|
@@:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
test ebx, ebx
|
|
|
|
jz @f
|
|
|
|
mov [ebx + SOCKET.PrevPtr], eax
|
|
|
|
@@:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
call kernel_free
|
|
|
|
pop ebx
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-06-13 12:22:37 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: success!\n"
|
|
|
|
|
|
|
|
.error:
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2015-12-16 00:54:06 +03:00
|
|
|
.error1:
|
|
|
|
pop ebx
|
2015-12-17 00:20:53 +03:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_free: error!\n"
|
2015-12-16 00:54:06 +03:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "socket ptr=0x%x caller=0x%x\n", eax, [esp]
|
|
|
|
ret
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_fork: Create a child socket. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ebx = socket number ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = child socket number on success ;
|
|
|
|
; eax = 0 on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_fork:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_fork: %x\n", ebx
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
; Exit if backlog queue is full
|
|
|
|
mov eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size]
|
|
|
|
cmp ax, [ebx + SOCKET.backlog]
|
|
|
|
jae .fail
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
; Allocate new socket
|
|
|
|
push ebx
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_alloc
|
2013-05-28 21:34:26 +04:00
|
|
|
pop ebx
|
2014-01-27 14:27:38 +04:00
|
|
|
test eax, eax
|
2013-05-28 21:34:26 +04:00
|
|
|
jz .fail
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
push eax
|
2013-05-28 21:34:26 +04:00
|
|
|
mov esi, esp
|
|
|
|
add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2
|
|
|
|
pop eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
; Copy structure from current socket to new
|
2013-07-12 18:49:00 +04:00
|
|
|
; We start at PID to preserve the socket num, 2 pointers and mutex
|
|
|
|
; TID will be filled in later
|
2013-05-28 21:34:26 +04:00
|
|
|
lea esi, [ebx + SOCKET.PID]
|
|
|
|
lea edi, [eax + SOCKET.PID]
|
|
|
|
mov ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4
|
2013-06-27 03:35:43 +04:00
|
|
|
rep movsd
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
and [eax + SOCKET.options], not SO_ACCEPTCON
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-07-12 18:49:00 +04:00
|
|
|
; Notify owner of parent socket
|
|
|
|
push eax
|
|
|
|
mov eax, ebx
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_notify
|
2013-07-12 18:49:00 +04:00
|
|
|
pop eax
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
.fail2:
|
|
|
|
add esp, 4+4+4
|
|
|
|
.fail:
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_fork: failed\n"
|
2011-10-15 01:38:50 +04:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_num_to_ptr: Get socket structure address by its number. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: ecx = socket number ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = socket ptr ;
|
|
|
|
; eax = 0 on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_num_to_ptr:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_num_to_ptr: num=%u ", ecx
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-06-12 14:21:41 +04:00
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov eax, net_sockets
|
|
|
|
.next_socket:
|
|
|
|
mov eax, [eax + SOCKET.NextPtr]
|
2015-12-16 00:54:06 +03:00
|
|
|
test eax, eax
|
2011-10-15 01:38:50 +04:00
|
|
|
jz .error
|
2013-05-28 21:34:26 +04:00
|
|
|
cmp [eax + SOCKET.Number], ecx
|
|
|
|
jne .next_socket
|
|
|
|
|
2013-06-12 14:21:41 +04:00
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "ptr=%x\n", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
2013-06-12 14:21:41 +04:00
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2014-02-18 23:42:18 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: socket %u not found!\n", eax
|
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: caller = 0x%x\n", [esp]
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_ptr_to_num: Get socket number by its address. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = socket number ;
|
|
|
|
; eax = 0 on error ;
|
|
|
|
; ZF = set on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_ptr_to_num:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ptr_to_num: ptr=%x ", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_check
|
2013-05-28 21:34:26 +04:00
|
|
|
jz .error
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov eax, [eax + SOCKET.Number]
|
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "num=%u\n", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_ptr_to_num: not found\n", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_check: Checks if the given ptr is really a socket ptr. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: eax = 0 on error ;
|
|
|
|
; ZF = set on error ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_check:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check: %x\n", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2015-12-16 00:54:06 +03:00
|
|
|
test eax, eax
|
|
|
|
jz .error
|
2013-05-28 21:34:26 +04:00
|
|
|
push ebx
|
|
|
|
mov ebx, net_sockets
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
.next_socket:
|
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
|
|
|
or ebx, ebx
|
|
|
|
jz .done
|
|
|
|
cmp ebx, eax
|
|
|
|
jnz .next_socket
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
.done:
|
2011-10-15 01:38:50 +04:00
|
|
|
mov eax, ebx
|
2013-05-28 21:34:26 +04:00
|
|
|
test eax, eax
|
|
|
|
pop ebx
|
2015-12-16 00:54:06 +03:00
|
|
|
ret
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2015-12-16 00:54:06 +03:00
|
|
|
.error:
|
2015-12-17 00:20:53 +03:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_check: called with argument 0\n"
|
2015-12-16 00:54:06 +03:00
|
|
|
DEBUGF DEBUG_NETWORK_ERROR, "stack: 0x%x, 0x%x, 0x%x\n", [esp], [esp+4], [esp+8]
|
2011-10-15 01:38:50 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_check_owner: Check if the caller app owns the socket. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: ZF = true/false ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_check_owner:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check_owner: %x\n", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
push ebx
|
|
|
|
mov ebx, [TASK_BASE]
|
|
|
|
mov ebx, [ebx + TASKDATA.pid]
|
|
|
|
cmp [eax + SOCKET.PID], ebx
|
2013-06-27 03:35:43 +04:00
|
|
|
pop ebx
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_process_end: Kernel calls this function when a certain ;
|
|
|
|
; process ends. This function will check if the process had any ;
|
|
|
|
; open sockets and update them accordingly (clean up). ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: edx = pid ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_process_end:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2014-01-10 12:07:54 +04:00
|
|
|
ret ; FIXME
|
|
|
|
|
2013-10-20 15:50:49 +04:00
|
|
|
cmp [net_sockets + SOCKET.NextPtr], 0 ; Are there any active sockets at all?
|
|
|
|
je .quickret ; nope, exit immediately
|
|
|
|
|
|
|
|
; TODO: run the following code in another thread, to avoid deadlock
|
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: %x\n", edx
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-06-12 14:21:41 +04:00
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_lock
|
|
|
|
popa
|
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
push ebx
|
|
|
|
mov ebx, net_sockets
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
.next_socket:
|
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
|
|
|
.next_socket_test:
|
|
|
|
test ebx, ebx
|
|
|
|
jz .done
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
cmp [ebx + SOCKET.PID], edx
|
|
|
|
jne .next_socket
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: killing socket %x\n", ebx
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
mov [ebx + SOCKET.PID], 0
|
|
|
|
mov eax, ebx
|
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
2013-06-12 14:53:22 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
pusha
|
2013-06-13 12:22:37 +04:00
|
|
|
cmp [eax + SOCKET.Domain], AF_INET4
|
|
|
|
jne .free
|
|
|
|
|
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
|
|
|
|
jne .free
|
|
|
|
|
2015-12-27 18:37:31 +03:00
|
|
|
call tcp_disconnect
|
2013-06-13 12:22:37 +04:00
|
|
|
jmp .closed
|
|
|
|
|
|
|
|
.free:
|
2015-12-27 18:37:31 +03:00
|
|
|
call socket_free
|
2013-06-13 12:22:37 +04:00
|
|
|
|
|
|
|
.closed:
|
2013-05-28 21:34:26 +04:00
|
|
|
popa
|
|
|
|
jmp .next_socket_test
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 21:34:26 +04:00
|
|
|
.done:
|
|
|
|
pop ebx
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-06-12 14:21:41 +04:00
|
|
|
pusha
|
|
|
|
mov ecx, socket_mutex
|
|
|
|
call mutex_unlock
|
|
|
|
popa
|
|
|
|
|
2013-10-20 15:50:49 +04:00
|
|
|
.quickret:
|
2013-05-28 21:34:26 +04:00
|
|
|
ret
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_is_connecting: Update socket state. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_is_connecting:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_is_connecting: %x\n", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-10-14 11:08:51 +04:00
|
|
|
and [eax + SOCKET.state], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING)
|
|
|
|
or [eax + SOCKET.state], SS_ISCONNECTING
|
|
|
|
ret
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_is_connected: Update socket state. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_is_connected:
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_is_connected: %x\n", eax
|
2013-05-28 21:34:26 +04:00
|
|
|
|
2013-06-17 22:17:17 +04:00
|
|
|
and [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING)
|
|
|
|
or [eax + SOCKET.state], SS_ISCONNECTED
|
2015-12-27 18:37:31 +03:00
|
|
|
jmp socket_notify
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_is_disconnecting: Update socket state. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_is_disconnecting:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnecting: %x\n", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-06-17 22:17:17 +04:00
|
|
|
and [eax + SOCKET.state], not (SS_ISCONNECTING)
|
|
|
|
or [eax + SOCKET.state], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE
|
2015-12-27 18:37:31 +03:00
|
|
|
jmp socket_notify
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_is_disconnected: Update socket state. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_is_disconnected:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnected: %x\n", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-06-17 22:17:17 +04:00
|
|
|
and [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING)
|
|
|
|
or [eax + SOCKET.state], SS_CANTRCVMORE + SS_CANTSENDMORE
|
2015-12-27 18:37:31 +03:00
|
|
|
jmp socket_notify
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_cant_recv_more: Update socket state. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_cant_recv_more:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_cant_recv_more: %x\n", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-06-17 22:17:17 +04:00
|
|
|
or [eax + SOCKET.state], SS_CANTRCVMORE
|
2015-12-27 18:37:31 +03:00
|
|
|
jmp socket_notify
|
2011-10-15 01:38:50 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-17 00:20:53 +03:00
|
|
|
;-----------------------------------------------------------------;
|
|
|
|
; ;
|
2015-12-27 18:37:31 +03:00
|
|
|
; socket_cant_send_more: Update socket state. ;
|
2015-12-17 00:20:53 +03:00
|
|
|
; ;
|
|
|
|
; IN: eax = socket ptr ;
|
|
|
|
; ;
|
|
|
|
; OUT: / ;
|
|
|
|
; ;
|
|
|
|
;-----------------------------------------------------------------;
|
2013-05-28 21:34:26 +04:00
|
|
|
align 4
|
2015-12-27 18:37:31 +03:00
|
|
|
socket_cant_send_more:
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-05-28 23:19:23 +04:00
|
|
|
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_cant_send_more: %x\n", eax
|
2011-10-15 01:38:50 +04:00
|
|
|
|
2013-06-17 22:17:17 +04:00
|
|
|
or [eax + SOCKET.state], SS_CANTSENDMORE
|
2013-06-15 00:14:35 +04:00
|
|
|
mov [eax + SOCKET.snd_proc], .notconn
|
2015-12-27 18:37:31 +03:00
|
|
|
jmp socket_notify
|
2013-06-15 00:14:35 +04:00
|
|
|
|
|
|
|
.notconn:
|
2013-06-17 17:59:20 +04:00
|
|
|
mov dword[esp+20], ENOTCONN
|
2013-06-15 00:14:35 +04:00
|
|
|
mov dword[esp+32], -1
|
2015-01-06 17:53:54 +03:00
|
|
|
ret
|