diff --git a/kernel/branches/net/network/icmp.inc b/kernel/branches/net/network/icmp.inc index 673a5edc56..736bdc0fbd 100644 --- a/kernel/branches/net/network/icmp.inc +++ b/kernel/branches/net/network/icmp.inc @@ -254,9 +254,10 @@ ICMP_input: DEBUGF 1,"Found valid ICMP packet for socket %x\n", ebx - mov eax, ebx - add ebx, SOCKET.lock - call wait_mutex + pusha + lea ecx, [eax + SOCKET.mutex] + call mutex_lock + popa mov esi, edx jmp SOCKET_input diff --git a/kernel/branches/net/network/socket.inc b/kernel/branches/net/network/socket.inc index fd7b171e7f..65ad5fd527 100644 --- a/kernel/branches/net/network/socket.inc +++ b/kernel/branches/net/network/socket.inc @@ -16,172 +16,172 @@ $Revision$ -struct SOCKET +struct SOCKET - NextPtr dd ? ; pointer to next socket in list - PrevPtr dd ? ; pointer to previous socket in list - Number dd ? ; socket number + NextPtr dd ? ; pointer to next socket in list + PrevPtr dd ? ; pointer to previous socket in list + Number dd ? ; socket number - lock dd ? ; lock mutex + mutex MUTEX - PID dd ? ; application process id - Domain dd ? ; INET/UNIX/.. - Type dd ? ; RAW/STREAM/DGRAP - Protocol dd ? ; ICMP/IPv4/ARP/TCP/UDP - errorcode dd ? + PID dd ? ; application process id + Domain dd ? ; INET/UNIX/.. + Type dd ? ; RAW/STREAM/DGRAP + Protocol dd ? ; ICMP/IPv4/ARP/TCP/UDP + errorcode dd ? - options dd ? - state dd ? - backlog dw ? ; how many incomming connections that can be queued + options dd ? + state dd ? + backlog dw ? ; how many incomming connections that can be queued - snd_proc dd ? - rcv_proc dd ? + snd_proc dd ? + rcv_proc dd ? ends -struct IP_SOCKET SOCKET +struct IP_SOCKET SOCKET - LocalIP rd 4 - RemoteIP rd 4 + LocalIP rd 4 + RemoteIP rd 4 ends -struct TCP_SOCKET IP_SOCKET +struct TCP_SOCKET IP_SOCKET - LocalPort dw ? - RemotePort dw ? + LocalPort dw ? + RemotePort dw ? - t_state dd ? ; TCB state - t_rxtshift dd ? - t_rxtcur dd ? - t_dupacks dd ? - t_maxseg dd ? - t_force dd ? - t_flags dd ? + 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 + 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 + 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 ? + RCV_ADV dd ? ; retransmit variables - SND_MAX dd ? + SND_MAX dd ? ; congestion control - SND_CWND dd ? - SND_SSTHRESH dd ? + 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 ? + 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 ? + 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 ? + SND_SCALE db ? + RCV_SCALE db ? + requested_s_scale db ? + request_r_scale db ? - ts_recent dd ? - ts_recent_age dd ? - last_ack_sent dd ? + ts_recent dd ? + ts_recent_age dd ? + last_ack_sent dd ? ;------- ; Timers - timer_retransmission dw ? ; rexmt - timer_persist dw ? - timer_keepalive dw ? ; keepalive/syn timeout - timer_timed_wait dw ? ; also used as 2msl timer + timer_retransmission dw ? ; rexmt + timer_persist dw ? + timer_keepalive dw ? ; keepalive/syn timeout + timer_timed_wait dw ? ; also used as 2msl timer ends -struct UDP_SOCKET IP_SOCKET +struct UDP_SOCKET IP_SOCKET - LocalPort dw ? - RemotePort dw ? - firstpacket db ? + LocalPort dw ? + RemotePort dw ? + firstpacket db ? ends -struct ICMP_SOCKET IP_SOCKET +struct ICMP_SOCKET IP_SOCKET - Identifier dw ? + Identifier dw ? ends -struct RING_BUFFER +struct 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 + 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 ends -struct STREAM_SOCKET TCP_SOCKET +struct STREAM_SOCKET TCP_SOCKET - rcv RING_BUFFER - snd RING_BUFFER + rcv RING_BUFFER + snd RING_BUFFER ends -struct socket_queue_entry +struct socket_queue_entry - data_ptr dd ? - buf_ptr dd ? - data_size dd ? + data_ptr dd ? + buf_ptr dd ? + data_size dd ? ends -SOCKETBUFFSIZE equ 4096 ; in bytes +SOCKETBUFFSIZE equ 4096 ; in bytes -SOCKET_QUEUE_SIZE equ 10 ; maximum number ofincoming packets queued for 1 socket +SOCKET_QUEUE_SIZE equ 10 ; maximum number ofincoming packets queued for 1 socket ; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start -SOCKET_QUEUE_LOCATION equ (SOCKETBUFFSIZE - SOCKET_QUEUE_SIZE*sizeof.socket_queue_entry - sizeof.queue) +SOCKET_QUEUE_LOCATION equ (SOCKETBUFFSIZE - SOCKET_QUEUE_SIZE*sizeof.socket_queue_entry - sizeof.queue) uglobal - net_sockets rd 4 - last_socket_num dd ? - last_UDP_port dw ? ; These values give the number of the last used ephemeral port - last_TCP_port dw ? ; + net_sockets rd 4 + last_socket_num dd ? + last_UDP_port dw ? ; These values give the number of the last used ephemeral port + last_TCP_port dw ? ; endg @@ -190,28 +190,28 @@ endg ; SOCKET_init ; ;----------------------------------------------------------------- -macro SOCKET_init { +macro SOCKET_init { - xor eax, eax - mov edi, net_sockets - mov ecx, 5 - rep stosd + xor eax, eax + mov edi, net_sockets + mov ecx, 5 + rep stosd @@: - pseudo_random eax - cmp ax, MIN_EPHEMERAL_PORT - jb @r - cmp ax, MAX_EPHEMERAL_PORT - ja @r - mov [last_UDP_port], ax + pseudo_random eax + cmp ax, MIN_EPHEMERAL_PORT + jb @r + cmp ax, MAX_EPHEMERAL_PORT + ja @r + mov [last_UDP_port], ax @@: - pseudo_random eax - cmp ax, MIN_EPHEMERAL_PORT - jb @r - cmp ax, MAX_EPHEMERAL_PORT - ja @r - mov [last_TCP_port], ax + pseudo_random eax + cmp ax, MIN_EPHEMERAL_PORT + jb @r + cmp ax, MAX_EPHEMERAL_PORT + ja @r + mov [last_TCP_port], ax } @@ -223,34 +223,34 @@ macro SOCKET_init { ;----------------------------------------------------------------- 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 + 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 align 4 sys_socket: - cmp ebx, SOCKET_SYSFUNCS-1 - ja @f - jmp dword [sock_sysfn_table + 4*ebx] + cmp ebx, SOCKET_SYSFUNCS-1 + ja @f + jmp dword [sock_sysfn_table + 4*ebx] @@: - cmp ebx, 255 - jz SOCKET_debug + cmp ebx, 255 + jz SOCKET_debug s_error: - DEBUGF 1,"socket error\n" - mov dword [esp+32], -1 + DEBUGF 1,"socket error\n" + mov dword [esp+32], -1 - ret + ret @@ -267,79 +267,79 @@ s_error: align 4 SOCKET_open: - DEBUGF 1,"SOCKET_open: domain: %u, type: %u protocol: %x\n", ecx, edx, esi + DEBUGF 1,"SOCKET_open: domain: %u, type: %u protocol: %x\n", ecx, edx, esi - push ecx edx esi - call SOCKET_alloc - pop esi edx ecx - jz s_error + push ecx edx esi + call SOCKET_alloc + pop esi edx ecx + jz s_error - mov [esp+32], edi ; return socketnumber + mov [esp+32], edi ; return socketnumber - mov [eax + SOCKET.Domain], ecx - mov [eax + SOCKET.Type], edx - mov [eax + SOCKET.Protocol], esi + mov [eax + SOCKET.Domain], ecx + mov [eax + SOCKET.Type], edx + mov [eax + SOCKET.Protocol], esi - cmp ecx, AF_INET4 - jne .no_inet4 + cmp ecx, AF_INET4 + jne .no_inet4 - cmp edx, SOCK_DGRAM - je .udp + cmp edx, SOCK_DGRAM + je .udp - cmp edx, SOCK_STREAM - je .tcp + cmp edx, SOCK_STREAM + je .tcp - cmp edx, SOCK_RAW - je .raw + cmp edx, SOCK_RAW + je .raw .no_inet4: - ret + ret align 4 .raw: - test esi, esi ; IP_PROTO_IP - jz .ip + test esi, esi ; IP_PROTO_IP + jz .ip - cmp esi, IP_PROTO_ICMP - je .icmp + cmp esi, IP_PROTO_ICMP + je .icmp - cmp esi, IP_PROTO_UDP - je .udp + cmp esi, IP_PROTO_UDP + je .udp - cmp esi, IP_PROTO_TCP - je .tcp + cmp esi, IP_PROTO_TCP + je .tcp - ret + ret align 4 .udp: - mov [eax + SOCKET.Protocol], IP_PROTO_UDP - mov [eax + SOCKET.snd_proc], SOCKET_send_udp - mov [eax + SOCKET.rcv_proc], SOCKET_receive_dgram - ret + mov [eax + SOCKET.Protocol], IP_PROTO_UDP + mov [eax + SOCKET.snd_proc], SOCKET_send_udp + mov [eax + SOCKET.rcv_proc], SOCKET_receive_dgram + ret align 4 .tcp: - mov [eax + SOCKET.Protocol], IP_PROTO_TCP - mov [eax + SOCKET.snd_proc], SOCKET_send_tcp - mov [eax + SOCKET.rcv_proc], SOCKET_receive_tcp + mov [eax + SOCKET.Protocol], IP_PROTO_TCP + mov [eax + SOCKET.snd_proc], SOCKET_send_tcp + mov [eax + SOCKET.rcv_proc], SOCKET_receive_tcp - mov [eax + TCP_SOCKET.t_maxseg], 1480 ;;;;; FIXME - ret + mov [eax + TCP_SOCKET.t_maxseg], 1480 ;;;;; FIXME + ret align 4 .ip: - mov [eax + SOCKET.snd_proc], SOCKET_send_ip - mov [eax + SOCKET.rcv_proc], SOCKET_receive_dgram - ret + mov [eax + SOCKET.snd_proc], SOCKET_send_ip + mov [eax + SOCKET.rcv_proc], SOCKET_receive_dgram + ret align 4 .icmp: - mov [eax + SOCKET.snd_proc], SOCKET_send_icmp - mov [eax + SOCKET.rcv_proc], SOCKET_receive_dgram - ret + mov [eax + SOCKET.snd_proc], SOCKET_send_icmp + mov [eax + SOCKET.rcv_proc], SOCKET_receive_dgram + ret @@ -356,47 +356,47 @@ align 4 align 4 SOCKET_bind: - DEBUGF 1,"socket_bind: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi + DEBUGF 1,"socket_bind: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi - call SOCKET_num_to_ptr - jz s_error + call SOCKET_num_to_ptr + jz s_error - cmp esi, 2 - jb s_error + cmp esi, 2 + jb s_error - cmp word [edx], AF_INET4 - je .af_inet4 + cmp word [edx], AF_INET4 + je .af_inet4 - cmp word [edx], AF_UNIX - je .af_unix + cmp word [edx], AF_UNIX + je .af_unix - jmp s_error + jmp s_error .af_unix: - ; TODO: write code here + ; TODO: write code here - mov dword [esp+32], 0 - ret + mov dword [esp+32], 0 + ret .af_inet4: - DEBUGF 1,"af_inet4\n" + DEBUGF 1,"af_inet4\n" - cmp esi, 6 - jb s_error + cmp esi, 6 + jb s_error - pushw [edx + 2] - pop [eax + UDP_SOCKET.LocalPort] + pushw [edx + 2] + pop [eax + UDP_SOCKET.LocalPort] - pushd [edx + 4] - pop [eax + IP_SOCKET.LocalIP] + pushd [edx + 4] + pop [eax + IP_SOCKET.LocalIP] - DEBUGF 1,"local ip: %u.%u.%u.%u\n",\ - [eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\ - [eax + IP_SOCKET.LocalIP + 2]:1,[eax + IP_SOCKET.LocalIP + 3]:1 + DEBUGF 1,"local ip: %u.%u.%u.%u\n",\ + [eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\ + [eax + IP_SOCKET.LocalIP + 2]:1,[eax + IP_SOCKET.LocalIP + 3]:1 - mov dword [esp+32], 0 - ret + mov dword [esp+32], 0 + ret @@ -414,124 +414,137 @@ SOCKET_bind: align 4 SOCKET_connect: - DEBUGF 1,"SOCKET_connect: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi + DEBUGF 1,"SOCKET_connect: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi - call SOCKET_num_to_ptr - jz s_error + call SOCKET_num_to_ptr + jz s_error - cmp esi, 8 - jb s_error + cmp esi, 8 + jb s_error - cmp word [edx], AF_INET4 - je .af_inet4 + cmp word [edx], AF_INET4 + je .af_inet4 - jmp s_error + jmp s_error .af_inet4: - cmp [eax + IP_SOCKET.LocalIP], 0 - jne @f - push [IP_LIST] - pop [eax + IP_SOCKET.LocalIP] + cmp [eax + IP_SOCKET.LocalIP], 0 + jne @f + push [IP_LIST] + pop [eax + IP_SOCKET.LocalIP] @@: - cmp [eax + SOCKET.Protocol], IP_PROTO_UDP - je .udp + cmp [eax + SOCKET.Protocol], IP_PROTO_UDP + je .udp - cmp [eax + SOCKET.Protocol], IP_PROTO_TCP - je .tcp + cmp [eax + SOCKET.Protocol], IP_PROTO_TCP + je .tcp - cmp [eax + SOCKET.Protocol], IP_PROTO_IP - je .ip + cmp [eax + SOCKET.Protocol], IP_PROTO_IP + je .ip - cmp [eax + SOCKET.Protocol], IP_PROTO_ICMP - je .ip + cmp [eax + SOCKET.Protocol], IP_PROTO_ICMP + je .ip - jmp s_error + jmp s_error align 4 .udp: - lea ebx, [eax + SOCKET.lock] - call wait_mutex + pusha + lea ecx, [eax + SOCKET.mutex] + call mutex_lock + popa - pushw [edx + 2] - pop [eax + UDP_SOCKET.RemotePort] + pushw [edx + 2] + pop [eax + UDP_SOCKET.RemotePort] - pushd [edx + 4] - pop [eax + IP_SOCKET.RemoteIP] + pushd [edx + 4] + pop [eax + IP_SOCKET.RemoteIP] - cmp [eax + UDP_SOCKET.LocalPort], 0 - jne @f - call SOCKET_find_port + cmp [eax + UDP_SOCKET.LocalPort], 0 + jne @f + call SOCKET_find_port @@: - mov [eax + UDP_SOCKET.firstpacket], 0 + mov [eax + UDP_SOCKET.firstpacket], 0 - push eax - init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue - pop eax + 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 - ret + lea ecx, [eax + SOCKET.mutex] + call mutex_unlock + + mov dword [esp+32], 0 + ret align 4 .tcp: - lea ebx, [eax + SOCKET.lock] - call wait_mutex + pusha + lea ecx, [eax + SOCKET.mutex] + call mutex_lock + popa - pushw [edx + 2] - pop [eax + TCP_SOCKET.RemotePort] + pushw [edx + 2] + pop [eax + TCP_SOCKET.RemotePort] - pushd [edx + 4] - pop [eax + IP_SOCKET.RemoteIP] + pushd [edx + 4] + pop [eax + IP_SOCKET.RemoteIP] - cmp [eax + TCP_SOCKET.LocalPort], 0 - jne @f - call SOCKET_find_port + cmp [eax + TCP_SOCKET.LocalPort], 0 + jne @f + call SOCKET_find_port @@: - mov [eax + TCP_SOCKET.timer_persist], 0 - mov [eax + TCP_SOCKET.t_state], TCPS_SYN_SENT - push [TCP_sequence_num] - add [TCP_sequence_num], 6400 - pop [eax + TCP_SOCKET.ISS] - mov [eax + TCP_SOCKET.timer_keepalive], TCP_time_keep_init + mov [eax + TCP_SOCKET.timer_persist], 0 + mov [eax + TCP_SOCKET.t_state], TCPS_SYN_SENT + push [TCP_sequence_num] + add [TCP_sequence_num], 6400 + pop [eax + TCP_SOCKET.ISS] + mov [eax + TCP_SOCKET.timer_keepalive], TCP_time_keep_init - TCP_sendseqinit eax + TCP_sendseqinit eax ; mov [ebx + TCP_SOCKET.timer_retransmission], ;; todo: create macro to set retransmission timer - mov ebx, eax + mov ebx, eax - lea eax, [ebx + STREAM_SOCKET.snd] - call SOCKET_ring_create + lea eax, [ebx + STREAM_SOCKET.snd] + call SOCKET_ring_create - lea eax, [ebx + STREAM_SOCKET.rcv] - call SOCKET_ring_create + lea eax, [ebx + STREAM_SOCKET.rcv] + call SOCKET_ring_create - mov [ebx + SOCKET.lock], 0 + pusha + lea ecx, [ebx + SOCKET.mutex] + call mutex_unlock + popa - mov eax, ebx - call TCP_output + mov eax, ebx + call TCP_output - mov dword [esp+32], 0 - ret + mov dword [esp+32], 0 + ret align 4 .ip: - lea ebx, [eax + SOCKET.lock] - call wait_mutex + pusha + lea ecx, [eax + SOCKET.mutex] + call mutex_lock + popa - pushd [edx + 4] - pop [eax + IP_SOCKET.RemoteIP] + pushd [edx + 4] + pop [eax + IP_SOCKET.RemoteIP] - push eax - init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue - pop eax + 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 - ret + lea ecx, [eax + SOCKET.mutex] + call mutex_unlock + + mov dword [esp+32], 0 + ret ;----------------------------------------------------------------- @@ -546,42 +559,42 @@ align 4 align 4 SOCKET_listen: - DEBUGF 1,"SOCKET_listen: socknum: %u backlog: %u\n", ecx, edx + DEBUGF 1,"SOCKET_listen: socknum: %u backlog: %u\n", ecx, edx - call SOCKET_num_to_ptr - jz s_error + call SOCKET_num_to_ptr + jz s_error - cmp [eax + SOCKET.Domain], AF_INET4 - jne s_error + cmp [eax + SOCKET.Domain], AF_INET4 + jne s_error - cmp [eax + SOCKET.Protocol], IP_PROTO_TCP - jne s_error + cmp [eax + SOCKET.Protocol], IP_PROTO_TCP + jne s_error - cmp [eax + TCP_SOCKET.LocalPort], 0 - je s_error + 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] + cmp [eax + IP_SOCKET.LocalIP], 0 + jne @f + push [IP_LIST] + pop [eax + IP_SOCKET.LocalIP] @@: - cmp edx, MAX_backlog - jbe @f - mov edx, MAX_backlog + 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 + SOCKET.backlog], dx + or [eax + SOCKET.options], SO_ACCEPTCON + mov [eax + TCP_SOCKET.t_state], TCPS_LISTEN - push eax - init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up sockets queue - pop eax + push eax + init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up sockets queue + pop eax - mov dword [esp+32], 0 + mov dword [esp+32], 0 - ret + ret ;----------------------------------------------------------------- @@ -597,27 +610,27 @@ SOCKET_listen: align 4 SOCKET_accept: - DEBUGF 1,"SOCKET_accept: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi + DEBUGF 1,"SOCKET_accept: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi - call SOCKET_num_to_ptr - jz s_error + call SOCKET_num_to_ptr + jz s_error - test [eax + SOCKET.options], SO_ACCEPTCON - jz s_error + test [eax + SOCKET.options], SO_ACCEPTCON + jz s_error - cmp [eax + SOCKET.Domain], AF_INET4 - jne s_error + cmp [eax + SOCKET.Domain], AF_INET4 + jne s_error - cmp [eax + SOCKET.Protocol], IP_PROTO_TCP - jne s_error + cmp [eax + SOCKET.Protocol], IP_PROTO_TCP + jne s_error - get_from_queue (eax + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, s_error + get_from_queue (eax + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, s_error - mov eax, [esi] - call SOCKET_ptr_to_num - jz s_error - mov [esp+32], eax - ret + mov eax, [esi] + call SOCKET_ptr_to_num + jz s_error + mov [esp+32], eax + ret ;----------------------------------------------------------------- @@ -631,42 +644,42 @@ SOCKET_accept: align 4 SOCKET_close: - DEBUGF 1,"SOCKET_close: socknum: %u\n", ecx + DEBUGF 1,"SOCKET_close: socknum: %u\n", ecx - call SOCKET_num_to_ptr - jz s_error + call SOCKET_num_to_ptr + jz s_error - cmp [eax + SOCKET.Domain], AF_INET4 - jne s_error + cmp [eax + SOCKET.Domain], AF_INET4 + jne s_error - cmp [eax + SOCKET.Protocol], IP_PROTO_UDP - je .free + cmp [eax + SOCKET.Protocol], IP_PROTO_UDP + je .free - cmp [eax + SOCKET.Protocol], IP_PROTO_ICMP - je .free + cmp [eax + SOCKET.Protocol], IP_PROTO_ICMP + je .free - cmp [eax + SOCKET.Protocol], IP_PROTO_IP - je .free + cmp [eax + SOCKET.Protocol], IP_PROTO_IP + je .free - cmp [eax + SOCKET.Protocol], IP_PROTO_TCP - je .tcp + cmp [eax + SOCKET.Protocol], IP_PROTO_TCP + je .tcp - jmp s_error + jmp s_error .tcp: - cmp [eax + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED ; state must be LISTEN, SYN_SENT or CLOSED - jb .free + cmp [eax + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED ; state must be LISTEN, SYN_SENT or CLOSED + jb .free - call TCP_output - mov dword [esp+32], 0 + call TCP_output + mov dword [esp+32], 0 - ret + ret .free: - call SOCKET_free - mov dword [esp+32], 0 + call SOCKET_free + mov dword [esp+32], 0 - ret + ret ;----------------------------------------------------------------- @@ -683,71 +696,71 @@ SOCKET_close: align 4 SOCKET_receive: - DEBUGF 1,"SOCKET_receive: socknum: %u bufaddr: %x, buflength: %u, flags: %x, ", ecx, edx, esi, edi + DEBUGF 1,"SOCKET_receive: socknum: %u bufaddr: %x, buflength: %u, flags: %x, ", ecx, edx, esi, edi - call SOCKET_num_to_ptr - jz s_error + call SOCKET_num_to_ptr + jz s_error - jmp [eax + SOCKET.rcv_proc] + jmp [eax + SOCKET.rcv_proc] align 4 SOCKET_receive_dgram: - DEBUGF 1,"SOCKET_receive: DGRAM\n" + DEBUGF 1,"SOCKET_receive: DGRAM\n" - mov ebx, esi - mov edi, edx ; addr to buffer + mov ebx, esi + mov edi, edx ; addr to buffer - get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, s_error ; destroys esi and ecx + get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, s_error ; destroys esi and ecx - mov ecx, [esi + socket_queue_entry.data_size] - DEBUGF 1,"Got %u bytes of data\n", ecx + mov ecx, [esi + socket_queue_entry.data_size] + DEBUGF 1,"Got %u bytes of data\n", ecx - cmp ecx, ebx - ja .too_small + cmp ecx, ebx + ja .too_small - 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] - DEBUGF 1,"Source buffer: %x, real addr: %x\n", [esp], esi - mov [esp+32+4], ecx ; return number of bytes copied + 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] + DEBUGF 1,"Source buffer: %x, real addr: %x\n", [esp], esi + mov [esp+32+4], ecx ; return number of bytes copied ; copy the data - shr ecx, 1 - jnc .nb - movsb + shr ecx, 1 + jnc .nb + movsb .nb: - shr ecx, 1 - jnc .nw - movsw + shr ecx, 1 + jnc .nw + movsw .nw: - test ecx, ecx - jz .nd - rep movsd + test ecx, ecx + jz .nd + rep movsd .nd: - call kernel_free ; remove the packet - ret + call kernel_free ; remove the packet + ret .too_small: - DEBUGF 1,"Buffer too small...\n" - jmp s_error + DEBUGF 1,"Buffer too small...\n" + jmp s_error align 4 SOCKET_receive_tcp: - DEBUGF 1,"SOCKET_receive: TCP\n" + DEBUGF 1,"SOCKET_receive: TCP\n" - mov ecx, esi - mov edi, edx - add eax, STREAM_SOCKET.rcv - call SOCKET_ring_read - call SOCKET_ring_free + mov ecx, esi + mov edi, edx + add eax, STREAM_SOCKET.rcv + call SOCKET_ring_read + call SOCKET_ring_free - mov [esp+32], ecx ; return number of bytes copied + mov [esp+32], ecx ; return number of bytes copied - ret + ret ;----------------------------------------------------------------- @@ -765,63 +778,63 @@ SOCKET_receive_tcp: align 4 SOCKET_send: - DEBUGF 1,"SOCKET_send: socknum: %u data ptr: %x, length: %u, flags: %x, ", ecx, edx, esi, edi + DEBUGF 1,"SOCKET_send: socknum: %u data ptr: %x, length: %u, flags: %x, ", ecx, edx, esi, edi - call SOCKET_num_to_ptr - jz s_error + call SOCKET_num_to_ptr + jz s_error - mov ecx, esi - mov esi, edx + mov ecx, esi + mov esi, edx - jmp [eax + SOCKET.snd_proc] + jmp [eax + SOCKET.snd_proc] align 4 SOCKET_send_udp: - DEBUGF 1,"SOCKET_send: UDP\n" + DEBUGF 1,"SOCKET_send: UDP\n" - call UDP_output + call UDP_output - mov [esp+32], eax - ret + mov [esp+32], eax + ret align 4 SOCKET_send_tcp: - DEBUGF 1,"SOCKET_send: TCP\n" + DEBUGF 1,"SOCKET_send: TCP\n" - push eax - add eax, STREAM_SOCKET.snd - call SOCKET_ring_write - pop eax + push eax + add eax, STREAM_SOCKET.snd + call SOCKET_ring_write + pop eax - call TCP_output + call TCP_output - mov [esp+32], eax - ret + mov [esp+32], eax + ret align 4 SOCKET_send_ip: - DEBUGF 1,"type: IP\n" + DEBUGF 1,"type: IP\n" - call IPv4_output_raw + call IPv4_output_raw - mov [esp+32], eax - ret + mov [esp+32], eax + ret align 4 SOCKET_send_icmp: - DEBUGF 1,"SOCKET_send: ICMP\n" + DEBUGF 1,"SOCKET_send: ICMP\n" - call ICMP_output_raw + call ICMP_output_raw - mov [esp+32], eax - ret + mov [esp+32], eax + ret @@ -843,17 +856,17 @@ SOCKET_send_icmp: align 4 SOCKET_get_opt: - DEBUGF 1,"SOCKET_get_opt\n" + DEBUGF 1,"SOCKET_get_opt\n" - call SOCKET_num_to_ptr - jz s_error + call SOCKET_num_to_ptr + jz s_error - cmp dword [edx], IP_PROTO_TCP - jne s_error - cmp dword [edx+4], -2 - je @f - cmp dword [edx+4], -3 - jne s_error + cmp dword [edx], IP_PROTO_TCP + jne s_error + cmp dword [edx+4], -2 + je @f + cmp dword [edx+4], -3 + jne s_error @@: ; mov eax, [edx+12] ; test eax, eax @@ -870,13 +883,13 @@ SOCKET_get_opt: ; jz @f ; mov ecx, [eax + TCP_SOCKET.state] @@: - mov eax, [edx+8] - test eax, eax - jz @f - mov [eax], ecx + mov eax, [edx+8] + test eax, eax + jz @f + mov [eax], ecx @@: - mov dword [esp+32], 0 - ret + mov dword [esp+32], 0 + ret @@ -884,7 +897,7 @@ SOCKET_get_opt: align 4 SOCKET_set_opt: - ret + ret @@ -902,18 +915,18 @@ SOCKET_set_opt: align 4 SOCKET_debug: - DEBUGF 1,"socket_debug\n" + DEBUGF 1,"socket_debug\n" - call SOCKET_num_to_ptr - jz s_error + call SOCKET_num_to_ptr + jz s_error - mov esi, eax - mov edi, edx - mov ecx, SOCKETBUFFSIZE/4 - rep movsd + mov esi, eax + mov edi, edx + mov ecx, SOCKETBUFFSIZE/4 + rep movsd - mov dword [esp+32], 0 - ret + mov dword [esp+32], 0 + ret ;----------------------------------------------------------------- @@ -931,49 +944,49 @@ SOCKET_debug: align 4 SOCKET_find_port: - DEBUGF 1,"SOCKET_find_port\n" + DEBUGF 1,"SOCKET_find_port\n" - push ebx esi ecx + push ebx esi ecx - cmp [eax + SOCKET.Protocol], IP_PROTO_UDP - je .udp + cmp [eax + SOCKET.Protocol], IP_PROTO_UDP + je .udp - cmp [eax + SOCKET.Protocol], IP_PROTO_TCP - je .tcp + cmp [eax + SOCKET.Protocol], IP_PROTO_TCP + je .tcp - jmp .error + jmp .error .done: - mov [eax + UDP_SOCKET.LocalPort], bx + mov [eax + UDP_SOCKET.LocalPort], bx .error: - pop ecx esi ebx - ret + pop ecx esi ebx + ret .udp: - mov bx, [last_UDP_port] - call .findit - mov [last_UDP_port], bx - jmp .done + mov bx, [last_UDP_port] + call .findit + mov [last_UDP_port], bx + jmp .done .tcp: - mov bx, [last_TCP_port] - call .findit - mov [last_TCP_port], bx - jmp .done + mov bx, [last_TCP_port] + call .findit + mov [last_TCP_port], bx + jmp .done .restart: - mov bx, MIN_EPHEMERAL_PORT + mov bx, MIN_EPHEMERAL_PORT .findit: - inc bx + inc bx - cmp bx, MAX_EPHEMERAL_PORT - jz .restart + cmp bx, MAX_EPHEMERAL_PORT + jz .restart - call SOCKET_check_port - jz .findit + call SOCKET_check_port + jz .findit - ret + ret @@ -993,30 +1006,30 @@ SOCKET_find_port: align 4 SOCKET_check_port: - DEBUGF 1,"SOCKET_check_port\n" + DEBUGF 1,"SOCKET_check_port\n" - mov ecx, [eax + SOCKET.Protocol] - mov esi, net_sockets + mov ecx, [eax + SOCKET.Protocol] + mov esi, net_sockets .next_socket: - mov esi, [esi + SOCKET.NextPtr] - or esi, esi - jz .port_ok + mov esi, [esi + SOCKET.NextPtr] + or esi, esi + jz .port_ok - cmp [esi + SOCKET.Protocol], ecx - jne .next_socket + cmp [esi + SOCKET.Protocol], ecx + jne .next_socket - cmp [esi + UDP_SOCKET.LocalPort], bx - jne .next_socket + cmp [esi + UDP_SOCKET.LocalPort], bx + jne .next_socket - DEBUGF 1,"local port %u already in use\n", bx - ret + DEBUGF 1,"local port %u already in use\n", bx + ret .port_ok: - mov [eax + UDP_SOCKET.LocalPort], bx - or bx, bx ; set the zero-flag + mov [eax + UDP_SOCKET.LocalPort], bx + or bx, bx ; set the zero-flag - ret + ret @@ -1041,26 +1054,36 @@ SOCKET_check_port: align 4 SOCKET_input: - DEBUGF 1,"SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx + DEBUGF 1,"SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx - mov [esp+4], ecx - push esi - mov esi, esp + mov [esp+4], ecx + push esi + mov esi, esp - add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, SOCKET_input.full + add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, SOCKET_input.full - DEBUGF 1,"SOCKET_input: queued packet successfully\n" - add esp, sizeof.socket_queue_entry - mov [eax + SOCKET.lock], 0 - jmp SOCKET_notify_owner + DEBUGF 1,"SOCKET_input: queued packet successfully\n" + add esp, sizeof.socket_queue_entry + + pusha + lea ecx, [eax + SOCKET.mutex] + call mutex_unlock + popa + + jmp SOCKET_notify_owner .full: - DEBUGF 2,"SOCKET_input: socket %x is full!\n", eax - mov [eax + SOCKET.lock], 0 - call kernel_free - add esp, 8 + DEBUGF 2,"SOCKET_input: socket %x is full!\n", eax - ret + pusha + lea ecx, [eax + SOCKET.mutex] + call mutex_unlock + popa + + call kernel_free + add esp, 8 + + ret ;-------------------------- @@ -1070,24 +1093,24 @@ SOCKET_input: align 4 SOCKET_ring_create: - push esi - mov esi, eax + push esi + mov esi, eax - push edx - stdcall create_ring_buffer, SOCKET_MAXDATA, PG_SW - pop edx + push edx + stdcall create_ring_buffer, SOCKET_MAXDATA, PG_SW + pop edx - 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 - mov eax, esi - pop esi + 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 + mov eax, esi + pop esi - ret + ret ;----------------------------------------------------------------- ; @@ -1105,55 +1128,55 @@ SOCKET_ring_create: align 4 SOCKET_ring_write: - DEBUGF 1,"SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx + DEBUGF 1,"SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx - add [eax + RING_BUFFER.size], ecx - cmp [eax + RING_BUFFER.size], SOCKET_MAXDATA - ja .too_large + add [eax + RING_BUFFER.size], ecx + cmp [eax + RING_BUFFER.size], SOCKET_MAXDATA + ja .too_large .copy: - mov edi, [eax + RING_BUFFER.write_ptr] - DEBUGF 2,"SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi + mov edi, [eax + RING_BUFFER.write_ptr] + DEBUGF 2,"SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi - push ecx - shr ecx, 1 - jnc .nb - movsb + push ecx + shr ecx, 1 + jnc .nb + movsb .nb: - shr ecx, 1 - jnc .nw - movsw + shr ecx, 1 + jnc .nw + movsw .nw: - test ecx, ecx - jz .nd - rep movsd + test ecx, ecx + jz .nd + rep movsd .nd: - pop ecx + pop ecx - cmp edi, [eax + RING_BUFFER.end_ptr] - jae .wrap - mov [eax + RING_BUFFER.write_ptr], edi + cmp edi, [eax + RING_BUFFER.end_ptr] + jae .wrap + mov [eax + RING_BUFFER.write_ptr], edi - ret + ret .wrap: - sub edi, SOCKET_MAXDATA - mov [eax + RING_BUFFER.write_ptr], edi + sub edi, SOCKET_MAXDATA + mov [eax + RING_BUFFER.write_ptr], edi - ret + ret .too_large: - mov ecx, SOCKET_MAXDATA ; calculate number of bytes available in buffer - sub ecx, [eax + RING_BUFFER.size] - jae .full + mov ecx, SOCKET_MAXDATA ; calculate number of bytes available in buffer + sub ecx, [eax + RING_BUFFER.size] + jae .full - mov [eax + RING_BUFFER.size], SOCKET_MAXDATA ; update size, we will fill buffer completely - jmp .copy + mov [eax + RING_BUFFER.size], SOCKET_MAXDATA ; update size, we will fill buffer completely + jmp .copy .full: - DEBUGF 2,"SOCKET_ring_write: ring buffer is full!\n" - xor ecx, ecx - ret + DEBUGF 2,"SOCKET_ring_write: ring buffer is full!\n" + xor ecx, ecx + ret ;----------------------------------------------------------------- @@ -1172,38 +1195,38 @@ SOCKET_ring_write: align 4 SOCKET_ring_read: - DEBUGF 1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u\n", eax, edi, ecx + DEBUGF 1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u\n", eax, edi, ecx - cmp ecx, [eax + RING_BUFFER.size] - ja .less_data + cmp ecx, [eax + RING_BUFFER.size] + ja .less_data .copy: - mov esi, [eax + RING_BUFFER.read_ptr] + mov esi, [eax + RING_BUFFER.read_ptr] - DEBUGF 2,"SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi - push ecx - shr ecx, 1 - jnc .nb - movsb + DEBUGF 2,"SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi + push ecx + shr ecx, 1 + jnc .nb + movsb .nb: - shr ecx, 1 - jnc .nw - movsw + shr ecx, 1 + jnc .nw + movsw .nw: - test ecx, ecx - jz .nd - rep movsd + test ecx, ecx + jz .nd + rep movsd .nd: - pop ecx + pop ecx .no_data_at_all: - ret + ret .less_data: - mov ecx, [eax + RING_BUFFER.size] + mov ecx, [eax + RING_BUFFER.size] ; test ecx, ecx ; jz .no_data_at_all - jmp .copy + jmp .copy ;----------------------------------------------------------------- @@ -1221,23 +1244,23 @@ SOCKET_ring_read: align 4 SOCKET_ring_free: - DEBUGF 1,"SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax + DEBUGF 1,"SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax - sub [eax + RING_BUFFER.size], ecx - jb .sumthinwong - add [eax + RING_BUFFER.read_ptr], ecx + sub [eax + RING_BUFFER.size], ecx + jb .sumthinwong + add [eax + RING_BUFFER.read_ptr], ecx - mov edx, [eax + RING_BUFFER.end_ptr] - cmp [eax + RING_BUFFER.read_ptr], edx - jb @f - sub [eax + RING_BUFFER.read_ptr], SOCKET_MAXDATA + mov edx, [eax + RING_BUFFER.end_ptr] + cmp [eax + RING_BUFFER.read_ptr], edx + jb @f + sub [eax + RING_BUFFER.read_ptr], SOCKET_MAXDATA @@: - ret + ret - .sumthinwong: ; we could free all available bytes, but that would be stupid, i guess.. - add [eax + RING_BUFFER.size], ecx - xor ecx, ecx - ret + .sumthinwong: ; we could free all available bytes, but that would be stupid, i guess.. + add [eax + RING_BUFFER.size], ecx + xor ecx, ecx + ret ;----------------------------------------------------------------- @@ -1253,44 +1276,44 @@ SOCKET_ring_free: align 4 SOCKET_notify_owner: - DEBUGF 1,"SOCKET_notify_owner: %x\n", eax + DEBUGF 1,"SOCKET_notify_owner: %x\n", eax - call SOCKET_check - jz .error + call SOCKET_check + jz .error - push eax ecx esi + push eax ecx esi ; socket exists, now try to flag an event to the application - mov eax, [eax + SOCKET.PID] - mov ecx, 1 - mov esi, TASK_DATA + TASKDATA.pid + mov eax, [eax + SOCKET.PID] + mov ecx, 1 + mov esi, TASK_DATA + TASKDATA.pid .next_pid: - cmp [esi], eax - je .found_pid - inc ecx - add esi, 0x20 - cmp ecx, [TASK_COUNT] - jbe .next_pid + cmp [esi], eax + je .found_pid + inc ecx + add esi, 0x20 + cmp ecx, [TASK_COUNT] + jbe .next_pid ; PID not found, TODO: close socket! - jmp .error2 + jmp .error2 .found_pid: - shl ecx, 8 - or [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK - mov [check_idle_semaphore], 200 + shl ecx, 8 + or [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK + mov [check_idle_semaphore], 200 - DEBUGF 1,"SOCKET_notify_owner: succes!\n" + DEBUGF 1,"SOCKET_notify_owner: succes!\n" .error2: - pop esi ecx eax + pop esi ecx eax .error: - ret + ret ;-------------------------------------------------------------------- @@ -1310,73 +1333,80 @@ SOCKET_notify_owner: align 4 SOCKET_alloc: - push ebx + push ebx - stdcall kernel_alloc, SOCKETBUFFSIZE - DEBUGF 1, "SOCKET_alloc: ptr=%x\n", eax - or eax, eax - jz .exit + stdcall kernel_alloc, SOCKETBUFFSIZE + DEBUGF 1, "SOCKET_alloc: ptr=%x\n", eax + or eax, eax + jz .exit ; zero-initialize allocated memory - push eax - mov edi, eax - mov ecx, SOCKETBUFFSIZE / 4 - xor eax, eax - rep stosd - pop eax + push eax + mov edi, eax + mov ecx, SOCKETBUFFSIZE / 4 + xor eax, eax + rep stosd + pop eax ; set send-and receive procedures to return -1 - mov [eax + SOCKET.snd_proc], s_error - mov [eax + SOCKET.rcv_proc], s_error + mov [eax + SOCKET.snd_proc], s_error + mov [eax + SOCKET.rcv_proc], s_error ; find first free socket number and use it - mov edi, [last_socket_num] + 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 + 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 .next_socket: - mov ebx, [ebx + SOCKET.NextPtr] - test ebx, ebx - jz .last_socket + mov ebx, [ebx + SOCKET.NextPtr] + test ebx, ebx + jz .last_socket - cmp [ebx + SOCKET.Number], edi - jne .next_socket - jmp .next_socket_number + cmp [ebx + SOCKET.Number], edi + jne .next_socket + jmp .next_socket_number .last_socket: - mov [last_socket_num], edi - mov [eax + SOCKET.Number], edi - DEBUGF 1, "SOCKET_alloc: number=%u\n", edi + mov [last_socket_num], edi + mov [eax + SOCKET.Number], edi + DEBUGF 1, "SOCKET_alloc: number=%u\n", edi ; Fill in PID - mov ebx, [TASK_BASE] - mov ebx, [ebx + TASKDATA.pid] - mov [eax + SOCKET.PID], ebx + mov ebx, [TASK_BASE] + mov ebx, [ebx + TASKDATA.pid] + mov [eax + SOCKET.PID], ebx ; add socket to the list by re-arranging some pointers - mov ebx, [net_sockets + SOCKET.NextPtr] + mov ebx, [net_sockets + SOCKET.NextPtr] - mov [eax + SOCKET.PrevPtr], net_sockets - mov [eax + SOCKET.NextPtr], ebx + mov [eax + SOCKET.PrevPtr], net_sockets + mov [eax + SOCKET.NextPtr], ebx - test ebx, ebx - jz @f - add ebx, SOCKET.lock ; lock the next socket - call wait_mutex - sub ebx, SOCKET.lock - mov [ebx + SOCKET.PrevPtr], eax - mov [ebx + SOCKET.lock], 0 ; and unlock it again + test ebx, ebx + jz @f + + pusha + lea ecx, [ebx + SOCKET.mutex] + call mutex_lock + popa + + mov [ebx + SOCKET.PrevPtr], eax + + pusha + lea ecx, [ebx + SOCKET.mutex] + call mutex_unlock + popa @@: - mov [net_sockets + SOCKET.NextPtr], eax - or eax, eax ; used to clear zero flag + mov [net_sockets + SOCKET.NextPtr], eax + or eax, eax ; used to clear zero flag .exit: - pop ebx + pop ebx - ret + ret ;---------------------------------------------------- @@ -1392,52 +1422,55 @@ SOCKET_alloc: align 4 SOCKET_free: - DEBUGF 1, "SOCKET_free: %x\n", eax + DEBUGF 1, "SOCKET_free: %x\n", eax - call SOCKET_check - jz .error + call SOCKET_check + jz .error - push ebx - lea ebx, [eax + SOCKET.lock] - call wait_mutex + push ebx - DEBUGF 1, "SOCKET_free: freeing socket..\n" + pusha + lea ecx, [eax + SOCKET.mutex] + call mutex_lock + popa - cmp [eax + SOCKET.Domain], AF_INET4 - jnz .no_tcp + DEBUGF 1, "SOCKET_free: freeing socket..\n" - cmp [eax + SOCKET.Protocol], IP_PROTO_TCP - jnz .no_tcp + cmp [eax + SOCKET.Domain], AF_INET4 + jnz .no_tcp - mov ebx, eax - stdcall kernel_free, [ebx + STREAM_SOCKET.rcv.start_ptr] - stdcall kernel_free, [ebx + STREAM_SOCKET.snd.start_ptr] - mov eax, ebx + cmp [eax + SOCKET.Protocol], IP_PROTO_TCP + jnz .no_tcp + + mov ebx, eax + stdcall kernel_free, [ebx + STREAM_SOCKET.rcv.start_ptr] + stdcall kernel_free, [ebx + STREAM_SOCKET.snd.start_ptr] + mov eax, ebx .no_tcp: - push eax ; this will be passed to kernel_free - mov ebx, [eax + SOCKET.NextPtr] - mov eax, [eax + SOCKET.PrevPtr] + push eax ; this will be passed to kernel_free + mov ebx, [eax + SOCKET.NextPtr] + mov eax, [eax + SOCKET.PrevPtr] - DEBUGF 1, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx + DEBUGF 1, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx - test eax, eax - jz @f - mov [eax + SOCKET.NextPtr], ebx + test eax, eax + jz @f + mov [eax + SOCKET.NextPtr], ebx @@: - test ebx, ebx - jz @f - mov [ebx + SOCKET.PrevPtr], eax + test ebx, ebx + jz @f + mov [ebx + SOCKET.PrevPtr], eax @@: - call kernel_free - pop ebx + call kernel_free + pop ebx - DEBUGF 1, "SOCKET_free: success!\n" + DEBUGF 1, "SOCKET_free: success!\n" .error: - ret + ret ;------------------------------------ ; @@ -1452,41 +1485,41 @@ SOCKET_free: align 4 SOCKET_fork: - DEBUGF 1,"SOCKET_fork: %x\n", ebx + 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] - jae .fail + mov eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size] + cmp ax, [ebx + SOCKET.backlog] + jae .fail ; Allocate new socket - push ebx - call SOCKET_alloc - pop ebx - jz .fail + push ebx + call SOCKET_alloc + pop ebx + jz .fail - push eax - mov esi, esp - add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2 - pop eax + push eax + mov esi, esp + add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2 + pop eax ; 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] - lea edi, [eax + SOCKET.PID] - mov ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4 - rep movsd + lea esi, [ebx + SOCKET.PID] + lea edi, [eax + SOCKET.PID] + mov ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4 + rep movsd - and [eax + SOCKET.options], not SO_ACCEPTCON + and [eax + SOCKET.options], not SO_ACCEPTCON - ret + ret .fail2: - add esp, 4+4+4 + add esp, 4+4+4 .fail: - DEBUGF 1,"SOCKET_fork: failed\n" - xor eax, eax - ret + DEBUGF 1,"SOCKET_fork: failed\n" + xor eax, eax + ret ;--------------------------------------------------- @@ -1503,22 +1536,22 @@ SOCKET_fork: align 4 SOCKET_num_to_ptr: - DEBUGF 1,"SOCKET_num_to_ptr: %u ", ecx + DEBUGF 1,"SOCKET_num_to_ptr: %u ", ecx - mov eax, net_sockets + mov eax, net_sockets .next_socket: - mov eax, [eax + SOCKET.NextPtr] - or eax, eax - jz .error - cmp [eax + SOCKET.Number], ecx - jne .next_socket + mov eax, [eax + SOCKET.NextPtr] + or eax, eax + jz .error + cmp [eax + SOCKET.Number], ecx + jne .next_socket - test eax, eax + test eax, eax - DEBUGF 1,"(%x)\n", eax + DEBUGF 1,"(%x)\n", eax .error: - ret + ret ;--------------------------------------------------- @@ -1535,17 +1568,17 @@ SOCKET_num_to_ptr: align 4 SOCKET_ptr_to_num: - DEBUGF 1,"SOCKET_ptr_to_num: %x ", eax + DEBUGF 1,"SOCKET_ptr_to_num: %x ", eax - call SOCKET_check - jz .error + call SOCKET_check + jz .error - mov eax, [eax + SOCKET.Number] + mov eax, [eax + SOCKET.Number] - DEBUGF 1,"(%u)\n", eax + DEBUGF 1,"(%u)\n", eax .error: - ret + ret ;--------------------------------------------------- @@ -1562,24 +1595,24 @@ SOCKET_ptr_to_num: align 4 SOCKET_check: - DEBUGF 1,"SOCKET_check: %x\n", eax + DEBUGF 1,"SOCKET_check: %x\n", eax - push ebx - mov ebx, net_sockets + push ebx + mov ebx, net_sockets .next_socket: - mov ebx, [ebx + SOCKET.NextPtr] - or ebx, ebx - jz .done - cmp ebx, eax - jnz .next_socket + mov ebx, [ebx + SOCKET.NextPtr] + or ebx, ebx + jz .done + cmp ebx, eax + jnz .next_socket .done: - mov eax, ebx - test eax, eax - pop ebx + mov eax, ebx + test eax, eax + pop ebx - ret + ret @@ -1596,15 +1629,15 @@ SOCKET_check: align 4 SOCKET_check_owner: - DEBUGF 1,"SOCKET_check_owner: %x\n", eax + DEBUGF 1,"SOCKET_check_owner: %x\n", eax - push ebx - mov ebx, [TASK_BASE] - mov ebx, [ecx + TASKDATA.pid] - cmp [eax + SOCKET.PID], ebx - pop ebx + push ebx + mov ebx, [TASK_BASE] + mov ebx, [ecx + TASKDATA.pid] + cmp [eax + SOCKET.PID], ebx + pop ebx - ret + ret @@ -1624,49 +1657,49 @@ SOCKET_check_owner: align 4 SOCKET_process_end: - DEBUGF 1,"SOCKET_process_end: %x\n", eax + DEBUGF 1,"SOCKET_process_end: %x\n", eax - push ebx - mov ebx, net_sockets + push ebx + mov ebx, net_sockets .next_socket: - mov ebx, [ebx + SOCKET.NextPtr] + mov ebx, [ebx + SOCKET.NextPtr] .test_socket: - test ebx, ebx - jz .done + test ebx, ebx + jz .done - cmp [ebx + SOCKET.PID], eax - jne .next_socket + cmp [ebx + SOCKET.PID], eax + jne .next_socket - DEBUGF 1,"closing socket %x", eax, ebx + DEBUGF 1,"closing socket %x", eax, ebx - mov [ebx + SOCKET.PID], 0 + mov [ebx + SOCKET.PID], 0 - cmp [ebx + SOCKET.Protocol], IP_PROTO_UDP - je .udp + cmp [ebx + SOCKET.Protocol], IP_PROTO_UDP + je .udp - cmp [ebx + SOCKET.Protocol], IP_PROTO_TCP - je .tcp + cmp [ebx + SOCKET.Protocol], IP_PROTO_TCP + je .tcp - jmp .next_socket ; kill all sockets for given PID + jmp .next_socket ; kill all sockets for given PID .udp: - mov eax, ebx - mov ebx, [ebx + SOCKET.NextPtr] - call SOCKET_free - jmp .test_socket + mov eax, ebx + mov ebx, [ebx + SOCKET.NextPtr] + call SOCKET_free + jmp .test_socket .tcp: - ;;; TODO + ;;; TODO - jmp .next_socket + jmp .next_socket .done: - pop ebx + pop ebx - ret + ret @@ -1684,10 +1717,10 @@ align 4 SOCKET_is_connecting: - and [eax + SOCKET.options], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING) - or [eax + SOCKET.options], SS_ISCONNECTING + and [eax + SOCKET.options], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING) + or [eax + SOCKET.options], SS_ISCONNECTING - jmp SOCKET_notify_owner + jmp SOCKET_notify_owner @@ -1704,10 +1737,10 @@ align 4 SOCKET_is_connected: - and [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING) - or [eax + SOCKET.options], SS_ISCONNECTED + and [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING) + or [eax + SOCKET.options], SS_ISCONNECTED - jmp SOCKET_notify_owner + jmp SOCKET_notify_owner @@ -1724,10 +1757,10 @@ SOCKET_is_connected: align 4 SOCKET_is_disconnecting: - and [eax + SOCKET.options], not (SS_ISCONNECTING) - or [eax + SOCKET.options], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE + and [eax + SOCKET.options], not (SS_ISCONNECTING) + or [eax + SOCKET.options], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE - jmp SOCKET_notify_owner + jmp SOCKET_notify_owner @@ -1743,10 +1776,10 @@ SOCKET_is_disconnecting: align 4 SOCKET_is_disconnected: - and [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING) - or [eax + SOCKET.options], SS_CANTRCVMORE + SS_CANTSENDMORE + and [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING) + or [eax + SOCKET.options], SS_CANTRCVMORE + SS_CANTSENDMORE - jmp SOCKET_notify_owner + jmp SOCKET_notify_owner ;----------------------------------------------------------------- @@ -1761,9 +1794,9 @@ SOCKET_is_disconnected: align 4 SOCKET_cant_recv_more: - or [eax + SOCKET.options], SS_CANTRCVMORE + or [eax + SOCKET.options], SS_CANTRCVMORE - ret + ret @@ -1779,6 +1812,6 @@ SOCKET_cant_recv_more: align 4 SOCKET_cant_send_more: - or [eax + SOCKET.options], SS_CANTSENDMORE + or [eax + SOCKET.options], SS_CANTSENDMORE - ret \ No newline at end of file + ret \ No newline at end of file diff --git a/kernel/branches/net/network/stack.inc b/kernel/branches/net/network/stack.inc index 28128761f5..19da98d3b9 100644 --- a/kernel/branches/net/network/stack.inc +++ b/kernel/branches/net/network/stack.inc @@ -151,12 +151,6 @@ macro ntohw reg { } -wait_mutex: ; stub - inc dword [ebx] - - ret - - include "queue.inc" include "ethernet.inc" diff --git a/kernel/branches/net/network/tcp_input.inc b/kernel/branches/net/network/tcp_input.inc index 6146f9bd9c..4ddcc025d1 100644 --- a/kernel/branches/net/network/tcp_input.inc +++ b/kernel/branches/net/network/tcp_input.inc @@ -143,13 +143,10 @@ TCP_input: ;---------------- ; Lock the socket - cmp [ebx + SOCKET.lock], 0 - jne .drop_not_locked ;;; HACK ! HACK ! dirty fucking HACK ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - - add ebx, SOCKET.lock - DEBUGF 1,"lock: %x\n", [ebx] - call wait_mutex - sub ebx, SOCKET.lock + pusha + lea ecx, [ebx + SOCKET.mutex] + call mutex_lock + popa DEBUGF 1,"Socket locked\n" @@ -171,7 +168,11 @@ TCP_input: DEBUGF 1,"Accepting new connection\n" - mov [ebx + SOCKET.lock], 0 + pusha + lea ecx, [ebx + SOCKET.mutex] + call mutex_unlock + popa + push ecx edx esi edi ;;; call SOCKET_fork pop edi esi edx ecx @@ -375,7 +376,11 @@ TCP_input: mov [ebx + TCP_SOCKET.timer_retransmission], 0 ; Awaken waiting processes - mov [ebx + SOCKET.lock], 0 + pusha + lea ecx, [ebx + SOCKET.mutex] + call mutex_unlock + popa + mov eax, ebx call SOCKET_notify_owner @@ -1465,7 +1470,10 @@ align 4 .dumpit: - mov [ebx + SOCKET.lock], 0 + pusha + lea ecx, [ebx + SOCKET.mutex] + call mutex_unlock + popa call kernel_free add esp, 4 @@ -1490,7 +1498,10 @@ align 4 and [ebx + TCP_SOCKET.t_flags], TF_ACKNOW - mov [ebx + SOCKET.lock], 0 + pusha + lea ecx, [ebx + SOCKET.mutex] + call mutex_unlock + popa push ebx ; mov cl, TH_ACK @@ -1516,7 +1527,10 @@ align 4 align 4 .drop_with_reset: - mov [ebx + SOCKET.lock], 0 + pusha + lea ecx, [ebx + SOCKET.mutex] + call mutex_unlock + popa .drop_with_reset_not_locked: @@ -1568,7 +1582,10 @@ align 4 align 4 .drop: - mov [ebx + SOCKET.lock], 0 + pusha + lea ecx, [ebx + SOCKET.mutex] + call mutex_unlock + popa .drop_not_locked: diff --git a/kernel/branches/net/network/tcp_output.inc b/kernel/branches/net/network/tcp_output.inc index 1b08d2247c..ff78c8aa4f 100644 --- a/kernel/branches/net/network/tcp_output.inc +++ b/kernel/branches/net/network/tcp_output.inc @@ -28,39 +28,39 @@ $Revision$ align 4 TCP_output: - DEBUGF 1,"TCP_output, socket: %x\n", eax + DEBUGF 1,"TCP_output, socket: %x\n", eax ; We'll detect the length of the data to be transmitted, and flags to be used ; If there is some data, or any critical controls to send (SYN / RST), then transmit ; Otherwise, investigate further - mov ebx, [eax + TCP_SOCKET.SND_MAX] - cmp ebx, [eax + TCP_SOCKET.SND_UNA] - jne .not_idle + mov ebx, [eax + TCP_SOCKET.SND_MAX] + cmp ebx, [eax + TCP_SOCKET.SND_UNA] + jne .not_idle - mov ebx, [eax + TCP_SOCKET.t_idle] - cmp ebx, [eax + TCP_SOCKET.t_rxtcur] - jbe .not_idle + mov ebx, [eax + TCP_SOCKET.t_idle] + cmp ebx, [eax + TCP_SOCKET.t_rxtcur] + jbe .not_idle ; We have been idle for a while and no ACKS are expected to clock out any data we send.. ; Slow start to get ack "clock" running again. - mov ebx, [eax + TCP_SOCKET.t_maxseg] - mov [eax + TCP_SOCKET.SND_CWND], ebx + mov ebx, [eax + TCP_SOCKET.t_maxseg] + mov [eax + TCP_SOCKET.SND_CWND], ebx .not_idle: .again: - mov ebx, [eax + TCP_SOCKET.SND_NXT] ; calculate offset (71) - sub ebx, [eax + TCP_SOCKET.SND_UNA] ; + mov ebx, [eax + TCP_SOCKET.SND_NXT] ; calculate offset (71) + sub ebx, [eax + TCP_SOCKET.SND_UNA] ; - mov ecx, [eax + TCP_SOCKET.SND_WND] ; determine window - cmp ecx, [eax + TCP_SOCKET.SND_CWND] ; - jb @f ; - mov ecx, [eax + TCP_SOCKET.SND_CWND] ; - @@: ; + mov ecx, [eax + TCP_SOCKET.SND_WND] ; determine window + cmp ecx, [eax + TCP_SOCKET.SND_CWND] ; + jb @f ; + mov ecx, [eax + TCP_SOCKET.SND_CWND] ; + @@: ; - call TCP_outflags ; flags in dl + call TCP_outflags ; flags in dl ;------------------------ ; data being forced out ? @@ -69,36 +69,36 @@ TCP_output: ; Otherwise, if window is small but nonzero, and timer expired, ; we will send what we can and go to transmit state - test [eax + TCP_SOCKET.t_force], -1 - jz .no_force + test [eax + TCP_SOCKET.t_force], -1 + jz .no_force - test ecx, ecx - jnz .no_zero_window + test ecx, ecx + jnz .no_zero_window - cmp ebx, [eax + STREAM_SOCKET.snd.size] - jae @f + cmp ebx, [eax + STREAM_SOCKET.snd.size] + jae @f - and dl, not (TH_FIN) ; clear the FIN flag ??? how can it be set before? + and dl, not (TH_FIN) ; clear the FIN flag ??? how can it be set before? @@: - inc ecx - jmp .no_force + inc ecx + jmp .no_force .no_zero_window: - mov [eax + TCP_SOCKET.timer_persist], 0 - mov [eax + TCP_SOCKET.t_rxtshift], 0 + mov [eax + TCP_SOCKET.timer_persist], 0 + mov [eax + TCP_SOCKET.t_rxtshift], 0 .no_force: ;-------------------------------- ; Calculate how much data to send (106) - mov esi, [eax + STREAM_SOCKET.snd.size] - cmp esi, ecx - jb @f - mov esi, ecx + mov esi, [eax + STREAM_SOCKET.snd.size] + cmp esi, ecx + jb @f + mov esi, ecx @@: - sub esi, ebx + sub esi, ebx ;------------------------ ; check for window shrink (107) @@ -106,21 +106,21 @@ TCP_output: ; If FIN has been set, but not ACKed, but we havent been called to retransmit, esi will be -1 ; Otherwise, window shrank after we sent into it. - jns .not_negative + jns .not_negative ; enter persist state - xor esi, esi + xor esi, esi ; If window shrank to 0 - test ecx, ecx - jnz @f + test ecx, ecx + jnz @f ; cancel pending retransmit - mov [eax + TCP_SOCKET.timer_retransmission], 0 + mov [eax + TCP_SOCKET.timer_retransmission], 0 ; pull SND_NXT back to (closed) window, We will enter persist state below. - push [eax + TCP_SOCKET.SND_UNA] - pop [eax + TCP_SOCKET.SND_NXT] + push [eax + TCP_SOCKET.SND_UNA] + pop [eax + TCP_SOCKET.SND_NXT] @@: @@ -131,10 +131,10 @@ TCP_output: ;--------------------------- ; Send one segment at a time (124) - cmp esi, [eax + TCP_SOCKET.t_maxseg] - jbe @f + cmp esi, [eax + TCP_SOCKET.t_maxseg] + jbe @f - mov esi, [eax + TCP_SOCKET.t_maxseg] + mov esi, [eax + TCP_SOCKET.t_maxseg] ;;; sendalot = 1 @@ -143,73 +143,73 @@ TCP_output: ;-------------------------------------------- ; Turn of FIN flag if send buffer not emptied (128) - mov edi, [eax + TCP_SOCKET.SND_NXT] - add edi, esi - sub edi, [eax + TCP_SOCKET.SND_UNA] - sub edi, [eax + STREAM_SOCKET.snd.size] - jns @f + mov edi, [eax + TCP_SOCKET.SND_NXT] + add edi, esi + sub edi, [eax + TCP_SOCKET.SND_UNA] + sub edi, [eax + STREAM_SOCKET.snd.size] + jns @f - and dl, not (TH_FIN) + and dl, not (TH_FIN) @@: ;------------------------------- ; calculate window advertisement (130) - mov ecx, SOCKET_MAXDATA - sub ecx, [eax + STREAM_SOCKET.rcv.size] + mov ecx, SOCKET_MAXDATA + sub ecx, [eax + STREAM_SOCKET.rcv.size] ;------------------------------ ; Sender silly window avoidance (131) - test esi, esi - jz .len_zero + test esi, esi + jz .len_zero - cmp esi, [eax + TCP_SOCKET.t_maxseg] - je .send + cmp esi, [eax + TCP_SOCKET.t_maxseg] + je .send - test [eax + TCP_SOCKET.t_flags], TF_NODELAY - jnz @f - ; TODO: if not 'idle', skip to next codeblock + test [eax + TCP_SOCKET.t_flags], TF_NODELAY + jnz @f + ; TODO: if not 'idle', skip to next codeblock @@: - add ebx, esi - cmp ebx, [eax + STREAM_SOCKET.snd.size] - jae .send + add ebx, esi + cmp ebx, [eax + STREAM_SOCKET.snd.size] + jae .send - test [eax + TCP_SOCKET.t_force], -1 ;;; - jnz .send + test [eax + TCP_SOCKET.t_force], -1 ;;; + jnz .send - mov ebx, [eax + TCP_SOCKET.max_sndwnd] - shr ebx, 1 - cmp esi, ebx - jae .send + mov ebx, [eax + TCP_SOCKET.max_sndwnd] + shr ebx, 1 + cmp esi, ebx + jae .send - mov ebx, [eax + TCP_SOCKET.SND_NXT] - cmp ebx, [eax + TCP_SOCKET.SND_MAX] - jb .send + mov ebx, [eax + TCP_SOCKET.SND_NXT] + cmp ebx, [eax + TCP_SOCKET.SND_MAX] + jb .send .len_zero: ;---------------------------------------- ; Check if a window update should be sent (154) - test ecx, ecx - jz .no_window + test ecx, ecx + jz .no_window - push ecx - mov cl, [eax + TCP_SOCKET.RCV_SCALE] - inc cl ; we want it *2 - mov ebx, TCP_max_win - shl ebx, cl - pop ecx - cmp ebx, ecx - cmovb ebx, ecx + push ecx + mov cl, [eax + TCP_SOCKET.RCV_SCALE] + inc cl ; we want it *2 + mov ebx, TCP_max_win + shl ebx, cl + pop ecx + cmp ebx, ecx + cmovb ebx, ecx - ; now ebx is TWICE the amount we can increase the window - ; (with TCP_max_win shl rcv_scale as the maximum) + ; now ebx is TWICE the amount we can increase the window + ; (with TCP_max_win shl rcv_scale as the maximum) - cmp ebx, [eax + TCP_SOCKET.t_maxseg] - jae .send + cmp ebx, [eax + TCP_SOCKET.t_maxseg] + jae .send ;;; cmp ebx, [eax + ] ;;; TODO: check receive buffer high water mark ;;; jae .send @@ -219,54 +219,57 @@ TCP_output: ;-------------------------- ; Should a segment be sent? (174) - test [eax + TCP_SOCKET.t_flags], TF_ACKNOW ; we need to ACK - jnz .send + test [eax + TCP_SOCKET.t_flags], TF_ACKNOW ; we need to ACK + jnz .send - test dl, TH_SYN + TH_RST ; we need to send a SYN or RST - jnz .send + test dl, TH_SYN + TH_RST ; we need to send a SYN or RST + jnz .send - mov ebx, [eax + TCP_SOCKET.SND_UP] ; when urgent pointer is beyond start of send bufer - cmp ebx, [eax + TCP_SOCKET.SND_UNA] - ja .send + mov ebx, [eax + TCP_SOCKET.SND_UP] ; when urgent pointer is beyond start of send bufer + cmp ebx, [eax + TCP_SOCKET.SND_UNA] + ja .send - test dl, TH_FIN - jz .enter_persist ; no reason to send, enter persist state + test dl, TH_FIN + jz .enter_persist ; no reason to send, enter persist state ; FIN was set, only send if not already sent, or on retransmit - test [eax + TCP_SOCKET.t_flags], TF_SENTFIN - jnz .send + test [eax + TCP_SOCKET.t_flags], TF_SENTFIN + jnz .send - mov ebx, [eax + TCP_SOCKET.SND_NXT] - cmp ebx, [eax + TCP_SOCKET.SND_UNA] - je .send + mov ebx, [eax + TCP_SOCKET.SND_NXT] + cmp ebx, [eax + TCP_SOCKET.SND_UNA] + je .send ;-------------------- ; Enter persist state (191) .enter_persist: - cmp [eax + STREAM_SOCKET.snd.size], 0 ; Data ready to send? - jne @f - cmp [eax + TCP_SOCKET.timer_retransmission], 0 - jne @f - cmp [eax + TCP_SOCKET.timer_persist], 0 ; Persist timer already expired? - jne @f + cmp [eax + STREAM_SOCKET.snd.size], 0 ; Data ready to send? + jne @f + cmp [eax + TCP_SOCKET.timer_retransmission], 0 + jne @f + cmp [eax + TCP_SOCKET.timer_persist], 0 ; Persist timer already expired? + jne @f - DEBUGF 1,"Entering persist state\n" + DEBUGF 1,"Entering persist state\n" - mov [eax + TCP_SOCKET.t_rxtshift], 0 - TCP_set_persist eax + mov [eax + TCP_SOCKET.t_rxtshift], 0 + TCP_set_persist eax @@: ;---------------------------- ; No reason to send a segment (219) - DEBUGF 1,"No reason to send a segment\n" + DEBUGF 1,"No reason to send a segment\n" - mov [eax + SOCKET.lock], 0 + pusha + lea ecx, [eax + SOCKET.mutex] + call mutex_unlock + popa - ret + ret @@ -288,75 +291,75 @@ TCP_output: .send: - DEBUGF 1,"Preparing to send a segment\n" + DEBUGF 1,"Preparing to send a segment\n" - mov edi, sizeof.TCP_header ; edi will contain headersize + mov edi, sizeof.TCP_header ; edi will contain headersize - sub esp, 8 ; create some space on stack - push eax ; save socket pointer + sub esp, 8 ; create some space on stack + push eax ; save socket pointer ;------------------------------------ ; Send options with first SYN segment - test dl, TH_SYN - jz .options_done + test dl, TH_SYN + jz .options_done - push [eax + TCP_SOCKET.ISS] - pop [eax + TCP_SOCKET.SND_NXT] + push [eax + TCP_SOCKET.ISS] + pop [eax + TCP_SOCKET.SND_NXT] - test [eax + TCP_SOCKET.t_flags], TF_NOOPT - jnz .options_done + test [eax + TCP_SOCKET.t_flags], TF_NOOPT + jnz .options_done - mov ecx, 1460 ;;;; FIXME - or ecx, TCP_OPT_MAXSEG shl 24 + 4 shl 16 - bswap ecx - push ecx - add di, 4 + mov ecx, 1460 ;;;; FIXME + or ecx, TCP_OPT_MAXSEG shl 24 + 4 shl 16 + bswap ecx + push ecx + add di, 4 - test [eax + TCP_SOCKET.t_flags], TF_REQ_SCALE - jz .no_syn + test [eax + TCP_SOCKET.t_flags], TF_REQ_SCALE + jz .no_syn - test dl, TH_ACK - jnz .scale_opt + test dl, TH_ACK + jnz .scale_opt - test [eax + TCP_SOCKET.t_flags], TF_RCVD_SCALE - jz .no_syn + test [eax + TCP_SOCKET.t_flags], TF_RCVD_SCALE + jz .no_syn .scale_opt: - movzx ecx, byte [eax + TCP_SOCKET.request_r_scale] - or ecx, TCP_OPT_WINDOW shl 24 + 4 shl 16 + TCP_OPT_NOP shl 8 - bswap ecx - pushd ecx - add di, 4 + movzx ecx, byte [eax + TCP_SOCKET.request_r_scale] + or ecx, TCP_OPT_WINDOW shl 24 + 4 shl 16 + TCP_OPT_NOP shl 8 + bswap ecx + pushd ecx + add di, 4 .no_syn: ;------------------------------------ ; Make the timestamp option if needed - test [eax + TCP_SOCKET.t_flags], TF_REQ_TSTMP - jz .no_timestamp + test [eax + TCP_SOCKET.t_flags], TF_REQ_TSTMP + jz .no_timestamp - test dl, TH_RST - jnz .no_timestamp + test dl, TH_RST + jnz .no_timestamp - test dl, TH_ACK - jz .timestamp + test dl, TH_ACK + jz .timestamp - test [eax + TCP_SOCKET.t_flags], TF_RCVD_TSTMP - jz .no_timestamp + test [eax + TCP_SOCKET.t_flags], TF_RCVD_TSTMP + jz .no_timestamp .timestamp: - mov ebx, [timer_ticks] - bswap ebx - push ebx - pushw 0 - pushd TCP_OPT_TIMESTAMP + 10 shl 8 + TCP_OPT_NOP shl 16 + TCP_OPT_NOP shl 24 - add di, 10 + mov ebx, [timer_ticks] + bswap ebx + push ebx + pushw 0 + pushd TCP_OPT_TIMESTAMP + 10 shl 8 + TCP_OPT_NOP shl 16 + TCP_OPT_NOP shl 24 + add di, 10 .no_timestamp: - ; + ; @@ -375,11 +378,11 @@ TCP_output: ;--------------------------------------------- ; check if we dont exceed the max segment size (270) - add esi, edi ; total TCP segment size - cmp esi, [eax + TCP_SOCKET.t_maxseg] - jbe .no_overflow + add esi, edi ; total TCP segment size + cmp esi, [eax + TCP_SOCKET.t_maxseg] + jbe .no_overflow - mov esi, [eax + TCP_SOCKET.t_maxseg] + mov esi, [eax + TCP_SOCKET.t_maxseg] ;;; sendalot = 1 @@ -389,60 +392,60 @@ TCP_output: ; Start by pushing all TCP header values in reverse order on stack ; (essentially, creating the tcp header on the stack!) - pushw 0 ; .UrgentPointer dw ? - pushw 0 ; .Checksum dw ? - pushw 0x00a0 ; .Window dw ? ;;;;;;; FIXME - shl edi, 2 ; .DataOffset db ? only 4 left-most bits - shl dx, 8 - or dx, di ; .Flags db ? - pushw dx - shr edi, 2 ; .DataOffset db ? ;;;; + pushw 0 ; .UrgentPointer dw ? + pushw 0 ; .Checksum dw ? + pushw 0x00a0 ; .Window dw ? ;;;;;;; FIXME + shl edi, 2 ; .DataOffset db ? only 4 left-most bits + shl dx, 8 + or dx, di ; .Flags db ? + pushw dx + shr edi, 2 ; .DataOffset db ? ;;;; - push [eax + TCP_SOCKET.RCV_NXT] ; .AckNumber dd ? - ntohd [esp] + push [eax + TCP_SOCKET.RCV_NXT] ; .AckNumber dd ? + ntohd [esp] - push [eax + TCP_SOCKET.SND_NXT] ; .SequenceNumber dd ? - ntohd [esp] + push [eax + TCP_SOCKET.SND_NXT] ; .SequenceNumber dd ? + ntohd [esp] - push [eax + TCP_SOCKET.RemotePort] ; .DestinationPort dw ? - ntohw [esp] + push [eax + TCP_SOCKET.RemotePort] ; .DestinationPort dw ? + ntohw [esp] - push [eax + TCP_SOCKET.LocalPort] ; .SourcePort dw ? - ntohw [esp] + push [eax + TCP_SOCKET.LocalPort] ; .SourcePort dw ? + ntohw [esp] - push edi ; header size + push edi ; header size ;--------------------- ; Create the IP packet - mov ecx, esi + mov ecx, esi - mov ebx, [eax + IP_SOCKET.LocalIP] ; source ip - mov eax, [eax + IP_SOCKET.RemoteIP] ; dest ip - mov di, IP_PROTO_TCP shl 8 + 128 - call IPv4_output - jz .fail + mov ebx, [eax + IP_SOCKET.LocalIP] ; source ip + mov eax, [eax + IP_SOCKET.RemoteIP] ; dest ip + mov di, IP_PROTO_TCP shl 8 + 128 + call IPv4_output + jz .fail ;----------------------------------------- ; Move TCP header from stack to TCP packet - push ecx - mov ecx, [esp+4] - lea esi, [esp+4+4] - shr ecx, 2 - rep movsd - pop ecx ; full TCP packet size + push ecx + mov ecx, [esp+4] + lea esi, [esp+4+4] + shr ecx, 2 + rep movsd + pop ecx ; full TCP packet size - pop esi ; headersize - add esp, esi + pop esi ; headersize + add esp, esi - mov [esp + 4], eax ; packet ptr - mov [esp + 4+4], edx ; packet size + mov [esp + 4], eax ; packet ptr + mov [esp + 4+4], edx ; packet size - mov edx, edi ; begin of data - sub edx, esi ; begin of packet (edi = begin of data) - push ecx - sub ecx, esi ; data size + mov edx, edi ; begin of data + sub edx, esi ; begin of packet (edi = begin of data) + push ecx + sub ecx, esi ; data size ;-------------- ; Copy the data @@ -451,52 +454,52 @@ TCP_output: ; ecx = buffer size ; edi = ptr to buffer - mov eax, [esp+4] ; get socket ptr + mov eax, [esp+4] ; get socket ptr - add [eax + TCP_SOCKET.SND_NXT], ecx ; update sequence number + add [eax + TCP_SOCKET.SND_NXT], ecx ; update sequence number - add eax, STREAM_SOCKET.snd - push edx - call SOCKET_ring_read - pop esi ; begin of data - pop ecx ; full packet size - pop eax ; socket ptr + add eax, STREAM_SOCKET.snd + push edx + call SOCKET_ring_read + pop esi ; begin of data + pop ecx ; full packet size + pop eax ; socket ptr ;---------------------------------- ; update sequence number and timers (400) - test [esi + TCP_header.Flags], TH_SYN + TH_FIN - jz @f - inc [eax + TCP_SOCKET.SND_NXT] ; syn and fin take a sequence number - test [esi + TCP_header.Flags], TH_FIN - jz @f - or [eax + TCP_SOCKET.t_flags], TF_SENTFIN ; if we sent a fin, set the sentfin flag + test [esi + TCP_header.Flags], TH_SYN + TH_FIN + jz @f + inc [eax + TCP_SOCKET.SND_NXT] ; syn and fin take a sequence number + test [esi + TCP_header.Flags], TH_FIN + jz @f + or [eax + TCP_SOCKET.t_flags], TF_SENTFIN ; if we sent a fin, set the sentfin flag @@: - mov edx, [eax + TCP_SOCKET.SND_NXT] - cmp edx, [eax + TCP_SOCKET.SND_MAX] - jbe @f - mov [eax + TCP_SOCKET.SND_MAX], edx + mov edx, [eax + TCP_SOCKET.SND_NXT] + cmp edx, [eax + TCP_SOCKET.SND_MAX] + jbe @f + mov [eax + TCP_SOCKET.SND_MAX], edx - ;;;; TODO: time transmission (420) + ;;;; TODO: time transmission (420) @@: ; set retransmission timer if not already set, and not doing an ACK or keepalive probe - cmp [eax + TCP_SOCKET.timer_retransmission], 1000 ;;;; FIXME - jb .retransmit_set + cmp [eax + TCP_SOCKET.timer_retransmission], 1000 ;;;; FIXME + jb .retransmit_set - cmp edx, [eax + TCP_SOCKET.SND_UNA] ; edx = [eax + TCP_SOCKET.SND_NXT] - je .retransmit_set + cmp edx, [eax + TCP_SOCKET.SND_UNA] ; edx = [eax + TCP_SOCKET.SND_NXT] + je .retransmit_set - mov edx, [eax + TCP_SOCKET.t_rxtcur] - mov [eax + TCP_SOCKET.timer_retransmission], dx + mov edx, [eax + TCP_SOCKET.t_rxtcur] + mov [eax + TCP_SOCKET.timer_retransmission], dx - cmp [eax + TCP_SOCKET.timer_persist], 0 - jne @f - mov [eax + TCP_SOCKET.timer_persist], 0 - mov [eax + TCP_SOCKET.t_rxtshift], 0 + cmp [eax + TCP_SOCKET.timer_persist], 0 + jne @f + mov [eax + TCP_SOCKET.timer_persist], 0 + mov [eax + TCP_SOCKET.t_rxtshift], 0 @@: .retransmit_set: @@ -504,30 +507,38 @@ TCP_output: ;-------------------- ; Create the checksum - DEBUGF 1,"checksum: ptr=%x size=%u\n", esi, ecx + DEBUGF 1,"checksum: ptr=%x size=%u\n", esi, ecx - TCP_checksum (eax + IP_SOCKET.LocalIP), (eax + IP_SOCKET.RemoteIP) - mov [esi + TCP_header.Checksum], dx + TCP_checksum (eax + IP_SOCKET.LocalIP), (eax + IP_SOCKET.RemoteIP) + mov [esi + TCP_header.Checksum], dx ; unlock socket - mov [eax + SOCKET.lock], 0 + pusha + lea ecx, [eax + SOCKET.mutex] + call mutex_unlock + popa ;---------------- ; Send the packet - DEBUGF 1,"Sending TCP Packet to device %x\n", ebx - call [ebx + NET_DEVICE.transmit] - ret + DEBUGF 1,"Sending TCP Packet to device %x\n", ebx + call [ebx + NET_DEVICE.transmit] + ret .fail: - pop ecx - add esp, ecx - pop eax - add esp, 8 - mov [eax + SOCKET.lock], 0 - DEBUGF 1,"TCP_output: failed\n" - ret + pop ecx + add esp, ecx + pop eax + add esp, 8 + + pusha + lea ecx, [eax + SOCKET.mutex] + call mutex_unlock + popa + + DEBUGF 1,"TCP_output: failed\n" + ret diff --git a/kernel/branches/net/network/udp.inc b/kernel/branches/net/network/udp.inc index 12a799a4f1..19021e8661 100644 --- a/kernel/branches/net/network/udp.inc +++ b/kernel/branches/net/network/udp.inc @@ -178,10 +178,10 @@ UDP_input: cmp [eax + UDP_SOCKET.RemotePort], cx jne .dump - push ebx - lea ebx, [eax + SOCKET.lock] - call wait_mutex - pop ebx + pusha + lea ecx, [eax + SOCKET.mutex] + call mutex_lock + popa .updatesock: inc [UDP_PACKETS_RX] @@ -193,10 +193,10 @@ UDP_input: jmp SOCKET_input .updateport: - push ebx - lea ebx, [eax + SOCKET.lock] - call wait_mutex - pop ebx + pusha + lea ecx, [eax + SOCKET.mutex] + call mutex_lock + popa DEBUGF 1,"Changing remote port to: %u\n", cx mov [eax + UDP_SOCKET.RemotePort], cx