Skip to content

Commit 1030fa7

Browse files
committed
Merge #8128: Net: Turn net structures into dumb storage classes
9e9d644 net: fixup nits (Cory Fields) 8945384 net: Have LookupNumeric return a CService directly (Cory Fields) 21ba407 net: narrow include scope after moving to netaddress (Cory Fields) 21e5b96 net: move CNetAddr/CService/CSubNet out of netbase (Cory Fields) 1017b8a net: Add direct tests for new CSubNet constructors (Cory Fields) b6c3ff3 net: Split resolving out of CSubNet (Cory Fields) f96c7c4 net: Split resolving out of CService (Cory Fields) 31d6b1d net: Split resolving out of CNetAddr (Cory Fields)
2 parents d727f77 + 9e9d644 commit 1030fa7

21 files changed

+1246
-1136
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ BITCOIN_CORE_H = \
105105
merkleblock.h \
106106
miner.h \
107107
net.h \
108+
netaddress.h \
108109
netbase.h \
109110
noui.h \
110111
policy/fees.h \
@@ -289,6 +290,7 @@ libbitcoin_common_a_SOURCES = \
289290
core_write.cpp \
290291
key.cpp \
291292
keystore.cpp \
293+
netaddress.cpp \
292294
netbase.cpp \
293295
protocol.cpp \
294296
scheduler.cpp \

src/addrman.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#ifndef BITCOIN_ADDRMAN_H
77
#define BITCOIN_ADDRMAN_H
88

9-
#include "netbase.h"
9+
#include "netaddress.h"
1010
#include "protocol.h"
1111
#include "random.h"
1212
#include "sync.h"

src/httpserver.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,17 @@ static bool ClientAllowed(const CNetAddr& netaddr)
197197
static bool InitHTTPAllowList()
198198
{
199199
rpc_allow_subnets.clear();
200-
rpc_allow_subnets.push_back(CSubNet("127.0.0.0/8")); // always allow IPv4 local subnet
201-
rpc_allow_subnets.push_back(CSubNet("::1")); // always allow IPv6 localhost
200+
CNetAddr localv4;
201+
CNetAddr localv6;
202+
LookupHost("127.0.0.1", localv4, false);
203+
LookupHost("::1", localv6, false);
204+
rpc_allow_subnets.push_back(CSubNet(localv4, 8)); // always allow IPv4 local subnet
205+
rpc_allow_subnets.push_back(CSubNet(localv6)); // always allow IPv6 localhost
202206
if (mapMultiArgs.count("-rpcallowip")) {
203207
const std::vector<std::string>& vAllow = mapMultiArgs["-rpcallowip"];
204208
for (std::string strAllow : vAllow) {
205-
CSubNet subnet(strAllow);
209+
CSubNet subnet;
210+
LookupSubNet(strAllow.c_str(), subnet);
206211
if (!subnet.IsValid()) {
207212
uiInterface.ThreadSafeMessageBox(
208213
strprintf("Invalid -rpcallowip subnet specification: %s. Valid are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24).", strAllow),
@@ -614,7 +619,7 @@ CService HTTPRequest::GetPeer()
614619
const char* address = "";
615620
uint16_t port = 0;
616621
evhttp_connection_get_peer(con, (char**)&address, &port);
617-
peer = CService(address, port);
622+
peer = LookupNumeric(address, port);
618623
}
619624
return peer;
620625
}

src/init.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "key.h"
2222
#include "main.h"
2323
#include "miner.h"
24+
#include "netbase.h"
2425
#include "net.h"
2526
#include "policy/policy.h"
2627
#include "rpc/server.h"
@@ -1134,7 +1135,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
11341135

11351136
if (mapArgs.count("-whitelist")) {
11361137
BOOST_FOREACH(const std::string& net, mapMultiArgs["-whitelist"]) {
1137-
CSubNet subnet(net);
1138+
CSubNet subnet;
1139+
LookupSubNet(net.c_str(), subnet);
11381140
if (!subnet.IsValid())
11391141
return InitError(strprintf(_("Invalid netmask specified in -whitelist: '%s'"), net));
11401142
CNode::AddWhitelistedRange(subnet);
@@ -1147,7 +1149,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
11471149
std::string proxyArg = GetArg("-proxy", "");
11481150
SetLimited(NET_TOR);
11491151
if (proxyArg != "" && proxyArg != "0") {
1150-
proxyType addrProxy = proxyType(CService(proxyArg, 9050), proxyRandomize);
1152+
CService resolved(LookupNumeric(proxyArg.c_str(), 9050));
1153+
proxyType addrProxy = proxyType(resolved, proxyRandomize);
11511154
if (!addrProxy.IsValid())
11521155
return InitError(strprintf(_("Invalid -proxy address: '%s'"), proxyArg));
11531156

@@ -1166,7 +1169,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
11661169
if (onionArg == "0") { // Handle -noonion/-onion=0
11671170
SetLimited(NET_TOR); // set onions as unreachable
11681171
} else {
1169-
proxyType addrOnion = proxyType(CService(onionArg, 9050), proxyRandomize);
1172+
CService resolved(LookupNumeric(onionArg.c_str(), 9050));
1173+
proxyType addrOnion = proxyType(resolved, proxyRandomize);
11701174
if (!addrOnion.IsValid())
11711175
return InitError(strprintf(_("Invalid -onion address: '%s'"), onionArg));
11721176
SetProxy(NET_TOR, addrOnion);

src/net.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "crypto/sha256.h"
1818
#include "hash.h"
1919
#include "primitives/transaction.h"
20+
#include "netbase.h"
2021
#include "scheduler.h"
2122
#include "ui_interface.h"
2223
#include "utilstrencodings.h"
@@ -175,7 +176,7 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn
175176
// one by discovery.
176177
CAddress GetLocalAddress(const CNetAddr *paddrPeer)
177178
{
178-
CAddress ret(CService("0.0.0.0",GetListenPort()), NODE_NONE);
179+
CAddress ret(CService(CNetAddr(),GetListenPort()), NODE_NONE);
179180
CService addr;
180181
if (GetLocal(addr, paddrPeer))
181182
{
@@ -494,7 +495,7 @@ void CNode::PushVersion()
494495
int nBestHeight = GetNodeSignals().GetHeight().get_value_or(0);
495496

496497
int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
497-
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0", 0), addr.nServices));
498+
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
498499
CAddress addrMe = GetLocalAddress(&addr);
499500
GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
500501
if (fLogIPs)
@@ -1396,8 +1397,11 @@ void ThreadMapPort()
13961397
{
13971398
if(externalIPAddress[0])
13981399
{
1399-
LogPrintf("UPnP: ExternalIPAddress = %s\n", externalIPAddress);
1400-
AddLocal(CNetAddr(externalIPAddress), LOCAL_UPNP);
1400+
CNetAddr resolved;
1401+
if(LookupHost(externalIPAddress, resolved, false)) {
1402+
LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
1403+
AddLocal(resolved, LOCAL_UPNP);
1404+
}
14011405
}
14021406
else
14031407
LogPrintf("UPnP: GetExternalIPAddress failed.\n");
@@ -1623,7 +1627,9 @@ void ThreadOpenConnections()
16231627
static bool done = false;
16241628
if (!done) {
16251629
LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
1626-
addrman.Add(convertSeed6(Params().FixedSeeds()), CNetAddr("127.0.0.1"));
1630+
CNetAddr local;
1631+
LookupHost("127.0.0.1", local, false);
1632+
addrman.Add(convertSeed6(Params().FixedSeeds()), local);
16271633
done = true;
16281634
}
16291635
}
@@ -1722,7 +1728,7 @@ std::vector<AddedNodeInfo> GetAddedNodeInfo()
17221728
}
17231729

17241730
BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
1725-
CService service(strAddNode, Params().GetDefaultPort());
1731+
CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
17261732
if (service.IsValid()) {
17271733
// strAddNode is an IP:port
17281734
auto it = mapConnected.find(service);
@@ -1760,7 +1766,7 @@ void ThreadOpenAddedConnections()
17601766
CSemaphoreGrant grant(*semOutbound);
17611767
// If strAddedNode is an IP/port, decode it immediately, so
17621768
// OpenNetworkConnection can detect existing connections to that IP/port.
1763-
CService service(info.strAddedNode, Params().GetDefaultPort());
1769+
CService service(LookupNumeric(info.strAddedNode.c_str(), Params().GetDefaultPort()));
17641770
OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false);
17651771
MilliSleep(500);
17661772
}
@@ -2060,8 +2066,11 @@ void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler)
20602066
semOutbound = new CSemaphore(nMaxOutbound);
20612067
}
20622068

2063-
if (pnodeLocalHost == NULL)
2064-
pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService("127.0.0.1", 0), nLocalServices));
2069+
if (pnodeLocalHost == NULL) {
2070+
CNetAddr local;
2071+
LookupHost("127.0.0.1", local, false);
2072+
pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService(local, 0), nLocalServices));
2073+
}
20652074

20662075
Discover(threadGroup);
20672076

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "bloom.h"
1111
#include "compat.h"
1212
#include "limitedmap.h"
13-
#include "netbase.h"
13+
#include "netaddress.h"
1414
#include "protocol.h"
1515
#include "random.h"
1616
#include "streams.h"

0 commit comments

Comments
 (0)