add proper error handling for socket functions

git-svn-id: svn://kolibrios.org@6436 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
nisargshah95 2016-05-28 10:13:18 +00:00
parent 7c0ebfd724
commit ed33bcec93

View File

@ -146,10 +146,14 @@ no_resolve:
cmp al, 0x20 cmp al, 0x20
jbe .port_done jbe .port_done
sub al, '0' sub al, '0'
jb error_hostname jnb @f
cmp al, 9 mov eax, str_err_host
ja error_hostname jmp error
lea ebx, [ebx*4 + ebx] @@: cmp al, 9
jna @f
mov eax, str_err_host
jmp error
@@: lea ebx, [ebx*4 + ebx]
shl ebx, 1 shl ebx, 1
add ebx, eax add ebx, eax
jmp .portloop jmp .portloop
@ -168,7 +172,10 @@ no_resolve:
pop esi pop esi
; test for error ; test for error
test eax, eax test eax, eax
jnz error_resolve jz @f
mov eax, str_err_resolve
jmp error
@@:
; write results ; write results
invoke con_write_asciiz, str8 ; ' (',0 invoke con_write_asciiz, str8 ; ' (',0
mov eax, [esi+addrinfo.ai_addr] ; convert IP address to decimal notation mov eax, [esi+addrinfo.ai_addr] ; convert IP address to decimal notation
@ -181,14 +188,18 @@ no_resolve:
; open the socket ; open the socket
mcall socket, AF_INET4, SOCK_STREAM, 0 mcall socket, AF_INET4, SOCK_STREAM, 0
cmp eax, -1 cmp eax, -1
je error_socket jne @f
mov [controlsocket], eax mov eax, str_err_socket
jmp error
@@: mov [controlsocket], eax
; connect to the server ; connect to the server
invoke con_write_asciiz, str_connect invoke con_write_asciiz, str_connect
mcall connect, [controlsocket], sockaddr1, 18 mcall connect, [controlsocket], sockaddr1, 18
cmp eax, -1 cmp eax, -1
je error_connect jne @f
mov [status], STATUS_CONNECTING mov eax, str_err_connect
jmp error
@@: mov [status], STATUS_CONNECTING
; Tell the user we're waiting for the server now. ; Tell the user we're waiting for the server now.
invoke con_write_asciiz, str_waiting invoke con_write_asciiz, str_waiting
@ -214,13 +225,17 @@ wait_for_servercommand:
mcall 23, 50 ; Wait for event with timeout mcall 23, 50 ; Wait for event with timeout
mcall 26, 9 mcall 26, 9
cmp eax, [timeout] cmp eax, [timeout]
jge error_timeout jl @f
mcall recv, [controlsocket], buf_buffer1, BUFFERSIZE, MSG_DONTWAIT mov eax, str_err_timeout
jmp error
@@: mcall recv, [controlsocket], buf_buffer1, BUFFERSIZE, MSG_DONTWAIT
test eax, eax test eax, eax
jnz .got_data jnz .got_data
cmp ebx, EWOULDBLOCK cmp ebx, EWOULDBLOCK
jne error_socket je @f
jmp .receive_loop mov eax, str_err_recv
jmp error
@@: jmp .receive_loop
.got_data: .got_data:
mov [offset], 0 mov [offset], 0
@ -450,8 +465,10 @@ open_dataconnection:
.active: .active:
mcall socket, AF_INET4, SOCK_STREAM, 0 mcall socket, AF_INET4, SOCK_STREAM, 0
cmp eax, -1 cmp eax, -1
je error_socket jne @f
mov [datasocket], eax mov eax, str_err_socket
jmp error
@@: mov [datasocket], eax
mov ax, [acti_port_start] mov ax, [acti_port_start]
xchg al, ah xchg al, ah
@ -459,11 +476,17 @@ open_dataconnection:
mcall bind, [datasocket], sockaddr2, 18 mcall bind, [datasocket], sockaddr2, 18
cmp eax, -1 cmp eax, -1
je error_socket jne @f
mov eax, str_err_bind
jmp error
@@:
mcall listen, [datasocket], 1 mcall listen, [datasocket], 1
cmp eax, -1 cmp eax, -1
je error_socket jne @f
mov eax, str_err_listen
jmp error
@@:
mov dword[buf_buffer1], 'PORT' mov dword[buf_buffer1], 'PORT'
mov byte[buf_buffer1+4], ' ' mov byte[buf_buffer1+4], ' '
@ -491,8 +514,10 @@ open_dataconnection:
mcall accept, [datasocket], sockaddr2, 18 ; time to accept the awaiting connection.. mcall accept, [datasocket], sockaddr2, 18 ; time to accept the awaiting connection..
cmp eax, -1 cmp eax, -1
je error_socket jne @f
push eax mov eax, str_err_accept
jmp error
@@: push eax
mcall close, [datasocket] mcall close, [datasocket]
pop [datasocket] pop [datasocket]
@ -526,29 +551,11 @@ dword_ascii:
pop ecx ebx edx pop ecx ebx edx
ret ret
error_hostname: error:
push eax
invoke con_set_flags, 0x0c ; print errors in red invoke con_set_flags, 0x0c ; print errors in red
invoke con_write_asciiz, str_err_host pop eax
jmp wait_for_keypress invoke con_write_asciiz, eax
error_connect:
invoke con_set_flags, 0x0c ; print errors in red
invoke con_write_asciiz, str_err_connect
jmp wait_for_keypress
error_timeout:
invoke con_set_flags, 0x0c ; print errors in red
invoke con_write_asciiz, str_err_timeout
jmp wait_for_keypress
error_socket:
invoke con_set_flags, 0x0c ; print errors in red
invoke con_write_asciiz, str_err_socket
jmp wait_for_keypress
error_resolve:
invoke con_set_flags, 0x0c ; print errors in red
invoke con_write_asciiz, str_err_resolve
jmp wait_for_keypress jmp wait_for_keypress
error_heap: error_heap:
@ -584,11 +591,16 @@ str_prompt db '> ',0
str_resolve db 'Resolving ',0 str_resolve db 'Resolving ',0
str_newline db 10,0 str_newline db 10,0
str_err_resolve db 10,'Name resolution failed.',10,0 str_err_resolve db 10,'Name resolution failed.',10,0
str_err_socket db 10,'Socket error.',10,0 str_err_socket db 10,'[75,0 socket]: Error creating a socket',10,0
str_err_bind db 10,'[75,2 bind]: Error binding to socket',10,0
str_err_listen db 10,'[75,3 listen]: Cannot accept incoming connections',10,0
str_err_accept db 10,'[75,5 accept]: Error accepting a connection',10,0
str_err_recv db 10,'[75,7 recv]: Error receiving data from server',10,0
str_err_heap db 10,'Cannot allocate memory from heap.',10,0 str_err_heap db 10,'Cannot allocate memory from heap.',10,0
str_err_timeout db 10,'Timeout - no response from server.',10,0 str_err_timeout db 10,'Timeout - no response from server.',10,0
str_err_connect db 10,'Cannot connect to the server.',10,0 str_err_connect db 10,'[75,4 connect]: Cannot connect to the server.',10,0
str_err_host db 10,'Invalid hostname.',10,0 str_err_host db 10,'Invalid hostname.',10,0
str_err_params db 10,'Invalid parameters',10,0
str8 db ' (',0 str8 db ' (',0
str9 db ')',10,0 str9 db ')',10,0
str_push db 'Push any key to continue.',0 str_push db 'Push any key to continue.',0