PDA

View Full Version : How to find out which network interface is used to communicate with specific IP



Buldozer
30th June 2012, 19:46
Hi all,


Recently I came across a problem with my software in a multi-NIC environment. The problem I have is, that I would like to find out the network interface (actually just it's IP), that is used when communicating with a specific external IP. Let's say my host has two NICs, one (call it eth0) is set to 192.168.1.100 and the other (eth1) to 10.11.12.13, and routing on the host is set that 10.0.0.0 goes over the 10.11.12.13 interface while all the other traffic goes over the 192.168.1.100 interface.

What I would like is a pseudo

QString MyIP=MyNIC_IP(ForeignAddress);

which would return '192.168.1.100' for a ForeignAddress 'www.google.com'
and '10.11.12.13' for a ForeignAddress '10.20.30.40'

... but can't seem to get a proper idea as to how to do it. Any hints/help would be greatly appreciated.

wysota
2nd July 2012, 00:29
How would you do that without using Qt?

ChrisW67
2nd July 2012, 07:43
You can determine which interface the operating might use by querying the system routing tables and looking for the least cost* route that would apply to your destination and protocol (IPv4, IPv6 or something more exotic). You could start at GetIpForwardTable() on Windows or the /proc/net/route pseudo-file on Linux (also see man route).

* for whatever metric the operating system concerned might apply

Buldozer
2nd July 2012, 08:40
How would you do that without using Qt?

On a windows environment, I'd create a connection to the desired address, then do a 'GetTcpTable', find the MIB_TCPROW that corresponds to the connection, and get the dwLocalAddr from the row structure. Now, as I'd like to have an 'OS-agnostic' solution (thus the move of my app from MFC to QT), I hoped that QT might have some functionality which would allow me to do the same.

Added after 5 minutes:


You can determine which interface the operating might use by querying the system routing tables and looking for the least cost* route that would apply to your destination and protocol (IPv4, IPv6 or something more exotic). You could start at GetIpForwardTable() on Windows or the /proc/net/route pseudo-file on Linux (also see man route).

* for whatever metric the operating system concerned might apply


I was really hoping not to have to 'parse' the routing table. In a way I was looking for something like a 'GetTcpTable' (see my other reply), where all the routing is already done by underlying OS. In the end though (if no proper QT solution can be found), I might have to write specific implementations of the function for all supported OS-es, which, of course, I'd really like to avoid if possible.

wysota
2nd July 2012, 10:04
I hoped that QT might have some functionality which would allow me to do the same.
Qt is an application development framework, not a swiss army knife.

Unfortunately /proc/net/route will not work on any system that is not using a static routing table. Moreover with bonded interfaces there is no answer to your question at all. And even custom firewalling rules might influence the final interface that is used to send/receive packets and there is nothing you can do about it.