Engineering Notes

Created by admin on Sat, 07/01/2012 - 15:56
Sub Topic: 
The get host by address function
Chapter Name: 
Network Programming and Management
Description: 
The get host by address function returns information about a host identified by address. The host information is returned in a structure of type hostent.
Content: 
<h2 align="center"> The get host by address function</h2> <h2> Description</h2> <p>The&nbsp;<strong>gethostbyaddr</strong>&nbsp;function returns information about a host identified by address. The host information is returned in a structure of type&nbsp;<strong>hostent</strong>. This structure is allocated by the WinSock2 library, and your program should not attempt to modify or de-allocate, the structure or any of its components.</p> <p>Only one copy of the&nbsp;<strong>hostent</strong>&nbsp;structure is allocated per calling thread, and since Irie Pascal programs only have one thread, there will be only one copy of this structure allocated for your program. As a result, you should copy any host information you need from the structure before calling any other Windows Sockets functions.</p> <h2> Declaration</h2> <p>The system include file&nbsp;<strong>WinSock2.inc</strong>&nbsp;contains the following declaration for the&nbsp;<strong>hostent</strong>&nbsp;and&nbsp;<strong>p_hostent</strong>&nbsp;types and the<strong>gethostbyaddr</strong>&nbsp;function:</p> <pre> <code><strong>&nbsp;hostent = packed record</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;h_name : address;</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;h_aliases : address;</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;h_addrtype : shortint;</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;h_length : shortint;</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;h_addr_list : address;</span></strong></code></pre> <pre> <code><strong>&nbsp;end;</span></strong></code></pre> <pre> &nbsp;</pre> <pre> <code><strong>&nbsp;p_hostent = ^hostent;</span></strong></code></pre> <pre> &nbsp;</pre> <pre> <code><strong>&nbsp;function gethostbyaddr(addr : address; len, typ : integer) : address;</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;external dll='ws2_32.dll';</span></strong></code></pre> <h2> Arguments</h2> <h3> The First Argument</h3> <p>The first argument passed to the&nbsp;<strong>gethostbyaddr</strong>&nbsp;function is the operating system address of the buffer containing the address of the host you want to get information about. The address stored in the buffer is not stored as a string, it is stored as a numeric value in in network byte order. The type of this argument is the built-in type&nbsp;<a href="http://www.irietools.com/iriepascal/progref104.html">address</a>, so you must use the built-in function&nbsp;<a href="http://www.irietools.com/iriepascal/progref166.html">addr</a>&nbsp;to get the operating system address of the buffer.</p> <p>If you have the address of a host, as a null-terminated string, and you want to get information about that host then you should do the following:</p> <ol> <li> Use the&nbsp;<a href="http://www.irietools.com/iriepascal/progref400.html">inet_addr</a>&nbsp;function to convert the address, represented as a string into an address, represented in a numeric format.</li> <li> Use the&nbsp;<a href="http://www.irietools.com/iriepascal/progref391.html">gethostbyaddr</a>&nbsp;function to get the host information.</li> </ol> <h3> The Second Argument</h3> <p>The second argument passed to the&nbsp;<strong>gethostbyaddr</strong>&nbsp;function is the length of the host address in the buffer pointed to be the first argument.</p> <h3> The Third Argument</h3> <p>The third argument passed to the&nbsp;<strong>gethostbyaddr</strong>&nbsp;function is the type of the host address in the buffer pointed to be the first argument.</p> <h2> Return Values</h2> <p>The&nbsp;<strong>gethostbyaddr</strong>&nbsp;function returns a pointer to the&nbsp;<strong>hostent</strong>&nbsp;structure that contains the host information, if the call is successful. If the call fails then the value&nbsp;<a href="http://www.irietools.com/iriepascal/progref054.html">nil</a>&nbsp;is returned, and in this case you can use the&nbsp;<a href="http://www.irietools.com/iriepascal/progref376.html">WSAGetLastError</a>&nbsp;function to retrieve a code that identifies the error that caused the call to fail.</p> <h2> Example</h2> <p>The following procedure was taken from one of the sample programs distributed with Irie Pascal and shows one way to use the&nbsp;<strong>gethostbyaddr</strong>&nbsp;function.</p> <pre> <code><strong>&nbsp;//PUPOSE: Given the IP address of a host perform a reverse DNS lookup.</span></strong></code></pre> <pre> <code><strong>&nbsp;//&nbsp;&nbsp;&nbsp; 1. strIPAddress - contains the name of the host</span></strong></code></pre> <pre> <code><strong>&nbsp;procedure ReverseDNSLookUp(strIPAddress : cstring);</span></strong></code></pre> <pre> <code><strong>&nbsp;var</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;IPAddress : in_addr;</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;pHostEnt : p_hostent;</span></strong></code></pre> <pre> <code><strong>&nbsp;begin</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;IPAddress := inet_addr(addr(strIPAddress));</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;if IPAddress = INADDR_NONE then</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;&nbsp;writeln('Invalid IP address')</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;else</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;&nbsp;begin</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;&nbsp;&nbsp;pHostEnt := gethostbyaddr(addr(IPAddress), sizeof(IPAddress), AF_INET);</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;&nbsp;&nbsp;if pHostEnt = nil then</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeln('Can not find address ''', hex(IPAddress), '''')</span></strong></code></pre> <pre> &nbsp;&nbsp;&nbsp;&nbsp;<code><strong>else</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DisplayHostEntInfo(pHostEnt);</span></strong></code></pre> <pre> <code><strong>&nbsp;&nbsp;&nbsp;end;</span></strong></code></pre> <pre> <code><strong>&nbsp;end;</span></strong></code></pre> <p>The procedure&nbsp;<strong>DisplayHostEntInfo</strong>&nbsp;is defined by the sample program and displays the host information stored in the<strong>hostent</strong>&nbsp;structure. See the&nbsp;<a href="http://www.irietools.com/iriepascal/progref390.html">gethostbyname</a>&nbsp;function for the actual&nbsp;<strong>DisplayHostEntInfo</strong>&nbsp;procedure.</p> <h2> Reference Information</h2> <p>The authoritative source of information about the WinSock2 library is the Microsoft Developers Network (MSDN). You can access the MSDN on the Microsoft website at&nbsp;<a href="http://msdn.microsoft.com/">msdn.microsoft.com</a>.</p> <p>&nbsp;</p>
engnotes_star_rating: 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.