1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
11 #include <netinet/ip.h>
20 #include "alloc-util.h"
23 #include "format-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 #include "socket-util.h"
31 #include "string-table.h"
32 #include "string-util.h"
34 #include "user-util.h"
39 # define IDN_FLAGS NI_IDN
44 static const char* const socket_address_type_table
[] = {
45 [SOCK_STREAM
] = "Stream",
46 [SOCK_DGRAM
] = "Datagram",
48 [SOCK_RDM
] = "ReliableDatagram",
49 [SOCK_SEQPACKET
] = "SequentialPacket",
50 [SOCK_DCCP
] = "DatagramCongestionControl",
53 DEFINE_STRING_TABLE_LOOKUP(socket_address_type
, int);
55 int socket_address_parse(SocketAddress
*a
, const char *s
) {
63 a
->type
= SOCK_STREAM
;
68 /* IPv6 in [x:.....:z]:p notation */
74 n
= strndupa(s
+1, e
-s
-1);
77 if (inet_pton(AF_INET6
, n
, &a
->sockaddr
.in6
.sin6_addr
) <= 0)
78 return errno
> 0 ? -errno
: -EINVAL
;
85 r
= parse_ip_port(e
, &port
);
89 a
->sockaddr
.in6
.sin6_family
= AF_INET6
;
90 a
->sockaddr
.in6
.sin6_port
= htobe16(port
);
91 a
->size
= sizeof(struct sockaddr_in6
);
93 } else if (*s
== '/') {
99 if (l
>= sizeof(a
->sockaddr
.un
.sun_path
))
102 a
->sockaddr
.un
.sun_family
= AF_UNIX
;
103 memcpy(a
->sockaddr
.un
.sun_path
, s
, l
);
104 a
->size
= offsetof(struct sockaddr_un
, sun_path
) + l
+ 1;
106 } else if (*s
== '@') {
107 /* Abstract AF_UNIX socket */
111 if (l
>= sizeof(a
->sockaddr
.un
.sun_path
) - 1)
114 a
->sockaddr
.un
.sun_family
= AF_UNIX
;
115 memcpy(a
->sockaddr
.un
.sun_path
+1, s
+1, l
);
116 a
->size
= offsetof(struct sockaddr_un
, sun_path
) + 1 + l
;
118 } else if (startswith(s
, "vsock:")) {
119 /* AF_VSOCK socket in vsock:cid:port notation */
120 const char *cid_start
= s
+ STRLEN("vsock:");
123 e
= strchr(cid_start
, ':');
127 r
= safe_atou(e
+1, &port
);
131 n
= strndupa(cid_start
, e
- cid_start
);
133 r
= safe_atou(n
, &a
->sockaddr
.vm
.svm_cid
);
137 a
->sockaddr
.vm
.svm_cid
= VMADDR_CID_ANY
;
139 a
->sockaddr
.vm
.svm_family
= AF_VSOCK
;
140 a
->sockaddr
.vm
.svm_port
= port
;
141 a
->size
= sizeof(struct sockaddr_vm
);
148 r
= parse_ip_port(e
+ 1, &port
);
152 n
= strndupa(s
, e
-s
);
154 /* IPv4 in w.x.y.z:p notation? */
155 r
= inet_pton(AF_INET
, n
, &a
->sockaddr
.in
.sin_addr
);
160 /* Gotcha, it's a traditional IPv4 address */
161 a
->sockaddr
.in
.sin_family
= AF_INET
;
162 a
->sockaddr
.in
.sin_port
= htobe16(port
);
163 a
->size
= sizeof(struct sockaddr_in
);
167 if (strlen(n
) > IF_NAMESIZE
-1)
170 /* Uh, our last resort, an interface name */
171 idx
= if_nametoindex(n
);
175 a
->sockaddr
.in6
.sin6_family
= AF_INET6
;
176 a
->sockaddr
.in6
.sin6_port
= htobe16(port
);
177 a
->sockaddr
.in6
.sin6_scope_id
= idx
;
178 a
->sockaddr
.in6
.sin6_addr
= in6addr_any
;
179 a
->size
= sizeof(struct sockaddr_in6
);
184 r
= parse_ip_port(s
, &port
);
188 if (socket_ipv6_is_supported()) {
189 a
->sockaddr
.in6
.sin6_family
= AF_INET6
;
190 a
->sockaddr
.in6
.sin6_port
= htobe16(port
);
191 a
->sockaddr
.in6
.sin6_addr
= in6addr_any
;
192 a
->size
= sizeof(struct sockaddr_in6
);
194 a
->sockaddr
.in
.sin_family
= AF_INET
;
195 a
->sockaddr
.in
.sin_port
= htobe16(port
);
196 a
->sockaddr
.in
.sin_addr
.s_addr
= INADDR_ANY
;
197 a
->size
= sizeof(struct sockaddr_in
);
205 int socket_address_parse_and_warn(SocketAddress
*a
, const char *s
) {
209 /* Similar to socket_address_parse() but warns for IPv6 sockets when we don't support them. */
211 r
= socket_address_parse(&b
, s
);
215 if (!socket_ipv6_is_supported() && b
.sockaddr
.sa
.sa_family
== AF_INET6
) {
216 log_warning("Binding to IPv6 address not available since kernel does not support IPv6.");
217 return -EAFNOSUPPORT
;
224 int socket_address_parse_netlink(SocketAddress
*a
, const char *s
) {
227 _cleanup_free_
char *sfamily
= NULL
;
235 if (sscanf(s
, "%ms %u", &sfamily
, &group
) < 1)
236 return errno
> 0 ? -errno
: -EINVAL
;
238 family
= netlink_family_from_string(sfamily
);
242 a
->sockaddr
.nl
.nl_family
= AF_NETLINK
;
243 a
->sockaddr
.nl
.nl_groups
= group
;
246 a
->size
= sizeof(struct sockaddr_nl
);
247 a
->protocol
= family
;
252 int socket_address_verify(const SocketAddress
*a
) {
255 switch (socket_address_family(a
)) {
258 if (a
->size
!= sizeof(struct sockaddr_in
))
261 if (a
->sockaddr
.in
.sin_port
== 0)
264 if (!IN_SET(a
->type
, SOCK_STREAM
, SOCK_DGRAM
))
270 if (a
->size
!= sizeof(struct sockaddr_in6
))
273 if (a
->sockaddr
.in6
.sin6_port
== 0)
276 if (!IN_SET(a
->type
, SOCK_STREAM
, SOCK_DGRAM
))
282 if (a
->size
< offsetof(struct sockaddr_un
, sun_path
))
285 if (a
->size
> offsetof(struct sockaddr_un
, sun_path
)) {
287 if (a
->sockaddr
.un
.sun_path
[0] != 0) {
291 e
= memchr(a
->sockaddr
.un
.sun_path
, 0, sizeof(a
->sockaddr
.un
.sun_path
));
295 if (a
->size
!= offsetof(struct sockaddr_un
, sun_path
) + (e
- a
->sockaddr
.un
.sun_path
) + 1)
300 if (!IN_SET(a
->type
, SOCK_STREAM
, SOCK_DGRAM
, SOCK_SEQPACKET
))
307 if (a
->size
!= sizeof(struct sockaddr_nl
))
310 if (!IN_SET(a
->type
, SOCK_RAW
, SOCK_DGRAM
))
316 if (a
->size
!= sizeof(struct sockaddr_vm
))
319 if (!IN_SET(a
->type
, SOCK_STREAM
, SOCK_DGRAM
))
325 return -EAFNOSUPPORT
;
329 int socket_address_print(const SocketAddress
*a
, char **ret
) {
335 r
= socket_address_verify(a
);
339 if (socket_address_family(a
) == AF_NETLINK
) {
340 _cleanup_free_
char *sfamily
= NULL
;
342 r
= netlink_family_to_string_alloc(a
->protocol
, &sfamily
);
346 r
= asprintf(ret
, "%s %u", sfamily
, a
->sockaddr
.nl
.nl_groups
);
353 return sockaddr_pretty(&a
->sockaddr
.sa
, a
->size
, false, true, ret
);
356 bool socket_address_can_accept(const SocketAddress
*a
) {
360 IN_SET(a
->type
, SOCK_STREAM
, SOCK_SEQPACKET
);
363 bool socket_address_equal(const SocketAddress
*a
, const SocketAddress
*b
) {
367 /* Invalid addresses are unequal to all */
368 if (socket_address_verify(a
) < 0 ||
369 socket_address_verify(b
) < 0)
372 if (a
->type
!= b
->type
)
375 if (socket_address_family(a
) != socket_address_family(b
))
378 switch (socket_address_family(a
)) {
381 if (a
->sockaddr
.in
.sin_addr
.s_addr
!= b
->sockaddr
.in
.sin_addr
.s_addr
)
384 if (a
->sockaddr
.in
.sin_port
!= b
->sockaddr
.in
.sin_port
)
390 if (memcmp(&a
->sockaddr
.in6
.sin6_addr
, &b
->sockaddr
.in6
.sin6_addr
, sizeof(a
->sockaddr
.in6
.sin6_addr
)) != 0)
393 if (a
->sockaddr
.in6
.sin6_port
!= b
->sockaddr
.in6
.sin6_port
)
399 if (a
->size
<= offsetof(struct sockaddr_un
, sun_path
) ||
400 b
->size
<= offsetof(struct sockaddr_un
, sun_path
))
403 if ((a
->sockaddr
.un
.sun_path
[0] == 0) != (b
->sockaddr
.un
.sun_path
[0] == 0))
406 if (a
->sockaddr
.un
.sun_path
[0]) {
407 if (!path_equal_or_files_same(a
->sockaddr
.un
.sun_path
, b
->sockaddr
.un
.sun_path
, 0))
410 if (a
->size
!= b
->size
)
413 if (memcmp(a
->sockaddr
.un
.sun_path
, b
->sockaddr
.un
.sun_path
, a
->size
) != 0)
420 if (a
->protocol
!= b
->protocol
)
423 if (a
->sockaddr
.nl
.nl_groups
!= b
->sockaddr
.nl
.nl_groups
)
429 if (a
->sockaddr
.vm
.svm_cid
!= b
->sockaddr
.vm
.svm_cid
)
432 if (a
->sockaddr
.vm
.svm_port
!= b
->sockaddr
.vm
.svm_port
)
438 /* Cannot compare, so we assume the addresses are different */
445 bool socket_address_is(const SocketAddress
*a
, const char *s
, int type
) {
446 struct SocketAddress b
;
451 if (socket_address_parse(&b
, s
) < 0)
456 return socket_address_equal(a
, &b
);
459 bool socket_address_is_netlink(const SocketAddress
*a
, const char *s
) {
460 struct SocketAddress b
;
465 if (socket_address_parse_netlink(&b
, s
) < 0)
468 return socket_address_equal(a
, &b
);
471 const char* socket_address_get_path(const SocketAddress
*a
) {
474 if (socket_address_family(a
) != AF_UNIX
)
477 if (a
->sockaddr
.un
.sun_path
[0] == 0)
480 return a
->sockaddr
.un
.sun_path
;
483 bool socket_ipv6_is_supported(void) {
484 if (access("/proc/net/if_inet6", F_OK
) != 0)
490 bool socket_address_matches_fd(const SocketAddress
*a
, int fd
) {
497 b
.size
= sizeof(b
.sockaddr
);
498 if (getsockname(fd
, &b
.sockaddr
.sa
, &b
.size
) < 0)
501 if (b
.sockaddr
.sa
.sa_family
!= a
->sockaddr
.sa
.sa_family
)
504 solen
= sizeof(b
.type
);
505 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &b
.type
, &solen
) < 0)
508 if (b
.type
!= a
->type
)
511 if (a
->protocol
!= 0) {
512 solen
= sizeof(b
.protocol
);
513 if (getsockopt(fd
, SOL_SOCKET
, SO_PROTOCOL
, &b
.protocol
, &solen
) < 0)
516 if (b
.protocol
!= a
->protocol
)
520 return socket_address_equal(a
, &b
);
523 int sockaddr_port(const struct sockaddr
*_sa
, unsigned *ret_port
) {
524 union sockaddr_union
*sa
= (union sockaddr_union
*) _sa
;
526 /* Note, this returns the port as 'unsigned' rather than 'uint16_t', as AF_VSOCK knows larger ports */
530 switch (sa
->sa
.sa_family
) {
533 *ret_port
= be16toh(sa
->in
.sin_port
);
537 *ret_port
= be16toh(sa
->in6
.sin6_port
);
541 *ret_port
= sa
->vm
.svm_port
;
545 return -EAFNOSUPPORT
;
549 int sockaddr_pretty(const struct sockaddr
*_sa
, socklen_t salen
, bool translate_ipv6
, bool include_port
, char **ret
) {
550 union sockaddr_union
*sa
= (union sockaddr_union
*) _sa
;
555 assert(salen
>= sizeof(sa
->sa
.sa_family
));
557 switch (sa
->sa
.sa_family
) {
562 a
= be32toh(sa
->in
.sin_addr
.s_addr
);
567 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF,
568 be16toh(sa
->in
.sin_port
));
572 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF);
579 static const unsigned char ipv4_prefix
[] = {
580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
583 if (translate_ipv6
&&
584 memcmp(&sa
->in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0) {
585 const uint8_t *a
= sa
->in6
.sin6_addr
.s6_addr
+12;
589 a
[0], a
[1], a
[2], a
[3],
590 be16toh(sa
->in6
.sin6_port
));
594 a
[0], a
[1], a
[2], a
[3]);
598 char a
[INET6_ADDRSTRLEN
];
600 inet_ntop(AF_INET6
, &sa
->in6
.sin6_addr
, a
, sizeof(a
));
606 be16toh(sa
->in6
.sin6_port
));
620 if (salen
<= offsetof(struct sockaddr_un
, sun_path
)) {
621 p
= strdup("<unnamed>");
625 } else if (sa
->un
.sun_path
[0] == 0) {
628 /* FIXME: We assume we can print the
629 * socket path here and that it hasn't
630 * more than one NUL byte. That is
631 * actually an invalid assumption */
633 p
= new(char, sizeof(sa
->un
.sun_path
)+1);
638 memcpy(p
+1, sa
->un
.sun_path
+1, sizeof(sa
->un
.sun_path
)-1);
639 p
[sizeof(sa
->un
.sun_path
)] = 0;
642 p
= strndup(sa
->un
.sun_path
, sizeof(sa
->un
.sun_path
));
656 r
= asprintf(&p
, "vsock:%u", sa
->vm
.svm_cid
);
669 int getpeername_pretty(int fd
, bool include_port
, char **ret
) {
670 union sockaddr_union sa
;
671 socklen_t salen
= sizeof(sa
);
677 if (getpeername(fd
, &sa
.sa
, &salen
) < 0)
680 if (sa
.sa
.sa_family
== AF_UNIX
) {
681 struct ucred ucred
= {};
683 /* UNIX connection sockets are anonymous, so let's use
684 * PID/UID as pretty credentials instead */
686 r
= getpeercred(fd
, &ucred
);
690 if (asprintf(ret
, "PID "PID_FMT
"/UID "UID_FMT
, ucred
.pid
, ucred
.uid
) < 0)
696 /* For remote sockets we translate IPv6 addresses back to IPv4
697 * if applicable, since that's nicer. */
699 return sockaddr_pretty(&sa
.sa
, salen
, true, include_port
, ret
);
702 int getsockname_pretty(int fd
, char **ret
) {
703 union sockaddr_union sa
;
704 socklen_t salen
= sizeof(sa
);
709 if (getsockname(fd
, &sa
.sa
, &salen
) < 0)
712 /* For local sockets we do not translate IPv6 addresses back
713 * to IPv6 if applicable, since this is usually used for
714 * listening sockets where the difference between IPv4 and
717 return sockaddr_pretty(&sa
.sa
, salen
, false, true, ret
);
720 int socknameinfo_pretty(union sockaddr_union
*sa
, socklen_t salen
, char **_ret
) {
722 char host
[NI_MAXHOST
], *ret
;
726 r
= getnameinfo(&sa
->sa
, salen
, host
, sizeof(host
), NULL
, 0, IDN_FLAGS
);
728 int saved_errno
= errno
;
730 r
= sockaddr_pretty(&sa
->sa
, salen
, true, true, &ret
);
734 log_debug_errno(saved_errno
, "getnameinfo(%s) failed: %m", ret
);
745 int socket_address_unlink(SocketAddress
*a
) {
748 if (socket_address_family(a
) != AF_UNIX
)
751 if (a
->sockaddr
.un
.sun_path
[0] == 0)
754 if (unlink(a
->sockaddr
.un
.sun_path
) < 0)
760 static const char* const netlink_family_table
[] = {
761 [NETLINK_ROUTE
] = "route",
762 [NETLINK_FIREWALL
] = "firewall",
763 [NETLINK_INET_DIAG
] = "inet-diag",
764 [NETLINK_NFLOG
] = "nflog",
765 [NETLINK_XFRM
] = "xfrm",
766 [NETLINK_SELINUX
] = "selinux",
767 [NETLINK_ISCSI
] = "iscsi",
768 [NETLINK_AUDIT
] = "audit",
769 [NETLINK_FIB_LOOKUP
] = "fib-lookup",
770 [NETLINK_CONNECTOR
] = "connector",
771 [NETLINK_NETFILTER
] = "netfilter",
772 [NETLINK_IP6_FW
] = "ip6-fw",
773 [NETLINK_DNRTMSG
] = "dnrtmsg",
774 [NETLINK_KOBJECT_UEVENT
] = "kobject-uevent",
775 [NETLINK_GENERIC
] = "generic",
776 [NETLINK_SCSITRANSPORT
] = "scsitransport",
777 [NETLINK_ECRYPTFS
] = "ecryptfs",
778 [NETLINK_RDMA
] = "rdma",
781 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family
, int, INT_MAX
);
783 static const char* const socket_address_bind_ipv6_only_table
[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX
] = {
784 [SOCKET_ADDRESS_DEFAULT
] = "default",
785 [SOCKET_ADDRESS_BOTH
] = "both",
786 [SOCKET_ADDRESS_IPV6_ONLY
] = "ipv6-only"
789 DEFINE_STRING_TABLE_LOOKUP(socket_address_bind_ipv6_only
, SocketAddressBindIPv6Only
);
791 SocketAddressBindIPv6Only
socket_address_bind_ipv6_only_or_bool_from_string(const char *n
) {
794 r
= parse_boolean(n
);
796 return SOCKET_ADDRESS_IPV6_ONLY
;
798 return SOCKET_ADDRESS_BOTH
;
800 return socket_address_bind_ipv6_only_from_string(n
);
803 bool sockaddr_equal(const union sockaddr_union
*a
, const union sockaddr_union
*b
) {
807 if (a
->sa
.sa_family
!= b
->sa
.sa_family
)
810 if (a
->sa
.sa_family
== AF_INET
)
811 return a
->in
.sin_addr
.s_addr
== b
->in
.sin_addr
.s_addr
;
813 if (a
->sa
.sa_family
== AF_INET6
)
814 return memcmp(&a
->in6
.sin6_addr
, &b
->in6
.sin6_addr
, sizeof(a
->in6
.sin6_addr
)) == 0;
816 if (a
->sa
.sa_family
== AF_VSOCK
)
817 return a
->vm
.svm_cid
== b
->vm
.svm_cid
;
822 int fd_inc_sndbuf(int fd
, size_t n
) {
824 socklen_t l
= sizeof(value
);
826 r
= getsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &l
);
827 if (r
>= 0 && l
== sizeof(value
) && (size_t) value
>= n
*2)
830 /* If we have the privileges we will ignore the kernel limit. */
833 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUFFORCE
, &value
, sizeof(value
)) < 0)
834 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &value
, sizeof(value
)) < 0)
840 int fd_inc_rcvbuf(int fd
, size_t n
) {
842 socklen_t l
= sizeof(value
);
844 r
= getsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &l
);
845 if (r
>= 0 && l
== sizeof(value
) && (size_t) value
>= n
*2)
848 /* If we have the privileges we will ignore the kernel limit. */
851 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUFFORCE
, &value
, sizeof(value
)) < 0)
852 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &value
, sizeof(value
)) < 0)
857 static const char* const ip_tos_table
[] = {
858 [IPTOS_LOWDELAY
] = "low-delay",
859 [IPTOS_THROUGHPUT
] = "throughput",
860 [IPTOS_RELIABILITY
] = "reliability",
861 [IPTOS_LOWCOST
] = "low-cost",
864 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos
, int, 0xff);
866 bool ifname_valid(const char *p
) {
869 /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources
870 * but slightly stricter, as we only allow non-control, non-space ASCII characters in the interface name. We
871 * also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */
876 if (strlen(p
) >= IFNAMSIZ
)
879 if (dot_or_dot_dot(p
))
883 if ((unsigned char) *p
>= 127U)
886 if ((unsigned char) *p
<= 32U)
889 if (IN_SET(*p
, ':', '/'))
892 numeric
= numeric
&& (*p
>= '0' && *p
<= '9');
902 bool address_label_valid(const char *p
) {
907 if (strlen(p
) >= IFNAMSIZ
)
911 if ((uint8_t) *p
>= 127U)
914 if ((uint8_t) *p
<= 31U)
922 int getpeercred(int fd
, struct ucred
*ucred
) {
923 socklen_t n
= sizeof(struct ucred
);
930 r
= getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &u
, &n
);
934 if (n
!= sizeof(struct ucred
))
937 /* Check if the data is actually useful and not suppressed due to namespacing issues */
938 if (!pid_is_valid(u
.pid
))
941 /* Note that we don't check UID/GID here, as namespace translation works differently there: instead of
942 * receiving in "invalid" user/group we get the overflow UID/GID. */
948 int getpeersec(int fd
, char **ret
) {
949 _cleanup_free_
char *s
= NULL
;
960 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERSEC
, s
, &n
) >= 0)
977 int getpeergroups(int fd
, gid_t
**ret
) {
978 socklen_t n
= sizeof(gid_t
) * 64;
979 _cleanup_free_ gid_t
*d
= NULL
;
989 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERGROUPS
, d
, &n
) >= 0)
998 assert_se(n
% sizeof(gid_t
) == 0);
1001 if ((socklen_t
) (int) n
!= n
)
1012 const struct sockaddr
*sa
, socklen_t len
,
1016 struct cmsghdr cmsghdr
;
1017 uint8_t buf
[CMSG_SPACE(sizeof(int))];
1019 struct msghdr mh
= {
1020 .msg_name
= (struct sockaddr
*) sa
,
1022 .msg_control
= &control
,
1023 .msg_controllen
= sizeof(control
),
1025 struct cmsghdr
*cmsg
;
1027 assert(transport_fd
>= 0);
1030 cmsg
= CMSG_FIRSTHDR(&mh
);
1031 cmsg
->cmsg_level
= SOL_SOCKET
;
1032 cmsg
->cmsg_type
= SCM_RIGHTS
;
1033 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
1034 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(int));
1036 mh
.msg_controllen
= CMSG_SPACE(sizeof(int));
1037 if (sendmsg(transport_fd
, &mh
, MSG_NOSIGNAL
| flags
) < 0)
1043 int receive_one_fd(int transport_fd
, int flags
) {
1045 struct cmsghdr cmsghdr
;
1046 uint8_t buf
[CMSG_SPACE(sizeof(int))];
1048 struct msghdr mh
= {
1049 .msg_control
= &control
,
1050 .msg_controllen
= sizeof(control
),
1052 struct cmsghdr
*cmsg
, *found
= NULL
;
1054 assert(transport_fd
>= 0);
1057 * Receive a single FD via @transport_fd. We don't care for
1058 * the transport-type. We retrieve a single FD at most, so for
1059 * packet-based transports, the caller must ensure to send
1060 * only a single FD per packet. This is best used in
1061 * combination with send_one_fd().
1064 if (recvmsg(transport_fd
, &mh
, MSG_NOSIGNAL
| MSG_CMSG_CLOEXEC
| flags
) < 0)
1067 CMSG_FOREACH(cmsg
, &mh
) {
1068 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1069 cmsg
->cmsg_type
== SCM_RIGHTS
&&
1070 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
1078 cmsg_close_all(&mh
);
1082 return *(int*) CMSG_DATA(found
);
1085 ssize_t
next_datagram_size_fd(int fd
) {
1089 /* This is a bit like FIONREAD/SIOCINQ, however a bit more powerful. The difference being: recv(MSG_PEEK) will
1090 * actually cause the next datagram in the queue to be validated regarding checksums, which FIONREAD doesn't
1091 * do. This difference is actually of major importance as we need to be sure that the size returned here
1092 * actually matches what we will read with recvmsg() next, as otherwise we might end up allocating a buffer of
1093 * the wrong size. */
1095 l
= recv(fd
, NULL
, 0, MSG_PEEK
|MSG_TRUNC
);
1097 if (IN_SET(errno
, EOPNOTSUPP
, EFAULT
))
1110 /* Some sockets (AF_PACKET) do not support null-sized recv() with MSG_TRUNC set, let's fall back to FIONREAD
1111 * for them. Checksums don't matter for raw sockets anyway, hence this should be fine. */
1113 if (ioctl(fd
, FIONREAD
, &k
) < 0)
1119 int flush_accept(int fd
) {
1121 struct pollfd pollfd
= {
1127 /* Similar to flush_fd() but flushes all incoming connection by accepting them and immediately closing them. */
1132 r
= poll(&pollfd
, 1, 0);
1142 cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
1147 if (errno
== EAGAIN
)
1157 struct cmsghdr
* cmsg_find(struct msghdr
*mh
, int level
, int type
, socklen_t length
) {
1158 struct cmsghdr
*cmsg
;
1162 CMSG_FOREACH(cmsg
, mh
)
1163 if (cmsg
->cmsg_level
== level
&&
1164 cmsg
->cmsg_type
== type
&&
1165 (length
== (socklen_t
) -1 || length
== cmsg
->cmsg_len
))
1171 int socket_ioctl_fd(void) {
1174 /* Create a socket to invoke the various network interface ioctl()s on. Traditionally only AF_INET was good for
1175 * that. Since kernel 4.6 AF_NETLINK works for this too. We first try to use AF_INET hence, but if that's not
1176 * available (for example, because it is made unavailable via SECCOMP or such), we'll fall back to the more
1177 * generic AF_NETLINK. */
1179 fd
= socket(AF_INET
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
1181 fd
= socket(AF_NETLINK
, SOCK_RAW
|SOCK_CLOEXEC
, NETLINK_GENERIC
);