Skip to content

Commit

Permalink
tcp,udp: set TOS (TCLASS) for IPv6 sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianfridrich committed Dec 12, 2024
1 parent a07bc3e commit f1d34a1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
32 changes: 30 additions & 2 deletions src/tcp/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1450,12 +1450,25 @@ int tcp_settos(struct tcp_sock *ts, uint32_t tos)
{
int err = 0;
int v = tos;
struct sa sa;

if (!ts)
return EINVAL;

ts->tos = tos;
err = tcp_sock_setopt(ts, IPPROTO_IP, IP_TOS, &v, sizeof(v));
err = tcp_local_get(ts, &sa);
if (err)
return err;

if (sa_af(&sa) == AF_INET) {
err = tcp_sock_setopt(ts, IPPROTO_IP, IP_TOS, &v, sizeof(v));
}
#if defined(IPV6_TCLASS) && !defined(WIN32)
else if (sa_af(&sa) == AF_INET6) {
err = tcp_sock_setopt(ts, IPPROTO_IPV6, IPV6_TCLASS, &v,
sizeof(v));
}
#endif

return err;
}
Expand All @@ -1465,16 +1478,31 @@ int tcp_conn_settos(struct tcp_conn *tc, uint32_t tos)
{
int err = 0;
int v = tos;
struct sa sa;

if (!tc)
return EINVAL;

tc->tos = tos;
if (tc->fdc != RE_BAD_SOCK) {
if (tc->fdc == RE_BAD_SOCK)
return err;

err = tcp_conn_local_get(tc, &sa);
if (err)
return err;

if (sa_af(&sa) == AF_INET) {
if (0 != setsockopt(tc->fdc, IPPROTO_IP, IP_TOS,
BUF_CAST &v, sizeof(v)))
err = RE_ERRNO_SOCK;
}
#if defined(IPV6_TCLASS) && !defined(WIN32)
else if (sa_af(&sa) == AF_INET6) {
if (0 != setsockopt(tc->fdc, IPPROTO_IPV6, IPV6_TCLASS,
BUF_CAST &v, sizeof(v)))
err = RE_ERRNO_SOCK;
}
#endif

return err;
}
Expand Down
16 changes: 15 additions & 1 deletion src/udp/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ int udp_settos(struct udp_sock *us, uint8_t tos)
{
int err = 0;
int v = tos;
struct sa sa;
#ifdef WIN32
QOS_VERSION qos_version = { 1 , 0 };
QOS_TRAFFIC_TYPE qos_type = QOSTrafficTypeBestEffort;
Expand Down Expand Up @@ -660,7 +661,20 @@ int udp_settos(struct udp_sock *us, uint8_t tos)
return WSAGetLastError();
}
#endif
err = udp_setsockopt(us, IPPROTO_IP, IP_TOS, &v, sizeof(v));
err = udp_local_get(us, &sa);
if (err)
return err;

if (sa_af(&sa) == AF_INET) {
err = udp_setsockopt(us, IPPROTO_IP, IP_TOS, &v, sizeof(v));
}
#if defined(IPV6_TCLASS) && !defined(WIN32)
else if (sa_af(&sa) == AF_INET6) {
err = udp_setsockopt(us, IPPROTO_IPV6, IPV6_TCLASS, &v,
sizeof(v));
}
#endif

return err;
}

Expand Down

0 comments on commit f1d34a1

Please sign in to comment.