Skip to content

Commit c5532e1

Browse files
committed
Merge pull request #1399 from sipa/ipparse
Improve parsing of IPv6 addresses
2 parents 7596a03 + c4971e2 commit c5532e1

File tree

3 files changed

+119
-43
lines changed

3 files changed

+119
-43
lines changed

src/netbase.cpp

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ enum Network ParseNetwork(std::string net) {
3333
return NET_UNROUTABLE;
3434
}
3535

36+
void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
37+
size_t colon = in.find_last_of(':');
38+
// if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
39+
bool fHaveColon = colon != in.npos;
40+
bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
41+
bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
42+
if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
43+
char *endp = NULL;
44+
int n = strtol(in.c_str() + colon + 1, &endp, 10);
45+
if (endp && *endp == 0 && n >= 0) {
46+
in = in.substr(0, colon);
47+
if (n > 0 && n < 0x10000)
48+
portOut = n;
49+
}
50+
}
51+
if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
52+
hostOut = in.substr(1, in.size()-2);
53+
else
54+
hostOut = in;
55+
}
56+
3657
bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
3758
{
3859
vIP.clear();
@@ -114,36 +135,11 @@ bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault,
114135
if (pszName[0] == 0)
115136
return false;
116137
int port = portDefault;
117-
char psz[256];
118-
char *pszHost = psz;
119-
strlcpy(psz, pszName, sizeof(psz));
120-
char* pszColon = strrchr(psz+1,':');
121-
char *pszPortEnd = NULL;
122-
int portParsed = pszColon ? strtoul(pszColon+1, &pszPortEnd, 10) : 0;
123-
if (pszColon && pszPortEnd && pszPortEnd[0] == 0)
124-
{
125-
if (psz[0] == '[' && pszColon[-1] == ']')
126-
{
127-
pszHost = psz+1;
128-
pszColon[-1] = 0;
129-
}
130-
else
131-
pszColon[0] = 0;
132-
if (port >= 0 && port <= USHRT_MAX)
133-
port = portParsed;
134-
}
135-
else
136-
{
137-
if (psz[0] == '[' && psz[strlen(psz)-1] == ']')
138-
{
139-
pszHost = psz+1;
140-
psz[strlen(psz)-1] = 0;
141-
}
142-
143-
}
138+
std::string hostname = "";
139+
SplitHostPort(std::string(pszName), port, hostname);
144140

145141
std::vector<CNetAddr> vIP;
146-
bool fRet = LookupIntern(pszHost, vIP, nMaxSolutions, fAllowLookup);
142+
bool fRet = LookupIntern(hostname.c_str(), vIP, nMaxSolutions, fAllowLookup);
147143
if (!fRet)
148144
return false;
149145
vAddr.resize(vIP.size());
@@ -496,22 +492,9 @@ bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
496492

497493
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout)
498494
{
499-
string strDest(pszDest);
495+
string strDest;
500496
int port = portDefault;
501-
502-
// split hostname and port
503-
size_t colon = strDest.find_last_of(':');
504-
if (colon != strDest.npos) {
505-
char *endp = NULL;
506-
int n = strtol(pszDest + colon + 1, &endp, 10);
507-
if (endp && *endp == 0 && n >= 0) {
508-
strDest = strDest.substr(0, colon);
509-
if (n > 0 && n < 0x10000)
510-
port = n;
511-
}
512-
}
513-
if (strDest[0] == '[' && strDest[strDest.size()-1] == ']')
514-
strDest = strDest.substr(1, strDest.size()-2);
497+
SplitHostPort(string(pszDest), port, strDest);
515498

516499
SOCKET hSocket = INVALID_SOCKET;
517500
CService addrResolved(CNetAddr(strDest, fNameLookup && !nameproxyInfo.second), port);

src/netbase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class CService : public CNetAddr
133133
};
134134

135135
enum Network ParseNetwork(std::string net);
136+
void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
136137
bool SetProxy(enum Network net, CService addrProxy, int nSocksVersion = 5);
137138
bool GetProxy(enum Network net, CService &addrProxy);
138139
bool IsProxy(const CNetAddr &addr);

src/test/netbase_tests.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <boost/test/unit_test.hpp>
2+
3+
#include <string>
4+
#include <vector>
5+
6+
#include "netbase.h"
7+
8+
using namespace std;
9+
10+
BOOST_AUTO_TEST_SUITE(netbase_tests)
11+
12+
BOOST_AUTO_TEST_CASE(netbase_networks)
13+
{
14+
BOOST_CHECK(CNetAddr("127.0.0.1").GetNetwork() == NET_UNROUTABLE);
15+
BOOST_CHECK(CNetAddr("::1").GetNetwork() == NET_UNROUTABLE);
16+
BOOST_CHECK(CNetAddr("8.8.8.8").GetNetwork() == NET_IPV4);
17+
BOOST_CHECK(CNetAddr("2001::8888").GetNetwork() == NET_IPV6);
18+
BOOST_CHECK(CNetAddr("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetNetwork() == NET_TOR);
19+
}
20+
21+
BOOST_AUTO_TEST_CASE(netbase_properties)
22+
{
23+
BOOST_CHECK(CNetAddr("127.0.0.1").IsIPv4());
24+
BOOST_CHECK(CNetAddr("::FFFF:192.168.1.1").IsIPv4());
25+
BOOST_CHECK(CNetAddr("::1").IsIPv6());
26+
BOOST_CHECK(CNetAddr("10.0.0.1").IsRFC1918());
27+
BOOST_CHECK(CNetAddr("192.168.1.1").IsRFC1918());
28+
BOOST_CHECK(CNetAddr("172.31.255.255").IsRFC1918());
29+
BOOST_CHECK(CNetAddr("2001:0DB8::").IsRFC3849());
30+
BOOST_CHECK(CNetAddr("169.254.1.1").IsRFC3927());
31+
BOOST_CHECK(CNetAddr("2002::1").IsRFC3964());
32+
BOOST_CHECK(CNetAddr("FC00::").IsRFC4193());
33+
BOOST_CHECK(CNetAddr("2001::2").IsRFC4380());
34+
BOOST_CHECK(CNetAddr("2001:10::").IsRFC4843());
35+
BOOST_CHECK(CNetAddr("FE80::").IsRFC4862());
36+
BOOST_CHECK(CNetAddr("64:FF9B::").IsRFC6052());
37+
BOOST_CHECK(CNetAddr("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").IsOnionCat());
38+
BOOST_CHECK(CNetAddr("127.0.0.1").IsLocal());
39+
BOOST_CHECK(CNetAddr("::1").IsLocal());
40+
BOOST_CHECK(CNetAddr("8.8.8.8").IsRoutable());
41+
BOOST_CHECK(CNetAddr("2001::1").IsRoutable());
42+
BOOST_CHECK(CNetAddr("127.0.0.1").IsValid());
43+
}
44+
45+
bool static TestSplitHost(string test, string host, int port)
46+
{
47+
string hostOut;
48+
int portOut = -1;
49+
SplitHostPort(test, portOut, hostOut);
50+
return hostOut == host && port == portOut;
51+
}
52+
53+
BOOST_AUTO_TEST_CASE(netbase_splithost)
54+
{
55+
BOOST_CHECK(TestSplitHost("www.bitcoin.org", "www.bitcoin.org", -1));
56+
BOOST_CHECK(TestSplitHost("[www.bitcoin.org]", "www.bitcoin.org", -1));
57+
BOOST_CHECK(TestSplitHost("www.bitcoin.org:80", "www.bitcoin.org", 80));
58+
BOOST_CHECK(TestSplitHost("[www.bitcoin.org]:80", "www.bitcoin.org", 80));
59+
BOOST_CHECK(TestSplitHost("127.0.0.1", "127.0.0.1", -1));
60+
BOOST_CHECK(TestSplitHost("127.0.0.1:8333", "127.0.0.1", 8333));
61+
BOOST_CHECK(TestSplitHost("[127.0.0.1]", "127.0.0.1", -1));
62+
BOOST_CHECK(TestSplitHost("[127.0.0.1]:8333", "127.0.0.1", 8333));
63+
BOOST_CHECK(TestSplitHost("::ffff:127.0.0.1", "::ffff:127.0.0.1", -1));
64+
BOOST_CHECK(TestSplitHost("[::ffff:127.0.0.1]:8333", "::ffff:127.0.0.1", 8333));
65+
BOOST_CHECK(TestSplitHost("[::]:8333", "::", 8333));
66+
BOOST_CHECK(TestSplitHost("::8333", "::8333", -1));
67+
BOOST_CHECK(TestSplitHost(":8333", "", 8333));
68+
BOOST_CHECK(TestSplitHost("[]:8333", "", 8333));
69+
BOOST_CHECK(TestSplitHost("", "", -1));
70+
}
71+
72+
bool static TestParse(string src, string canon)
73+
{
74+
CService addr;
75+
if (!LookupNumeric(src.c_str(), addr, 65535))
76+
return canon == "";
77+
return canon == addr.ToString();
78+
}
79+
80+
BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
81+
{
82+
BOOST_CHECK(TestParse("127.0.0.1", "127.0.0.1:65535"));
83+
BOOST_CHECK(TestParse("127.0.0.1:8333", "127.0.0.1:8333"));
84+
BOOST_CHECK(TestParse("::ffff:127.0.0.1", "127.0.0.1:65535"));
85+
BOOST_CHECK(TestParse("::", "[::]:65535"));
86+
BOOST_CHECK(TestParse("[::]:8333", "[::]:8333"));
87+
BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535"));
88+
BOOST_CHECK(TestParse(":::", ""));
89+
BOOST_CHECK(TestParse("128.5.1", "128.5.0.1:65535"));
90+
}
91+
92+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)