Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
Stunserver
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
Stunserver
Commits
95bd802f
Commit
95bd802f
authored
Feb 08, 2026
by
John Selbie
Committed by
GitHub
Feb 08, 2026
2
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introducing the SO_REUSEADDR flag. Client only for now (#69)
parent
01b2f1aa
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
27 additions
and
15 deletions
+27
-15
client/clientmain.cpp
client/clientmain.cpp
+2
-2
networkutils/stunsocket.cpp
networkutils/stunsocket.cpp
+18
-6
networkutils/stunsocket.h
networkutils/stunsocket.h
+3
-3
server/server.cpp
server/server.cpp
+1
-1
server/tcpserver.cpp
server/tcpserver.cpp
+1
-1
testcode/testrecvfromex.cpp
testcode/testrecvfromex.cpp
+2
-2
No files found.
client/clientmain.cpp
View file @
95bd802f
...
@@ -327,7 +327,7 @@ void TcpClientLoop(StunClientLogicConfig& config, ClientSocketConfig& socketconf
...
@@ -327,7 +327,7 @@ void TcpClientLoop(StunClientLogicConfig& config, ClientSocketConfig& socketconf
{
{
stunsocket
.
Close
();
stunsocket
.
Close
();
hr
=
stunsocket
.
TCPInit
(
addrLocal
,
RolePP
,
true
);
hr
=
stunsocket
.
TCPInit
(
socketconfig
.
addrLocal
,
RolePP
,
true
,
true
);
if
(
FAILED
(
hr
))
if
(
FAILED
(
hr
))
{
{
Logging
::
LogMsg
(
LL_ALWAYS
,
"Unable to create local socket for TCP connection (hr == %x)"
,
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
...
@@ -474,7 +474,7 @@ HRESULT UdpClientLoop(StunClientLogicConfig& config, const ClientSocketConfig& s
Chk
(
hr
);
Chk
(
hr
);
}
}
hr
=
stunSocket
.
UDPInit
(
socketconfig
.
addrLocal
,
RolePP
,
false
);
hr
=
stunSocket
.
UDPInit
(
socketconfig
.
addrLocal
,
RolePP
,
false
,
false
);
if
(
FAILED
(
hr
))
if
(
FAILED
(
hr
))
{
{
Logging
::
LogMsg
(
LL_ALWAYS
,
"Unable to create local socket: (error = x%x)"
,
hr
);
Logging
::
LogMsg
(
LL_ALWAYS
,
"Unable to create local socket: (error = x%x)"
,
hr
);
...
...
networkutils/stunsocket.cpp
View file @
95bd802f
...
@@ -273,7 +273,7 @@ void CStunSocket::UpdateAddresses()
...
@@ -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
sock
=
-
1
;
int
ret
;
int
ret
;
...
@@ -299,7 +299,19 @@ HRESULT CStunSocket::InitCommon(int socktype, const CSocketAddress& addrlocal, S
...
@@ -299,7 +299,19 @@ HRESULT CStunSocket::InitCommon(int socktype, const CSocketAddress& addrlocal, S
ret
=
::
setsockopt
(
sock
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
fAllow
,
sizeof
(
fAllow
));
ret
=
::
setsockopt
(
sock
,
SOL_SOCKET
,
SO_REUSEADDR
,
&
fAllow
,
sizeof
(
fAllow
));
ChkIf
(
ret
==
-
1
,
ERRNOHR
);
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
());
ret
=
bind
(
sock
,
addrlocal
.
GetSockAddr
(),
addrlocal
.
GetSockAddrLength
());
ChkIf
(
ret
==
-
1
,
ERRNOHR
);
ChkIf
(
ret
==
-
1
,
ERRNOHR
);
...
@@ -319,14 +331,14 @@ Cleanup:
...
@@ -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
);
}
}
networkutils/stunsocket.h
View file @
95bd802f
...
@@ -30,7 +30,7 @@ private:
...
@@ -30,7 +30,7 @@ private:
CStunSocket
(
const
CStunSocket
&
)
{;}
CStunSocket
(
const
CStunSocket
&
)
{;}
void
operator
=
(
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
();
void
Reset
();
...
@@ -65,8 +65,8 @@ public:
...
@@ -65,8 +65,8 @@ public:
void
UpdateAddresses
();
void
UpdateAddresses
();
HRESULT
UDPInit
(
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
);
HRESULT
TCPInit
(
const
CSocketAddress
&
local
,
SocketRole
role
,
bool
fSetReuseFlag
,
bool
fSetReusePortFlag
);
};
};
typedef
boost
::
shared_ptr
<
CStunSocket
>
CRefCountedStunSocket
;
typedef
boost
::
shared_ptr
<
CStunSocket
>
CRefCountedStunSocket
;
...
...
server/server.cpp
View file @
95bd802f
...
@@ -57,7 +57,7 @@ HRESULT CStunServer::AddSocket(TransportAddressSet* pTSA, SocketRole role, const
...
@@ -57,7 +57,7 @@ HRESULT CStunServer::AddSocket(TransportAddressSet* pTSA, SocketRole role, const
ASSERT
(
IsValidSocketRole
(
role
));
ASSERT
(
IsValidSocketRole
(
role
));
Chk
(
_arrSockets
[
role
].
UDPInit
(
addrListen
,
role
,
fSetReuseFlag
));
Chk
(
_arrSockets
[
role
].
UDPInit
(
addrListen
,
role
,
fSetReuseFlag
,
false
));
ChkA
(
_arrSockets
[
role
].
EnablePktInfoOption
(
true
));
ChkA
(
_arrSockets
[
role
].
EnablePktInfoOption
(
true
));
...
...
server/tcpserver.cpp
View file @
95bd802f
...
@@ -179,7 +179,7 @@ HRESULT CTCPStunThread::CreateListenSockets()
...
@@ -179,7 +179,7 @@ HRESULT CTCPStunThread::CreateListenSockets()
{
{
if
(
_tsaListen
.
set
[
r
].
fValid
)
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
();
_socketTable
[
r
]
=
_socketListenArray
[
r
].
GetSocketHandle
();
ChkA
(
_socketListenArray
[
r
].
SetNonBlocking
(
true
));
ChkA
(
_socketListenArray
[
r
].
SetNonBlocking
(
true
));
ret
=
listen
(
_socketTable
[
r
],
128
);
// 128 - large backlog.
ret
=
listen
(
_socketTable
[
r
],
128
);
// 128 - large backlog.
...
...
testcode/testrecvfromex.cpp
View file @
95bd802f
...
@@ -70,9 +70,9 @@ HRESULT CTestRecvFromEx::DoTest(bool fIPV6)
...
@@ -70,9 +70,9 @@ HRESULT CTestRecvFromEx::DoTest(bool fIPV6)
// create two sockets listening on INADDR_ANY. One for sending and one for receiving
// 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
);
socketRecv
.
EnablePktInfoOption
(
true
);
...
...
nanahira
@nanahira
mentioned in commit
1582042c
·
Feb 13, 2026
mentioned in commit
1582042c
mentioned in commit 1582042ca33f747f88101fbadec4944714aa8bbb
Toggle commit list
nanahira
@nanahira
mentioned in commit
a6b079a9
·
Feb 13, 2026
mentioned in commit
a6b079a9
mentioned in commit a6b079a9f14b72c6948aae4d12bf12cf9c57935f
Toggle commit list
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment