Implemented global socket mutex.

git-svn-id: svn://kolibrios.org@3647 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
hidnplayr 2013-06-12 10:21:41 +00:00
parent 97a976fc9a
commit 3e584a778f
5 changed files with 151 additions and 4 deletions

View File

@ -63,6 +63,11 @@ PPPoE_discovery_input:
; First, find open PPPoE socket
pusha
mov ecx, socket_mutex
call mutex_lock
popa
mov eax, net_sockets
.next_socket:
@ -76,6 +81,11 @@ PPPoE_discovery_input:
cmp [eax + SOCKET.Protocol], PPP_PROTO_ETHERNET
jne .next_socket
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
; Now, send it to the this socket
mov ecx, [esp + 4]
@ -84,6 +94,11 @@ PPPoE_discovery_input:
jmp SOCKET_input
.dump:
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
DEBUGF DEBUG_NETWORK_VERBOSE, 'PPPoE_discovery_input: dumping\n'
call kernel_free
add esp, 4

View File

@ -238,6 +238,11 @@ ICMP_input:
.check_sockets:
; Look for an open ICMP socket
pusha
mov ecx, socket_mutex
call mutex_lock
popa
mov esi, [edi] ; ipv4 source address
mov eax, net_sockets
.try_more:
@ -245,7 +250,7 @@ ICMP_input:
.next_socket:
mov eax, [eax + SOCKET.NextPtr]
or eax, eax
jz .dump
jz .dump_
cmp [eax + SOCKET.Domain], AF_INET4
jne .next_socket
@ -262,9 +267,14 @@ ICMP_input:
; Update stats (and validate device ptr)
call NET_ptr_to_num4
cmp edi, -1
je .dump
je .dump_
inc [ICMP_PACKETS_RX + edi]
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
DEBUGF DEBUG_NETWORK_VERBOSE, "socket=%x\n", eax
pusha
@ -275,6 +285,16 @@ ICMP_input:
mov esi, edx
jmp SOCKET_input
.dump_:
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
DEBUGF DEBUG_NETWORK_VERBOSE, "ICMP_input: no socket found\n"
jmp .dump
.checksum_mismatch:
DEBUGF DEBUG_NETWORK_VERBOSE, "checksum mismatch\n"

View File

@ -199,6 +199,7 @@ uglobal
last_socket_num dd ?
last_UDP_port dw ? ; These values give the number of the last used ephemeral port
last_TCP_port dw ? ;
socket_mutex MUTEX
endg
@ -232,6 +233,9 @@ macro SOCKET_init {
xchg al, ah
mov [last_TCP_port], ax
mov ecx, socket_mutex
call mutex_init
}
;-----------------------------------------------------------------
@ -1343,6 +1347,11 @@ SOCKET_check_port:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check_port: "
pusha
mov ecx, socket_mutex
call mutex_lock
popa
mov ecx, [eax + SOCKET.Protocol]
mov edx, [eax + IP_SOCKET.LocalIP]
mov esi, net_sockets
@ -1361,10 +1370,20 @@ SOCKET_check_port:
cmp [esi + UDP_SOCKET.LocalPort], bx
jne .next_socket
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
DEBUGF DEBUG_NETWORK_VERBOSE, "local port %x already in use\n", bx ; FIXME: find a way to print big endian values with debugf
ret
.port_ok:
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
DEBUGF DEBUG_NETWORK_VERBOSE, "local port %x is free\n", bx ; FIXME: find a way to print big endian values with debugf
mov [eax + UDP_SOCKET.LocalPort], bx
or bx, bx ; clear the zero-flag
@ -1815,6 +1834,11 @@ SOCKET_alloc:
mov [eax + SOCKET.snd_proc], s_error
mov [eax + SOCKET.rcv_proc], s_error
pusha
mov ecx, socket_mutex
call mutex_lock
popa
; find first free socket number and use it
mov edi, [last_socket_num]
.next_socket_number:
@ -1873,6 +1897,12 @@ SOCKET_alloc:
mov [net_sockets + SOCKET.NextPtr], eax
or eax, eax ; used to clear zero flag
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
.exit:
pop ebx
@ -1899,6 +1929,11 @@ SOCKET_free:
push ebx
pusha
mov ecx, socket_mutex
call mutex_lock
popa
pusha
lea ecx, [eax + SOCKET.mutex]
call mutex_lock
@ -1935,6 +1970,11 @@ SOCKET_free:
call kernel_free
pop ebx
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: success!\n"
.error:
@ -2006,6 +2046,11 @@ SOCKET_num_to_ptr:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_num_to_ptr: num=%u ", ecx
pusha
mov ecx, socket_mutex
call mutex_lock
popa
mov eax, net_sockets
.next_socket:
@ -2017,10 +2062,20 @@ SOCKET_num_to_ptr:
test eax, eax
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
DEBUGF DEBUG_NETWORK_VERBOSE, "ptr=%x\n", eax
ret
.error:
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: not found\n", eax
ret
@ -2070,6 +2125,11 @@ SOCKET_check:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check: %x\n", eax
pusha
mov ecx, socket_mutex
call mutex_lock
popa
push ebx
mov ebx, net_sockets
@ -2085,6 +2145,11 @@ SOCKET_check:
test eax, eax
pop ebx
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
ret
@ -2132,6 +2197,11 @@ SOCKET_process_end:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: %x\n", edx
pusha
mov ecx, socket_mutex
call mutex_lock
popa
push ebx
mov ebx, net_sockets
@ -2157,6 +2227,11 @@ SOCKET_process_end:
.done:
pop ebx
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
ret

View File

@ -150,13 +150,18 @@ TCP_process_input:
; (IP Packet TCP Source Port = remote Port) OR (remote Port = 0)
.findpcb:
pusha
mov ecx, socket_mutex
call mutex_lock
popa
mov ebx, net_sockets
mov si, [edx + TCP_header.DestinationPort]
.socket_loop:
mov ebx, [ebx + SOCKET.NextPtr]
or ebx, ebx
jz .respond_seg_reset
jz .no_socket ;respond_seg_reset
cmp [ebx + SOCKET.Domain], AF_INET4
jne .socket_loop
@ -180,6 +185,11 @@ TCP_process_input:
test ax, ax
jnz .socket_loop
.found_socket: ; ebx now contains the socketpointer
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: socket ptr=%x state=%u flags=%x\n", ebx, [ebx + TCP_SOCKET.t_state], [edx + TCP_header.Flags]:2
;----------------------------
@ -1619,6 +1629,13 @@ align 4
pop ebx
jmp .destroy_new_socket
.no_socket:
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
.respond_seg_reset:
test [edx + TCP_header.Flags], TH_RST
jnz .drop_no_socket

View File

@ -139,6 +139,11 @@ UDP_input:
; IP Packet UDP Destination Port = local Port
; IP Packet SA = Remote IP
pusha
mov ecx, socket_mutex
call mutex_lock
popa
mov cx, [esi + UDP_header.SourcePort]
mov dx, [esi + UDP_header.DestinationPort]
mov edi, [edi + 4] ; ipv4 source address
@ -147,7 +152,7 @@ UDP_input:
.next_socket:
mov eax, [eax + SOCKET.NextPtr]
or eax, eax
jz .dump
jz .dump_
cmp [eax + SOCKET.Domain], AF_INET4
jne .next_socket
@ -160,6 +165,11 @@ UDP_input:
DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: socket=%x\n", eax
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
;;; TODO: when packet is processed, check more sockets!
; cmp [eax + IP_SOCKET.RemoteIP], 0xffffffff
@ -203,6 +213,16 @@ UDP_input:
jmp .updatesock
.dump_:
pusha
mov ecx, socket_mutex
call mutex_unlock
popa
DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: no socket found\n"
jmp .dump
.checksum_mismatch:
DEBUGF DEBUG_NETWORK_VERBOSE, "UDP_input: checksum mismatch\n"