2022-02-19 19:03:30 +00:00
|
|
|
|
/* $Id: getifaddr.c,v 1.28 2022/02/19 18:58:25 nanard Exp $ */
|
2018-07-06 11:20:30 +00:00
|
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
|
* MiniUPnP project
|
|
|
|
|
* http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
|
2023-11-14 20:15:58 +00:00
|
|
|
|
* (c) 2006-2023 Thomas Bernard
|
2011-09-28 19:13:20 +00:00
|
|
|
|
* This software is subject to the conditions detailed
|
|
|
|
|
* in the LICENCE file provided within the distribution */
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <syslog.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <net/if.h>
|
|
|
|
|
#include <netinet/in.h>
|
2012-01-20 21:55:43 +00:00
|
|
|
|
#include <arpa/inet.h>
|
2011-09-28 19:13:20 +00:00
|
|
|
|
#if defined(sun)
|
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#include "getifaddr.h"
|
2013-07-09 13:36:53 +00:00
|
|
|
|
#if defined(USE_GETIFADDRS) || defined(ENABLE_IPV6) || defined(ENABLE_PCP)
|
2011-09-28 19:13:20 +00:00
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
int
|
2013-03-23 10:50:57 +00:00
|
|
|
|
getifaddr(const char * ifname, char * buf, int len,
|
|
|
|
|
struct in_addr * addr, struct in_addr * mask)
|
2011-09-28 19:13:20 +00:00
|
|
|
|
{
|
|
|
|
|
#ifndef USE_GETIFADDRS
|
|
|
|
|
/* use ioctl SIOCGIFADDR. Works only for ip v4 */
|
|
|
|
|
/* SIOCGIFADDR struct ifreq * */
|
|
|
|
|
int s;
|
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
int ifrlen;
|
2013-03-23 10:50:57 +00:00
|
|
|
|
struct sockaddr_in * ifaddr;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
ifrlen = sizeof(ifr);
|
|
|
|
|
|
|
|
|
|
if(!ifname || ifname[0]=='\0')
|
|
|
|
|
return -1;
|
|
|
|
|
s = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
if(s < 0)
|
|
|
|
|
{
|
|
|
|
|
syslog(LOG_ERR, "socket(PF_INET, SOCK_DGRAM): %m");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2019-05-20 19:55:17 +00:00
|
|
|
|
strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1);
|
2015-06-24 05:30:41 +00:00
|
|
|
|
ifr.ifr_name[IFNAMSIZ-1] = '\0';
|
2014-04-10 22:03:13 +00:00
|
|
|
|
if(ioctl(s, SIOCGIFFLAGS, &ifr, &ifrlen) < 0)
|
|
|
|
|
{
|
|
|
|
|
syslog(LOG_DEBUG, "ioctl(s, SIOCGIFFLAGS, ...): %m");
|
|
|
|
|
close(s);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if ((ifr.ifr_flags & IFF_UP) == 0)
|
|
|
|
|
{
|
|
|
|
|
syslog(LOG_DEBUG, "network interface %s is down", ifname);
|
|
|
|
|
close(s);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2019-05-20 19:55:17 +00:00
|
|
|
|
strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1);
|
|
|
|
|
ifr.ifr_name[IFNAMSIZ-1] = '\0';
|
2011-09-28 19:13:20 +00:00
|
|
|
|
if(ioctl(s, SIOCGIFADDR, &ifr, &ifrlen) < 0)
|
|
|
|
|
{
|
|
|
|
|
syslog(LOG_ERR, "ioctl(s, SIOCGIFADDR, ...): %m");
|
|
|
|
|
close(s);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2013-03-23 10:50:57 +00:00
|
|
|
|
ifaddr = (struct sockaddr_in *)&ifr.ifr_addr;
|
|
|
|
|
if(addr) *addr = ifaddr->sin_addr;
|
2014-03-14 09:04:49 +00:00
|
|
|
|
if(buf)
|
2011-09-28 19:13:20 +00:00
|
|
|
|
{
|
2014-03-14 09:04:49 +00:00
|
|
|
|
if(!inet_ntop(AF_INET, &ifaddr->sin_addr, buf, len))
|
|
|
|
|
{
|
|
|
|
|
syslog(LOG_ERR, "inet_ntop(): %m");
|
|
|
|
|
close(s);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
|
}
|
2013-03-23 10:50:57 +00:00
|
|
|
|
if(mask)
|
|
|
|
|
{
|
2019-05-20 19:55:17 +00:00
|
|
|
|
strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1);
|
|
|
|
|
ifr.ifr_name[IFNAMSIZ-1] = '\0';
|
2013-03-23 10:50:57 +00:00
|
|
|
|
if(ioctl(s, SIOCGIFNETMASK, &ifr, &ifrlen) < 0)
|
|
|
|
|
{
|
|
|
|
|
syslog(LOG_ERR, "ioctl(s, SIOCGIFNETMASK, ...): %m");
|
|
|
|
|
close(s);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2013-04-27 15:51:53 +00:00
|
|
|
|
#ifdef ifr_netmask
|
2013-03-23 10:50:57 +00:00
|
|
|
|
*mask = ((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr;
|
2013-04-27 15:51:53 +00:00
|
|
|
|
#else
|
|
|
|
|
*mask = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
|
|
|
|
|
#endif
|
2013-03-23 10:50:57 +00:00
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
|
close(s);
|
|
|
|
|
#else /* ifndef USE_GETIFADDRS */
|
|
|
|
|
/* Works for all address families (both ip v4 and ip v6) */
|
|
|
|
|
struct ifaddrs * ifap;
|
|
|
|
|
struct ifaddrs * ife;
|
2022-02-16 16:58:42 +00:00
|
|
|
|
struct ifaddrs * candidate = NULL;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
|
|
if(!ifname || ifname[0]=='\0')
|
|
|
|
|
return -1;
|
|
|
|
|
if(getifaddrs(&ifap)<0)
|
|
|
|
|
{
|
|
|
|
|
syslog(LOG_ERR, "getifaddrs: %m");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
for(ife = ifap; ife; ife = ife->ifa_next)
|
|
|
|
|
{
|
2012-01-02 10:12:52 +00:00
|
|
|
|
/* skip other interfaces if one was specified */
|
|
|
|
|
if(ifname && (0 != strcmp(ifname, ife->ifa_name)))
|
|
|
|
|
continue;
|
|
|
|
|
if(ife->ifa_addr == NULL)
|
2011-09-28 19:13:20 +00:00
|
|
|
|
continue;
|
|
|
|
|
switch(ife->ifa_addr->sa_family)
|
|
|
|
|
{
|
|
|
|
|
case AF_INET:
|
2022-02-19 19:03:30 +00:00
|
|
|
|
/* only consider the address if it is the 1st candidate or
|
|
|
|
|
if it is not privante AND the current candidate is a private address.
|
|
|
|
|
So we return a private address only if there is no public address
|
|
|
|
|
on this interface */
|
2022-02-16 16:58:42 +00:00
|
|
|
|
if(!candidate ||
|
|
|
|
|
(addr_is_reserved(&((struct sockaddr_in *)candidate->ifa_addr)->sin_addr) &&
|
|
|
|
|
!addr_is_reserved(&((struct sockaddr_in *)ife->ifa_addr)->sin_addr)))
|
|
|
|
|
candidate = ife;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
break;
|
|
|
|
|
/*
|
|
|
|
|
case AF_INET6:
|
|
|
|
|
inet_ntop(ife->ifa_addr->sa_family,
|
|
|
|
|
&((struct sockaddr_in6 *)ife->ifa_addr)->sin6_addr,
|
|
|
|
|
buf, len);
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-16 16:58:42 +00:00
|
|
|
|
if(candidate)
|
|
|
|
|
{
|
|
|
|
|
if(buf)
|
|
|
|
|
{
|
|
|
|
|
inet_ntop(candidate->ifa_addr->sa_family,
|
|
|
|
|
&((struct sockaddr_in *)candidate->ifa_addr)->sin_addr,
|
|
|
|
|
buf, len);
|
|
|
|
|
}
|
|
|
|
|
if(addr) *addr = ((struct sockaddr_in *)candidate->ifa_addr)->sin_addr;
|
|
|
|
|
if(mask) *mask = ((struct sockaddr_in *)candidate->ifa_netmask)->sin_addr;
|
|
|
|
|
}
|
2023-11-14 20:15:58 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
syslog(LOG_WARNING, "no AF_INET address found for %s", ifname);
|
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2011-09-28 19:13:20 +00:00
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
|
#endif
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-09 13:36:53 +00:00
|
|
|
|
#ifdef ENABLE_PCP
|
2014-05-03 07:13:25 +00:00
|
|
|
|
|
|
|
|
|
int getifaddr_in6(const char * ifname, int af, struct in6_addr * addr)
|
2014-03-13 09:42:07 +00:00
|
|
|
|
{
|
2014-03-14 09:04:49 +00:00
|
|
|
|
#if defined(ENABLE_IPV6) || defined(USE_GETIFADDRS)
|
2013-07-09 13:36:53 +00:00
|
|
|
|
struct ifaddrs * ifap;
|
|
|
|
|
struct ifaddrs * ife;
|
2014-03-13 09:42:07 +00:00
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
|
const struct sockaddr_in6 * tmpaddr;
|
|
|
|
|
#endif /* ENABLE_IPV6 */
|
2014-03-13 08:56:34 +00:00
|
|
|
|
int found = 0;
|
2013-07-09 13:36:53 +00:00
|
|
|
|
|
|
|
|
|
if(!ifname || ifname[0]=='\0')
|
|
|
|
|
return -1;
|
|
|
|
|
if(getifaddrs(&ifap)<0)
|
|
|
|
|
{
|
|
|
|
|
syslog(LOG_ERR, "getifaddrs: %m");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2014-03-13 08:56:34 +00:00
|
|
|
|
for(ife = ifap; ife && !found; ife = ife->ifa_next)
|
2013-07-09 13:36:53 +00:00
|
|
|
|
{
|
|
|
|
|
/* skip other interfaces if one was specified */
|
|
|
|
|
if(ifname && (0 != strcmp(ifname, ife->ifa_name)))
|
|
|
|
|
continue;
|
|
|
|
|
if(ife->ifa_addr == NULL)
|
|
|
|
|
continue;
|
2014-05-03 07:13:25 +00:00
|
|
|
|
if (ife->ifa_addr->sa_family != af)
|
|
|
|
|
continue;
|
2013-07-09 13:36:53 +00:00
|
|
|
|
switch(ife->ifa_addr->sa_family)
|
|
|
|
|
{
|
|
|
|
|
case AF_INET:
|
2014-03-13 09:42:07 +00:00
|
|
|
|
/* IPv4-mapped IPv6 address ::ffff:1.2.3.4 */
|
2013-12-13 15:59:51 +00:00
|
|
|
|
memset(addr->s6_addr, 0, 10);
|
|
|
|
|
addr->s6_addr[10] = 0xff;
|
|
|
|
|
addr->s6_addr[11] = 0xff;
|
2014-03-13 09:42:07 +00:00
|
|
|
|
memcpy(addr->s6_addr + 12,
|
|
|
|
|
&(((struct sockaddr_in *)ife->ifa_addr)->sin_addr.s_addr),
|
|
|
|
|
4);
|
2013-07-09 13:36:53 +00:00
|
|
|
|
found = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
2014-03-13 09:00:42 +00:00
|
|
|
|
#ifdef ENABLE_IPV6
|
2013-07-09 13:36:53 +00:00
|
|
|
|
case AF_INET6:
|
2014-03-13 09:42:07 +00:00
|
|
|
|
tmpaddr = (const struct sockaddr_in6 *)ife->ifa_addr;
|
|
|
|
|
if(!IN6_IS_ADDR_LOOPBACK(&tmpaddr->sin6_addr)
|
|
|
|
|
&& !IN6_IS_ADDR_LINKLOCAL(&tmpaddr->sin6_addr))
|
|
|
|
|
{
|
|
|
|
|
memcpy(addr->s6_addr,
|
|
|
|
|
&tmpaddr->sin6_addr,
|
|
|
|
|
16);
|
2013-07-09 13:36:53 +00:00
|
|
|
|
found = 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2014-03-13 09:00:42 +00:00
|
|
|
|
#endif /* ENABLE_IPV6 */
|
2013-07-09 13:36:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
freeifaddrs(ifap);
|
2014-03-13 08:56:34 +00:00
|
|
|
|
return (found ? 0 : -1);
|
2014-03-14 09:04:49 +00:00
|
|
|
|
#else /* defined(ENABLE_IPV6) || defined(USE_GETIFADDRS) */
|
|
|
|
|
/* IPv4 only */
|
|
|
|
|
struct in_addr addr4;
|
2014-05-06 13:15:07 +00:00
|
|
|
|
if(af != AF_INET)
|
|
|
|
|
return -1;
|
2014-03-14 09:04:49 +00:00
|
|
|
|
if(getifaddr(ifname, NULL, 0, &addr4, NULL) < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
/* IPv4-mapped IPv6 address ::ffff:1.2.3.4 */
|
|
|
|
|
memset(addr->s6_addr, 0, 10);
|
|
|
|
|
addr->s6_addr[10] = 0xff;
|
|
|
|
|
addr->s6_addr[11] = 0xff;
|
|
|
|
|
memcpy(addr->s6_addr + 12, &addr4.s_addr, 4);
|
|
|
|
|
return 0;
|
|
|
|
|
#endif
|
2013-07-09 13:36:53 +00:00
|
|
|
|
}
|
2014-03-13 09:00:42 +00:00
|
|
|
|
#endif /* ENABLE_PCP */
|
2013-07-09 13:36:53 +00:00
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
|
int
|
|
|
|
|
find_ipv6_addr(const char * ifname,
|
|
|
|
|
char * dst, int n)
|
|
|
|
|
{
|
|
|
|
|
struct ifaddrs * ifap;
|
|
|
|
|
struct ifaddrs * ife;
|
|
|
|
|
const struct sockaddr_in6 * addr;
|
|
|
|
|
char buf[64];
|
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
|
|
if(!dst)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if(getifaddrs(&ifap)<0)
|
|
|
|
|
{
|
|
|
|
|
syslog(LOG_ERR, "getifaddrs: %m");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
for(ife = ifap; ife; ife = ife->ifa_next)
|
|
|
|
|
{
|
|
|
|
|
/* skip other interfaces if one was specified */
|
|
|
|
|
if(ifname && (0 != strcmp(ifname, ife->ifa_name)))
|
|
|
|
|
continue;
|
2012-01-02 10:12:52 +00:00
|
|
|
|
if(ife->ifa_addr == NULL)
|
|
|
|
|
continue;
|
2011-09-28 19:13:20 +00:00
|
|
|
|
if(ife->ifa_addr->sa_family == AF_INET6)
|
|
|
|
|
{
|
|
|
|
|
addr = (const struct sockaddr_in6 *)ife->ifa_addr;
|
|
|
|
|
if(!IN6_IS_ADDR_LOOPBACK(&addr->sin6_addr)
|
|
|
|
|
&& !IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr))
|
|
|
|
|
{
|
|
|
|
|
inet_ntop(ife->ifa_addr->sa_family,
|
|
|
|
|
&addr->sin6_addr,
|
|
|
|
|
buf, sizeof(buf));
|
|
|
|
|
/* add brackets */
|
|
|
|
|
snprintf(dst, n, "[%s]", buf);
|
|
|
|
|
r = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-08 23:33:06 +00:00
|
|
|
|
freeifaddrs(ifap);
|
2011-09-28 19:13:20 +00:00
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-05-19 11:31:14 +00:00
|
|
|
|
/* List of IP address blocks which are private / reserved and therefore not suitable for public external IP addresses */
|
|
|
|
|
/* If interface has IP address from one of this block, then it is either behind NAT or port forwarding is impossible */
|
|
|
|
|
#define IP(a, b, c, d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
|
|
|
|
|
#define MSK(m) (32-(m))
|
2018-07-06 11:20:30 +00:00
|
|
|
|
static const struct { uint32_t address; uint32_t rmask; } reserved[] = {
|
2018-05-19 11:31:14 +00:00
|
|
|
|
{ IP( 0, 0, 0, 0), MSK( 8) }, /* RFC1122 "This host on this network" */
|
|
|
|
|
{ IP( 10, 0, 0, 0), MSK( 8) }, /* RFC1918 Private-Use */
|
|
|
|
|
{ IP(100, 64, 0, 0), MSK(10) }, /* RFC6598 Shared Address Space */
|
|
|
|
|
{ IP(127, 0, 0, 0), MSK( 8) }, /* RFC1122 Loopback */
|
|
|
|
|
{ IP(169, 254, 0, 0), MSK(16) }, /* RFC3927 Link-Local */
|
|
|
|
|
{ IP(172, 16, 0, 0), MSK(12) }, /* RFC1918 Private-Use */
|
|
|
|
|
{ IP(192, 0, 0, 0), MSK(24) }, /* RFC6890 IETF Protocol Assignments */
|
|
|
|
|
{ IP(192, 0, 2, 0), MSK(24) }, /* RFC5737 Documentation (TEST-NET-1) */
|
|
|
|
|
{ IP(192, 31, 196, 0), MSK(24) }, /* RFC7535 AS112-v4 */
|
|
|
|
|
{ IP(192, 52, 193, 0), MSK(24) }, /* RFC7450 AMT */
|
|
|
|
|
{ IP(192, 88, 99, 0), MSK(24) }, /* RFC7526 6to4 Relay Anycast */
|
|
|
|
|
{ IP(192, 168, 0, 0), MSK(16) }, /* RFC1918 Private-Use */
|
2020-07-12 11:45:30 +00:00
|
|
|
|
{ IP(192, 175, 48, 0), MSK(24) }, /* RFC7534 Direct Delegation AS112 Service */
|
2018-05-19 11:31:14 +00:00
|
|
|
|
{ IP(198, 18, 0, 0), MSK(15) }, /* RFC2544 Benchmarking */
|
|
|
|
|
{ IP(198, 51, 100, 0), MSK(24) }, /* RFC5737 Documentation (TEST-NET-2) */
|
|
|
|
|
{ IP(203, 0, 113, 0), MSK(24) }, /* RFC5737 Documentation (TEST-NET-3) */
|
|
|
|
|
{ IP(224, 0, 0, 0), MSK( 4) }, /* RFC1112 Multicast */
|
|
|
|
|
{ IP(240, 0, 0, 0), MSK( 4) }, /* RFC1112 Reserved for Future Use + RFC919 Limited Broadcast */
|
|
|
|
|
};
|
|
|
|
|
#undef IP
|
|
|
|
|
#undef MSK
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
addr_is_reserved(struct in_addr * addr)
|
|
|
|
|
{
|
|
|
|
|
uint32_t address = ntohl(addr->s_addr);
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(reserved)/sizeof(reserved[0]); ++i) {
|
|
|
|
|
if ((address >> reserved[i].rmask) == (reserved[i].address >> reserved[i].rmask))
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|