May 09, 2024, 10:29:44 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Winsock2 - in_addr confusion

Started by rickmcneely, December 27, 2008, 12:18:36 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

rickmcneely

Hello all.

I'm using Sapero's excellent winsock2.inc in a program I'm writing just as an exercise in understanding winsock.  I have come to a roadblock in figuring out how to retrieve information from the in_addr structure.  I understand that gethostbyname and gethostbyaddr return information in a hostent structure, and that part of the hostent structure (h_addr_list) uses an in_addr structure to store addresses.  Does anyone have an example showing the contents of this structure (in_addr) dereferenced?

Using the following code I can copy the hostent info returned by gethostbyname into my own hostent structure.  From there I can print the h_name field and force out the first entry in the h_aliases field, although I don't think I'm getting the aliases appropriately.


if ptrHost <> 0 ' if Host Pointer references a valid structure (from gethostbyname)

' set local udt "myHost" = to winsock udt "hostent"
myHost.h_name      = #<hostent>ptrHost.h_name ' official name of host
myHost.h_aliases   = #<hostent>ptrHost.h_aliases ' alias list
myHost.h_addrtype  = #<hostent>ptrHost.h_addrtype ' host address type
myHost.h_length    = #<hostent>ptrHost.h_length ' length of address
' may be 4 or 6.  (IPV4) or (IPV6)
myHost.h_addr_list = #<hostent>ptrHost.h_addr_list ' list of addresses
' h_addr_list is an in_addr  structure representing IPv4 Internet addresses.
' or
' h_addr_list is an in6_addr structure representing IPv6 Internet addresses.
myHost.h_addr      = #<hostent>ptrHost.h_addr ' address, for backward compatibility

PRINT
PRINT

pointer xyz = myHost.h_name

if xyz <> null
print "h_name      " + #<string>xyz ' returns lb1.www.ms.akadns.net for gethostbyname(www.microsoft.com)
ENDIF

xyz = myHost.h_aliases ' returns www.microsoft.com for gethostbyname(www.microsoft.com)
xyz = #<pointer>xyz
if xyz <> null
print "h_aliases   " + #<string>xyz
ENDIF

xyz = myHost.h_length
if xyz <> null

print "h_length   " + STR$(#<int>xyz) ' returns 2079326212 for gethostbyname(www.microsoft.com)
ENDIF

xyz = myHost.h_addr_list
if xyz <> null

print "h_addr_list " + #<string>xyz
ENDIF

xyz = myHost.h_addr
if xyz <> null
print "h_addr      " + #<string>xyz
ENDIF

endIF


It (hostent\in_addr) combine three things that severely confuse me:  Pointers to Pointers, dereferencing and unions.  None are too bad by themselves but combine them and my head hurts.


Ionic Wind Support Team

With the NCS library this is easier ;)

Anyway I normally get the IP address like this:

pointer AddrPtr = #<HOSTENT>HostPtr.#<POINTER>h_addr_list
IP = #<ULONG>AddrPtr[Index]

Where Index is normally 0.

Which gives an idea how to do multiple dereferencing.

Paul.
Ionic Wind Support Team

sapero

December 27, 2008, 01:33:56 PM #2 Last Edit: December 27, 2008, 01:39:01 PM by sapero
Here's a short example how to use h_name and h_addr_list:
$include "windowssdk.inc"

WSADATA wsd
WSAstartup(0x202, &wsd)

pointer p 'h30097.www3.hp.com
pointer pHost = gethostbyname("osdir.com")
settype pHost, hostent

if (pHost)
print "h_name: ", *pHost.*<string>h_name

print "h_aliases:"
p = *pHost.h_aliases
while (*<pointer>p)
' *<pointer>p = string address
print " - ", *<string>*<pointer>p
p += 4
endwhile

print "h_addr_list:"
p = *pHost.h_addr_list
while (*<pointer>p)
' *<pointer>p = address of 4 bytes array
print " - ", ToString(inet_ntoa(*<int>*<pointer>p))
p += 4
endwhile
endif

WSACleanup()
_system("pause")

sub ToString(pointer p),string
return *<string>p
endsub

rickmcneely

Thanks very much guys.  Paul, I do have the NCS library, just trying to dig a little deeper for my own edification. 

Sapero, that's exactly what I was looking for.  Thanks much!