Advertisement
Guest User

Bitcoin -port patch

a guest
Jul 27th, 2010
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.60 KB | None | 0 0
  1. diff --git a/init.cpp b/init.cpp
  2. index 1cbe497..c5b9852 100644
  3. --- a/init.cpp
  4. +++ b/init.cpp
  5. @@ -155,6 +156,8 @@ bool AppInit2(int argc, char* argv[])
  6. " -gen=0 \t " + _("Don't generate coins\n") +
  7. " -min \t " + _("Start minimized\n") +
  8. " -datadir=<dir> \t " + _("Specify data directory\n") +
  9. + " -port=<port> \t " + _("Specify listen port\n") +
  10. + " -rpcport=<port> \t " + _("Specify JSON-RPC port\n") +
  11. " -proxy=<ip:port>\t " + _("Connect through socks4 proxy\n") +
  12. " -addnode=<ip> \t " + _("Add a node to connect to\n") +
  13. " -connect=<ip> \t " + _("Connect only to the specified node\n") +
  14. @@ -173,6 +176,9 @@ bool AppInit2(int argc, char* argv[])
  15. return false;
  16. }
  17.  
  18. + if (mapArgs.count("-port"))
  19. + SetListenPort(atoi(mapArgs["-port"]));
  20. +
  21. if (mapArgs.count("-debug"))
  22. fDebug = true;
  23.  
  24. @@ -210,7 +216,7 @@ bool AppInit2(int argc, char* argv[])
  25. //
  26. #if defined(__WXMSW__) && defined(GUI)
  27. // todo: wxSingleInstanceChecker wasn't working on Linux, never deleted its lock file
  28. - // maybe should go by whether successfully bind port 8333 instead
  29. + // maybe should go by whether successfully bind port instead
  30. wxString strMutexName = wxString("bitcoin_running.") + getenv("HOMEPATH");
  31. for (int i = 0; i < strMutexName.size(); i++)
  32. if (!isalnum(strMutexName[i]))
  33. diff --git a/net.cpp b/net.cpp
  34. index f121acd..6d79309 100644
  35. --- a/net.cpp
  36. +++ b/net.cpp
  37. @@ -18,7 +18,7 @@ bool OpenNetworkConnection(const CAddress& addrConnect);
  38. //
  39. bool fClient = false;
  40. uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
  41. -CAddress addrLocalHost(0, DEFAULT_PORT, nLocalServices);
  42. +CAddress addrLocalHost(0, 0, nLocalServices);
  43. CNode* pnodeLocalHost = NULL;
  44. uint64 nLocalHostNonce = 0;
  45. array<int, 10> vnThreadsRunning;
  46. @@ -957,7 +957,7 @@ void ThreadOpenConnections2(void* parg)
  47.  
  48. // Randomize the order in a deterministic way, putting the standard port first
  49. int64 nRandomizer = (uint64)(nStart * 4951 + addr.nLastTry * 9567851 + addr.ip * 7789) % (2 * 60 * 60);
  50. - if (addr.port != DEFAULT_PORT)
  51. + if (addr.port != GetDefaultPortNS())
  52. nRandomizer += 2 * 60 * 60;
  53.  
  54. // Last seen Base retry frequency
  55. @@ -1124,9 +1124,27 @@ void ThreadMessageHandler2(void* parg)
  56.  
  57.  
  58.  
  59. +static unsigned short nListenPortNS = 0;
  60.  
  61. +void SetListenPort(unsigned short port) // port in host byte order
  62. +{
  63. + if (nListenPortNS != 0)
  64. + throw(runtime_error("Error, must call SetListenPort exactly once."));
  65. + nListenPortNS = htons(port);
  66. + addrLocalHost.port = nListenPortNS;
  67. +}
  68.  
  69. +unsigned short GetListenPortNS() // returns port in network byte order
  70. +{
  71. + if (nListenPortNS == 0)
  72. + nListenPortNS = GetDefaultPortNS();
  73. + return nListenPortNS;
  74. +}
  75.  
  76. +unsigned short GetDefaultPortNS() // returns port in network byte order
  77. +{
  78. + return htons(8333);
  79. +}
  80.  
  81. bool BindListenPort(string& strError)
  82. {
  83. @@ -1183,7 +1201,7 @@ bool BindListenPort(string& strError)
  84. memset(&sockaddr, 0, sizeof(sockaddr));
  85. sockaddr.sin_family = AF_INET;
  86. sockaddr.sin_addr.s_addr = INADDR_ANY; // bind to all IPs on this computer
  87. - sockaddr.sin_port = DEFAULT_PORT;
  88. + sockaddr.sin_port = GetListenPortNS();
  89. if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
  90. {
  91. int nErr = WSAGetLastError();
  92. @@ -1225,7 +1243,7 @@ void StartNode(void* parg)
  93. printf("host ip %d: %s\n", i, CAddress(*(unsigned int*)phostent->h_addr_list[i]).ToStringIP().c_str());
  94. for (int i = 0; phostent->h_addr_list[i] != NULL; i++)
  95. {
  96. - CAddress addr(*(unsigned int*)phostent->h_addr_list[i], DEFAULT_PORT, nLocalServices);
  97. + CAddress addr(*(unsigned int*)phostent->h_addr_list[i], GetListenPortNS(), nLocalServices);
  98. if (addr.IsValid() && addr.GetByte(3) != 127)
  99. {
  100. addrLocalHost = addr;
  101. @@ -1253,7 +1271,7 @@ void StartNode(void* parg)
  102. printf("ipv4 %s: %s\n", ifa->ifa_name, pszIP);
  103.  
  104. // Take the first IP that isn't loopback 127.x.x.x
  105. - CAddress addr(*(unsigned int*)&s4->sin_addr, DEFAULT_PORT, nLocalServices);
  106. + CAddress addr(*(unsigned int*)&s4->sin_addr, GetListenPortNS(), nLocalServices);
  107. if (addr.IsValid() && addr.GetByte(3) != 127)
  108. {
  109. addrLocalHost = addr;
  110. diff --git a/net.h b/net.h
  111. index 43c49a2..8914b7e 100644
  112. --- a/net.h
  113. +++ b/net.h
  114. @@ -12,7 +12,6 @@ extern int nBestHeight;
  115.  
  116.  
  117.  
  118. -static const unsigned short DEFAULT_PORT = 0x8d20; // htons(8333)
  119. static const unsigned int PUBLISH_HOPS = 5;
  120. enum
  121. {
  122. @@ -22,6 +21,9 @@ enum
  123.  
  124.  
  125.  
  126. +void SetListenPort(unsigned short nPort);
  127. +unsigned short GetListenPortNS();
  128. +unsigned short GetDefaultPortNS();
  129. bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet);
  130. bool GetMyExternalIP(unsigned int& ipRet);
  131. bool AddAddress(CAddress addr);
  132. @@ -153,7 +155,7 @@ public:
  133. Init();
  134. }
  135.  
  136. - CAddress(unsigned int ipIn, unsigned short portIn=DEFAULT_PORT, uint64 nServicesIn=NODE_NETWORK)
  137. + CAddress(unsigned int ipIn, unsigned short portIn, uint64 nServicesIn=NODE_NETWORK)
  138. {
  139. Init();
  140. ip = ipIn;
  141. @@ -188,7 +190,7 @@ public:
  142. nServices = NODE_NETWORK;
  143. memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
  144. ip = INADDR_NONE;
  145. - port = DEFAULT_PORT;
  146. + port = GetDefaultPortNS();
  147. nTime = GetAdjustedTime();
  148. nLastTry = 0;
  149. }
  150. @@ -196,7 +198,7 @@ public:
  151. bool SetAddress(const char* pszIn)
  152. {
  153. ip = INADDR_NONE;
  154. - port = DEFAULT_PORT;
  155. + port = GetDefaultPortNS();
  156. char psz[100];
  157. strlcpy(psz, pszIn, sizeof(psz));
  158. unsigned int a=0, b=0, c=0, d=0, e=0;
  159. diff --git a/rpc.cpp b/rpc.cpp
  160. index 5ae9e88..34a5cba 100644
  161. --- a/rpc.cpp
  162. +++ b/rpc.cpp
  163. @@ -840,7 +840,13 @@ string JSONRPCReply(const Value& result, const Value& error, const Value& id)
  164. }
  165.  
  166.  
  167. -
  168. +unsigned short nRPCPort()
  169. +{
  170. + unsigned short n = 8332;
  171. + if (!mapArgs["-rpcport"].empty())
  172. + n = atoi(mapArgs["-rpcport"]);
  173. + return n;
  174. +}
  175.  
  176. void ThreadRPCServer(void* parg)
  177. {
  178. @@ -883,7 +889,7 @@ void ThreadRPCServer2(void* parg)
  179.  
  180. // Bind to loopback 127.0.0.1 so the socket can only be accessed locally
  181. boost::asio::io_service io_service;
  182. - tcp::endpoint endpoint(boost::asio::ip::address_v4::loopback(), 8332);
  183. + tcp::endpoint endpoint(boost::asio::ip::address_v4::loopback(), nRPCPort());
  184. tcp::acceptor acceptor(io_service, endpoint);
  185.  
  186. loop
  187. @@ -980,7 +986,8 @@ Value CallRPC(const string& strMethod, const Array& params)
  188. GetConfigFile().c_str()));
  189.  
  190. // Connect to localhost
  191. - tcp::iostream stream("127.0.0.1", "8332");
  192. + std::stringstream ss; ss << nRPCPort();
  193. + tcp::iostream stream("127.0.0.1", ss.str());
  194. if (stream.fail())
  195. throw runtime_error("couldn't connect to server");
  196.  
  197. diff --git a/db.cpp b/db.cpp
  198. index fa4ae90..5c4b0fa 100644
  199. --- a/db.cpp
  200. +++ b/db.cpp
  201. @@ -17,7 +17,7 @@ unsigned int nWalletDBUpdated;
  202. //
  203.  
  204. static CCriticalSection cs_db;
  205. -static bool fDbEnvInit = false;
  206. +static boost::interprocess::file_lock *dbLock = NULL;
  207. DbEnv dbenv(0);
  208. static map<string, int> mapFileUseCount;
  209. static map<string, Db*> mapDb;
  210. @@ -30,10 +30,10 @@ public:
  211. }
  212. ~CDBInit()
  213. {
  214. - if (fDbEnvInit)
  215. + if (dbLock)
  216. {
  217. dbenv.close(0);
  218. - fDbEnvInit = false;
  219. + delete dbLock; dbLock = NULL;
  220. }
  221. }
  222. }
  223. @@ -54,7 +54,7 @@ CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
  224.  
  225. CRITICAL_BLOCK(cs_db)
  226. {
  227. - if (!fDbEnvInit)
  228. + if (!dbLock)
  229. {
  230. if (fShutdown)
  231. return;
  232. @@ -64,6 +64,16 @@ CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
  233. string strErrorFile = strDataDir + "/db.log";
  234. printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
  235.  
  236. + // if strErrorFile exists and is locked, exception: another Bitcoin must
  237. + // be using this DataDir:
  238. + if (boost::filesystem::exists(strErrorFile))
  239. + {
  240. + dbLock = new boost::interprocess::file_lock(strErrorFile.c_str());
  241. + bool locked = dbLock->try_lock();
  242. + if (!locked)
  243. + throw runtime_error(_("Cannot lock db.log, is bitcoin already running?\n"));
  244. + }
  245. +
  246. dbenv.set_lg_dir(strLogDir.c_str());
  247. dbenv.set_lg_max(10000000);
  248. dbenv.set_lk_max_locks(10000);
  249. @@ -82,7 +92,12 @@ CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
  250. S_IRUSR | S_IWUSR);
  251. if (ret > 0)
  252. throw runtime_error(strprintf("CDB() : error %d opening database environment\n", ret));
  253. - fDbEnvInit = true;
  254. +
  255. + if (!dbLock)
  256. + {
  257. + dbLock = new boost::interprocess::file_lock(strErrorFile.c_str());
  258. + dbLock->lock();
  259. + }
  260. }
  261.  
  262. strFile = pszFile;
  263. @@ -162,8 +177,8 @@ void DBFlush(bool fShutdown)
  264. {
  265. // Flush log data to the actual data file
  266. // on all files that are not in use
  267. - printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
  268. - if (!fDbEnvInit)
  269. + printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", dbLock ? "" : " db not started");
  270. + if (!dbLock)
  271. return;
  272. CRITICAL_BLOCK(cs_db)
  273. {
  274. @@ -191,7 +206,7 @@ void DBFlush(bool fShutdown)
  275. if (mapFileUseCount.empty())
  276. dbenv.log_archive(&listp, DB_ARCH_REMOVE);
  277. dbenv.close(0);
  278. - fDbEnvInit = false;
  279. + delete dbLock; dbLock = NULL;
  280. }
  281. }
  282. }
  283. diff --git a/headers.h b/headers.h
  284. index b0ab18c..881922c 100644
  285. --- a/headers.h
  286. +++ b/headers.h
  287. @@ -68,6 +68,7 @@
  288. #include <boost/filesystem/fstream.hpp>
  289. #include <boost/algorithm/string.hpp>
  290. #include <boost/thread.hpp>
  291. +#include <boost/interprocess/sync/file_lock.hpp>
  292. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  293. #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
  294. #include <boost/date_time/gregorian/gregorian_types.hpp>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement