libtins
libtins copied to clipboard
How to get gateway?
I can get ipv4 address by NetworkInferface::ipv4_address(). But how to get gateway address on that interface?
Such like, 172.18.0.1/24 and 172.18.0.123 is the ipv4 address and 172.18.0.1 is the gateway address?
Then how to get 172.18.0.1?
take alook at this code
// First fetch all network interfaces
vector<NetworkInterface> interfaces = NetworkInterface::all();
// Now iterate them
for (const NetworkInterface& iface : interfaces) {
// First print the name (GUID)
//cout << "Interface name: " << iface.name() << endl;
std::wcout << " (" << iface.friendly_name() << ")" << endl;
// Second print the ip address got by the iface
// this can helps to identifiy which gate you connected to
cout << " *"<< iface.addresses().ip_addr<< "*" << endl;
// Third print my subnet mask
cout << " #" << iface.addresses().netmask << "#" << endl;
// Forth thing is to print the status of the current iface
cout << " !" << iface.addresses().is_up << "!" << endl;
// Fifth thing to get the Bcast of current inface
cout << " -" << iface.addresses().bcast_addr << "-" << endl;
// Sixth thing to get the MAC address of this iface
cout << " >" << iface.addresses().hw_addr << ">" << endl;
// Seventh under testing katch current gateway device on the range
auto gw = IPv4Address("0.0.0.0");
auto reply = gateway_from_ip(iface.addresses().ip_addr, gw);
if (reply > 0)
cout << " <" << gw << "<" << endl;