17 #include <sys/socket.h> 19 #include <arpa/inet.h> 23 void NetworkInterfaces::initIfaddr()
31 if (interface.compare(std::string(
"*")) == 0)
39 int correct_family = AF_INET;
40 int size_of_struct =
sizeof(
struct sockaddr_in);
43 correct_family = AF_INET6;
44 size_of_struct =
sizeof(
struct sockaddr_in6);
46 struct ifaddrs * it =
ifaddr;
50 char host[NI_MAXHOST];
51 family = it->ifa_addr->sa_family;
52 if (family == correct_family)
54 s = getnameinfo(it->ifa_addr,size_of_struct,host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
55 if (interface.compare(std::string(it->ifa_name)) == 0)
57 s_addr = inet_addr(host);
63 printf(
"Warning: interface %s does not exist, therefore no connection can be established!\n",interface.c_str());
static struct ifaddrs * ifaddr
static void bindInterface(in_addr_t &s_addr, std::string interface, bool ipv6=false)