You need to sign in or sign up before continuing.
Commit e642b254 authored by John Selbie's avatar John Selbie

Introducing the SO_REUSEADDR flag. Client only for now

parent 2049da33
......@@ -327,7 +327,7 @@ void TcpClientLoop(StunClientLogicConfig& config, ClientSocketConfig& socketconf
{
stunsocket.Close();
hr = stunsocket.TCPInit(socketconfig.addrLocal, RolePP, true);
hr = stunsocket.TCPInit(socketconfig.addrLocal, RolePP, true, true);
if (FAILED(hr))
{
Logging::LogMsg(LL_ALWAYS, "Unable to create local socket for TCP connection (hr == %x)", hr);
......@@ -474,7 +474,7 @@ HRESULT UdpClientLoop(StunClientLogicConfig& config, const ClientSocketConfig& s
Chk(hr);
}
hr = stunSocket.UDPInit(socketconfig.addrLocal, RolePP, false);
hr = stunSocket.UDPInit(socketconfig.addrLocal, RolePP, false, false);
if (FAILED(hr))
{
Logging::LogMsg(LL_ALWAYS, "Unable to create local socket: (error = x%x)", hr);
......
......@@ -273,7 +273,7 @@ void CStunSocket::UpdateAddresses()
HRESULT CStunSocket::InitCommon(int socktype, const CSocketAddress& addrlocal, SocketRole role, bool fSetReuseFlag)
HRESULT CStunSocket::InitCommon(int socktype, const CSocketAddress& addrlocal, SocketRole role, bool fSetReuseFlag, bool fSetReusePortFlag)
{
int sock = -1;
int ret;
......@@ -299,7 +299,19 @@ HRESULT CStunSocket::InitCommon(int socktype, const CSocketAddress& addrlocal, S
ret = ::setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &fAllow, sizeof(fAllow));
ChkIf(ret == -1, ERRNOHR);
}
#ifdef SO_REUSEPORT
if (fSetReusePortFlag)
{
int fAllow = 1;
ret = ::setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &fAllow, sizeof(fAllow));
if (ret == -1)
{
Logging::LogMsg(LL_DEBUG, "Warning: Failed to set SO_REUSEPORT option (errno = %d)", errno);
}
}
#endif
ret = bind(sock, addrlocal.GetSockAddr(), addrlocal.GetSockAddrLength());
ChkIf(ret == -1, ERRNOHR);
......@@ -319,14 +331,14 @@ Cleanup:
HRESULT CStunSocket::UDPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag)
HRESULT CStunSocket::UDPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag, bool fSetReusePortFlag)
{
return InitCommon(SOCK_DGRAM, local, role, fSetReuseFlag);
return InitCommon(SOCK_DGRAM, local, role, fSetReuseFlag, fSetReusePortFlag);
}
HRESULT CStunSocket::TCPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag)
HRESULT CStunSocket::TCPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag, bool fSetReusePortFlag)
{
return InitCommon(SOCK_STREAM, local, role, fSetReuseFlag);
return InitCommon(SOCK_STREAM, local, role, fSetReuseFlag, fSetReusePortFlag);
}
......@@ -30,7 +30,7 @@ private:
CStunSocket(const CStunSocket&) {;}
void operator=(const CStunSocket&) {;}
HRESULT InitCommon(int socktype, const CSocketAddress& addrlocal, SocketRole role, bool fSetReuseFlag);
HRESULT InitCommon(int socktype, const CSocketAddress& addrlocal, SocketRole role, bool fSetReuseFlag, bool fSetReusePortFlag);
void Reset();
......@@ -65,8 +65,8 @@ public:
void UpdateAddresses();
HRESULT UDPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag);
HRESULT TCPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag);
HRESULT UDPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag, bool fSetReusePortFlag);
HRESULT TCPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag, bool fSetReusePortFlag);
};
typedef boost::shared_ptr<CStunSocket> CRefCountedStunSocket;
......
......@@ -57,7 +57,7 @@ HRESULT CStunServer::AddSocket(TransportAddressSet* pTSA, SocketRole role, const
ASSERT(IsValidSocketRole(role));
Chk(_arrSockets[role].UDPInit(addrListen, role, fSetReuseFlag));
Chk(_arrSockets[role].UDPInit(addrListen, role, fSetReuseFlag, false));
ChkA(_arrSockets[role].EnablePktInfoOption(true));
......
......@@ -179,7 +179,7 @@ HRESULT CTCPStunThread::CreateListenSockets()
{
if (_tsaListen.set[r].fValid)
{
ChkA(_socketListenArray[r].TCPInit(_tsaListen.set[r].addr, (SocketRole)r, true));
ChkA(_socketListenArray[r].TCPInit(_tsaListen.set[r].addr, (SocketRole)r, true, false));
_socketTable[r] = _socketListenArray[r].GetSocketHandle();
ChkA(_socketListenArray[r].SetNonBlocking(true));
ret = listen(_socketTable[r], 128); // 128 - large backlog.
......
......@@ -70,9 +70,9 @@ HRESULT CTestRecvFromEx::DoTest(bool fIPV6)
// create two sockets listening on INADDR_ANY. One for sending and one for receiving
ChkA(socketSend.UDPInit(addrAny, RolePP, false));
ChkA(socketSend.UDPInit(addrAny, RolePP, false, false));
ChkA(socketRecv.UDPInit(addrAny, RolePP, false));
ChkA(socketRecv.UDPInit(addrAny, RolePP, false, false));
socketRecv.EnablePktInfoOption(true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment