Skip to content

Commit d037a5f

Browse files
tcp,udp: set TOS (TCLASS) for IPv6 sockets
1 parent a07bc3e commit d037a5f

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/tcp/tcp.c

+30-2
Original file line numberDiff line numberDiff line change
@@ -1450,12 +1450,25 @@ int tcp_settos(struct tcp_sock *ts, uint32_t tos)
14501450
{
14511451
int err = 0;
14521452
int v = tos;
1453+
struct sa sa;
14531454

14541455
if (!ts)
14551456
return EINVAL;
14561457

14571458
ts->tos = tos;
1458-
err = tcp_sock_setopt(ts, IPPROTO_IP, IP_TOS, &v, sizeof(v));
1459+
err = tcp_local_get(ts, &sa);
1460+
if (err)
1461+
return err;
1462+
1463+
if (sa_af(&sa) == AF_INET) {
1464+
err = tcp_sock_setopt(ts, IPPROTO_IP, IP_TOS, &v, sizeof(v));
1465+
}
1466+
#ifdef IPV6_TCLASS
1467+
else if (sa_af(&sa) == AF_INET6) {
1468+
err = tcp_sock_setopt(ts, IPPROTO_IPV6, IPV6_TCLASS, &v,
1469+
sizeof(v));
1470+
}
1471+
#endif
14591472

14601473
return err;
14611474
}
@@ -1465,16 +1478,31 @@ int tcp_conn_settos(struct tcp_conn *tc, uint32_t tos)
14651478
{
14661479
int err = 0;
14671480
int v = tos;
1481+
struct sa sa;
14681482

14691483
if (!tc)
14701484
return EINVAL;
14711485

14721486
tc->tos = tos;
1473-
if (tc->fdc != RE_BAD_SOCK) {
1487+
if (tc->fdc == RE_BAD_SOCK)
1488+
return err;
1489+
1490+
err = tcp_conn_local_get(tc, &sa);
1491+
if (err)
1492+
return err;
1493+
1494+
if (sa_af(&sa) == AF_INET) {
14741495
if (0 != setsockopt(tc->fdc, IPPROTO_IP, IP_TOS,
14751496
BUF_CAST &v, sizeof(v)))
14761497
err = RE_ERRNO_SOCK;
14771498
}
1499+
#ifdef IPV6_TCLASS
1500+
else if (sa_af(&sa) == AF_INET6) {
1501+
if (0 != setsockopt(tc->fdc, IPPROTO_IPV6, IPV6_TCLASS,
1502+
BUF_CAST &v, sizeof(v)))
1503+
err = RE_ERRNO_SOCK;
1504+
}
1505+
#endif
14781506

14791507
return err;
14801508
}

src/udp/udp.c

+15-1
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ int udp_settos(struct udp_sock *us, uint8_t tos)
628628
{
629629
int err = 0;
630630
int v = tos;
631+
struct sa sa;
631632
#ifdef WIN32
632633
QOS_VERSION qos_version = { 1 , 0 };
633634
QOS_TRAFFIC_TYPE qos_type = QOSTrafficTypeBestEffort;
@@ -660,7 +661,20 @@ int udp_settos(struct udp_sock *us, uint8_t tos)
660661
return WSAGetLastError();
661662
}
662663
#endif
663-
err = udp_setsockopt(us, IPPROTO_IP, IP_TOS, &v, sizeof(v));
664+
err = udp_local_get(us, &sa);
665+
if (err)
666+
return err;
667+
668+
if (sa_af(&sa) == AF_INET) {
669+
err = udp_setsockopt(us, IPPROTO_IP, IP_TOS, &v, sizeof(v));
670+
}
671+
#ifdef IPV6_TCLASS
672+
else if (sa_af(&sa) == AF_INET6) {
673+
err = udp_setsockopt(us, IPPROTO_IPV6, IPV6_TCLASS, &v,
674+
sizeof(v));
675+
}
676+
#endif
677+
664678
return err;
665679
}
666680

0 commit comments

Comments
 (0)