• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 25976879819

16 May 2026 07:16PM UTC coverage: 72.329% (-0.2%) from 72.557%
25976879819

push

github

web-flow
sd-dhcp-client: two tiny cleanups (#42129)

22 of 32 new or added lines in 4 files covered. (68.75%)

4308 existing lines in 77 files now uncovered.

328373 of 453998 relevant lines covered (72.33%)

1552889.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.0
/src/basic/socket-util.h
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
#pragma once
3

4
#include <linux/if_packet.h>
5
#include <linux/netlink.h>
6
#include <linux/vm_sockets.h>
7
#include <netinet/in.h>
8
#include <sys/socket.h>
9
#include <sys/un.h>
10

11
#include "basic-forward.h"
12
#include "memory-util.h"
13
#include "missing-network.h"
14

15
union sockaddr_union {
16
        /* The minimal, abstract version */
17
        struct sockaddr sa;
18

19
        /* The libc provided version that allocates "enough room" for every protocol */
20
        struct sockaddr_storage storage;
21

22
        /* Protocol-specific implementations */
23
        struct sockaddr_in in;
24
        struct sockaddr_in6 in6;
25
        struct sockaddr_un un;
26
        struct sockaddr_nl nl;
27
        struct sockaddr_ll ll;
28
        struct sockaddr_vm vm;
29

30
        /* Ensure there is enough space to store an arbitrary hardware address, e.g. Infiniband */
31
        uint8_t ll_buffer[offsetof(struct sockaddr_ll, sll_addr) + HW_ADDR_MAX_SIZE];
32

33
        /* Ensure there is enough space after the AF_UNIX sun_path for one more NUL byte, just to be sure that the path
34
         * component is always followed by at least one NUL byte. */
35
        uint8_t un_buffer[sizeof(struct sockaddr_un) + 1];
36
};
37

38
#define SUN_PATH_LEN (sizeof(((struct sockaddr_un){}).sun_path))
39

40
typedef struct SocketAddress {
41
        union sockaddr_union sockaddr;
42

43
        /* We store the size here explicitly due to the weird
44
         * sockaddr_un semantics for abstract sockets */
45
        socklen_t size;
46

47
        /* Socket type, i.e. SOCK_STREAM, SOCK_DGRAM, ... */
48
        int type;
49

50
        /* Socket protocol, IPPROTO_xxx, usually 0, except for netlink */
51
        int protocol;
52
} SocketAddress;
53

54
#define socket_address_family(a) ((a)->sockaddr.sa.sa_family)
55

56
DECLARE_STRING_TABLE_LOOKUP(socket_address_type, int);
57

58
int sockaddr_un_unlink(const struct sockaddr_un *sa);
59

60
static inline int socket_address_unlink(const SocketAddress *a) {
313✔
61
        return socket_address_family(a) == AF_UNIX ? sockaddr_un_unlink(&a->sockaddr.un) : 0;
313✔
62
}
63

64
bool socket_address_can_accept(const SocketAddress *a) _pure_;
65

66
int socket_address_verify(const SocketAddress *a, bool strict) _pure_;
67
int socket_address_print(const SocketAddress *a, char **ret);
68
bool socket_address_matches_fd(const SocketAddress *a, int fd);
69

70
bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) _pure_;
71

72
const char* socket_address_get_path(const SocketAddress *a);
73

74
bool socket_ipv6_is_supported(void);
75
bool socket_ipv6_is_enabled(void);
76

77
int sockaddr_port(const struct sockaddr *_sa, unsigned *port);
78
const union in_addr_union *sockaddr_in_addr(const struct sockaddr *sa);
79
int sockaddr_set_in_addr(union sockaddr_union *u, int family, const union in_addr_union *a, uint16_t port);
80

81
int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, bool include_port, char **ret);
82
int getpeername_pretty(int fd, bool include_port, char **ret);
83
int getsockname_pretty(int fd, char **ret);
84

85
int socknameinfo_pretty(const struct sockaddr *sa, socklen_t salen, char **_ret);
86

87
DECLARE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int);
88

89
bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b);
90

91
int fd_set_sndbuf(int fd, size_t n, bool increase);
92
static inline int fd_inc_sndbuf(int fd, size_t n) {
452,546✔
93
        return fd_set_sndbuf(fd, n, true);
452,546✔
94
}
95
int fd_set_rcvbuf(int fd, size_t n, bool increase);
96
static inline int fd_increase_rxbuf(int fd, size_t n) {
39,771✔
97
        return fd_set_rcvbuf(fd, n, true);
39,771✔
98
}
99

100
DECLARE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int);
101

102
typedef enum {
103
        IFNAME_VALID_ALTERNATIVE = 1 << 0, /* Allow "altnames" too */
104
        IFNAME_VALID_NUMERIC     = 1 << 1, /* Allow decimal formatted ifindexes too */
105
        IFNAME_VALID_SPECIAL     = 1 << 2, /* Allow the special names "all" and "default" */
106
        _IFNAME_VALID_ALL        = IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC | IFNAME_VALID_SPECIAL,
107
} IfnameValidFlags;
108
bool ifname_valid_char(char a) _const_;
109
bool ifname_valid_full(const char *p, IfnameValidFlags flags) _pure_;
110
static inline bool ifname_valid(const char *p) {
5,798✔
111
        return ifname_valid_full(p, 0);
5,798✔
112
}
113
bool address_label_valid(const char *p) _pure_;
114

115
int getpeercred(int fd, struct ucred *ucred);
116
int getpeersec(int fd, char **ret);
117
int getpeergroups(int fd, gid_t **ret);
118
int getpeerpidfd(int fd);
119
int getpeerpidref(int fd, PidRef *ret);
120

121
ssize_t send_one_fd_iov_sa(
122
                int transport_fd,
123
                int fd,
124
                const struct iovec *iov, size_t iovlen,
125
                const struct sockaddr *sa, socklen_t len,
126
                int flags);
127
int send_one_fd_sa(int transport_fd,
128
                   int fd,
129
                   const struct sockaddr *sa, socklen_t len,
130
                   int flags);
131
#define send_one_fd_iov(transport_fd, fd, iov, iovlen, flags) send_one_fd_iov_sa(transport_fd, fd, iov, iovlen, NULL, 0, flags)
132
#define send_one_fd(transport_fd, fd, flags) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, NULL, 0, flags)
133
ssize_t receive_one_fd_iov(int transport_fd, struct iovec *iov, size_t iovlen, int flags, int *ret_fd);
134
int receive_one_fd(int transport_fd, int flags);
135

136
ssize_t next_datagram_size_fd(int fd);
137

138
int flush_accept(int fd);
139
ssize_t flush_mqueue(int fd);
140

141
#define CMSG_FOREACH(cmsg, mh)                                          \
142
        for ((cmsg) = CMSG_FIRSTHDR(mh); (cmsg); (cmsg) = CMSG_NXTHDR((mh), (cmsg)))
143

144
/* Returns the cmsghdr's data pointer, but safely cast to the specified type. Does two alignment checks: one
145
 * at compile time, that the requested type has a smaller or same alignment as 'struct cmsghdr', and one
146
 * during runtime, that the actual pointer matches the alignment too. This is supposed to catch cases such as
147
 * 'struct timeval' is embedded into 'struct cmsghdr' on architectures where the alignment of the former is 8
148
 * bytes (because of a 64-bit time_t), but of the latter is 4 bytes (because size_t is 32 bits), such as
149
 * riscv32. */
150
#define CMSG_TYPED_DATA(cmsg, type)                                     \
151
        ({                                                              \
152
                struct cmsghdr *_cmsg = (cmsg);                         \
153
                assert_cc(alignof(type) <= alignof(struct cmsghdr));    \
154
                _cmsg ? CAST_ALIGN_PTR(type, CMSG_DATA(_cmsg)) : (type*) NULL; \
155
        })
156

157
struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length);
158
void* cmsg_find_and_copy_data(struct msghdr *mh, int level, int type, void *buf, size_t buf_len);
159

160
/* Type-safe, dereferencing version of cmsg_find() */
161
#define CMSG_FIND_DATA(mh, level, type, ctype)                          \
162
        CMSG_TYPED_DATA(cmsg_find(mh, level, type, CMSG_LEN(sizeof(ctype))), ctype)
163

164
/* Type-safe version of cmsg_find_and_copy_data() */
165
#define CMSG_FIND_AND_COPY_DATA(mh, level, type, ctype)             \
166
        (ctype*) cmsg_find_and_copy_data(mh, level, type, &(ctype){}, sizeof(ctype))
167

168
/* Resolves to a type that can carry cmsghdr structures. Make sure things are properly aligned, i.e. the type
169
 * itself is placed properly in memory and the size is also aligned to what's appropriate for "cmsghdr"
170
 * structures. */
171
#define CMSG_BUFFER_TYPE(size)                                          \
172
        union {                                                         \
173
                struct cmsghdr cmsghdr;                                 \
174
                uint8_t buf[size];                                      \
175
                uint8_t align_check[(size) >= CMSG_SPACE(0) &&          \
176
                                    (size) == CMSG_ALIGN(size) ? 1 : -1]; \
177
        }
178

179
size_t sockaddr_ll_len(const struct sockaddr_ll *sa);
180
size_t sockaddr_un_len(const struct sockaddr_un *sa);
181
size_t sockaddr_len(const union sockaddr_union *sa);
182

183
int socket_ioctl_fd(void);
184

185
int sockaddr_un_set_path(struct sockaddr_un *ret, const char *path);
186

187
static inline int setsockopt_int(int fd, int level, int optname, int value) {
1,180,326✔
188
        if (setsockopt(fd, level, optname, &value, sizeof(value)) < 0)
1,180,326✔
189
                return -errno;
39,356✔
190

191
        return 0;
192
}
193

194
int getsockopt_int(int fd, int level, int optname, int *ret);
195

196
int socket_bind_to_ifname(int fd, const char *ifname);
197
int socket_bind_to_ifindex(int fd, int ifindex);
198

199
int socket_autobind(int fd, char **ret_name);
200

201
/* glibc duplicates timespec/timeval on certain 32-bit arches, once in 32-bit and once in 64-bit.
202
 * See __convert_scm_timestamps() in glibc source code. Hence, we need additional buffer space for them
203
 * to prevent truncating control msg (recvmsg() MSG_CTRUNC). */
204
#define CMSG_SPACE_TIMEVAL                                              \
205
        (CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(2 * sizeof(uint64_t)))
206
#define CMSG_SPACE_TIMESPEC                                             \
207
        (CMSG_SPACE(sizeof(struct timespec)) + CMSG_SPACE(2 * sizeof(uint64_t)))
208

209
ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags);
210

211
int socket_get_family(int fd);
212
int socket_set_recvpktinfo(int fd, int af, bool b);
213
int socket_set_unicast_if(int fd, int af, int ifi);
214

215
int socket_set_option(int fd, int af, int opt_ipv4, int opt_ipv6, int val);
216
static inline int socket_set_recverr(int fd, int af, bool b) {
20,205✔
217
        return socket_set_option(fd, af, IP_RECVERR, IPV6_RECVERR, b);
20,205✔
218
}
219
static inline int socket_set_recvttl(int fd, int af, bool b) {
1,578✔
220
        return socket_set_option(fd, af, IP_RECVTTL, IPV6_RECVHOPLIMIT, b);
1,578✔
221
}
222
static inline int socket_set_ttl(int fd, int af, int ttl) {
1,161✔
223
        return socket_set_option(fd, af, IP_TTL, IPV6_UNICAST_HOPS, ttl);
1,161✔
224
}
225
static inline int socket_set_freebind(int fd, int af, bool b) {
6✔
226
        return socket_set_option(fd, af, IP_FREEBIND, IPV6_FREEBIND, b);
6✔
227
}
UNCOV
228
static inline int socket_set_transparent(int fd, int af, bool b) {
×
UNCOV
229
        return socket_set_option(fd, af, IP_TRANSPARENT, IPV6_TRANSPARENT, b);
×
230
}
231
static inline int socket_set_recvfragsize(int fd, int af, bool b) {
20,419✔
232
        return socket_set_option(fd, af, IP_RECVFRAGSIZE, IPV6_RECVFRAGSIZE, b);
20,419✔
233
}
234

235
int socket_get_mtu(int fd, int af, size_t *ret);
236

237
/* an initializer for struct ucred that initialized all fields to the invalid value appropriate for each */
238
#define UCRED_INVALID { .pid = 0, .uid = UID_INVALID, .gid = GID_INVALID }
239

240
int connect_unix_path(int fd, int dir_fd, const char *path);
241

242
static inline bool VSOCK_CID_IS_REGULAR(unsigned cid) {
64✔
243
        /* 0, 1, 2, UINT32_MAX are special, refuse those */
244
        return cid > 2 && cid < UINT32_MAX;
64✔
245
}
246

247
int vsock_parse_port(const char *s, unsigned *ret);
248
int vsock_parse_cid(const char *s, unsigned *ret);
249

250
/* Parses AF_UNIX and AF_VSOCK addresses. AF_INET[6] require some netlink calls, so it cannot be in
251
 * src/basic/ and is done from 'socket_local_address from src/shared/. Return -EPROTO in case of
252
 * protocol mismatch. */
253
int socket_address_parse_unix(SocketAddress *ret_address, const char *s);
254
int socket_address_parse_vsock(SocketAddress *ret_address, const char *s);
255
int socket_address_equal_unix(const char *a, const char *b);
256

257
/* libc's SOMAXCONN is defined to 128 or 4096 (at least on glibc). But actually, the value can be much
258
 * larger. In our codebase we want to set it to the max usually, since nowadays socket memory is properly
259
 * tracked by memcg, and hence we don't need to enforce extra limits here. Moreover, the kernel caps it to
260
 * /proc/sys/net/core/somaxconn anyway, thus by setting this to unbounded we just make that sysctl file
261
 * authoritative. */
262
#define SOMAXCONN_DELUXE INT_MAX
263

264
int vsock_get_local_cid(unsigned *ret);
265

266
int netlink_socket_get_multicast_groups(int fd, size_t *ret_len, uint32_t **ret_groups);
267

268
int socket_get_cookie(int fd, uint64_t *ret);
269

270
void cmsg_close_all(struct msghdr *mh);
271

272
int tos_to_priority(uint8_t tos);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc