Change in libosmocore[master]: socket: IPv6 support for osmo_sock_set_dscp()

laforge gerrit-no-reply at lists.osmocom.org
Wed Apr 28 17:33:40 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/23935 )

Change subject: socket: IPv6 support for osmo_sock_set_dscp()
......................................................................

socket: IPv6 support for osmo_sock_set_dscp()

IPv6 has the analogous to DSCP: The "traffic class" field.

See https://tools.ietf.org/html/draft-itojun-ipv6-tclass-api-03

Change-Id: Ib31b977f67d60aa7f30ca4ab6eceba3d1d5eeee1
Related: SYS#5427
---
M src/socket.c
1 file changed, 36 insertions(+), 7 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/src/socket.c b/src/socket.c
index 6afe986..59d0876 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1800,25 +1800,54 @@
  *  \returns 0 on success; negative on error. */
 int osmo_sock_set_dscp(int fd, uint8_t dscp)
 {
+	struct sockaddr_storage local_addr;
+	socklen_t local_addr_len = sizeof(local_addr);
 	uint8_t tos;
 	socklen_t tos_len = sizeof(tos);
+	int tclass;
+	socklen_t tclass_len = sizeof(tclass);
 	int rc;
 
 	/* DSCP is a 6-bit value stored in the upper 6 bits of the 8-bit TOS */
 	if (dscp > 63)
 		return -EINVAL;
 
-	/* read the original value */
-	rc = getsockopt(fd, IPPROTO_IP, IP_TOS, &tos, &tos_len);
+	rc = getsockname(fd, (struct sockaddr *)&local_addr, &local_addr_len);
 	if (rc < 0)
 		return rc;
 
-	/* mask-in the DSCP into the upper 6 bits */
-	tos &= 0x03;
-	tos |= dscp << 2;
+	switch (local_addr.ss_family) {
+	case AF_INET:
+		/* read the original value */
+		rc = getsockopt(fd, IPPROTO_IP, IP_TOS, &tos, &tos_len);
+		if (rc < 0)
+			return rc;
+		/* mask-in the DSCP into the upper 6 bits */
+		tos &= 0x03;
+		tos |= dscp << 2;
+		/* and write it back to the kernel */
+		rc = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
+		break;
+	case AF_INET6:
+		/* read the original value */
+		rc = getsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tclass, &tclass_len);
+		if (rc < 0)
+			return rc;
+		/* mask-in the DSCP into the upper 6 bits */
+		tclass &= 0x03;
+		tclass |= dscp << 2;
+		/* and write it back to the kernel */
+		rc = setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tclass, sizeof(tclass));
+		break;
+	case AF_UNSPEC:
+	default:
+		LOGP(DLGLOBAL, LOGL_ERROR, "No DSCP support for socket family %u\n",
+		     local_addr.ss_family);
+		rc = -1;
+		break;
+	}
 
-	/* and write it back to the kernel */
-	return setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
+	return rc;
 }
 
 /*! Set the priority value of a socket.

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/23935
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Ib31b977f67d60aa7f30ca4ab6eceba3d1d5eeee1
Gerrit-Change-Number: 23935
Gerrit-PatchSet: 3
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: pespin <pespin at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210428/0db129f7/attachment.htm>


More information about the gerrit-log mailing list