News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how to get remote host name and ip

Started by ossama, December 05, 2007, 03:56:10 PM

Previous topic - Next topic

ossama

hello,
how can i get the the remote host name and ip address of a connected socket?
i have written this code, but it returns wrong results:

to get remote host name, i used this code


get_remote_host_name proc SocketID:dword,pszBuffer:dword
local sock_addr:sockaddr_in
local _size:dword

;get remote host address
mov _size,sizeof sockaddr_in
invoke getpeername,SocketID,addr sock_addr,addr _size
.if eax==SOCKET_ERROR
return FALSE
.endif

;get host name from ip address
invoke gethostbyaddr,addr sock_addr.sin_addr,4,AF_INET
.if eax==NULL
return FALSE
.endif
lea eax,(hostent ptr [eax]).h_name
invoke lstrcpy,pszBuffer,eax

return TRUE
get_remote_host_name endp



and to get remote ip address, i  used this:



get_remote_ip_address proc SocketID:dword,pszBuffer:dword
local sock_addr:sockaddr_in
local _size:dword

;get remote host address
mov _size,sizeof sockaddr_in
invoke getpeername,SocketID,addr sock_addr,addr _size
.if eax==SOCKET_ERROR
return FALSE
.endif

;convert ip address (network byte) to standard dotted format
invoke inet_ntoa,addr sock_addr.sin_addr
.if eax==NULL
return FALSE
.endif
invoke lstrcpy,pszBuffer,eax

return TRUE
get_remote_ip_address endp



thank you in advance for your help and time

ossama

i did not find prototyping of the function getnameinfo in masm32 package

Vortex

Quote from: ossama on December 05, 2007, 04:22:32 PM
i did not find prototyping of the function getnameinfo in masm32 package

http://msdn2.microsoft.com/en-us/library/ms738532.aspx

Yes, the prototype is not defined in ws2_32.inc

ossama

vortex,
QuoteYes, the prototype is not defined in ws2_32.inc
what should i do? do i need to use LoadLibrary and GetProcAddress ?

Vortex

GetProcAddress can be a solution. The attachment contains Ws2_32.lib from PellesC development package and the associated include file extracted from the import library with lib2inc :

getnameinfo PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
[attachment deleted by admin]

ossama

any way, i had used the function getnameinfo, but no/or wrong results.

ossama

is there an updated masm32 package that includes all functions prototyping and constants (found in msdn)

ossama

Quote from: ossama on December 05, 2007, 03:56:10 PM
hello,
how can i get the the remote host name and ip address of a connected socket?
i have written this code, but it returns wrong results:

to get remote host name, i used this code


get_remote_host_name proc SocketID:dword,pszBuffer:dword
local sock_addr:sockaddr_in
local _size:dword

;get remote host address
mov _size,sizeof sockaddr_in
invoke getpeername,SocketID,addr sock_addr,addr _size
.if eax==SOCKET_ERROR
return FALSE
.endif

;get host name from ip address
invoke gethostbyaddr,addr sock_addr.sin_addr,4,AF_INET
.if eax==NULL
return FALSE
.endif
lea eax,(hostent ptr [eax]).h_name
invoke lstrcpy,pszBuffer,eax

return TRUE
get_remote_host_name endp



and to get remote ip address, i  used this:



get_remote_ip_address proc SocketID:dword,pszBuffer:dword
local sock_addr:sockaddr_in
local _size:dword

;get remote host address
mov _size,sizeof sockaddr_in
invoke getpeername,SocketID,addr sock_addr,addr _size
.if eax==SOCKET_ERROR
return FALSE
.endif

;convert ip address (network byte) to standard dotted format
invoke inet_ntoa,addr sock_addr.sin_addr
.if eax==NULL
return FALSE
.endif
invoke lstrcpy,pszBuffer,eax

return TRUE
get_remote_ip_address endp



thank you in advance for your help and time

when i use the second procedure (get_remote_ip_address) with my socket is connected to a server in my pc (the two ends of the socket are in the same pc),but i get this ip address:
172.247.18.0
it is not my ip address,
i am behind a router, the router gived me the ip address 192.168.30.11,
and my internet connection ip (my isp assigned it to me) is 41.209.133.29,
and even if i disconnect the LAN cable from my pc, i get the same ip address 172.247.18.0

ossama

i have found my error in the first procedure, it it the line:
   lea eax,(hostent ptr [eax]).h_name
change it to the folowing resolve the remote host name
   mov eax,(hostent ptr [eax]).h_name
and the right procedure will be :
get_remote_host_name proc SocketID:dword,pszBuffer:dword
local sock_addr:sockaddr_in
local _size:dword

;get remote host address
mov _size,sizeof sockaddr_in
invoke getpeername,SocketID,addr sock_addr,addr _size
.if eax==SOCKET_ERROR
return FALSE
.endif

;get host name from ip address
invoke gethostbyaddr,addr sock_addr.sin_addr,4,AF_INET
.if eax==NULL
return FALSE
.endif
mov eax,(hostent ptr [eax]).h_name
invoke lstrcpy,pszBuffer,eax

return TRUE
get_remote_host_name endp


but i still have the wrong ip address returned from the second proc: get_remote_ip_address

ossama

finaly, i got the solution, it was masm confusing me,
first of all this is the right procedure to get remote ip address of a connected socket

get_remote_ip_address proc SocketID:dword,pszBuffer:dword
local sock_addr:sockaddr_in
local _size:dword

;get remote host address
mov _size,sizeof sockaddr_in
invoke getpeername,SocketID,addr sock_addr,addr _size
.if eax==SOCKET_ERROR
return FALSE
.endif

;convert ip address (network byte) to standard dotted format
                mov eax,sock_addr.sin_addr
invoke inet_ntoa,eax
.if eax==NULL
return FALSE
.endif
invoke lstrcpy,pszBuffer,eax

return TRUE
get_remote_ip_address endp


the change of the line
invoke inet_ntoa,addr sock_addr.sin_addr
to this
mov eax,sock_addr.sin_addr
   invoke inet_ntoa,eax

solved the problem

but what was confusing me is that masm was telling me this:
error A2114: INVOKE argument type mismatch : argument : 1

when i write this :
invoke inet_ntoa,addr sock_addr.sin_addr

and after changing it to
mov eax,sock_addr.sin_addr
invoke inet_ntoa,eax

it was OKAY :U

ossama

i forgot some thing,
i thank ICZELION for his very excelent tutorial of winsock
http://win32assembly.online.fr/asmsockguide.html
:U