Results 1 to 5 of 5

Thread: How to find out which network interface is used to communicate with specific IP

  1. #1
    Join Date
    Mar 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to find out which network interface is used to communicate with specific IP

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to find out which network interface is used to communicate with specific IP

    How would you do that without using Qt?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to find out which network interface is used to communicate with specific IP

    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

  4. #4
    Join Date
    Mar 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to find out which network interface is used to communicate with specific IP

    Quote Originally Posted by wysota View Post
    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:


    Quote Originally Posted by ChrisW67 View Post
    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.
    Last edited by Buldozer; 2nd July 2012 at 07:51.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to find out which network interface is used to communicate with specific IP

    Quote Originally Posted by Buldozer View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Deduct network interface from incoming UDP Packet
    By mastupristi in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2012, 17:09
  2. Replies: 3
    Last Post: 22nd December 2010, 14:22
  3. Binding QTcpSocket to a specific network interface
    By vkincaid in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2010, 21:28
  4. Replies: 12
    Last Post: 22nd March 2009, 11:22
  5. Replies: 0
    Last Post: 18th March 2009, 06:33

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.