ga_port.lc 4.0 KB

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