net.c 6.6 KB

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