if2ip.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, 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. #if defined(HAVE_GETIFADDRS)
  60. bool Curl_if_is_interface_name(const char *interf)
  61. {
  62. bool result = FALSE;
  63. struct ifaddrs *iface, *head;
  64. if(getifaddrs(&head) >= 0) {
  65. for(iface=head; iface != NULL; iface=iface->ifa_next) {
  66. if(curl_strequal(iface->ifa_name, interf)) {
  67. result = TRUE;
  68. break;
  69. }
  70. }
  71. freeifaddrs(head);
  72. }
  73. return result;
  74. }
  75. if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
  76. const char *interf, char *buf, int buf_size)
  77. {
  78. struct ifaddrs *iface, *head;
  79. if2ip_result_t res = IF2IP_NOT_FOUND;
  80. #ifndef ENABLE_IPV6
  81. (void) remote_scope;
  82. #endif
  83. if(getifaddrs(&head) >= 0) {
  84. for(iface=head; iface != NULL; iface=iface->ifa_next) {
  85. if(iface->ifa_addr != NULL) {
  86. if(iface->ifa_addr->sa_family == af) {
  87. if(curl_strequal(iface->ifa_name, interf)) {
  88. void *addr;
  89. char *ip;
  90. char scope[12]="";
  91. char ipstr[64];
  92. #ifdef ENABLE_IPV6
  93. if(af == AF_INET6) {
  94. unsigned int scopeid = 0;
  95. addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
  96. #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
  97. /* Include the scope of this interface as part of the address */
  98. scopeid =
  99. ((struct sockaddr_in6 *)iface->ifa_addr)->sin6_scope_id;
  100. #endif
  101. if(scopeid != remote_scope) {
  102. /* We are interested only in interface addresses whose
  103. scope ID matches the remote address we want to
  104. connect to: global (0) for global, link-local for
  105. link-local, etc... */
  106. if(res == IF2IP_NOT_FOUND) res = IF2IP_AF_NOT_SUPPORTED;
  107. continue;
  108. }
  109. if(scopeid)
  110. snprintf(scope, sizeof(scope), "%%%u", scopeid);
  111. }
  112. else
  113. #endif
  114. addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
  115. res = IF2IP_FOUND;
  116. ip = (char *) Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr));
  117. snprintf(buf, buf_size, "%s%s", ip, scope);
  118. break;
  119. }
  120. }
  121. else if((res == IF2IP_NOT_FOUND) &&
  122. curl_strequal(iface->ifa_name, interf)) {
  123. res = IF2IP_AF_NOT_SUPPORTED;
  124. }
  125. }
  126. }
  127. freeifaddrs(head);
  128. }
  129. return res;
  130. }
  131. #elif defined(HAVE_IOCTL_SIOCGIFADDR)
  132. bool Curl_if_is_interface_name(const char *interf)
  133. {
  134. /* This is here just to support the old interfaces */
  135. char buf[256];
  136. return (Curl_if2ip(AF_INET, 0, interf, buf, sizeof(buf)) ==
  137. IF2IP_NOT_FOUND) ? FALSE : TRUE;
  138. }
  139. if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
  140. const char *interf, char *buf, int buf_size)
  141. {
  142. struct ifreq req;
  143. struct in_addr in;
  144. struct sockaddr_in *s;
  145. curl_socket_t dummy;
  146. size_t len;
  147. (void)remote_scope;
  148. if(!interf || (af != AF_INET))
  149. return IF2IP_NOT_FOUND;
  150. len = strlen(interf);
  151. if(len >= sizeof(req.ifr_name))
  152. return IF2IP_NOT_FOUND;
  153. dummy = socket(AF_INET, SOCK_STREAM, 0);
  154. if(CURL_SOCKET_BAD == dummy)
  155. return IF2IP_NOT_FOUND;
  156. memset(&req, 0, sizeof(req));
  157. memcpy(req.ifr_name, interf, len+1);
  158. req.ifr_addr.sa_family = AF_INET;
  159. if(ioctl(dummy, SIOCGIFADDR, &req) < 0) {
  160. sclose(dummy);
  161. /* With SIOCGIFADDR, we cannot tell the difference between an interface
  162. that does not exist and an interface that has no address of the
  163. correct family. Assume the interface does not exist */
  164. return IF2IP_NOT_FOUND;
  165. }
  166. s = (struct sockaddr_in *)&req.ifr_addr;
  167. memcpy(&in, &s->sin_addr, sizeof(in));
  168. Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
  169. sclose(dummy);
  170. return IF2IP_FOUND;
  171. }
  172. #else
  173. bool Curl_if_is_interface_name(const char *interf)
  174. {
  175. (void) interf;
  176. return FALSE;
  177. }
  178. if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
  179. const char *interf, char *buf, int buf_size)
  180. {
  181. (void) af;
  182. (void) remote_scope;
  183. (void) interf;
  184. (void) buf;
  185. (void) buf_size;
  186. return IF2IP_NOT_FOUND;
  187. }
  188. #endif