changeset 2482:6499c25a47e7 draft

Keep port information for local addresses
author Pieter Wuille <pieter.wuille@gmail.com>
date Thu, 10 May 2012 20:35:13 +0200
parents bdf78041eb31
children 0e85af1ee86d
files src/irc.cpp src/net.cpp src/net.h
diffstat 3 files changed, 29 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/irc.cpp
+++ b/src/irc.cpp
@@ -246,7 +246,7 @@
                 return;
         }
 
-        CNetAddr addrLocal;
+        CService addrLocal;
         string strMyName;
         if (GetLocal(addrLocal, &addrConnect))
             strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -46,7 +46,7 @@
 static bool fUseUPnP = false;
 uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
 static CCriticalSection cs_mapLocalHost;
-static map<CNetAddr, int> mapLocalHost;
+static map<CService, int> mapLocalHost;
 static bool vfReachable[NET_MAX] = {};
 static bool vfLimited[NET_MAX] = {};
 static CNode* pnodeLocalHost = NULL;
@@ -96,7 +96,7 @@
 }
 
 // find 'best' local address for a particular peer
-bool GetLocal(CNetAddr& addr, const CNetAddr *paddrPeer)
+bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
 {
     if (fUseProxy || mapArgs.count("-connect") || fNoListen)
         return false;
@@ -105,7 +105,7 @@
     int nBestReachability = -1;
     {
         LOCK(cs_mapLocalHost);
-        for (map<CNetAddr, int>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
+        for (map<CService, int>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
         {
             int nCount = (*it).second;
             int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
@@ -124,11 +124,10 @@
 CAddress GetLocalAddress(const CNetAddr *paddrPeer)
 {
     CAddress ret(CService("0.0.0.0",0),0);
-    CNetAddr addr;
+    CService addr;
     if (GetLocal(addr, paddrPeer))
     {
-        ret.SetIP(addr);
-        ret.SetPort(GetListenPort());
+        ret = CAddress(addr);
         ret.nServices = nLocalServices;
         ret.nTime = GetAdjustedTime();
     }
@@ -196,7 +195,7 @@
         if (pnode->fSuccessfullyConnected)
         {
             CAddress addrLocal = GetLocalAddress(&pnode->addr);
-            if (addrLocal.IsRoutable() && (CNetAddr)addrLocal != (CNetAddr)pnode->addrLocal)
+            if (addrLocal.IsRoutable() && (CService)addrLocal != (CService)pnode->addrLocal)
             {
                 pnode->PushAddress(addrLocal);
                 pnode->addrLocal = addrLocal;
@@ -206,7 +205,7 @@
 }
 
 // learn a new local address
-bool AddLocal(const CNetAddr& addr, int nScore)
+bool AddLocal(const CService& addr, int nScore)
 {
     if (!addr.IsRoutable())
         return false;
@@ -226,6 +225,13 @@
     return true;
 }
 
+bool AddLocal(const CNetAddr& addr, int nScore, int port)
+{
+    if (port == -1)
+        port = GetListenPort();
+    return AddLocal(CService(addr, port), nScore);
+}
+
 /** Make a particular network entirely off-limits (no automatic connects to it) */
 void SetLimited(enum Network net, bool fLimited)
 {
@@ -240,7 +246,7 @@
 }
 
 /** vote for a local address */
-bool SeenLocal(const CNetAddr& addr)
+bool SeenLocal(const CService& addr)
 {
     {
         LOCK(cs_mapLocalHost);
@@ -255,7 +261,7 @@
 }
 
 /** check whether a given address is potentially local */
-bool IsLocal(const CNetAddr& addr)
+bool IsLocal(const CService& addr)
 {
     LOCK(cs_mapLocalHost);
     return mapLocalHost.count(addr) > 0;
--- a/src/net.h
+++ b/src/net.h
@@ -44,22 +44,23 @@
 
 enum
 {
-    LOCAL_NONE,
-    LOCAL_IF,
-    LOCAL_UPNP,
-    LOCAL_IRC,
-    LOCAL_HTTP,
-    LOCAL_MANUAL,
+    LOCAL_NONE,   // unknown
+    LOCAL_IF,     // address a local interface listens on
+    LOCAL_UPNP,   // address reported by UPnP
+    LOCAL_IRC,    // address reported by IRC (deprecated)
+    LOCAL_HTTP,   // address reported by whatismyip.com and similars
+    LOCAL_MANUAL, // address explicitly specified (-externalip=)
 
     LOCAL_MAX
 };
 
 void SetLimited(enum Network net, bool fLimited = true);
 bool IsLimited(const CNetAddr& addr);
-bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
-bool SeenLocal(const CNetAddr& addr);
-bool IsLocal(const CNetAddr& addr);
-bool GetLocal(CNetAddr &addr, const CNetAddr *paddrPeer = NULL);
+bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
+bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE, int port = -1);
+bool SeenLocal(const CService& addr);
+bool IsLocal(const CService& addr);
+bool GetLocal(CService &addr, const CNetAddr *paddrPeer = NULL);
 bool IsReachable(const CNetAddr &addr);
 CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL);
 
@@ -142,7 +143,7 @@
     unsigned int nMessageStart;
     CAddress addr;
     std::string addrName;
-    CNetAddr addrLocal;
+    CService addrLocal;
     int nVersion;
     std::string strSubVer;
     bool fOneShot;