I have a mental block. I am having trouble
implementing what appears to be a simple straight
forward piece of basic functionality.
I need to access the "hostent" structure returned by
a call to "GetHostByName". This structure contains relevent IP address info about the host named in the
call.
NOTE: The structure is owned by the OS, so we cannot "create" or "free" it. The GetHostByName
system call (defined in Winsock.ppl) returns a
pointer to the structure. We need to be able to
access the data at the pointer as a "hostent"
structure.
I have included some code as an example of what I
require. The code block also includes (as comments)
the relevent API definitions from MSDN.
Any guidance would be much appreciated. I am sure the solution is so "simple"
implementing what appears to be a simple straight
forward piece of basic functionality.
I need to access the "hostent" structure returned by
a call to "GetHostByName". This structure contains relevent IP address info about the host named in the
call.
NOTE: The structure is owned by the OS, so we cannot "create" or "free" it. The GetHostByName
system call (defined in Winsock.ppl) returns a
pointer to the structure. We need to be able to
access the data at the pointer as a "hostent"
structure.
I have included some code as an example of what I
require. The code block also includes (as comments)
the relevent API definitions from MSDN.
- Code:
//------------------------------------------------------------------------------
//This function gets host data corresponding to a host name.
//
//struct hostent *gethostbyname(const char *name );
//Parameters
//
//name
// [out] Pointer to the null-terminated string that contains the host name to resolve.
//
//Return Values
//
//A pointer to the HOSTENT structure indicates no error occurred. A NULL pointer
//indicates failure. To get a specific error value, call WSAGetLastError.
//
//Remarks
//
//The gethostbyname function returns a pointer to a HOSTENT structure — a structure
//allocated by Windows Sockets. The HOSTENT structure contains the results of a
//successful search for the host specified in the name parameter.
//
//The application must never attempt to modify this structure, or to free any of its
//components. Furthermore, only one copy of this structure is allocated per thread,
//so the application should copy data it needs before issuing other Windows Sockets
//function calls.
//
//The gethostbyname function cannot resolve IP address strings passed to it. Such a
//request is treated exactly as if an unknown host name were passed. Use inet_addr
//to convert an IP address string the string to an actual IP address, then use another
//function, gethostbyaddr, to obtain the contents of the HOSTENT structure
//------------------------------------------------------------------------------
//This structure is allocated by Windows Sockets. An application should never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, and so the application should copy any information that it needs before issuing any other Windows Sockets API calls.
//
//struct hostent {
// char FAR *h_name;
// char FAR *FAR *h_aliases;
// short h_addrtype;
// short h_length;
// char FAR *FAR *h_addr_list;
// };
//
//Members
//
//h_name
// Official name of the host (PC).If using the DNS or similar resolution system, it is the Fully Qualified Domain Name (FQDN) that caused the server to return a reply. If using a local "hosts" file, it is the first entry after the IP address.
//h_aliases
// Null-terminated array of alternate names.
//h_addrtype
// Type of address being returned.
//h_length
// Length, in bytes, of each address.
//h_addr_list
// Null-terminated list of addresses for the host. Addresses are returned in network byte order. The macro h_addr is defined to be h_addr_list[0] for compatibility with older software.
//------------------------------------------------------------------------------
#define hostent {"h_name","h_aliases","h_addrtype",tshort,"h_length",tshort,"h_addr_list"}
proc ShowHostInfo(hn$)
local (MyHostEnt$);
MyHostEnt$ = GetHostByName(hn$);
// MyHostEnt$ is in fact the address of a hostent struct returned by the OS.
// Question : How do we access this as structured data? In teh first instance
// we might want to print the fully qualified domain name returned as the
// h_name entry in the hostent structure.
if (NOT(MyHostEnt$==NULL))
print (concat("Hostname : ",@MyHostEnt.h_name$));
else
print ("HostByName returned a NULL result");
end;
end;
Any guidance would be much appreciated. I am sure the solution is so "simple"