PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Externals/curl/lib/if2ip.c

https://gitlab.com/WoomyNightClub/dolphin
C | 272 lines | 198 code | 40 blank | 34 comment | 39 complexity | 4dcba01d160d47d0bd9c0a18381204a1 MD5 | raw file
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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 "strequal.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. switch(w & 0xFFC0) {
  69. case 0xFE80:
  70. return IPV6_SCOPE_LINKLOCAL;
  71. case 0xFEC0:
  72. return IPV6_SCOPE_SITELOCAL;
  73. case 0x0000:
  74. w = b[1] | b[2] | b[3] | b[4] | b[5] | b[6] | b[7] | b[8] | b[9] |
  75. b[10] | b[11] | b[12] | b[13] | b[14];
  76. if(w || b[15] != 0x01)
  77. break;
  78. return IPV6_SCOPE_NODELOCAL;
  79. default:
  80. break;
  81. }
  82. }
  83. #endif
  84. return IPV6_SCOPE_GLOBAL;
  85. }
  86. #if defined(HAVE_GETIFADDRS)
  87. bool Curl_if_is_interface_name(const char *interf)
  88. {
  89. bool result = FALSE;
  90. struct ifaddrs *iface, *head;
  91. if(getifaddrs(&head) >= 0) {
  92. for(iface=head; iface != NULL; iface=iface->ifa_next) {
  93. if(curl_strequal(iface->ifa_name, interf)) {
  94. result = TRUE;
  95. break;
  96. }
  97. }
  98. freeifaddrs(head);
  99. }
  100. return result;
  101. }
  102. if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
  103. unsigned int remote_scope_id, const char *interf,
  104. char *buf, int buf_size)
  105. {
  106. struct ifaddrs *iface, *head;
  107. if2ip_result_t res = IF2IP_NOT_FOUND;
  108. #ifndef ENABLE_IPV6
  109. (void) remote_scope;
  110. #ifndef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
  111. (void) remote_scope_id;
  112. #endif
  113. #endif
  114. if(getifaddrs(&head) >= 0) {
  115. for(iface = head; iface != NULL; iface=iface->ifa_next) {
  116. if(iface->ifa_addr != NULL) {
  117. if(iface->ifa_addr->sa_family == af) {
  118. if(curl_strequal(iface->ifa_name, interf)) {
  119. void *addr;
  120. char *ip;
  121. char scope[12] = "";
  122. char ipstr[64];
  123. #ifdef ENABLE_IPV6
  124. if(af == AF_INET6) {
  125. unsigned int scopeid = 0;
  126. unsigned int ifscope = Curl_ipv6_scope(iface->ifa_addr);
  127. if(ifscope != remote_scope) {
  128. /* We are interested only in interface addresses whose
  129. scope matches the remote address we want to
  130. connect to: global for global, link-local for
  131. link-local, etc... */
  132. if(res == IF2IP_NOT_FOUND) res = IF2IP_AF_NOT_SUPPORTED;
  133. continue;
  134. }
  135. addr =
  136. &((struct sockaddr_in6 *)(void *)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 = ((struct sockaddr_in6 *)(void *)iface->ifa_addr)
  140. ->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 =
  154. &((struct sockaddr_in *)(void *)iface->ifa_addr)->sin_addr;
  155. res = IF2IP_FOUND;
  156. ip = (char *) Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr));
  157. snprintf(buf, buf_size, "%s%s", ip, scope);
  158. break;
  159. }
  160. }
  161. else if((res == IF2IP_NOT_FOUND) &&
  162. curl_strequal(iface->ifa_name, interf)) {
  163. res = IF2IP_AF_NOT_SUPPORTED;
  164. }
  165. }
  166. }
  167. freeifaddrs(head);
  168. }
  169. return res;
  170. }
  171. #elif defined(HAVE_IOCTL_SIOCGIFADDR)
  172. bool Curl_if_is_interface_name(const char *interf)
  173. {
  174. /* This is here just to support the old interfaces */
  175. char buf[256];
  176. return (Curl_if2ip(AF_INET, 0 /* unused */, 0, interf, buf, sizeof(buf)) ==
  177. IF2IP_NOT_FOUND) ? FALSE : TRUE;
  178. }
  179. if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
  180. unsigned int remote_scope_id, const char *interf,
  181. char *buf, int buf_size)
  182. {
  183. struct ifreq req;
  184. struct in_addr in;
  185. struct sockaddr_in *s;
  186. curl_socket_t dummy;
  187. size_t len;
  188. (void)remote_scope;
  189. (void)remote_scope_id;
  190. if(!interf || (af != AF_INET))
  191. return IF2IP_NOT_FOUND;
  192. len = strlen(interf);
  193. if(len >= sizeof(req.ifr_name))
  194. return IF2IP_NOT_FOUND;
  195. dummy = socket(AF_INET, SOCK_STREAM, 0);
  196. if(CURL_SOCKET_BAD == dummy)
  197. return IF2IP_NOT_FOUND;
  198. memset(&req, 0, sizeof(req));
  199. memcpy(req.ifr_name, interf, len+1);
  200. req.ifr_addr.sa_family = AF_INET;
  201. if(ioctl(dummy, SIOCGIFADDR, &req) < 0) {
  202. sclose(dummy);
  203. /* With SIOCGIFADDR, we cannot tell the difference between an interface
  204. that does not exist and an interface that has no address of the
  205. correct family. Assume the interface does not exist */
  206. return IF2IP_NOT_FOUND;
  207. }
  208. s = (struct sockaddr_in *)&req.ifr_addr;
  209. memcpy(&in, &s->sin_addr, sizeof(in));
  210. Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
  211. sclose(dummy);
  212. return IF2IP_FOUND;
  213. }
  214. #else
  215. bool Curl_if_is_interface_name(const char *interf)
  216. {
  217. (void) interf;
  218. return FALSE;
  219. }
  220. if2ip_result_t Curl_if2ip(int af, unsigned int remote_scope,
  221. unsigned int remote_scope_id, const char *interf,
  222. char *buf, int buf_size)
  223. {
  224. (void) af;
  225. (void) remote_scope;
  226. (void) remote_scope_id;
  227. (void) interf;
  228. (void) buf;
  229. (void) buf_size;
  230. return IF2IP_NOT_FOUND;
  231. }
  232. #endif