Chapter Contents

Previous

Next
gethostent

gethostent



Gets the Next Entry in the Host File

Portability: UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
PORTABILITY
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS


SYNOPSIS

#include <netdb.h>

struct hostent *gethostent(void);


DESCRIPTION

gethostent returns the next sequential entry in the host file.


RETURN VALUE

If gethostent succeeds, it returns a pointer to the hostent structure. Refer to <netdb.h> for details on the hostent structure. A null pointer indicates an error occured or there were no more nework entries. If the resolver and the name server are in use, gethostent returns NULL .


CAUTION

The value that gethostent 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, gethostent is portable to other environments, including most UNIX systems, that implement BSD sockets.


IMPLEMENTATION

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


EXAMPLE

This program demonstrates the socket calls: gethostent , endhostent , and sethostent . The local host must be configured to use hosts tables for name resolution, prefix.ETC.HOSTS; where prefix is described in Network Administration.

#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>

#define TRUE 1

static void prthost(struct hostent *h);

main(int argc, char *argv[])
{
   struct hostent *h;

/* sethostent() opens the prefix.ETC.HOSTS file, or when using a  */
/* nameserver, opens a TCP connection to the nameserver.          */

   sethostent(TRUE);

/* gethostent() reads the next sequential entry in the            */
/* prefix.ETC.HOSTS file. It returns a pointer to a "hostent"     */
/* structure.                                                     */

   while (h=gethostent())
      prthost(h);

/* endhostent() closes the prefix.ETC.HOSTS file, or the          */
/* connection to the nameserver.                                  */

   endhostent();
   exit(EXIT_SUCCESS);
}

/* prthost() prints the information returned by gethostent()      */
/* from the hostent structure.                                    */

static void prthost(struct hostent *h)
{
   char **p;

   /* Print primary name and aliases. */
   printf("\nname: %s\n",h->h_name);
   for (p=h->h_aliases; *p; p++)
      printf("alternate name: %s\n",*p);

   /* Handle unexpected situations gracefully. */
   if (h->h_addrtype != AF_INET) {
      printf("Not an internet address.\n");
      return;
   }
   if (h->h_length != sizeof(struct in_addr)) {
      printf("Invalid length: %d.\n",h->h_length);
      return;
   }

   /* Print the primary address and any alternates.  */
   for (p=h->h_addr_list; *p; p++) {
      printf("%s address: %s\n",
             p==h->h_addr_list ? "primary " : "alternate ",
             inet_ntoa((*(struct in_addr *)*p)) );
   }
}


RELATED FUNCTIONS

endhostent , gethostbyname , sethostent


Chapter Contents

Previous

Next

Top of Page

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