]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/socket-util.h
Merge pull request #16981 from keszybz/use-crypt_ra
[thirdparty/systemd.git] / src / basic / socket-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include <inttypes.h>
5 #include <linux/netlink.h>
6 #include <linux/if_ether.h>
7 #include <linux/if_infiniband.h>
8 #include <linux/if_packet.h>
9 #include <netinet/in.h>
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <string.h>
13 #include <sys/socket.h>
14 #include <sys/types.h>
15 #include <sys/un.h>
16
17 #include "macro.h"
18 #include "missing_socket.h"
19 #include "sparse-endian.h"
20
21 union sockaddr_union {
22 /* The minimal, abstract version */
23 struct sockaddr sa;
24
25 /* The libc provided version that allocates "enough room" for every protocol */
26 struct sockaddr_storage storage;
27
28 /* Protoctol-specific implementations */
29 struct sockaddr_in in;
30 struct sockaddr_in6 in6;
31 struct sockaddr_un un;
32 struct sockaddr_nl nl;
33 struct sockaddr_ll ll;
34 struct sockaddr_vm vm;
35
36 /* Ensure there is enough space to store Infiniband addresses */
37 uint8_t ll_buffer[offsetof(struct sockaddr_ll, sll_addr) + CONST_MAX(ETH_ALEN, INFINIBAND_ALEN)];
38
39 /* Ensure there is enough space after the AF_UNIX sun_path for one more NUL byte, just to be sure that the path
40 * component is always followed by at least one NUL byte. */
41 uint8_t un_buffer[sizeof(struct sockaddr_un) + 1];
42 };
43
44 #define SUN_PATH_LEN (sizeof(((struct sockaddr_un){}).sun_path))
45
46 typedef struct SocketAddress {
47 union sockaddr_union sockaddr;
48
49 /* We store the size here explicitly due to the weird
50 * sockaddr_un semantics for abstract sockets */
51 socklen_t size;
52
53 /* Socket type, i.e. SOCK_STREAM, SOCK_DGRAM, ... */
54 int type;
55
56 /* Socket protocol, IPPROTO_xxx, usually 0, except for netlink */
57 int protocol;
58 } SocketAddress;
59
60 typedef enum SocketAddressBindIPv6Only {
61 SOCKET_ADDRESS_DEFAULT,
62 SOCKET_ADDRESS_BOTH,
63 SOCKET_ADDRESS_IPV6_ONLY,
64 _SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX,
65 _SOCKET_ADDRESS_BIND_IPV6_ONLY_INVALID = -1
66 } SocketAddressBindIPv6Only;
67
68 #define socket_address_family(a) ((a)->sockaddr.sa.sa_family)
69
70 const char* socket_address_type_to_string(int t) _const_;
71 int socket_address_type_from_string(const char *s) _pure_;
72
73 int sockaddr_un_unlink(const struct sockaddr_un *sa);
74
75 static inline int socket_address_unlink(const SocketAddress *a) {
76 return socket_address_family(a) == AF_UNIX ? sockaddr_un_unlink(&a->sockaddr.un) : 0;
77 }
78
79 bool socket_address_can_accept(const SocketAddress *a) _pure_;
80
81 int socket_address_listen(
82 const SocketAddress *a,
83 int flags,
84 int backlog,
85 SocketAddressBindIPv6Only only,
86 const char *bind_to_device,
87 bool reuse_port,
88 bool free_bind,
89 bool transparent,
90 mode_t directory_mode,
91 mode_t socket_mode,
92 const char *label);
93
94 int socket_address_verify(const SocketAddress *a, bool strict) _pure_;
95 int socket_address_print(const SocketAddress *a, char **p);
96 bool socket_address_matches_fd(const SocketAddress *a, int fd);
97
98 bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) _pure_;
99
100 const char* socket_address_get_path(const SocketAddress *a);
101
102 bool socket_ipv6_is_supported(void);
103
104 int sockaddr_port(const struct sockaddr *_sa, unsigned *port);
105
106 int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, bool include_port, char **ret);
107 int getpeername_pretty(int fd, bool include_port, char **ret);
108 int getsockname_pretty(int fd, char **ret);
109
110 int socknameinfo_pretty(union sockaddr_union *sa, socklen_t salen, char **_ret);
111
112 const char* socket_address_bind_ipv6_only_to_string(SocketAddressBindIPv6Only b) _const_;
113 SocketAddressBindIPv6Only socket_address_bind_ipv6_only_from_string(const char *s) _pure_;
114 SocketAddressBindIPv6Only socket_address_bind_ipv6_only_or_bool_from_string(const char *s);
115
116 int netlink_family_to_string_alloc(int b, char **s);
117 int netlink_family_from_string(const char *s) _pure_;
118
119 bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b);
120
121 int fd_set_sndbuf(int fd, size_t n, bool increase);
122 static inline int fd_inc_sndbuf(int fd, size_t n) {
123 return fd_set_sndbuf(fd, n, true);
124 }
125 int fd_set_rcvbuf(int fd, size_t n, bool increase);
126 static inline int fd_inc_rcvbuf(int fd, size_t n) {
127 return fd_set_rcvbuf(fd, n, true);
128 }
129
130 int ip_tos_to_string_alloc(int i, char **s);
131 int ip_tos_from_string(const char *s);
132
133 typedef enum {
134 IFNAME_VALID_ALTERNATIVE = 1 << 0,
135 IFNAME_VALID_NUMERIC = 1 << 1,
136 _IFNAME_VALID_ALL = IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC,
137 } IfnameValidFlags;
138 bool ifname_valid_full(const char *p, IfnameValidFlags flags);
139 static inline bool ifname_valid(const char *p) {
140 return ifname_valid_full(p, 0);
141 }
142 bool address_label_valid(const char *p);
143
144 int getpeercred(int fd, struct ucred *ucred);
145 int getpeersec(int fd, char **ret);
146 int getpeergroups(int fd, gid_t **ret);
147
148 ssize_t send_one_fd_iov_sa(
149 int transport_fd,
150 int fd,
151 struct iovec *iov, size_t iovlen,
152 const struct sockaddr *sa, socklen_t len,
153 int flags);
154 int send_one_fd_sa(int transport_fd,
155 int fd,
156 const struct sockaddr *sa, socklen_t len,
157 int flags);
158 #define send_one_fd_iov(transport_fd, fd, iov, iovlen, flags) send_one_fd_iov_sa(transport_fd, fd, iov, iovlen, NULL, 0, flags)
159 #define send_one_fd(transport_fd, fd, flags) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, NULL, 0, flags)
160 ssize_t receive_one_fd_iov(int transport_fd, struct iovec *iov, size_t iovlen, int flags, int *ret_fd);
161 int receive_one_fd(int transport_fd, int flags);
162
163 ssize_t next_datagram_size_fd(int fd);
164
165 int flush_accept(int fd);
166
167 #define CMSG_FOREACH(cmsg, mh) \
168 for ((cmsg) = CMSG_FIRSTHDR(mh); (cmsg); (cmsg) = CMSG_NXTHDR((mh), (cmsg)))
169
170 struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length);
171
172 /* Type-safe, dereferencing version of cmsg_find() */
173 #define CMSG_FIND_DATA(mh, level, type, ctype) \
174 ({ \
175 struct cmsghdr *_found; \
176 _found = cmsg_find(mh, level, type, CMSG_LEN(sizeof(ctype))); \
177 (ctype*) (_found ? CMSG_DATA(_found) : NULL); \
178 })
179
180 /* Resolves to a type that can carry cmsghdr structures. Make sure things are properly aligned, i.e. the type
181 * itself is placed properly in memory and the size is also aligned to what's appropriate for "cmsghdr"
182 * structures. */
183 #define CMSG_BUFFER_TYPE(size) \
184 union { \
185 struct cmsghdr cmsghdr; \
186 uint8_t buf[size]; \
187 uint8_t align_check[(size) >= CMSG_SPACE(0) && \
188 (size) == CMSG_ALIGN(size) ? 1 : -1]; \
189 }
190
191 /*
192 * Certain hardware address types (e.g Infiniband) do not fit into sll_addr
193 * (8 bytes) and run over the structure. This macro returns the correct size that
194 * must be passed to kernel.
195 */
196 #define SOCKADDR_LL_LEN(sa) \
197 ({ \
198 const struct sockaddr_ll *_sa = &(sa); \
199 size_t _mac_len = sizeof(_sa->sll_addr); \
200 assert(_sa->sll_family == AF_PACKET); \
201 if (be16toh(_sa->sll_hatype) == ARPHRD_ETHER) \
202 _mac_len = MAX(_mac_len, (size_t) ETH_ALEN); \
203 if (be16toh(_sa->sll_hatype) == ARPHRD_INFINIBAND) \
204 _mac_len = MAX(_mac_len, (size_t) INFINIBAND_ALEN); \
205 offsetof(struct sockaddr_ll, sll_addr) + _mac_len; \
206 })
207
208 /* Covers only file system and abstract AF_UNIX socket addresses, but not unnamed socket addresses. */
209 #define SOCKADDR_UN_LEN(sa) \
210 ({ \
211 const struct sockaddr_un *_sa = &(sa); \
212 assert(_sa->sun_family == AF_UNIX); \
213 offsetof(struct sockaddr_un, sun_path) + \
214 (_sa->sun_path[0] == 0 ? \
215 1 + strnlen(_sa->sun_path+1, sizeof(_sa->sun_path)-1) : \
216 strnlen(_sa->sun_path, sizeof(_sa->sun_path))+1); \
217 })
218
219 #define SOCKADDR_LEN(sa) \
220 ({ \
221 const union sockaddr_union *__sa = &(sa); \
222 size_t _len; \
223 switch(__sa->sa.sa_family) { \
224 case AF_INET: \
225 _len = sizeof(struct sockaddr_in); \
226 break; \
227 case AF_INET6: \
228 _len = sizeof(struct sockaddr_in6); \
229 break; \
230 case AF_UNIX: \
231 _len = SOCKADDR_UN_LEN(__sa->un); \
232 break; \
233 case AF_PACKET: \
234 _len = SOCKADDR_LL_LEN(__sa->ll); \
235 break; \
236 case AF_NETLINK: \
237 _len = sizeof(struct sockaddr_nl); \
238 break; \
239 case AF_VSOCK: \
240 _len = sizeof(struct sockaddr_vm); \
241 break; \
242 default: \
243 assert_not_reached("invalid socket family"); \
244 } \
245 _len; \
246 })
247
248 int socket_ioctl_fd(void);
249
250 int sockaddr_un_set_path(struct sockaddr_un *ret, const char *path);
251
252 static inline int setsockopt_int(int fd, int level, int optname, int value) {
253 if (setsockopt(fd, level, optname, &value, sizeof(value)) < 0)
254 return -errno;
255
256 return 0;
257 }
258
259 int socket_bind_to_ifname(int fd, const char *ifname);
260 int socket_bind_to_ifindex(int fd, int ifindex);
261
262 ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags);
263
264 int socket_get_family(int fd, int *ret);
265 int socket_set_recvpktinfo(int fd, int af, bool b);
266 int socket_set_recverr(int fd, int af, bool b);
267 int socket_set_recvttl(int fd, int af, bool b);
268 int socket_set_ttl(int fd, int af, int ttl);
269 int socket_set_unicast_if(int fd, int af, int ifi);
270 int socket_set_freebind(int fd, int af, bool b);
271 int socket_set_transparent(int fd, int af, bool b);