netdb.h File Reference

Typedef size_t

typedef __size_t size_t

_SIZE_T_DECLARED

_PATH_HEQUIV

_PATH_SERVICES

Defines the services path.

h_errno

defines errno

h_addr

NETDB_INTERNAL

Error return codes from gethostbyname() and gethostbyaddr(). see errno.

NETDB_SUCCESS

Error return codes from gethostbyname() and gethostbyaddr(). No Problem.

HOST_NOT_FOUND

Error return codes from gethostbyname() and gethostbyaddr(). Authoritative Answer Host not found.

TRY_AGAIN

Error return codes from gethostbyname() and gethostbyaddr(). Non-Authoritative Host not found, or SERVERFAIL

NO_RECOVERY

Error return codes from gethostbyname() and gethostbyaddr(). Non recoverable errors, FORMERR, REFUSED, NOTIMP

NO_DATA

Error return codes from gethostbyname() and gethostbyaddr(). Valid name, no data record of requested type

NO_ADDRESS

Error return codes from gethostbyname() and gethostbyaddr(). no address, look for MX record

EAI_AGAIN

Error return codes from getaddrinfo(). emporary failure in name resolution

EAI_BADFLAGS

Error return codes from getaddrinfo(). invalid value for ai_flags

EAI_FAIL

Error return codes from getaddrinfo(). non-recoverable failure in name resolution

EAI_FAMILY

Error return codes from getaddrinfo(). ai_family not supported

EAI_MEMORY

Error return codes from getaddrinfo(). memory allocation failure .

EAI_NONAME

Error return codes from getaddrinfo(). hostname nor servname provided, or not known.

EAI_SERVICE

Error return codes from getaddrinfo(). servname not supported for ai_socktype.

EAI_SOCKTYPE

Error return codes from getaddrinfo(). ai_socktype not supported

Error return codes from getaddrinfo().

EAI_SYSTEM

Error return codes from getaddrinfo(). system error returned in errno.

EAI_BADHINTS

Error return codes from getaddrinfo().

EAI_PROTOCOL

Error return codes from getaddrinfo().

EAI_MAX

Error return codes from getaddrinfo().

AI_PASSIVE

Flag values for getaddrinfo(). get address to use bind()

AI_CANONNAME

Flag values for getaddrinfo(). fill ai_canonname

AI_NUMERICHOST

Flag values for getaddrinfo(). prevent host name resolution

AI_NUMERICSERV

Flag values for getaddrinfo(). prevent service name resolution

AI_MASK

valid flags for addrinfo (not a standard def, apps should not use it)

AI_ALL

IPv6 and IPv4-mapped (with AI_V4MAPPED)

AI_V4MAPPED_CFG

accept IPv4-mapped if kernel supports

AI_ADDRCONFIG

only if any address is assigned

AI_V4MAPPED

accept IPv4-mapped IPv6 address

AI_V4CONVERTED

AI_DEFAULT

special recommended flags for getipnodebyname

NI_MAXHOST

Constants for getnameinfo()

NI_MAXSERV

Constants for getnameinfo()

NI_NOFQDN

Flag values for getnameinfo(). Only the nodename portion of the FQDN is returned for local hosts.

NI_NUMERICHOST

Flag values for getnameinfo(). The numeric form of the node's address is returned instead of its name.

NI_NAMEREQD

Flag values for getnameinfo(). Return an error if the node's name cannot be located in the database.

NI_NUMERICSERV

Flag values for getnameinfo(). The numeric form of the service address is returned instead of its name.

NI_DGRAM

Flag values for getnameinfo(). Indicates that the service is a datagram service (SOCK_DGRAM).

SCOPE_DELIMITER

Scope delimit character

endservent ( void )

IMPORT_C voidendservent(void)

Refer to getservent() for the documentation

gethostbyaddr ( const char *, int, int )

IMPORT_C struct hostent *gethostbyaddr(const char *,
int,
int
)
Parameters
Refer to gethostbyname() for the documentation

gethostbyname ( const char * )

IMPORT_C struct hostent *gethostbyname(const char *)

The gethostbyname, and gethostbyaddr functions each return a pointer to an object with the following structure describing an internet host referenced by name or by address, respectively.

The name argument passed to gethostbyname should point to a NUL -terminated hostname. The addr argument passed to gethostbyaddr should point to an address which is len bytes long, in binary form (i.e., not an IP address in human readable ASCII form). The type argument specifies the address family (e.g. AF_INET, etc.) of this address.

The structure returned contains either the information obtained from the name server,

struct  hostent {
        char    *h_name;        // official name of host
        char    **h_aliases;    // alias list 
        int     h_addrtype;     // host address type 
        int     h_length;       // length of address 
        char    **h_addr_list;  // list of addresses from name server 
};
#define h_addr  h_addr_list[0]  //address, for backward compatibility
The members of this structure are:
h_name  Official name of the host.  
h_aliases  A NULL -terminated array of alternate names for the host.  
h_addrtype  The type of address being returned; usually AF_INET.  
h_length  The length, in bytes, of the address.  
h_addr_list  A NULL -terminated array of network addresses for the host. Host addresses are returned in network byte order.  
h_addr  The first address in h_addr_list; this is for backward compatibility.  

When using the nameserver, gethostbyname will search for the named host in the current domain and its parents unless the name ends in a dot.

getaddrinfo and getnameinfo functions are preferred over the gethostbyname, and gethostbyaddr functions.

Examples:
#include<stdio.h>
#include<arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
Int main()
{
struct hostent *hp = 0;
char *test_url=www.google.com:
hp = gethostbyname(test_url);
if(hp==NULL)
printf("gethostbyname failed"):
else
printf("gethostbyname passed");
return 0;
}
Output
Gethostbyname passed
#include<stdio.h>
#include<arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#define urlsize 50
Int main()
{
struct hostent *hp = 0;
char addr[256];
unsigned long  test_addr;
strcpy(addr," 147.243.3.83");
test_addr=inet_addr(addr);
struct hostent *hp;
hp=gethostbyaddr((const char *)&test;_addr,sizeof(test_addr),AF_INET);
if(hp)
printf("DNS query resolved");
else
printf("gethostbyaddr failed");
return 0;
}

Diagnostics:

Error return status from gethostbyname and gethostbyaddr is indicated by return of a NULL pointer. The external integer h_errno may then be checked to see whether this is a temporary failure or an invalid or unknown host. The routine If its argument string is non- NULL, it is printed, followed by a colon and a space. The error message is printed with a trailing newline. The variable h_errno can have the following values: TRY_AGAIN This is usually a temporary error and means that the local server did not receive a response from an authoritative server. A retry at some later time may succeed. NO_RECOVERY Some unexpected server failure was encountered. This is a non-recoverable error.

See also: getaddrinfo() getnameinfo()

Bugs:

These functions use a thread-specific data storage; if the data is needed for future use it should be copied before any subsequent calls overwrite it. Though these functions are thread-safe the getaddrinfo family of functions is recommended instead. Only the Internet address format is currently understood.

Parameters
Note: This description also covers the following functions - gethostbyaddr()
Return Value
The gethostbyname and gethostbyaddr functions return NULL if an error occurs or a valid hostent structure otherwise.

getprotobyname ( const char * )

IMPORT_C struct protoent *getprotobyname(const char *)
The getprotobyname, and getprotobynumber functions each return a pointer to an object with the following structure from the network protocol data base
struct  protoent {
        char    *p_name;        /* official name of protocol */
        char    **p_aliases;    /* alias list */
        int     p_proto;        /* protocol number */
};
The members of this structure are:
p_name  The official name of the protocol.  
p_aliases    A zero terminated list of alternate names for the protocol.  
p_proto  The protocol number.  

The getprotobyname function and getprotobynumber sequentially search from the beginning of the database until a matching protocol name or protocol number is found,

Examples:
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<string.h>
#include<netdb.h>
Int main()
{
struct protoent *p =0;
char *protoname="tcp";
p=getprotobyname(protoname);
if(p!=NULL)
printf("protocol not supported:");
else
printf("protocol supported");
return 0;
}
Output
Protocol supported/not supported based on the support for protocol
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<string.h>
#include<netdb.h>
int main()
	{
	struct protoent *p =0;
	int protonum=6;
	p=getprotobynumber(protonum);
	if(p!=NULL)
		printf("protocol not supported:");
	else
		printf("protocol supported");
	return 0;
	}
Output
Protocol supported/not supported based on the support for protocol
The getprotobynumber, getprotobyname, functions appeared in BSD 4.2 .

Bugs:

These functions use a thread-specific data space; if the data is needed for future use it must be copied before any subsequent calls overwrite it. Only the Internet protocols are currently understood.

Parameters
Note: This description also covers the following functions - getprotobynumber()
Return Value
Null pointer (0) returned on error.

getprotobynumber ( int )

IMPORT_C struct protoent *getprotobynumber(int)

Parameters
Refer to getprotobyname() for the documentation

getservbyname ( const char *, const char * )

IMPORT_C struct servent *getservbyname(const char *,
const char *
)

Parameters
Refer to getservent() for the documentation

getservbyport ( int, const char * )

IMPORT_C struct servent *getservbyport(int,
const char *
)

Parameters
Refer to getservent() for the documentation

getservent ( void )

IMPORT_C struct servent *getservent(void)

Note: This description also covers the following functions - getservbyname() getservbyport() setservent() endservent()

 s_name The official name of the service.
 s_aliases
  A zero terminated list of alternate names for the service.
 s_port The port number at which the service resides.
 Port numbers are returned in network byte order.
 s_proto The name of the protocol to use when contacting the
 service.
The getservent, getservbyname, and getservbyport functions
each return a pointer to an object with the
following structure
containing the broken-out
fields of a line in the network services data base, c:/sys/data/services. 
structservent {
char*s_name;/* official name of service */
char**s_aliases;/* alias list */
ints_port;/* port service resides at */
char*s_proto;/* protocol to use */
};

The members of this structure are: s_name The official name of the service. s_aliases A zero terminated list of alternate names for the service. s_port The port number at which the service resides. Port numbers are returned in network byte order. s_proto The name of the protocol to use when contacting the service.

The getservent function reads the next line of the file, opening the file if necessary.

The setservent function opens and rewinds the file.

The endservent function closes the file.

The getservbyname and getservbyport functions sequentially search from the beginning of the file until a matching protocol name or port number (which must be specified in network byte order) is found, or until EOF is encountered. If a protocol name is also supplied (non- NULL ), searches must also match the protocol.

Examples:
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<netdb.h>
int main()
{
Char *service="http";
Char *protocol="tcp";
Struct servent *p=0;
P=getservbyname(service,protocol);
if(p!=NULL)
printf("service not supported:");
else
printf("Service  supported");
return 0;
}
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<netdb.h>
int main()
{
struct servent *p;
char *protocol="tcp";
int port;
port=htons(80);
p=(port,protocol);
if(p)
{
Printf("port is  assigned");
else
printf("port is not assigned");
}
Output
Port is assigned
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<netdb.h>
Int main()
{
struct servent *p;
p=getservent();
if(p)
printf("getservent successful");
else
printf("getservent failed");
return 0;
}
Output
Getservent passed
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<netdb.h>
int main()
{
int stayopen=1;
retservent(stayopen):
return 0;
}
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<netdb.h>
int main()
{
struct servent *p;
  p=getservent();
if(p)
endservent();
return 0;
}

Diagnostics: Null pointer (0) returned on EOF or error. The getservent, getservbyport, getservbyname, setservent, and endservent functions appeared in BSD 4.2.

Bugs:

These functions use a thread-specific data storage. If the data is needed for future use it should be copied before any subsequent calls overwrite it. Expecting port numbers to fit in a 32 bit quantity is probably naive.

Return Value
getservent, getservbyname and getservbyport functions return the servent structure, or a NULL pointer if an error occurs or the end of the file is reached.

getaddrinfo ( const char *, const char *, const struct addrinfo *, struct addrinfo ** )

IMPORT_C intgetaddrinfo(const char *,
const char *,
const struct addrinfo *,
struct addrinfo **
)

The freeaddrinfo() function shall free one or more addrinfo structures returned by getaddrinfo(), along with any additional storage associated with those structures. If the ai_next field of the structure is not null, the entire list of structures shall be freed. The freeaddrinfo() function shall support the freeing of arbitrary sublists of an addrinfo list originally returned by getaddrinfo().

The getaddrinfo() function shall translate the name of a service location (for example, a host name) and//or a service name and shall return a set of socket addresses and associated information to be used in creating a socket with which to address the specified service.

Return Value
A zero return value for getaddrinfo() indicates successful completion; a non-zero return value indicates failure. The possible values for the failures are listed in the ERRORS section. Upon successful return of getaddrinfo(), the location to which res points shall refer to a linked list of addrinfo structures, each of which shall specify a socket address and information for use in creating a socket with which to use that socket address. The list shall include at least one addrinfo structure.

getnameinfo ( const struct sockaddr *, socklen_t, char *, size_t, char *, size_t, int )

IMPORT_C intgetnameinfo(const struct sockaddr *,
socklen_t,
char *,
size_t,
char *,
size_t,
int
)

The getnameinfo function is used to convert a sockaddr structure to a pair of host name and service strings. It is a replacement for and provides more flexibility than the gethostbyaddr and getservbyport functions and is the converse of the getaddrinfo function. The sockaddr structure sa should point to sockaddr_in (for IPv4) that is salen bytes long.

The host and service names associated with sa are stored in host and serv which have length parameters hostlen and servlen. The maximum value for hostlen is NI_MAXHOST and the maximum value for servlen is NI_MAXSERV, as defined by <netdb.h.> If a length parameter is zero, no string will be stored. Otherwise, enough space must be provided to store the host name or service string plus a byte for the NUL terminator.

The flags argument is formed by OR ing the following values: NI_NOFQDN A fully qualified domain name is not required for local hosts. The local part of the fully qualified domain name is returned instead.
NI_NUMERICHOST  Return the address in numeric form, as if calling inet_ntop, instead of a host name.  
NI_NAMEREQD  A name is required. If the host name cannot be found in DNS and this flag is set, a non-zero error code is returned. If the host name is not found and the flag is not set, the address is returned in numeric form.  
NI_NUMERICSERV  The service name is returned as a digit string representing the port number.  
NI_DGRAM  Specifies that the service being looked up is a datagram service, and causes getservbyport to be called with a second argument of "udp" instead of its default of "tcp." This is required for the few ports (512-514) that have different services for UDP and TCP.  
Examples:
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
int main()
{
     struct addrinfo *result;
     char hostname[80];
     int error;
     if (error = getaddrinfo("www.yahoo.com",NULL, NULL, &result;))
     {
            fprintf(stderr, "error using getaddrinfo: %s
", gai_strerror(error));
     }
     if (result)
     {
            if (error = getnameinfo(result->ai_addr,result->ai_addrlen, hostname, sizeof(hostname), NULL,0,0))
            {
                   printf( "error using getnameinfo: %s
", gai_strerror(error));
            }
    }
return 0;
}

See also: gai_strerror() getaddrinfo() inet_ntop()

Caveats:

getnameinfo can return both numeric and FQDN forms of the address specified in sa. There is no return value that indicates whether the string returned in host is a result of binary to numeric-text translation (like inet_ntop or is the result of a DNS reverse lookup. Because of this, malicious parties could set up a PTR record as follows:
1.0.0.127.in-addr.arpa. IN PTR  10.1.1.1
and trick the caller of getnameinfo into believing that sa is 10.1.1.1 when it is actually 127.0.0.1. To prevent such attacks, the use of NI_NAMEREQD is recommended when the result of getnameinfo is used for access control purposes:
struct sockaddr *sa;
socklen_t salen;
char addr[NI_MAXHOST];
struct addrinfo hints, *res;
int error;
error = getnameinfo(sa, salen, addr, sizeof(addr),
    NULL, 0, NI_NAMEREQD);
if (error == 0) {
        memset(&hints;, 0, sizeof(hints));
        hints.ai_socktype = SOCK_DGRAM; /*dummy*/
        hints.ai_flags = AI_NUMERICHOST;
        if (getaddrinfo(addr, "0", &hints;, &res;) == 0) {
                /* malicious PTR record */
                freeaddrinfo(res);
                printf("bogus PTR record
");
                return -1;
        }
        /* addr is FQDN as a result of PTR lookup */
} else {
        /* addr is numeric string */
        error = getnameinfo(sa, salen, addr, sizeof(addr),
            NULL, 0, NI_NUMERICHOST);
}

Caveats:

getnameinfo can return both numeric and FQDN forms of the address specified in sa. There is no return value that indicates whether the string returned in host is a result of binary to numeric-text translation (like inet_ntop or is the result of a DNS reverse lookup. Because of this, malicious parties could set up a PTR record as follows:
1.0.0.127.in-addr.arpa. IN PTR  10.1.1.1
and trick the caller of getnameinfo into believing that sa is 10.1.1.1 when it is actually 127.0.0.1. To prevent such attacks, the use of NI_NAMEREQD is recommended when the result of getnameinfo is used for access control purposes:
struct sockaddr *sa;
socklen_t salen;
char addr[NI_MAXHOST];
struct addrinfo hints, *res;
int error;
error = getnameinfo(sa, salen, addr, sizeof(addr),
    NULL, 0, NI_NAMEREQD);
if (error == 0) {
        memset(&hints;, 0, sizeof(hints));
        hints.ai_socktype = SOCK_DGRAM; /*dummy*/
        hints.ai_flags = AI_NUMERICHOST;
        if (getaddrinfo(addr, "0", &hints;, &res;) == 0) {
                /* malicious PTR record */
                freeaddrinfo(res);
                printf("bogus PTR record
");
                return -1;
        }
        /* addr is FQDN as a result of PTR lookup */
} else {
        /* addr is numeric string */
        error = getnameinfo(sa, salen, addr, sizeof(addr),
            NULL, 0, NI_NUMERICHOST);
}
Return Value
getnameinfo returns zero on success or one of the error codes listed in gai_strerror if an error occurs.

freeaddrinfo ( struct addrinfo * )

IMPORT_C voidfreeaddrinfo(struct addrinfo *)

See also: bind() connect() send() socket() gai_strerror() gethostbyname() getnameinfo() getservent()

Parameters
Refer to t() for the documentation

gai_strerror ( int )

IMPORT_C const char *gai_strerror(int)
 EAI_AGAIN temporary failure in name resolution
 EAI_BADFLAGS
  invalid value for ai_flags
 EAI_BADHINTS
  invalid value for hints
 EAI_FAIL non-recoverable failure in name resolution
 EAI_FAMILY ai_family not supported
 EAI_MEMORY memory allocation failure
 EAI_NONAME hostname or servname not provided, or not known
 EAI_PROTOCOL
  resolved protocol is unknown
 EAI_SERVICE servname not supported for ai_socktype
 EAI_SOCKTYPE
  ai_socktype not supported
 EAI_SYSTEM system error returned in errno
The gai_strerror function returns an error message string corresponding to the error code returned by getaddrinfo or getnameinfo .

The following error codes and their meaning are defined in #include <netdb.h>

EAI_AGAIN temporary failure in name resolution EAI_BADFLAGS invalid value for ai_flags EAI_BADHINTS invalid value for hints EAI_FAIL non-recoverable failure in name resolution EAI_FAMILY ai_family not supported EAI_MEMORY memory allocation failure EAI_NONAME hostname or servname not provided, or not known EAI_PROTOCOL resolved protocol is unknown EAI_SERVICE servname not supported for ai_socktype EAI_SOCKTYPE ai_socktype not supported EAI_SYSTEM system error returned in errno

Examples:
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
int main()
{
      struct addrinfo *result;
      char hostname[80];
      int error;
      if (error = getaddrinfo("www.nokia.com",NULL, NULL, &result;))
      {
              fprintf(stderr, "error using getaddrinfo: %s
", gai_strerror(error));
      }
      if (result)
      {
              if (error = getnameinfo(result->ai_addr, sizeof(struct sockaddr), hostname, sizeof(hostname), NULL,0,0))
              {
                      printf( "error using getnameinfo: %s
", gai_strerror(error));
              }
      }
return 0;
}

See also: getaddrinfo() getnameinfo()

Return Value
The gai_strerror function returns a pointer to the error message string corresponding to ecode. If ecode is out of range, an implementation-specific error message string is returned.

setnetgrent ( const char * )

voidsetnetgrent(const char *)

setservent ( int )

IMPORT_C voidsetservent(int)

Parameters
Refer to getservent() for the documentation

__h_error ( void )

int *__h_error(void)