Changeset View
Changeset View
Standalone View
Standalone View
src/net.cpp
Show First 20 Lines • Show All 2,202 Lines • ▼ Show 20 Lines | while (!flagInterruptMsgProc) { | ||||
std::chrono::milliseconds(100), | std::chrono::milliseconds(100), | ||||
[this]() EXCLUSIVE_LOCKS_REQUIRED( | [this]() EXCLUSIVE_LOCKS_REQUIRED( | ||||
mutexMsgProc) { return fMsgProcWake; }); | mutexMsgProc) { return fMsgProcWake; }); | ||||
} | } | ||||
fMsgProcWake = false; | fMsgProcWake = false; | ||||
} | } | ||||
} | } | ||||
bool CConnman::BindListenPort(const CService &addrBind, std::string &strError, | bool CConnman::BindListenPort(const CService &addrBind, bilingual_str &strError, | ||||
NetPermissionFlags permissions) { | NetPermissionFlags permissions) { | ||||
strError = ""; | |||||
int nOne = 1; | int nOne = 1; | ||||
// Create socket for listening for incoming connections | // Create socket for listening for incoming connections | ||||
struct sockaddr_storage sockaddr; | struct sockaddr_storage sockaddr; | ||||
socklen_t len = sizeof(sockaddr); | socklen_t len = sizeof(sockaddr); | ||||
if (!addrBind.GetSockAddr((struct sockaddr *)&sockaddr, &len)) { | if (!addrBind.GetSockAddr((struct sockaddr *)&sockaddr, &len)) { | ||||
strError = strprintf("Error: Bind address family for %s not supported", | strError = strprintf( | ||||
Untranslated("Error: Bind address family for %s not supported"), | |||||
addrBind.ToString()); | addrBind.ToString()); | ||||
LogPrintf("%s\n", strError); | LogPrintf("%s\n", strError.original); | ||||
return false; | return false; | ||||
} | } | ||||
SOCKET hListenSocket = CreateSocket(addrBind); | SOCKET hListenSocket = CreateSocket(addrBind); | ||||
if (hListenSocket == INVALID_SOCKET) { | if (hListenSocket == INVALID_SOCKET) { | ||||
strError = strprintf("Error: Couldn't open socket for incoming " | strError = | ||||
"connections (socket returned error %s)", | strprintf(Untranslated("Error: Couldn't open socket for incoming " | ||||
"connections (socket returned error %s)"), | |||||
NetworkErrorString(WSAGetLastError())); | NetworkErrorString(WSAGetLastError())); | ||||
LogPrintf("%s\n", strError); | LogPrintf("%s\n", strError.original); | ||||
return false; | return false; | ||||
} | } | ||||
// Allow binding if the port is still in TIME_WAIT state after | // Allow binding if the port is still in TIME_WAIT state after | ||||
// the program was closed and restarted. | // the program was closed and restarted. | ||||
setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, | setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, | ||||
sizeof(int)); | sizeof(int)); | ||||
Show All 12 Lines | |||||
#endif | #endif | ||||
} | } | ||||
if (::bind(hListenSocket, (struct sockaddr *)&sockaddr, len) == | if (::bind(hListenSocket, (struct sockaddr *)&sockaddr, len) == | ||||
SOCKET_ERROR) { | SOCKET_ERROR) { | ||||
int nErr = WSAGetLastError(); | int nErr = WSAGetLastError(); | ||||
if (nErr == WSAEADDRINUSE) { | if (nErr == WSAEADDRINUSE) { | ||||
strError = strprintf(_("Unable to bind to %s on this computer. %s " | strError = strprintf(_("Unable to bind to %s on this computer. %s " | ||||
"is probably already running.") | "is probably already running."), | ||||
.translated, | |||||
addrBind.ToString(), PACKAGE_NAME); | addrBind.ToString(), PACKAGE_NAME); | ||||
} else { | } else { | ||||
strError = strprintf(_("Unable to bind to %s on this computer " | strError = strprintf(_("Unable to bind to %s on this computer " | ||||
"(bind returned error %s)") | "(bind returned error %s)"), | ||||
.translated, | |||||
addrBind.ToString(), NetworkErrorString(nErr)); | addrBind.ToString(), NetworkErrorString(nErr)); | ||||
} | } | ||||
LogPrintf("%s\n", strError); | LogPrintf("%s\n", strError.original); | ||||
CloseSocket(hListenSocket); | CloseSocket(hListenSocket); | ||||
return false; | return false; | ||||
} | } | ||||
LogPrintf("Bound to %s\n", addrBind.ToString()); | LogPrintf("Bound to %s\n", addrBind.ToString()); | ||||
// Listen for incoming connections | // Listen for incoming connections | ||||
if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR) { | if (listen(hListenSocket, SOMAXCONN) == SOCKET_ERROR) { | ||||
strError = strprintf(_("Error: Listening for incoming connections " | strError = strprintf(_("Error: Listening for incoming connections " | ||||
"failed (listen returned error %s)") | "failed (listen returned error %s)"), | ||||
.translated, | |||||
NetworkErrorString(WSAGetLastError())); | NetworkErrorString(WSAGetLastError())); | ||||
LogPrintf("%s\n", strError); | LogPrintf("%s\n", strError.original); | ||||
CloseSocket(hListenSocket); | CloseSocket(hListenSocket); | ||||
return false; | return false; | ||||
} | } | ||||
vhListenSocket.push_back(ListenSocket(hListenSocket, permissions)); | vhListenSocket.push_back(ListenSocket(hListenSocket, permissions)); | ||||
if (addrBind.IsRoutable() && fDiscover && (permissions & PF_NOBAN) == 0) { | if (addrBind.IsRoutable() && fDiscover && (permissions & PF_NOBAN) == 0) { | ||||
AddLocal(addrBind, LOCAL_BIND); | AddLocal(addrBind, LOCAL_BIND); | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | NodeId CConnman::GetNewNodeId() { | ||||
return nLastNodeId.fetch_add(1); | return nLastNodeId.fetch_add(1); | ||||
} | } | ||||
bool CConnman::Bind(const CService &addr, unsigned int flags, | bool CConnman::Bind(const CService &addr, unsigned int flags, | ||||
NetPermissionFlags permissions) { | NetPermissionFlags permissions) { | ||||
if (!(flags & BF_EXPLICIT) && !IsReachable(addr)) { | if (!(flags & BF_EXPLICIT) && !IsReachable(addr)) { | ||||
return false; | return false; | ||||
} | } | ||||
std::string strError; | bilingual_str strError; | ||||
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; | ||||
} | } | ||||
return true; | return true; | ||||
Show All 37 Lines | Init(connOptions); | ||||
nMaxOutboundTotalBytesSentInCycle = 0; | nMaxOutboundTotalBytesSentInCycle = 0; | ||||
nMaxOutboundCycleStartTime = 0; | nMaxOutboundCycleStartTime = 0; | ||||
} | } | ||||
if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) { | if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) { | ||||
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."), | ||||
.translated, | |||||
"", CClientUIInterface::MSG_ERROR); | "", CClientUIInterface::MSG_ERROR); | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
for (const auto &strDest : connOptions.vSeedNodes) { | for (const auto &strDest : connOptions.vSeedNodes) { | ||||
AddOneShot(strDest); | AddOneShot(strDest); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | threadOpenAddedConnections = | ||||
std::function<void()>(std::bind( | std::function<void()>(std::bind( | ||||
&CConnman::ThreadOpenAddedConnections, this))); | &CConnman::ThreadOpenAddedConnections, this))); | ||||
if (connOptions.m_use_addrman_outgoing && | if (connOptions.m_use_addrman_outgoing && | ||||
!connOptions.m_specified_outgoing.empty()) { | !connOptions.m_specified_outgoing.empty()) { | ||||
if (clientInterface) { | if (clientInterface) { | ||||
clientInterface->ThreadSafeMessageBox( | clientInterface->ThreadSafeMessageBox( | ||||
_("Cannot provide specific connections and have addrman find " | _("Cannot provide specific connections and have addrman find " | ||||
"outgoing connections at the same.") | "outgoing connections at the same."), | ||||
.translated, | |||||
"", CClientUIInterface::MSG_ERROR); | "", CClientUIInterface::MSG_ERROR); | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
if (connOptions.m_use_addrman_outgoing || | if (connOptions.m_use_addrman_outgoing || | ||||
!connOptions.m_specified_outgoing.empty()) { | !connOptions.m_specified_outgoing.empty()) { | ||||
threadOpenConnections = | threadOpenConnections = | ||||
std::thread(&TraceThread<std::function<void()>>, "opencon", | std::thread(&TraceThread<std::function<void()>>, "opencon", | ||||
▲ Show 20 Lines • Show All 520 Lines • Show Last 20 Lines |