ga_port.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "gai_hdr.h"
  2. /*
  3. * Go through all the addrinfo structures, checking for a match of the
  4. * socket type and filling in the socket type, and then the port number
  5. * in the corresponding socket address structures.
  6. *
  7. * The AI_CLONE flag works as follows. Consider a multihomed host with
  8. * two IP addresses and no socket type specified by the caller. After
  9. * the "host" search there are two addrinfo structures, one per IP address.
  10. * Assuming a service supported by both TCP and UDP (say the daytime
  11. * service) we need to return *four* addrinfo structures:
  12. * IP#1, SOCK_STREAM, TCP port,
  13. * IP#1, SOCK_DGRAM, UDP port,
  14. * IP#2, SOCK_STREAM, TCP port,
  15. * IP#2, SOCK_DGRAM, UDP port.
  16. * To do this, when the "host" loop creates an addrinfo structure, if the
  17. * caller has not specified a socket type (hintsp->ai_socktype == 0), the
  18. * AI_CLONE flag is set. When the following function finds an entry like
  19. * this it is handled as follows: If the entry's ai_socktype is still 0,
  20. * this is the first use of the structure, and the ai_socktype field is set.
  21. * But, if the entry's ai_socktype is nonzero, then we clone a new addrinfo
  22. * structure and set it's ai_socktype to the new value. Although we only
  23. * need two socket types today (SOCK_STREAM and SOCK_DGRAM) this algorithm
  24. * will handle any number. Also notice that Posix.1g requires all socket
  25. * types to be nonzero.
  26. */
  27. /* include ga_port */
  28. int
  29. ga_port(struct addrinfo *aihead, int port, int socktype)
  30. /* port must be in network byte order */
  31. {
  32. int nfound = 0;
  33. struct addrinfo *ai;
  34. for (ai = aihead; ai != NULL; ai = ai->ai_next) {
  35. if (ai->ai_flags & AI_CLONE) {
  36. if (ai->ai_socktype != 0) {
  37. if ( (ai = ga_clone(ai)) == NULL)
  38. return(-1); /* memory allocation error */
  39. /* ai points to newly cloned entry, which is what we want */
  40. }
  41. } else if (ai->ai_socktype != socktype)
  42. continue; /* ignore if mismatch on socket type */
  43. ai->ai_socktype = socktype;
  44. switch (ai->ai_family) {
  45. #ifdef IPv4
  46. case AF_INET:
  47. ((struct sockaddr_in *) ai->ai_addr)->sin_port = port;
  48. nfound++;
  49. break;
  50. #endif
  51. #ifdef IPv6
  52. case AF_INET6:
  53. ((struct sockaddr_in6 *) ai->ai_addr)->sin6_port = port;
  54. nfound++;
  55. break;
  56. #endif
  57. }
  58. }
  59. return(nfound);
  60. }
  61. /* end ga_port */