if2ip.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifdef HAVE_UNISTD_H
  25. # include <unistd.h>
  26. #endif
  27. #ifdef HAVE_SYS_SOCKET_H
  28. # include <sys/socket.h>
  29. #endif
  30. #ifdef HAVE_NETINET_IN_H
  31. # include <netinet/in.h>
  32. #endif
  33. #ifdef HAVE_ARPA_INET_H
  34. # include <arpa/inet.h>
  35. #endif
  36. #ifdef HAVE_NET_IF_H
  37. # include <net/if.h>
  38. #endif
  39. #ifdef HAVE_SYS_IOCTL_H
  40. # include <sys/ioctl.h>
  41. #endif
  42. #ifdef HAVE_NETDB_H
  43. # include <netdb.h>
  44. #endif
  45. #ifdef HAVE_SYS_SOCKIO_H
  46. # include <sys/sockio.h>
  47. #endif
  48. #ifdef HAVE_IFADDRS_H
  49. # include <ifaddrs.h>
  50. #endif
  51. #ifdef HAVE_STROPTS_H
  52. # include <stropts.h>
  53. #endif
  54. #ifdef __VMS
  55. # include <inet.h>
  56. #endif
  57. #include "inet_ntop.h"
  58. #include "strequal.h"
  59. #include "if2ip.h"
  60. #define _MPRINTF_REPLACE /* use our functions only */
  61. #include <curl/mprintf.h>
  62. #include "curl_memory.h"
  63. /* The last #include file should be: */
  64. #include "memdebug.h"
  65. /* ------------------------------------------------------------------ */
  66. #if defined(HAVE_GETIFADDRS)
  67. char *Curl_if2ip(int af, const char *interface, char *buf, int buf_size)
  68. {
  69. struct ifaddrs *iface, *head;
  70. char *ip=NULL;
  71. if (getifaddrs(&head) >= 0) {
  72. for (iface=head; iface != NULL; iface=iface->ifa_next) {
  73. if ((iface->ifa_addr != NULL) &&
  74. (iface->ifa_addr->sa_family == af) &&
  75. curl_strequal(iface->ifa_name, interface)) {
  76. void *addr;
  77. char scope[12]="";
  78. #ifdef ENABLE_IPV6
  79. if (af == AF_INET6) {
  80. unsigned int scopeid = 0;
  81. addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
  82. #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
  83. /* Include the scope of this interface as part of the address */
  84. scopeid = ((struct sockaddr_in6 *)iface->ifa_addr)->sin6_scope_id;
  85. #endif
  86. if (scopeid)
  87. snprintf(scope, sizeof(scope), "%%%u", scopeid);
  88. }
  89. else
  90. #endif
  91. addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
  92. ip = (char *) Curl_inet_ntop(af, addr, buf, buf_size);
  93. strlcat(buf, scope, buf_size);
  94. break;
  95. }
  96. }
  97. freeifaddrs(head);
  98. }
  99. return ip;
  100. }
  101. #elif defined(HAVE_IOCTL_SIOCGIFADDR)
  102. char *Curl_if2ip(int af, const char *interface, char *buf, int buf_size)
  103. {
  104. struct ifreq req;
  105. struct in_addr in;
  106. struct sockaddr_in *s;
  107. curl_socket_t dummy;
  108. size_t len;
  109. char *ip;
  110. if(!interface || (af != AF_INET))
  111. return NULL;
  112. len = strlen(interface);
  113. if(len >= sizeof(req.ifr_name))
  114. return NULL;
  115. dummy = socket(AF_INET, SOCK_STREAM, 0);
  116. if(CURL_SOCKET_BAD == dummy)
  117. return NULL;
  118. memset(&req, 0, sizeof(req));
  119. memcpy(req.ifr_name, interface, len+1);
  120. req.ifr_addr.sa_family = AF_INET;
  121. if(ioctl(dummy, SIOCGIFADDR, &req) < 0) {
  122. sclose(dummy);
  123. return NULL;
  124. }
  125. s = (struct sockaddr_in *)&req.ifr_addr;
  126. memcpy(&in, &s->sin_addr, sizeof(in));
  127. ip = (char *) Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
  128. sclose(dummy);
  129. return ip;
  130. }
  131. #else
  132. char *Curl_if2ip(int af, const char *interf, char *buf, int buf_size)
  133. {
  134. (void) af;
  135. (void) interf;
  136. (void) buf;
  137. (void) buf_size;
  138. return NULL;
  139. }
  140. #endif