-
Notifications
You must be signed in to change notification settings - Fork 240
IPv6 QoS fix #3622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rdica
wants to merge
6
commits into
jamulussoftware:main
Choose a base branch
from
rdica:IPv6-QoS-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+80
−6
Open
IPv6 QoS fix #3622
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bdd65af
Correct data size for IPV6_TCLASS
e39af2d
Add error checking for setsockopt()
softins a647941
Correct data size for IPV6_V6ONLY
softins 6de1d02
Also need setsockopt() for IPv4 on a dual-proto socket
softins b5a38db
Remove socket options that do not work on Windows and Apple
softins 22a4293
Add macOS support for TOS on IPv4 over IPv6
softins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -114,12 +114,28 @@ void CSocket::Init ( const quint16 iNewPortNumber, const quint16 iNewQosNumber, | |||||
|
|
||||||
| // The IPV6_V6ONLY socket option must be false in order for the socket to listen on both protocols. | ||||||
| // On Linux it's false by default on most (all?) distros, but on Windows it is true by default | ||||||
| const uint8_t no = 0; | ||||||
| setsockopt ( UdpSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*) &no, sizeof ( no ) ); | ||||||
| const int no = 0; | ||||||
| if ( setsockopt ( UdpSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*) &no, sizeof ( no ) ) == -1 ) | ||||||
| { | ||||||
| throw CGenErr ( "setsockopt for IPV6_V6ONLY failed", "Network Error" ); | ||||||
| } | ||||||
|
|
||||||
| // set the QoS | ||||||
| const char tos = (char) iQosNumber; // Quality of Service | ||||||
| setsockopt ( UdpSocket, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof ( tos ) ); | ||||||
| const int tos = (int) iQosNumber; // Quality of Service | ||||||
ann0see marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| #if !defined( Q_OS_WIN ) | ||||||
| if ( setsockopt ( UdpSocket, IPPROTO_IPV6, IPV6_TCLASS, (const char*) &tos, sizeof ( tos ) ) == -1 ) | ||||||
| { | ||||||
| throw CGenErr ( "setsockopt for IPV6_TCLASS failed", "Network Error" ); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| #endif | ||||||
|
|
||||||
| #if !defined( Q_OS_DARWIN ) | ||||||
| // set the QoS for IPv4 as well, as this is a dual-protocol socket | ||||||
| if ( setsockopt ( UdpSocket, IPPROTO_IP, IP_TOS, (const char*) &tos, sizeof ( tos ) ) == -1 ) | ||||||
| { | ||||||
| throw CGenErr ( "setsockopt for IP_TOS failed", "Network Error" ); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| #endif | ||||||
|
|
||||||
| UdpSocketAddr.sa6.sin6_family = AF_INET6; | ||||||
| UdpSocketAddr.sa6.sin6_addr = in6addr_any; | ||||||
|
|
@@ -147,8 +163,11 @@ void CSocket::Init ( const quint16 iNewPortNumber, const quint16 iNewQosNumber, | |||||
| } | ||||||
|
|
||||||
| // set the QoS | ||||||
| const char tos = (char) iQosNumber; // Quality of Service | ||||||
| setsockopt ( UdpSocket, IPPROTO_IP, IP_TOS, &tos, sizeof ( tos ) ); | ||||||
| const int tos = (int) iQosNumber; // Quality of Service | ||||||
| if ( setsockopt ( UdpSocket, IPPROTO_IP, IP_TOS, (const char*) &tos, sizeof ( tos ) ) == -1 ) | ||||||
| { | ||||||
| throw CGenErr ( "setsockopt for IP_TOS failed", "Network Error" ); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| // preinitialize socket in address (only the port number is missing) | ||||||
| UdpSocketAddr.sa4.sin_family = AF_INET; | ||||||
|
|
@@ -251,6 +270,50 @@ CSocket::~CSocket() | |||||
| #endif | ||||||
| } | ||||||
|
|
||||||
| #if defined( Q_OS_DARWIN ) | ||||||
| // sendto_ipv4_with_tos - helper function for macOS to set TOS when sending IPv4 over IPv6 socket | ||||||
| static ssize_t sendto_ipv4_with_tos ( int fd, const void* buf, size_t len, int flags, const struct sockaddr* dest, socklen_t destlen, int tos ) | ||||||
| { | ||||||
| // For a description of 'struct cmsghdr' and the 'CMSG_xxx' macros, see 'man 3 cmsg' on a Linux machine. | ||||||
| // The macOS man pages are less descriptive, but the API is the same, being based on the BSD socket interface. | ||||||
|
|
||||||
| // The cmsg buffer is only set up once (tos doesn't change) so can be static | ||||||
| static union | ||||||
| { | ||||||
| unsigned char cbuf[CMSG_SPACE ( sizeof ( int ) )]; | ||||||
| struct cmsghdr h; | ||||||
| } u; | ||||||
| static socklen_t clen = 0; | ||||||
pljones marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| if ( clen == 0 ) | ||||||
| { | ||||||
| // set up the cmsg buffer | ||||||
| memset ( u.cbuf, 0, sizeof ( u.cbuf ) ); | ||||||
|
|
||||||
| u.h.cmsg_level = IPPROTO_IP; | ||||||
| u.h.cmsg_type = IP_TOS; | ||||||
| u.h.cmsg_len = CMSG_LEN ( sizeof ( int ) ); | ||||||
| memcpy ( CMSG_DATA ( &u.h ), &tos, sizeof ( int ) ); | ||||||
| clen = (socklen_t) u.h.cmsg_len; | ||||||
| } | ||||||
|
|
||||||
| struct iovec iov; | ||||||
| iov.iov_base = const_cast<void*> ( buf ); | ||||||
| iov.iov_len = len; | ||||||
|
|
||||||
| struct msghdr msg; | ||||||
|
|
||||||
| msg.msg_name = const_cast<sockaddr*> ( dest ); | ||||||
| msg.msg_namelen = destlen; | ||||||
| msg.msg_iov = &iov; | ||||||
| msg.msg_iovlen = 1; | ||||||
| msg.msg_control = (void*) u.cbuf; | ||||||
| msg.msg_controllen = clen; | ||||||
|
|
||||||
| return sendmsg ( fd, &msg, flags ); | ||||||
| } | ||||||
| #endif | ||||||
|
|
||||||
| void CSocket::SendPacket ( const CVector<uint8_t>& vecbySendBuf, const CHostAddress& HostAddr ) | ||||||
| { | ||||||
| int status = 0; | ||||||
|
|
@@ -290,12 +353,23 @@ void CSocket::SendPacket ( const CVector<uint8_t>& vecbySendBuf, const CHostAddr | |||||
| addr[2] = htonl ( 0xFFFF ); | ||||||
| addr[3] = htonl ( HostAddr.InetAddr.toIPv4Address() ); | ||||||
|
|
||||||
| #if defined( Q_OS_DARWIN ) | ||||||
| // In macOS we need to set TOS explicitly when sending IPv4 over IPv6 socket | ||||||
| status = sendto_ipv4_with_tos ( UdpSocket, | ||||||
| (const char*) &( (CVector<uint8_t>) vecbySendBuf )[0], | ||||||
| iVecSizeOut, | ||||||
| 0, | ||||||
| &UdpSocketAddr.sa, | ||||||
| sizeof ( UdpSocketAddr.sa6 ), | ||||||
| (int) iQosNumber ); | ||||||
| #else | ||||||
| status = sendto ( UdpSocket, | ||||||
| (const char*) &( (CVector<uint8_t>) vecbySendBuf )[0], | ||||||
| iVecSizeOut, | ||||||
| 0, | ||||||
| &UdpSocketAddr.sa, | ||||||
| sizeof ( UdpSocketAddr.sa6 ) ); | ||||||
| #endif | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this say "to clear IPV6_V6ONLY" - otherwise people might get confused as to what went wrong.
Also, something "less technical", similar to the line 112 error -- maybe "request to clear block on IPv4 over IPv6 failed"?