Changeset View
Changeset View
Standalone View
Standalone View
src/net.cpp
Show First 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | |||||
#define MSG_DONTWAIT 0 | #define MSG_DONTWAIT 0 | ||||
#endif | #endif | ||||
/** Used to pass flags to the Bind() function */ | /** Used to pass flags to the Bind() function */ | ||||
enum BindFlags { | enum BindFlags { | ||||
BF_NONE = 0, | BF_NONE = 0, | ||||
BF_EXPLICIT = (1U << 0), | BF_EXPLICIT = (1U << 0), | ||||
BF_REPORT_ERROR = (1U << 1), | BF_REPORT_ERROR = (1U << 1), | ||||
/** | |||||
* Do not call AddLocal() for our special addresses, e.g., for incoming | |||||
* Tor connections, to prevent gossiping them over the network. | |||||
*/ | |||||
BF_DONT_ADVERTISE = (1U << 2), | |||||
}; | }; | ||||
// The set of sockets cannot be modified while waiting | // The set of sockets cannot be modified while waiting | ||||
// The sleep time needs to be small to avoid new sockets stalling | // The sleep time needs to be small to avoid new sockets stalling | ||||
static const uint64_t SELECT_TIMEOUT_MILLISECONDS = 50; | static const uint64_t SELECT_TIMEOUT_MILLISECONDS = 50; | ||||
const std::string NET_MESSAGE_COMMAND_OTHER = "*other*"; | const std::string NET_MESSAGE_COMMAND_OTHER = "*other*"; | ||||
▲ Show 20 Lines • Show All 2,499 Lines • ▼ Show 20 Lines | bool CConnman::Bind(const CService &addr, unsigned int flags, | ||||
if (!BindListenPort(addr, strError, permissions)) { | if (!BindListenPort(addr, strError, permissions)) { | ||||
if ((flags & BF_REPORT_ERROR) && clientInterface) { | if ((flags & BF_REPORT_ERROR) && clientInterface) { | ||||
clientInterface->ThreadSafeMessageBox( | clientInterface->ThreadSafeMessageBox( | ||||
strError, "", CClientUIInterface::MSG_ERROR); | strError, "", CClientUIInterface::MSG_ERROR); | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
if (addr.IsRoutable() && fDiscover && (permissions & PF_NOBAN) == 0) { | if (addr.IsRoutable() && fDiscover && !(flags & BF_DONT_ADVERTISE) && | ||||
!(permissions & PF_NOBAN)) { | |||||
AddLocal(addr, LOCAL_BIND); | AddLocal(addr, LOCAL_BIND); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
bool CConnman::InitBinds( | bool CConnman::InitBinds(const std::vector<CService> &binds, | ||||
const std::vector<CService> &binds, | const std::vector<NetWhitebindPermissions> &whiteBinds, | ||||
const std::vector<NetWhitebindPermissions> &whiteBinds) { | const std::vector<CService> &onion_binds) { | ||||
bool fBound = false; | bool fBound = false; | ||||
for (const auto &addrBind : binds) { | for (const auto &addrBind : binds) { | ||||
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR), | fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR), | ||||
NetPermissionFlags::PF_NONE); | NetPermissionFlags::PF_NONE); | ||||
} | } | ||||
for (const auto &addrBind : whiteBinds) { | for (const auto &addrBind : whiteBinds) { | ||||
fBound |= Bind(addrBind.m_service, (BF_EXPLICIT | BF_REPORT_ERROR), | fBound |= Bind(addrBind.m_service, (BF_EXPLICIT | BF_REPORT_ERROR), | ||||
addrBind.m_flags); | addrBind.m_flags); | ||||
} | } | ||||
if (binds.empty() && whiteBinds.empty()) { | if (binds.empty() && whiteBinds.empty()) { | ||||
struct in_addr inaddr_any; | struct in_addr inaddr_any; | ||||
inaddr_any.s_addr = htonl(INADDR_ANY); | inaddr_any.s_addr = htonl(INADDR_ANY); | ||||
struct in6_addr inaddr6_any = IN6ADDR_ANY_INIT; | struct in6_addr inaddr6_any = IN6ADDR_ANY_INIT; | ||||
fBound |= Bind(CService(inaddr6_any, GetListenPort()), BF_NONE, | fBound |= Bind(CService(inaddr6_any, GetListenPort()), BF_NONE, | ||||
NetPermissionFlags::PF_NONE); | NetPermissionFlags::PF_NONE); | ||||
fBound |= Bind(CService(inaddr_any, GetListenPort()), | fBound |= Bind(CService(inaddr_any, GetListenPort()), | ||||
!fBound ? BF_REPORT_ERROR : BF_NONE, | !fBound ? BF_REPORT_ERROR : BF_NONE, | ||||
NetPermissionFlags::PF_NONE); | NetPermissionFlags::PF_NONE); | ||||
} | } | ||||
for (const auto &addr_bind : onion_binds) { | |||||
fBound |= Bind(addr_bind, BF_EXPLICIT | BF_DONT_ADVERTISE, | |||||
NetPermissionFlags::PF_NONE); | |||||
} | |||||
return fBound; | return fBound; | ||||
} | } | ||||
bool CConnman::Start(CScheduler &scheduler, const Options &connOptions) { | bool CConnman::Start(CScheduler &scheduler, const Options &connOptions) { | ||||
Init(connOptions); | Init(connOptions); | ||||
{ | { | ||||
LOCK(cs_totalBytesRecv); | LOCK(cs_totalBytesRecv); | ||||
nTotalBytesRecv = 0; | nTotalBytesRecv = 0; | ||||
} | } | ||||
{ | { | ||||
LOCK(cs_totalBytesSent); | LOCK(cs_totalBytesSent); | ||||
nTotalBytesSent = 0; | nTotalBytesSent = 0; | ||||
nMaxOutboundTotalBytesSentInCycle = 0; | nMaxOutboundTotalBytesSentInCycle = 0; | ||||
nMaxOutboundCycleStartTime = 0; | nMaxOutboundCycleStartTime = 0; | ||||
} | } | ||||
if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) { | if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds, | ||||
connOptions.onion_binds)) { | |||||
if (clientInterface) { | if (clientInterface) { | ||||
clientInterface->ThreadSafeMessageBox( | clientInterface->ThreadSafeMessageBox( | ||||
_("Failed to listen on any port. Use -listen=0 if you want " | _("Failed to listen on any port. Use -listen=0 if you want " | ||||
"this."), | "this."), | ||||
"", CClientUIInterface::MSG_ERROR); | "", CClientUIInterface::MSG_ERROR); | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 705 Lines • Show Last 20 Lines |