basic: Add our own <netinet/in.h> and <net/if.h> headers

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.
This commit is contained in:
Daan De Meyer
2025-04-30 07:50:03 +02:00
parent 3a03b97d6f
commit d559f46384
5 changed files with 44 additions and 11 deletions
+11 -6
View 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) {
+9
View File
@@ -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;
+19
View File
@@ -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;