daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
bd1529
From 96681723232e9eb0182279086fef291283004806 Mon Sep 17 00:00:00 2001
bd1529
From: Lennart Poettering <lennart@poettering.net>
bd1529
Date: Wed, 27 May 2020 19:27:51 +0200
bd1529
Subject: [PATCH] socket-util: add generic socket_pass_pktinfo() helper
bd1529
bd1529
The helper turns on the protocol specific "packet info" structure cmsg
bd1529
for three relevant protocols we know.
bd1529
bd1529
(cherry picked from commit 35a3eb9bdc95d1e6ba25bc65c78959ea104e45a1)
bd1529
bd1529
Related: #1887181
bd1529
---
bd1529
 src/basic/socket-util.c | 23 +++++++++++++++++++++++
bd1529
 src/basic/socket-util.h |  2 ++
bd1529
 2 files changed, 25 insertions(+)
bd1529
bd1529
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
bd1529
index 986bc6e67f..053bcba670 100644
bd1529
--- a/src/basic/socket-util.c
bd1529
+++ b/src/basic/socket-util.c
bd1529
@@ -1246,3 +1246,26 @@ int socket_ioctl_fd(void) {
bd1529
 
bd1529
         return fd;
bd1529
 }
bd1529
+
bd1529
+int socket_pass_pktinfo(int fd, bool b) {
bd1529
+        int af;
bd1529
+        socklen_t sl = sizeof(af);
bd1529
+
bd1529
+        if (getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &af, &sl) < 0)
bd1529
+                return -errno;
bd1529
+
bd1529
+        switch (af) {
bd1529
+
bd1529
+        case AF_INET:
bd1529
+                return setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, b);
bd1529
+
bd1529
+        case AF_INET6:
bd1529
+                return setsockopt_int(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, b);
bd1529
+
bd1529
+        case AF_NETLINK:
bd1529
+                return setsockopt_int(fd, SOL_NETLINK, NETLINK_PKTINFO, b);
bd1529
+
bd1529
+        default:
bd1529
+                return -EAFNOSUPPORT;
bd1529
+        }
bd1529
+}
bd1529
diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h
bd1529
index 616f2e0d05..c7c9ad34d6 100644
bd1529
--- a/src/basic/socket-util.h
bd1529
+++ b/src/basic/socket-util.h
bd1529
@@ -190,3 +190,5 @@ static inline int setsockopt_int(int fd, int level, int optname, int value) {
bd1529
 
bd1529
         return 0;
bd1529
 }
bd1529
+
bd1529
+int socket_pass_pktinfo(int fd, bool b);