From 8ea12252f005626cff0aa2e2f6cf8b7072dad3f5 Mon Sep 17 00:00:00 2001
From: "Uwe L. Korn" <uwelk@xhochy.com>
Date: Fri, 20 Jun 2014 18:07:18 +0100
Subject: [PATCH] Always return fresh network adresses

---
 src/libtomahawk/network/Servent.cpp | 34 ++++++++++++++++++++++++++---
 src/libtomahawk/network/Servent_p.h |  8 +++++++
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/src/libtomahawk/network/Servent.cpp b/src/libtomahawk/network/Servent.cpp
index 0916b5c5d..67c7c2627 100644
--- a/src/libtomahawk/network/Servent.cpp
+++ b/src/libtomahawk/network/Servent.cpp
@@ -138,6 +138,8 @@ Servent::startListening( QHostAddress ha, bool upnp, int port, Tomahawk::Network
         }
     }
 
+    d->externalListenAll = false;
+
 #if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
     if ( ha == QHostAddress::Any )
 #else
@@ -156,7 +158,8 @@ Servent::startListening( QHostAddress ha, bool upnp, int port, Tomahawk::Network
             if ( addr.isInSubnet( QHostAddress::parseSubnet( "fe80::/10" ) ) )
                 continue; // Skip link local addresses
             tLog( LOGVERBOSE ) << Q_FUNC_INFO << "Listening to " << addr.toString();
-            d_func()->externalAddresses.append( addr );
+            d->externalAddresses.append( addr );
+            d->externalListenAll = true;
         }
 
     }
@@ -411,8 +414,17 @@ Servent::lookupControlConnection( const QString& nodeid )
 QList<SipInfo>
 Servent::getLocalSipInfos( const QString& nodeid, const QString& key )
 {
+    Q_D( Servent );
+
     QList<SipInfo> sipInfos = QList<SipInfo>();
-    foreach ( QHostAddress ha, d_func()->externalAddresses )
+    QList<QHostAddress> addresses = d->externalAddresses;
+
+    if ( d->externalListenAll )
+    {
+        addresses = QNetworkInterface::allAddresses();
+    }
+
+    foreach ( QHostAddress ha, addresses )
     {
         SipInfo info = SipInfo();
         info.setHost( ha.toString() );
@@ -867,13 +879,22 @@ Servent::cleanupSocket( QTcpSocketExtra* sock )
 void
 Servent::initiateConnection( const SipInfo& sipInfo, Connection* conn )
 {
+    Q_D( Servent );
+
     Q_ASSERT( sipInfo.isValid() );
     Q_ASSERT( sipInfo.isVisible() );
     Q_ASSERT( sipInfo.port() > 0 );
     Q_ASSERT( conn );
 
     // Check that we are not connecting to ourselves
-    foreach ( QHostAddress ha, d_func()->externalAddresses )
+    QList<QHostAddress> addresses = d->externalAddresses;
+
+    if ( d->externalListenAll )
+    {
+        addresses = QNetworkInterface::allAddresses();
+    }
+
+    foreach ( QHostAddress ha, addresses )
     {
         if ( sipInfo.host() == ha.toString() )
         {
@@ -1063,6 +1084,13 @@ Servent::port() const
 QList<QHostAddress>
 Servent::addresses() const
 {
+    Q_D( const Servent );
+
+    if ( d->externalListenAll )
+    {
+        return QNetworkInterface::allAddresses();
+    }
+
     return d_func()->externalAddresses;
 }
 
diff --git a/src/libtomahawk/network/Servent_p.h b/src/libtomahawk/network/Servent_p.h
index a5c2fcab3..230e1d1f7 100644
--- a/src/libtomahawk/network/Servent_p.h
+++ b/src/libtomahawk/network/Servent_p.h
@@ -70,6 +70,14 @@ private:
      */
     QList<QHostAddress> externalAddresses;
 
+    /**
+     * Do we listen to all external addresses.
+     *
+     * If true, we will always reiterate over the current adresses known to Qt
+     * instead of returning a static list for the local SipInfos.
+     */
+    bool externalListenAll;
+
     /**
      * Either the static set or the UPnP set external host
      */