Example: Use gethostbyaddr_r() for threadsafe network routines

Here is an example of a program that uses gethostbyaddr_r().

All other routines with names that end in "_r" have similar semantics and are also threadsafe. This example program takes an IP address in the dotted-decimal notation and prints the host name.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
/********************************************************/
/* Header files                                         */
/********************************************************/
#include </netdb.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define HEX00 '\x00'
#define NUMPARMS 2
/********************************************************/
/* Pass one parameter that is the IP address in         */
/* dotted decimal notation. The host name will be       */
/* displayed if found; otherwise, a message states      */
/* host not found.                                      */
/********************************************************/
int main(int argc, char *argv[])
    {
    int rc;
    struct in_addr internet_address;
    struct hostent hst_ent;
    struct hostent_data  hst_ent_data;
    char dotted_decimal_address [16];
    char host_name[MAXHOSTNAMELEN];

    /**********************************************************/
    /* Verify correct number of arguments have been passed    */
    /**********************************************************/
    if (argc != NUMPARMS)
     {
      printf("Wrong number of parms passed\n");
      exit(-1);
     }
    /**********************************************************/
    /* Obtain addressability to parameters passed             */
    /**********************************************************/
    strcpy(dotted_decimal_address, argv[1]);

    /**********************************************************/
    /* Initialize the structure-field                         */
    /* hostent_data.host_control_blk with hexadecimal zeros   */
    /* before its initial use. If you require compatibility   */
    /* with other platforms, then you must initialize the     */
    /* entire hostent_data structure with hexadecimal zeros.  */
    /**********************************************************/
    /* Initialize to hex 00 hostent_data structure            */
    /**********************************************************/
    memset(&hst_ent_data,HEX00,sizeof(struct hostent_data));

    /**********************************************************/
    /* Translate an IP address from dotted decimal      */
    /* notation to 32-bit IP address format.                  */
    /**********************************************************/
    internet_address.s_addr=inet_addr(dotted_decimal_address);

    /**********************************************************/
    /* Obtain host name                                       */
    /**********************************************************/
    /**********************************************************/
    /* NOTE: The gethostbyaddr_r() returns an integer.        */
    /* The following are possible values:                     */
    /*   -1 (unsuccessful call)                               */
    /*   0 (successful call)                                  */
    /**********************************************************/
    rc=gethostbyaddr_r((char *) &internet_address,
                        sizeof(struct in_addr), AF_INET,
                        &hst_ent, &hst_ent_data);

    if (rc== -1)
{
         printf("Host name not found\n");
         exit(-1);
       }
    else
       {
        /*****************************************************/
        /*   Copy the host name to an output buffer          */
        /*****************************************************/
          (void) memcpy((void *) host_name,
        /****************************************************/
        /* You must address all the results through the     */
        /* hostent structure hst_ent.                       */
        /* NOTE: Hostent_data structure hst_ent_data is just */
        /* a data repository that is used to support the    */
        /* hostent structure. Applications should consider  */
        /* hostent_data a storage area to put host level data */
        /* that the application does not need to access.    */
        /****************************************************/
                        (void *) hst_ent.h_name,
                        MAXHOSTNAMELEN);
        /*****************************************************/
        /* Print the host name                               */
        /*****************************************************/
          printf("The host name is %s\n", host_name);

         }
       exit(0);
     }
Related concepts
Thread safety
Related reference
Socket network functions
Related information
gethostbyaddr_r()