if2ip.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 http://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 "strequal.h"
  52. #include "if2ip.h"
  53. #define _MPRINTF_REPLACE /* use our functions only */
  54. #include <curl/mprintf.h>
  55. #include "curl_memory.h"
  56. /* The last #include file should be: */
  57. #include "memdebug.h"
  58. /* ------------------------------------------------------------------ */
  59. /* Return the scope of the given address. */
  60. unsigned int Curl_ipv6_scope(const struct sockaddr *sa)
  61. {
  62. #ifndef ENABLE_IPV6
  63. (void) sa;
  64. #else
  65. if(sa->sa_family == AF_INET6) {
  66. const struct sockaddr_in6 * sa6 = (const struct sockaddr_in6 *) sa;
  67. const unsigned char * b = sa6->sin6_addr.s6_addr;
  68. unsigned short w = (unsigned short) ((b[0] << 8) | b[1]);
  69. switch(w & 0xFFC0) {
  70. case 0xFE80:
  71. return IPV6_SCOPE_LINKLOCAL;
  72. case 0xFEC0:
  73. return IPV6_SCOPE_SITELOCAL;
  74. case 0x0000:
  75. w = b[1] | b[2] | b[3] | b[4] | b[5] | b[6] | b[7] | b[8] | b[9] |
  76. b[10] | b[11] | b[12] | b[13] | b[14];
  77. if(w || b[15] != 0x01)
  78. break;
  79. return IPV6_SCOPE_NODELOCAL;
  80. default:
  81. break;
  82. }
  83. }
  84. #endif
  85. return IPV6_SCOPE_GLOBAL;
  86. }
  87. #if defined(HAVE_GETIFADDRS)
  88. bool Curl_if_is_interface_name(const char *interf)
  89. {
  90. bool result = FALSE;
  91. struct ifaddrs *iface, *head;
  92. if(getifaddrs(&head) >= 0) {
  93. for(iface=head; iface != NULL; iface=iface->ifa_next) {
  94. if(curl_strequal(iface->ifa_name, interf)) {
  95. result = TRUE;
  96. break;
  97. }
  98. }
  99. freeifaddrs(head);
  100. }
  101. return result;
  102. }
  103. if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
  104. unsigned int remote_scope_id, const char *interf,
  105. char *buf, int buf_size)
  106. {
  107. struct ifaddrs *iface, *head;
  108. if2ip_result_t res = IF2IP_NOT_FOUND;
  109. #ifndef ENABLE_IPV6
  110. (void) remote_scope;
  111. #ifndef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
  112. (void) remote_scope_id;
  113. #endif
  114. #endif
  115. if(getifaddrs(&head) >= 0) {
  116. for(iface = head; iface != NULL; iface=iface->ifa_next) {
  117. if(iface->ifa_addr != NULL) {
  118. if(iface->ifa_addr->sa_family == af) {
  119. if(curl_strequal(iface->ifa_name, interf)) {
  120. void *addr;
  121. char *ip;
  122. char scope[12] = "";
  123. char ipstr[64];
  124. #ifdef ENABLE_IPV6
  125. if(af == AF_INET6) {
  126. unsigned int scopeid = 0;
  127. unsigned int ifscope = Curl_ipv6_scope(iface->ifa_addr);
  128. if(ifscope != remote_scope) {
  129. /* We are interested only in interface addresses whose
  130. scope matches the remote address we want to
  131. connect to: global for global, link-local for
  132. link-local, etc... */
  133. if(res == IF2IP_NOT_FOUND) res = IF2IP_AF_NOT_SUPPORTED;
  134. continue;
  135. }
  136. addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
  137. #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
  138. /* Include the scope of this interface as part of the address */
  139. scopeid =
  140. ((struct sockaddr_in6 *)iface->ifa_addr)->sin6_scope_id;
  141. /* If given, scope id should match. */
  142. if(remote_scope_id && scopeid != remote_scope_id) {
  143. if(res == IF2IP_NOT_FOUND)
  144. res = IF2IP_AF_NOT_SUPPORTED;
  145. continue;
  146. }
  147. #endif
  148. if(scopeid)
  149. snprintf(scope, sizeof(scope), "%%%u", scopeid);
  150. }
  151. else
  152. #endif
  153. addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
  154. res = IF2IP_FOUND;
  155. ip = (char *) Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr));
  156. snprintf(buf, buf_size, "%s%s", ip, scope);
  157. break;
  158. }
  159. }
  160. else if((res == IF2IP_NOT_FOUND) &&
  161. curl_strequal(iface->ifa_name, interf)) {
  162. res = IF2IP_AF_NOT_SUPPORTED;
  163. }
  164. }
  165. }
  166. freeifaddrs(head);
  167. }
  168. return res;
  169. }
  170. #elif defined(HAVE_IOCTL_SIOCGIFADDR)
  171. bool Curl_if_is_interface_name(const char *interf)
  172. {
  173. /* This is here just to support the old interfaces */
  174. char buf[256];
  175. return (Curl_if2ip(AF_INET, 0 /* unused */, 0, interf, buf, sizeof(buf)) ==
  176. IF2IP_NOT_FOUND) ? FALSE : TRUE;
  177. }
  178. if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
  179. unsigned int remote_scope_id, const char *interf,
  180. char *buf, int buf_size)
  181. {
  182. struct ifreq req;
  183. struct in_addr in;
  184. struct sockaddr_in *s;
  185. curl_socket_t dummy;
  186. size_t len;
  187. (void)remote_scope;
  188. (void)remote_scope_id;
  189. if(!interf || (af != AF_INET))
  190. return IF2IP_NOT_FOUND;
  191. len = strlen(interf);
  192. if(len >= sizeof(req.ifr_name))
  193. return IF2IP_NOT_FOUND;
  194. dummy = socket(AF_INET, SOCK_STREAM, 0);
  195. if(CURL_SOCKET_BAD == dummy)
  196. return IF2IP_NOT_FOUND;
  197. memset(&req, 0, sizeof(req));
  198. memcpy(req.ifr_name, interf, len+1);
  199. req.ifr_addr.sa_family = AF_INET;
  200. if(ioctl(dummy, SIOCGIFADDR, &req) < 0) {
  201. sclose(dummy);
  202. /* With SIOCGIFADDR, we cannot tell the difference between an interface
  203. that does not exist and an interface that has no address of the
  204. correct family. Assume the interface does not exist */
  205. return IF2IP_NOT_FOUND;
  206. }
  207. s = (struct sockaddr_in *)&req.ifr_addr;
  208. memcpy(&in, &s->sin_addr, sizeof(in));
  209. Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
  210. sclose(dummy);
  211. return IF2IP_FOUND;
  212. }
  213. #else
  214. bool Curl_if_is_interface_name(const char *interf)
  215. {
  216. (void) interf;
  217. return FALSE;
  218. }
  219. if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
  220. unsigned int remote_scope_id, const char *interf,
  221. char *buf, int buf_size)
  222. {
  223. (void) af;
  224. (void) remote_scope;
  225. (void) remote_scope_id;
  226. (void) interf;
  227. (void) buf;
  228. (void) buf_size;
  229. return IF2IP_NOT_FOUND;
  230. }
  231. #endif