2018-05-22 15:54:19 +00:00
|
|
|
#include "net.hpp"
|
2018-02-01 17:06:49 +00:00
|
|
|
#include "str.hpp"
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2018-02-01 17:07:01 +00:00
|
|
|
#include <ifaddrs.h>
|
2018-05-23 13:49:00 +00:00
|
|
|
#include <net/if.h>
|
2018-05-18 13:17:58 +00:00
|
|
|
#include <cstdio>
|
2018-05-29 12:15:34 +00:00
|
|
|
#include "logger.hpp"
|
2018-02-01 17:07:01 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
operator==(const sockaddr& a, const sockaddr& b)
|
|
|
|
{
|
|
|
|
socklen_t sz = sizeof(a.sa_data);
|
|
|
|
switch(a.sa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
sz = sizeof(sockaddr_in);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
sz = sizeof(sockaddr_in6);
|
|
|
|
break;
|
2018-05-29 12:15:34 +00:00
|
|
|
#ifdef AF_PACKET
|
2018-05-22 15:54:19 +00:00
|
|
|
case AF_PACKET:
|
|
|
|
sz = sizeof(sockaddr_ll);
|
|
|
|
break;
|
2018-05-29 12:15:34 +00:00
|
|
|
#endif
|
2018-05-22 15:54:19 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return a.sa_family == b.sa_family && memcmp(a.sa_data, b.sa_data, sz) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator<(const sockaddr_in6& a, const sockaddr_in6& b)
|
|
|
|
{
|
|
|
|
return memcmp(&a, &b, sizeof(sockaddr_in6)) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator<(const in6_addr& a, const in6_addr& b)
|
|
|
|
{
|
|
|
|
return memcmp(&a, &b, sizeof(in6_addr)) < 0;
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:00:58 +00:00
|
|
|
extern "C" {
|
2018-05-22 15:54:19 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_getifaddr(const char* ifname, int af, struct sockaddr* addr)
|
|
|
|
{
|
2018-02-01 17:07:01 +00:00
|
|
|
ifaddrs* ifa = nullptr;
|
2018-05-22 15:54:19 +00:00
|
|
|
bool found = false;
|
2018-02-01 17:07:01 +00:00
|
|
|
socklen_t sl = sizeof(sockaddr_in6);
|
2018-05-22 15:54:19 +00:00
|
|
|
if(af == AF_INET)
|
|
|
|
sl = sizeof(sockaddr_in);
|
2018-05-29 12:15:34 +00:00
|
|
|
#ifdef AF_LINK
|
|
|
|
//printf("AF_INET[%d]\n", AF_INET); // FBSD 2
|
|
|
|
//printf("AF_INET6[%d]\n", AF_INET6); // FBSD 28
|
|
|
|
//printf("AF_LINK[%d]\n", AF_LINK); // FBSD 18
|
|
|
|
if(af == AF_LINK) {
|
|
|
|
printf("We dont support AF_LINK yet\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef AF_PACKET
|
|
|
|
//printf("AF_PACKET[%d]\n", AF_PACKET); // FBSD dne
|
2018-05-22 15:54:19 +00:00
|
|
|
if(af == AF_PACKET)
|
|
|
|
sl = sizeof(sockaddr_ll);
|
2018-05-29 12:15:34 +00:00
|
|
|
#endif
|
2018-05-22 15:54:19 +00:00
|
|
|
if(getifaddrs(&ifa) == -1)
|
|
|
|
return false;
|
2018-02-01 17:07:01 +00:00
|
|
|
ifaddrs* i = ifa;
|
2018-05-22 15:54:19 +00:00
|
|
|
while(i)
|
|
|
|
{
|
|
|
|
if(i->ifa_addr)
|
2018-05-18 13:17:58 +00:00
|
|
|
{
|
2018-05-29 12:15:34 +00:00
|
|
|
//llarp::Info(__FILE__, "scanning ", i->ifa_name, " af: ", std::to_string(i->ifa_addr->sa_family));
|
2018-05-22 15:54:19 +00:00
|
|
|
if(llarp::StrEq(i->ifa_name, ifname) && i->ifa_addr->sa_family == af)
|
|
|
|
{
|
2018-05-29 12:15:34 +00:00
|
|
|
//llarp::Info(__FILE__, "found ", ifname, " af: ", af);
|
2018-05-18 13:17:58 +00:00
|
|
|
memcpy(addr, i->ifa_addr, sl);
|
2018-05-23 13:49:00 +00:00
|
|
|
if(af == AF_INET6)
|
|
|
|
{
|
|
|
|
// set scope id
|
|
|
|
sockaddr_in6* ip6addr = (sockaddr_in6*)addr;
|
|
|
|
ip6addr->sin6_scope_id = if_nametoindex(ifname);
|
|
|
|
ip6addr->sin6_flowinfo = 0;
|
|
|
|
}
|
|
|
|
found = true;
|
2018-05-18 13:17:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-02-01 17:06:49 +00:00
|
|
|
}
|
2018-02-01 17:07:01 +00:00
|
|
|
i = i->ifa_next;
|
2018-02-01 17:06:49 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
if(ifa)
|
|
|
|
freeifaddrs(ifa);
|
2018-02-01 17:07:01 +00:00
|
|
|
return found;
|
2018-02-01 17:06:49 +00:00
|
|
|
}
|
2018-05-17 20:00:58 +00:00
|
|
|
}
|