typedef __size_t | size_t |
Error return codes from gethostbyname() and gethostbyaddr(). Authoritative Answer Host not found.
Error return codes from gethostbyname() and gethostbyaddr(). Non-Authoritative Host not found, or SERVERFAIL
Error return codes from gethostbyname() and gethostbyaddr(). Non recoverable errors, FORMERR, REFUSED, NOTIMP
Error return codes from gethostbyname() and gethostbyaddr(). Valid name, no data record of requested type
Error return codes from gethostbyname() and gethostbyaddr(). no address, look for MX record
Error return codes from getaddrinfo(). ai_socktype not supported
Error return codes from getaddrinfo().
Flag values for getnameinfo(). Only the nodename portion of the FQDN is returned for local hosts.
Flag values for getnameinfo(). The numeric form of the node's address is returned instead of its name.
Flag values for getnameinfo(). Return an error if the node's name cannot be located in the database.
Flag values for getnameinfo(). The numeric form of the service address is returned instead of its name.
Flag values for getnameinfo(). Indicates that the service is a datagram service (SOCK_DGRAM).
IMPORT_C struct hostent * | gethostbyaddr | ( | const char * | , |
int | , | |||
int | ||||
) |
See also: getaddrinfo() getnameinfo()
Parameters | |
---|---|
Refer to gethostbyname() for the documentation |
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
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.
#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() |
IMPORT_C struct protoent * | getprotobyname | ( | const char * | ) |
struct protoent { char *p_name; /* official name of protocol */ char **p_aliases; /* alias list */ int p_proto; /* protocol number */ };
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,
#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 protocolThe 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() |
IMPORT_C struct protoent * | getprotobynumber | ( | int | ) |
Parameters | |
---|---|
Refer to getprotobyname() for the documentation |
IMPORT_C struct servent * | getservbyname | ( | const char * | , |
const char * | ||||
) |
Parameters | |
---|---|
Refer to getservent() for the documentation |
IMPORT_C struct servent * | getservbyport | ( | int | , |
const char * | ||||
) |
Parameters | |
---|---|
Refer to getservent() for the documentation |
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.
#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.
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.
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.
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.
#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:
1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1and 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:
1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1and 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); }
IMPORT_C void | freeaddrinfo | ( | struct addrinfo * | ) |
See also: bind() connect() send() socket() gai_strerror() gethostbyname() getnameinfo() getservent()
Parameters | |
---|---|
Refer to t() for the documentation |
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 errnoThe 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
#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()