Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/init.cpp b/init.cpp
- index 1cbe497..c5b9852 100644
- --- a/init.cpp
- +++ b/init.cpp
- @@ -155,6 +156,8 @@ bool AppInit2(int argc, char* argv[])
- " -gen=0 \t " + _("Don't generate coins\n") +
- " -min \t " + _("Start minimized\n") +
- " -datadir=<dir> \t " + _("Specify data directory\n") +
- + " -port=<port> \t " + _("Specify listen port\n") +
- + " -rpcport=<port> \t " + _("Specify JSON-RPC port\n") +
- " -proxy=<ip:port>\t " + _("Connect through socks4 proxy\n") +
- " -addnode=<ip> \t " + _("Add a node to connect to\n") +
- " -connect=<ip> \t " + _("Connect only to the specified node\n") +
- @@ -173,6 +176,9 @@ bool AppInit2(int argc, char* argv[])
- return false;
- }
- + if (mapArgs.count("-port"))
- + SetListenPort(atoi(mapArgs["-port"]));
- +
- if (mapArgs.count("-debug"))
- fDebug = true;
- @@ -210,7 +216,7 @@ bool AppInit2(int argc, char* argv[])
- //
- #if defined(__WXMSW__) && defined(GUI)
- // todo: wxSingleInstanceChecker wasn't working on Linux, never deleted its lock file
- - // maybe should go by whether successfully bind port 8333 instead
- + // maybe should go by whether successfully bind port instead
- wxString strMutexName = wxString("bitcoin_running.") + getenv("HOMEPATH");
- for (int i = 0; i < strMutexName.size(); i++)
- if (!isalnum(strMutexName[i]))
- diff --git a/net.cpp b/net.cpp
- index f121acd..6d79309 100644
- --- a/net.cpp
- +++ b/net.cpp
- @@ -18,7 +18,7 @@ bool OpenNetworkConnection(const CAddress& addrConnect);
- //
- bool fClient = false;
- uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
- -CAddress addrLocalHost(0, DEFAULT_PORT, nLocalServices);
- +CAddress addrLocalHost(0, 0, nLocalServices);
- CNode* pnodeLocalHost = NULL;
- uint64 nLocalHostNonce = 0;
- array<int, 10> vnThreadsRunning;
- @@ -957,7 +957,7 @@ void ThreadOpenConnections2(void* parg)
- // Randomize the order in a deterministic way, putting the standard port first
- int64 nRandomizer = (uint64)(nStart * 4951 + addr.nLastTry * 9567851 + addr.ip * 7789) % (2 * 60 * 60);
- - if (addr.port != DEFAULT_PORT)
- + if (addr.port != GetDefaultPortNS())
- nRandomizer += 2 * 60 * 60;
- // Last seen Base retry frequency
- @@ -1124,9 +1124,27 @@ void ThreadMessageHandler2(void* parg)
- +static unsigned short nListenPortNS = 0;
- +void SetListenPort(unsigned short port) // port in host byte order
- +{
- + if (nListenPortNS != 0)
- + throw(runtime_error("Error, must call SetListenPort exactly once."));
- + nListenPortNS = htons(port);
- + addrLocalHost.port = nListenPortNS;
- +}
- +unsigned short GetListenPortNS() // returns port in network byte order
- +{
- + if (nListenPortNS == 0)
- + nListenPortNS = GetDefaultPortNS();
- + return nListenPortNS;
- +}
- +unsigned short GetDefaultPortNS() // returns port in network byte order
- +{
- + return htons(8333);
- +}
- bool BindListenPort(string& strError)
- {
- @@ -1183,7 +1201,7 @@ bool BindListenPort(string& strError)
- memset(&sockaddr, 0, sizeof(sockaddr));
- sockaddr.sin_family = AF_INET;
- sockaddr.sin_addr.s_addr = INADDR_ANY; // bind to all IPs on this computer
- - sockaddr.sin_port = DEFAULT_PORT;
- + sockaddr.sin_port = GetListenPortNS();
- if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
- {
- int nErr = WSAGetLastError();
- @@ -1225,7 +1243,7 @@ void StartNode(void* parg)
- printf("host ip %d: %s\n", i, CAddress(*(unsigned int*)phostent->h_addr_list[i]).ToStringIP().c_str());
- for (int i = 0; phostent->h_addr_list[i] != NULL; i++)
- {
- - CAddress addr(*(unsigned int*)phostent->h_addr_list[i], DEFAULT_PORT, nLocalServices);
- + CAddress addr(*(unsigned int*)phostent->h_addr_list[i], GetListenPortNS(), nLocalServices);
- if (addr.IsValid() && addr.GetByte(3) != 127)
- {
- addrLocalHost = addr;
- @@ -1253,7 +1271,7 @@ void StartNode(void* parg)
- printf("ipv4 %s: %s\n", ifa->ifa_name, pszIP);
- // Take the first IP that isn't loopback 127.x.x.x
- - CAddress addr(*(unsigned int*)&s4->sin_addr, DEFAULT_PORT, nLocalServices);
- + CAddress addr(*(unsigned int*)&s4->sin_addr, GetListenPortNS(), nLocalServices);
- if (addr.IsValid() && addr.GetByte(3) != 127)
- {
- addrLocalHost = addr;
- diff --git a/net.h b/net.h
- index 43c49a2..8914b7e 100644
- --- a/net.h
- +++ b/net.h
- @@ -12,7 +12,6 @@ extern int nBestHeight;
- -static const unsigned short DEFAULT_PORT = 0x8d20; // htons(8333)
- static const unsigned int PUBLISH_HOPS = 5;
- enum
- {
- @@ -22,6 +21,9 @@ enum
- +void SetListenPort(unsigned short nPort);
- +unsigned short GetListenPortNS();
- +unsigned short GetDefaultPortNS();
- bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet);
- bool GetMyExternalIP(unsigned int& ipRet);
- bool AddAddress(CAddress addr);
- @@ -153,7 +155,7 @@ public:
- Init();
- }
- - CAddress(unsigned int ipIn, unsigned short portIn=DEFAULT_PORT, uint64 nServicesIn=NODE_NETWORK)
- + CAddress(unsigned int ipIn, unsigned short portIn, uint64 nServicesIn=NODE_NETWORK)
- {
- Init();
- ip = ipIn;
- @@ -188,7 +190,7 @@ public:
- nServices = NODE_NETWORK;
- memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
- ip = INADDR_NONE;
- - port = DEFAULT_PORT;
- + port = GetDefaultPortNS();
- nTime = GetAdjustedTime();
- nLastTry = 0;
- }
- @@ -196,7 +198,7 @@ public:
- bool SetAddress(const char* pszIn)
- {
- ip = INADDR_NONE;
- - port = DEFAULT_PORT;
- + port = GetDefaultPortNS();
- char psz[100];
- strlcpy(psz, pszIn, sizeof(psz));
- unsigned int a=0, b=0, c=0, d=0, e=0;
- diff --git a/rpc.cpp b/rpc.cpp
- index 5ae9e88..34a5cba 100644
- --- a/rpc.cpp
- +++ b/rpc.cpp
- @@ -840,7 +840,13 @@ string JSONRPCReply(const Value& result, const Value& error, const Value& id)
- }
- -
- +unsigned short nRPCPort()
- +{
- + unsigned short n = 8332;
- + if (!mapArgs["-rpcport"].empty())
- + n = atoi(mapArgs["-rpcport"]);
- + return n;
- +}
- void ThreadRPCServer(void* parg)
- {
- @@ -883,7 +889,7 @@ void ThreadRPCServer2(void* parg)
- // Bind to loopback 127.0.0.1 so the socket can only be accessed locally
- boost::asio::io_service io_service;
- - tcp::endpoint endpoint(boost::asio::ip::address_v4::loopback(), 8332);
- + tcp::endpoint endpoint(boost::asio::ip::address_v4::loopback(), nRPCPort());
- tcp::acceptor acceptor(io_service, endpoint);
- loop
- @@ -980,7 +986,8 @@ Value CallRPC(const string& strMethod, const Array& params)
- GetConfigFile().c_str()));
- // Connect to localhost
- - tcp::iostream stream("127.0.0.1", "8332");
- + std::stringstream ss; ss << nRPCPort();
- + tcp::iostream stream("127.0.0.1", ss.str());
- if (stream.fail())
- throw runtime_error("couldn't connect to server");
- diff --git a/db.cpp b/db.cpp
- index fa4ae90..5c4b0fa 100644
- --- a/db.cpp
- +++ b/db.cpp
- @@ -17,7 +17,7 @@ unsigned int nWalletDBUpdated;
- //
- static CCriticalSection cs_db;
- -static bool fDbEnvInit = false;
- +static boost::interprocess::file_lock *dbLock = NULL;
- DbEnv dbenv(0);
- static map<string, int> mapFileUseCount;
- static map<string, Db*> mapDb;
- @@ -30,10 +30,10 @@ public:
- }
- ~CDBInit()
- {
- - if (fDbEnvInit)
- + if (dbLock)
- {
- dbenv.close(0);
- - fDbEnvInit = false;
- + delete dbLock; dbLock = NULL;
- }
- }
- }
- @@ -54,7 +54,7 @@ CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
- CRITICAL_BLOCK(cs_db)
- {
- - if (!fDbEnvInit)
- + if (!dbLock)
- {
- if (fShutdown)
- return;
- @@ -64,6 +64,16 @@ CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
- string strErrorFile = strDataDir + "/db.log";
- printf("dbenv.open strLogDir=%s strErrorFile=%s\n", strLogDir.c_str(), strErrorFile.c_str());
- + // if strErrorFile exists and is locked, exception: another Bitcoin must
- + // be using this DataDir:
- + if (boost::filesystem::exists(strErrorFile))
- + {
- + dbLock = new boost::interprocess::file_lock(strErrorFile.c_str());
- + bool locked = dbLock->try_lock();
- + if (!locked)
- + throw runtime_error(_("Cannot lock db.log, is bitcoin already running?\n"));
- + }
- +
- dbenv.set_lg_dir(strLogDir.c_str());
- dbenv.set_lg_max(10000000);
- dbenv.set_lk_max_locks(10000);
- @@ -82,7 +92,12 @@ CDB::CDB(const char* pszFile, const char* pszMode) : pdb(NULL)
- S_IRUSR | S_IWUSR);
- if (ret > 0)
- throw runtime_error(strprintf("CDB() : error %d opening database environment\n", ret));
- - fDbEnvInit = true;
- +
- + if (!dbLock)
- + {
- + dbLock = new boost::interprocess::file_lock(strErrorFile.c_str());
- + dbLock->lock();
- + }
- }
- strFile = pszFile;
- @@ -162,8 +177,8 @@ void DBFlush(bool fShutdown)
- {
- // Flush log data to the actual data file
- // on all files that are not in use
- - printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
- - if (!fDbEnvInit)
- + printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", dbLock ? "" : " db not started");
- + if (!dbLock)
- return;
- CRITICAL_BLOCK(cs_db)
- {
- @@ -191,7 +206,7 @@ void DBFlush(bool fShutdown)
- if (mapFileUseCount.empty())
- dbenv.log_archive(&listp, DB_ARCH_REMOVE);
- dbenv.close(0);
- - fDbEnvInit = false;
- + delete dbLock; dbLock = NULL;
- }
- }
- }
- diff --git a/headers.h b/headers.h
- index b0ab18c..881922c 100644
- --- a/headers.h
- +++ b/headers.h
- @@ -68,6 +68,7 @@
- #include <boost/filesystem/fstream.hpp>
- #include <boost/algorithm/string.hpp>
- #include <boost/thread.hpp>
- +#include <boost/interprocess/sync/file_lock.hpp>
- #include <boost/interprocess/sync/interprocess_mutex.hpp>
- #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
- #include <boost/date_time/gregorian/gregorian_types.hpp>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement