lokinet/llarp/net.cpp
Jeff Becker 1fdfdc0244
more
2018-02-01 12:06:49 -05:00

37 lines
771 B
C++

#include "net.hpp"
#include "str.hpp"
#include <ifaddrs.h>
#include <arpa/inet.h>
namespace llarp
{
namespace net
{
bool GetIfAddr(const std::string & ifname, int af, sockaddr * addr)
{
ifaddrs * ifa = nullptr;
bool found = false;
socklen_t sl = sizeof(sockaddr_in6);
if(af == AF_INET)
sl = sizeof(sockaddr_in);
if(getifaddrs(&ifa) == -1)
return false;
ifaddrs * i = ifa;
while(i)
{
if(llarp::StrEq(i->ifa_name, ifname.c_str()) && i->ifa_addr && i->ifa_addr->sa_family == af)
{
memcpy(addr, i->ifa_addr, sl);
found = true;
break;
}
i = i->ifa_next;
}
if(ifa) freeifaddrs(ifa);
return found;
}
}
}