if2ip.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef HAVE_NETINET_IN_H
  24. # include <netinet/in.h>
  25. #endif
  26. #ifdef HAVE_ARPA_INET_H
  27. # include <arpa/inet.h>
  28. #endif
  29. #ifdef HAVE_NET_IF_H
  30. # include <net/if.h>
  31. #endif
  32. #ifdef HAVE_SYS_IOCTL_H
  33. # include <sys/ioctl.h>
  34. #endif
  35. #ifdef HAVE_NETDB_H
  36. # include <netdb.h>
  37. #endif
  38. #ifdef HAVE_SYS_SOCKIO_H
  39. # include <sys/sockio.h>
  40. #endif
  41. #ifdef HAVE_IFADDRS_H
  42. # include <ifaddrs.h>
  43. #endif
  44. #ifdef HAVE_STROPTS_H
  45. # include <stropts.h>
  46. #endif
  47. #ifdef __VMS
  48. # include <inet.h>
  49. #endif
  50. #include "inet_ntop.h"
  51. #include "strcase.h"
  52. #include "if2ip.h"
  53. /* The last 3 #include files should be in this order */
  54. #include "curl_printf.h"
  55. #include "curl_memory.h"
  56. #include "memdebug.h"
  57. /* ------------------------------------------------------------------ */
  58. /* Return the scope of the given address. */
  59. unsigned int Curl_ipv6_scope(const struct sockaddr *sa)
  60. {
  61. #ifndef ENABLE_IPV6
  62. (void) sa;
  63. #else
  64. if(sa->sa_family == AF_INET6) {
  65. const struct sockaddr_in6 * sa6 = (const struct sockaddr_in6 *)(void *) sa;
  66. const unsigned char *b = sa6->sin6_addr.s6_addr;
  67. unsigned short w = (unsigned short) ((b[0] << 8) | b[1]);
  68. if((b[0] & 0xFE) == 0xFC) /* Handle ULAs */
  69. return IPV6_SCOPE_UNIQUELOCAL;
  70. switch(w & 0xFFC0) {
  71. case 0xFE80:
  72. return IPV6_SCOPE_LINKLOCAL;
  73. case 0xFEC0:
  74. return IPV6_SCOPE_SITELOCAL;
  75. case 0x0000:
  76. w = b[1] | b[2] | b[3] | b[4] | b[5] | b[6] | b[7] | b[8] | b[9] |
  77. b[10] | b[11] | b[12] | b[13] | b[14];
  78. if(w || b[15] != 0x01)
  79. break;
  80. return IPV6_SCOPE_NODELOCAL;
  81. default:
  82. break;
  83. }
  84. }
  85. #endif
  86. return IPV6_SCOPE_GLOBAL;
  87. }
  88. #if defined(HAVE_GETIFADDRS)
  89. if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
  90. unsigned int local_scope_id, const char *interf,
  91. char *buf, int buf_size)
  92. {
  93. struct ifaddrs *iface, *head;
  94. if2ip_result_t res = IF2IP_NOT_FOUND;
  95. #ifndef ENABLE_IPV6
  96. (void) remote_scope;
  97. #endif
  98. #if !defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID) || \
  99. !defined(ENABLE_IPV6)
  100. (void) local_scope_id;
  101. #endif
  102. if(getifaddrs(&head) >= 0) {
  103. for(iface = head; iface != NULL; iface = iface->ifa_next) {
  104. if(iface->ifa_addr != NULL) {
  105. if(iface->ifa_addr->sa_family == af) {
  106. if(strcasecompare(iface->ifa_name, interf)) {
  107. void *addr;
  108. const char *ip;
  109. char scope[12] = "";
  110. char ipstr[64];
  111. #ifdef ENABLE_IPV6
  112. if(af == AF_INET6) {
  113. #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
  114. unsigned int scopeid = 0;
  115. #endif
  116. unsigned int ifscope = Curl_ipv6_scope(iface->ifa_addr);
  117. if(ifscope != remote_scope) {
  118. /* We are interested only in interface addresses whose scope
  119. matches the remote address we want to connect to: global
  120. for global, link-local for link-local, etc... */
  121. if(res == IF2IP_NOT_FOUND)
  122. res = IF2IP_AF_NOT_SUPPORTED;
  123. continue;
  124. }
  125. addr =
  126. &((struct sockaddr_in6 *)(void *)iface->ifa_addr)->sin6_addr;
  127. #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
  128. /* Include the scope of this interface as part of the address */
  129. scopeid = ((struct sockaddr_in6 *)(void *)iface->ifa_addr)
  130. ->sin6_scope_id;
  131. /* If given, scope id should match. */
  132. if(local_scope_id && scopeid != local_scope_id) {
  133. if(res == IF2IP_NOT_FOUND)
  134. res = IF2IP_AF_NOT_SUPPORTED;
  135. continue;
  136. }
  137. if(scopeid)
  138. msnprintf(scope, sizeof(scope), "%%%u", scopeid);
  139. #endif
  140. }
  141. else
  142. #endif
  143. addr =
  144. &((struct sockaddr_in *)(void *)iface->ifa_addr)->sin_addr;
  145. res = IF2IP_FOUND;
  146. ip = Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr));
  147. msnprintf(buf, buf_size, "%s%s", ip, scope);
  148. break;
  149. }
  150. }
  151. else if((res == IF2IP_NOT_FOUND) &&
  152. strcasecompare(iface->ifa_name, interf)) {
  153. res = IF2IP_AF_NOT_SUPPORTED;
  154. }
  155. }
  156. }
  157. freeifaddrs(head);
  158. }
  159. return res;
  160. }
  161. #elif defined(HAVE_IOCTL_SIOCGIFADDR)
  162. if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
  163. unsigned int local_scope_id, const char *interf,
  164. char *buf, int buf_size)
  165. {
  166. struct ifreq req;
  167. struct in_addr in;
  168. struct sockaddr_in *s;
  169. curl_socket_t dummy;
  170. size_t len;
  171. const char *r;
  172. (void)remote_scope;
  173. (void)local_scope_id;
  174. if(!interf || (af != AF_INET))
  175. return IF2IP_NOT_FOUND;
  176. len = strlen(interf);
  177. if(len >= sizeof(req.ifr_name))
  178. return IF2IP_NOT_FOUND;
  179. dummy = socket(AF_INET, SOCK_STREAM, 0);
  180. if(CURL_SOCKET_BAD == dummy)
  181. return IF2IP_NOT_FOUND;
  182. memset(&req, 0, sizeof(req));
  183. memcpy(req.ifr_name, interf, len + 1);
  184. req.ifr_addr.sa_family = AF_INET;
  185. if(ioctl(dummy, SIOCGIFADDR, &req) < 0) {
  186. sclose(dummy);
  187. /* With SIOCGIFADDR, we cannot tell the difference between an interface
  188. that does not exist and an interface that has no address of the
  189. correct family. Assume the interface does not exist */
  190. return IF2IP_NOT_FOUND;
  191. }
  192. s = (struct sockaddr_in *)(void *)&req.ifr_addr;
  193. memcpy(&in, &s->sin_addr, sizeof(in));
  194. r = Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
  195. sclose(dummy);
  196. if(!r)
  197. return IF2IP_NOT_FOUND;
  198. return IF2IP_FOUND;
  199. }
  200. #else
  201. if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
  202. unsigned int local_scope_id, const char *interf,
  203. char *buf, int buf_size)
  204. {
  205. (void) af;
  206. (void) remote_scope;
  207. (void) local_scope_id;
  208. (void) interf;
  209. (void) buf;
  210. (void) buf_size;
  211. return IF2IP_NOT_FOUND;
  212. }
  213. #endif