3
0

ipv4_ipv6.txt 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. We need better network address conv helpers.
  2. This is what our applets want:
  3. sockaddr -> hostname
  4. udhcp: hostname -> ipv4 addr
  5. nslookup: hostname -> list of names - done
  6. tftp: host,port -> sockaddr
  7. nc: host,port -> sockaddr
  8. inetd: ?
  9. traceroute: ?, hostname -> ipv4 addr
  10. arping hostname -> ipv4 addr
  11. ping6 hostname -> ipv6 addr
  12. ifconfig hostname -> ipv4 addr (FIXME error check?)
  13. ipcalc ipv4 addr -> hostname
  14. syslogd hostname -> sockaddr
  15. inet_common.c: buggy. hostname -> ipv4 addr
  16. mount hostname -> sockaddr_in
  17. ==================
  18. HOWTO get rid of inet_ntoa/aton:
  19. foo.sin_addr.s_addr = inet_addr(cp);
  20. -
  21. inet_pton(AF_INET, cp, &foo.sin_addr);
  22. inet_aton(cp, &foo.sin_addr);
  23. -
  24. inet_pton(AF_INET, cp, &foo.sin_addr);
  25. ptr = inet_ntoa(foo.sin_addr);
  26. -
  27. char str[INET_ADDRSTRLEN];
  28. ptr = inet_ntop(AF_INET, &foo.sin_addr, str, sizeof(str));
  29. ===================
  30. struct addrinfo {
  31. int ai_flags;
  32. int ai_family;
  33. int ai_socktype;
  34. int ai_protocol;
  35. size_t ai_addrlen;
  36. struct sockaddr *ai_addr;
  37. char *ai_canonname;
  38. struct addrinfo *ai_next;
  39. };
  40. int getaddrinfo(const char *node, const char *service,
  41. const struct addrinfo *hints,
  42. struct addrinfo **res);
  43. void freeaddrinfo(struct addrinfo *res);
  44. const char *gai_strerror(int errcode);
  45. The members ai_family, ai_socktype, and ai_protocol have the same meaning
  46. as the corresponding parameters in the socket(2) system call. The getad-
  47. drinfo(3) function returns socket addresses in either IPv4 or IPv6 address
  48. family, (ai_family will be set to either AF_INET or AF_INET6).
  49. The hints parameter specifies the preferred socket type, or protocol. A
  50. NULL hints specifies that any network address or protocol is acceptable.
  51. If this parameter is not NULL it points to an addrinfo structure whose
  52. ai_family, ai_socktype, and ai_protocol members specify the preferred
  53. socket type. AF_UNSPEC in ai_family specifies any protocol family (either
  54. IPv4 or IPv6, for example). 0 in ai_socktype or ai_protocol specifies
  55. that any socket type or protocol is acceptable as well. The ai_flags mem-
  56. ber specifies additional options, defined below. Multiple flags are spec-
  57. ified by logically OR-ing them together. All the other members in the
  58. hints parameter must contain either 0, or a null pointer.
  59. The node or service parameter, but not both, may be NULL. node specifies
  60. either a numerical network address (dotted-decimal format for IPv4, hex-
  61. adecimal format for IPv6) or a network hostname, whose network addresses
  62. are looked up and resolved. If hints.ai_flags contains the AI_NUMERICHOST
  63. flag then the node parameter must be a numerical network address. The
  64. AI_NUMERICHOST flag suppresses any potentially lengthy network host
  65. address lookups.
  66. The getaddrinfo(3) function creates a linked list of addrinfo structures,
  67. one for each network address subject to any restrictions imposed by the
  68. hints parameter. The ai_canonname field of the first of these addrinfo
  69. structures is set to point to the official name of the host, if
  70. hints.ai_flags includes the AI_CANONNAME flag. ai_family, ai_socktype,
  71. and ai_protocol specify the socket creation parameters. A pointer to the
  72. socket address is placed in the ai_addr member, and the length of the
  73. socket address, in bytes, is placed in the ai_addrlen member.
  74. If node is NULL, the network address in each socket structure is initial-
  75. ized according to the AI_PASSIVE flag, which is set in hints.ai_flags.
  76. The network address in each socket structure will be left unspecified if
  77. AI_PASSIVE flag is set. This is used by server applications, which intend
  78. to accept client connections on any network address. The network address
  79. will be set to the loopback interface address if the AI_PASSIVE flag is
  80. not set. This is used by client applications, which intend to connect to
  81. a server running on the same network host.
  82. If hints.ai_flags includes the AI_ADDRCONFIG flag, then IPv4 addresses are
  83. returned in the list pointed to by result only if the local system has at
  84. least has at least one IPv4 address configured, and IPv6 addresses are
  85. only returned if the local system has at least one IPv6 address config-
  86. ured.
  87. If hint.ai_flags specifies the AI_V4MAPPED flag, and hints.ai_family was
  88. specified as AF_INET6, and no matching IPv6 addresses could be found, then
  89. return IPv4-mapped IPv6 addresses in the list pointed to by result. If
  90. both AI_V4MAPPED and AI_ALL are specified in hints.ai_family, then return
  91. both IPv6 and IPv4-mapped IPv6 addresses in the list pointed to by result.
  92. AI_ALL is ignored if AI_V4MAPPED is not also specified.
  93. service sets the port number in the network address of each socket struc-
  94. ture. If service is NULL the port number will be left uninitialized. If
  95. AI_NUMERICSERV is specified in hints.ai_flags and service is not NULL,
  96. then service must point to a string containing a numeric port number.
  97. This flag is used to inhibit the invocation of a name resolution service
  98. in cases where it is known not to be required.
  99. ==============
  100. int getnameinfo(const struct sockaddr *sa, socklen_t salen,
  101. char *host, size_t hostlen,
  102. char *serv, size_t servlen, int flags);
  103. The getnameinfo(3) function is defined for protocol-independent
  104. address-to-nodename translation. It combines the functionality
  105. of gethostbyaddr(3) and getservbyport(3) and is the inverse of
  106. getaddrinfo(3). The sa argument is a pointer to a generic socket address
  107. structure (of type sockaddr_in or sockaddr_in6) of size salen that
  108. holds the input IP address and port number. The arguments host and
  109. serv are pointers to buffers (of size hostlen and servlen respectively)
  110. to hold the return values.
  111. The caller can specify that no hostname (or no service name) is required
  112. by providing a NULL host (or serv) argument or a zero hostlen (or servlen)
  113. parameter. However, at least one of hostname or service name must be requested.
  114. The flags argument modifies the behaviour of getnameinfo(3) as follows:
  115. NI_NOFQDN
  116. If set, return only the hostname part of the FQDN for local hosts.
  117. NI_NUMERICHOST
  118. If set, then the numeric form of the hostname is returned.
  119. (When not set, this will still happen in case the node's name
  120. cannot be looked up.)
  121. NI_NAMEREQD
  122. If set, then a error is returned if the hostname cannot be looked up.
  123. NI_NUMERICSERV
  124. If set, then the service address is returned in numeric form,
  125. for example by its port number.
  126. NI_DGRAM
  127. If set, then the service is datagram (UDP) based rather than stream
  128. (TCP) based. This is required for the few ports (512-514) that have different
  129. services for UDP and TCP.
  130. =================
  131. Modified IPv6-aware C code:
  132. struct addrinfo *res, *aip;
  133. struct addrinfo hints;
  134. int sock = -1;
  135. int error;
  136. /* Get host address. Any type of address will do. */
  137. memset(&hints, 0, sizeof(hints));
  138. hints.ai_flags = AI_ALL|AI_ADDRCONFIG;
  139. hints.ai_socktype = SOCK_STREAM;
  140. error = getaddrinfo(hostname, servicename, &hints, &res);
  141. if (error != 0) {
  142. (void) fprintf(stderr,
  143. "getaddrinfo: %s for host %s service %s\n",
  144. gai_strerror(error), hostname, servicename);
  145. return -1;
  146. }
  147. /* Try all returned addresses until one works */
  148. for (aip = res; aip != NULL; aip = aip->ai_next) {
  149. /*
  150. * Open socket. The address type depends on what
  151. * getaddrinfo() gave us.
  152. */
  153. sock = socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol);
  154. if (sock == -1) {
  155. perror("socket");
  156. freeaddrinfo(res);
  157. return -1;
  158. }
  159. /* Connect to the host. */
  160. if (connect(sock, aip->ai_addr, aip->ai_addrlen) == -1) {
  161. perror("connect");
  162. (void) close(sock);
  163. sock = -1;
  164. continue;
  165. }
  166. break;
  167. }
  168. freeaddrinfo(res);
  169. Note that for new applications, if you write address-family-agnostic data structures,
  170. there is no need for porting.
  171. However, when it comes to server-side programming in C/C++, there is an additional wrinkle.
  172. Namely, depending on whether your application is written for a dual-stack platform, such
  173. as Solaris or Linux, or a single-stack platform, such as Windows, you would need to
  174. structure the code differently.
  175. Here's the corresponding server C code for a dual-stack platform:
  176. int ServSock, csock;
  177. /* struct sockaddr is too small! */
  178. struct sockaddr_storage addr, from;
  179. ...
  180. ServSock = socket(AF_INET6, SOCK_STREAM, PF_INET6);
  181. bind(ServSock, &addr, sizeof(addr));
  182. do {
  183. csock = accept(ServSocket, &from, sizeof(from));
  184. doClientStuff(csock);
  185. } while (!finished);