Chapter Contents

Previous

Next
gethostbyname

gethostbyname



Gets Host Information by Name

Portability: UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
PORTABILITY
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS


SYNOPSIS

#include <netdb.h>

struct hostent *gethostbyname(const char *name);


DESCRIPTION

Given the name of a host, gethostbyname returns a pointer to the hostent structure containing the host's IP address and other information. Refer to <netdb.h> for details on the hostent structure. This structure is typically used to find the previous address of the host via the h_addr field. Host information is found either through the resolver or in your system's equivalent of the /etc/hosts file. Refer to gethostbyname and Resolver Configuration for a description of the logic that determines how the host name is found.


RETURN VALUE

If gethostbyname succeeds, it returns a pointer to a host name. A null pointer indicates the network address was not found in the network file.


CAUTION

The value that gethostbyname returns points to a static structure within the library. You must copy the information from this structure before you make further gethostbyname , gethostbyaddr , or gethostent calls.


PORTABILITY

The logic that determines whether to use the host file or the resolver is not uniform across environments. At the source code level, however, gethostbyname is portable to other environments, including most UNIX systems, that implement BSD sockets.


IMPLEMENTATION

The SAS/C implementation of gethostbyname is a combination of the host file and resolver versions of the BSD UNIX Socket Library gethostbyname function.


EXAMPLE

This program uses the socket call, gethostbyname to return an IP address that corresponds to the supplied hostname. gethostbyname will determine if a nameserver or local host tables are being used for name resolution. The answer is returned in the hostent structure, hp and then printed. The local host must be properly configured for name resolution by either a nameserver or host tables.

#include <sys/types.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>

main(int argc, char *argv[])
{
   struct hostent *hp;
   struct in_addr ip_addr;

   /* Verify a "hostname" parameter was supplied */
   if (argc <1 || *argv[1] == '\0')
      exit(EXIT_FAILURE);

/* call gethostbyname() with a host name. gethostbyname() returns a */
/* pointer to a hostent struct or NULL.                             */
   hp = gethostbyname(argv[1]);

   if (!hp) {
      printf("%s was not resolved\n",argv[1]);
      exit(EXIT_FAILURE);
   }

/* move h_addr to ip_addr. This enables conversion to a form        */
/* suitable for printing with the inet_ntoa() function.             */

   ip_addr = *(struct in_addr *)(hp->h_addr);
   printf("Hostname: %s, was resolved to: %s\n",
           argv[1],inet_ntoa(ip_addr));

   exit(EXIT_SUCCESS);
}


RELATED FUNCTIONS

gethostbyaddr , herror , sethostent


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.