
  % filename  "t.exe"
  includepath "$/inc/"
 'include "RTL64.inc"
  include "console.inc"

/* Get IP address from domain name */

% SOCKET_ERROR          = -1
% SOCK_STREAM           = 1
% AF_INET               = 2 
% AF_NETBIOS	        = 17
% INVALID_SOCKET        = 0xFFFFFFFF
% MSG_WAITALL           = 0x8

typedef sys WSOCKET


type WSADATA 'Requires Windows Sockets 2.0
  wVersion       as word          'WORD ( = 0x0202)                      
  wHighVersion   as word          'WORD ( = 0x0202)                      
  szDescription  as zString * 257 'char [WSADESCRIPTION_LEN+1] 
  szSystemStatus as zString * 129 'char [WSASYS_STATUS_LEN+1]  
  iMaxSockets    as short         'unsigned short
  iMaxUdpDg      as short         'unsigned short
  lpVendorInfo   as sys           'char far*
end type

'#recordof WSADATA

/*
typedef struct in_addr {
  union {
    struct {
      u_char s_b1,s_b2,s_b3,s_b4;
    } S_un_b;
    struct {
      u_short s_w1,s_w2;
    } S_un_w;
    u_long S_addr;
  } S_un;
} IN_ADDR, *PIN_ADDR, *LPIN_ADDR;
*/

'type in_addr 
  union in_addr 's_un
  {
    type s_un_b 
    {
      byte s_b1
      byte s_b2
      byte s_b3
      byte s_b4
    }
    type s_un_w 
    {
      short s_w1
      short s_w2
    }
    long s_addr
  }
'end type

struct sockaddr {
    short    sa_family    // address family, AF_xxx
    char       sa_data[14]  // 14 bytes of protocol address}
}


// IPv4 AF_INET sockets:
type sockaddr_in 
    sin_family as short   // e.g. AF_INET, AF_INET6
    sin_port as short     // e.g. htons(3490)
    sin_addr as in_addr   // see struct in_addr, below
    sin_zero[8] as char   // zero this if you want to
end type

/*
typedef struct hostent {
  char FAR      *h_name;
  char FAR  FAR **h_aliases;
  short         h_addrtype;
  short         h_length;
  char FAR  FAR **h_addr_list;
} HOSTENT, *PHOSTENT, FAR *LPHOSTENT;
*/

'type hostent 
'   char    *h_name
'   sys     *h_aliases
'   short   h_addrtype
'   short   h_length
'   in_addr *h_addr_list 'list of in_addr (s_addr is needed)
'end type

type hostent 
   char   *h_name
   sys     h_aliases   'char**
   short   h_addrtype
   short   h_length
   sys     h_addr_list 'char**
end type

'print "sizeof(hostent): " sizeof(hostent) & cr & cr

'SELECTED PROCEDURES
====================

extern lib "ws2_32.dll" 

! WSAStartup (word wVersionRequested, WSADATA *lpWSAData) as int
! WSAGetLastError () as int
! socket(int af, int type, int protocol) as WSOCKET
! closesocket(WSOCKET s) as int
! WSACleanup () as int
! inet_addr (char *cp) as u_long 'cp: NULL-terminated character string representing a number expressed in the Internet standard ".'' (dotted) notation.
! htons(short hostshort) as short 'returns the value in TCP/IP network byte order
! connect(WSOCKET hSocket, sys *Name, int namelen) as int 'returns zero if no error
! send(WSOCKET s, char *buf, int len, int flags) as int
! recv(WSOCKET s, char *buf, int len, int flags) as int
! gethostbyname(char *name) as sys ' ptr to struct hostent
! inet_ntoa(in_addr in) as char ptr


end extern

 
sub main()


WSADATA wsa 

'string host_name = "www.google.co.uk" '216.58.209.100
'string host_name = "www.google.com" '216.58.209.100
'string host_name = "google.com" '173.194.112.228
'string host_name = "www.oxygenbasic.org" '12.158.189.153
string host_name = "www.dict.org" '216.18.20.172

sys he 'hostent


print cr & "Initialising Winsock..." & cr    
if WSAStartup(0x0202, wsa) != 0 then    
   print "Failed. Error Code : " WSAGetLastError() & cr        
   return 1    
end if         

print "Initialised." & cr  

    print "Calling gethostbyname with "  host_name & cr
    he = gethostbyname( host_name )
' printl "he: " he & cr
    if  he = NULL then    
       //gethostbyname failed        
       print "gethostbyname failed Error: "  WSAGetLastError() & cr        
       exit sub  
    end if

    hostent remoteHost at he
    print "Function returned:" & cr
    print chr(9) & "Official name: "  remoteHost.h_name & cr

    print chr(9) & "Address type: "
        select remoteHost.h_addrtype
        case AF_INET:
            print "AF_INET" & cr
        case AF_NETBIOS:
            print "AF_NETBIOS" & cr
        case else
            print remoteHost.h_addrtype & cr
        end select
        
    print chr(9) & "Address length: " remoteHost.h_length & cr

    if remoteHost.h_addrtype = AF_INET then
    
    sys *aa = remoteHost.h_aliases
    sys *ad = remoteHost.h_addr_list
    sys x
    do
      if ad then
        x++
        print x ": " inet_ntoa(byval *ad) & "  " (char)*aa  & cr
        @aa+=sizeof sys
        @ad+=sizeof sys
      else
        exit do
      end if
    end do


    end if       
 
end sub

main()

printl "enter ..." :waitkey()