Get local host IP on Linux, Shutdown() a little cleaner -- linux-0.1.6-test1 waypoint
authors_nakamoto <s_nakamoto@1a98c847-1fd6-4fd8-948a-caf3550aa51b>
Sun, 8 Nov 2009 04:24:52 +0000 (04:24 +0000)
committers_nakamoto <s_nakamoto@1a98c847-1fd6-4fd8-948a-caf3550aa51b>
Sun, 8 Nov 2009 04:24:52 +0000 (04:24 +0000)
headers.h
main.cpp
makefile.unix
net.cpp
ui.cpp

index 34eafb2..76881bb 100644 (file)
--- a/headers.h
+++ b/headers.h
@@ -72,6 +72,8 @@
 #include <netdb.h>\r
 #include <unistd.h>\r
 #include <errno.h>\r
+#include <net/if.h>\r
+#include <ifaddrs.h>\r
 #include <boost/filesystem.hpp>\r
 #include <boost/thread/thread.hpp>\r
 #include <boost/algorithm/string.hpp>\r
index fe213c0..b957027 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -1215,7 +1215,7 @@ bool CBlock::AcceptBlock()
         return error("AcceptBlock() : block's timestamp is too early");\r
 \r
     // Check that all transactions are finalized (starting around Dec 2009)\r
-    if (nBestHeight > 31000) // 25620 + 5320\r
+    if (nBestHeight > 31000)\r
         foreach(const CTransaction& tx, vtx)\r
             if (!tx.IsFinal(nTime))\r
                 return error("AcceptBlock() : contains a non-final transaction");\r
@@ -1802,7 +1802,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
         {\r
             if (fShutdown)\r
                 return true;\r
-            addr.nTime = GetAdjustedTime();\r
+            addr.nTime = GetAdjustedTime() - 2 * 60 * 60;\r
             if (pfrom->fGetAddr)\r
                 addr.nTime -= 5 * 24 * 60 * 60;\r
             AddAddress(addrdb, addr, false);\r
index c62efc0..24aa8bf 100644 (file)
@@ -15,21 +15,24 @@ endif
 \r
 \r
 \r
-INCLUDEPATHS=-I"/usr/include" \\r
-             -I"/usr/local/boost_1_40_0" \\r
-             -I"/usr/local/db-4.7.25.NC/build_unix" \\r
-             -I"/usr/local/include/wx-2.8" \\r
-             -I"/usr/local/lib/wx/include/gtk2-ansi-debug-static-2.8"\r
-\r
-LIBPATHS=-L"/usr/lib" \\r
-         -L"/usr/local/lib" \\r
-         -L"/usr/local/db-4.7.25.NC/build_unix"\r
+INCLUDEPATHS= \\r
+ -I"/usr/include" \\r
+ -I"/usr/local/boost_1_40_0" \\r
+ -I"/usr/local/db-4.7.25.NC/build_unix" \\r
+ -I"/usr/local/include/wx-2.8" \\r
+ -I"/usr/local/lib/wx/include/gtk2-ansi-debug-static-2.8"\r
+\r
+LIBPATHS= \\r
+ -L"/usr/lib" \\r
+ -L"/usr/local/lib" \\r
+ -L"/usr/local/db-4.7.25.NC/build_unix"\r
 \r
 LIBS= \\r
  -Wl,-Bstatic -l boost_thread -l boost_system -l boost_filesystem -Wl,-Bdynamic \\r
  -Wl,-Bstatic -l db_cxx -l wx_gtk2$(D)-2.8 -Wl,-Bdynamic \\r
  -l crypto \\r
  -l gtk-x11-2.0 -l gthread-2.0 -l SM\r
+\r
 WXDEFS=-D__WXGTK__ -DNOPCH\r
 CFLAGS=-O0 -w -Wno-invalid-offsetof -Wformat $(DEBUGFLAGS) $(WXDEFS) $(INCLUDEPATHS)\r
 HEADERS=headers.h util.h main.h serialize.h uint256.h key.h bignum.h script.h db.h base58.h\r
@@ -76,7 +79,7 @@ obj/irc.o:  irc.cpp               $(HEADERS)
 \r
 \r
 OBJS=obj/util.o obj/script.o obj/db.o obj/net.o obj/main.o obj/market.o \\r
-       obj/ui.o obj/uibase.o obj/sha.o obj/irc.o \r
+       obj/ui.o obj/uibase.o obj/sha.o obj/irc.o\r
 \r
 bitcoin: headers.h.gch $(OBJS)\r
        g++ $(CFLAGS) -o $@ $(LIBPATHS) $(OBJS) $(LIBS)\r
diff --git a/net.cpp b/net.cpp
index d70a62a..0569604 100644 (file)
--- a/net.cpp
+++ b/net.cpp
@@ -842,20 +842,20 @@ void ThreadOpenConnections2(void* parg)
                 if (!addr.IsIPv4() || !addr.IsValid() || setConnected.count(addr.ip))\r
                     continue;\r
 \r
+                // Randomize the order in a deterministic way, putting the standard port first\r
+                int64 nRandomizer = (uint64)(addr.nLastFailed * 9567851 + addr.ip * 7789) % (1 * 60 * 60);\r
+                if (addr.port != DEFAULT_PORT)\r
+                    nRandomizer += 1 * 60 * 60;\r
+\r
                 // Limit retry frequency\r
-                if (GetAdjustedTime() < addr.nLastFailed + nDelay)\r
+                if (GetAdjustedTime() < addr.nLastFailed + nDelay + nRandomizer)\r
                     continue;\r
 \r
                 // Try again only after all addresses had a first attempt\r
-                int64 nTime = addr.nTime;\r
+                int64 nTime = addr.nTime - nRandomizer;\r
                 if (addr.nLastFailed > addr.nTime)\r
                     nTime -= 365 * 24 * 60 * 60;\r
 \r
-                // Randomize the order a little, putting the standard port first\r
-                nTime += GetRand(1 * 60 * 60);\r
-                if (addr.port != DEFAULT_PORT)\r
-                    nTime -= 1 * 60 * 60;\r
-\r
                 if (nTime > nBestTime)\r
                 {\r
                     nBestTime = nTime;\r
@@ -1069,6 +1069,7 @@ bool StartNode(string& strError)
     if (pnodeLocalHost == NULL)\r
         pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress("127.0.0.1", nLocalServices));\r
 \r
+#ifdef __WXMSW__\r
     // Get local host ip\r
     char pszHostName[255];\r
     if (gethostname(pszHostName, sizeof(pszHostName)) == SOCKET_ERROR)\r
@@ -1090,10 +1091,49 @@ bool StartNode(string& strError)
         printf("host ip %d: %s\n", i, CAddress(*(unsigned int*)phostent->h_addr_list[i]).ToStringIP().c_str());\r
     for (int i = 0; phostent->h_addr_list[i] != NULL; i++)\r
     {\r
-        addrLocalHost = CAddress(*(unsigned int*)phostent->h_addr_list[i], DEFAULT_PORT, nLocalServices);\r
-        if (addrLocalHost.IsValid() && addrLocalHost.GetByte(3) != 127)\r
+        CAddress addr(*(unsigned int*)phostent->h_addr_list[i], DEFAULT_PORT, nLocalServices);\r
+        if (addr.IsValid() && addr.GetByte(3) != 127)\r
+        {\r
+            addrLocalHost = addr;\r
             break;\r
+        }\r
+    }\r
+#else\r
+    // Get local host ip\r
+    struct ifaddrs* myaddrs;\r
+    if (getifaddrs(&myaddrs) == 0)\r
+    {\r
+        for (struct ifaddrs* ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)\r
+        {\r
+            if (ifa->ifa_addr == NULL) continue;\r
+            if ((ifa->ifa_flags & IFF_UP) == 0) continue;\r
+            if (strcmp(ifa->ifa_name, "lo") == 0) continue;\r
+            if (strcmp(ifa->ifa_name, "lo0") == 0) continue;\r
+            char pszIP[100];\r
+            if (ifa->ifa_addr->sa_family == AF_INET)\r
+            {\r
+                struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);\r
+                if (inet_ntop(ifa->ifa_addr->sa_family, (void*)&(s4->sin_addr), pszIP, sizeof(pszIP)) != NULL)\r
+                    printf("ipv4 %s: %s\n", ifa->ifa_name, pszIP);\r
+\r
+                // Take the first IP that isn't loopback 127.x.x.x\r
+                CAddress addr(*(unsigned int*)&s4->sin_addr, DEFAULT_PORT, nLocalServices);\r
+                if (addr.IsValid() && addr.GetByte(3) != 127)\r
+                {\r
+                    addrLocalHost = addr;\r
+                    break;\r
+                }\r
+            }\r
+            else if (ifa->ifa_addr->sa_family == AF_INET6)\r
+            {\r
+                struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);\r
+                if (inet_ntop(ifa->ifa_addr->sa_family, (void*)&(s6->sin6_addr), pszIP, sizeof(pszIP)) != NULL)\r
+                    printf("ipv6 %s: %s\n", ifa->ifa_name, pszIP);\r
+            }\r
+        }\r
+        freeifaddrs(myaddrs);\r
     }\r
+#endif\r
     printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str());\r
 \r
     // Get our external IP address for incoming connections\r
diff --git a/ui.cpp b/ui.cpp
index a1873d5..e12970b 100644 (file)
--- a/ui.cpp
+++ b/ui.cpp
@@ -387,17 +387,28 @@ CMainFrame::~CMainFrame()
 void Shutdown(void* parg)\r
 {\r
     static CCriticalSection cs_Shutdown;\r
+    static bool fTaken;\r
+    bool fFirstThread;\r
     CRITICAL_BLOCK(cs_Shutdown)\r
     {\r
+        fFirstThread = !fTaken;\r
+        fTaken = true;\r
+    }\r
+    if (fFirstThread)\r
+    {\r
         fShutdown = true;\r
         nTransactionsUpdated++;\r
         DBFlush(false);\r
         StopNode();\r
         DBFlush(true);\r
-\r
         printf("Bitcoin exiting\n\n");\r
         exit(0);\r
     }\r
+    else\r
+    {\r
+        loop\r
+            Sleep(100000);\r
+    }\r
 }\r
 \r
 void CMainFrame::OnClose(wxCloseEvent& event)\r