Permalink
Browse files

IPv6 sockets don't get IPv4 bindings

  • Loading branch information...
1 parent ffe0577 commit d6d816e928601ea063702fcb799c8ee32fd4d0be @jselbie committed Jun 22, 2013
Showing with 34 additions and 1 deletion.
  1. +31 −1 networkutils/stunsocket.cpp
  2. +3 −0 networkutils/stunsocket.h
@@ -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();
@@ -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;
@@ -221,6 +241,7 @@ HRESULT CStunSocket::SetNonBlocking(bool fEnable)
return hr;
}
+
void CStunSocket::UpdateAddresses()
{
sockaddr_storage addrLocal = {};
@@ -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;
@@ -38,6 +38,8 @@ class CStunSocket
HRESULT EnablePktInfo_IPV4(bool fEnable);
HRESULT EnablePktInfo_IPV6(bool fEnable);
+ HRESULT SetV6Only(int sock);
+
public:
CStunSocket();
@@ -60,6 +62,7 @@ class CStunSocket
HRESULT EnablePktInfoOption(bool fEnable);
HRESULT SetNonBlocking(bool fEnable);
+
void UpdateAddresses();
HRESULT UDPInit(const CSocketAddress& local, SocketRole role);

0 comments on commit d6d816e

Please sign in to comment.