]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: Add our own <netinet/in.h> and <net/if.h> headers
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 29 Apr 2025 13:20:02 +0000 (15:20 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 30 Apr 2025 05:50:03 +0000 (07:50 +0200)
These glibc headers conflicts with the corresponding linux headers
(<linux/in.h> and <linux/if.h>) and impose an include order (the glibc one
has to be included before any linux header is included). This makes sorting
includes a royal pain so let's define our own versions of these headers using
various linux headers to do all the work and filling in the missing bits
ourselves.

src/basic/in-addr-util.c
src/basic/include/net/if.h [new file with mode: 0644]
src/basic/include/netinet/in.h [new file with mode: 0644]
src/libsystemd-network/icmp6-util.c
src/resolve/resolved-dns-scope.c

index f1f3a195c089d657e3eec4be85b15ceb836bdc99..4e39985e27514c2b92f61d1f3120e9d0b629cadb 100644 (file)
@@ -13,6 +13,7 @@
 #include "in-addr-util.h"
 #include "logarithm.h"
 #include "macro.h"
+#include "memory-util.h"
 #include "parse-util.h"
 #include "random-util.h"
 #include "stdio-util.h"
@@ -28,7 +29,7 @@ bool in4_addr_is_null(const struct in_addr *a) {
 bool in6_addr_is_null(const struct in6_addr *a) {
         assert(a);
 
-        return IN6_IS_ADDR_UNSPECIFIED(a);
+        return eqzero(a->in6_u.u6_addr32);
 }
 
 int in_addr_is_null(int family, const union in_addr_union *u) {
@@ -66,7 +67,7 @@ bool in4_addr_is_link_local_dynamic(const struct in_addr *a) {
 bool in6_addr_is_link_local(const struct in6_addr *a) {
         assert(a);
 
-        return IN6_IS_ADDR_LINKLOCAL(a);
+        return (a->in6_u.u6_addr32[0] & htobe32(0xffc00000)) == htobe32(0xfe800000);
 }
 
 int in_addr_is_link_local(int family, const union in_addr_union *u) {
@@ -100,7 +101,7 @@ bool in4_addr_is_multicast(const struct in_addr *a) {
 bool in6_addr_is_multicast(const struct in6_addr *a) {
         assert(a);
 
-        return IN6_IS_ADDR_MULTICAST(a);
+        return a->in6_u.u6_addr8[0] == 0xff;
 }
 
 int in_addr_is_multicast(int family, const union in_addr_union *u) {
@@ -136,6 +137,10 @@ bool in4_addr_is_non_local(const struct in_addr *a) {
                !in4_addr_is_localhost(a);
 }
 
+static bool in6_addr_is_loopback(const struct in6_addr *a) {
+        return memcmp(a, &(struct in6_addr) IN6ADDR_LOOPBACK_INIT, sizeof(struct in6_addr)) == 0;
+}
+
 int in_addr_is_localhost(int family, const union in_addr_union *u) {
         assert(u);
 
@@ -143,7 +148,7 @@ int in_addr_is_localhost(int family, const union in_addr_union *u) {
                 return in4_addr_is_localhost(&u->in);
 
         if (family == AF_INET6)
-                return IN6_IS_ADDR_LOOPBACK(&u->in6);
+                return in6_addr_is_loopback(&u->in6);
 
         return -EAFNOSUPPORT;
 }
@@ -156,7 +161,7 @@ int in_addr_is_localhost_one(int family, const union in_addr_union *u) {
                 return be32toh(u->in.s_addr) == UINT32_C(0x7F000001);
 
         if (family == AF_INET6)
-                return IN6_IS_ADDR_LOOPBACK(&u->in6);
+                return in6_addr_is_loopback(&u->in6);
 
         return -EAFNOSUPPORT;
 }
@@ -178,7 +183,7 @@ bool in6_addr_equal(const struct in6_addr *a, const struct in6_addr *b) {
         assert(a);
         assert(b);
 
-        return IN6_ARE_ADDR_EQUAL(a, b);
+        return memcmp(a, b, sizeof(struct in6_addr)) == 0;
 }
 
 int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b) {
diff --git a/src/basic/include/net/if.h b/src/basic/include/net/if.h
new file mode 100644 (file)
index 0000000..7d5b61b
--- /dev/null
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <linux/if.h>
+
+#define IF_NAMESIZE       16
+
+extern unsigned int if_nametoindex(const char *__ifname) __THROW;
+extern char *if_indextoname(unsigned int __ifindex, char __ifname[IF_NAMESIZE]) __THROW;
diff --git a/src/basic/include/netinet/in.h b/src/basic/include/netinet/in.h
new file mode 100644 (file)
index 0000000..97475ac
--- /dev/null
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <linux/in.h>
+#include <linux/in6.h>
+#include <linux/ipv6.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <sys/socket.h>
+
+#define INET_ADDRSTRLEN 16
+#define INET6_ADDRSTRLEN 46
+
+extern const struct in6_addr in6addr_any;        /* :: */
+extern const struct in6_addr in6addr_loopback;   /* ::1 */
+#define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
+#define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
+
+typedef uint32_t in_addr_t;
index b3e3adfee0ef98df5ae4eb0d2a3802a549bae2e6..70190164c4f548dc763556a3119a5fb4b4cc2362 100644 (file)
@@ -34,13 +34,13 @@ int icmp6_bind(int ifindex, bool is_router) {
         if (is_router) {
                 mreq = (struct ipv6_mreq) {
                         .ipv6mr_multiaddr = IN6_ADDR_ALL_ROUTERS_MULTICAST,
-                        .ipv6mr_interface = ifindex,
+                        .ipv6mr_ifindex = ifindex,
                 };
                 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
         } else {
                 mreq = (struct ipv6_mreq) {
                         .ipv6mr_multiaddr = IN6_ADDR_ALL_NODES_MULTICAST,
-                        .ipv6mr_interface = ifindex,
+                        .ipv6mr_ifindex = ifindex,
                 };
                 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
                 ICMP6_FILTER_SETPASS(ND_NEIGHBOR_ADVERT, &filter);
@@ -76,7 +76,7 @@ int icmp6_bind(int ifindex, bool is_router) {
         if (r < 0)
                 return r;
 
-        r = setsockopt_int(s, SOL_IPV6, IPV6_RECVHOPLIMIT, true);
+        r = setsockopt_int(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, true);
         if (r < 0)
                 return r;
 
@@ -149,7 +149,7 @@ int icmp6_receive(
 
         assert(!(msg.msg_flags & MSG_TRUNC));
 
-        int *hops = CMSG_FIND_DATA(&msg, SOL_IPV6, IPV6_HOPLIMIT, int);
+        int *hops = CMSG_FIND_DATA(&msg, IPPROTO_IPV6, IPV6_HOPLIMIT, int);
         if (hops && *hops != 255)
                 return -EMULTIHOP;
 
index c9ea2d31d541ebc60bff33f03eec8aaa40ab2195..b76bc710fa8729e13ae808e8d26dd0fb129fbb7c 100644 (file)
@@ -929,7 +929,7 @@ static int dns_scope_multicast_membership(DnsScope *s, bool b, struct in_addr in
         } else if (s->family == AF_INET6) {
                 struct ipv6_mreq mreq = {
                         .ipv6mr_multiaddr = in6,
-                        .ipv6mr_interface = s->link->ifindex,
+                        .ipv6mr_ifindex = s->link->ifindex,
                 };
 
                 if (s->protocol == DNS_PROTOCOL_LLMNR)