You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
5.9 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* $Id: getifaddr.c,v 1.24 2015/07/09 12:27:26 nanard Exp $ */
  2. /* MiniUPnP project
  3. * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
  4. * (c) 2006-2014 Thomas Bernard
  5. * This software is subject to the conditions detailed
  6. * in the LICENCE file provided within the distribution */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <syslog.h>
  10. #include <unistd.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <net/if.h>
  15. #include <netinet/in.h>
  16. #include <arpa/inet.h>
  17. #if defined(sun)
  18. #include <sys/sockio.h>
  19. #endif
  20. #include "config.h"
  21. #include "getifaddr.h"
  22. #if defined(USE_GETIFADDRS) || defined(ENABLE_IPV6) || defined(ENABLE_PCP)
  23. #include <ifaddrs.h>
  24. #endif
  25. int
  26. getifaddr(const char * ifname, char * buf, int len,
  27. struct in_addr * addr, struct in_addr * mask)
  28. {
  29. #ifndef USE_GETIFADDRS
  30. /* use ioctl SIOCGIFADDR. Works only for ip v4 */
  31. /* SIOCGIFADDR struct ifreq * */
  32. int s;
  33. struct ifreq ifr;
  34. int ifrlen;
  35. struct sockaddr_in * ifaddr;
  36. ifrlen = sizeof(ifr);
  37. if(!ifname || ifname[0]=='\0')
  38. return -1;
  39. s = socket(PF_INET, SOCK_DGRAM, 0);
  40. if(s < 0)
  41. {
  42. syslog(LOG_ERR, "socket(PF_INET, SOCK_DGRAM): %m");
  43. return -1;
  44. }
  45. strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  46. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  47. if(ioctl(s, SIOCGIFFLAGS, &ifr, &ifrlen) < 0)
  48. {
  49. syslog(LOG_DEBUG, "ioctl(s, SIOCGIFFLAGS, ...): %m");
  50. close(s);
  51. return -1;
  52. }
  53. if ((ifr.ifr_flags & IFF_UP) == 0)
  54. {
  55. syslog(LOG_DEBUG, "network interface %s is down", ifname);
  56. close(s);
  57. return -1;
  58. }
  59. strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  60. if(ioctl(s, SIOCGIFADDR, &ifr, &ifrlen) < 0)
  61. {
  62. syslog(LOG_ERR, "ioctl(s, SIOCGIFADDR, ...): %m");
  63. close(s);
  64. return -1;
  65. }
  66. ifaddr = (struct sockaddr_in *)&ifr.ifr_addr;
  67. if(addr) *addr = ifaddr->sin_addr;
  68. if(buf)
  69. {
  70. if(!inet_ntop(AF_INET, &ifaddr->sin_addr, buf, len))
  71. {
  72. syslog(LOG_ERR, "inet_ntop(): %m");
  73. close(s);
  74. return -1;
  75. }
  76. }
  77. if(mask)
  78. {
  79. strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  80. if(ioctl(s, SIOCGIFNETMASK, &ifr, &ifrlen) < 0)
  81. {
  82. syslog(LOG_ERR, "ioctl(s, SIOCGIFNETMASK, ...): %m");
  83. close(s);
  84. return -1;
  85. }
  86. #ifdef ifr_netmask
  87. *mask = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr;
  88. #else
  89. *mask = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
  90. #endif
  91. }
  92. close(s);
  93. #else /* ifndef USE_GETIFADDRS */
  94. /* Works for all address families (both ip v4 and ip v6) */
  95. struct ifaddrs * ifap;
  96. struct ifaddrs * ife;
  97. if(!ifname || ifname[0]=='\0')
  98. return -1;
  99. if(getifaddrs(&ifap)<0)
  100. {
  101. syslog(LOG_ERR, "getifaddrs: %m");
  102. return -1;
  103. }
  104. for(ife = ifap; ife; ife = ife->ifa_next)
  105. {
  106. /* skip other interfaces if one was specified */
  107. if(ifname && (0 != strcmp(ifname, ife->ifa_name)))
  108. continue;
  109. if(ife->ifa_addr == NULL)
  110. continue;
  111. switch(ife->ifa_addr->sa_family)
  112. {
  113. case AF_INET:
  114. if(buf)
  115. {
  116. inet_ntop(ife->ifa_addr->sa_family,
  117. &((struct sockaddr_in *)ife->ifa_addr)->sin_addr,
  118. buf, len);
  119. }
  120. if(addr) *addr = ((struct sockaddr_in *)ife->ifa_addr)->sin_addr;
  121. if(mask) *mask = ((struct sockaddr_in *)ife->ifa_netmask)->sin_addr;
  122. break;
  123. /*
  124. case AF_INET6:
  125. inet_ntop(ife->ifa_addr->sa_family,
  126. &((struct sockaddr_in6 *)ife->ifa_addr)->sin6_addr,
  127. buf, len);
  128. */
  129. }
  130. }
  131. freeifaddrs(ifap);
  132. #endif
  133. return 0;
  134. }
  135. #ifdef ENABLE_PCP
  136. int getifaddr_in6(const char * ifname, int af, struct in6_addr * addr)
  137. {
  138. #if defined(ENABLE_IPV6) || defined(USE_GETIFADDRS)
  139. struct ifaddrs * ifap;
  140. struct ifaddrs * ife;
  141. #ifdef ENABLE_IPV6
  142. const struct sockaddr_in6 * tmpaddr;
  143. #endif /* ENABLE_IPV6 */
  144. int found = 0;
  145. if(!ifname || ifname[0]=='\0')
  146. return -1;
  147. if(getifaddrs(&ifap)<0)
  148. {
  149. syslog(LOG_ERR, "getifaddrs: %m");
  150. return -1;
  151. }
  152. for(ife = ifap; ife && !found; ife = ife->ifa_next)
  153. {
  154. /* skip other interfaces if one was specified */
  155. if(ifname && (0 != strcmp(ifname, ife->ifa_name)))
  156. continue;
  157. if(ife->ifa_addr == NULL)
  158. continue;
  159. if (ife->ifa_addr->sa_family != af)
  160. continue;
  161. switch(ife->ifa_addr->sa_family)
  162. {
  163. case AF_INET:
  164. /* IPv4-mapped IPv6 address ::ffff:1.2.3.4 */
  165. memset(addr->s6_addr, 0, 10);
  166. addr->s6_addr[10] = 0xff;
  167. addr->s6_addr[11] = 0xff;
  168. memcpy(addr->s6_addr + 12,
  169. &(((struct sockaddr_in *)ife->ifa_addr)->sin_addr.s_addr),
  170. 4);
  171. found = 1;
  172. break;
  173. #ifdef ENABLE_IPV6
  174. case AF_INET6:
  175. tmpaddr = (const struct sockaddr_in6 *)ife->ifa_addr;
  176. if(!IN6_IS_ADDR_LOOPBACK(&tmpaddr->sin6_addr)
  177. && !IN6_IS_ADDR_LINKLOCAL(&tmpaddr->sin6_addr))
  178. {
  179. memcpy(addr->s6_addr,
  180. &tmpaddr->sin6_addr,
  181. 16);
  182. found = 1;
  183. }
  184. break;
  185. #endif /* ENABLE_IPV6 */
  186. }
  187. }
  188. freeifaddrs(ifap);
  189. return (found ? 0 : -1);
  190. #else /* defined(ENABLE_IPV6) || defined(USE_GETIFADDRS) */
  191. /* IPv4 only */
  192. struct in_addr addr4;
  193. if(af != AF_INET)
  194. return -1;
  195. if(getifaddr(ifname, NULL, 0, &addr4, NULL) < 0)
  196. return -1;
  197. /* IPv4-mapped IPv6 address ::ffff:1.2.3.4 */
  198. memset(addr->s6_addr, 0, 10);
  199. addr->s6_addr[10] = 0xff;
  200. addr->s6_addr[11] = 0xff;
  201. memcpy(addr->s6_addr + 12, &addr4.s_addr, 4);
  202. return 0;
  203. #endif
  204. }
  205. #endif /* ENABLE_PCP */
  206. #ifdef ENABLE_IPV6
  207. int
  208. find_ipv6_addr(const char * ifname,
  209. char * dst, int n)
  210. {
  211. struct ifaddrs * ifap;
  212. struct ifaddrs * ife;
  213. const struct sockaddr_in6 * addr;
  214. char buf[64];
  215. int r = 0;
  216. if(!dst)
  217. return -1;
  218. if(getifaddrs(&ifap)<0)
  219. {
  220. syslog(LOG_ERR, "getifaddrs: %m");
  221. return -1;
  222. }
  223. for(ife = ifap; ife; ife = ife->ifa_next)
  224. {
  225. /* skip other interfaces if one was specified */
  226. if(ifname && (0 != strcmp(ifname, ife->ifa_name)))
  227. continue;
  228. if(ife->ifa_addr == NULL)
  229. continue;
  230. if(ife->ifa_addr->sa_family == AF_INET6)
  231. {
  232. addr = (const struct sockaddr_in6 *)ife->ifa_addr;
  233. if(!IN6_IS_ADDR_LOOPBACK(&addr->sin6_addr)
  234. && !IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr))
  235. {
  236. inet_ntop(ife->ifa_addr->sa_family,
  237. &addr->sin6_addr,
  238. buf, sizeof(buf));
  239. /* add brackets */
  240. snprintf(dst, n, "[%s]", buf);
  241. r = 1;
  242. }
  243. }
  244. }
  245. freeifaddrs(ifap);
  246. return r;
  247. }
  248. #endif