Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How to get gateway? #522

Open
WorstCodeWay opened this issue Dec 21, 2023 · 1 comment
Open

How to get gateway? #522

WorstCodeWay opened this issue Dec 21, 2023 · 1 comment

Comments

@WorstCodeWay
Copy link

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
Copy link

ahmedtalaat327 commented Jan 23, 2024

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;

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants