Skip to content

Commit

Permalink
IPv6 sockets don't get IPv4 bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
jselbie committed Jun 22, 2013
1 parent ffe0577 commit d6d816e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
32 changes: 31 additions & 1 deletion networkutils/stunsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ HRESULT CStunSocket::EnablePktInfo_IPV6(bool fEnable)
return EnablePktInfoImpl(level, option1, option2, fEnable);
}


HRESULT CStunSocket::EnablePktInfoOption(bool fEnable)
{
int family = _addrlocal.GetFamily();
Expand All @@ -194,6 +193,27 @@ HRESULT CStunSocket::EnablePktInfoOption(bool fEnable)
return hr;
}

HRESULT CStunSocket::SetV6Only(int sock)
{
int optname = -1;
int result = 0;
HRESULT hr = S_OK;
int enabled = 1;

#ifdef IPV6_BINDV6ONLY
optname = IPV6_BINDV6ONLY;
#elif IPV6_V6ONLY
optname = IPV6_V6ONLY;
#else
return E_NOTIMPL;
#endif

result = setsockopt(sock, IPPROTO_IPV6, optname, (char *)&enabled, sizeof(enabled));
hr = (result == 0) ? S_OK : ERRNOHR ;

return hr;
}

HRESULT CStunSocket::SetNonBlocking(bool fEnable)
{
HRESULT hr = S_OK;
Expand Down Expand Up @@ -221,6 +241,7 @@ HRESULT CStunSocket::SetNonBlocking(bool fEnable)
return hr;
}


void CStunSocket::UpdateAddresses()
{
sockaddr_storage addrLocal = {};
Expand Down Expand Up @@ -263,6 +284,15 @@ HRESULT CStunSocket::InitCommon(int socktype, const CSocketAddress& addrlocal, S
sock = socket(addrlocal.GetFamily(), socktype, 0);
ChkIf(sock < 0, ERRNOHR);

if (addrlocal.GetFamily() == AF_INET6)
{
// Don't allow IPv6 socket to receive binding request from IPv4 client
// Because if we don't then an IPv4 client will get an IPv6 mapped address in the binding response
// I'm pretty sure you have to call this before bind()
// Intentionally ignoring result
(void)SetV6Only(sock);
}

if (fSetReuseFlag)
{
int fAllow = 1;
Expand Down
3 changes: 3 additions & 0 deletions networkutils/stunsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class CStunSocket
HRESULT EnablePktInfo_IPV4(bool fEnable);
HRESULT EnablePktInfo_IPV6(bool fEnable);

HRESULT SetV6Only(int sock);

public:

CStunSocket();
Expand All @@ -60,6 +62,7 @@ class CStunSocket
HRESULT EnablePktInfoOption(bool fEnable);
HRESULT SetNonBlocking(bool fEnable);


void UpdateAddresses();

HRESULT UDPInit(const CSocketAddress& local, SocketRole role);
Expand Down

0 comments on commit d6d816e

Please sign in to comment.