ryantimwilson / rpms / systemd

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