Update contrib.
1 /** @file ../include/netdb.h
5 /** @fn endservent(void)
7 Refer to getservent() for the documentation
16 /** @fn gethostbyaddr(const char *addr, int length, int format)
20 Refer to gethostbyname() for the documentation
31 /** @fn gethostbyname(const char *name)
34 Note: This description also covers the following functions -
37 @return The gethostbyname and gethostbyaddr functions return NULL if an error occurs or a valid hostent
40 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.
42 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.
44 The structure returned contains either the information obtained from the name server,
48 char *h_name; // official name of host
49 char **h_aliases; // alias list
50 int h_addrtype; // host address type
51 int h_length; // length of address
52 char **h_addr_list; // list of addresses from name server
54 #define h_addr h_addr_list[0] //address, for backward compatibility
59 The members of this structure are:
61 h_name Official name of the host.
62 h_aliases A NULL -terminated array of alternate names for the host.
63 h_addrtype The type of address being returned; usually AF_INET.
64 h_length The length, in bytes, of the address.
65 h_addr_list A NULL -terminated array of network addresses for the host. Host addresses are returned in network byte order.
66 h_addr The first address in h_addr_list; this is for backward compatibility.
70 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.
72 getaddrinfo and getnameinfo functions are preferred over the gethostbyname, and gethostbyaddr functions.
83 #include <sys/socket.h>
84 #include <netinet/in.h>
89 struct hostent *hp = 0;
90 char *test_url=www.google.com:
91 hp = gethostbyname(test_url);
93 printf("gethostbyname failed"):
95 printf("gethostbyname passed");
107 #include<arpa/inet.h>
108 #include <sys/socket.h>
109 #include <netinet/in.h>
115 struct hostent *hp = 0;
117 unsigned long test_addr;
118 strcpy(addr," 147.243.3.83");
119 test_addr=inet_addr(addr);
121 hp=gethostbyaddr((const char *)&test;_addr,sizeof(test_addr),AF_INET);
123 printf("DNS query resolved");
125 printf("gethostbyaddr failed");
133 Error return status from gethostbyname and gethostbyaddr is indicated by return of a NULL pointer.
134 The external integer h_errno may then be checked to see whether this is a temporary failure
135 or an invalid or unknown host.
137 If its argument string is non- NULL, it is printed, followed by a colon and a space.
138 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
139 and means that the local server did not receive
140 a response from an authoritative server.
141 A retry at some later time may succeed. NO_RECOVERY Some unexpected server failure was encountered.
142 This is a non-recoverable error.
150 These functions use a thread-specific data storage; if the data is needed
151 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
152 instead. Only the Internet
153 address format is currently understood.
157 @externallyDefinedApi
160 /** @fn getprotobyname(const char *name)
163 Note: This description also covers the following functions -
170 The getprotobyname, and getprotobynumber functions each return a pointer to an object with the following structure from the network protocol data base
173 char *p_name; /* official name of protocol */
174 char **p_aliases; /* alias list */
175 int p_proto; /* protocol number */
179 The members of this structure are:
181 p_name The official name of the protocol.
182 p_aliases A zero terminated list of alternate names for the protocol.
183 p_proto The protocol number.
187 The getprotobyname function and getprotobynumber sequentially search from the beginning of the database until a matching protocol name or protocol number is found,
192 #include<netinet/in.h>
193 #include<arpa/inet.h>
199 struct protoent *p =0;
200 char *protoname="tcp";
201 p=getprotobyname(protoname);
203 printf("protocol not supported:");
205 printf("protocol supported");
212 Protocol supported/not supported based on the support for protocol
216 #include<netinet/in.h>
217 #include<arpa/inet.h>
223 struct protoent *p =0;
225 p=getprotobynumber(protonum);
227 printf("protocol not supported:");
229 printf("protocol supported");
236 Protocol supported/not supported based on the support for protocol
239 The getprotobynumber, getprotobyname, functions appeared in BSD 4.2 .
243 These functions use a thread-specific data space; if the data is needed for
244 future use it must be copied before any subsequent calls overwrite it. Only the
245 Internet protocols are currently understood.
250 @externallyDefinedApi
253 /** @fn getprotobynumber(int proto)
256 Refer to getprotobyname() for the documentation
262 @externallyDefinedApi
265 /** @fn getservbyname(const char *name, const char *proto)
268 Refer to getservent() for the documentation
274 @externallyDefinedApi
277 /** @fn getservbyport(int port, const char *proto)
280 Refer to getservent() for the documentation
286 @externallyDefinedApi
289 /** @fn getservent(void)
291 Note: This description also covers the following functions -
292 getservbyname() getservbyport() setservent() endservent()
294 @return 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.
297 s_name The official name of the service.
299 A zero terminated list of alternate names for the service.
300 s_port The port number at which the service resides.
301 Port numbers are returned in network byte order.
302 s_proto The name of the protocol to use when contacting the
308 The getservent, getservbyname, and getservbyport functions
309 each return a pointer to an object with the
311 containing the broken-out
312 fields of a line in the network services data base, c:/sys/data/services.
314 char*s_name;/* official name of service */
315 char**s_aliases;/* alias list */
316 ints_port;/* port service resides at */
317 char*s_proto;/* protocol to use */
321 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.
322 Port numbers are returned in network byte order. s_proto The name of the protocol to use when contacting the
325 The getservent function
326 reads the next line of the file, opening the file if necessary.
328 The setservent function
329 opens and rewinds the file.
331 The endservent function
334 The getservbyname and getservbyport functions
335 sequentially search from the beginning
336 of the file until a matching
338 port number (which must be specified in
339 network byte order) is found,
340 or until EOF is encountered.
341 If a protocol name is also supplied (non- NULL ), searches must also match the protocol.
346 #include<netinet/in.h>
347 #include<arpa/inet.h>
352 Char *service="http";
353 Char *protocol="tcp";
355 P=getservbyname(service,protocol);
357 printf("service not supported:");
359 printf("Service supported");
366 #include<netinet/in.h>
367 #include<arpa/inet.h>
373 char *protocol="tcp";
379 Printf("port is assigned");
381 printf("port is not assigned");
392 #include<netinet/in.h>
393 #include<arpa/inet.h>
401 printf("getservent successful");
403 printf("getservent failed");
415 #include<netinet/in.h>
416 #include<arpa/inet.h>
422 retservent(stayopen):
429 #include<netinet/in.h>
430 #include<arpa/inet.h>
446 (0) returned on EOF or error. The getservent, getservbyport, getservbyname, setservent, and endservent functions appeared in BSD 4.2.
450 These functions use a thread-specific data storage. If the data is needed
451 for future use it should be copied before any subsequent calls overwrite it. Expecting
452 port numbers to fit in a 32 bit quantity is probably naive.
456 @externallyDefinedApi
459 /** @fn getaddrinfo(const char *hostname, const char *servname,const struct addrinfo *hints, struct addrinfo **res)
464 @return 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.
465 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.
466 The list shall include at least one addrinfo structure.
468 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().
470 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.
473 @externallyDefinedApi
476 /** @fn getnameinfo( const struct sockaddr * sa , socklen_t salen, char * host,size_t
477 hostlen, char * serv, size_t servlen, int flags )
485 @return getnameinfo returns zero on success or one of the error codes listed in gai_strerror if an error occurs.
487 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.
488 The sockaddr structure sa should point to sockaddr_in (for IPv4) that is salen bytes long.
490 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.
492 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.
494 NI_NUMERICHOST Return the address in numeric form, as if calling inet_ntop, instead of a host name.
495 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.
496 NI_NUMERICSERV The service name is returned as a digit string representing the port number.
497 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.
506 #include <netinet/in.h>
509 struct addrinfo *result;
512 if (error = getaddrinfo("www.yahoo.com",NULL, NULL, &result;))
514 fprintf(stderr, "error using getaddrinfo: %s
515 ", gai_strerror(error));
519 if (error = getnameinfo(result->ai_addr,result->ai_addrlen, hostname, sizeof(hostname), NULL,0,0))
521 printf( "error using getnameinfo: %s
522 ", gai_strerror(error));
536 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.
537 Because of this, malicious parties could set up a PTR record as follows:
539 1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
542 and trick the caller of getnameinfo into believing that sa is 10.1.1.1
543 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
544 for access control purposes:
548 char addr[NI_MAXHOST];
549 struct addrinfo hints, *res;
551 error = getnameinfo(sa, salen, addr, sizeof(addr),
552 NULL, 0, NI_NAMEREQD);
554 memset(&hints;, 0, sizeof(hints));
555 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
556 hints.ai_flags = AI_NUMERICHOST;
557 if (getaddrinfo(addr, "0", &hints;, &res;) == 0) {
558 /* malicious PTR record */
560 printf("bogus PTR record
564 /* addr is FQDN as a result of PTR lookup */
566 /* addr is numeric string */
567 error = getnameinfo(sa, salen, addr, sizeof(addr),
568 NULL, 0, NI_NUMERICHOST);
575 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.
576 Because of this, malicious parties could set up a PTR record as follows:
578 1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
581 and trick the caller of getnameinfo into believing that sa is 10.1.1.1
582 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
583 for access control purposes:
587 char addr[NI_MAXHOST];
588 struct addrinfo hints, *res;
590 error = getnameinfo(sa, salen, addr, sizeof(addr),
591 NULL, 0, NI_NAMEREQD);
593 memset(&hints;, 0, sizeof(hints));
594 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
595 hints.ai_flags = AI_NUMERICHOST;
596 if (getaddrinfo(addr, "0", &hints;, &res;) == 0) {
597 /* malicious PTR record */
599 printf("bogus PTR record
603 /* addr is FQDN as a result of PTR lookup */
605 /* addr is numeric string */
606 error = getnameinfo(sa, salen, addr, sizeof(addr),
607 NULL, 0, NI_NUMERICHOST);
615 @externallyDefinedApi
618 /** @fn freeaddrinfo(struct addrinfo *ai)
620 Refer to t() for the documentation
634 @externallyDefinedApi
637 /** @fn gai_strerror(int ecode)
639 @return The gai_strerror function
640 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.
643 EAI_AGAIN temporary failure in name resolution
645 invalid value for ai_flags
647 invalid value for hints
648 EAI_FAIL non-recoverable failure in name resolution
649 EAI_FAMILY ai_family not supported
650 EAI_MEMORY memory allocation failure
651 EAI_NONAME hostname or servname not provided, or not known
653 resolved protocol is unknown
654 EAI_SERVICE servname not supported for ai_socktype
656 ai_socktype not supported
657 EAI_SYSTEM system error returned in errno
660 The gai_strerror function returns an error message string corresponding to the error code
661 returned by getaddrinfo or getnameinfo .
663 The following error codes and their meaning are defined in \#include \<netdb.h\>
665 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
671 #include <netinet/in.h>
674 struct addrinfo *result;
677 if (error = getaddrinfo("www.nokia.com",NULL, NULL, &result;))
679 fprintf(stderr, "error using getaddrinfo: %s
680 ", gai_strerror(error));
684 if (error = getnameinfo(result->ai_addr, sizeof(struct sockaddr), hostname, sizeof(hostname), NULL,0,0))
686 printf( "error using getnameinfo: %s
687 ", gai_strerror(error));
701 @externallyDefinedApi
704 /** @fn setservent(int f)
706 Refer to getservent() for the documentation
712 @externallyDefinedApi
720 @externallyDefinedApi
723 /** @def _PATH_SERVICES
725 Defines the services path.
728 @externallyDefinedApi
731 /** @def NETDB_INTERNAL
733 Error return codes from gethostbyname() and gethostbyaddr(). see errno.
736 @externallyDefinedApi
739 /** @def NETDB_SUCCESS
741 Error return codes from gethostbyname() and gethostbyaddr(). No Problem.
744 @externallyDefinedApi
747 /** @def HOST_NOT_FOUND
749 Error return codes from gethostbyname() and gethostbyaddr(). Authoritative Answer Host not found.
752 @externallyDefinedApi
757 Error return codes from gethostbyname() and gethostbyaddr(). Non-Authoritative Host not found, or SERVERFAIL
760 @externallyDefinedApi
765 Error return codes from gethostbyname() and gethostbyaddr(). Non recoverable errors, FORMERR, REFUSED, NOTIMP
768 @externallyDefinedApi
773 Error return codes from gethostbyname() and gethostbyaddr(). Valid name, no data record of requested type
776 @externallyDefinedApi
781 Error return codes from gethostbyname() and gethostbyaddr(). no address, look for MX record
784 @externallyDefinedApi
789 Error return codes from getaddrinfo(). emporary failure in name resolution
792 @externallyDefinedApi
795 /** @def EAI_BADFLAGS
797 Error return codes from getaddrinfo(). invalid value for ai_flags
800 @externallyDefinedApi
805 Error return codes from getaddrinfo(). non-recoverable failure in name resolution
808 @externallyDefinedApi
813 Error return codes from getaddrinfo(). ai_family not supported
816 @externallyDefinedApi
821 Error return codes from getaddrinfo(). memory allocation failure .
824 @externallyDefinedApi
829 Error return codes from getaddrinfo(). hostname nor servname provided, or not known.
832 @externallyDefinedApi
837 Error return codes from getaddrinfo(). servname not supported for ai_socktype.
840 @externallyDefinedApi
843 /** @def EAI_SOCKTYPE
845 Error return codes from getaddrinfo(). ai_socktype not supported
848 @externallyDefinedApi
853 Error return codes from getaddrinfo(). system error returned in errno.
856 @externallyDefinedApi
859 /** @def EAI_SOCKTYPE
861 Error return codes from getaddrinfo().
864 @externallyDefinedApi
867 /** @def EAI_BADHINTS
869 Error return codes from getaddrinfo().
872 @externallyDefinedApi
875 /** @def EAI_PROTOCOL
877 Error return codes from getaddrinfo().
880 @externallyDefinedApi
885 Error return codes from getaddrinfo().
888 @externallyDefinedApi
893 Flag values for getaddrinfo(). get address to use bind()
896 @externallyDefinedApi
899 /** @def AI_NUMERICHOST
901 Flag values for getaddrinfo(). prevent host name resolution
904 @externallyDefinedApi
907 /** @def AI_CANONNAME
909 Flag values for getaddrinfo(). fill ai_canonname
912 @externallyDefinedApi
915 /** @def AI_NUMERICSERV
917 Flag values for getaddrinfo(). prevent service name resolution
920 @externallyDefinedApi
925 valid flags for addrinfo (not a standard def, apps should not use it)
928 @externallyDefinedApi
933 IPv6 and IPv4-mapped (with AI_V4MAPPED)
936 @externallyDefinedApi
939 /** @def AI_V4MAPPED_CFG
941 accept IPv4-mapped if kernel supports
944 @externallyDefinedApi
947 /** @def AI_ADDRCONFIG
949 only if any address is assigned
952 @externallyDefinedApi
957 accept IPv4-mapped IPv6 address
960 @externallyDefinedApi
965 special recommended flags for getipnodebyname
968 @externallyDefinedApi
973 Constants for getnameinfo()
976 @externallyDefinedApi
981 Constants for getnameinfo()
984 @externallyDefinedApi
989 Flag values for getnameinfo().
990 Only the nodename portion of the FQDN is returned for local hosts.
993 @externallyDefinedApi
996 /** @def NI_NUMERICHOST
998 Flag values for getnameinfo().
999 The numeric form of the node's address is returned instead of its name.
1002 @externallyDefinedApi
1005 /** @def NI_NAMEREQD
1007 Flag values for getnameinfo().
1008 Return an error if the node's name cannot be located in the database.
1011 @externallyDefinedApi
1014 /** @def NI_NUMERICSERV
1016 Flag values for getnameinfo().
1017 The numeric form of the service address is returned instead of its name.
1020 @externallyDefinedApi
1025 Flag values for getnameinfo().
1026 Indicates that the service is a datagram service (SOCK_DGRAM).
1029 @externallyDefinedApi
1032 /** @def SCOPE_DELIMITER
1034 Scope delimit character
1037 @externallyDefinedApi
1042 Structures returned by network data base library.
1043 All addresses are supplied in host order, and returned in network order (suitable for use in system calls).
1046 @externallyDefinedApi
1049 /** @var hostent::h_name
1050 official name of host
1053 /** @var hostent::h_aliases
1057 /** @var hostent::h_addrtype
1061 /** @var hostent::h_length
1065 /** @var hostent::h_addr_list
1066 list of addresses from name server
1071 Contains following members,
1074 @externallyDefinedApi
1077 /** @var netent::n_name
1078 official name of net
1081 /** @var netent::n_aliases
1085 /** @var netent::n_addrtype
1089 /** @var netent::n_net
1095 Includes following members,
1098 @externallyDefinedApi
1101 /** @var servent::s_name
1102 official service name
1105 /** @var servent::s_aliases
1109 /** @var servent::s_port
1113 /** @var servent::s_proto
1117 /** @struct protoent
1119 Includes the following members,
1122 @externallyDefinedApi
1125 /** @var protoent::p_name
1126 official protocol name
1129 /** @var protoent::p_aliases
1133 /** @var protoent::p_proto
1137 /** @struct addrinfo
1139 Includes the following members,
1142 @externallyDefinedApi
1145 /** @var addrinfo::ai_flags
1146 AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST
1149 /** @var addrinfo::ai_family
1153 /** @var addrinfo::ai_socktype
1157 /** @var addrinfo::ai_protocol
1158 0 or IPPROTO_xxx for IPv4 and IPv6
1161 /** @var addrinfo::ai_addrlen
1165 /** @var addrinfo::ai_canonname
1166 canonical name for hostname
1169 /** @var addrinfo::ai_addr
1173 /** @var addrinfo::ai_next
1174 next structure in linked list
1178 /** @typedef typedef __size_t size_t
1180 Used for sizes of objects.
1183 @externallyDefinedApi
1186 /** @typedef typedef __socklen_t socklen_t
1188 Socket address length type.
1191 @externallyDefinedApi
1194 /** @typedef typedef __uint32_t uint32_t
1199 @externallyDefinedApi