net.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2001-2004 Willem Dijkstra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #include <netinet/in.h>
  33. #include <netdb.h>
  34. #include <string.h>
  35. #include "conf.h"
  36. #include "data.h"
  37. #include "error.h"
  38. #include "net.h"
  39. /*
  40. * getip( address | fqdn ) - get ip address
  41. *
  42. * getip returns 1 if address could be reworked into an ip address. Resolved
  43. * data is stored in the globals res_host. The address structure res_addr is
  44. * also filled with sockaddr information that was obtained.
  45. */
  46. char res_host[NI_MAXHOST];
  47. char res_service[NI_MAXSERV];
  48. struct sockaddr_storage res_addr;
  49. int
  50. getip(char *name, int family)
  51. {
  52. struct addrinfo hints, *res;
  53. int error;
  54. res = NULL;
  55. bzero((void *) &hints, sizeof(struct addrinfo));
  56. /* don't lookup if we have a numeric address already */
  57. hints.ai_flags = AI_NUMERICHOST;
  58. hints.ai_family = family;
  59. if (getaddrinfo(name, NULL, &hints, &res) != 0) {
  60. hints.ai_flags = 0;
  61. if ((error = getaddrinfo(name, NULL, &hints, &res)) < 0) {
  62. debug("getaddrinfo(%.200s, %d): %.200s", name, family, gai_strerror(error));
  63. return 0;
  64. }
  65. }
  66. if (res && hints.ai_flags & AI_NUMERICHOST) {
  67. strncpy(res_host, name, NI_MAXHOST);
  68. res_host[NI_MAXHOST - 1] = 0;
  69. cpysock(res->ai_addr, &res_addr);
  70. freeaddrinfo(res);
  71. return 1;
  72. } else {
  73. if (res && res->ai_addr) {
  74. if ((error = getnameinfo(res->ai_addr, res->ai_addrlen,
  75. res_host, NI_MAXHOST,
  76. NULL, 0, NI_NUMERICHOST)) == 0) {
  77. res_host[NI_MAXHOST - 1] = 0;
  78. cpysock(res->ai_addr, &res_addr);
  79. freeaddrinfo(res);
  80. return 1;
  81. } else {
  82. debug("getnameinfo(%.200s, %d): %.200s", name, family, gai_strerror(error));
  83. }
  84. } else {
  85. debug("getip(%.200s, family): could not get numeric host via getaddrinfo nor getnameinfo", name, family);
  86. }
  87. }
  88. return 0;
  89. }
  90. /*
  91. * getaddr( address | fqdn, service ) - get the addrinfo structure
  92. *
  93. * getaddr returns a sockaddr structure in res_addr. it will only resolve
  94. * the address if that is necessary.
  95. */
  96. int
  97. getaddr(char *name, char *service, int socktype, int flags)
  98. {
  99. struct addrinfo hints, *res;
  100. int error;
  101. res = NULL;
  102. bzero((void *) &hints, sizeof(hints));
  103. hints.ai_flags = flags;
  104. hints.ai_socktype = socktype;
  105. /* don't lookup if not necessary */
  106. hints.ai_flags |= AI_NUMERICHOST;
  107. if (getaddrinfo(name, service, &hints, &res) != 0) {
  108. hints.ai_flags = flags;
  109. if ((error = getaddrinfo(name, service, &hints, &res)) < 0) {
  110. warning("getaddrinfo(%.200s): %.200s", name, gai_strerror(error));
  111. return 0;
  112. }
  113. }
  114. if (res->ai_addrlen > sizeof(res_addr))
  115. fatal("%s:%d: internal error: getaddr returned bigger sockaddr than expected (%d>%d)",
  116. __FILE__, __LINE__, res->ai_addrlen, sizeof(res_addr));
  117. cpysock(res->ai_addr, &res_addr);
  118. return 1;
  119. }
  120. int
  121. get_numeric_name(struct sockaddr_storage * source)
  122. {
  123. snprintf(res_host, sizeof(res_host), "<unknown>");
  124. snprintf(res_service, sizeof(res_service), "<unknown>");
  125. return getnameinfo((struct sockaddr *)source, SS_LEN(source),
  126. res_host, sizeof(res_host),
  127. res_service, sizeof(res_service),
  128. NI_NUMERICHOST | NI_NUMERICSERV);
  129. }
  130. void
  131. cpysock(struct sockaddr * source, struct sockaddr_storage * dest)
  132. {
  133. bzero(dest, sizeof(struct sockaddr_storage));
  134. bcopy(source, dest, SA_LEN(source));
  135. }
  136. /*
  137. * cmpsock_addr(sockaddr, sockaddr)
  138. *
  139. * compare if two sockaddr are talking about the same host
  140. */
  141. int
  142. cmpsock_addr(struct sockaddr * first, struct sockaddr * second)
  143. {
  144. if (first == NULL || second == NULL)
  145. return 0;
  146. if ((SA_LEN(first) != SA_LEN(second)) ||
  147. (first->sa_family != second->sa_family))
  148. return 0;
  149. if (first->sa_family == PF_INET) {
  150. if (bcmp((void *) &((struct sockaddr_in *) first)->sin_addr,
  151. (void *) &((struct sockaddr_in *) second)->sin_addr,
  152. sizeof(struct in_addr)) == 0)
  153. return 1;
  154. else
  155. return 0;
  156. }
  157. if (first->sa_family == PF_INET6) {
  158. if (bcmp((void *) &((struct sockaddr_in6 *) first)->sin6_addr,
  159. (void *) &((struct sockaddr_in6 *) second)->sin6_addr,
  160. sizeof(struct in6_addr)) == 0)
  161. return 1;
  162. else
  163. return 0;
  164. }
  165. /* do not know what to compare for this family */
  166. return 0;
  167. }
  168. /* generate sockaddr based on family, type and getaddrinfo flags */
  169. void
  170. get_sockaddr(struct sockaddr_storage * sockaddr, int family, int socktype,
  171. int flags, char * host, char * port)
  172. {
  173. struct addrinfo hints, *res;
  174. bzero((void *) &hints, sizeof(struct addrinfo));
  175. hints.ai_family = family;
  176. hints.ai_socktype = socktype;
  177. hints.ai_flags = flags;
  178. if (getaddrinfo(host, port, &hints, &res) != 0)
  179. fatal("could not get sockaddr address");
  180. else {
  181. cpysock((struct sockaddr *) res->ai_addr, sockaddr);
  182. freeaddrinfo(res);
  183. }
  184. }
  185. /* fill a source->sockaddr with a sockaddr for use in address compares */
  186. int
  187. get_source_sockaddr(struct source * source, int family)
  188. {
  189. if (getip(source->addr, family)) {
  190. cpysock((struct sockaddr *) &res_addr, &source->sockaddr);
  191. return 1;
  192. }
  193. return 0;
  194. }
  195. /* fill mux->sockaddr with a udp listen sockaddr */
  196. void
  197. get_mux_sockaddr(struct mux * mux, int socktype)
  198. {
  199. if (getaddr(mux->addr, mux->port, socktype, AI_PASSIVE) == 0)
  200. fatal("could not get address information for %.200s %.200s",
  201. mux->addr, mux->port);
  202. cpysock((struct sockaddr *) &res_addr, &mux->sockaddr);
  203. }