Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QWebkit: downloading a page using a specific network interface



  • Hi Everybody,

    I have a PC with two network interfaces; eth0 and eth1
    eth1 has no IP address but it is bound to a ppp connection

    eth0 Link encap:Ethernet HWaddr 00:26:18:f1:91:d1
    inet addr:192.168.23.7 Bcast:192.168.23.255 Mask:255.255.255.0

    ppp0 Link encap:Point-to-Point Protocol
    inet addr:192.168.60.7 P-t-P:192.168.60.213 Mask:255.255.255.255

    I'd like is to be able to use Qt webkit to download pages using the ppp0 and not the eth0 interface.

    What I did at code level using Qt is the following:

    • Retrieve the list of available network configurations
    • Select the network configuration associated with the ppp0 iface
    • create a QNetworkAccessManager
    • set the configuration of the QNetworkAccessManager
    • create a QWebPage
    • set the network access manager of the webpage to the above one
    • load the webpage

    Unfortunately it seems that the data always exit via the eth0 iface (I monitored both ifaces with wireshark/tcpdump) The problem is not related to the routing configuration of the PC, I configured the system so that I can send packets out via the ppp0 iface.

    ie. ping -c 3 -I 192.168.60.7 www.google.com is correctly routed out via the ppp0 iface

    Here's a snapshot of my code:

    @
    QNetworkConfigurationManager* m_netConfigMngPtr;
    QNetworkAccessManager* m_netAccessMngPtr;

    m_netConfigMngPtr = new QNetworkConfigurationManager();
    qDebug() << "network config manager capabilities" << m_netConfigMngPtr->capabilities();

    QList<QNetworkConfiguration> netConfigList = m_netConfigMngPtr->allConfigurations();

    QString ifaceId;
    qDebug() << Q_FUNC_INFO << "Available Networck configurations:" << netConfigList.size();
    foreach( QNetworkConfiguration c, netConfigList)
    {
    qDebug() << Q_FUNC_INFO << "network configuration:" << c.identifier() << c.name() << c.state() << c.isValid();
    if (c.name() == "ppp0")
    {
    ifaceId = c.identifier();
    }
    }

    QNetworkConfiguration netConf = m_netConfigMngPtr->configurationFromIdentifier(ifaceId);
    qDebug() << "Net config for " << ifaceId << "(" << netConf.name() << ")" << "is valid: " << netConf.isValid();

    m_netAccessMngPtr = new QNetworkAccessManager;
    m_netAccessMngPtr->setConfiguration( netConf );

    QWebPage* m_theWebPagePtr = new QWebPage;
    m_theWebPagePtr->setNetworkAccessManager(m_netAccessMngPtr);
    m_theWebPagePtr->currentFrame()->load( QUrl("http://www.google.com") );

    @

    I'm using Qt 4.8.1 on Linux 64bit
    Any hint or suggestion on what I'm doing wrong?


Log in to reply