Answer was simple: I used the bitwise OR (|
) operator instead of the AND (&
) operator. Fixed code is:
bool is_interface_online(std::string interface) {
struct ifreq ifr;
int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, interface.c_str());
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
perror("SIOCGIFFLAGS");
}
close(sock);
return !!(ifr.ifr_flags & IFF_UP);
}