mfontanini / libtins

High-level, multiplatform C++ network packet sniffing and crafting library.
http://libtins.github.io/
BSD 2-Clause "Simplified" License
1.89k stars 373 forks source link

How to get gateway? #522

Open WorstCodeWay opened 6 months ago

WorstCodeWay commented 6 months ago

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?

ahmedtalaat327 commented 5 months ago

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;