United States-English |
|
|
HP-UX Reference > Ggethostent(3N)HP-UX 11i Version 3: February 2007 |
|
NAMEgethostent(), gethostbyaddr(), gethostbyname(), sethostent(), endhostent() — get, set, or end network host entry SYNOPSIS#include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> struct hostent *gethostent(void); struct hostent *gethostbyname(const char *name); struct hostent *gethostbyaddr(const char *addr, int len, int type); _XOPEN_SOURCE_EXTENDED only struct hostent *gethostbyaddr(const void *addr, size_t len, int type); int sethostent(int stayopen); int endhostent(void); _XOPEN_SOURCE_EXTENDED only void sethostent(int stayopen); void endhostent(void); DESCRIPTIONThe gethostent(), gethostbyname(), and gethostbyaddr() functions each return a pointer to a structure of type hostent, defined as follows in <netdb.h>: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; }; #define h_addr h_addr_list[0] The members of this structure are:
Name Service Switch-Based OperationThese host entry library routines internally call the name service switch to access the "hosts" database lookup policy configured in the /etc/nsswitch.conf file (see nsswitch.conf(4)). The lookup policy defines the order and the criteria of the supported name services used to resolve host names and Internet addresses. The operations of these name services: Domain Name Server, NIS, and nonserver mode (e.g., files) are listed below. Domain Name Server OperationIf the local system is configured to use the named name server (see named(1M) and resolver(4)) for name or address resolution, then the function:
NIS Server OperationIf ypserv, the server for the Network Information Service (see ypserv(1M)), is used for name or address resolution, then the function:
Nonserver OperationIf the /etc/hosts file is used for name or address resolution, then the function:
The return value, struct hostent, must be saved before a subsequent call to the functions gethost*(), getaddrinfo(), and getnameinfo(). In a multithreaded application, gethostent(), gethostbyaddr(), and gethostbyname() use thread-specific storage that is re-used in each call. The return value, struct hostent, should be unique for each thread and should be saved, if desired, before the thread makes the next gethost*() call. The return value must be saved before a subsequent call to the function getaddrinfo() or getnameinfo(), because these functions may internally call the gethost*() function which may overwrite their return value. For enumeration in multithreaded applications, the position within the enumeration is a process-wide property shared by all threads. sethostent() may be used in a multithreaded application, but resets the enumeration position for all threads. If multiple threads interleave calls to gethostent(), the threads will enumerate disjoint subsets of the host database. ArgumentsCurrently, only the Internet address format is understood. In calls to gethostbyaddr(), the parameter addr must be a pointer to an in_addr structure, an Internet address in network order (see byteorder(3N)) and the header file <netinet/in.h>). The parameter len must be the number of bytes in an Internet address; that is, sizeof (struct in_addr). The parameter type must be the constant AF_INET. RETURN VALUEIf successful, gethostbyname(), gethostbyaddr(), and gethostent() return a pointer to the requested hostent structure. gethostbyname() and gethostbyaddr() return NULL if their host or addr parameters, respectively, cannot be found in the database. If /etc/hosts is being used, they also return NULL if they are unable to open /etc/hosts. gethostbyaddr() also returns NULL if either its addr or len parameter is invalid. gethostent() always returns NULL if the name server is being used. ERRORSIf the name server is being used and gethostbyname() or gethostbyaddr() returns a NULL pointer, the external integer h_errno contains one of the following values:
If the name server is not being used, the value of h_errno may not be meaningful. EXAMPLESThe following code excerpt counts the number of host entries: int count = 0; (void) sethostent(0); while (gethostent() != NULL) count++; (void) endhostent(); The following sample program prints the canonical name, aliases, and "." separated Internet IP addresses for a given "." separated IP address. #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <netinet/in.h> main(int argc, const char **argv) { u_int addr; struct hostent *hp; char **p; if (argc != 2) { (void) printf("usage: %s IP-address\n",argv[0]); exit (1); } if ((int) (addr = inet_addr (argv[1])) == -1) { (void) printf("IP-address must be of the form a.b.c.d\n"); exit (2); } hp=gethostbyaddr((char *) &addr, sizeof (addr), AF_INET); if (hp == NULL) { (void) printf("host information for %s no found \n", argv[1]); exit (3); } for (p = hp->h_addr_list; *p!=0;p++){ struct in_addr in; char **q; (void)memcpy(&in.s_addr, *p, sizeof(in.s_addr)); (void)printf("%s\t%s",inet_ntoa(in), hp->h_name); for (q=hp->h_aliases;*q != 0; q++) (void) printf("%s", *q); (void)putchar('\n'); } exit (0); } WARNINGSPrograms that use the interfaces described in this manpage cannot be linked statically because the implementations of these functions employ dynamic loading and linking of shared objects at run time. h_errno is referenced as an extern int for single thread applications and is defined as function call macro for multithreaded applications in file /usr/include/netdb.h. Applications that reference h_errno need to include /usr/include/netdb.h. OBSOLESCENT INTERFACESint gethostent_r(struct hostent *result, struct hostent_data *buffer); int gethostbyname_r(const char *name, struct hostent *result, struct hostent_data *buffer); int gethostbyaddr_r(const char *addr, int len, int type, struct hostent *result, struct hostent_data *buffer); int sethostent_r(int stayopen, struct hostent_data *buffer); int endhostent_r(struct hostent_data *buffer); The above reentrant interfaces have been moved from libc to libd4r. They are included to support existing applications and may be removed in the future release. New multithreaded applications should use the regular APIs (those without the -r suffix). The reentrant interfaces function the same as the regular interfaces without the -r suffix. However, gethostent_r(), gethostbyname_r(), and gethostbyaddr_r() expect to be passed the address of a struct hostent and will store the address of the result at the supplied parameter. The passed in address of struct hostent_data in the reentrant interfaces cannot be a NULL pointer. The reentrant routines return -1 if the operation is unsuccessful, or, in the case of gethostent_r(), if the end of the hosts list has been reached. 0 is returned otherwise. SEE ALSOnamed(1M), ypserv(1M), resolver(3N), ypclnt(3C), hosts(4), nsswitch.conf(4), ypfiles(4), thread_safety(5). |
Printable version | ||
|