Skip to content

Commit

Permalink
Allow other processes to bind to the STUN UDP port. (jselbie#20)
Browse files Browse the repository at this point in the history
* allow multiple port binds for UDP

* adding --reuseaddr command line switch for setting the SO_REUSEADDR option

* initializing fReuseAddr and modifying help page
  • Loading branch information
spencerlambert authored and jselbie committed Apr 2, 2017
1 parent 137c2f0 commit 9c0e8a9
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 95 deletions.
2 changes: 1 addition & 1 deletion client/clientmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ HRESULT UdpClientLoop(StunClientLogicConfig& config, const ClientSocketConfig& s
Chk(hr);
}

hr = stunSocket.UDPInit(socketconfig.addrLocal, RolePP);
hr = stunSocket.UDPInit(socketconfig.addrLocal, RolePP, false);
if (FAILED(hr))
{
Logging::LogMsg(LL_ALWAYS, "Unable to create local socket: (error = x%x)", hr);
Expand Down
4 changes: 2 additions & 2 deletions networkutils/stunsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ HRESULT CStunSocket::InitCommon(int socktype, const CSocketAddress& addrlocal, S



HRESULT CStunSocket::UDPInit(const CSocketAddress& local, SocketRole role)
HRESULT CStunSocket::UDPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag)
{
return InitCommon(SOCK_DGRAM, local, role, false);
return InitCommon(SOCK_DGRAM, local, role, fSetReuseFlag);
}

HRESULT CStunSocket::TCPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag)
Expand Down
2 changes: 1 addition & 1 deletion networkutils/stunsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CStunSocket

void UpdateAddresses();

HRESULT UDPInit(const CSocketAddress& local, SocketRole role);
HRESULT UDPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag);
HRESULT TCPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag);
};

Expand Down
13 changes: 12 additions & 1 deletion resources/stunserver.1
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,18 @@ the same process (each in a separate thread).
The fields of each configuration node are named identical to the
corresponding command line parameters (with the leading dashes removed).
An example stun.conf configuration file is shipped in the "testcode"
folder of the source package ____
folder of the source package
.PP
* * * * *
.PP
\f[B]\-\-reuseaddr\f[]
.PP
The \-\-reuseaddr switch allows the STUN server port to be shared with
other processes.
This is useful for scenarios where another process needs to send from
the STUN server port.
.PP
* * * * *
.PP
\f[B]\-\-help\f[]
.PP
Expand Down
6 changes: 6 additions & 0 deletions resources/stunserver.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ configuration file is shipped in the "testcode" folder of the source package

____

**--reuseaddr**

The --reuseaddr switch allows the STUN server port to be shared with other processes. This is useful for scenarios where another process needs to send from the STUN server port.

____

**--help**

Prints this help page
Expand Down
182 changes: 102 additions & 80 deletions resources/stunserver.txtcode
Original file line number Diff line number Diff line change
Expand Up @@ -612,85 +612,107 @@ const char stunserver_text[] = {
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d,
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d,
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d,
0x2d, 0x0a, 0x0a, 0x2d, 0x2d, 0x68, 0x65, 0x6c, 0x70, 0x0a, 0x0a, 0x50,
0x72, 0x69, 0x6e, 0x74, 0x73, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20, 0x68,
0x65, 0x6c, 0x70, 0x20, 0x70, 0x61, 0x67, 0x65, 0x0a, 0x0a, 0x0a, 0x0a,
0x45, 0x58, 0x41, 0x4d, 0x50, 0x4c, 0x45, 0x53, 0x0a, 0x0a, 0x0a, 0x73,
0x74, 0x75, 0x6e, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x57, 0x69, 0x74, 0x68, 0x20, 0x6e, 0x6f, 0x20, 0x6f, 0x70,
0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2c, 0x20, 0x73, 0x74, 0x61, 0x72, 0x74,
0x73, 0x20, 0x61, 0x20, 0x62, 0x61, 0x73, 0x69, 0x63, 0x20, 0x53, 0x54,
0x55, 0x4e, 0x20, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x20, 0x73,
0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x20, 0x6f, 0x6e, 0x20, 0x55, 0x44,
0x50, 0x20, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x33, 0x34, 0x37, 0x38, 0x2e,
0x0a, 0x0a, 0x73, 0x74, 0x75, 0x6e, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72,
0x20, 0x2d, 0x2d, 0x6d, 0x6f, 0x64, 0x65, 0x20, 0x66, 0x75, 0x6c, 0x6c,
0x20, 0x2d, 0x2d, 0x70, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x69, 0x6e,
0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x20, 0x31, 0x32, 0x38, 0x2e,
0x33, 0x34, 0x2e, 0x35, 0x36, 0x2e, 0x37, 0x38, 0x20, 0x2d, 0x2d, 0x61,
0x6c, 0x74, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x20,
0x31, 0x32, 0x38, 0x2e, 0x33, 0x34, 0x2e, 0x35, 0x36, 0x2e, 0x37, 0x39,
0x0a, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x41, 0x62, 0x6f, 0x76, 0x65, 0x20,
0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x73, 0x74, 0x61, 0x72,
0x74, 0x73, 0x20, 0x61, 0x20, 0x64, 0x75, 0x61, 0x6c, 0x2d, 0x68, 0x6f,
0x73, 0x74, 0x20, 0x53, 0x54, 0x55, 0x4e, 0x20, 0x73, 0x65, 0x72, 0x76,
0x69, 0x63, 0x65, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74,
0x68, 0x65, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65,
0x73, 0x20, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, 0x65, 0x64,
0x20, 0x62, 0x79, 0x20, 0x74, 0x68, 0x65, 0x20, 0x49, 0x50, 0x20, 0x61,
0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x20, 0x22, 0x31, 0x32, 0x38, 0x2e,
0x33, 0x34, 0x2e, 0x35, 0x36, 0x2e, 0x37, 0x38, 0x22, 0x20, 0x61, 0x6e,
0x64, 0x20, 0x22, 0x31, 0x32, 0x38, 0x2e, 0x33, 0x34, 0x2e, 0x35, 0x36,
0x2e, 0x37, 0x39, 0x22, 0x2e, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65, 0x20,
0x61, 0x72, 0x65, 0x20, 0x66, 0x6f, 0x75, 0x72, 0x20, 0x55, 0x44, 0x50,
0x20, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x20, 0x6c, 0x69, 0x73, 0x74,
0x65, 0x6e, 0x65, 0x72, 0x73, 0x0a, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x31,
0x32, 0x38, 0x2e, 0x33, 0x34, 0x2e, 0x35, 0x36, 0x2e, 0x37, 0x38, 0x3a,
0x33, 0x34, 0x37, 0x38, 0x20, 0x28, 0x50, 0x72, 0x69, 0x6d, 0x61, 0x72,
0x79, 0x20, 0x49, 0x50, 0x2c, 0x20, 0x50, 0x72, 0x69, 0x6d, 0x61, 0x72,
0x79, 0x20, 0x50, 0x6f, 0x72, 0x74, 0x29, 0x20, 0x31, 0x32, 0x38, 0x2e,
0x33, 0x34, 0x2e, 0x35, 0x36, 0x2e, 0x37, 0x38, 0x3a, 0x33, 0x34, 0x37,
0x39, 0x20, 0x28, 0x50, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x20, 0x49,
0x2d, 0x0a, 0x0a, 0x2d, 0x2d, 0x72, 0x65, 0x75, 0x73, 0x65, 0x61, 0x64,
0x64, 0x72, 0x0a, 0x0a, 0x54, 0x68, 0x65, 0x20, 0x2d, 0x2d, 0x72, 0x65,
0x75, 0x73, 0x65, 0x61, 0x64, 0x64, 0x72, 0x20, 0x73, 0x77, 0x69, 0x74,
0x63, 0x68, 0x20, 0x61, 0x6c, 0x6c, 0x6f, 0x77, 0x73, 0x20, 0x74, 0x68,
0x65, 0x20, 0x53, 0x54, 0x55, 0x4e, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65,
0x72, 0x20, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x74, 0x6f, 0x20, 0x62, 0x65,
0x20, 0x73, 0x68, 0x61, 0x72, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68,
0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x70, 0x72, 0x6f, 0x63, 0x65,
0x73, 0x73, 0x65, 0x73, 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69,
0x73, 0x20, 0x75, 0x73, 0x65, 0x66, 0x75, 0x6c, 0x20, 0x66, 0x6f, 0x72,
0x20, 0x73, 0x63, 0x65, 0x6e, 0x61, 0x72, 0x69, 0x6f, 0x73, 0x20, 0x77,
0x68, 0x65, 0x72, 0x65, 0x20, 0x61, 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72,
0x20, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x20, 0x6e, 0x65, 0x65,
0x64, 0x73, 0x20, 0x74, 0x6f, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x20, 0x66,
0x72, 0x6f, 0x6d, 0x20, 0x74, 0x68, 0x65, 0x20, 0x53, 0x54, 0x55, 0x4e,
0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x70, 0x6f, 0x72, 0x74,
0x2e, 0x0a, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d,
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d,
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d,
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d,
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d,
0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d,
0x2d, 0x2d, 0x2d, 0x0a, 0x0a, 0x2d, 0x2d, 0x68, 0x65, 0x6c, 0x70, 0x0a,
0x0a, 0x50, 0x72, 0x69, 0x6e, 0x74, 0x73, 0x20, 0x74, 0x68, 0x69, 0x73,
0x20, 0x68, 0x65, 0x6c, 0x70, 0x20, 0x70, 0x61, 0x67, 0x65, 0x0a, 0x0a,
0x0a, 0x0a, 0x45, 0x58, 0x41, 0x4d, 0x50, 0x4c, 0x45, 0x53, 0x0a, 0x0a,
0x0a, 0x73, 0x74, 0x75, 0x6e, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x57, 0x69, 0x74, 0x68, 0x20, 0x6e, 0x6f, 0x20,
0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2c, 0x20, 0x73, 0x74, 0x61,
0x72, 0x74, 0x73, 0x20, 0x61, 0x20, 0x62, 0x61, 0x73, 0x69, 0x63, 0x20,
0x53, 0x54, 0x55, 0x4e, 0x20, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67,
0x20, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x20, 0x6f, 0x6e, 0x20,
0x55, 0x44, 0x50, 0x20, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x33, 0x34, 0x37,
0x38, 0x2e, 0x0a, 0x0a, 0x73, 0x74, 0x75, 0x6e, 0x73, 0x65, 0x72, 0x76,
0x65, 0x72, 0x20, 0x2d, 0x2d, 0x6d, 0x6f, 0x64, 0x65, 0x20, 0x66, 0x75,
0x6c, 0x6c, 0x20, 0x2d, 0x2d, 0x70, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79,
0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x20, 0x31, 0x32,
0x38, 0x2e, 0x33, 0x34, 0x2e, 0x35, 0x36, 0x2e, 0x37, 0x38, 0x20, 0x2d,
0x2d, 0x61, 0x6c, 0x74, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63,
0x65, 0x20, 0x31, 0x32, 0x38, 0x2e, 0x33, 0x34, 0x2e, 0x35, 0x36, 0x2e,
0x37, 0x39, 0x0a, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x41, 0x62, 0x6f, 0x76,
0x65, 0x20, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x73, 0x74,
0x61, 0x72, 0x74, 0x73, 0x20, 0x61, 0x20, 0x64, 0x75, 0x61, 0x6c, 0x2d,
0x68, 0x6f, 0x73, 0x74, 0x20, 0x53, 0x54, 0x55, 0x4e, 0x20, 0x73, 0x65,
0x72, 0x76, 0x69, 0x63, 0x65, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68, 0x65,
0x20, 0x74, 0x68, 0x65, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61,
0x63, 0x65, 0x73, 0x20, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69,
0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x74, 0x68, 0x65, 0x20, 0x49, 0x50,
0x20, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x20, 0x22, 0x31, 0x32,
0x38, 0x2e, 0x33, 0x34, 0x2e, 0x35, 0x36, 0x2e, 0x37, 0x38, 0x22, 0x20,
0x61, 0x6e, 0x64, 0x20, 0x22, 0x31, 0x32, 0x38, 0x2e, 0x33, 0x34, 0x2e,
0x35, 0x36, 0x2e, 0x37, 0x39, 0x22, 0x2e, 0x20, 0x54, 0x68, 0x65, 0x72,
0x65, 0x20, 0x61, 0x72, 0x65, 0x20, 0x66, 0x6f, 0x75, 0x72, 0x20, 0x55,
0x44, 0x50, 0x20, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x20, 0x6c, 0x69,
0x73, 0x74, 0x65, 0x6e, 0x65, 0x72, 0x73, 0x0a, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x31, 0x32, 0x38, 0x2e, 0x33, 0x34, 0x2e, 0x35, 0x36, 0x2e, 0x37,
0x38, 0x3a, 0x33, 0x34, 0x37, 0x38, 0x20, 0x28, 0x50, 0x72, 0x69, 0x6d,
0x61, 0x72, 0x79, 0x20, 0x49, 0x50, 0x2c, 0x20, 0x50, 0x72, 0x69, 0x6d,
0x61, 0x72, 0x79, 0x20, 0x50, 0x6f, 0x72, 0x74, 0x29, 0x20, 0x31, 0x32,
0x38, 0x2e, 0x33, 0x34, 0x2e, 0x35, 0x36, 0x2e, 0x37, 0x38, 0x3a, 0x33,
0x34, 0x37, 0x39, 0x20, 0x28, 0x50, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79,
0x20, 0x49, 0x50, 0x2c, 0x20, 0x41, 0x6c, 0x74, 0x65, 0x72, 0x6e, 0x61,
0x74, 0x65, 0x20, 0x50, 0x6f, 0x72, 0x74, 0x29, 0x20, 0x31, 0x32, 0x38,
0x2e, 0x33, 0x34, 0x2e, 0x35, 0x36, 0x2e, 0x37, 0x39, 0x3a, 0x33, 0x34,
0x37, 0x38, 0x20, 0x28, 0x50, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x20,
0x49, 0x50, 0x2c, 0x20, 0x50, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x20,
0x50, 0x6f, 0x72, 0x74, 0x29, 0x20, 0x31, 0x32, 0x38, 0x2e, 0x33, 0x34,
0x2e, 0x35, 0x36, 0x2e, 0x37, 0x39, 0x3a, 0x33, 0x34, 0x37, 0x39, 0x20,
0x28, 0x41, 0x6c, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x74, 0x65, 0x20, 0x49,
0x50, 0x2c, 0x20, 0x41, 0x6c, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x74, 0x65,
0x20, 0x50, 0x6f, 0x72, 0x74, 0x29, 0x20, 0x31, 0x32, 0x38, 0x2e, 0x33,
0x34, 0x2e, 0x35, 0x36, 0x2e, 0x37, 0x39, 0x3a, 0x33, 0x34, 0x37, 0x38,
0x20, 0x28, 0x50, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x20, 0x49, 0x50,
0x2c, 0x20, 0x50, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x20, 0x50, 0x6f,
0x72, 0x74, 0x29, 0x20, 0x31, 0x32, 0x38, 0x2e, 0x33, 0x34, 0x2e, 0x35,
0x36, 0x2e, 0x37, 0x39, 0x3a, 0x33, 0x34, 0x37, 0x39, 0x20, 0x28, 0x41,
0x6c, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x74, 0x65, 0x20, 0x49, 0x50, 0x2c,
0x20, 0x41, 0x6c, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x74, 0x65, 0x20, 0x50,
0x6f, 0x72, 0x74, 0x29, 0x0a, 0x0a, 0x41, 0x6e, 0x20, 0x65, 0x72, 0x72,
0x6f, 0x72, 0x20, 0x6f, 0x63, 0x63, 0x75, 0x72, 0x73, 0x20, 0x69, 0x66,
0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73,
0x65, 0x73, 0x20, 0x73, 0x70, 0x65, 0x63, 0x69, 0x66, 0x69, 0x65, 0x64,
0x20, 0x64, 0x6f, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x65, 0x78, 0x69, 0x73,
0x74, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x6f, 0x63,
0x61, 0x6c, 0x20, 0x68, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x75, 0x6e, 0x6e,
0x69, 0x6e, 0x67, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x65, 0x72, 0x76,
0x69, 0x63, 0x65, 0x2e, 0x0a, 0x0a, 0x73, 0x74, 0x75, 0x6e, 0x73, 0x65,
0x72, 0x76, 0x65, 0x72, 0x20, 0x2d, 0x2d, 0x6d, 0x6f, 0x64, 0x65, 0x20,
0x66, 0x75, 0x6c, 0x6c, 0x20, 0x2d, 0x2d, 0x70, 0x72, 0x69, 0x6d, 0x61,
0x72, 0x79, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x20,
0x65, 0x74, 0x68, 0x30, 0x20, 0x2d, 0x2d, 0x61, 0x6c, 0x74, 0x69, 0x6e,
0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x20, 0x65, 0x74, 0x68, 0x31,
0x0a, 0x20, 0x20, 0x20, 0x20, 0x53, 0x61, 0x6d, 0x65, 0x20, 0x61, 0x73,
0x20, 0x61, 0x62, 0x6f, 0x76, 0x65, 0x2c, 0x20, 0x65, 0x78, 0x63, 0x65,
0x70, 0x74, 0x20, 0x74, 0x68, 0x65, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x72,
0x66, 0x61, 0x63, 0x65, 0x73, 0x20, 0x61, 0x72, 0x65, 0x20, 0x73, 0x70,
0x65, 0x63, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x74,
0x68, 0x65, 0x69, 0x72, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x20, 0x61,
0x73, 0x20, 0x65, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x61, 0x74, 0x65, 0x64,
0x20, 0x62, 0x79, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x79, 0x73, 0x74,
0x65, 0x6d, 0x2e, 0x20, 0x54, 0x68, 0x65, 0x20, 0x22, 0x69, 0x66, 0x63,
0x6f, 0x6e, 0x66, 0x69, 0x67, 0x22, 0x20, 0x6f, 0x72, 0x20, 0x22, 0x69,
0x70, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x22, 0x20, 0x63, 0x6f, 0x6d,
0x6d, 0x61, 0x6e, 0x64, 0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20, 0x65, 0x6e,
0x75, 0x6d, 0x65, 0x72, 0x61, 0x74, 0x65, 0x20, 0x61, 0x76, 0x61, 0x69,
0x6c, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66,
0x61, 0x63, 0x65, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x2e, 0x0a, 0x0a,
0x0a, 0x0a, 0x41, 0x55, 0x54, 0x48, 0x4f, 0x52, 0x0a, 0x0a, 0x0a, 0x6a,
0x6f, 0x68, 0x6e, 0x20, 0x73, 0x65, 0x6c, 0x62, 0x69, 0x65, 0x20, 0x28,
0x6a, 0x6f, 0x68, 0x6e, 0x40, 0x73, 0x65, 0x6c, 0x62, 0x69, 0x65, 0x2e,
0x63, 0x6f, 0x6d, 0x29, 0x0a,0x00};
0x20, 0x50, 0x6f, 0x72, 0x74, 0x29, 0x0a, 0x0a, 0x41, 0x6e, 0x20, 0x65,
0x72, 0x72, 0x6f, 0x72, 0x20, 0x6f, 0x63, 0x63, 0x75, 0x72, 0x73, 0x20,
0x69, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x64, 0x64, 0x72, 0x65,
0x73, 0x73, 0x65, 0x73, 0x20, 0x73, 0x70, 0x65, 0x63, 0x69, 0x66, 0x69,
0x65, 0x64, 0x20, 0x64, 0x6f, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x65, 0x78,
0x69, 0x73, 0x74, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c,
0x6f, 0x63, 0x61, 0x6c, 0x20, 0x68, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x75,
0x6e, 0x6e, 0x69, 0x6e, 0x67, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x65,
0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x0a, 0x0a, 0x73, 0x74, 0x75, 0x6e,
0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x2d, 0x2d, 0x6d, 0x6f, 0x64,
0x65, 0x20, 0x66, 0x75, 0x6c, 0x6c, 0x20, 0x2d, 0x2d, 0x70, 0x72, 0x69,
0x6d, 0x61, 0x72, 0x79, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63,
0x65, 0x20, 0x65, 0x74, 0x68, 0x30, 0x20, 0x2d, 0x2d, 0x61, 0x6c, 0x74,
0x69, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x20, 0x65, 0x74,
0x68, 0x31, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x53, 0x61, 0x6d, 0x65, 0x20,
0x61, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x76, 0x65, 0x2c, 0x20, 0x65, 0x78,
0x63, 0x65, 0x70, 0x74, 0x20, 0x74, 0x68, 0x65, 0x20, 0x69, 0x6e, 0x74,
0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x73, 0x20, 0x61, 0x72, 0x65, 0x20,
0x73, 0x70, 0x65, 0x63, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x62, 0x79,
0x20, 0x74, 0x68, 0x65, 0x69, 0x72, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x73,
0x20, 0x61, 0x73, 0x20, 0x65, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x61, 0x74,
0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x79,
0x73, 0x74, 0x65, 0x6d, 0x2e, 0x20, 0x54, 0x68, 0x65, 0x20, 0x22, 0x69,
0x66, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x22, 0x20, 0x6f, 0x72, 0x20,
0x22, 0x69, 0x70, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x22, 0x20, 0x63,
0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20,
0x65, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x61, 0x74, 0x65, 0x20, 0x61, 0x76,
0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x69, 0x6e, 0x74, 0x65,
0x72, 0x66, 0x61, 0x63, 0x65, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x2e,
0x0a, 0x0a, 0x0a, 0x0a, 0x41, 0x55, 0x54, 0x48, 0x4f, 0x52, 0x0a, 0x0a,
0x0a, 0x6a, 0x6f, 0x68, 0x6e, 0x20, 0x73, 0x65, 0x6c, 0x62, 0x69, 0x65,
0x20, 0x28, 0x6a, 0x6f, 0x68, 0x6e, 0x40, 0x73, 0x65, 0x6c, 0x62, 0x69,
0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x29, 0x0a,0x00};

7 changes: 7 additions & 0 deletions server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ struct StartupArgs
std::string strMaxConnections;
std::string strDosProtect;
std::string strConfigFile;
std::string strReuseAddr;

};

Expand All @@ -149,6 +150,7 @@ void DumpStartupArgs(StartupArgs& args)
PRINTARG(strVerbosity);
PRINTARG(strMaxConnections);
PRINTARG(strDosProtect);
PRINTARG(strReuseAddr);
Logging::LogMsg(LL_DEBUG, "--------------------------\n");
}

Expand Down Expand Up @@ -508,6 +510,9 @@ HRESULT BuildServerConfigurationFromArgs(StartupArgs& argsIn, CStunServerConfig*
// ---- DDOS PROTECTION SWITCH -------------------------------------------
config.fEnableDosProtection = (argsIn.strDosProtect.length() > 0);

// ---- REUSE ADDRESS SWITCH -------------------------------------------
config.fReuseAddr = (argsIn.strReuseAddr.length() > 0);

*pConfigOut = config;
hr = S_OK;

Expand Down Expand Up @@ -536,6 +541,7 @@ HRESULT ParseCommandLineArgs(int argc, char** argv, int startindex, StartupArgs*
cmdline.AddOption("verbosity", required_argument, &pStartupArgs->strVerbosity);
cmdline.AddOption("ddp", no_argument, &pStartupArgs->strDosProtect);
cmdline.AddOption("configfile", required_argument, &pStartupArgs->strConfigFile);
cmdline.AddOption("reuseaddr", no_argument, &pStartupArgs->strReuseAddr);

cmdline.ParseCommandLine(argc, argv, startindex, &fError);

Expand Down Expand Up @@ -591,6 +597,7 @@ HRESULT LoadConfigsFromFile(const std::string& filename, std::vector<StartupArgs
args.strProtocol = child.get("protocol", "");
args.strMaxConnections = child.get("maxconn", "");
args.strDosProtect = child.get("ddp", "");
args.strReuseAddr = child.get("reuseaddr", "");

configurations.push_back(args);
}
Expand Down
15 changes: 8 additions & 7 deletions server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ fHasAA(false),
fMultiThreadedMode(false),
fTCP(false),
nMaxConnections(0), // zero means default
fEnableDosProtection(false)
fEnableDosProtection(false),
fReuseAddr(false)
{
;
}
Expand All @@ -50,13 +51,13 @@ CStunServer::~CStunServer()
Shutdown();
}

HRESULT CStunServer::AddSocket(TransportAddressSet* pTSA, SocketRole role, const CSocketAddress& addrListen, const CSocketAddress& addrAdvertise)
HRESULT CStunServer::AddSocket(TransportAddressSet* pTSA, SocketRole role, const CSocketAddress& addrListen, const CSocketAddress& addrAdvertise, bool fSetReuseFlag)
{
HRESULT hr = S_OK;

ASSERT(IsValidSocketRole(role));

Chk(_arrSockets[role].UDPInit(addrListen, role));
Chk(_arrSockets[role].UDPInit(addrListen, role, fSetReuseFlag));
ChkA(_arrSockets[role].EnablePktInfoOption(true));


Expand Down Expand Up @@ -110,25 +111,25 @@ HRESULT CStunServer::Initialize(const CStunServerConfig& config)
// Create the sockets and initialize the TSA thing
if (config.fHasPP)
{
Chk(AddSocket(&tsa, RolePP, config.addrPP, config.addrPrimaryAdvertised));
Chk(AddSocket(&tsa, RolePP, config.addrPP, config.addrPrimaryAdvertised, config.fReuseAddr));
socketcount++;
}

if (config.fHasPA)
{
Chk(AddSocket(&tsa, RolePA, config.addrPA, config.addrPrimaryAdvertised));
Chk(AddSocket(&tsa, RolePA, config.addrPA, config.addrPrimaryAdvertised, config.fReuseAddr));
socketcount++;
}

if (config.fHasAP)
{
Chk(AddSocket(&tsa, RoleAP, config.addrAP, config.addrAlternateAdvertised));
Chk(AddSocket(&tsa, RoleAP, config.addrAP, config.addrAlternateAdvertised, config.fReuseAddr));
socketcount++;
}

if (config.fHasAA)
{
Chk(AddSocket(&tsa, RoleAA, config.addrAA, config.addrAlternateAdvertised));
Chk(AddSocket(&tsa, RoleAA, config.addrAA, config.addrAlternateAdvertised, config.fReuseAddr));
socketcount++;
}

Expand Down
Loading

0 comments on commit 9c0e8a9

Please sign in to comment.