dcavalca / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone
Blob Blame History Raw
From 96681723232e9eb0182279086fef291283004806 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Wed, 27 May 2020 19:27:51 +0200
Subject: [PATCH] socket-util: add generic socket_pass_pktinfo() helper

The helper turns on the protocol specific "packet info" structure cmsg
for three relevant protocols we know.

(cherry picked from commit 35a3eb9bdc95d1e6ba25bc65c78959ea104e45a1)

Related: #1887181
---
 src/basic/socket-util.c | 23 +++++++++++++++++++++++
 src/basic/socket-util.h |  2 ++
 2 files changed, 25 insertions(+)

diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
index 986bc6e67f..053bcba670 100644
--- a/src/basic/socket-util.c
+++ b/src/basic/socket-util.c
@@ -1246,3 +1246,26 @@ int socket_ioctl_fd(void) {
 
         return fd;
 }
+
+int socket_pass_pktinfo(int fd, bool b) {
+        int af;
+        socklen_t sl = sizeof(af);
+
+        if (getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &af, &sl) < 0)
+                return -errno;
+
+        switch (af) {
+
+        case AF_INET:
+                return setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, b);
+
+        case AF_INET6:
+                return setsockopt_int(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, b);
+
+        case AF_NETLINK:
+                return setsockopt_int(fd, SOL_NETLINK, NETLINK_PKTINFO, b);
+
+        default:
+                return -EAFNOSUPPORT;
+        }
+}
diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h
index 616f2e0d05..c7c9ad34d6 100644
--- a/src/basic/socket-util.h
+++ b/src/basic/socket-util.h
@@ -190,3 +190,5 @@ static inline int setsockopt_int(int fd, int level, int optname, int value) {
 
         return 0;
 }
+
+int socket_pass_pktinfo(int fd, bool b);