Skip to content

Commit 126a7ba

Browse files
committed
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.
1 parent 3a03b97 commit 126a7ba

File tree

5 files changed

+44
-11
lines changed

5 files changed

+44
-11
lines changed

src/basic/in-addr-util.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "in-addr-util.h"
1414
#include "logarithm.h"
1515
#include "macro.h"
16+
#include "memory-util.h"
1617
#include "parse-util.h"
1718
#include "random-util.h"
1819
#include "stdio-util.h"
@@ -28,7 +29,7 @@ bool in4_addr_is_null(const struct in_addr *a) {
2829
bool in6_addr_is_null(const struct in6_addr *a) {
2930
assert(a);
3031

31-
return IN6_IS_ADDR_UNSPECIFIED(a);
32+
return eqzero(a->in6_u.u6_addr32);
3233
}
3334

3435
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) {
6667
bool in6_addr_is_link_local(const struct in6_addr *a) {
6768
assert(a);
6869

69-
return IN6_IS_ADDR_LINKLOCAL(a);
70+
return (a->in6_u.u6_addr32[0] & htobe32(0xffc00000)) == htobe32(0xfe800000);
7071
}
7172

7273
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) {
100101
bool in6_addr_is_multicast(const struct in6_addr *a) {
101102
assert(a);
102103

103-
return IN6_IS_ADDR_MULTICAST(a);
104+
return a->in6_u.u6_addr8[0] == 0xff;
104105
}
105106

106107
int in_addr_is_multicast(int family, const union in_addr_union *u) {
@@ -136,14 +137,18 @@ bool in4_addr_is_non_local(const struct in_addr *a) {
136137
!in4_addr_is_localhost(a);
137138
}
138139

140+
static bool in6_addr_is_loopback(const struct in6_addr *a) {
141+
return memcmp(a, &(struct in6_addr) IN6ADDR_LOOPBACK_INIT, sizeof(struct in6_addr)) == 0;
142+
}
143+
139144
int in_addr_is_localhost(int family, const union in_addr_union *u) {
140145
assert(u);
141146

142147
if (family == AF_INET)
143148
return in4_addr_is_localhost(&u->in);
144149

145150
if (family == AF_INET6)
146-
return IN6_IS_ADDR_LOOPBACK(&u->in6);
151+
return in6_addr_is_loopback(&u->in6);
147152

148153
return -EAFNOSUPPORT;
149154
}
@@ -156,7 +161,7 @@ int in_addr_is_localhost_one(int family, const union in_addr_union *u) {
156161
return be32toh(u->in.s_addr) == UINT32_C(0x7F000001);
157162

158163
if (family == AF_INET6)
159-
return IN6_IS_ADDR_LOOPBACK(&u->in6);
164+
return in6_addr_is_loopback(&u->in6);
160165

161166
return -EAFNOSUPPORT;
162167
}
@@ -178,7 +183,7 @@ bool in6_addr_equal(const struct in6_addr *a, const struct in6_addr *b) {
178183
assert(a);
179184
assert(b);
180185

181-
return IN6_ARE_ADDR_EQUAL(a, b);
186+
return memcmp(a, b, sizeof(struct in6_addr)) == 0;
182187
}
183188

184189
int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b) {

src/basic/include/net/if.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2+
#pragma once
3+
4+
#include <linux/if.h>
5+
6+
#define IF_NAMESIZE 16
7+
8+
extern unsigned int if_nametoindex(const char *__ifname) __THROW;
9+
extern char *if_indextoname(unsigned int __ifindex, char __ifname[IF_NAMESIZE]) __THROW;

src/basic/include/netinet/in.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2+
#pragma once
3+
4+
#include <linux/in.h>
5+
#include <linux/in6.h>
6+
#include <linux/ipv6.h>
7+
#include <stdint.h>
8+
#include <stddef.h>
9+
#include <sys/socket.h>
10+
11+
#define INET_ADDRSTRLEN 16
12+
#define INET6_ADDRSTRLEN 46
13+
14+
extern const struct in6_addr in6addr_any; /* :: */
15+
extern const struct in6_addr in6addr_loopback; /* ::1 */
16+
#define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
17+
#define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
18+
19+
typedef uint32_t in_addr_t;

src/libsystemd-network/icmp6-util.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ int icmp6_bind(int ifindex, bool is_router) {
3434
if (is_router) {
3535
mreq = (struct ipv6_mreq) {
3636
.ipv6mr_multiaddr = IN6_ADDR_ALL_ROUTERS_MULTICAST,
37-
.ipv6mr_interface = ifindex,
37+
.ipv6mr_ifindex = ifindex,
3838
};
3939
ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
4040
} else {
4141
mreq = (struct ipv6_mreq) {
4242
.ipv6mr_multiaddr = IN6_ADDR_ALL_NODES_MULTICAST,
43-
.ipv6mr_interface = ifindex,
43+
.ipv6mr_ifindex = ifindex,
4444
};
4545
ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
4646
ICMP6_FILTER_SETPASS(ND_NEIGHBOR_ADVERT, &filter);
@@ -76,7 +76,7 @@ int icmp6_bind(int ifindex, bool is_router) {
7676
if (r < 0)
7777
return r;
7878

79-
r = setsockopt_int(s, SOL_IPV6, IPV6_RECVHOPLIMIT, true);
79+
r = setsockopt_int(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, true);
8080
if (r < 0)
8181
return r;
8282

@@ -149,7 +149,7 @@ int icmp6_receive(
149149

150150
assert(!(msg.msg_flags & MSG_TRUNC));
151151

152-
int *hops = CMSG_FIND_DATA(&msg, SOL_IPV6, IPV6_HOPLIMIT, int);
152+
int *hops = CMSG_FIND_DATA(&msg, IPPROTO_IPV6, IPV6_HOPLIMIT, int);
153153
if (hops && *hops != 255)
154154
return -EMULTIHOP;
155155

src/resolve/resolved-dns-scope.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ static int dns_scope_multicast_membership(DnsScope *s, bool b, struct in_addr in
929929
} else if (s->family == AF_INET6) {
930930
struct ipv6_mreq mreq = {
931931
.ipv6mr_multiaddr = in6,
932-
.ipv6mr_interface = s->link->ifindex,
932+
.ipv6mr_ifindex = s->link->ifindex,
933933
};
934934

935935
if (s->protocol == DNS_PROTOCOL_LLMNR)

0 commit comments

Comments
 (0)