Improved error handling in sockets code.

git-svn-id: svn://kolibrios.org@5969 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
hidnplayr 2015-12-15 21:54:06 +00:00
parent caacfd1550
commit 7cff3aa25b

View File

@ -421,6 +421,7 @@ SOCKET_bind:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_bind: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_bind: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
call SOCKET_num_to_ptr call SOCKET_num_to_ptr
test eax, eax
jz .invalid jz .invalid
cmp esi, 2 cmp esi, 2
@ -510,6 +511,7 @@ SOCKET_connect:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_connect: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_connect: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
call SOCKET_num_to_ptr call SOCKET_num_to_ptr
test eax, eax
jz .invalid jz .invalid
cmp esi, 8 cmp esi, 8
@ -566,6 +568,7 @@ SOCKET_listen:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_listen: socknum=%u backlog=%u\n", ecx, edx DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_listen: socknum=%u backlog=%u\n", ecx, edx
call SOCKET_num_to_ptr call SOCKET_num_to_ptr
test eax, eax
jz .invalid jz .invalid
cmp [eax + SOCKET.Domain], AF_INET4 cmp [eax + SOCKET.Domain], AF_INET4
@ -632,6 +635,7 @@ SOCKET_accept:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_accept: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_accept: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
call SOCKET_num_to_ptr call SOCKET_num_to_ptr
test eax, eax
jz .invalid jz .invalid
test [eax + SOCKET.options], SO_ACCEPTCON test [eax + SOCKET.options], SO_ACCEPTCON
@ -649,16 +653,17 @@ SOCKET_accept:
; Ok, we got a socket ptr ; Ok, we got a socket ptr
mov eax, [esi] mov eax, [esi]
; Change thread ID to that of the current thread ; Verify that it is (still) a valid socket
call SOCKET_check
jz .invalid
; Change sockets thread owner ID to that of the current thread
mov ebx, [TASK_BASE] mov ebx, [TASK_BASE]
mov ebx, [ebx + TASKDATA.pid] mov ebx, [ebx + TASKDATA.pid]
mov [eax + SOCKET.TID], ebx mov [eax + SOCKET.TID], ebx
; Convert it to a socket number ; Return socket number to caller
call SOCKET_ptr_to_num mov eax, [eax + SOCKET.Number]
jz .invalid ; FIXME ?
; and return it to caller
mov [esp+32], eax mov [esp+32], eax
ret ret
@ -698,6 +703,7 @@ SOCKET_close:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_close: socknum=%u\n", ecx DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_close: socknum=%u\n", ecx
call SOCKET_num_to_ptr call SOCKET_num_to_ptr
test eax, eax
jz .invalid jz .invalid
mov dword[esp+32], 0 ; The socket exists, so we will succeed in closing it. mov dword[esp+32], 0 ; The socket exists, so we will succeed in closing it.
@ -754,6 +760,7 @@ SOCKET_receive:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: socknum=%u bufaddr=%x buflength=%u flags=%x\n", ecx, edx, esi, edi DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_receive: socknum=%u bufaddr=%x buflength=%u flags=%x\n", ecx, edx, esi, edi
call SOCKET_num_to_ptr call SOCKET_num_to_ptr
test eax, eax
jz .invalid jz .invalid
.loop: .loop:
@ -927,6 +934,7 @@ SOCKET_send:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: socknum=%u data ptr=%x length=%u flags=%x\n", ecx, edx, esi, edi DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_send: socknum=%u data ptr=%x length=%u flags=%x\n", ecx, edx, esi, edi
call SOCKET_num_to_ptr call SOCKET_num_to_ptr
test eax, eax
jz .invalid jz .invalid
mov ecx, esi mov ecx, esi
@ -1096,6 +1104,7 @@ SOCKET_get_opt:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_get_opt\n" DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_get_opt\n"
call SOCKET_num_to_ptr call SOCKET_num_to_ptr
test eax, eax
jz .invalid jz .invalid
cmp dword [edx], IP_PROTO_TCP cmp dword [edx], IP_PROTO_TCP
@ -1157,6 +1166,7 @@ SOCKET_set_opt:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt\n" DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt\n"
call SOCKET_num_to_ptr call SOCKET_num_to_ptr
test eax, eax
jz .invalid jz .invalid
cmp [edx + socket_options.level], IP_PROTO_IP cmp [edx + socket_options.level], IP_PROTO_IP
@ -1264,7 +1274,7 @@ SOCKET_pair:
lea eax, [eax + STREAM_SOCKET.rcv] lea eax, [eax + STREAM_SOCKET.rcv]
call SOCKET_ring_create call SOCKET_ring_create
test eax, eax test eax, eax
jz .nomem1 jz .nomem2
lea eax, [ebx + STREAM_SOCKET.rcv] lea eax, [ebx + STREAM_SOCKET.rcv]
call SOCKET_ring_create call SOCKET_ring_create
@ -1274,11 +1284,15 @@ SOCKET_pair:
ret ret
.nomem2: .nomem2:
mov eax, ebx mov eax, [esp+20]
call SOCKET_free call SOCKET_free
.nomem1: .nomem1:
mov eax, [esp+32]
call SOCKET_free
mov dword[esp+32], -1 mov dword[esp+32], -1
mov dword[esp+28], ENOMEM mov dword[esp+20], ENOMEM
ret ret
@ -1305,6 +1319,7 @@ SOCKET_debug:
jz .returnall jz .returnall
call SOCKET_num_to_ptr call SOCKET_num_to_ptr
test eax, eax
jz .invalid jz .invalid
mov esi, eax mov esi, eax
@ -1331,7 +1346,7 @@ SOCKET_debug:
.invalid: .invalid:
mov dword[esp+32], -1 mov dword[esp+32], -1
mov dword[esp+28], EINVAL mov dword[esp+20], EINVAL
ret ret
@ -1523,7 +1538,7 @@ SOCKET_ring_create:
test eax, eax test eax, eax
jz .fail jz .fail
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_created: %x\n", eax DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_ring_create: %x\n", eax
pusha pusha
lea ecx, [esi + RING_BUFFER.mutex] lea ecx, [esi + RING_BUFFER.mutex]
@ -1538,7 +1553,11 @@ SOCKET_ring_create:
mov [esi + RING_BUFFER.end_ptr], eax mov [esi + RING_BUFFER.end_ptr], eax
mov eax, esi mov eax, esi
pop esi
ret
.fail: .fail:
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_ring_create: Out of memory!\n"
pop esi pop esi
ret ret
@ -1858,7 +1877,7 @@ SOCKET_notify:
; ;
; Allocate memory for socket data and put new socket into the list ; Allocate memory for socket data and put new socket into the list
; Newly created socket is initialized with calling PID and number and ; Newly created socket is initialized with calling PID and number and
; put into beginning of list (which is a fastest way). ; put into beginning of list (which is the fastest way).
; ;
; IN: / ; IN: /
; OUT: eax = 0 on error, socket ptr otherwise ; OUT: eax = 0 on error, socket ptr otherwise
@ -1871,9 +1890,9 @@ SOCKET_alloc:
push ebx push ebx
stdcall kernel_alloc, SOCKETBUFFSIZE stdcall kernel_alloc, SOCKETBUFFSIZE
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: ptr=%x\n", eax
or eax, eax or eax, eax
jz .exit jz .nomem
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: ptr=%x\n", eax
; zero-initialize allocated memory ; zero-initialize allocated memory
push eax push eax
@ -1954,12 +1973,15 @@ SOCKET_alloc:
mov ecx, socket_mutex mov ecx, socket_mutex
call mutex_unlock call mutex_unlock
popa popa
.exit:
pop ebx pop ebx
ret ret
.nomem:
DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_alloc: Out of memory!\n"
pop ebx
ret
.not_yet: .not_yet:
mov dword[esp+20], ENOTCONN mov dword[esp+20], ENOTCONN
mov dword[esp+32], -1 mov dword[esp+32], -1
@ -1992,17 +2014,20 @@ SOCKET_free:
call mutex_lock call mutex_lock
popa popa
cmp [eax + SOCKET.Domain], AF_INET4 cmp [eax + SOCKET.Type], SOCK_STREAM
jnz .no_tcp jne .no_stream
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
jnz .no_tcp
mov ebx, eax mov ebx, eax
stdcall kernel_free, [ebx + STREAM_SOCKET.rcv.start_ptr] cmp [eax + STREAM_SOCKET.rcv.start_ptr], 0
stdcall kernel_free, [ebx + STREAM_SOCKET.snd.start_ptr] je @f
stdcall free_kernel_space, [eax + STREAM_SOCKET.rcv.start_ptr]
@@:
cmp [ebx + STREAM_SOCKET.snd.start_ptr], 0
je @f
stdcall free_kernel_space, [ebx + STREAM_SOCKET.snd.start_ptr]
@@:
mov eax, ebx mov eax, ebx
.no_tcp: .no_stream:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: freeing socket %x\n", eax DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_free: freeing socket %x\n", eax
push eax ; this will be passed to kernel_free push eax ; this will be passed to kernel_free
@ -2029,6 +2054,12 @@ SOCKET_free:
.error: .error:
ret ret
.error1:
pop ebx
DEBUGF DEBUG_NETWORK_ERROR, "Error in socket free!\n"
DEBUGF DEBUG_NETWORK_ERROR, "socket ptr=0x%x caller=0x%x\n", eax, [esp]
ret
;------------------------------------ ;------------------------------------
; ;
; SOCKET_fork ; SOCKET_fork
@ -2095,7 +2126,6 @@ SOCKET_fork:
; ;
; IN: ecx = socket number ; IN: ecx = socket number
; OUT: eax = 0 on error, socket ptr otherwise ; OUT: eax = 0 on error, socket ptr otherwise
; ZF = set on error
; ;
;--------------------------------------------------- ;---------------------------------------------------
align 4 align 4
@ -2109,16 +2139,13 @@ SOCKET_num_to_ptr:
popa popa
mov eax, net_sockets mov eax, net_sockets
.next_socket: .next_socket:
mov eax, [eax + SOCKET.NextPtr] mov eax, [eax + SOCKET.NextPtr]
or eax, eax test eax, eax
jz .error jz .error
cmp [eax + SOCKET.Number], ecx cmp [eax + SOCKET.Number], ecx
jne .next_socket jne .next_socket
test eax, eax
pusha pusha
mov ecx, socket_mutex mov ecx, socket_mutex
call mutex_unlock call mutex_unlock
@ -2183,6 +2210,8 @@ SOCKET_check:
DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check: %x\n", eax DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_check: %x\n", eax
test eax, eax
jz .error
push ebx push ebx
mov ebx, net_sockets mov ebx, net_sockets
@ -2197,7 +2226,11 @@ SOCKET_check:
mov eax, ebx mov eax, ebx
test eax, eax test eax, eax
pop ebx pop ebx
ret
.error:
DEBUGF DEBUG_NETWORK_ERROR, "Socket_check called with argument 0\n"
DEBUGF DEBUG_NETWORK_ERROR, "stack: 0x%x, 0x%x, 0x%x\n", [esp], [esp+4], [esp+8]
ret ret