From a95e546089f82078037d9b78145e0d78fa6c4b12 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Sat, 19 Sep 2015 00:09:50 +0300 Subject: [PATCH] Use uint16_t for port number. --- src/init.cpp | 4 ++-- src/irc.cpp | 4 ++-- src/net.cpp | 10 +++++----- src/net.h | 1 + src/netbase.cpp | 28 ++++++++++++++-------------- src/netbase.h | 22 +++++++++++----------- src/qt/optionsmodel.cpp | 14 +++++++------- 7 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 7cc0e0f..1eb28a5 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -634,7 +634,7 @@ bool AppInit2() CService addrProxy; bool fProxy = false; if (mapArgs.count("-proxy")) { - addrProxy = CService(mapArgs["-proxy"], 9050); + addrProxy = CService(mapArgs["-proxy"], nSocksDefault); if (!addrProxy.IsValid()) return InitError(strprintf(_("Invalid -proxy address: '%s'"), mapArgs["-proxy"].c_str())); @@ -656,7 +656,7 @@ bool AppInit2() if (!mapArgs.count("-tor")) addrOnion = addrProxy; else - addrOnion = CService(mapArgs["-tor"], 9050); + addrOnion = CService(mapArgs["-tor"], nSocksDefault); if (!addrOnion.IsValid()) return InitError(strprintf(_("Invalid -tor address: '%s'"), mapArgs["-tor"].c_str())); SetProxy(NET_TOR, addrOnion, 5); diff --git a/src/irc.cpp b/src/irc.cpp index 25033df..7c00d50 100644 --- a/src/irc.cpp +++ b/src/irc.cpp @@ -228,9 +228,9 @@ void ThreadIRCSeed2(void* parg) while (!fShutdown) { - CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org + CService addrConnect("92.243.23.21", (uint16_t)6667); // irc.lfnet.org - CService addrIRC("irc.lfnet.org", 6667, true); + CService addrIRC("irc.lfnet.org", (uint16_t)6667, true); if (addrIRC.IsValid()) addrConnect = addrIRC; diff --git a/src/net.cpp b/src/net.cpp index ab079cf..15c0e7f 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -48,7 +48,7 @@ void ThreadDNSAddressSeed2(void* parg); struct LocalServiceInfo { int nScore; - int nPort; + uint16_t nPort; }; // @@ -64,7 +64,7 @@ static bool vfReachable[NET_MAX] = {}; static bool vfLimited[NET_MAX] = {}; static CNode* pnodeLocalHost = NULL; static CNode* pnodeSync = NULL; -CAddress addrSeenByPeer(CService("0.0.0.0", 0), nLocalServices); +CAddress addrSeenByPeer(CService("0.0.0.0", (uint16_t)0), nLocalServices); uint64_t nLocalHostNonce = 0; boost::array vnThreadsRunning; static std::vector vhListenSocket; @@ -138,7 +138,7 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer) // get best local address for a particular peer as a CAddress CAddress GetLocalAddress(const CNetAddr *paddrPeer) { - CAddress ret(CService("0.0.0.0",0),0); + CAddress ret(CService("0.0.0.0", (uint16_t)0), 0); CService addr; if (GetLocal(addr, paddrPeer)) { @@ -497,7 +497,7 @@ void CNode::PushVersion() } if (!fHidden) { - addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0",0))); + addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0",(uint16_t)0))); addrMe = GetLocalAddress(&addr); } @@ -1898,7 +1898,7 @@ void StartNode(void* parg) } if (pnodeLocalHost == NULL) - pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService("127.0.0.1", 0), nLocalServices)); + pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService("127.0.0.1", (uint16_t)0), nLocalServices)); Discover(); diff --git a/src/net.h b/src/net.h index d1696fc..9af8864 100644 --- a/src/net.h +++ b/src/net.h @@ -28,6 +28,7 @@ class CNode; class CBlockIndex; extern int nBestHeight; +const uint16_t nSocksDefault = 9050; inline uint64_t ReceiveBufferSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); } diff --git a/src/netbase.cpp b/src/netbase.cpp index 320db3e..d5c4f92 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -44,7 +44,7 @@ enum Network ParseNetwork(std::string net) { return NET_UNROUTABLE; } -void SplitHostPort(std::string in, int &portOut, std::string &hostOut) { +void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut) { size_t colon = in.find_last_of(':'); // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator bool fHaveColon = colon != in.npos; @@ -141,11 +141,11 @@ bool LookupHostNumeric(const char *pszName, std::vector& vIP, unsigned return LookupHost(pszName, vIP, nMaxSolutions, false); } -bool Lookup(const char *pszName, std::vector& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions) +bool Lookup(const char *pszName, std::vector& vAddr, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions) { if (pszName[0] == 0) return false; - int port = portDefault; + uint16_t port = portDefault; std::string hostname = ""; SplitHostPort(std::string(pszName), port, hostname); @@ -159,7 +159,7 @@ bool Lookup(const char *pszName, std::vector& vAddr, int portDefault, return true; } -bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup) +bool Lookup(const char *pszName, CService& addr, uint16_t portDefault, bool fAllowLookup) { std::vector vService; bool fRet = Lookup(pszName, vService, portDefault, fAllowLookup, 1); @@ -169,7 +169,7 @@ bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLoo return true; } -bool LookupNumeric(const char *pszName, CService& addr, int portDefault) +bool LookupNumeric(const char *pszName, CService& addr, uint16_t portDefault) { return Lookup(pszName, addr, portDefault, false); } @@ -218,7 +218,7 @@ bool static Socks4(const CService &addrDest, SOCKET& hSocket) return true; } -bool static Socks5(string strDest, int port, SOCKET& hSocket) +bool static Socks5(string strDest, uint16_t port, SOCKET& hSocket) { printf("SOCKS5 connecting %s\n", strDest.c_str()); if (strDest.size() > 255) @@ -516,10 +516,10 @@ bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout) return true; } -bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout) +bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, uint16_t portDefault, int nTimeout) { string strDest; - int port = portDefault; + uint16_t port = portDefault; SplitHostPort(string(pszDest), port, strDest); SOCKET hSocket = INVALID_SOCKET; @@ -788,7 +788,7 @@ std::string CNetAddr::ToStringIP() const return EncodeBase32(&ip[6], 10) + ".onion"; if (IsI2P()) return EncodeBase32(&ip[6], 10) + ".oc.b32.i2p"; - CService serv(*this, 0); + CService serv(*this, (uint16_t)0); #ifdef USE_IPV6 struct sockaddr_storage sockaddr; #else @@ -1022,16 +1022,16 @@ CService::CService() Init(); } -CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn) +CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn) { } -CService::CService(const struct in_addr& ipv4Addr, unsigned short portIn) : CNetAddr(ipv4Addr), port(portIn) +CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn) { } #ifdef USE_IPV6 -CService::CService(const struct in6_addr& ipv6Addr, unsigned short portIn) : CNetAddr(ipv6Addr), port(portIn) +CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn) { } #endif @@ -1072,7 +1072,7 @@ CService::CService(const char *pszIpPort, bool fAllowLookup) *this = ip; } -CService::CService(const char *pszIpPort, int portDefault, bool fAllowLookup) +CService::CService(const char *pszIpPort, uint16_t portDefault, bool fAllowLookup) { Init(); CService ip; @@ -1088,7 +1088,7 @@ CService::CService(const std::string &strIpPort, bool fAllowLookup) *this = ip; } -CService::CService(const std::string &strIpPort, int portDefault, bool fAllowLookup) +CService::CService(const std::string &strIpPort, uint16_t portDefault, bool fAllowLookup) { Init(); CService ip; diff --git a/src/netbase.h b/src/netbase.h index 765609c..9300003 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -96,15 +96,15 @@ class CService : public CNetAddr public: CService(); - CService(const CNetAddr& ip, unsigned short port); - CService(const struct in_addr& ipv4Addr, unsigned short port); + CService(const CNetAddr& ip, uint16_t port); + CService(const struct in_addr& ipv4Addr, uint16_t port); CService(const struct sockaddr_in& addr); - explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false); + explicit CService(const char *pszIpPort, uint16_t portDefault, bool fAllowLookup = false); explicit CService(const char *pszIpPort, bool fAllowLookup = false); - explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false); + explicit CService(const std::string& strIpPort, uint16_t portDefault, bool fAllowLookup = false); explicit CService(const std::string& strIpPort, bool fAllowLookup = false); void Init(); - void SetPort(unsigned short portIn); + void SetPort(uint16_t portIn); unsigned short GetPort() const; bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const; bool SetSockAddr(const struct sockaddr* paddr); @@ -118,7 +118,7 @@ class CService : public CNetAddr void print() const; #ifdef USE_IPV6 - CService(const struct in6_addr& ipv6Addr, unsigned short port); + CService(const struct in6_addr& ipv6Addr, uint16_t port); CService(const struct sockaddr_in6& addr); #endif @@ -136,7 +136,7 @@ class CService : public CNetAddr typedef std::pair proxyType; enum Network ParseNetwork(std::string net); -void SplitHostPort(std::string in, int &portOut, std::string &hostOut); +void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut); bool SetProxy(enum Network net, CService addrProxy, int nSocksVersion = 5); bool GetProxy(enum Network net, proxyType &proxyInfoOut); bool IsProxy(const CNetAddr &addr); @@ -144,10 +144,10 @@ bool SetNameProxy(CService addrProxy, int nSocksVersion = 5); bool HaveNameProxy(); bool LookupHost(const char *pszName, std::vector& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true); bool LookupHostNumeric(const char *pszName, std::vector& vIP, unsigned int nMaxSolutions = 0); -bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true); -bool Lookup(const char *pszName, std::vector& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0); -bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0); +bool Lookup(const char *pszName, CService& addr, uint16_t portDefault = 0, bool fAllowLookup = true); +bool Lookup(const char *pszName, std::vector& vAddr, uint16_t portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0); +bool LookupNumeric(const char *pszName, CService& addr, uint16_t portDefault = 0); bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout); -bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout); +bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, uint16_t portDefault = 0, int nTimeout = nConnectTimeout); #endif diff --git a/src/qt/optionsmodel.cpp b/src/qt/optionsmodel.cpp index 3d93577..56e1df4 100644 --- a/src/qt/optionsmodel.cpp +++ b/src/qt/optionsmodel.cpp @@ -141,7 +141,7 @@ QVariant OptionsModel::data(const QModelIndex & index, int role) const if (GetProxy(NET_IPV4, proxy)) return QVariant(proxy.first.GetPort()); else - return QVariant(9050); + return QVariant(nSocksDefault); } case ProxySocksVersion: return settings.value("nSocksVersion", 5); @@ -159,7 +159,7 @@ QVariant OptionsModel::data(const QModelIndex & index, int role) const if (GetProxy(NET_TOR, proxy)) return QVariant(proxy.first.GetPort()); else - return QVariant(9050); + return QVariant(nSocksDefault); } case TorOnly: return settings.value("fTorOnly", false); @@ -216,7 +216,7 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in break; case ProxyIP: { proxyType proxy; - proxy.first = CService("127.0.0.1", 9050); + proxy.first = CService("127.0.0.1", nSocksDefault); GetProxy(NET_IPV4, proxy); CNetAddr addr(value.toString().toStdString()); @@ -227,7 +227,7 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in break; case ProxyPort: { proxyType proxy; - proxy.first = CService("127.0.0.1", 9050); + proxy.first = CService("127.0.0.1", nSocksDefault); GetProxy(NET_IPV4, proxy); proxy.first.SetPort(value.toInt()); @@ -252,7 +252,7 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in break; case TorIP: { proxyType proxy; - proxy.first = CService("127.0.0.1", 9050); + proxy.first = CService("127.0.0.1", nSocksDefault); GetProxy(NET_TOR, proxy); CNetAddr addr(value.toString().toStdString()); @@ -263,10 +263,10 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in break; case TorPort: { proxyType proxy; - proxy.first = CService("127.0.0.1", 9050); + proxy.first = CService("127.0.0.1", nSocksDefault); GetProxy(NET_TOR, proxy); - proxy.first.SetPort(value.toInt()); + proxy.first.SetPort((uint16_t)value.toUInt()); settings.setValue("addrTor", proxy.first.ToStringIPPort().c_str()); successful = ApplyTorSettings(); } -- 1.7.1