net.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* $Id: net.c,v 1.10 2004/02/26 22:48:08 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 "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. * aslo 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)
  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. if (getaddrinfo(name, NULL, &hints, &res) != 0) {
  59. hints.ai_flags = 0;
  60. if ((error = getaddrinfo(name, NULL, &hints, &res)) < 0) {
  61. warning("getaddrinfo(%.200s): %.200s", name, gai_strerror(error));
  62. return 0;
  63. }
  64. }
  65. if (res && hints.ai_flags & AI_NUMERICHOST) {
  66. strncpy(res_host, name, NI_MAXHOST);
  67. res_host[NI_MAXHOST - 1] = 0;
  68. cpysock(res->ai_addr, &res_addr);
  69. freeaddrinfo(res);
  70. return 1;
  71. } else {
  72. if (res->ai_addr) {
  73. if ((error = getnameinfo(res->ai_addr, res->ai_addrlen,
  74. res_host, NI_MAXHOST,
  75. NULL, 0, NI_NUMERICHOST)) == 0) {
  76. res_host[NI_MAXHOST - 1] = 0;
  77. cpysock(res->ai_addr, &res_addr);
  78. freeaddrinfo(res);
  79. return 1;
  80. } else
  81. warning("getnameinfo(%.200s): %.200s", name, gai_strerror(error));
  82. } else
  83. warning("getip(%.200s): could not get numeric host via getaddrinfo nor getnameinfo", name);
  84. }
  85. return 0;
  86. }
  87. /*
  88. * getaddr( address | fqdn, service ) - get the addrinfo structure
  89. *
  90. * getaddr returns a sockaddr structure in res_addr. it will only resolve
  91. * the address if that is necessary.
  92. */
  93. int
  94. getaddr(char *name, char *service, int socktype, int flags)
  95. {
  96. struct addrinfo hints, *res;
  97. int error;
  98. res = NULL;
  99. bzero((void *) &hints, sizeof(hints));
  100. hints.ai_flags = flags;
  101. hints.ai_socktype = socktype;
  102. /* don't lookup if not necessary */
  103. hints.ai_flags |= AI_NUMERICHOST;
  104. if (getaddrinfo(name, service, &hints, &res) != 0) {
  105. hints.ai_flags = flags;
  106. if ((error = getaddrinfo(name, service, &hints, &res)) < 0) {
  107. warning("getaddrinfo(%.200s): %.200s", name, gai_strerror(error));
  108. return 0;
  109. }
  110. }
  111. if (res->ai_addrlen > sizeof(res_addr))
  112. fatal("%s:%d: internal error: getaddr returned bigger sockaddr than expected (%d>%d)",
  113. __FILE__, __LINE__, res->ai_addrlen, sizeof(res_addr));
  114. cpysock(res->ai_addr, &res_addr);
  115. return 1;
  116. }
  117. int
  118. get_numeric_name(struct sockaddr_storage * source)
  119. {
  120. snprintf(res_host, sizeof(res_host), "<unknown>");
  121. snprintf(res_service, sizeof(res_service), "<unknown>");
  122. return getnameinfo((struct sockaddr *)source, source->ss_len,
  123. res_host, sizeof(res_host),
  124. res_service, sizeof(res_service),
  125. NI_NUMERICHOST | NI_NUMERICSERV);
  126. }
  127. void
  128. cpysock(struct sockaddr * source, struct sockaddr_storage * dest)
  129. {
  130. bzero(dest, sizeof(struct sockaddr_storage));
  131. bcopy(source, dest, source->sa_len);
  132. }
  133. /*
  134. * cmpsock_addr(sockaddr, sockaddr)
  135. *
  136. * compare if two sockaddr are talking about the same host
  137. */
  138. int
  139. cmpsock_addr(struct sockaddr * first, struct sockaddr * second)
  140. {
  141. if (first == NULL || second == NULL)
  142. return 0;
  143. if ((first->sa_len != second->sa_len) ||
  144. (first->sa_family != second->sa_family))
  145. return 0;
  146. if (first->sa_family == PF_INET) {
  147. if (bcmp((void *) &((struct sockaddr_in *) first)->sin_addr,
  148. (void *) &((struct sockaddr_in *) second)->sin_addr,
  149. sizeof(struct in_addr)) == 0)
  150. return 1;
  151. else
  152. return 0;
  153. }
  154. if (first->sa_family == PF_INET6) {
  155. if (bcmp((void *) &((struct sockaddr_in6 *) first)->sin6_addr,
  156. (void *) &((struct sockaddr_in6 *) second)->sin6_addr,
  157. sizeof(struct in6_addr)) == 0)
  158. return 1;
  159. else
  160. return 0;
  161. }
  162. /* don't know what to compare for this family */
  163. return 0;
  164. }
  165. /* generate INADDR_ANY info */
  166. void
  167. get_inaddrany_sockaddr(struct sockaddr_storage * sockaddr, int family, int socktype, char *port)
  168. {
  169. struct addrinfo hints, *res;
  170. bzero((void *) &hints, sizeof(struct addrinfo));
  171. hints.ai_family = family;
  172. hints.ai_socktype = socktype;
  173. hints.ai_flags = AI_PASSIVE;
  174. if (getaddrinfo(NULL, port, &hints, &res) != 0)
  175. fatal("could not get inaddr address");
  176. else {
  177. cpysock((struct sockaddr *) res->ai_addr, sockaddr);
  178. freeaddrinfo(res);
  179. }
  180. }
  181. /* fill a source->sockaddr with a sockaddr for use in address compares */
  182. void
  183. get_source_sockaddr(struct source * source)
  184. {
  185. if (!getip(source->addr))
  186. fatal("could not get address information for %.200s",
  187. source->addr);
  188. cpysock((struct sockaddr *) &res_addr, &source->sockaddr);
  189. }
  190. /* fill mux->sockaddr with a udp listen sockaddr */
  191. void
  192. get_mux_sockaddr(struct mux * mux, int socktype)
  193. {
  194. if (getaddr(mux->addr, mux->port, socktype, AI_PASSIVE) == 0)
  195. fatal("could not get address information for %.200s %.200s",
  196. mux->addr, mux->port);
  197. cpysock((struct sockaddr *) &res_addr, &mux->sockaddr);
  198. }