mirror of https://github.com/oxen-io/lokinet
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
645 B
C++
31 lines
645 B
C++
#include <llarp/net.h>
|
|
#include "str.hpp"
|
|
|
|
#include <arpa/inet.h>
|
|
#include <ifaddrs.h>
|
|
|
|
extern "C" {
|
|
|
|
bool llarp_getifaddr(const char * ifname, int af, struct 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) && 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;
|
|
}
|
|
|
|
}
|