in_addr_t broadcastFromNetmask(in_addr_t address, in_addr_t netmask) { // The broadcast address is the address with the bits excluded in the // netmask set to 1. For example if address = 10.0.2.15 and netmask is // 255.255.255.0 then the broadcast is 10.0.2.255. If instead netmask was // 255.0.0.0.0 then the broadcast would be 10.255.255.255 // // Simply set all the lower bits to 1 and that should do it. return address | (~netmask);
}
Result Interface::init(constchar* interfaceName) {
mInterfaceName = interfaceName;
if (mSocketFd != -1) { return Result::error("Interface initialized more than once");
}
mSocketFd = ::socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); if (mSocketFd == -1) { return Result::error("Failed to create interface socket for '%s': %s", interfaceName,
strerror(errno));
}
Result res = populateIndex(); if (!res) { return res;
}
res = populateMacAddress(); if (!res) { return res;
}
res = bringUp(); if (!res) { return res;
}
res = setAddress(0, 0); if (!res) { return res;
}
return Result::success();
}
Result Interface::bringUp() { return setInterfaceUp(true);
}
Result Interface::bringDown() { return setInterfaceUp(false);
}
Result Interface::setMtu(uint16_t mtu) { struct ifreq request = createRequest();
strncpy(request.ifr_name, mInterfaceName.c_str(), sizeof(request.ifr_name));
request.ifr_mtu = mtu; int status = ::ioctl(mSocketFd, SIOCSIFMTU, &request); if (status != 0) { return Result::error("Failed to set interface MTU %u for '%s': %s", static_cast<unsignedint>(mtu), mInterfaceName.c_str(),
strerror(errno));
}
request.msg.ifa_family = AF_INET; // Count the number of bits in the subnet mask, this is the length.
request.msg.ifa_prefixlen = __builtin_popcount(subnetMask);
request.msg.ifa_index = mIndex;
Result Interface::populateIndex() { struct ifreq request = createRequest();
int status = ::ioctl(mSocketFd, SIOCGIFINDEX, &request); if (status != 0) { return Result::error("Failed to get interface index for '%s': %s", mInterfaceName.c_str(),
strerror(errno));
}
mIndex = request.ifr_ifindex; return Result::success();
}
Result Interface::populateMacAddress() { struct ifreq request = createRequest();
int status = ::ioctl(mSocketFd, SIOCGIFHWADDR, &request); if (status != 0) { return Result::error("Failed to get MAC address for '%s': %s", mInterfaceName.c_str(),
strerror(errno));
}
memcpy(mMacAddress, &request.ifr_hwaddr.sa_data, ETH_ALEN); return Result::success();
}
Result Interface::setInterfaceUp(bool shouldBeUp) { struct ifreq request = createRequest();
int status = ::ioctl(mSocketFd, SIOCGIFFLAGS, &request); if (status != 0) { return Result::error("Failed to get interface flags for '%s': %s", mInterfaceName.c_str(),
strerror(errno));
}
bool isUp = (request.ifr_flags & IFF_UP) != 0; if (isUp != shouldBeUp) { // Toggle the up flag
request.ifr_flags ^= IFF_UP;
} else { // Interface is already in desired state, do nothing return Result::success();
}
status = ::ioctl(mSocketFd, SIOCSIFFLAGS, &request); if (status != 0) { return Result::error("Failed to set interface flags for '%s': %s", mInterfaceName.c_str(),
strerror(errno));
}
return Result::success();
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.