diff options
Diffstat (limited to 'src/netbase.cpp')
-rw-r--r-- | src/netbase.cpp | 167 |
1 files changed, 126 insertions, 41 deletions
diff --git a/src/netbase.cpp b/src/netbase.cpp index 6697a13921..e6d4f16ba0 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2020 The Bitcoin Core developers +// Copyright (c) 2009-2021 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -23,8 +23,6 @@ #ifndef WIN32 #include <fcntl.h> -#else -#include <codecvt> #endif #ifdef USE_POLL @@ -33,8 +31,8 @@ // Settings static Mutex g_proxyinfo_mutex; -static proxyType proxyInfo[NET_MAX] GUARDED_BY(g_proxyinfo_mutex); -static proxyType nameProxy GUARDED_BY(g_proxyinfo_mutex); +static Proxy proxyInfo[NET_MAX] GUARDED_BY(g_proxyinfo_mutex); +static Proxy nameProxy GUARDED_BY(g_proxyinfo_mutex); int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT; bool fNameLookup = DEFAULT_NAME_LOOKUP; @@ -98,6 +96,9 @@ enum Network ParseNetwork(const std::string& net_in) { if (net == "i2p") { return NET_I2P; } + if (net == "cjdns") { + return NET_CJDNS; + } return NET_UNROUTABLE; } @@ -122,7 +123,7 @@ std::vector<std::string> GetNetworkNames(bool append_unroutable) std::vector<std::string> names; for (int n = 0; n < NET_MAX; ++n) { const enum Network network{static_cast<Network>(n)}; - if (network == NET_UNROUTABLE || network == NET_CJDNS || network == NET_INTERNAL) continue; + if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue; names.emplace_back(GetNetworkName(network)); } if (append_unroutable) { @@ -604,7 +605,7 @@ bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nT return true; } -bool SetProxy(enum Network net, const proxyType &addrProxy) { +bool SetProxy(enum Network net, const Proxy &addrProxy) { assert(net >= 0 && net < NET_MAX); if (!addrProxy.IsValid()) return false; @@ -613,7 +614,7 @@ bool SetProxy(enum Network net, const proxyType &addrProxy) { return true; } -bool GetProxy(enum Network net, proxyType &proxyInfoOut) { +bool GetProxy(enum Network net, Proxy &proxyInfoOut) { assert(net >= 0 && net < NET_MAX); LOCK(g_proxyinfo_mutex); if (!proxyInfo[net].IsValid()) @@ -622,7 +623,7 @@ bool GetProxy(enum Network net, proxyType &proxyInfoOut) { return true; } -bool SetNameProxy(const proxyType &addrProxy) { +bool SetNameProxy(const Proxy &addrProxy) { if (!addrProxy.IsValid()) return false; LOCK(g_proxyinfo_mutex); @@ -630,7 +631,7 @@ bool SetNameProxy(const proxyType &addrProxy) { return true; } -bool GetNameProxy(proxyType &nameProxyOut) { +bool GetNameProxy(Proxy &nameProxyOut) { LOCK(g_proxyinfo_mutex); if(!nameProxy.IsValid()) return false; @@ -652,7 +653,7 @@ bool IsProxy(const CNetAddr &addr) { return false; } -bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed) +bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed) { // first connect to proxy server if (!ConnectSocketDirectly(proxy.proxy, sock, nTimeout, true)) { @@ -675,42 +676,36 @@ bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uin return true; } -bool LookupSubNet(const std::string& strSubnet, CSubNet& ret, DNSLookupFn dns_lookup_function) +bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out) { - if (!ValidAsCString(strSubnet)) { + if (!ValidAsCString(subnet_str)) { return false; } - size_t slash = strSubnet.find_last_of('/'); - std::vector<CNetAddr> vIP; - std::string strAddress = strSubnet.substr(0, slash); - // TODO: Use LookupHost(const std::string&, CNetAddr&, bool) instead to just get - // one CNetAddr. - if (LookupHost(strAddress, vIP, 1, false, dns_lookup_function)) - { - CNetAddr network = vIP[0]; - if (slash != strSubnet.npos) - { - std::string strNetmask = strSubnet.substr(slash + 1); - uint8_t n; - if (ParseUInt8(strNetmask, &n)) { - // If valid number, assume CIDR variable-length subnet masking - ret = CSubNet(network, n); - return ret.IsValid(); - } - else // If not a valid number, try full netmask syntax - { - // Never allow lookup for netmask - if (LookupHost(strNetmask, vIP, 1, false, dns_lookup_function)) { - ret = CSubNet(network, vIP[0]); - return ret.IsValid(); + const size_t slash_pos{subnet_str.find_last_of('/')}; + const std::string str_addr{subnet_str.substr(0, slash_pos)}; + CNetAddr addr; + + if (LookupHost(str_addr, addr, /*fAllowLookup=*/false)) { + if (slash_pos != subnet_str.npos) { + const std::string netmask_str{subnet_str.substr(slash_pos + 1)}; + uint8_t netmask; + if (ParseUInt8(netmask_str, &netmask)) { + // Valid number; assume CIDR variable-length subnet masking. + subnet_out = CSubNet{addr, netmask}; + return subnet_out.IsValid(); + } else { + // Invalid number; try full netmask syntax. Never allow lookup for netmask. + CNetAddr full_netmask; + if (LookupHost(netmask_str, full_netmask, /*fAllowLookup=*/false)) { + subnet_out = CSubNet{addr, full_netmask}; + return subnet_out.IsValid(); } } - } - else - { - ret = CSubNet(network); - return ret.IsValid(); + } else { + // Single IP subnet (<ipv4>/32 or <ipv6>/128). + subnet_out = CSubNet{addr}; + return subnet_out.IsValid(); } } return false; @@ -742,3 +737,93 @@ void InterruptSocks5(bool interrupt) { interruptSocks5Recv = interrupt; } + +bool IsBadPort(uint16_t port) +{ + /* Don't forget to update doc/p2p-bad-ports.md if you change this list. */ + + switch (port) { + case 1: // tcpmux + case 7: // echo + case 9: // discard + case 11: // systat + case 13: // daytime + case 15: // netstat + case 17: // qotd + case 19: // chargen + case 20: // ftp data + case 21: // ftp access + case 22: // ssh + case 23: // telnet + case 25: // smtp + case 37: // time + case 42: // name + case 43: // nicname + case 53: // domain + case 69: // tftp + case 77: // priv-rjs + case 79: // finger + case 87: // ttylink + case 95: // supdup + case 101: // hostname + case 102: // iso-tsap + case 103: // gppitnp + case 104: // acr-nema + case 109: // pop2 + case 110: // pop3 + case 111: // sunrpc + case 113: // auth + case 115: // sftp + case 117: // uucp-path + case 119: // nntp + case 123: // NTP + case 135: // loc-srv /epmap + case 137: // netbios + case 139: // netbios + case 143: // imap2 + case 161: // snmp + case 179: // BGP + case 389: // ldap + case 427: // SLP (Also used by Apple Filing Protocol) + case 465: // smtp+ssl + case 512: // print / exec + case 513: // login + case 514: // shell + case 515: // printer + case 526: // tempo + case 530: // courier + case 531: // chat + case 532: // netnews + case 540: // uucp + case 548: // AFP (Apple Filing Protocol) + case 554: // rtsp + case 556: // remotefs + case 563: // nntp+ssl + case 587: // smtp (rfc6409) + case 601: // syslog-conn (rfc3195) + case 636: // ldap+ssl + case 989: // ftps-data + case 990: // ftps + case 993: // ldap+ssl + case 995: // pop3+ssl + case 1719: // h323gatestat + case 1720: // h323hostcall + case 1723: // pptp + case 2049: // nfs + case 3659: // apple-sasl / PasswordServer + case 4045: // lockd + case 5060: // sip + case 5061: // sips + case 6000: // X11 + case 6566: // sane-port + case 6665: // Alternate IRC + case 6666: // Alternate IRC + case 6667: // Standard IRC + case 6668: // Alternate IRC + case 6669: // Alternate IRC + case 6697: // IRC + TLS + case 10080: // Amanda + return true; + } + return false; +} |