2009-10-06 13:52:00 +02:00
|
|
|
format MS COFF
|
|
|
|
|
|
|
|
public @EXPORT as 'EXPORTS'
|
|
|
|
|
2011-11-13 16:19:27 +01:00
|
|
|
include '../../struct.inc'
|
|
|
|
include '../../proc32.inc'
|
|
|
|
include '../../macros.inc'
|
2009-10-06 13:52:00 +02:00
|
|
|
purge section,mov,add,sub
|
|
|
|
|
2011-11-13 16:19:27 +01:00
|
|
|
include '../../network.inc'
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
section '.flat' code readable align 16
|
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
lib_init: ;//////////////////////////////////////////////////////////////////;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Library entry point (called after library load) ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> eax = pointer to memory allocation routine ;;
|
|
|
|
;> ebx = pointer to memory freeing routine ;;
|
|
|
|
;> ecx = pointer to memory reallocation routine ;;
|
|
|
|
;> edx = pointer to library loading routine ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;< eax = 1 (fail) / 0 (ok) (library initialization result) ;;
|
|
|
|
;;===========================================================================;;
|
2012-07-11 14:08:45 +02:00
|
|
|
mov [mem.alloc], eax
|
|
|
|
mov [mem.free], ebx
|
|
|
|
mov [mem.realloc], ecx
|
|
|
|
mov [dll.load], edx
|
|
|
|
mov [DNSrequestID], 1
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; in_addr_t __stdcall inet_addr(__in const char* hostname); ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
inet_addr: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Convert the string from standard IPv4 dotted notation to integer IP addr. ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> first parameter = host name ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;< eax = IP address on success / -1 on error ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
; 0. Save used registers for __stdcall.
|
2012-07-11 14:08:45 +02:00
|
|
|
push ebx esi edi
|
|
|
|
mov esi, [esp+16] ; esi = hostname
|
2009-10-06 13:52:00 +02:00
|
|
|
; 1. Check that only allowed symbols are present.
|
|
|
|
; (hex digits, possibly letters 'x'/'X' and up to 3 dots)
|
2012-07-11 14:08:45 +02:00
|
|
|
push esi
|
|
|
|
xor ecx, ecx
|
2009-10-06 13:52:00 +02:00
|
|
|
.calcdots_loop:
|
|
|
|
; loop for all characters in string
|
2012-07-11 14:08:45 +02:00
|
|
|
lodsb
|
2009-10-06 13:52:00 +02:00
|
|
|
; check for end of string
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp al, 0
|
|
|
|
jz .calcdots_loop_done
|
2009-10-06 13:52:00 +02:00
|
|
|
; check for dot
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp al, '.'
|
|
|
|
jz .dot
|
2009-10-06 13:52:00 +02:00
|
|
|
; check for digit
|
2012-07-11 14:08:45 +02:00
|
|
|
sub al, '0'
|
|
|
|
cmp al, 9
|
|
|
|
jbe .calcdots_loop
|
2009-10-06 13:52:00 +02:00
|
|
|
; check for hex letter
|
2012-07-11 14:08:45 +02:00
|
|
|
sub al, 'A' - '0' ; 'A'-'F' -> 0-5, 'a'-'f' -> 20h-25h
|
|
|
|
and al, not 20h
|
|
|
|
cmp al, 'F' - 'A'
|
|
|
|
jbe .calcdots_loop
|
2009-10-06 13:52:00 +02:00
|
|
|
; check for 'x'/'X'
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp al, 'X' - 'A'
|
|
|
|
jz .calcdots_loop
|
|
|
|
jmp .fail.pop
|
2009-10-06 13:52:00 +02:00
|
|
|
.dot:
|
2012-07-11 14:08:45 +02:00
|
|
|
inc ecx
|
|
|
|
jmp .calcdots_loop
|
2009-10-06 13:52:00 +02:00
|
|
|
.calcdots_loop_done:
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp ecx, 4
|
|
|
|
jae .fail.pop
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2. The name can be valid dotted name; try to convert, checking limit
|
2012-07-11 14:08:45 +02:00
|
|
|
pop esi
|
|
|
|
xor edi, edi ; edi = address
|
|
|
|
push 0xFFFFFFFF
|
|
|
|
pop edx ; edx = mask for rest of address
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2a. Convert name except for last group.
|
2012-07-11 14:08:45 +02:00
|
|
|
jecxz .ip_convert_2b
|
2009-10-06 13:52:00 +02:00
|
|
|
.ip_convert_2a:
|
2012-07-11 14:08:45 +02:00
|
|
|
push ecx
|
|
|
|
mov ecx, 0xFF ; limit for all groups except for last
|
|
|
|
call .get_number
|
|
|
|
pop ecx
|
|
|
|
jc .fail
|
|
|
|
cmp byte [esi-1], '.'
|
|
|
|
jnz .fail
|
|
|
|
shl edi, 8
|
|
|
|
shr edx, 8
|
|
|
|
add edi, eax
|
|
|
|
loop .ip_convert_2a
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2b. Convert last group.
|
|
|
|
.ip_convert_2b:
|
2012-07-11 14:08:45 +02:00
|
|
|
mov ecx, edx
|
|
|
|
call .get_number
|
|
|
|
jc .fail
|
|
|
|
cmp byte [esi-1], 0
|
|
|
|
jnz .fail
|
2009-10-06 13:52:00 +02:00
|
|
|
@@:
|
2012-07-11 14:08:45 +02:00
|
|
|
shl edi, 8
|
|
|
|
shr edx, 8
|
|
|
|
jnz @b
|
|
|
|
add edi, eax
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2c. Convert to network byte order.
|
2012-07-11 14:08:45 +02:00
|
|
|
bswap edi
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3. Set return value, restore used registers and return.
|
2012-07-11 14:08:45 +02:00
|
|
|
xchg eax, edi
|
2009-10-06 13:52:00 +02:00
|
|
|
.ret:
|
2012-07-11 14:08:45 +02:00
|
|
|
pop edi esi ebx
|
|
|
|
ret 4
|
2009-10-06 13:52:00 +02:00
|
|
|
; 4. On error, return -1.
|
|
|
|
.fail.pop:
|
2012-07-11 14:08:45 +02:00
|
|
|
pop esi
|
2009-10-06 13:52:00 +02:00
|
|
|
.fail:
|
2012-07-11 14:08:45 +02:00
|
|
|
push -1
|
|
|
|
pop eax
|
|
|
|
jmp .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; Internal auxiliary function for IP parsing. ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
.get_number: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Converts string to number. ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> esi -> string ;;
|
|
|
|
;> ecx = limit for number ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;< eax = number ;;
|
|
|
|
;< CF set on error (too big number) / cleared on success ;;
|
|
|
|
;< esi -> end of number representation ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
; 0. Save edx, which is used in caller.
|
2012-07-11 14:08:45 +02:00
|
|
|
push edx
|
2009-10-06 13:52:00 +02:00
|
|
|
; 1. Initialize number, zero eax so that lodsb gets full dword.
|
2012-07-11 14:08:45 +02:00
|
|
|
xor eax, eax
|
|
|
|
xor edx, edx
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2. Get used numeral system: 0x = hex, otherwise 0 = octal, otherwise decimal
|
2012-07-11 14:08:45 +02:00
|
|
|
push 10
|
|
|
|
pop ebx
|
|
|
|
lodsb
|
|
|
|
cmp al, '0'
|
|
|
|
jnz .convert
|
|
|
|
push 8
|
|
|
|
pop ebx
|
|
|
|
lodsb
|
|
|
|
cmp al, 'x'
|
|
|
|
jnz .convert
|
|
|
|
add ebx, ebx
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3. Loop while digits are encountered.
|
|
|
|
.convert:
|
|
|
|
; 4. Convert digit from text representation to binary value.
|
2012-07-11 14:08:45 +02:00
|
|
|
or al, 20h ; '0'-'9' -> '0'-'9', 'A'-'F' -> 'a'-'f'
|
|
|
|
sub al, '0'
|
|
|
|
cmp al, 9
|
|
|
|
jbe .digit
|
|
|
|
sub al, 'a' - '0'
|
|
|
|
cmp al, 'f' - 'a'
|
|
|
|
ja .convert_done
|
|
|
|
add al, 10
|
2009-10-06 13:52:00 +02:00
|
|
|
.digit:
|
|
|
|
; 5. Digit must be less than base of numeral system.
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp eax, ebx
|
|
|
|
jae .convert_done
|
2009-10-06 13:52:00 +02:00
|
|
|
; 6. Advance the number.
|
2012-07-11 14:08:45 +02:00
|
|
|
imul edx, ebx
|
|
|
|
add edx, eax
|
|
|
|
cmp edx, ecx
|
|
|
|
ja .gn_error
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3b. Continue loop.
|
2012-07-11 14:08:45 +02:00
|
|
|
lodsb
|
|
|
|
jmp .convert
|
2009-10-06 13:52:00 +02:00
|
|
|
.convert_done:
|
|
|
|
; 7. Invalid character, number converted, return success.
|
2012-07-11 14:08:45 +02:00
|
|
|
xchg eax, edx
|
|
|
|
pop edx
|
|
|
|
clc
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
.gn_error:
|
|
|
|
; 8. Too big number, return error.
|
2012-07-11 14:08:45 +02:00
|
|
|
pop edx
|
|
|
|
stc
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; char* __stdcall inet_ntoa(struct in_addr in); ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
inet_ntoa: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Convert the Internet host address to standard IPv4 dotted notation. ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> first parameter = host address ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;< eax = pointer to resulting string (in static buffer) ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
; 0. Save used registers for __stdcall.
|
2012-07-11 14:08:45 +02:00
|
|
|
push ebx esi edi
|
|
|
|
mov bl, 0xCD ; constant for div 10
|
2009-10-06 13:52:00 +02:00
|
|
|
; 1. Write octet 4 times.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov edi, .buffer
|
|
|
|
mov edx, [esp+16] ; eax = in
|
|
|
|
mov al, dl
|
|
|
|
call .write
|
|
|
|
mov al, dh
|
|
|
|
shr edx, 16
|
|
|
|
call .write
|
|
|
|
mov al, dl
|
|
|
|
call .write
|
|
|
|
mov al, dh
|
|
|
|
call .write
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2. Replace final dot with terminating zero.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov byte [edi-1], 0
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3. Restore used registers, set result value and return.
|
2012-07-11 14:08:45 +02:00
|
|
|
pop edi esi ebx
|
|
|
|
mov eax, .buffer
|
|
|
|
ret 4
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
.write:
|
2012-07-11 14:08:45 +02:00
|
|
|
movzx esi, al
|
|
|
|
mul bl
|
|
|
|
add esi, ('.' shl 8) + '0'
|
|
|
|
shr ah, 3 ; ah = al / 10
|
|
|
|
movzx ecx, ah
|
|
|
|
add ecx, ecx
|
|
|
|
lea ecx, [ecx*5]
|
|
|
|
sub esi, ecx ; lobyte(esi) = al % 10, hibyte(esi) = '.'
|
|
|
|
test ah, ah
|
|
|
|
jz .1digit
|
|
|
|
cmp ah, 10
|
|
|
|
jb .2digit
|
|
|
|
cmp ah, 20
|
|
|
|
sbb cl, cl
|
|
|
|
add cl, '2'
|
|
|
|
mov byte [edi], cl
|
|
|
|
movzx ecx, cl
|
|
|
|
lea ecx, [ecx*5]
|
|
|
|
sub ah, cl
|
|
|
|
sub ah, cl
|
|
|
|
add ah, ('0'*11) and 255
|
|
|
|
mov byte [edi+1], ah
|
|
|
|
mov word [edi+2], si
|
|
|
|
add edi, 4
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
.2digit:
|
2012-07-11 14:08:45 +02:00
|
|
|
add ah, '0'
|
|
|
|
mov byte [edi], ah
|
|
|
|
mov word [edi+1], si
|
|
|
|
add edi, 3
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
.1digit:
|
2012-07-11 14:08:45 +02:00
|
|
|
mov word [edi], si
|
|
|
|
add edi, 2
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
struct __gai_reqdata
|
2012-07-11 14:08:45 +02:00
|
|
|
socketnum dd ?
|
2009-10-06 13:52:00 +02:00
|
|
|
; external code should not look on rest of this structure,
|
|
|
|
; it is internal for getaddrinfo_start/process/abort
|
2012-07-11 14:08:45 +02:00
|
|
|
reqid dw ? ; DNS request ID
|
|
|
|
socktype db ? ; SOCK_* or 0 for any
|
|
|
|
db ?
|
|
|
|
service dd ?
|
|
|
|
flags dd ?
|
|
|
|
reserved rb 16
|
2009-10-06 13:52:00 +02:00
|
|
|
ends
|
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; int __stdcall getaddrinfo(__in const char* hostname, ;;
|
|
|
|
;; __in const char* servname, ;;
|
|
|
|
;; __in const struct addrinfo* hints, ;;
|
|
|
|
;; __out struct addrinfo **res); ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
getaddrinfo: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Get a list of IP addresses and port numbers for given host and service ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> first parameter (optional) = host name ;;
|
|
|
|
;> second parameter (optional) = service name (decimal number for now) ;;
|
2010-07-31 13:17:06 +02:00
|
|
|
;> third parameter (optional) = hints for socketnum type ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;> fourth parameter = pointer to result (head of L1-list) ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;< eax = 0 on success / one of EAI_ codes on error ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
; 0. Save used registers for __stdcall.
|
2012-07-11 14:08:45 +02:00
|
|
|
push ebx esi edi
|
|
|
|
mov edi, [esp+28] ; edi = res
|
2009-10-06 13:52:00 +02:00
|
|
|
; 1. Create and send DNS packet.
|
2012-07-11 14:08:45 +02:00
|
|
|
sub esp, sizeof.__gai_reqdata ; reserve stack place (1)
|
|
|
|
push esp ; fifth parameter = pointer to (1)
|
|
|
|
push edi ; fourth parameter = res
|
|
|
|
push dword [esp+32+sizeof.__gai_reqdata] ; third parameter = hints
|
|
|
|
push dword [esp+32+sizeof.__gai_reqdata] ; second parameter = servname
|
|
|
|
push dword [esp+32+sizeof.__gai_reqdata] ; first parameter = hostname
|
|
|
|
call getaddrinfo_start
|
|
|
|
test eax, eax
|
|
|
|
jns .ret ; if name resolved without network activity, return
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2. Wait for DNS reply.
|
|
|
|
; 2a. Ignore all events except network stack.
|
2012-07-11 14:08:45 +02:00
|
|
|
mcall 40, EVM_STACK
|
|
|
|
push eax ; save previous event mask (2)
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2b. Get upper limit for wait time. Use timeout = 5 seconds.
|
2012-07-11 14:08:45 +02:00
|
|
|
mcall 26, 9 ; get time stamp
|
|
|
|
xchg esi, eax ; save time stamp to esi
|
|
|
|
mov ebx, 500 ; start value for timeout
|
|
|
|
add esi, ebx
|
2009-10-06 13:52:00 +02:00
|
|
|
.wait:
|
|
|
|
; 2c. Wait for event with timeout.
|
2012-07-11 14:08:45 +02:00
|
|
|
mcall 23 ; wait for event - must be stack event
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2d. Check for timeout.
|
2012-07-11 14:08:45 +02:00
|
|
|
test eax, eax
|
|
|
|
lea eax, [esp+4] ; pointer to (1)
|
|
|
|
jz .timeout
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3. Got packet. Call processing function.
|
2012-07-11 14:08:45 +02:00
|
|
|
push edi ; second parameter: pointer to result
|
|
|
|
push eax ; first parameter: pointer to reqdata
|
|
|
|
call getaddrinfo_process
|
2009-10-06 13:52:00 +02:00
|
|
|
; 4. Test whether wait loop must be continued.
|
2012-07-11 14:08:45 +02:00
|
|
|
test eax, eax
|
|
|
|
jns .ret.restore
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2e. Recalculate timeout value.
|
2012-07-11 14:08:45 +02:00
|
|
|
mcall 26, 9
|
|
|
|
mov ebx, esi
|
|
|
|
sub ebx, eax
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2f. Check that time is not over; if not, continue wait loop
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp ebx, 500
|
|
|
|
jbe .wait
|
2009-10-06 13:52:00 +02:00
|
|
|
.timeout:
|
|
|
|
; 5. Timeout: abort and return error
|
2012-07-11 14:08:45 +02:00
|
|
|
push eax
|
|
|
|
call getaddrinfo_abort
|
|
|
|
and dword [edi], 0
|
|
|
|
push EAI_AGAIN
|
|
|
|
pop eax
|
2009-10-06 13:52:00 +02:00
|
|
|
.ret.restore:
|
|
|
|
; 6. Restore event mask.
|
2012-07-11 14:08:45 +02:00
|
|
|
pop ebx ; get event mask (2)
|
|
|
|
push eax ; save return code (3)
|
|
|
|
mcall 40
|
|
|
|
pop eax ; restore return code (3)
|
2009-10-06 13:52:00 +02:00
|
|
|
.ret:
|
|
|
|
; 7. Restore stack pointer, used registers and return.
|
2012-07-11 14:08:45 +02:00
|
|
|
add esp, sizeof.__gai_reqdata ; undo (1)
|
|
|
|
pop edi esi ebx
|
|
|
|
ret 16
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; int __stdcall getaddrinfo_start(__in const char* hostname, ;;
|
|
|
|
;; __in const char* servname, ;;
|
|
|
|
;; __in const struct addrinfo* hints, ;;
|
|
|
|
;; __out struct addrinfo **res, ;;
|
|
|
|
;; __out struct __gai_reqdata* reqdata); ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
getaddrinfo_start: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Initiator for getaddrinfo, sends DNS request ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> first 4 parameters same as for getaddrinfo ;;
|
|
|
|
;> last parameter = pointer to buffer for __gai_reqdata, must be passed to ;;
|
|
|
|
;> getaddrinfo_process as is ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;< eax = <0 if wait loop must be entered / 0 on success / EAI_* on error ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; Known limitations: ;;
|
|
|
|
;; 1. No support for TCP connections => ;;
|
|
|
|
;; 1a. Long replies will be truncated, and not all IP addresses will be got. ;;
|
|
|
|
;; 2. No support for iterative resolving => ;;
|
|
|
|
;; 2a. In theory may fail with some servers. ;;
|
|
|
|
;; 3. Assumes that domain for relative names is always root, ".". ;;
|
|
|
|
;; 4. Does not support lookup of services by name, ;;
|
|
|
|
;; only decimal representation is supported. ;;
|
|
|
|
;; 5. Assumes that IPv4 is always configured, so AI_ADDRCONFIG has no effect.;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
; 0. Create stack frame and save used registers for __stdcall.
|
2012-07-11 14:08:45 +02:00
|
|
|
push ebx esi edi
|
|
|
|
push ebp
|
|
|
|
mov ebp, esp
|
2009-10-06 13:52:00 +02:00
|
|
|
virtual at ebp-8
|
2012-07-11 14:08:45 +02:00
|
|
|
.recent_restsize dd ? ; this is for memory alloc in ._.generate_data
|
|
|
|
.recent_page dd ? ; this is for memory alloc in ._.generate_data
|
|
|
|
rd 5 ; saved regs and return address
|
|
|
|
.hostname dd ?
|
|
|
|
.servname dd ?
|
|
|
|
.hints dd ?
|
|
|
|
.res dd ?
|
|
|
|
.reqdata dd ?
|
2009-10-06 13:52:00 +02:00
|
|
|
end virtual
|
2012-07-11 14:08:45 +02:00
|
|
|
xor edi, edi
|
|
|
|
push edi ; init .recent_page
|
|
|
|
push edi ; init .recent_restsize
|
2009-10-06 13:52:00 +02:00
|
|
|
; 1. Check that parameters are correct and can be handled by this implementation.
|
|
|
|
; 1a. If 'res' pointer is given, set result to zero.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, [.res]
|
|
|
|
test eax, eax
|
|
|
|
jz @f
|
|
|
|
mov [eax], edi
|
2009-10-06 13:52:00 +02:00
|
|
|
@@:
|
|
|
|
; 1b. Only AI_SUPPORTED flags are supported for hints->ai_flags.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov ecx, [.hints]
|
|
|
|
xor edx, edx
|
|
|
|
jecxz .nohints
|
|
|
|
mov edx, [ecx+addrinfo.ai_flags]
|
2009-10-06 13:52:00 +02:00
|
|
|
.nohints:
|
2012-07-11 14:08:45 +02:00
|
|
|
mov ebx, [.reqdata]
|
|
|
|
mov [ebx+__gai_reqdata.flags], edx
|
|
|
|
push EAI_BADFLAGS
|
|
|
|
pop eax
|
|
|
|
test edx, not AI_SUPPORTED
|
|
|
|
jnz .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
; 1c. Either hostname or servname must be given. If AI_CANONNAME is set,
|
|
|
|
; hostname must also be set.
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp [.hostname], edi
|
|
|
|
jnz @f
|
|
|
|
test dl, AI_CANONNAME
|
|
|
|
jnz .ret
|
|
|
|
push EAI_NONAME
|
|
|
|
pop eax
|
|
|
|
cmp [.servname], edi
|
|
|
|
jz .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
@@:
|
|
|
|
; 1d. Only IPv4 is supported, so hints->ai_family must be either PF_UNSPEC or PF_INET.
|
2012-07-11 14:08:45 +02:00
|
|
|
push EAI_FAMILY
|
|
|
|
pop eax
|
|
|
|
jecxz @f
|
|
|
|
cmp [ecx+addrinfo.ai_family], edi
|
|
|
|
jz @f
|
|
|
|
cmp [ecx+addrinfo.ai_family], AF_INET4
|
|
|
|
jnz .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
@@:
|
|
|
|
; 1e. Valid combinations for ai_socktype/ai_protocol: 0/0 for any or
|
2009-10-07 22:36:58 +02:00
|
|
|
; SOCK_STREAM/IPPROTO_TCP, SOCK_DGRAM/IPPROTO_UDP
|
2010-07-31 13:17:06 +02:00
|
|
|
; (raw socketnums are not yet supported by the kernel)
|
2012-07-11 14:08:45 +02:00
|
|
|
xor edx, edx ; assume 0=any if no hints
|
|
|
|
jecxz .socketnum_type_ok
|
|
|
|
mov edx, [ecx+addrinfo.ai_socktype]
|
|
|
|
mov esi, [ecx+addrinfo.ai_protocol]
|
2009-10-06 13:52:00 +02:00
|
|
|
; 1f. Test for ai_socktype=0 and ai_protocol=0.
|
2012-07-11 14:08:45 +02:00
|
|
|
test edx, edx
|
|
|
|
jnz .check_socktype
|
|
|
|
test esi, esi
|
|
|
|
jz .socketnum_type_ok
|
2009-10-06 13:52:00 +02:00
|
|
|
; 1g. ai_socktype=0, ai_protocol is nonzero.
|
2012-07-11 14:08:45 +02:00
|
|
|
push EAI_SERVICE
|
|
|
|
pop eax
|
|
|
|
inc edx ; edx = SOCK_STREAM
|
|
|
|
cmp esi, IPPROTO_TCP
|
|
|
|
jz .socketnum_type_ok
|
|
|
|
inc edx ; edx = SOCK_DGRAM
|
|
|
|
cmp esi, IPPROTO_UDP
|
|
|
|
jz .socketnum_type_ok
|
2009-10-06 13:52:00 +02:00
|
|
|
.ret:
|
|
|
|
; Restore saved registers, destroy stack frame and return.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov esp, ebp
|
|
|
|
pop ebp
|
|
|
|
pop edi esi ebx
|
|
|
|
ret 20
|
2009-10-06 13:52:00 +02:00
|
|
|
; 1h. ai_socktype is nonzero.
|
|
|
|
.check_socktype:
|
2012-07-11 14:08:45 +02:00
|
|
|
push EAI_SOCKTYPE
|
|
|
|
pop eax
|
|
|
|
cmp edx, SOCK_STREAM
|
|
|
|
jz .check_tcp
|
|
|
|
cmp edx, SOCK_DGRAM
|
|
|
|
jnz .ret
|
|
|
|
test esi, esi
|
|
|
|
jz .socketnum_type_ok
|
|
|
|
cmp esi, IPPROTO_UDP
|
|
|
|
jz .socketnum_type_ok
|
|
|
|
jmp .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
.check_tcp:
|
2012-07-11 14:08:45 +02:00
|
|
|
test esi, esi
|
|
|
|
jz .socketnum_type_ok
|
|
|
|
cmp esi, IPPROTO_TCP
|
|
|
|
jnz .ret
|
2010-07-31 13:17:06 +02:00
|
|
|
.socketnum_type_ok:
|
2012-07-11 14:08:45 +02:00
|
|
|
mov [ebx+__gai_reqdata.socktype], dl
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2. Resolve service.
|
|
|
|
; 2a. If no name is given, remember value -1.
|
2012-07-11 14:08:45 +02:00
|
|
|
push -1
|
|
|
|
pop edx
|
|
|
|
mov esi, [.servname]
|
|
|
|
test esi, esi
|
|
|
|
jz .service_resolved
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2b. Loop for characters of string while digits are encountered.
|
2012-07-11 14:08:45 +02:00
|
|
|
xor edx, edx
|
|
|
|
xor eax, eax
|
2009-10-06 13:52:00 +02:00
|
|
|
.serv_to_number:
|
2012-07-11 14:08:45 +02:00
|
|
|
lodsb
|
|
|
|
sub al, '0'
|
|
|
|
cmp al, 9
|
|
|
|
ja .serv_to_number_done
|
2009-10-06 13:52:00 +02:00
|
|
|
; for each digit, set edx = edx*10 + <digit>
|
2012-07-11 14:08:45 +02:00
|
|
|
lea edx, [edx*5]
|
|
|
|
lea edx, [edx*2+eax]
|
2009-10-06 13:52:00 +02:00
|
|
|
; check for correctness: service port must fit in word
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp edx, 0x10000
|
|
|
|
jae .service_not_number
|
|
|
|
jmp .serv_to_number
|
2009-10-06 13:52:00 +02:00
|
|
|
.serv_to_number_done:
|
2012-07-11 14:08:45 +02:00
|
|
|
and edx, 0xFFFF ; make sure that port fits
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2c. If zero character reached, name is resolved;
|
|
|
|
; otherwise, return error (no support for symbolic names yet)
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp al, -'0'
|
|
|
|
jz .service_resolved
|
2009-10-06 13:52:00 +02:00
|
|
|
.service_not_number:
|
2012-07-11 14:08:45 +02:00
|
|
|
push EAI_NONAME
|
|
|
|
pop eax
|
|
|
|
jmp .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
.service_resolved:
|
|
|
|
; 2d. Save result to reqdata.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov [ebx+__gai_reqdata.service], edx
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3. Process host name.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov esi, [.hostname]
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3a. If hostname is not given,
|
2010-07-31 13:17:06 +02:00
|
|
|
; use localhost for active socketnums and INADDR_ANY for passive socketnums.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, 0x0100007F ; 127.0.0.1 in network byte order
|
|
|
|
test byte [ebx+__gai_reqdata.flags], AI_PASSIVE
|
|
|
|
jz @f
|
|
|
|
xor eax, eax
|
2009-10-06 13:52:00 +02:00
|
|
|
@@:
|
2012-07-11 14:08:45 +02:00
|
|
|
test esi, esi
|
|
|
|
jz .hostname_is_ip
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3b. Check for dotted IPv4 name.
|
2012-07-11 14:08:45 +02:00
|
|
|
push esi
|
|
|
|
call inet_addr
|
|
|
|
cmp eax, -1
|
|
|
|
jz .resolve_hostname
|
2009-10-06 13:52:00 +02:00
|
|
|
.hostname_is_ip:
|
|
|
|
; 3c. hostname is valid representation of IP address, and we have resolved it.
|
|
|
|
; Generate result, if .res pointer is not NULL.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov ebx, [.reqdata]
|
|
|
|
mov esi, [.res]
|
|
|
|
test esi, esi
|
|
|
|
jz .no_result
|
|
|
|
call getaddrinfo._.generate_data
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3d. Check for memory allocation error.
|
|
|
|
.3d:
|
2012-07-11 14:08:45 +02:00
|
|
|
push EAI_MEMORY
|
|
|
|
pop eax
|
|
|
|
test esi, esi
|
|
|
|
jz .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3e. If AI_CANONNAME is set, copy input name.
|
2012-07-11 14:08:45 +02:00
|
|
|
test byte [ebx+__gai_reqdata.flags], AI_CANONNAME
|
|
|
|
jz .no_result
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3f. Calculate length of name.
|
2012-07-11 14:08:45 +02:00
|
|
|
push -1
|
|
|
|
pop ecx
|
|
|
|
mov edi, [.hostname]
|
|
|
|
xor eax, eax
|
|
|
|
repnz scasb
|
|
|
|
not ecx
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3g. Check whether it fits on one page with main data.
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp ecx, [.recent_restsize]
|
|
|
|
jbe .name_fits
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3h. If not, allocate new page.
|
2012-07-11 14:08:45 +02:00
|
|
|
push ecx
|
|
|
|
add ecx, 4 ; first dword contains number of objects on the page
|
|
|
|
mcall 68, 12
|
|
|
|
pop ecx
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3i. If allocation has failed, free addrinfo and return error.
|
2012-07-11 14:08:45 +02:00
|
|
|
test eax, eax
|
|
|
|
jnz .name_allocated
|
|
|
|
push [.res]
|
|
|
|
call freeaddrinfo
|
|
|
|
push EAI_MEMORY
|
|
|
|
pop eax
|
|
|
|
jmp .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
.name_allocated:
|
|
|
|
; 3j. Otherwise, set edi to allocated memory and continue to 3l.
|
2012-07-11 14:08:45 +02:00
|
|
|
xchg edi, eax ; put result to edi
|
|
|
|
push 1
|
|
|
|
pop eax
|
|
|
|
stosd ; number of objects on the page = 1
|
|
|
|
jmp .copy_name
|
2009-10-06 13:52:00 +02:00
|
|
|
.name_fits:
|
|
|
|
; 3k. Get pointer to free memory in allocated page.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov edi, [.recent_page]
|
|
|
|
mov eax, edi
|
|
|
|
and eax, not 0xFFF
|
|
|
|
inc dword [eax] ; increase number of objects
|
2009-10-06 13:52:00 +02:00
|
|
|
.copy_name:
|
|
|
|
; 3l. Put pointer to struct addrinfo.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, [.res]
|
|
|
|
mov eax, [eax]
|
|
|
|
mov [eax+addrinfo.ai_canonname], edi
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3m. Copy name.
|
2012-07-11 14:08:45 +02:00
|
|
|
rep movsb
|
2009-10-06 13:52:00 +02:00
|
|
|
.no_result:
|
|
|
|
; 3n. Return success.
|
2012-07-11 14:08:45 +02:00
|
|
|
xor eax, eax
|
|
|
|
jmp .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
; 4. Host address is not dotted IP. Test whether we are allowed to contact DNS.
|
|
|
|
; Return error if no.
|
|
|
|
.resolve_hostname:
|
2012-07-11 14:08:45 +02:00
|
|
|
push EAI_NONAME
|
|
|
|
pop eax
|
|
|
|
mov ebx, [.reqdata]
|
|
|
|
test byte [ebx+__gai_reqdata.flags], AI_NUMERICHOST
|
|
|
|
jnz .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
; Host address is domain name. Contact DNS server.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov esi, [.hostname]
|
2009-10-06 13:52:00 +02:00
|
|
|
; 5. Reserve stack place for UDP packet.
|
|
|
|
; According to RFC1035, maximum UDP packet size in DNS is 512 bytes.
|
2012-07-11 14:08:45 +02:00
|
|
|
sub esp, 512
|
2009-10-06 13:52:00 +02:00
|
|
|
; 6. Create DNS request packet.
|
|
|
|
; 6a. Set pointer to start of buffer.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov edi, esp
|
2009-10-06 13:52:00 +02:00
|
|
|
; 6b. Get request ID, write it to buffer.
|
2012-07-11 14:08:45 +02:00
|
|
|
push 1
|
|
|
|
pop eax
|
|
|
|
lock xadd [DNSrequestID], eax ; atomically increment ID, get old value
|
|
|
|
stosw
|
|
|
|
mov [ebx+__gai_reqdata.reqid], ax
|
2009-10-06 13:52:00 +02:00
|
|
|
; 6c. Packed field: QR=0 (query), Opcode=0000 (standard query),
|
2009-10-07 22:36:58 +02:00
|
|
|
; AA=0 (ignored in requests), TC=0 (no truncation),
|
|
|
|
; RD=1 (recursion desired)
|
2012-07-11 14:08:45 +02:00
|
|
|
mov al, 00000001b
|
|
|
|
stosb
|
2009-10-06 13:52:00 +02:00
|
|
|
; 6d. Packed field: ignored in requests
|
2012-07-11 14:08:45 +02:00
|
|
|
mov al, 0
|
|
|
|
stosb
|
2009-10-06 13:52:00 +02:00
|
|
|
; 6e. Write questions count = 1 and answers count = 0
|
|
|
|
; Note that network byte order is big-endian.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, 0x00000100
|
|
|
|
stosd
|
2009-10-06 13:52:00 +02:00
|
|
|
; 6f. Write nameservers count = 0 and additional records count = 0
|
2012-07-11 14:08:45 +02:00
|
|
|
xor eax, eax
|
|
|
|
stosd
|
2009-10-06 13:52:00 +02:00
|
|
|
; 6g. Write request data: name
|
|
|
|
; According to RFC1035, maximum length of name is 255 bytes.
|
|
|
|
; For correct names, buffer cannot overflow.
|
2012-07-11 14:08:45 +02:00
|
|
|
lea ebx, [esi+256] ; ebx = limit for name (including terminating zero)
|
2009-10-06 13:52:00 +02:00
|
|
|
; translate string "www.yandex.ru" {00} to byte data {03} "www" {06} "yandex" {02} "ru" {00}
|
2012-07-11 14:08:45 +02:00
|
|
|
.nameloop: ; here we go in the start of each label: before "www", before "yandex", before "ru"
|
|
|
|
xor ecx, ecx ; ecx = length of current label
|
|
|
|
inc edi ; skip length, it will be filled later
|
|
|
|
.labelloop: ; here we go for each symbol of name
|
|
|
|
lodsb ; get next character
|
|
|
|
test al, al ; terminating zero?
|
|
|
|
jz .endname
|
|
|
|
cmp esi, ebx ; limit exceeded?
|
|
|
|
jae .wrongname
|
|
|
|
cmp al, '.' ; end of label?
|
|
|
|
jz .labelend
|
|
|
|
stosb ; put next character
|
|
|
|
inc ecx ; increment label length
|
|
|
|
jmp .labelloop
|
2009-10-06 13:52:00 +02:00
|
|
|
.wrongname:
|
2012-07-11 14:08:45 +02:00
|
|
|
push EAI_NONAME
|
|
|
|
pop eax
|
|
|
|
jmp .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
.labelend:
|
2012-07-11 14:08:45 +02:00
|
|
|
test ecx, ecx ; null label can be only in the end of name
|
|
|
|
jz .wrongname
|
2009-10-06 13:52:00 +02:00
|
|
|
.endname:
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp ecx, 63
|
|
|
|
ja .wrongname
|
2009-10-06 13:52:00 +02:00
|
|
|
; write length to byte [edi-ecx-1]
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, ecx
|
|
|
|
neg eax
|
|
|
|
mov byte [edi+eax-1], cl
|
|
|
|
cmp byte [esi-1], 0 ; that was last label in the name?
|
|
|
|
jnz .nameloop
|
2009-10-06 13:52:00 +02:00
|
|
|
; write terminating zero if not yet
|
2012-07-11 14:08:45 +02:00
|
|
|
mov al, 0
|
|
|
|
cmp byte [edi-1], al
|
|
|
|
jz @f
|
|
|
|
stosb
|
2009-10-06 13:52:00 +02:00
|
|
|
@@:
|
|
|
|
; 6h. Write request data:
|
2009-10-07 22:36:58 +02:00
|
|
|
; query type = A (host address) = 1,
|
|
|
|
; query class = IN (internet IPv4 address) = 1
|
2009-10-06 13:52:00 +02:00
|
|
|
; Note that network byte order is big-endian.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, 0x01000100
|
|
|
|
stosd
|
2009-10-06 13:52:00 +02:00
|
|
|
; 7. Get DNS server address.
|
2012-07-11 14:08:45 +02:00
|
|
|
mcall 76, API_IPv4 + 4 ; protocol IP=0, device number=0, function=get DNS address
|
|
|
|
cmp eax, -1
|
|
|
|
je .ret.dnserr
|
|
|
|
mov esi, eax ; put server address to esi
|
2010-07-31 13:17:06 +02:00
|
|
|
; 8. Open UDP socketnum to DNS server, port 53.
|
|
|
|
; 8a. Create new socketnum.
|
2012-07-11 14:08:45 +02:00
|
|
|
mcall 75, 0, AF_INET4, SOCK_DGRAM
|
|
|
|
cmp eax, -1 ; error?
|
|
|
|
jz .ret.dnserr
|
|
|
|
mov ecx, eax ; put socketnum handle to ecx
|
2009-10-06 13:52:00 +02:00
|
|
|
; 8b. Create sockaddr structure on the stack.
|
2012-07-11 14:08:45 +02:00
|
|
|
push 0
|
|
|
|
push 0 ; sin_zero
|
|
|
|
push esi ; sin_addr
|
|
|
|
push AF_INET4 + (53 shl 16)
|
|
|
|
; sin_family and sin_port in network byte order
|
2009-10-12 20:14:14 +02:00
|
|
|
; 8c. Connect.
|
2012-07-11 14:08:45 +02:00
|
|
|
mcall 75, 4, , esp, sizeof.sockaddr_in
|
2009-10-06 13:52:00 +02:00
|
|
|
; 8d. Restore the stack, undo 8b.
|
2012-07-11 14:08:45 +02:00
|
|
|
add esp, esi
|
2009-10-06 13:52:00 +02:00
|
|
|
; 8e. Check result.
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp eax, -1
|
|
|
|
jz .ret.close
|
2009-10-06 13:52:00 +02:00
|
|
|
; 9. Send DNS request packet.
|
2012-07-11 14:08:45 +02:00
|
|
|
sub edi, esp ; get packet length
|
|
|
|
mov esi, edi
|
|
|
|
xor edi, edi
|
|
|
|
mcall 75, 6, , esp
|
|
|
|
cmp eax, -1
|
|
|
|
jz .ret.close
|
|
|
|
mov eax, [.reqdata]
|
|
|
|
mov [eax+__gai_reqdata.socketnum], ecx
|
|
|
|
push -1
|
|
|
|
pop eax ; return status: more processing required
|
|
|
|
jmp .ret.dns
|
2009-10-06 13:52:00 +02:00
|
|
|
.ret.close:
|
2012-07-11 14:08:45 +02:00
|
|
|
mcall 75, 1
|
2009-10-06 13:52:00 +02:00
|
|
|
.ret.dnserr:
|
2012-07-11 14:08:45 +02:00
|
|
|
push EAI_AGAIN
|
|
|
|
pop eax
|
2009-10-06 13:52:00 +02:00
|
|
|
.ret.dns:
|
|
|
|
; 6. Restore stack pointer and return.
|
2012-07-11 14:08:45 +02:00
|
|
|
jmp .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; int __stdcall getaddrinfo_process(__in struct __gai_reqdata* reqdata, ;;
|
|
|
|
;; __out struct addrinfo** res); ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
getaddrinfo_process: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Processes network events from DNS reply ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> first parameter = pointer to struct __gai_reqdata filled by ..._start ;;
|
|
|
|
;> second parameter = same as for getaddrinfo ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;< eax = -1 if more processing required / 0 on success / >0 = error code ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
; 0. Create stack frame.
|
2012-07-11 14:08:45 +02:00
|
|
|
push ebp
|
|
|
|
mov ebp, esp
|
2009-10-06 13:52:00 +02:00
|
|
|
virtual at ebp-.locals_size
|
|
|
|
.locals_start:
|
2012-07-11 14:08:45 +02:00
|
|
|
.datagram rb 512
|
|
|
|
.addrname dd ?
|
|
|
|
.name dd ?
|
|
|
|
.res_list_tail dd ?
|
|
|
|
.cname dd ?
|
|
|
|
.recent_restsize dd ? ; this is for memory alloc in ._.generate_data
|
|
|
|
.recent_page dd ? ; this is for memory alloc in ._.generate_data
|
2009-10-06 13:52:00 +02:00
|
|
|
.locals_size = $ - .locals_start
|
2012-07-11 14:08:45 +02:00
|
|
|
rd 2
|
|
|
|
.reqdata dd ?
|
|
|
|
.res dd ?
|
2009-10-06 13:52:00 +02:00
|
|
|
end virtual
|
2012-07-11 14:08:45 +02:00
|
|
|
xor eax, eax
|
|
|
|
push eax ; initialize .recent_page
|
|
|
|
push eax ; initialize .recent_restsize
|
|
|
|
push eax ; initialize .cname
|
|
|
|
push [.res] ; initialize .res_list_tail
|
|
|
|
sub esp, .locals_size-16 ; reserve place for other vars
|
|
|
|
mov edx, esp ; edx -> buffer for datagram
|
2009-10-06 13:52:00 +02:00
|
|
|
; 1. Save used registers for __stdcall.
|
2012-07-11 14:08:45 +02:00
|
|
|
push ebx esi edi
|
|
|
|
mov edi, [.reqdata]
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2. Read UDP datagram.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov ecx, [edi+__gai_reqdata.socketnum]
|
|
|
|
push edi
|
|
|
|
mcall 75, 7, , , 512, 0
|
|
|
|
pop edi
|
2010-07-31 13:17:06 +02:00
|
|
|
; 3. Ignore events for other socketnums (return if no data read)
|
2012-07-11 14:08:45 +02:00
|
|
|
test eax, eax
|
|
|
|
jz .ret.more_processing_required
|
2009-10-06 13:52:00 +02:00
|
|
|
; 4. Sanity check: discard too short packets.
|
2012-07-11 14:08:45 +02:00
|
|
|
xchg ecx, eax ; save packet length in ecx
|
|
|
|
cmp ecx, 12
|
|
|
|
jb .ret.more_processing_required
|
2009-10-06 13:52:00 +02:00
|
|
|
; 5. Discard packets with ID != request ID.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, dword [edi+__gai_reqdata.reqid]
|
|
|
|
cmp ax, [edx]
|
|
|
|
jnz .ret.more_processing_required
|
2009-10-06 13:52:00 +02:00
|
|
|
; 6. Sanity check: discard query packets.
|
2012-07-11 14:08:45 +02:00
|
|
|
test byte [edx+2], 80h
|
|
|
|
jz .ret.more_processing_required
|
2009-10-06 13:52:00 +02:00
|
|
|
; 7. Sanity check: must be exactly one query (our).
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp word [edx+4], 0x0100 ; note network byte order
|
|
|
|
jnz .ret.more_processing_required
|
2009-10-06 13:52:00 +02:00
|
|
|
; 8. Check for errors. Return EAI_NONAME for error code 3 and EAI_FAIL for other.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov al, [edx+3]
|
|
|
|
and al, 0xF
|
|
|
|
jz @f
|
|
|
|
cmp al, 3
|
|
|
|
jnz .ret.no_recovery
|
|
|
|
jmp .ret.no_name
|
2009-10-06 13:52:00 +02:00
|
|
|
@@:
|
|
|
|
; 9. Locate answers section. Exactly 1 query is present in this packet.
|
2012-07-11 14:08:45 +02:00
|
|
|
add ecx, edx ; ecx = limit
|
|
|
|
lea esi, [edx+12]
|
|
|
|
call .skip_name
|
|
|
|
lodsd ; skip QTYPE and QCLASS field
|
|
|
|
cmp esi, ecx
|
|
|
|
ja .ret.no_recovery
|
2009-10-06 13:52:00 +02:00
|
|
|
; 10. Loop through all answers.
|
2012-07-11 14:08:45 +02:00
|
|
|
movzx ebx, word [edx+6] ; get answers count
|
|
|
|
xchg bl, bh ; network -> Intel byte order
|
2009-10-06 13:52:00 +02:00
|
|
|
.answers_loop:
|
2012-07-11 14:08:45 +02:00
|
|
|
dec ebx
|
|
|
|
js .answers_done
|
2009-10-06 13:52:00 +02:00
|
|
|
; 10a. Process each record.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov [.name], esi
|
2009-10-06 13:52:00 +02:00
|
|
|
; 10b. Skip name field.
|
2012-07-11 14:08:45 +02:00
|
|
|
call .skip_name
|
2009-10-06 13:52:00 +02:00
|
|
|
; 10c. Get record information, handle two types for class IN (internet).
|
2012-07-11 14:08:45 +02:00
|
|
|
lodsd ; get type and class
|
|
|
|
cmp esi, ecx
|
|
|
|
ja .ret.no_recovery
|
|
|
|
cmp eax, 0x01000500 ; type=5, class=1?
|
|
|
|
jz .got_cname
|
|
|
|
cmp eax, 0x01000100 ; type=1, class=1?
|
|
|
|
jnz .answers_loop.next
|
2009-10-06 13:52:00 +02:00
|
|
|
.got_addr:
|
|
|
|
; 10d. Process record A, host address.
|
2012-07-11 14:08:45 +02:00
|
|
|
add esi, 10
|
|
|
|
cmp esi, ecx
|
|
|
|
ja .ret.no_recovery
|
|
|
|
cmp word [esi-6], 0x0400 ; RDATA for A records must be 4 bytes long
|
|
|
|
jnz .ret.no_recovery
|
|
|
|
mov eax, [.name]
|
|
|
|
mov [.addrname], eax
|
2009-10-06 13:52:00 +02:00
|
|
|
; 10e. Create corresponding record in the answer.
|
2012-07-11 14:08:45 +02:00
|
|
|
push ebx ecx esi
|
|
|
|
mov eax, [esi-4] ; IP address
|
|
|
|
mov esi, [.res_list_tail] ; pointer to result
|
|
|
|
test esi, esi
|
|
|
|
jz .no_result ; do not save if .res is NULL
|
|
|
|
mov ebx, [.reqdata] ; request data
|
|
|
|
call getaddrinfo._.generate_data
|
|
|
|
mov [.res_list_tail], esi
|
|
|
|
pop esi ecx ebx
|
|
|
|
cmp [.res_list_tail], 0
|
|
|
|
jnz .answers_loop
|
2009-10-06 13:52:00 +02:00
|
|
|
; 10f. If generate_data failed (this means memory allocation failure), abort
|
2012-07-11 14:08:45 +02:00
|
|
|
jmp .ret.no_memory
|
2009-10-06 13:52:00 +02:00
|
|
|
.no_result:
|
2012-07-11 14:08:45 +02:00
|
|
|
pop esi ecx ebx
|
|
|
|
jmp .answers_loop
|
2009-10-06 13:52:00 +02:00
|
|
|
.got_cname:
|
|
|
|
; 10g. Process record CNAME, main host name.
|
2012-07-11 14:08:45 +02:00
|
|
|
lea eax, [esi+6]
|
|
|
|
mov [.cname], eax
|
2009-10-06 13:52:00 +02:00
|
|
|
.answers_loop.next:
|
|
|
|
; 10h. Skip other record fields, advance to next record.
|
2012-07-11 14:08:45 +02:00
|
|
|
lodsd ; skip TTL
|
|
|
|
xor eax, eax
|
|
|
|
lodsw ; get length of RDATA field
|
|
|
|
xchg al, ah ; network -> Intel byte order
|
|
|
|
add esi, eax
|
|
|
|
cmp esi, ecx
|
|
|
|
ja .ret.no_recovery
|
|
|
|
jmp .answers_loop
|
2009-10-06 13:52:00 +02:00
|
|
|
.answers_done:
|
|
|
|
; 11. Check that there is at least 1 answer.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, [.res_list_tail]
|
|
|
|
cmp [.res], eax
|
|
|
|
jz .ret.no_data
|
2009-10-06 13:52:00 +02:00
|
|
|
; 12. If canonical name was required, add it now.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, [.reqdata]
|
|
|
|
test byte [eax+__gai_reqdata.flags], AI_CANONNAME
|
|
|
|
jz .no_canon_name
|
2009-10-06 13:52:00 +02:00
|
|
|
; 12a. If at least one CNAME record is present, use name from last such record.
|
|
|
|
; Otherwise, use name from one of A records.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov esi, [.cname]
|
|
|
|
test esi, esi
|
|
|
|
jnz .has_cname
|
|
|
|
mov esi, [.addrname]
|
2009-10-06 13:52:00 +02:00
|
|
|
.has_cname:
|
|
|
|
; 12b. Calculate name length.
|
2012-07-11 14:08:45 +02:00
|
|
|
call .get_name_length
|
|
|
|
jc .ret.no_recovery
|
2009-10-06 13:52:00 +02:00
|
|
|
; 12c. Check that the caller really want to get data.
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp [.res], 0
|
|
|
|
jz .no_canon_name
|
2009-10-06 13:52:00 +02:00
|
|
|
; 12d. Allocate memory for name.
|
2012-07-11 14:08:45 +02:00
|
|
|
call getaddrinfo._.memalloc
|
|
|
|
test edi, edi
|
|
|
|
jz .ret.no_memory
|
2009-10-06 13:52:00 +02:00
|
|
|
; 12e. Make first entry in .res list point to canonical name.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, [.res]
|
|
|
|
mov eax, [eax]
|
|
|
|
mov [eax+addrinfo.ai_canonname], edi
|
2009-10-06 13:52:00 +02:00
|
|
|
; 12f. Decode name.
|
2012-07-11 14:08:45 +02:00
|
|
|
call .decode_name
|
2009-10-06 13:52:00 +02:00
|
|
|
.no_canon_name:
|
|
|
|
; 13. Set status to success.
|
2012-07-11 14:08:45 +02:00
|
|
|
xor eax, eax
|
|
|
|
jmp .ret.close
|
2009-10-06 13:52:00 +02:00
|
|
|
; Handle errors.
|
|
|
|
.ret.more_processing_required:
|
2012-07-11 14:08:45 +02:00
|
|
|
push -1
|
|
|
|
pop eax
|
|
|
|
jmp .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
.ret.no_recovery:
|
2012-07-11 14:08:45 +02:00
|
|
|
push EAI_FAIL
|
|
|
|
pop eax
|
|
|
|
jmp .ret.destroy
|
2009-10-06 13:52:00 +02:00
|
|
|
.ret.no_memory:
|
2012-07-11 14:08:45 +02:00
|
|
|
push EAI_MEMORY
|
|
|
|
pop eax
|
|
|
|
jmp .ret.destroy
|
2009-10-06 13:52:00 +02:00
|
|
|
.ret.no_name:
|
|
|
|
.ret.no_data:
|
2012-07-11 14:08:45 +02:00
|
|
|
push EAI_NONAME
|
|
|
|
pop eax
|
2009-10-06 13:52:00 +02:00
|
|
|
.ret.destroy:
|
|
|
|
; 14. If an error occured, free memory acquired so far.
|
2012-07-11 14:08:45 +02:00
|
|
|
push eax
|
|
|
|
mov esi, [.res]
|
|
|
|
test esi, esi
|
|
|
|
jz @f
|
|
|
|
pushd [esi]
|
|
|
|
call freeaddrinfo
|
|
|
|
and dword [esi], 0
|
2009-10-06 13:52:00 +02:00
|
|
|
@@:
|
2012-07-11 14:08:45 +02:00
|
|
|
pop eax
|
2009-10-06 13:52:00 +02:00
|
|
|
.ret.close:
|
2010-07-31 13:17:06 +02:00
|
|
|
; 15. Close socketnum.
|
2012-07-11 14:08:45 +02:00
|
|
|
push eax
|
|
|
|
mov ecx, [.reqdata]
|
|
|
|
mov ecx, [ecx+__gai_reqdata.socketnum]
|
|
|
|
mcall 75, 1
|
|
|
|
pop eax
|
2009-10-06 13:52:00 +02:00
|
|
|
; 16. Restore used registers, destroy stack frame and return.
|
|
|
|
.ret:
|
2012-07-11 14:08:45 +02:00
|
|
|
pop edi esi ebx
|
|
|
|
mov esp, ebp
|
|
|
|
pop ebp
|
|
|
|
ret 8
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; Internal auxiliary function for skipping names in DNS packet. ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
.skip_name: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Skips name in DNS packet. ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> esi -> name ;;
|
|
|
|
;> ecx = end of packet ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;< esi -> end of name ;;
|
|
|
|
;;===========================================================================;;
|
2012-07-11 14:08:45 +02:00
|
|
|
xor eax, eax
|
|
|
|
cmp esi, ecx
|
|
|
|
jae .skip_name.done
|
|
|
|
lodsb
|
|
|
|
test al, al
|
|
|
|
jz .skip_name.done
|
|
|
|
test al, 0xC0
|
|
|
|
jnz .skip_name.pointer
|
|
|
|
add esi, eax
|
|
|
|
jmp .skip_name
|
2009-10-06 13:52:00 +02:00
|
|
|
.skip_name.pointer:
|
2012-07-11 14:08:45 +02:00
|
|
|
inc esi
|
2009-10-06 13:52:00 +02:00
|
|
|
.skip_name.done:
|
2012-07-11 14:08:45 +02:00
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; Internal auxiliary function for calculating length of name in DNS packet. ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
.get_name_length: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Calculate length of name (including terminating zero) in DNS packet. ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> edx = start of packet ;;
|
|
|
|
;> esi -> name ;;
|
|
|
|
;> ecx = end of packet ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;< eax = length of name ;;
|
|
|
|
;< CF set on error / cleared on success ;;
|
|
|
|
;;===========================================================================;;
|
2012-07-11 14:08:45 +02:00
|
|
|
xor ebx, ebx ; ebx will hold data length
|
2009-10-06 13:52:00 +02:00
|
|
|
.get_name_length.zero:
|
2012-07-11 14:08:45 +02:00
|
|
|
xor eax, eax
|
2009-10-06 13:52:00 +02:00
|
|
|
.get_name_length.loop:
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp esi, ecx
|
|
|
|
jae .get_name_length.fail
|
|
|
|
lodsb
|
|
|
|
test al, al
|
|
|
|
jz .get_name_length.done
|
|
|
|
test al, 0xC0
|
|
|
|
jnz .get_name_length.pointer
|
|
|
|
add esi, eax
|
|
|
|
inc ebx
|
|
|
|
add ebx, eax
|
|
|
|
cmp ebx, 256
|
|
|
|
jbe .get_name_length.loop
|
2009-10-06 13:52:00 +02:00
|
|
|
.get_name_length.fail:
|
2012-07-11 14:08:45 +02:00
|
|
|
stc
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
.get_name_length.pointer:
|
2012-07-11 14:08:45 +02:00
|
|
|
and al, 0x3F
|
|
|
|
mov ah, al
|
|
|
|
lodsb
|
|
|
|
lea esi, [edx+eax]
|
|
|
|
jmp .get_name_length.zero
|
2009-10-06 13:52:00 +02:00
|
|
|
.get_name_length.done:
|
2012-07-11 14:08:45 +02:00
|
|
|
test ebx, ebx
|
|
|
|
jz .get_name_length.fail
|
|
|
|
xchg eax, ebx
|
|
|
|
clc
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; Internal auxiliary function for decoding DNS name. ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
.decode_name: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Decode name in DNS packet. ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> edx = start of packet ;;
|
|
|
|
;> esi -> name in packet ;;
|
|
|
|
;> edi -> buffer for decoded name ;;
|
|
|
|
;;===========================================================================;;
|
2012-07-11 14:08:45 +02:00
|
|
|
xor eax, eax
|
|
|
|
lodsb
|
|
|
|
test al, al
|
|
|
|
jz .decode_name.done
|
|
|
|
test al, 0xC0
|
|
|
|
jnz .decode_name.pointer
|
|
|
|
mov ecx, eax
|
|
|
|
rep movsb
|
|
|
|
mov al, '.'
|
|
|
|
stosb
|
|
|
|
jmp .decode_name
|
2009-10-06 13:52:00 +02:00
|
|
|
.decode_name.pointer:
|
2012-07-11 14:08:45 +02:00
|
|
|
and al, 0x3F
|
|
|
|
mov ah, al
|
|
|
|
lodsb
|
|
|
|
lea esi, [edx+eax]
|
|
|
|
jmp .decode_name
|
2009-10-06 13:52:00 +02:00
|
|
|
.decode_name.done:
|
2012-07-11 14:08:45 +02:00
|
|
|
mov byte [edi-1], 0
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; Internal auxiliary function for allocating memory for getaddrinfo. ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
getaddrinfo._.memalloc: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Memory allocation. ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> eax = size in bytes, must be less than page size. ;;
|
|
|
|
;> [ebp-4] = .recent_page = last allocated page ;;
|
|
|
|
;> [ebp-8] = .recent_restsize = bytes rest in last allocated page ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;< edi -> allocated memory / NULL on error ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
; 1. Set edi to result of function.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov edi, [ebp-4]
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2. Check whether we need to allocate a new page.
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp eax, [ebp-8]
|
|
|
|
jbe .no_new_page
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2. Allocate new page if need. Reset edi to new result.
|
2012-07-11 14:08:45 +02:00
|
|
|
push eax ebx
|
|
|
|
mcall 68, 12, 0x1000
|
|
|
|
xchg edi, eax ; put result to edi
|
|
|
|
pop ebx eax
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3. Check returned value of allocator. Fail if it failed.
|
2012-07-11 14:08:45 +02:00
|
|
|
test edi, edi
|
|
|
|
jz .ret
|
2009-10-06 13:52:00 +02:00
|
|
|
; 4. Update .recent_page and .recent_restsize.
|
2012-07-11 14:08:45 +02:00
|
|
|
add edi, 4
|
|
|
|
sub ecx, 4
|
|
|
|
mov [ebp-4], edi
|
|
|
|
mov [ebp-8], ecx
|
2009-10-06 13:52:00 +02:00
|
|
|
.no_new_page:
|
|
|
|
; 5. Increase number of objects on this page.
|
2012-07-11 14:08:45 +02:00
|
|
|
push eax
|
|
|
|
mov eax, edi
|
|
|
|
and eax, not 0xFFF
|
|
|
|
inc dword [eax]
|
|
|
|
pop eax
|
2009-10-06 13:52:00 +02:00
|
|
|
; 6. Advance last allocated pointer, decrease memory size.
|
2012-07-11 14:08:45 +02:00
|
|
|
add [ebp-4], eax
|
|
|
|
sub [ebp-8], eax
|
2009-10-06 13:52:00 +02:00
|
|
|
; 7. Return.
|
|
|
|
.ret:
|
2012-07-11 14:08:45 +02:00
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; Internal auxiliary function for freeing memory for freeaddrinfo. ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
getaddrinfo._.memfree: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Free memory. ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> eax = pointer ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
; 1. Get start of page.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov ecx, eax
|
|
|
|
and ecx, not 0xFFF
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2. Decrease number of objects.
|
2012-07-11 14:08:45 +02:00
|
|
|
dec dword [ecx]
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3. If it goes to zero, free the page.
|
2012-07-11 14:08:45 +02:00
|
|
|
jnz @f
|
|
|
|
push ebx
|
|
|
|
mcall 68, 13
|
|
|
|
pop ebx
|
2009-10-06 13:52:00 +02:00
|
|
|
@@:
|
|
|
|
; 4. Done.
|
2012-07-11 14:08:45 +02:00
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
2012-07-11 14:08:45 +02:00
|
|
|
getaddrinfo._.generate_data: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Generate item(s) of getaddrinfo result list by one IP address. ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> eax = IP address ;;
|
|
|
|
;> ebx = request data ;;
|
|
|
|
;> esi = pointer to result ;;
|
|
|
|
;> [ebp-4] = .recent_page = last allocated page ;;
|
|
|
|
;> [ebp-8] = .recent_restsize = bytes rest in last allocated page ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;< esi = pointer to next list item for result / NULL on error ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
; 1. If no service is given, append one item with zero port.
|
|
|
|
; append one item with zero socktype/protocol/port.
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp [ebx+__gai_reqdata.service], -1
|
|
|
|
jnz .has_service
|
|
|
|
call .append_item
|
2009-10-06 13:52:00 +02:00
|
|
|
; 1a. If neither protocol nor socktype were specified,
|
2009-10-07 22:36:58 +02:00
|
|
|
; leave zeroes in socktype and protocol.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov cl, [ebx+__gai_reqdata.socktype]
|
|
|
|
test cl, cl
|
|
|
|
jz .no_socktype
|
2009-10-06 13:52:00 +02:00
|
|
|
; 1b. Otherwise, set socktype and protocol to desired.
|
2012-07-11 14:08:45 +02:00
|
|
|
call .set_socktype
|
2009-10-06 13:52:00 +02:00
|
|
|
.no_socktype:
|
2012-07-11 14:08:45 +02:00
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
.has_service:
|
|
|
|
; 2. If TCP is allowed, append item for TCP.
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp [ebx+__gai_reqdata.socktype], 0
|
|
|
|
jz .tcp_ok
|
|
|
|
cmp [ebx+__gai_reqdata.socktype], SOCK_STREAM
|
|
|
|
jnz .tcp_disallowed
|
2009-10-06 13:52:00 +02:00
|
|
|
.tcp_ok:
|
2012-07-11 14:08:45 +02:00
|
|
|
call .append_item
|
|
|
|
mov cl, SOCK_STREAM
|
|
|
|
call .set_socktype
|
|
|
|
call .set_port
|
2009-10-06 13:52:00 +02:00
|
|
|
.tcp_disallowed:
|
|
|
|
; 3. If UDP is allowed, append item for UDP.
|
2012-07-11 14:08:45 +02:00
|
|
|
cmp [ebx+__gai_reqdata.socktype], 0
|
|
|
|
jz .udp_ok
|
|
|
|
cmp [ebx+__gai_reqdata.socktype], SOCK_DGRAM
|
|
|
|
jnz .udp_disallowed
|
2009-10-06 13:52:00 +02:00
|
|
|
.udp_ok:
|
2012-07-11 14:08:45 +02:00
|
|
|
call .append_item
|
|
|
|
mov cl, SOCK_DGRAM
|
|
|
|
call .set_socktype
|
|
|
|
call .set_port
|
2009-10-06 13:52:00 +02:00
|
|
|
.udp_disallowed:
|
2012-07-11 14:08:45 +02:00
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
.append_item:
|
|
|
|
; 1. Allocate memory for struct sockaddr_in and struct addrinfo.
|
2012-07-11 14:08:45 +02:00
|
|
|
push eax
|
|
|
|
push sizeof.addrinfo + sizeof.sockaddr_in
|
|
|
|
pop eax
|
|
|
|
call getaddrinfo._.memalloc
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2. Check for memory allocation fail.
|
2012-07-11 14:08:45 +02:00
|
|
|
test edi, edi
|
|
|
|
jz .no_memory
|
2009-10-06 13:52:00 +02:00
|
|
|
; 3. Zero allocated memory.
|
2012-07-11 14:08:45 +02:00
|
|
|
push (sizeof.addrinfo + sizeof.sockaddr_in) / 4
|
|
|
|
pop ecx
|
|
|
|
xor eax, eax
|
|
|
|
push edi
|
|
|
|
rep stosd
|
|
|
|
pop edi
|
2009-10-06 13:52:00 +02:00
|
|
|
; 4. Fill struct addrinfo.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, [ebx+__gai_reqdata.flags]
|
|
|
|
mov [edi+addrinfo.ai_flags], eax
|
|
|
|
mov byte [edi+addrinfo.ai_family], AF_INET4
|
|
|
|
mov byte [edi+addrinfo.ai_addrlen], sizeof.sockaddr_in
|
|
|
|
lea ecx, [edi+sizeof.addrinfo]
|
|
|
|
mov [edi+addrinfo.ai_addr], ecx
|
2009-10-06 13:52:00 +02:00
|
|
|
; 5. Fill struct sockaddr_in.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov byte [ecx+sockaddr_in.sin_family], AF_INET4
|
|
|
|
pop eax
|
|
|
|
mov [ecx+sockaddr_in.sin_addr], eax
|
2009-10-06 13:52:00 +02:00
|
|
|
; 6. Append new item to the list.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov [esi], edi
|
|
|
|
lea esi, [edi+addrinfo.ai_next]
|
2009-10-06 13:52:00 +02:00
|
|
|
; 7. Return.
|
2012-07-11 14:08:45 +02:00
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
.no_memory:
|
2012-07-11 14:08:45 +02:00
|
|
|
pop eax
|
|
|
|
xor esi, esi
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
.set_socktype:
|
2010-07-31 13:17:06 +02:00
|
|
|
; Set ai_socktype and ai_protocol fields by given socketnum type.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov byte [edi+addrinfo.ai_socktype], cl
|
|
|
|
dec cl
|
|
|
|
jnz .set_udp
|
2009-10-06 13:52:00 +02:00
|
|
|
.set_tcp:
|
2012-07-11 14:08:45 +02:00
|
|
|
mov byte [edi+addrinfo.ai_protocol], IPPROTO_TCP
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
.set_udp:
|
2012-07-11 14:08:45 +02:00
|
|
|
mov byte [edi+addrinfo.ai_protocol], IPPROTO_UDP
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
.set_port:
|
|
|
|
; Just copy port from input __gai_reqdata to output addrinfo.
|
2012-07-11 14:08:45 +02:00
|
|
|
push edx
|
|
|
|
mov edx, [ebx+__gai_reqdata.service]
|
|
|
|
xchg dl, dh ; convert to network byte order
|
|
|
|
mov [edi+sizeof.addrinfo+sockaddr_in.sin_port], dx
|
|
|
|
pop edx
|
|
|
|
ret
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; void __stdcall getaddrinfo_abort(__in struct __gai_reqdata* reqdata); ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
getaddrinfo_abort: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Abort process started by getaddrinfo_start, free all resources. ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> first parameter = pointer to struct __gai_reqdata filled by ..._start ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
; 0. Save used registers for __stdcall.
|
2012-07-11 14:08:45 +02:00
|
|
|
push ebx
|
2010-07-31 13:17:06 +02:00
|
|
|
; 1. Allocated resources: only socketnum, so close it and return.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, [esp+8]
|
|
|
|
mov ecx, [eax+__gai_reqdata.socketnum]
|
|
|
|
mcall 75, 1
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2. Restore used registers and return.
|
2012-07-11 14:08:45 +02:00
|
|
|
pop ebx
|
|
|
|
ret 4
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;; void __stdcall freeaddrinfo(__in struct addrinfo* ai); ;;
|
2012-07-11 14:08:45 +02:00
|
|
|
freeaddrinfo: ;;
|
2009-10-06 13:52:00 +02:00
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Free one or more addrinfo structures returned by getaddrinfo. ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> first parameter = head of list of structures ;;
|
|
|
|
; (may be arbitrary sublist of original) ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
; 1. Loop for all items in the list.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov edx, [esp+4] ; eax = ai
|
2009-10-06 13:52:00 +02:00
|
|
|
.loop:
|
2012-07-11 14:08:45 +02:00
|
|
|
test edx, edx
|
|
|
|
jz .done
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2. Free each item.
|
|
|
|
; 2a. Free ai_canonname, if allocated.
|
2012-07-11 14:08:45 +02:00
|
|
|
mov eax, [edx+addrinfo.ai_canonname]
|
|
|
|
test eax, eax
|
|
|
|
jz .no_canon_name
|
|
|
|
call getaddrinfo._.memfree
|
2009-10-06 13:52:00 +02:00
|
|
|
.no_canon_name:
|
|
|
|
; 2b. Remember next item
|
2009-10-07 22:36:58 +02:00
|
|
|
; (after freeing the field ai_next can became unavailable).
|
2012-07-11 14:08:45 +02:00
|
|
|
pushd [edx+addrinfo.ai_next]
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2c. Free item itself.
|
2012-07-11 14:08:45 +02:00
|
|
|
xchg eax, edx
|
|
|
|
call getaddrinfo._.memfree
|
2009-10-06 13:52:00 +02:00
|
|
|
; 2d. Restore pointer to next item and continue loop.
|
2012-07-11 14:08:45 +02:00
|
|
|
pop edx
|
|
|
|
jmp .loop
|
2009-10-06 13:52:00 +02:00
|
|
|
.done:
|
|
|
|
; 3. Done.
|
2012-07-11 14:08:45 +02:00
|
|
|
ret 4
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;;///////////////////////////////////////////////////////////////////////////;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
;! Exported functions section ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
;;///////////////////////////////////////////////////////////////////////////;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
|
|
|
@EXPORT:
|
2012-07-11 14:08:45 +02:00
|
|
|
export \
|
|
|
|
lib_init , 'lib_init' , \
|
|
|
|
0x00010001 , 'version' , \
|
|
|
|
inet_addr , 'inet_addr' , \
|
|
|
|
inet_ntoa , 'inet_ntoa' , \
|
|
|
|
getaddrinfo , 'getaddrinfo' , \
|
|
|
|
getaddrinfo_start , 'getaddrinfo_start' , \
|
|
|
|
getaddrinfo_process , 'getaddrinfo_process' , \
|
|
|
|
getaddrinfo_abort , 'getaddrinfo_abort' , \
|
|
|
|
freeaddrinfo , 'freeaddrinfo'
|
2009-10-06 13:52:00 +02:00
|
|
|
|
|
|
|
section '.data' data readable writable align 16
|
|
|
|
; uninitialized data
|
|
|
|
mem.alloc dd ?
|
|
|
|
mem.free dd ?
|
|
|
|
mem.realloc dd ?
|
|
|
|
dll.load dd ?
|
|
|
|
|
2012-07-11 14:08:45 +02:00
|
|
|
DNSrequestID dd ?
|
2009-10-06 13:52:00 +02:00
|
|
|
|
2012-07-11 14:08:45 +02:00
|
|
|
inet_ntoa.buffer rb 16 ; static buffer for inet_ntoa
|