Skip to content

Commit b8b1bd5

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 cf0f741 commit b8b1bd5

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

src/basic/in-addr-util.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ bool in4_addr_is_null(const struct in_addr *a) {
2828
bool in6_addr_is_null(const struct in6_addr *a) {
2929
assert(a);
3030

31-
return IN6_IS_ADDR_UNSPECIFIED(a);
31+
return a->in6_u.u6_addr32[0] == 0 && a->in6_u.u6_addr32[1] == 0 && a->in6_u.u6_addr32[2] == 0 && a->in6_u.u6_addr32[3] == 0;
3232
}
3333

3434
int in_addr_is_null(int family, const union in_addr_union *u) {
@@ -66,7 +66,7 @@ bool in4_addr_is_link_local_dynamic(const struct in_addr *a) {
6666
bool in6_addr_is_link_local(const struct in6_addr *a) {
6767
assert(a);
6868

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

7272
int in_addr_is_link_local(int family, const union in_addr_union *u) {
@@ -100,7 +100,7 @@ bool in4_addr_is_multicast(const struct in_addr *a) {
100100
bool in6_addr_is_multicast(const struct in6_addr *a) {
101101
assert(a);
102102

103-
return IN6_IS_ADDR_MULTICAST(a);
103+
return (((const uint8_t *) (a))[0] == 0xff);
104104
}
105105

106106
int in_addr_is_multicast(int family, const union in_addr_union *u) {
@@ -136,14 +136,21 @@ bool in4_addr_is_non_local(const struct in_addr *a) {
136136
!in4_addr_is_localhost(a);
137137
}
138138

139+
static bool in6_addr_is_loopback(const struct in6_addr *a) {
140+
return (((const uint32_t *) (a))[0] == 0 &&
141+
((const uint32_t *) (a))[1] == 0 &&
142+
((const uint32_t *) (a))[2] == 0 &&
143+
((const uint32_t *) (a))[3] == htobe32(1));
144+
}
145+
139146
int in_addr_is_localhost(int family, const union in_addr_union *u) {
140147
assert(u);
141148

142149
if (family == AF_INET)
143150
return in4_addr_is_localhost(&u->in);
144151

145152
if (family == AF_INET6)
146-
return IN6_IS_ADDR_LOOPBACK(&u->in6);
153+
return in6_addr_is_loopback(&u->in6);
147154

148155
return -EAFNOSUPPORT;
149156
}
@@ -156,7 +163,7 @@ int in_addr_is_localhost_one(int family, const union in_addr_union *u) {
156163
return be32toh(u->in.s_addr) == UINT32_C(0x7F000001);
157164

158165
if (family == AF_INET6)
159-
return IN6_IS_ADDR_LOOPBACK(&u->in6);
166+
return in6_addr_is_loopback(&u->in6);
160167

161168
return -EAFNOSUPPORT;
162169
}
@@ -178,7 +185,10 @@ bool in6_addr_equal(const struct in6_addr *a, const struct in6_addr *b) {
178185
assert(a);
179186
assert(b);
180187

181-
return IN6_ARE_ADDR_EQUAL(a, b);
188+
return a->in6_u.u6_addr32[0] == b->in6_u.u6_addr32[0] &&
189+
a->in6_u.u6_addr32[1] == b->in6_u.u6_addr32[1] &&
190+
a->in6_u.u6_addr32[2] == b->in6_u.u6_addr32[2] &&
191+
a->in6_u.u6_addr32[3] == b->in6_u.u6_addr32[3];
182192
}
183193

184194
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 __attr_access ((__write_only__, 2));

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)