2009-09-17 13:55:38 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2011-01-24 23:01:54 +01:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2011. All rights reserved. ;;
|
2009-09-17 13:55:38 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
2010-07-27 20:53:38 +02:00
|
|
|
;; Written by hidnplayr@kolibrios.org, ;;
|
|
|
|
;; and Clevermouse. ;;
|
2009-09-17 13:55:38 +02:00
|
|
|
;; ;;
|
2010-07-27 20:53:38 +02:00
|
|
|
;; Based on code by mike.dld ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
2009-09-17 13:55:38 +02:00
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
$Revision$
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-01-28 17:49:08 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
virtual at 0
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET:
|
|
|
|
.NextPtr dd ? ; pointer to next socket in list
|
|
|
|
.PrevPtr dd ? ; pointer to previous socket in list
|
|
|
|
.Number dd ? ; socket number
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.lock dd ? ; lock mutex
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.PID dd ? ; application process id
|
|
|
|
.Domain dd ? ; INET/UNIX/..
|
2010-07-31 21:44:17 +02:00
|
|
|
.Type dd ? ; RAW/STREAM/DGRAP
|
|
|
|
.Protocol dd ? ; ICMP/IPv4/ARP/TCP/UDP
|
2010-12-10 19:37:02 +01:00
|
|
|
.errorcode dd ?
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.options dd ?
|
2010-07-27 20:53:38 +02:00
|
|
|
.state dd ?
|
2010-07-31 21:44:17 +02:00
|
|
|
.backlog dw ? ; how many incomming connections that can be queued
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
.snd_proc dd ?
|
|
|
|
.rcv_proc dd ?
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.end:
|
|
|
|
end virtual
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
virtual at SOCKET.end
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
IP_SOCKET:
|
2011-01-24 23:01:54 +01:00
|
|
|
.LocalIP rd 4
|
|
|
|
.RemoteIP rd 4
|
2009-11-06 21:45:08 +01:00
|
|
|
|
|
|
|
.end:
|
2010-07-12 01:13:12 +02:00
|
|
|
end virtual
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
virtual at IP_SOCKET.end
|
|
|
|
|
|
|
|
TCP_SOCKET:
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
.LocalPort dw ?
|
|
|
|
.RemotePort dw ?
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
.t_state dd ? ; TCB state
|
|
|
|
.t_rxtshift dd ?
|
|
|
|
.t_rxtcur dd ?
|
|
|
|
.t_dupacks dd ?
|
|
|
|
.t_maxseg dd ?
|
|
|
|
.t_force dd ?
|
|
|
|
.t_flags dd ?
|
|
|
|
|
|
|
|
;---------------
|
|
|
|
; RFC783 page 21
|
|
|
|
|
|
|
|
; send sequence
|
|
|
|
.SND_UNA dd ? ; sequence number of unack'ed sent Packets
|
|
|
|
.SND_NXT dd ? ; next send sequence number to use
|
|
|
|
.SND_UP dd ?
|
|
|
|
.SND_WL1 dd ? ; window minus one
|
|
|
|
.SND_WL2 dd ? ;
|
|
|
|
.ISS dd ? ; initial send sequence number
|
|
|
|
.SND_WND dd ? ; send window
|
|
|
|
|
|
|
|
; receive sequence
|
|
|
|
.RCV_WND dw ? ; receive window
|
|
|
|
.RCV_NXT dd ? ; next receive sequence number to use
|
|
|
|
.RCV_UP dd ?
|
|
|
|
.IRS dd ? ; initial receive sequence number
|
|
|
|
|
|
|
|
;---------------------
|
|
|
|
; Additional variables
|
|
|
|
|
|
|
|
; receive variables
|
|
|
|
.RCV_ADV dd ?
|
|
|
|
|
|
|
|
; retransmit variables
|
|
|
|
.SND_MAX dd ?
|
|
|
|
|
|
|
|
; congestion control
|
|
|
|
.SND_CWND dd ?
|
|
|
|
.SND_SSTHRESH dd ?
|
|
|
|
|
|
|
|
;----------------------
|
|
|
|
; Transmit timing stuff
|
|
|
|
.t_idle dd ?
|
|
|
|
.t_rtt dd ?
|
|
|
|
.t_rtseq dd ?
|
|
|
|
.t_srtt dd ?
|
|
|
|
.t_rttvar dd ?
|
|
|
|
.t_rttmin dd ?
|
|
|
|
.max_sndwnd dd ?
|
|
|
|
|
|
|
|
;-----------------
|
|
|
|
; Out-of-band data
|
|
|
|
.t_oobflags dd ?
|
|
|
|
.t_iobc dd ?
|
|
|
|
.t_softerror dd ?
|
|
|
|
|
|
|
|
|
|
|
|
;---------
|
|
|
|
; RFC 1323
|
|
|
|
.SND_SCALE db ? ; Scale factor
|
|
|
|
.RCV_SCALE db ?
|
|
|
|
.request_r_scale db ?
|
|
|
|
.requested_s_scale dd ?
|
|
|
|
|
|
|
|
.ts_recent dd ?
|
|
|
|
.ts_recent_age dd ?
|
|
|
|
.last_ack_sent dd ?
|
|
|
|
|
2010-07-15 20:54:19 +02:00
|
|
|
|
|
|
|
;-------
|
|
|
|
; Timers
|
2010-07-27 20:53:38 +02:00
|
|
|
.timer_retransmission dw ? ; rexmt
|
2010-07-15 20:54:19 +02:00
|
|
|
.timer_persist dw ?
|
2010-07-27 20:53:38 +02:00
|
|
|
.timer_keepalive dw ? ; keepalive/syn timeout
|
|
|
|
.timer_timed_wait dw ? ; also used as 2msl timer
|
2010-07-15 20:54:19 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.end:
|
|
|
|
end virtual
|
|
|
|
|
|
|
|
virtual at IP_SOCKET.end
|
|
|
|
|
|
|
|
UDP_SOCKET:
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
.LocalPort dw ?
|
|
|
|
.RemotePort dw ?
|
2010-07-12 01:13:12 +02:00
|
|
|
.firstpacket db ?
|
|
|
|
|
|
|
|
.end:
|
|
|
|
end virtual
|
|
|
|
|
|
|
|
virtual at IP_SOCKET.end
|
|
|
|
|
|
|
|
ICMP_SOCKET:
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
.Identifier dw ?
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
.end:
|
|
|
|
end virtual
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
struc RING_BUFFER {
|
|
|
|
.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
|
2010-07-29 01:59:42 +02:00
|
|
|
.end:
|
2010-07-27 20:53:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual at 0
|
|
|
|
|
|
|
|
RING_BUFFER RING_BUFFER
|
|
|
|
|
|
|
|
end virtual
|
|
|
|
|
|
|
|
virtual at TCP_SOCKET.end
|
|
|
|
|
|
|
|
STREAM_SOCKET:
|
2010-07-29 01:59:42 +02:00
|
|
|
.rcv rd RING_BUFFER.end/4
|
|
|
|
.snd rd RING_BUFFER.end/4
|
2010-07-27 20:53:38 +02:00
|
|
|
.end:
|
|
|
|
|
|
|
|
end virtual
|
|
|
|
|
2009-11-15 22:44:17 +01:00
|
|
|
struct socket_queue_entry
|
|
|
|
.data_ptr dd ?
|
2010-07-12 01:13:12 +02:00
|
|
|
.buf_ptr dd ?
|
2009-11-15 22:44:17 +01:00
|
|
|
.data_size dd ?
|
|
|
|
.size:
|
|
|
|
ends
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
SOCKETBUFFSIZE equ 4096 ; in bytes
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
SOCKET_QUEUE_SIZE equ 10 ; maximum number ofincoming packets queued for 1 socket
|
2010-07-12 01:13:12 +02:00
|
|
|
; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start
|
2010-07-31 21:44:17 +02:00
|
|
|
SOCKET_QUEUE_LOCATION equ (SOCKETBUFFSIZE - SOCKET_QUEUE_SIZE*socket_queue_entry.size - queue.data)
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
uglobal
|
2010-07-12 01:13:12 +02:00
|
|
|
net_sockets rd 4
|
2010-07-31 21:44:17 +02:00
|
|
|
last_socket_num dd ?
|
2009-09-17 13:55:38 +02:00
|
|
|
last_UDP_port dw ? ; These values give the number of the last used ephemeral port
|
|
|
|
last_TCP_port dw ? ;
|
|
|
|
endg
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; SOCKET_init
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2010-07-27 20:53:38 +02:00
|
|
|
macro SOCKET_init {
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
xor eax, eax
|
|
|
|
mov edi, net_sockets
|
2010-07-31 21:44:17 +02:00
|
|
|
mov ecx, 5
|
2010-07-12 01:13:12 +02:00
|
|
|
rep stosd
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
@@:
|
|
|
|
pseudo_random eax
|
|
|
|
cmp ax, MIN_EPHEMERAL_PORT
|
2011-11-07 21:25:47 +01:00
|
|
|
jb @r
|
2010-07-27 20:53:38 +02:00
|
|
|
cmp ax, MAX_EPHEMERAL_PORT
|
2011-11-07 21:25:47 +01:00
|
|
|
ja @r
|
2010-07-27 20:53:38 +02:00
|
|
|
mov [last_UDP_port], ax
|
|
|
|
|
|
|
|
@@:
|
|
|
|
pseudo_random eax
|
|
|
|
cmp ax, MIN_EPHEMERAL_PORT
|
2011-11-07 21:25:47 +01:00
|
|
|
jb @r
|
2010-07-27 20:53:38 +02:00
|
|
|
cmp ax, MAX_EPHEMERAL_PORT
|
2011-11-07 21:25:47 +01:00
|
|
|
ja @r
|
2010-07-27 20:53:38 +02:00
|
|
|
mov [last_TCP_port], ax
|
|
|
|
|
|
|
|
}
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; Socket API (function 74)
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2011-11-07 22:03:20 +01:00
|
|
|
align 16
|
|
|
|
sock_sysfn_table:
|
|
|
|
dd SOCKET_open ; 0
|
|
|
|
dd SOCKET_close ; 1
|
|
|
|
dd SOCKET_bind ; 2
|
|
|
|
dd SOCKET_listen ; 3
|
|
|
|
dd SOCKET_connect ; 4
|
|
|
|
dd SOCKET_accept ; 5
|
|
|
|
dd SOCKET_send ; 6
|
|
|
|
dd SOCKET_receive ; 7
|
|
|
|
dd SOCKET_set_opt ; 8
|
|
|
|
dd SOCKET_get_opt ; 9
|
|
|
|
|
|
|
|
SOCKET_SYSFUNCS = ($ - sock_sysfn_table)/4
|
|
|
|
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
align 4
|
|
|
|
sys_socket:
|
2011-11-07 22:03:20 +01:00
|
|
|
cmp ebx, SOCKET_SYSFUNCS-1
|
2011-11-07 21:25:47 +01:00
|
|
|
ja @f
|
2010-07-31 13:17:06 +02:00
|
|
|
jmp dword [sock_sysfn_table + 4*ebx]
|
2010-07-27 20:53:38 +02:00
|
|
|
@@:
|
|
|
|
cmp ebx, 255
|
|
|
|
jz SOCKET_debug
|
2009-11-08 19:00:01 +01:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
s_error:
|
|
|
|
DEBUGF 1,"socket error\n"
|
|
|
|
mov dword [esp+32], -1
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
2011-11-07 22:03:20 +01:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; SOCKET_open
|
|
|
|
;
|
|
|
|
; IN: domain in ecx
|
|
|
|
; type in edx
|
2009-10-05 22:47:27 +02:00
|
|
|
; protocol in esi
|
2009-09-17 13:55:38 +02:00
|
|
|
; OUT: eax is socket num, -1 on error
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET_open:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_open: domain: %u, type: %u protocol: %x\n", ecx, edx, esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
call SOCKET_alloc
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
mov [esp+32], edi ; return socketnumber
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
mov [eax + SOCKET.Domain], ecx
|
|
|
|
mov [eax + SOCKET.Type], edx
|
|
|
|
mov [eax + SOCKET.Protocol], esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
cmp ecx, AF_INET4
|
2010-07-30 11:09:06 +02:00
|
|
|
jne .no_inet4
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp edx, SOCK_DGRAM
|
2010-07-30 11:09:06 +02:00
|
|
|
je .udp
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp edx, SOCK_STREAM
|
2010-07-30 11:09:06 +02:00
|
|
|
je .tcp
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
cmp edx, SOCK_RAW
|
|
|
|
je .raw
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
.no_inet4:
|
|
|
|
ret
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
align 4
|
|
|
|
.raw:
|
2010-07-31 21:44:17 +02:00
|
|
|
test esi, esi ; IP_PROTO_IP
|
|
|
|
jz .ip
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp esi, IP_PROTO_ICMP
|
|
|
|
je .icmp
|
2010-07-29 01:59:42 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp esi, IP_PROTO_UDP
|
|
|
|
je .udp
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp esi, IP_PROTO_TCP
|
|
|
|
je .tcp
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
ret
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
align 4
|
2010-07-30 11:09:06 +02:00
|
|
|
.udp:
|
2010-07-31 13:17:06 +02:00
|
|
|
mov [eax + SOCKET.Protocol], IP_PROTO_UDP
|
2010-07-30 11:09:06 +02:00
|
|
|
mov [eax + SOCKET.snd_proc], SOCKET_send_udp
|
2010-07-30 23:54:27 +02:00
|
|
|
mov [eax + SOCKET.rcv_proc], SOCKET_receive_dgram
|
|
|
|
ret
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
align 4
|
|
|
|
.tcp:
|
|
|
|
mov [eax + SOCKET.Protocol], IP_PROTO_TCP
|
2010-07-31 21:44:17 +02:00
|
|
|
mov [eax + SOCKET.snd_proc], SOCKET_send_tcp
|
|
|
|
mov [eax + SOCKET.rcv_proc], SOCKET_receive_tcp
|
2011-02-03 22:35:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
mov [eax + TCP_SOCKET.t_maxseg], 1480 ;;;;; FIXME
|
2010-07-30 23:54:27 +02:00
|
|
|
ret
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
align 4
|
|
|
|
.ip:
|
|
|
|
mov [eax + SOCKET.snd_proc], SOCKET_send_ip
|
|
|
|
mov [eax + SOCKET.rcv_proc], SOCKET_receive_dgram
|
|
|
|
ret
|
2010-07-30 23:54:27 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
|
|
|
|
align 4
|
2010-07-30 23:54:27 +02:00
|
|
|
.icmp:
|
|
|
|
mov [eax + SOCKET.snd_proc], SOCKET_send_icmp
|
|
|
|
mov [eax + SOCKET.rcv_proc], SOCKET_receive_dgram
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; SOCKET_bind
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
|
|
|
; pointer to sockaddr struct in edx
|
|
|
|
; length of that struct in esi
|
|
|
|
; OUT: 0 on success
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET_bind:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
DEBUGF 1,"socket_bind: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
call SOCKET_num_to_ptr
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
cmp esi, 2
|
2011-11-07 21:25:47 +01:00
|
|
|
jb s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
cmp word [edx], AF_INET4
|
2009-11-06 21:45:08 +01:00
|
|
|
je .af_inet4
|
|
|
|
|
|
|
|
cmp word [edx], AF_UNIX
|
|
|
|
je .af_unix
|
|
|
|
|
|
|
|
jmp s_error
|
|
|
|
|
|
|
|
.af_unix:
|
|
|
|
; TODO: write code here
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
mov dword [esp+32], 0
|
2009-11-06 21:45:08 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.af_inet4:
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
DEBUGF 1,"af_inet4\n"
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
cmp esi, 6
|
2011-11-07 21:25:47 +01:00
|
|
|
jb s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-07 22:03:20 +01:00
|
|
|
pushw [edx + 2]
|
|
|
|
pop [eax + UDP_SOCKET.LocalPort]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-07 22:03:20 +01:00
|
|
|
pushd [edx + 4]
|
2010-07-31 21:44:17 +02:00
|
|
|
pop [eax + IP_SOCKET.LocalIP]
|
2010-07-30 11:09:06 +02:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
DEBUGF 1,"local ip: %u.%u.%u.%u\n",\
|
2010-07-12 01:13:12 +02: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
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
mov dword [esp+32], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; SOCKET_connect
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
|
|
|
; pointer to sockaddr struct in edx
|
|
|
|
; length of that struct in esi
|
|
|
|
; OUT: 0 on success
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET_connect:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1,"SOCKET_connect: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
call SOCKET_num_to_ptr
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
cmp esi, 8
|
2011-11-07 21:25:47 +01:00
|
|
|
jb s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
cmp word [edx], AF_INET4
|
|
|
|
je .af_inet4
|
|
|
|
|
2009-09-28 21:48:32 +02:00
|
|
|
jmp s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.af_inet4:
|
2010-07-31 21:44:17 +02:00
|
|
|
cmp [eax + IP_SOCKET.LocalIP], 0
|
|
|
|
jne @f
|
|
|
|
push [IP_LIST]
|
|
|
|
pop [eax + IP_SOCKET.LocalIP]
|
|
|
|
@@:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_UDP
|
2009-09-17 13:55:38 +02:00
|
|
|
je .udp
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
|
2009-10-12 16:39:59 +02:00
|
|
|
je .tcp
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_IP
|
|
|
|
je .ip
|
|
|
|
|
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_ICMP
|
|
|
|
je .ip
|
2010-07-30 23:54:27 +02:00
|
|
|
|
2009-09-28 21:48:32 +02:00
|
|
|
jmp s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
align 4
|
2009-11-08 19:00:01 +01:00
|
|
|
.udp:
|
2010-07-31 21:44:17 +02:00
|
|
|
lea ebx, [eax + SOCKET.lock]
|
|
|
|
call wait_mutex
|
|
|
|
|
2011-11-07 22:03:20 +01:00
|
|
|
pushw [edx + 2]
|
2010-07-31 21:44:17 +02:00
|
|
|
pop [eax + UDP_SOCKET.RemotePort]
|
|
|
|
|
2011-11-07 22:03:20 +01:00
|
|
|
pushd [edx + 4]
|
2010-07-31 21:44:17 +02:00
|
|
|
pop [eax + IP_SOCKET.RemoteIP]
|
|
|
|
|
|
|
|
cmp [eax + UDP_SOCKET.LocalPort], 0
|
|
|
|
jne @f
|
|
|
|
call SOCKET_find_port
|
|
|
|
@@:
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
mov [eax + UDP_SOCKET.firstpacket], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
push eax
|
|
|
|
init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue
|
2011-01-08 15:59:21 +01:00
|
|
|
pop eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
mov [eax + SOCKET.lock], 0
|
2010-07-12 01:13:12 +02:00
|
|
|
mov dword [esp+32], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
align 4
|
2009-11-08 19:00:01 +01:00
|
|
|
.tcp:
|
2010-07-12 01:13:12 +02:00
|
|
|
lea ebx, [eax + SOCKET.lock]
|
2009-11-22 18:25:31 +01:00
|
|
|
call wait_mutex
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-07 22:03:20 +01:00
|
|
|
pushw [edx + 2]
|
2010-07-31 21:44:17 +02:00
|
|
|
pop [eax + TCP_SOCKET.RemotePort]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-11-07 22:03:20 +01:00
|
|
|
pushd [edx + 4]
|
2010-07-31 21:44:17 +02:00
|
|
|
pop [eax + IP_SOCKET.RemoteIP]
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
cmp [eax + TCP_SOCKET.LocalPort], 0
|
|
|
|
jne @f
|
|
|
|
call SOCKET_find_port
|
|
|
|
@@:
|
2009-11-08 19:00:01 +01:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
mov [eax + TCP_SOCKET.timer_persist], 0
|
2011-02-01 18:00:04 +01:00
|
|
|
mov [eax + TCP_SOCKET.t_state], TCPS_SYN_SENT
|
2011-01-08 15:59:21 +01:00
|
|
|
push [TCP_sequence_num]
|
2010-07-27 20:53:38 +02:00
|
|
|
add [TCP_sequence_num], 6400
|
2011-01-08 15:59:21 +01:00
|
|
|
pop [eax + TCP_SOCKET.ISS]
|
2010-07-29 01:59:42 +02:00
|
|
|
mov [eax + TCP_SOCKET.timer_keepalive], TCP_time_keep_init
|
|
|
|
|
|
|
|
TCP_sendseqinit eax
|
2009-11-08 19:00:01 +01:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
; mov [ebx + TCP_SOCKET.timer_retransmission], ;; todo: create macro to set retransmission timer
|
2010-07-15 20:54:19 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
mov ebx, eax
|
|
|
|
|
|
|
|
lea eax, [ebx + STREAM_SOCKET.snd]
|
|
|
|
call SOCKET_ring_create
|
|
|
|
|
|
|
|
lea eax, [ebx + STREAM_SOCKET.rcv]
|
|
|
|
call SOCKET_ring_create
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
mov [ebx + SOCKET.lock], 0
|
2011-01-08 15:59:21 +01:00
|
|
|
|
|
|
|
mov eax, ebx
|
|
|
|
call TCP_output
|
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
mov dword [esp+32], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
align 4
|
|
|
|
.ip:
|
2010-07-31 21:44:17 +02:00
|
|
|
lea ebx, [eax + SOCKET.lock]
|
|
|
|
call wait_mutex
|
|
|
|
|
2011-11-07 22:03:20 +01:00
|
|
|
pushd [edx + 4]
|
|
|
|
pop [eax + IP_SOCKET.RemoteIP]
|
2010-07-30 23:54:27 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
push eax
|
|
|
|
init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue
|
|
|
|
pop eax
|
|
|
|
|
|
|
|
mov [eax + SOCKET.lock], 0
|
|
|
|
mov dword [esp+32], 0
|
2010-07-30 23:54:27 +02:00
|
|
|
ret
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; SOCKET_listen
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
|
|
|
; backlog in edx
|
|
|
|
; OUT: eax is socket num, -1 on error
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET_listen:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1,"SOCKET_listen: socknum: %u backlog: %u\n", ecx, edx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
call SOCKET_num_to_ptr
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
cmp word [eax + SOCKET.Domain], AF_INET4
|
2009-11-08 19:00:01 +01:00
|
|
|
jne s_error
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
|
2009-11-08 19:00:01 +01:00
|
|
|
jne s_error
|
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
cmp [eax + TCP_SOCKET.LocalPort], 0
|
|
|
|
je s_error
|
|
|
|
|
|
|
|
cmp [eax + IP_SOCKET.LocalIP], 0
|
|
|
|
jne @f
|
|
|
|
push [IP_LIST]
|
|
|
|
pop [eax + IP_SOCKET.LocalIP]
|
|
|
|
@@:
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
cmp edx, MAX_backlog
|
2011-11-07 21:25:47 +01:00
|
|
|
jbe @f
|
2010-07-12 01:13:12 +02:00
|
|
|
mov edx, MAX_backlog
|
2010-07-31 21:44:17 +02:00
|
|
|
@@:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
mov [eax + SOCKET.backlog], dx
|
|
|
|
or [eax + SOCKET.options], SO_ACCEPTCON
|
2011-02-01 18:00:04 +01:00
|
|
|
mov [eax + TCP_SOCKET.t_state], TCPS_LISTEN
|
2010-07-31 21:44:17 +02:00
|
|
|
|
|
|
|
push eax
|
|
|
|
init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up sockets queue
|
|
|
|
pop eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
mov dword [esp+32], 0
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; SOCKET_accept
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
|
|
|
; addr in edx
|
|
|
|
; addrlen in esi
|
|
|
|
; OUT: eax is socket num, -1 on error
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET_accept:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1,"SOCKET_accept: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
call SOCKET_num_to_ptr
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
test [eax + SOCKET.options], SO_ACCEPTCON
|
|
|
|
jz s_error
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
cmp word [eax + SOCKET.Domain], AF_INET4
|
|
|
|
jne s_error
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
|
2010-07-31 21:44:17 +02:00
|
|
|
jne s_error
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
get_from_queue (eax + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, s_error
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
mov eax, [esi]
|
|
|
|
call SOCKET_ptr_to_num
|
|
|
|
jz s_error
|
|
|
|
mov dword [esp+32], eax
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; SOCKET_close
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
|
|
|
; OUT: eax is socket num, -1 on error
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET_close:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1,"SOCKET_close: socknum: %u\n", ecx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
call SOCKET_num_to_ptr
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
cmp [eax + SOCKET.Domain], AF_INET4
|
2009-11-06 21:45:08 +01:00
|
|
|
jne s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_UDP
|
2010-07-12 01:13:12 +02:00
|
|
|
je .free
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_ICMP
|
2010-07-12 01:13:12 +02:00
|
|
|
je .free
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_IP
|
|
|
|
je .free
|
|
|
|
|
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
|
2009-10-12 16:39:59 +02:00
|
|
|
je .tcp
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-09-28 21:48:32 +02:00
|
|
|
jmp s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.tcp:
|
2011-02-01 18:00:04 +01:00
|
|
|
cmp [eax + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED ; state must be LISTEN, SYN_SENT or CLOSED
|
2011-11-07 21:25:47 +01:00
|
|
|
jb .free
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
call TCP_output
|
|
|
|
mov dword [esp+32], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.free:
|
|
|
|
call SOCKET_free
|
|
|
|
mov dword [esp+32], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; SOCKET_receive
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
2009-11-06 21:45:08 +01:00
|
|
|
; addr to buffer in edx
|
|
|
|
; length of buffer in esi
|
2009-09-17 13:55:38 +02:00
|
|
|
; flags in edi
|
|
|
|
; OUT: eax is number of bytes copied, -1 on error
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET_receive:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_receive: socknum: %u bufaddr: %x, buflength: %u, flags: %x, ", ecx, edx, esi, edi
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
call SOCKET_num_to_ptr
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
jmp [eax + SOCKET.rcv_proc]
|
2010-07-29 01:59:42 +02:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
align 4
|
2010-07-30 23:54:27 +02:00
|
|
|
SOCKET_receive_dgram:
|
2010-07-30 11:09:06 +02:00
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1,"SOCKET_receive: DGRAM\n"
|
2010-07-30 11:09:06 +02:00
|
|
|
|
|
|
|
mov ebx, esi
|
2009-11-22 18:25:31 +01:00
|
|
|
mov edi, edx ; addr to buffer
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, socket_queue_entry.size, s_error ; destroys esi and ecx
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
mov ecx, [esi + socket_queue_entry.data_size]
|
2009-11-06 21:45:08 +01:00
|
|
|
DEBUGF 1,"Got %u bytes of data\n", ecx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2009-11-22 18:25:31 +01:00
|
|
|
cmp ecx, ebx
|
2011-11-07 21:25:47 +01:00
|
|
|
ja .too_small
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
push [esi + socket_queue_entry.buf_ptr] ; save the buffer addr so we can clear it later
|
2010-07-12 01:13:12 +02:00
|
|
|
mov esi, [esi + socket_queue_entry.data_ptr]
|
2009-11-06 21:45:08 +01:00
|
|
|
DEBUGF 1,"Source buffer: %x, real addr: %x\n", [esp], esi
|
2010-07-30 11:09:06 +02:00
|
|
|
mov dword[esp+32+4], ecx ; return number of bytes copied
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
; copy the data
|
2009-11-06 21:45:08 +01:00
|
|
|
shr ecx, 1
|
|
|
|
jnc .nb
|
|
|
|
movsb
|
2010-07-30 11:09:06 +02:00
|
|
|
.nb:
|
|
|
|
shr ecx, 1
|
2009-11-06 21:45:08 +01:00
|
|
|
jnc .nw
|
|
|
|
movsw
|
2010-07-30 11:09:06 +02:00
|
|
|
.nw:
|
|
|
|
test ecx, ecx
|
2009-11-15 22:44:17 +01:00
|
|
|
jz .nd
|
|
|
|
rep movsd
|
2010-07-30 11:09:06 +02:00
|
|
|
.nd:
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
call kernel_free ; remove the packet
|
2010-07-29 01:59:42 +02:00
|
|
|
ret
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
.too_small:
|
|
|
|
|
|
|
|
DEBUGF 1,"Buffer too small...\n"
|
|
|
|
jmp s_error
|
|
|
|
|
|
|
|
align 4
|
|
|
|
SOCKET_receive_tcp:
|
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1,"SOCKET_receive: TCP\n"
|
2010-07-29 01:59:42 +02:00
|
|
|
|
|
|
|
mov ecx, esi
|
|
|
|
mov edi, edx
|
|
|
|
add eax, STREAM_SOCKET.rcv
|
|
|
|
call SOCKET_ring_read
|
2010-07-29 16:03:16 +02:00
|
|
|
call SOCKET_ring_free
|
2010-07-29 01:59:42 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
mov dword[esp+32], ecx ; return number of bytes copied
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
|
|
|
; SOCKET_send
|
|
|
|
;
|
|
|
|
;
|
|
|
|
; IN: socket number in ecx
|
2009-10-12 16:39:59 +02:00
|
|
|
; pointer to data in edx
|
|
|
|
; datalength in esi
|
2009-09-17 13:55:38 +02:00
|
|
|
; flags in edi
|
|
|
|
; OUT: -1 on error
|
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET_send:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1,"SOCKET_send: socknum: %u data ptr: %x, length: %u, flags: %x, ", ecx, edx, esi, edi
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
call SOCKET_num_to_ptr
|
2009-09-28 21:48:32 +02:00
|
|
|
jz s_error
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2011-01-24 23:01:54 +01:00
|
|
|
mov ecx, esi
|
|
|
|
mov esi, edx
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
jmp [eax + SOCKET.snd_proc]
|
2009-10-12 16:39:59 +02:00
|
|
|
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
align 4
|
|
|
|
SOCKET_send_udp:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1,"SOCKET_send: UDP\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
call UDP_output
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
mov dword [esp+32], 0
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
2009-11-08 19:00:01 +01:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
align 4
|
|
|
|
SOCKET_send_tcp:
|
2009-11-08 19:00:01 +01:00
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1,"SOCKET_send: TCP\n"
|
2009-11-08 19:00:01 +01:00
|
|
|
|
2010-07-29 01:59:42 +02:00
|
|
|
push eax
|
|
|
|
add eax, STREAM_SOCKET.snd
|
|
|
|
call SOCKET_ring_write
|
|
|
|
pop eax
|
2010-07-30 11:09:06 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
call TCP_output
|
2009-11-08 19:00:01 +01:00
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
mov [esp+32], eax
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
align 4
|
|
|
|
SOCKET_send_ip:
|
|
|
|
|
|
|
|
DEBUGF 1,"type: IP\n"
|
|
|
|
|
|
|
|
call IPv4_output_raw
|
|
|
|
|
2011-01-24 23:01:54 +01:00
|
|
|
mov [esp+32], eax
|
2010-07-31 21:44:17 +02:00
|
|
|
ret
|
2010-07-30 23:54:27 +02:00
|
|
|
|
|
|
|
align 4
|
|
|
|
SOCKET_send_icmp:
|
|
|
|
|
|
|
|
DEBUGF 1,"SOCKET_send: ICMP\n"
|
|
|
|
|
|
|
|
call ICMP_output_raw
|
|
|
|
|
|
|
|
mov dword [esp+32], 0
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-06 21:45:08 +01:00
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-11-09 12:34:51 +01:00
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
; SOCKET_get_options
|
2009-11-09 12:34:51 +01:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; IN: ecx = socket number
|
|
|
|
; edx = pointer to the options:
|
2009-11-09 14:59:46 +01:00
|
|
|
; dd level, optname, optval, optlen
|
2009-11-09 12:34:51 +01:00
|
|
|
; OUT: -1 on error
|
|
|
|
;
|
|
|
|
; 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
|
2011-02-01 18:00:04 +01:00
|
|
|
; Also pseudo-optname -3 is valid and returns socket state, one of TCPS_*.
|
2009-11-09 12:34:51 +01:00
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET_get_opt:
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_get_opt\n"
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
call SOCKET_num_to_ptr
|
|
|
|
jz s_error
|
2009-11-09 14:59:46 +01:00
|
|
|
|
2009-11-09 12:34:51 +01:00
|
|
|
cmp dword [edx], IP_PROTO_TCP
|
2010-07-12 01:13:12 +02:00
|
|
|
jnz s_error
|
2009-11-09 12:34:51 +01:00
|
|
|
cmp dword [edx+4], -2
|
2009-11-30 12:19:59 +01:00
|
|
|
jz @f
|
|
|
|
cmp dword [edx+4], -3
|
2010-07-12 01:13:12 +02:00
|
|
|
jnz s_error
|
2009-11-30 12:19:59 +01:00
|
|
|
@@:
|
2010-07-12 01:13:12 +02: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]
|
2009-11-30 12:19:59 +01:00
|
|
|
@@:
|
2009-11-09 12:34:51 +01:00
|
|
|
mov eax, [edx+8]
|
|
|
|
test eax, eax
|
|
|
|
jz @f
|
|
|
|
mov [eax], ecx
|
|
|
|
@@:
|
2009-11-09 14:59:46 +01:00
|
|
|
mov dword [esp+32], 0
|
2009-11-09 12:34:51 +01:00
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
|
|
|
|
align 4
|
|
|
|
SOCKET_set_opt:
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_debug
|
|
|
|
;
|
|
|
|
; Copies socket variables to application buffer
|
|
|
|
;
|
|
|
|
; IN: ecx = socket number
|
|
|
|
; edx = pointer to buffer
|
|
|
|
;
|
|
|
|
; OUT: -1 on error
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
SOCKET_debug:
|
|
|
|
|
|
|
|
DEBUGF 1,"socket_debug\n"
|
|
|
|
|
|
|
|
call SOCKET_num_to_ptr
|
|
|
|
jz s_error
|
|
|
|
|
|
|
|
mov esi, eax
|
|
|
|
mov edi, edx
|
|
|
|
mov ecx, SOCKETBUFFSIZE/4
|
|
|
|
rep movsd
|
|
|
|
|
|
|
|
mov dword [esp+32], 0
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; SOCKET_find_port
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; Fills 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
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; IN: eax = socket pointer
|
|
|
|
; OUT: /
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET_find_port:
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_find_port\n"
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
push ebx esi ecx
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_UDP
|
2009-10-12 16:39:59 +02:00
|
|
|
je .udp
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
|
2009-10-12 16:39:59 +02:00
|
|
|
je .tcp
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
jmp .error
|
|
|
|
|
|
|
|
.done:
|
|
|
|
mov [eax + UDP_SOCKET.LocalPort], bx
|
|
|
|
.error:
|
|
|
|
pop ecx esi ebx
|
|
|
|
ret
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
.udp:
|
|
|
|
mov bx, [last_UDP_port]
|
2010-07-12 01:13:12 +02:00
|
|
|
call .findit
|
|
|
|
mov [last_UDP_port], bx
|
|
|
|
jmp .done
|
2009-10-12 16:39:59 +02:00
|
|
|
|
|
|
|
.tcp:
|
|
|
|
mov bx, [last_TCP_port]
|
2010-07-12 01:13:12 +02:00
|
|
|
call .findit
|
|
|
|
mov [last_TCP_port], bx
|
|
|
|
jmp .done
|
2009-10-12 16:39:59 +02:00
|
|
|
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.restart:
|
|
|
|
mov bx, MIN_EPHEMERAL_PORT
|
|
|
|
.findit:
|
2009-10-12 16:39:59 +02:00
|
|
|
inc bx
|
|
|
|
|
|
|
|
cmp bx, MAX_EPHEMERAL_PORT
|
2010-07-12 01:13:12 +02:00
|
|
|
jz .restart
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
call SOCKET_check_port
|
|
|
|
jz .findit
|
2009-10-12 16:39:59 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2010-07-31 13:17:06 +02:00
|
|
|
; SOCKET_check_port (to be used with AF_INET only!)
|
2010-07-12 01:13:12 +02:00
|
|
|
;
|
|
|
|
; Checks if a local port number is unused
|
|
|
|
; If the proposed port number is unused, it is filled in in the socket structure
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; IN: eax = socket ptr (to find out if its a TCP/UDP socket)
|
|
|
|
; bx = proposed socket number
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; OUT: ZF = cleared on error
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET_check_port:
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_check_port\n"
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
mov ecx, [eax + SOCKET.Protocol]
|
2009-10-12 16:39:59 +02:00
|
|
|
mov esi, net_sockets
|
|
|
|
|
|
|
|
.next_socket:
|
2010-07-12 01:13:12 +02:00
|
|
|
mov esi, [esi + SOCKET.NextPtr]
|
2009-10-12 16:39:59 +02:00
|
|
|
or esi, esi
|
|
|
|
jz .port_ok
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [esi + SOCKET.Protocol], ecx
|
2009-10-12 16:39:59 +02:00
|
|
|
jne .next_socket
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
cmp [esi + UDP_SOCKET.LocalPort], bx
|
2009-10-12 16:39:59 +02:00
|
|
|
jne .next_socket
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
DEBUGF 1,"local port %u already in use\n", bx
|
|
|
|
ret
|
2009-10-12 16:39:59 +02:00
|
|
|
|
|
|
|
.port_ok:
|
2010-07-12 01:13:12 +02:00
|
|
|
mov [eax + UDP_SOCKET.LocalPort], bx
|
|
|
|
or bx, bx ; set the zero-flag
|
|
|
|
|
2009-10-12 16:39:59 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2009-11-09 14:59:46 +01:00
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; SOCKET_input
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2010-07-30 11:09:06 +02:00
|
|
|
; Updates a (stateless) socket with received data
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; Note: the mutex should already be set !
|
2009-11-06 21:45:08 +01:00
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
2010-07-12 01:13:12 +02:00
|
|
|
; ebx = pointer to device struct
|
|
|
|
; ecx = data size
|
|
|
|
; esi = ptr to data
|
|
|
|
; [esp] = ptr to buf
|
|
|
|
; [esp + 4] = buf size
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; OUT: /
|
2009-10-12 16:39:59 +02:00
|
|
|
;
|
2009-11-09 14:59:46 +01:00
|
|
|
;-----------------------------------------------------------------
|
2009-10-12 16:39:59 +02:00
|
|
|
align 4
|
2010-07-12 01:13:12 +02:00
|
|
|
SOCKET_input:
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
mov dword[esp+4], ecx
|
|
|
|
push esi
|
2009-11-06 21:45:08 +01:00
|
|
|
mov esi, esp
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, socket_queue_entry.size, SOCKET_input.full
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1,"SOCKET_input: queued packet successfully\n"
|
2009-11-09 14:59:46 +01:00
|
|
|
add esp, socket_queue_entry.size
|
2010-07-12 01:13:12 +02:00
|
|
|
mov [eax + SOCKET.lock], 0
|
|
|
|
jmp SOCKET_notify_owner
|
|
|
|
|
|
|
|
.full:
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 2,"SOCKET_input: socket %x is full!\n", eax
|
2010-07-12 01:13:12 +02:00
|
|
|
mov [eax + SOCKET.lock], 0
|
|
|
|
call kernel_free
|
|
|
|
add esp, 8
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
2010-07-29 01:59:42 +02:00
|
|
|
|
|
|
|
;--------------------------
|
|
|
|
;
|
|
|
|
; eax = ptr to ring struct (just a buffer of the right size)
|
|
|
|
;
|
|
|
|
align 4
|
|
|
|
SOCKET_ring_create:
|
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
push esi
|
2010-07-29 01:59:42 +02:00
|
|
|
mov esi, eax
|
2010-07-31 21:44:17 +02:00
|
|
|
|
|
|
|
push edx
|
2010-07-29 01:59:42 +02:00
|
|
|
stdcall create_ring_buffer, SOCKET_MAXDATA, PG_SW
|
2010-07-31 21:44:17 +02:00
|
|
|
pop edx
|
2010-07-29 01:59:42 +02:00
|
|
|
|
|
|
|
DEBUGF 1,"SOCKET_ring_created: %x\n", eax
|
|
|
|
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
|
|
|
|
add eax, SOCKET_MAXDATA
|
|
|
|
mov [esi + RING_BUFFER.end_ptr], eax
|
2010-07-31 21:44:17 +02:00
|
|
|
mov eax, esi
|
|
|
|
pop esi
|
2010-07-29 01:59:42 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
2010-07-29 01:59:42 +02:00
|
|
|
; SOCKET_ring_write
|
2010-07-27 20:53:38 +02:00
|
|
|
;
|
2010-07-29 01:59:42 +02:00
|
|
|
; Adds data to a stream socket, and updates write pointer and size
|
2010-07-27 20:53:38 +02:00
|
|
|
;
|
|
|
|
; IN: eax = ptr to ring struct
|
|
|
|
; ecx = data size
|
|
|
|
; esi = ptr to data
|
|
|
|
;
|
2010-07-29 01:59:42 +02:00
|
|
|
; OUT: ecx = number of bytes stored
|
2010-07-27 20:53:38 +02:00
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
2010-07-29 01:59:42 +02:00
|
|
|
SOCKET_ring_write:
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-29 01:59:42 +02:00
|
|
|
DEBUGF 1,"SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-29 01:59:42 +02:00
|
|
|
add [eax + RING_BUFFER.size], ecx
|
|
|
|
cmp [eax + RING_BUFFER.size], SOCKET_MAXDATA
|
2011-11-07 21:25:47 +01:00
|
|
|
ja .too_large
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
.copy:
|
2010-07-29 01:59:42 +02:00
|
|
|
mov edi, [eax + RING_BUFFER.write_ptr]
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 2,"SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
push ecx
|
2010-07-29 01:59:42 +02:00
|
|
|
shr ecx, 1
|
|
|
|
jnc .nb
|
|
|
|
movsb
|
2010-07-30 11:09:06 +02:00
|
|
|
.nb:
|
2010-07-29 01:59:42 +02:00
|
|
|
shr ecx, 1
|
|
|
|
jnc .nw
|
|
|
|
movsw
|
2010-07-30 11:09:06 +02:00
|
|
|
.nw:
|
2010-07-29 01:59:42 +02:00
|
|
|
test ecx, ecx
|
|
|
|
jz .nd
|
2010-07-27 20:53:38 +02:00
|
|
|
rep movsd
|
2010-07-30 11:09:06 +02:00
|
|
|
.nd:
|
2010-07-29 01:59:42 +02:00
|
|
|
pop ecx
|
|
|
|
|
|
|
|
cmp edi, [eax + RING_BUFFER.end_ptr]
|
2011-11-07 21:25:47 +01:00
|
|
|
jae .wrap
|
2010-07-29 01:59:42 +02:00
|
|
|
mov [eax + RING_BUFFER.write_ptr], edi
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.wrap:
|
|
|
|
sub edi, SOCKET_MAXDATA
|
|
|
|
mov [eax + RING_BUFFER.write_ptr], edi
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.too_large:
|
|
|
|
mov ecx, SOCKET_MAXDATA ; calculate number of bytes available in buffer
|
|
|
|
sub ecx, [eax + RING_BUFFER.size]
|
2011-11-07 21:25:47 +01:00
|
|
|
jae .full
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
mov [eax + RING_BUFFER.size], SOCKET_MAXDATA ; update size, we will fill buffer completely
|
|
|
|
jmp .copy
|
|
|
|
|
|
|
|
.full:
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 2,"SOCKET_ring_write: ring buffer is full!\n"
|
2010-07-27 20:53:38 +02:00
|
|
|
xor ecx, ecx
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_ring_read
|
|
|
|
;
|
2010-07-29 01:59:42 +02:00
|
|
|
; reads the data, BUT DOES NOT CLEAR IT FROM MEMORY YET
|
2010-07-27 20:53:38 +02:00
|
|
|
;
|
|
|
|
; IN: eax = ptr to ring struct
|
|
|
|
; ecx = buffer size
|
|
|
|
; edi = ptr to buffer
|
|
|
|
;
|
2010-07-29 01:59:42 +02:00
|
|
|
; OUT: ecx = number of bytes read
|
2010-07-27 20:53:38 +02:00
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
SOCKET_ring_read:
|
|
|
|
|
2010-07-29 01:59:42 +02:00
|
|
|
DEBUGF 1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u\n", eax, edi, ecx
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-29 01:59:42 +02:00
|
|
|
cmp ecx, [eax + RING_BUFFER.size]
|
2011-11-07 21:25:47 +01:00
|
|
|
ja .less_data
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
.copy:
|
2010-07-29 01:59:42 +02:00
|
|
|
mov esi, [eax + RING_BUFFER.read_ptr]
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 2,"SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi
|
2010-07-27 20:53:38 +02:00
|
|
|
push ecx
|
2010-07-29 01:59:42 +02:00
|
|
|
shr ecx, 1
|
|
|
|
jnc .nb
|
|
|
|
movsb
|
2010-07-30 11:09:06 +02:00
|
|
|
.nb:
|
2010-07-29 01:59:42 +02:00
|
|
|
shr ecx, 1
|
|
|
|
jnc .nw
|
|
|
|
movsw
|
2010-07-30 11:09:06 +02:00
|
|
|
.nw:
|
2010-07-29 01:59:42 +02:00
|
|
|
test ecx, ecx
|
|
|
|
jz .nd
|
2010-07-27 20:53:38 +02:00
|
|
|
rep movsd
|
2010-07-30 11:09:06 +02:00
|
|
|
.nd:
|
2010-07-29 01:59:42 +02:00
|
|
|
pop ecx
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2011-02-01 17:14:18 +01:00
|
|
|
.no_data_at_all:
|
2010-07-27 20:53:38 +02:00
|
|
|
ret
|
|
|
|
|
2010-07-29 01:59:42 +02:00
|
|
|
.less_data:
|
2010-07-27 20:53:38 +02:00
|
|
|
mov ecx, [eax + RING_BUFFER.size]
|
2011-02-01 17:14:18 +01:00
|
|
|
; test ecx, ecx
|
2010-07-29 01:59:42 +02:00
|
|
|
; jz .no_data_at_all
|
2010-07-27 20:53:38 +02:00
|
|
|
jmp .copy
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_ring_free
|
|
|
|
;
|
|
|
|
; Free's some bytes from the ringbuffer
|
|
|
|
;
|
|
|
|
; IN: eax = ptr to ring struct
|
|
|
|
; ecx = data size
|
|
|
|
;
|
|
|
|
; OUT: ecx = number of bytes free-ed
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
SOCKET_ring_free:
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
sub [eax + RING_BUFFER.size], ecx
|
2011-11-07 21:25:47 +01:00
|
|
|
jb .sumthinwong
|
2010-07-27 20:53:38 +02:00
|
|
|
add [eax + RING_BUFFER.read_ptr], ecx
|
|
|
|
|
|
|
|
mov edx, [eax + RING_BUFFER.end_ptr]
|
|
|
|
cmp [eax + RING_BUFFER.read_ptr], edx
|
2011-11-07 21:25:47 +01:00
|
|
|
jb @f
|
2010-07-29 01:59:42 +02:00
|
|
|
sub [eax + RING_BUFFER.read_ptr], SOCKET_MAXDATA
|
2010-07-27 20:53:38 +02:00
|
|
|
@@:
|
|
|
|
ret
|
|
|
|
|
2010-07-29 01:59:42 +02:00
|
|
|
.sumthinwong: ; we could free all available bytes, but that would be stupid, i guess..
|
|
|
|
add [eax + RING_BUFFER.size], ecx
|
2010-07-27 20:53:38 +02:00
|
|
|
xor ecx, ecx
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_notify_owner
|
|
|
|
;
|
|
|
|
; notify's the owner of a socket that something happened
|
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
SOCKET_notify_owner:
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_notify_owner: %x\n", eax
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
call SOCKET_check
|
|
|
|
jz .error
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
push eax ecx esi
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
; socket exists, now try to flag an event to the application
|
|
|
|
|
|
|
|
mov eax, [eax + SOCKET.PID]
|
2009-10-12 16:39:59 +02:00
|
|
|
mov ecx, 1
|
|
|
|
mov esi, TASK_DATA + TASKDATA.pid
|
|
|
|
|
|
|
|
.next_pid:
|
2010-07-12 01:13:12 +02:00
|
|
|
cmp [esi], eax
|
2009-10-12 16:39:59 +02:00
|
|
|
je .found_pid
|
|
|
|
inc ecx
|
|
|
|
add esi, 0x20
|
|
|
|
cmp ecx, [TASK_COUNT]
|
|
|
|
jbe .next_pid
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
; PID not found, TODO: close socket!
|
|
|
|
|
|
|
|
jmp .error2
|
2009-10-12 16:39:59 +02:00
|
|
|
|
|
|
|
.found_pid:
|
|
|
|
shl ecx, 8
|
2010-07-12 01:13:12 +02:00
|
|
|
or [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK
|
2009-10-12 16:39:59 +02:00
|
|
|
mov [check_idle_semaphore], 200
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_notify_owner: succes!\n"
|
2009-10-12 16:39:59 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.error2:
|
2010-07-31 21:44:17 +02:00
|
|
|
pop esi ecx eax
|
2010-07-30 11:09:06 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.error:
|
|
|
|
|
|
|
|
ret
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
;--------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_alloc
|
|
|
|
;
|
2009-09-17 13:55:38 +02:00
|
|
|
; Allocate memory for socket data and put new socket into the list
|
|
|
|
; Newly created socket is initialized with calling PID and number and
|
|
|
|
; put into beginning of list (which is a fastest way).
|
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; IN: /
|
|
|
|
; OUT: eax = 0 on error, socket ptr otherwise
|
|
|
|
; edi = socket number
|
|
|
|
; ZF = cleared on error
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
;--------------------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
SOCKET_alloc:
|
|
|
|
|
|
|
|
push ecx ebx
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
stdcall kernel_alloc, SOCKETBUFFSIZE
|
2010-07-27 20:53:38 +02:00
|
|
|
DEBUGF 1, "SOCKET_alloc: ptr=%x\n", eax
|
2009-09-17 13:55:38 +02:00
|
|
|
or eax, eax
|
|
|
|
jz .exit
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
; zero-initialize allocated memory
|
|
|
|
push eax edi
|
2009-09-17 13:55:38 +02:00
|
|
|
mov edi, eax
|
|
|
|
mov ecx, SOCKETBUFFSIZE / 4
|
|
|
|
xor eax, eax
|
|
|
|
rep stosd
|
2010-07-12 01:13:12 +02:00
|
|
|
pop edi eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
; set send-and receive procedures to return -1
|
|
|
|
mov [eax + SOCKET.snd_proc], s_error
|
|
|
|
mov [eax + SOCKET.rcv_proc], s_error
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
; find first free socket number and use it
|
2010-07-31 21:44:17 +02:00
|
|
|
mov ecx, [last_socket_num]
|
2009-09-17 13:55:38 +02:00
|
|
|
.next_socket_number:
|
|
|
|
inc ecx
|
2010-07-31 21:44:17 +02:00
|
|
|
jz .next_socket_number ; avoid socket nr 0
|
|
|
|
cmp ecx, -1
|
|
|
|
je .next_socket_number ; avoid socket nr -1
|
2010-07-30 11:09:06 +02:00
|
|
|
mov ebx, net_sockets
|
2009-09-17 13:55:38 +02:00
|
|
|
.next_socket:
|
2010-07-12 01:13:12 +02:00
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
2010-07-27 20:53:38 +02:00
|
|
|
test ebx, ebx
|
2010-07-12 01:13:12 +02:00
|
|
|
jz .last_socket
|
2010-07-30 11:09:06 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
cmp [ebx + SOCKET.Number], ecx
|
2009-09-17 13:55:38 +02:00
|
|
|
jne .next_socket
|
|
|
|
jmp .next_socket_number
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
.last_socket:
|
2010-07-31 21:44:17 +02:00
|
|
|
mov [last_socket_num], ecx
|
2010-07-12 01:13:12 +02:00
|
|
|
mov [eax + SOCKET.Number], ecx
|
2010-07-27 20:53:38 +02:00
|
|
|
DEBUGF 1, "SOCKET_alloc: number=%u\n", ecx
|
|
|
|
mov edi, ecx
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
; Fill in PID
|
|
|
|
mov ebx, [TASK_BASE]
|
|
|
|
mov ebx, [ebx + TASKDATA.pid]
|
|
|
|
mov [eax + SOCKET.PID], ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
; add socket to the list by re-arranging some pointers
|
2010-07-12 01:13:12 +02:00
|
|
|
mov ebx, [net_sockets + SOCKET.NextPtr]
|
|
|
|
|
|
|
|
mov [eax + SOCKET.PrevPtr], net_sockets
|
|
|
|
mov [eax + SOCKET.NextPtr], ebx
|
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
test ebx, ebx
|
2010-07-12 01:13:12 +02:00
|
|
|
jz @f
|
|
|
|
add ebx, SOCKET.lock ; lock the next socket
|
|
|
|
call wait_mutex
|
|
|
|
sub ebx, SOCKET.lock
|
|
|
|
mov [ebx + SOCKET.PrevPtr], eax
|
2010-07-30 11:09:06 +02:00
|
|
|
mov [ebx + SOCKET.lock], 0 ; and unlock it again
|
2010-07-12 01:13:12 +02:00
|
|
|
@@:
|
|
|
|
|
|
|
|
mov [net_sockets + SOCKET.NextPtr], eax
|
2010-07-27 20:53:38 +02:00
|
|
|
or eax, eax ; used to clear zero flag
|
2009-09-17 13:55:38 +02:00
|
|
|
.exit:
|
2010-07-12 01:13:12 +02:00
|
|
|
pop ebx ecx
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
;----------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; SOCKET_free
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; Free socket data memory and remove socket from the list
|
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;----------------------------------------------------
|
|
|
|
align 4
|
|
|
|
SOCKET_free:
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1, "SOCKET_free: %x\n", eax
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
call SOCKET_check
|
2009-09-17 13:55:38 +02:00
|
|
|
jz .error
|
2009-11-06 21:45:08 +01:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
push ebx
|
|
|
|
lea ebx, [eax + SOCKET.lock]
|
2009-11-06 21:45:08 +01:00
|
|
|
call wait_mutex
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1, "SOCKET_free: freeing socket..\n"
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2010-07-27 20:53:38 +02:00
|
|
|
cmp [eax + SOCKET.Domain], AF_INET4
|
2010-07-30 11:09:06 +02:00
|
|
|
jnz .no_tcp
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
|
2010-07-30 11:09:06 +02:00
|
|
|
jnz .no_tcp
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
mov ebx, eax
|
|
|
|
stdcall kernel_free, [ebx + STREAM_SOCKET.rcv + RING_BUFFER.start_ptr]
|
|
|
|
stdcall kernel_free, [ebx + STREAM_SOCKET.snd + RING_BUFFER.start_ptr]
|
|
|
|
mov eax, ebx
|
|
|
|
.no_tcp:
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
push eax ; this will be passed to kernel_free
|
|
|
|
mov ebx, [eax + SOCKET.NextPtr]
|
|
|
|
mov eax, [eax + SOCKET.PrevPtr]
|
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
test eax, eax
|
|
|
|
jz @f
|
|
|
|
mov [eax + SOCKET.NextPtr], ebx
|
|
|
|
@@:
|
|
|
|
|
|
|
|
test ebx, ebx
|
|
|
|
jz @f
|
|
|
|
mov [ebx + SOCKET.PrevPtr], eax
|
|
|
|
@@:
|
|
|
|
|
|
|
|
call kernel_free
|
|
|
|
pop ebx
|
|
|
|
|
2010-07-30 23:54:27 +02:00
|
|
|
DEBUGF 1, "SOCKET_free: success!\n"
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.error:
|
|
|
|
ret
|
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
;------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_fork
|
|
|
|
;
|
|
|
|
; Create a child socket
|
|
|
|
;
|
2010-07-29 01:59:42 +02:00
|
|
|
; IN: socket nr in ebx
|
2010-07-31 21:44:17 +02:00
|
|
|
; OUT: child socket nr in eax
|
|
|
|
;
|
|
|
|
;-----------------------------------
|
2010-07-27 20:53:38 +02:00
|
|
|
align 4
|
|
|
|
SOCKET_fork:
|
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
DEBUGF 1,"SOCKET_fork: %x\n", ebx
|
|
|
|
|
|
|
|
; Exit if backlog queue is full
|
|
|
|
mov eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size]
|
|
|
|
cmp ax, [ebx + SOCKET.backlog]
|
2011-11-07 21:25:47 +01:00
|
|
|
jae .fail
|
2010-07-27 20:53:38 +02:00
|
|
|
|
|
|
|
; Allocate new socket
|
|
|
|
call SOCKET_alloc
|
2010-07-31 21:44:17 +02:00
|
|
|
jz .fail
|
|
|
|
|
|
|
|
push esi ecx edi
|
|
|
|
push eax
|
|
|
|
mov esi, esp
|
|
|
|
add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2
|
|
|
|
pop eax
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
; Copy structure from current socket to new
|
|
|
|
; We start at PID to preserve the socket num, and the 2 pointers at beginning of socket
|
|
|
|
lea esi, [ebx + SOCKET.PID]
|
2010-07-27 20:53:38 +02:00
|
|
|
lea edi, [eax + SOCKET.PID]
|
2010-07-31 21:44:17 +02:00
|
|
|
mov ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4
|
2010-07-27 20:53:38 +02:00
|
|
|
rep movsd
|
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
and [eax + SOCKET.options], not SO_ACCEPTCON
|
|
|
|
pop edi ecx esi
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
ret
|
2010-07-27 20:53:38 +02:00
|
|
|
|
2010-07-31 21:44:17 +02:00
|
|
|
.fail2:
|
|
|
|
add esp, 4+4+4
|
|
|
|
.fail:
|
|
|
|
DEBUGF 1,"SOCKET_fork: failed\n"
|
|
|
|
xor eax, eax
|
2010-07-27 20:53:38 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
;---------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_num_to_ptr
|
|
|
|
;
|
2009-09-17 13:55:38 +02:00
|
|
|
; Get socket structure address by its number
|
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; IN: ecx = socket number
|
2010-07-29 01:59:42 +02:00
|
|
|
; OUT: eax = 0 on error, socket ptr otherwise
|
2010-07-12 01:13:12 +02:00
|
|
|
; ZF = set on error
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
;---------------------------------------------------
|
|
|
|
align 4
|
|
|
|
SOCKET_num_to_ptr:
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_num_to_ptr: %u ", ecx
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
mov eax, net_sockets
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
.next_socket:
|
2010-07-12 01:13:12 +02:00
|
|
|
mov eax, [eax + SOCKET.NextPtr]
|
|
|
|
or eax, eax
|
2009-09-17 13:55:38 +02:00
|
|
|
jz .error
|
2010-07-12 01:13:12 +02:00
|
|
|
cmp [eax + SOCKET.Number], ecx
|
2009-09-17 13:55:38 +02:00
|
|
|
jne .next_socket
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
test eax, eax
|
2009-09-17 13:55:38 +02:00
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
DEBUGF 1,"(%x)\n", eax
|
2009-09-17 13:55:38 +02:00
|
|
|
.error:
|
|
|
|
ret
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
;---------------------------------------------------
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; SOCKET_ptr_to_num
|
2009-09-17 13:55:38 +02:00
|
|
|
;
|
2010-07-12 01:13:12 +02:00
|
|
|
; Get socket number by its address
|
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; OUT: eax = 0 on error, socket num otherwise
|
|
|
|
; ZF = set on error
|
|
|
|
;
|
|
|
|
;---------------------------------------------------
|
|
|
|
align 4
|
|
|
|
SOCKET_ptr_to_num:
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_ptr_to_num: %x ", eax
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
call SOCKET_check
|
2009-09-17 13:55:38 +02:00
|
|
|
jz .error
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
mov eax, [eax + SOCKET.Number]
|
|
|
|
|
|
|
|
DEBUGF 1,"(%u)\n", eax
|
|
|
|
|
|
|
|
.error:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;---------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_check
|
|
|
|
;
|
|
|
|
; checks if the given value is really a socket ptr
|
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; OUT: eax = 0 on error, unchanged otherwise
|
|
|
|
; ZF = set on error
|
|
|
|
;
|
|
|
|
;---------------------------------------------------
|
|
|
|
align 4
|
|
|
|
SOCKET_check:
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_check: %x\n", eax
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
push ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
mov ebx, net_sockets
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
.next_socket:
|
2010-07-12 01:13:12 +02:00
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
2009-09-17 13:55:38 +02:00
|
|
|
or ebx, ebx
|
2010-07-12 01:13:12 +02:00
|
|
|
jz .done
|
2009-09-17 13:55:38 +02:00
|
|
|
cmp ebx, eax
|
2010-07-12 01:13:12 +02:00
|
|
|
jnz .next_socket
|
|
|
|
|
|
|
|
.done:
|
|
|
|
mov eax, ebx
|
|
|
|
test eax, eax
|
|
|
|
pop ebx
|
2009-09-17 13:55:38 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
;---------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_check_owner
|
|
|
|
;
|
|
|
|
; checks if the caller application owns the socket
|
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; OUT: ZF = true/false
|
|
|
|
;
|
|
|
|
;---------------------------------------------------
|
|
|
|
align 4
|
|
|
|
SOCKET_check_owner:
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_check_owner: %x\n", eax
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
push ebx
|
|
|
|
mov ebx, [TASK_BASE]
|
|
|
|
mov ebx, [ecx + TASKDATA.pid]
|
|
|
|
cmp [eax + SOCKET.PID], ebx
|
|
|
|
pop ebx
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-02-24 15:16:02 +01:00
|
|
|
;------------------------------------------------------
|
2010-07-12 01:13:12 +02: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
|
|
|
|
;
|
|
|
|
; IN: eax = pid
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;------------------------------------------------------
|
|
|
|
align 4
|
|
|
|
SOCKET_process_end:
|
|
|
|
|
2010-07-30 11:09:06 +02:00
|
|
|
DEBUGF 1,"SOCKET_process_end: %x\n", eax
|
2010-07-12 01:13:12 +02:00
|
|
|
|
|
|
|
push ebx
|
|
|
|
mov ebx, net_sockets
|
|
|
|
|
|
|
|
.next_socket:
|
|
|
|
|
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
|
|
|
.test_socket:
|
|
|
|
test ebx, ebx
|
|
|
|
jz .done
|
|
|
|
|
|
|
|
cmp [ebx + SOCKET.PID], eax
|
|
|
|
jne .next_socket
|
|
|
|
|
|
|
|
DEBUGF 1,"closing socket %x", eax, ebx
|
|
|
|
|
|
|
|
mov [ebx + SOCKET.PID], 0
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [ebx + SOCKET.Protocol], IP_PROTO_UDP
|
2010-07-12 01:13:12 +02:00
|
|
|
je .udp
|
|
|
|
|
2010-07-31 13:17:06 +02:00
|
|
|
cmp [ebx + SOCKET.Protocol], IP_PROTO_TCP
|
2010-07-12 01:13:12 +02:00
|
|
|
je .tcp
|
|
|
|
|
|
|
|
jmp .next_socket ; kill all sockets for given PID
|
|
|
|
|
|
|
|
.udp:
|
|
|
|
mov eax, ebx
|
|
|
|
mov ebx, [ebx + SOCKET.NextPtr]
|
|
|
|
call SOCKET_free
|
|
|
|
jmp .test_socket
|
|
|
|
|
|
|
|
.tcp:
|
|
|
|
|
2011-02-24 15:16:02 +01:00
|
|
|
;;; TODO
|
|
|
|
|
2010-07-12 01:13:12 +02:00
|
|
|
jmp .next_socket
|
|
|
|
|
|
|
|
.done:
|
|
|
|
pop ebx
|
|
|
|
|
2009-09-17 13:55:38 +02:00
|
|
|
ret
|
2011-01-28 17:49:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-02-24 15:16:02 +01:00
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_is_connecting
|
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
|
|
|
|
align 4
|
|
|
|
SOCKET_is_connecting:
|
|
|
|
|
|
|
|
|
|
|
|
and [eax + SOCKET.options], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING)
|
|
|
|
or [eax + SOCKET.options], SS_ISCONNECTING
|
|
|
|
|
|
|
|
jmp SOCKET_notify_owner
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_is_connected
|
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
|
|
|
|
align 4
|
|
|
|
SOCKET_is_connected:
|
|
|
|
|
|
|
|
|
|
|
|
and [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING)
|
|
|
|
or [eax + SOCKET.options], SS_ISCONNECTED
|
|
|
|
|
|
|
|
jmp SOCKET_notify_owner
|
2011-01-28 17:49:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_is_disconnecting
|
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
|
|
|
|
align 4
|
|
|
|
SOCKET_is_disconnecting:
|
|
|
|
|
|
|
|
and [eax + SOCKET.options], not (SS_ISCONNECTING)
|
|
|
|
or [eax + SOCKET.options], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE
|
|
|
|
|
|
|
|
jmp SOCKET_notify_owner
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_is_disconnected
|
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
|
|
|
|
align 4
|
|
|
|
SOCKET_is_disconnected:
|
|
|
|
|
|
|
|
and [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING)
|
|
|
|
or [eax + SOCKET.options], SS_CANTRCVMORE + SS_CANTSENDMORE
|
|
|
|
|
|
|
|
jmp SOCKET_notify_owner
|
2011-01-29 00:07:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; SOCKET_cant_recv_more
|
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
|
|
|
|
align 4
|
|
|
|
SOCKET_cant_recv_more:
|
|
|
|
|
2011-02-24 15:16:02 +01:00
|
|
|
or [eax + SOCKET.options], SS_CANTRCVMORE
|
|
|
|
|
2011-02-01 18:00:04 +01:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
;
|
2011-02-24 15:16:02 +01:00
|
|
|
; SOCKET_cant_send_more
|
2011-02-01 18:00:04 +01:00
|
|
|
;
|
|
|
|
; IN: eax = socket ptr
|
|
|
|
; OUT: /
|
|
|
|
;
|
|
|
|
;-----------------------------------------------------------------
|
|
|
|
|
|
|
|
align 4
|
2011-02-24 15:16:02 +01:00
|
|
|
SOCKET_cant_send_more:
|
2011-02-01 18:00:04 +01:00
|
|
|
|
2011-02-24 15:16:02 +01:00
|
|
|
or [eax + SOCKET.options], SS_CANTSENDMORE
|
2011-02-01 18:00:04 +01:00
|
|
|
|
2011-02-24 15:16:02 +01:00
|
|
|
ret
|