; IP lookup.
; Usage example:
;	mygetip google.com

;
; to build:
; $ fasm mygetip.asm
; $ gcc mygetip.o -o mygetip
;
; Only tested on Linux (Ubuntu -- Hardy Heron)
;

	format ELF

	section '.text' executable

public main
extrn printf
extrn gethostbyname
extrn inet_ntoa

main:
	enter 4, 0
	cmp dword [ebp+8], 2
	jne .missing_args_err
	
	mov eax, [ebp+12]
	push dword [eax+4]
	call gethostbyname
	add esp, 4

	cmp eax, 0
	jz .gethostbyname_err

	push dword [eax]
	mov eax, [eax+16]
	mov eax, [eax]
	push dword [eax] ; h_addr
	call inet_ntoa
	add esp, 4
	push eax
	push host_ip_msg
	call printf
	add esp, 8

	xor eax, eax
	leave
	ret
 
.missing_args_err:
	push missing_args_err_msg
	call printf
.gethostbyname_err:
	leave
	ret

;-------------------------------------------------------------------------
	section '.data' writeable

host_ip_msg db "IP: %s Host: %s",10,0
missing_args_err_msg db "usage: getip address",10,0


