network library, version 1: inet_addr/ntoa, getaddrinfo/freeaddrinfo

git-svn-id: svn://kolibrios.org@1155 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
CleverMouse 2009-09-09 11:46:09 +00:00
parent 257b31bb6d
commit 178bc5ad6f
4 changed files with 1605 additions and 0 deletions

View File

@ -0,0 +1,124 @@
proc dll.Load, import_table:dword
mov esi,[import_table]
.next_lib: mov edx,[esi]
or edx,edx
jz .exit
push esi
mov esi,[esi+4]
mov edi,s_libdir.fname
@@: lodsb
stosb
or al,al
jnz @b
mcall 68,19,s_libdir
or eax,eax
jz .fail
stdcall dll.Link,eax,edx
push eax
mov eax, [eax]
cmp dword [eax], 'lib_'
pop eax
jnz @f
stdcall dll.Init,[eax+4]
@@:
pop esi
add esi,8
jmp .next_lib
.exit: xor eax,eax
ret
.fail: add esp,4
xor eax,eax
inc eax
ret
endp
proc dll.Link, exp:dword,imp:dword
push eax
mov esi,[imp]
test esi,esi
jz .done
.next: lodsd
test eax,eax
jz .done
stdcall dll.GetProcAddress,[exp],eax
or eax,eax
jz @f
mov [esi-4],eax
jmp .next
@@: mov dword[esp],0
.done: pop eax
ret
endp
proc dll.Init, dllentry:dword
pushad
mov eax,mem.Alloc
mov ebx,mem.Free
mov ecx,mem.ReAlloc
mov edx,dll.Load
stdcall [dllentry]
popad
ret
endp
proc dll.GetProcAddress, exp:dword,sz_name:dword
mov edx,[exp]
xor eax,eax
.next: or edx,edx
jz .end
cmp dword[edx],0
jz .end
stdcall strcmp,[edx],[sz_name]
test eax,eax
jz .ok
add edx,8
jmp .next
.ok: mov eax,[edx+4]
.end: ret
endp
proc strcmp, str1:dword,str2:dword
push esi edi
mov esi,[str1]
mov edi,[str2]
xor eax,eax
@@: lodsb
scasb
jne .fail
or al,al
jnz @b
jmp .ok
.fail: or eax,-1
.ok: pop edi esi
ret
endp
; void* __stdcall mem.Alloc(unsigned size);
mem.Alloc:
push ebx ecx
mov ecx, [esp+12]
mcall 68, 12
pop ecx ebx
ret 4
; void* __stdcall mem.ReAlloc(void* mptr, unsigned size);
mem.ReAlloc:
push ebx ecx edx
mov edx, [esp+16]
mov ecx, [esp+20]
mcall 68, 20
pop edx ecx ebx
ret 8
; void __stdcall mem.Free(void* mptr);
mem.Free:
push ebx ecx
mov ecx, [esp+12]
mcall 68, 13
pop ecx ebx
ret 4
s_libdir:
db '/sys/lib/'
.fname rb 32

View File

@ -0,0 +1,134 @@
use32
; standard header
db 'MENUET01' ; signature
dd 1 ; header version
dd start ; entry point
dd i_end ; initialized size
dd mem ; required memory
dd mem ; stack pointer
dd 0 ; parameters
dd 0 ; path
; useful includes
include '../../../../macros.inc'
purge mov,add,sub
include '../../../../proc32.inc'
include 'dll.inc'
include '../network.inc'
; entry point
start:
; load libraries
stdcall dll.Load, @IMPORT
test eax, eax
jnz exit
; initialize console
push 1
call [con_start]
push title
push -1
push -1
push -1
push -1
call [con_init]
; main loop
main:
; write prompt
push str1
call [con_write_asciiz]
; read string
mov esi, s
push 256
push esi
call [con_gets]
; check for exit
test eax, eax
jz done
cmp byte [esi], 10
jz done
; delete terminating '\n'
push esi
@@:
lodsb
test al, al
jnz @b
mov byte [esi-2], al
pop esi
; resolve name
push esp ; reserve stack place
push esp ; fourth parameter
push 0 ; third parameter
push 0 ; second parameter
push esi ; first parameter
call [getaddrinfo]
pop esi
; test for error
test eax, eax
jnz fail
; write results
push str2
call [con_write_asciiz]
mov edi, esi
addrloop:
; before all subsequent addresses print comma
cmp edi, esi
jz @f
push str3
call [con_write_asciiz]
@@:
; convert IP address to decimal notation
mov eax, [edi+addrinfo.ai_addr]
pushd [eax+sockaddr_in.sin_addr]
call [inet_ntoa]
; write result
push eax
call [con_write_asciiz]
; advance to next item
mov edi, [edi+addrinfo.ai_next]
test edi, edi
jnz addrloop
; free allocated memory
push esi
call [freeaddrinfo]
; write newline and continue main loop
push str4
@@:
call [con_write_asciiz]
jmp main
fail:
push str5
jmp @b
done:
push 1
call [con_exit]
exit:
mcall -1
; data
title db 'Names resolver',0
str1 db 'Host name to resolve: ',0
str2 db 'IP address(es): ',0
str3 db ', ',0
str4 db 10,0
str5 db 'Name resolution failed.',10,0
; import
align 4
@IMPORT:
library network, 'network.obj', console, 'console.obj'
import network, \
getaddrinfo, 'getaddrinfo', \
freeaddrinfo, 'freeaddrinfo', \
inet_ntoa, 'inet_ntoa'
import console, \
con_start, 'START', \
con_init, 'con_init', \
con_write_asciiz, 'con_write_asciiz', \
con_exit, 'con_exit', \
con_gets, 'con_gets'
i_end:
s rb 256
align 4
rb 4096 ; stack
mem:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,61 @@
; Socket types
SOCK_STREAM = 1
SOCK_DGRAM = 2
SOCK_RAW = 3 ; not supported by the kernel
; IP protocols
IPPROTO_IP = 0
IPPROTO_ICMP = 1 ; not supported by the kernel
IPPROTO_TCP = 6
IPPROTO_UDP = 17
; Address families
AF_UNSPEC = 0
AF_INET = 2 ; IPv4
;AF_INET6 = 28 ; IPv6 (not supported)
PF_UNSPEC = AF_UNSPEC
PF_INET = AF_INET
;PF_INET6 = AF_INET6
; Flags for addrinfo
AI_PASSIVE = 1
AI_CANONNAME = 2
AI_NUMERICHOST = 4
AI_NUMERICSERV = 8
AI_ADDRCONFIG = 0x400
; internal definition
AI_SUPPORTED = 0x40F
struct sockaddr_in
sin_len db ? ; uint8_t
sin_family db ? ; sa_family_t
sin_port dw ? ; in_port_t
sin_addr dd ? ; struct in_addr
sin_zero rb 8 ; zero
ends
struct addrinfo
ai_flags dd ? ; bitmask of AI_*
ai_family dd ? ; PF_*
ai_socktype dd ? ; SOCK_*
ai_protocol dd ? ; 0 or IPPROTO_*
ai_addrlen dd ? ; length of ai_addr
ai_canonname dd ? ; char*
ai_addr dd ? ; struct sockaddr*
ai_next dd ? ; struct addrinfo*
ends
EAI_ADDRFAMILY = 1
EAI_AGAIN = 2
EAI_BADFLAGS = 3
EAI_FAIL = 4
EAI_FAMILY = 5
EAI_MEMORY = 6
EAI_NONAME = 8
EAI_SERVICE = 9
EAI_SOCKTYPE = 10
EAI_BADHINTS = 12
EAI_PROTOCOL = 13
EAI_OVERFLOW = 14