2015-11-18 08:52:54 +00:00
|
|
|
/* $Id: getroute.c,v 1.11 2015/11/18 08:49:26 nanard Exp $ */
|
2013-02-06 14:06:59 +00:00
|
|
|
/* MiniUPnP project
|
|
|
|
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
|
2015-11-18 08:52:54 +00:00
|
|
|
* (c) 2006-2015 Thomas Bernard
|
2013-02-06 14:06:59 +00:00
|
|
|
* This software is subject to the conditions detailed
|
|
|
|
* in the LICENCE file provided within the distribution */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#ifdef AF_LINK
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../config.h"
|
|
|
|
#include "../upnputils.h"
|
|
|
|
|
2015-11-17 09:55:17 +00:00
|
|
|
#ifndef SA_SIZE
|
|
|
|
#define SA_SIZE(sa) (SA_LEN(sa))
|
|
|
|
#endif /* SA_SIZE */
|
2013-02-06 14:06:59 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
get_src_for_route_to(const struct sockaddr * dst,
|
|
|
|
void * src, size_t * src_len,
|
|
|
|
int * index)
|
|
|
|
{
|
|
|
|
int found = 0;
|
|
|
|
int s;
|
|
|
|
int l, i;
|
|
|
|
char * p;
|
|
|
|
struct sockaddr * sa;
|
|
|
|
struct {
|
|
|
|
struct rt_msghdr m_rtm;
|
|
|
|
char m_space[512];
|
|
|
|
} m_rtmsg;
|
|
|
|
#define rtm m_rtmsg.m_rtm
|
|
|
|
|
|
|
|
if(dst == NULL)
|
|
|
|
return -1;
|
2015-11-17 09:15:36 +00:00
|
|
|
if(dst->sa_len > 0) {
|
|
|
|
l = dst->sa_len;
|
|
|
|
} else {
|
|
|
|
if(dst->sa_family == AF_INET)
|
|
|
|
l = sizeof(struct sockaddr_in);
|
|
|
|
else if(dst->sa_family == AF_INET6)
|
|
|
|
l = sizeof(struct sockaddr_in6);
|
|
|
|
else {
|
|
|
|
syslog(LOG_ERR, "unknown address family %d", dst->sa_family);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2013-02-06 14:06:59 +00:00
|
|
|
s = socket(PF_ROUTE, SOCK_RAW, dst->sa_family);
|
|
|
|
if(s < 0) {
|
|
|
|
syslog(LOG_ERR, "socket(PF_ROUTE) failed : %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset(&rtm, 0, sizeof(rtm));
|
|
|
|
rtm.rtm_type = RTM_GET;
|
2015-11-18 01:06:05 +00:00
|
|
|
rtm.rtm_flags = RTF_UP;
|
2013-02-06 14:06:59 +00:00
|
|
|
rtm.rtm_version = RTM_VERSION;
|
|
|
|
rtm.rtm_seq = 1;
|
2015-11-18 01:29:09 +00:00
|
|
|
rtm.rtm_addrs = RTA_DST | RTA_IFA | RTA_IFP; /* pass destination address, request source address & interface */
|
2015-11-16 21:56:18 +00:00
|
|
|
memcpy(m_rtmsg.m_space, dst, l);
|
2015-11-17 09:15:36 +00:00
|
|
|
((struct sockaddr *)m_rtmsg.m_space)->sa_len = l;
|
2015-11-16 21:56:18 +00:00
|
|
|
rtm.rtm_msglen = sizeof(struct rt_msghdr) + l;
|
2013-02-06 14:06:59 +00:00
|
|
|
if(write(s, &m_rtmsg, rtm.rtm_msglen) < 0) {
|
|
|
|
syslog(LOG_ERR, "write: %m");
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
l = read(s, &m_rtmsg, sizeof(m_rtmsg));
|
|
|
|
if(l<0) {
|
|
|
|
syslog(LOG_ERR, "read: %m");
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-11-17 10:25:10 +00:00
|
|
|
syslog(LOG_DEBUG, "read l=%d seq=%d pid=%d sizeof(struct rt_msghdr)=%d",
|
2015-11-18 08:52:54 +00:00
|
|
|
l, rtm.rtm_seq, rtm.rtm_pid, (int)sizeof(struct rt_msghdr));
|
2013-02-06 14:06:59 +00:00
|
|
|
} while(l > 0 && (rtm.rtm_pid != getpid() || rtm.rtm_seq != 1));
|
|
|
|
close(s);
|
|
|
|
p = m_rtmsg.m_space;
|
|
|
|
if(rtm.rtm_addrs) {
|
|
|
|
for(i=1; i<0x8000; i <<= 1) {
|
|
|
|
if(i & rtm.rtm_addrs) {
|
|
|
|
char tmp[256] = { 0 };
|
|
|
|
sa = (struct sockaddr *)p;
|
|
|
|
sockaddr_to_string(sa, tmp, sizeof(tmp));
|
|
|
|
syslog(LOG_DEBUG, "type=%d sa_len=%d sa_family=%d %s",
|
2015-11-17 01:34:42 +00:00
|
|
|
i, sa->sa_len, sa->sa_family, tmp);
|
2015-11-18 01:06:05 +00:00
|
|
|
if(i == RTA_IFA) {
|
2013-02-06 14:06:59 +00:00
|
|
|
size_t len = 0;
|
|
|
|
void * paddr = NULL;
|
|
|
|
if(sa->sa_family == AF_INET) {
|
|
|
|
paddr = &((struct sockaddr_in *)sa)->sin_addr;
|
|
|
|
len = sizeof(struct in_addr);
|
|
|
|
} else if(sa->sa_family == AF_INET6) {
|
|
|
|
paddr = &((struct sockaddr_in6 *)sa)->sin6_addr;
|
|
|
|
len = sizeof(struct in6_addr);
|
|
|
|
}
|
|
|
|
if(paddr) {
|
2015-11-16 19:32:02 +00:00
|
|
|
if(src && src_len) {
|
|
|
|
if(*src_len < len) {
|
|
|
|
syslog(LOG_WARNING, "cannot copy src. %u<%u",
|
|
|
|
(unsigned)*src_len, (unsigned)len);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(src, paddr, len);
|
|
|
|
*src_len = len;
|
2013-02-06 14:06:59 +00:00
|
|
|
}
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef AF_LINK
|
2015-11-18 01:06:05 +00:00
|
|
|
else if((i == RTA_IFP) && (sa->sa_family == AF_LINK)) {
|
2013-02-06 14:06:59 +00:00
|
|
|
struct sockaddr_dl * sdl = (struct sockaddr_dl *)sa;
|
|
|
|
if(index)
|
|
|
|
*index = sdl->sdl_index;
|
|
|
|
}
|
|
|
|
#endif
|
2015-11-18 08:52:54 +00:00
|
|
|
/* at least 4 bytes per address are reserved,
|
|
|
|
* that is true with OpenBSD 4.3 */
|
|
|
|
if(SA_SIZE(sa) > 0)
|
|
|
|
p += SA_SIZE(sa);
|
|
|
|
else
|
|
|
|
p += 4;
|
2013-02-06 14:06:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found ? 0 : -1;
|
|
|
|
}
|
|
|
|
|