]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/socket-util.h
tree-wide: use -EINVAL for enum invalid values
[thirdparty/systemd.git] / src / basic / socket-util.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c2f1db8f 2#pragma once
42f4e3c4 3
ef118d00
YW
4#include <inttypes.h>
5#include <linux/netlink.h>
8f815e8b 6#include <linux/if_ether.h>
ef118d00
YW
7#include <linux/if_infiniband.h>
8#include <linux/if_packet.h>
71d35b6b 9#include <netinet/in.h>
11c3a366
TA
10#include <stdbool.h>
11#include <stddef.h>
56fb30d9 12#include <string.h>
71d35b6b 13#include <sys/socket.h>
11c3a366 14#include <sys/types.h>
42f4e3c4
LP
15#include <sys/un.h>
16
17#include "macro.h"
402506ce 18#include "missing_network.h"
ef118d00
YW
19#include "missing_socket.h"
20#include "sparse-endian.h"
42f4e3c4 21
f6144808 22union sockaddr_union {
8b7f989a 23 /* The minimal, abstract version */
f6144808 24 struct sockaddr sa;
8b7f989a
LP
25
26 /* The libc provided version that allocates "enough room" for every protocol */
27 struct sockaddr_storage storage;
28
29 /* Protoctol-specific implementations */
4d49b48c 30 struct sockaddr_in in;
f6144808
LP
31 struct sockaddr_in6 in6;
32 struct sockaddr_un un;
7a22745a 33 struct sockaddr_nl nl;
e88bc795 34 struct sockaddr_ll ll;
0fc0f14b 35 struct sockaddr_vm vm;
8b7f989a 36
b1f24b75
BG
37 /* Ensure there is enough space to store Infiniband addresses */
38 uint8_t ll_buffer[offsetof(struct sockaddr_ll, sll_addr) + CONST_MAX(ETH_ALEN, INFINIBAND_ALEN)];
8b7f989a
LP
39
40 /* Ensure there is enough space after the AF_UNIX sun_path for one more NUL byte, just to be sure that the path
41 * component is always followed by at least one NUL byte. */
42 uint8_t un_buffer[sizeof(struct sockaddr_un) + 1];
f6144808
LP
43};
44
5c3fa98d
ZJS
45#define SUN_PATH_LEN (sizeof(((struct sockaddr_un){}).sun_path))
46
542563ba 47typedef struct SocketAddress {
f6144808 48 union sockaddr_union sockaddr;
42f4e3c4
LP
49
50 /* We store the size here explicitly due to the weird
51 * sockaddr_un semantics for abstract sockets */
52 socklen_t size;
53
54 /* Socket type, i.e. SOCK_STREAM, SOCK_DGRAM, ... */
55 int type;
7a22745a
LP
56
57 /* Socket protocol, IPPROTO_xxx, usually 0, except for netlink */
58 int protocol;
542563ba 59} SocketAddress;
42f4e3c4 60
542563ba
LP
61typedef enum SocketAddressBindIPv6Only {
62 SOCKET_ADDRESS_DEFAULT,
63 SOCKET_ADDRESS_BOTH,
c0120d99
LP
64 SOCKET_ADDRESS_IPV6_ONLY,
65 _SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX,
2d93c20e 66 _SOCKET_ADDRESS_BIND_IPV6_ONLY_INVALID = -EINVAL,
542563ba 67} SocketAddressBindIPv6Only;
42f4e3c4 68
542563ba 69#define socket_address_family(a) ((a)->sockaddr.sa.sa_family)
42f4e3c4 70
398ce0bc
YW
71const char* socket_address_type_to_string(int t) _const_;
72int socket_address_type_from_string(const char *s) _pure_;
73
9f20fc28
LP
74int sockaddr_un_unlink(const struct sockaddr_un *sa);
75
76static inline int socket_address_unlink(const SocketAddress *a) {
77 return socket_address_family(a) == AF_UNIX ? sockaddr_un_unlink(&a->sockaddr.un) : 0;
78}
b5a0699f 79
44a6b1b6 80bool socket_address_can_accept(const SocketAddress *a) _pure_;
4f2d528d 81
b5a0699f
LP
82int socket_address_listen(
83 const SocketAddress *a,
175a3d25 84 int flags,
b5a0699f
LP
85 int backlog,
86 SocketAddressBindIPv6Only only,
87 const char *bind_to_device,
54255c64 88 bool reuse_port,
4fd5948e 89 bool free_bind,
6b6d2dee 90 bool transparent,
b5a0699f
LP
91 mode_t directory_mode,
92 mode_t socket_mode,
175a3d25 93 const char *label);
a16e1123 94
5c3fa98d
ZJS
95int socket_address_verify(const SocketAddress *a, bool strict) _pure_;
96int socket_address_print(const SocketAddress *a, char **p);
01e10de3
LP
97bool socket_address_matches_fd(const SocketAddress *a, int fd);
98
44a6b1b6 99bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) _pure_;
a16e1123 100
a57f7e2c 101const char* socket_address_get_path(const SocketAddress *a);
6e2ef85b 102
4d49b48c
LP
103bool socket_ipv6_is_supported(void);
104
69dc6922 105int sockaddr_port(const struct sockaddr *_sa, unsigned *port);
31325971 106const union in_addr_union *sockaddr_in_addr(const struct sockaddr *sa);
3b1c5241
SL
107
108int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, bool include_port, char **ret);
366b7db4 109int getpeername_pretty(int fd, bool include_port, char **ret);
4d49b48c
LP
110int getsockname_pretty(int fd, char **ret);
111
b31f535c 112int socknameinfo_pretty(union sockaddr_union *sa, socklen_t salen, char **_ret);
b31f535c 113
44a6b1b6
ZJS
114const char* socket_address_bind_ipv6_only_to_string(SocketAddressBindIPv6Only b) _const_;
115SocketAddressBindIPv6Only socket_address_bind_ipv6_only_from_string(const char *s) _pure_;
b54e98ef 116SocketAddressBindIPv6Only socket_address_bind_ipv6_only_or_bool_from_string(const char *s);
c0120d99 117
f8b69d1d 118int netlink_family_to_string_alloc(int b, char **s);
4d49b48c 119int netlink_family_from_string(const char *s) _pure_;
f01e5736
LP
120
121bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b);
2583fbea 122
d9d9b2a0
YW
123int fd_set_sndbuf(int fd, size_t n, bool increase);
124static inline int fd_inc_sndbuf(int fd, size_t n) {
125 return fd_set_sndbuf(fd, n, true);
126}
127int fd_set_rcvbuf(int fd, size_t n, bool increase);
128static inline int fd_inc_rcvbuf(int fd, size_t n) {
129 return fd_set_rcvbuf(fd, n, true);
130}
2583fbea
LP
131
132int ip_tos_to_string_alloc(int i, char **s);
133int ip_tos_from_string(const char *s);
134
2313524a
ZJS
135typedef enum {
136 IFNAME_VALID_ALTERNATIVE = 1 << 0,
137 IFNAME_VALID_NUMERIC = 1 << 1,
138 _IFNAME_VALID_ALL = IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC,
139} IfnameValidFlags;
140bool ifname_valid_full(const char *p, IfnameValidFlags flags);
4252696a 141static inline bool ifname_valid(const char *p) {
2313524a 142 return ifname_valid_full(p, 0);
4252696a 143}
26808948 144bool address_label_valid(const char *p);
ef76dff2 145
2583fbea
LP
146int getpeercred(int fd, struct ucred *ucred);
147int getpeersec(int fd, char **ret);
43f2c88d 148int getpeergroups(int fd, gid_t **ret);
2583fbea 149
d34673ec
FB
150ssize_t send_one_fd_iov_sa(
151 int transport_fd,
152 int fd,
153 struct iovec *iov, size_t iovlen,
154 const struct sockaddr *sa, socklen_t len,
155 int flags);
726f4c47
ZJS
156int send_one_fd_sa(int transport_fd,
157 int fd,
158 const struct sockaddr *sa, socklen_t len,
159 int flags);
d34673ec
FB
160#define send_one_fd_iov(transport_fd, fd, iov, iovlen, flags) send_one_fd_iov_sa(transport_fd, fd, iov, iovlen, NULL, 0, flags)
161#define send_one_fd(transport_fd, fd, flags) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, NULL, 0, flags)
162ssize_t receive_one_fd_iov(int transport_fd, struct iovec *iov, size_t iovlen, int flags, int *ret_fd);
2583fbea 163int receive_one_fd(int transport_fd, int flags);
8f328d36 164
4edc2c9b
LP
165ssize_t next_datagram_size_fd(int fd);
166
60d9771c
LP
167int flush_accept(int fd);
168
8f328d36
LP
169#define CMSG_FOREACH(cmsg, mh) \
170 for ((cmsg) = CMSG_FIRSTHDR(mh); (cmsg); (cmsg) = CMSG_NXTHDR((mh), (cmsg)))
fc2fffe7 171
29206d46
LP
172struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length);
173
371d72e0
LP
174/* Type-safe, dereferencing version of cmsg_find() */
175#define CMSG_FIND_DATA(mh, level, type, ctype) \
176 ({ \
177 struct cmsghdr *_found; \
178 _found = cmsg_find(mh, level, type, CMSG_LEN(sizeof(ctype))); \
179 (ctype*) (_found ? CMSG_DATA(_found) : NULL); \
180 })
181
fb29cdbe
LP
182/* Resolves to a type that can carry cmsghdr structures. Make sure things are properly aligned, i.e. the type
183 * itself is placed properly in memory and the size is also aligned to what's appropriate for "cmsghdr"
184 * structures. */
185#define CMSG_BUFFER_TYPE(size) \
186 union { \
187 struct cmsghdr cmsghdr; \
188 uint8_t buf[size]; \
189 uint8_t align_check[(size) >= CMSG_SPACE(0) && \
190 (size) == CMSG_ALIGN(size) ? 1 : -1]; \
191 }
192
b1f24b75
BG
193/*
194 * Certain hardware address types (e.g Infiniband) do not fit into sll_addr
195 * (8 bytes) and run over the structure. This macro returns the correct size that
196 * must be passed to kernel.
197 */
198#define SOCKADDR_LL_LEN(sa) \
199 ({ \
200 const struct sockaddr_ll *_sa = &(sa); \
201 size_t _mac_len = sizeof(_sa->sll_addr); \
202 assert(_sa->sll_family == AF_PACKET); \
203 if (be16toh(_sa->sll_hatype) == ARPHRD_ETHER) \
204 _mac_len = MAX(_mac_len, (size_t) ETH_ALEN); \
205 if (be16toh(_sa->sll_hatype) == ARPHRD_INFINIBAND) \
206 _mac_len = MAX(_mac_len, (size_t) INFINIBAND_ALEN); \
207 offsetof(struct sockaddr_ll, sll_addr) + _mac_len; \
208 })
209
fc2fffe7
LP
210/* Covers only file system and abstract AF_UNIX socket addresses, but not unnamed socket addresses. */
211#define SOCKADDR_UN_LEN(sa) \
212 ({ \
213 const struct sockaddr_un *_sa = &(sa); \
214 assert(_sa->sun_family == AF_UNIX); \
215 offsetof(struct sockaddr_un, sun_path) + \
216 (_sa->sun_path[0] == 0 ? \
217 1 + strnlen(_sa->sun_path+1, sizeof(_sa->sun_path)-1) : \
69995bff 218 strnlen(_sa->sun_path, sizeof(_sa->sun_path))+1); \
fc2fffe7 219 })
429b4350 220
4e0a46f6
YW
221#define SOCKADDR_LEN(sa) \
222 ({ \
223 const union sockaddr_union *__sa = &(sa); \
224 size_t _len; \
225 switch(__sa->sa.sa_family) { \
226 case AF_INET: \
227 _len = sizeof(struct sockaddr_in); \
228 break; \
229 case AF_INET6: \
230 _len = sizeof(struct sockaddr_in6); \
231 break; \
232 case AF_UNIX: \
233 _len = SOCKADDR_UN_LEN(__sa->un); \
234 break; \
235 case AF_PACKET: \
236 _len = SOCKADDR_LL_LEN(__sa->ll); \
237 break; \
238 case AF_NETLINK: \
239 _len = sizeof(struct sockaddr_nl); \
240 break; \
241 case AF_VSOCK: \
242 _len = sizeof(struct sockaddr_vm); \
243 break; \
244 default: \
245 assert_not_reached("invalid socket family"); \
246 } \
247 _len; \
248 })
249
429b4350 250int socket_ioctl_fd(void);
5cf91ea9
LP
251
252int sockaddr_un_set_path(struct sockaddr_un *ret, const char *path);
2ff48e98
LP
253
254static inline int setsockopt_int(int fd, int level, int optname, int value) {
255 if (setsockopt(fd, level, optname, &value, sizeof(value)) < 0)
256 return -errno;
257
258 return 0;
259}
5d594d01 260
4e25d4cf
LP
261static inline int getsockopt_int(int fd, int level, int optname, int *ret) {
262 int v;
263 socklen_t sl = sizeof(v);
264
265 if (getsockopt(fd, level, optname, &v, &sl) < 0)
266 return -errno;
267 if (sl != sizeof(v))
268 return -EIO;
269
270 *ret = v;
271 return 0;
272}
273
5d594d01
LP
274int socket_bind_to_ifname(int fd, const char *ifname);
275int socket_bind_to_ifindex(int fd, int ifindex);
47eae6ce
LP
276
277ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags);
35a3eb9b 278
5d0fe423
LP
279int socket_get_family(int fd, int *ret);
280int socket_set_recvpktinfo(int fd, int af, bool b);
5d0fe423 281int socket_set_unicast_if(int fd, int af, int ifi);
00ed2fff 282
402506ce
YW
283int socket_set_option(int fd, int af, int opt_ipv4, int opt_ipv6, int val);
284static inline int socket_set_recverr(int fd, int af, bool b) {
285 return socket_set_option(fd, af, IP_RECVERR, IPV6_RECVERR, b);
286}
287static inline int socket_set_recvttl(int fd, int af, bool b) {
288 return socket_set_option(fd, af, IP_RECVTTL, IPV6_RECVHOPLIMIT, b);
289}
290static inline int socket_set_ttl(int fd, int af, int ttl) {
291 return socket_set_option(fd, af, IP_TTL, IPV6_UNICAST_HOPS, ttl);
292}
293static inline int socket_set_freebind(int fd, int af, bool b) {
294 return socket_set_option(fd, af, IP_FREEBIND, IPV6_FREEBIND, b);
295}
296static inline int socket_set_transparent(int fd, int af, bool b) {
297 return socket_set_option(fd, af, IP_TRANSPARENT, IPV6_TRANSPARENT, b);
298}
00ed2fff
LP
299static inline int socket_set_recvfragsize(int fd, int af, bool b) {
300 return socket_set_option(fd, af, IP_RECVFRAGSIZE, IPV6_RECVFRAGSIZE, b);
301}
52975f86
LP
302
303int socket_get_mtu(int fd, int af, size_t *ret);