From a6a5bb7c204130dd4f3ced74446798eb67ea6472 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 2 May 2011 15:34:42 +0200 Subject: Support for name lookups in -connect and -addnode * A new option -dns is introduced that enables name lookups in -connect and -addnode, which is not enabled by default, as it may be considered a security issue. * A Lookup function is added that supports retrieving one or more addresses based on a host name * CAddress constructors (optionally) support name lookups. * The different places in the source code that did name lookups are refactored to use NameLookup or CAddress instead (dns seeding, irc server lookup, getexternalip, ...). * Removed ToStringLog() from CAddress, and switched to ToString(), since it was empty. --- src/net.h | 62 ++++++++++++++++++++++---------------------------------------- 1 file changed, 22 insertions(+), 40 deletions(-) (limited to 'src/net.h') diff --git a/src/net.h b/src/net.h index b3bd74da47..ea12b983e4 100644 --- a/src/net.h +++ b/src/net.h @@ -12,7 +12,7 @@ extern int nBestHeight; -inline unsigned short GetDefaultPort() { return fTestNet ? htons(18333) : htons(8333); } +inline unsigned short GetDefaultPort() { return fTestNet ? 18333 : 8333; } static const unsigned int PUBLISH_HOPS = 5; enum { @@ -23,6 +23,8 @@ enum bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet); +bool Lookup(const char *pszName, vector& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); +bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); bool GetMyExternalIP(unsigned int& ipRet); bool AddAddress(CAddress addr, int64 nTimePenalty=0); void AddressCurrentlyConnected(const CAddress& addr); @@ -156,7 +158,7 @@ public: { Init(); ip = ipIn; - port = (portIn == 0 ? GetDefaultPort() : portIn); + port = htons(portIn == 0 ? GetDefaultPort() : portIn); nServices = nServicesIn; } @@ -168,54 +170,38 @@ public: nServices = nServicesIn; } - explicit CAddress(const char* pszIn, uint64 nServicesIn=NODE_NETWORK) + explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK) { Init(); - SetAddress(pszIn); - nServices = nServicesIn; + Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn); } - explicit CAddress(string strIn, uint64 nServicesIn=NODE_NETWORK) + explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK) { Init(); - SetAddress(strIn.c_str()); - nServices = nServicesIn; + Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true); } - void Init() + explicit CAddress(string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK) { - nServices = NODE_NETWORK; - memcpy(pchReserved, pchIPv4, sizeof(pchReserved)); - ip = INADDR_NONE; - port = GetDefaultPort(); - nTime = 100000000; - nLastTry = 0; + Init(); + Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn); } - bool SetAddress(const char* pszIn) + explicit CAddress(string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK) { - ip = INADDR_NONE; - port = GetDefaultPort(); - char psz[100]; - strlcpy(psz, pszIn, sizeof(psz)); - unsigned int a=0, b=0, c=0, d=0, e=0; - if (sscanf(psz, "%u.%u.%u.%u:%u", &a, &b, &c, &d, &e) < 4) - return false; - char* pszPort = strchr(psz, ':'); - if (pszPort) - { - *pszPort++ = '\0'; - port = htons(atoi(pszPort)); - if (atoi(pszPort) < 0 || atoi(pszPort) > USHRT_MAX) - port = htons(USHRT_MAX); - } - ip = inet_addr(psz); - return IsValid(); + Init(); + Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true); } - bool SetAddress(string strIn) + void Init() { - return SetAddress(strIn.c_str()); + nServices = NODE_NETWORK; + memcpy(pchReserved, pchIPv4, sizeof(pchReserved)); + ip = INADDR_NONE; + port = htons(GetDefaultPort()); + nTime = 100000000; + nLastTry = 0; } IMPLEMENT_SERIALIZE @@ -330,11 +316,6 @@ public: return strprintf("%u", ntohs(port)); } - string ToStringLog() const - { - return ""; - } - string ToString() const { return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port)); @@ -460,6 +441,7 @@ public: extern bool fClient; +extern bool fAllowDNS; extern uint64 nLocalServices; extern CAddress addrLocalHost; extern CNode* pnodeLocalHost; -- cgit v1.2.3