1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include <netinet/ip.h>
17 #include "alloc-util.h"
20 #include "format-util.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "process-util.h"
27 #include "socket-util.h"
28 #include "string-table.h"
29 #include "string-util.h"
31 #include "user-util.h"
36 # define IDN_FLAGS NI_IDN
41 static const char* const socket_address_type_table
[] = {
42 [SOCK_STREAM
] = "Stream",
43 [SOCK_DGRAM
] = "Datagram",
45 [SOCK_RDM
] = "ReliableDatagram",
46 [SOCK_SEQPACKET
] = "SequentialPacket",
47 [SOCK_DCCP
] = "DatagramCongestionControl",
50 DEFINE_STRING_TABLE_LOOKUP(socket_address_type
, int);
52 int socket_address_parse(SocketAddress
*a
, const char *s
) {
60 a
->type
= SOCK_STREAM
;
65 /* IPv6 in [x:.....:z]:p notation */
71 n
= strndupa(s
+1, e
-s
-1);
74 if (inet_pton(AF_INET6
, n
, &a
->sockaddr
.in6
.sin6_addr
) <= 0)
75 return errno
> 0 ? -errno
: -EINVAL
;
82 r
= parse_ip_port(e
, &port
);
86 a
->sockaddr
.in6
.sin6_family
= AF_INET6
;
87 a
->sockaddr
.in6
.sin6_port
= htobe16(port
);
88 a
->size
= sizeof(struct sockaddr_in6
);
90 } else if (*s
== '/') {
96 if (l
>= sizeof(a
->sockaddr
.un
.sun_path
))
99 a
->sockaddr
.un
.sun_family
= AF_UNIX
;
100 memcpy(a
->sockaddr
.un
.sun_path
, s
, l
);
101 a
->size
= offsetof(struct sockaddr_un
, sun_path
) + l
+ 1;
103 } else if (*s
== '@') {
104 /* Abstract AF_UNIX socket */
108 if (l
>= sizeof(a
->sockaddr
.un
.sun_path
) - 1)
111 a
->sockaddr
.un
.sun_family
= AF_UNIX
;
112 memcpy(a
->sockaddr
.un
.sun_path
+1, s
+1, l
);
113 a
->size
= offsetof(struct sockaddr_un
, sun_path
) + 1 + l
;
115 } else if (startswith(s
, "vsock:")) {
116 /* AF_VSOCK socket in vsock:cid:port notation */
117 const char *cid_start
= s
+ STRLEN("vsock:");
120 e
= strchr(cid_start
, ':');
124 r
= safe_atou(e
+1, &port
);
128 n
= strndupa(cid_start
, e
- cid_start
);
130 r
= safe_atou(n
, &a
->sockaddr
.vm
.svm_cid
);
134 a
->sockaddr
.vm
.svm_cid
= VMADDR_CID_ANY
;
136 a
->sockaddr
.vm
.svm_family
= AF_VSOCK
;
137 a
->sockaddr
.vm
.svm_port
= port
;
138 a
->size
= sizeof(struct sockaddr_vm
);
145 r
= parse_ip_port(e
+ 1, &port
);
149 n
= strndupa(s
, e
-s
);
151 /* IPv4 in w.x.y.z:p notation? */
152 r
= inet_pton(AF_INET
, n
, &a
->sockaddr
.in
.sin_addr
);
157 /* Gotcha, it's a traditional IPv4 address */
158 a
->sockaddr
.in
.sin_family
= AF_INET
;
159 a
->sockaddr
.in
.sin_port
= htobe16(port
);
160 a
->size
= sizeof(struct sockaddr_in
);
164 if (strlen(n
) > IF_NAMESIZE
-1)
167 /* Uh, our last resort, an interface name */
168 idx
= if_nametoindex(n
);
172 a
->sockaddr
.in6
.sin6_family
= AF_INET6
;
173 a
->sockaddr
.in6
.sin6_port
= htobe16(port
);
174 a
->sockaddr
.in6
.sin6_scope_id
= idx
;
175 a
->sockaddr
.in6
.sin6_addr
= in6addr_any
;
176 a
->size
= sizeof(struct sockaddr_in6
);
181 r
= parse_ip_port(s
, &port
);
185 if (socket_ipv6_is_supported()) {
186 a
->sockaddr
.in6
.sin6_family
= AF_INET6
;
187 a
->sockaddr
.in6
.sin6_port
= htobe16(port
);
188 a
->sockaddr
.in6
.sin6_addr
= in6addr_any
;
189 a
->size
= sizeof(struct sockaddr_in6
);
191 a
->sockaddr
.in
.sin_family
= AF_INET
;
192 a
->sockaddr
.in
.sin_port
= htobe16(port
);
193 a
->sockaddr
.in
.sin_addr
.s_addr
= INADDR_ANY
;
194 a
->size
= sizeof(struct sockaddr_in
);
202 int socket_address_parse_and_warn(SocketAddress
*a
, const char *s
) {
206 /* Similar to socket_address_parse() but warns for IPv6 sockets when we don't support them. */
208 r
= socket_address_parse(&b
, s
);
212 if (!socket_ipv6_is_supported() && b
.sockaddr
.sa
.sa_family
== AF_INET6
) {
213 log_warning("Binding to IPv6 address not available since kernel does not support IPv6.");
214 return -EAFNOSUPPORT
;
221 int socket_address_parse_netlink(SocketAddress
*a
, const char *s
) {
224 _cleanup_free_
char *sfamily
= NULL
;
232 if (sscanf(s
, "%ms %u", &sfamily
, &group
) < 1)
233 return errno
> 0 ? -errno
: -EINVAL
;
235 family
= netlink_family_from_string(sfamily
);
239 a
->sockaddr
.nl
.nl_family
= AF_NETLINK
;
240 a
->sockaddr
.nl
.nl_groups
= group
;
243 a
->size
= sizeof(struct sockaddr_nl
);
244 a
->protocol
= family
;
249 int socket_address_verify(const SocketAddress
*a
) {
252 switch (socket_address_family(a
)) {
255 if (a
->size
!= sizeof(struct sockaddr_in
))
258 if (a
->sockaddr
.in
.sin_port
== 0)
261 if (!IN_SET(a
->type
, SOCK_STREAM
, SOCK_DGRAM
))
267 if (a
->size
!= sizeof(struct sockaddr_in6
))
270 if (a
->sockaddr
.in6
.sin6_port
== 0)
273 if (!IN_SET(a
->type
, SOCK_STREAM
, SOCK_DGRAM
))
279 if (a
->size
< offsetof(struct sockaddr_un
, sun_path
))
282 if (a
->size
> offsetof(struct sockaddr_un
, sun_path
)) {
284 if (a
->sockaddr
.un
.sun_path
[0] != 0) {
288 e
= memchr(a
->sockaddr
.un
.sun_path
, 0, sizeof(a
->sockaddr
.un
.sun_path
));
292 if (a
->size
!= offsetof(struct sockaddr_un
, sun_path
) + (e
- a
->sockaddr
.un
.sun_path
) + 1)
297 if (!IN_SET(a
->type
, SOCK_STREAM
, SOCK_DGRAM
, SOCK_SEQPACKET
))
304 if (a
->size
!= sizeof(struct sockaddr_nl
))
307 if (!IN_SET(a
->type
, SOCK_RAW
, SOCK_DGRAM
))
313 if (a
->size
!= sizeof(struct sockaddr_vm
))
316 if (!IN_SET(a
->type
, SOCK_STREAM
, SOCK_DGRAM
))
322 return -EAFNOSUPPORT
;
326 int socket_address_print(const SocketAddress
*a
, char **ret
) {
332 r
= socket_address_verify(a
);
336 if (socket_address_family(a
) == AF_NETLINK
) {
337 _cleanup_free_
char *sfamily
= NULL
;
339 r
= netlink_family_to_string_alloc(a
->protocol
, &sfamily
);
343 r
= asprintf(ret
, "%s %u", sfamily
, a
->sockaddr
.nl
.nl_groups
);
350 return sockaddr_pretty(&a
->sockaddr
.sa
, a
->size
, false, true, ret
);
353 bool socket_address_can_accept(const SocketAddress
*a
) {
357 IN_SET(a
->type
, SOCK_STREAM
, SOCK_SEQPACKET
);
360 bool socket_address_equal(const SocketAddress
*a
, const SocketAddress
*b
) {
364 /* Invalid addresses are unequal to all */
365 if (socket_address_verify(a
) < 0 ||
366 socket_address_verify(b
) < 0)
369 if (a
->type
!= b
->type
)
372 if (socket_address_family(a
) != socket_address_family(b
))
375 switch (socket_address_family(a
)) {
378 if (a
->sockaddr
.in
.sin_addr
.s_addr
!= b
->sockaddr
.in
.sin_addr
.s_addr
)
381 if (a
->sockaddr
.in
.sin_port
!= b
->sockaddr
.in
.sin_port
)
387 if (memcmp(&a
->sockaddr
.in6
.sin6_addr
, &b
->sockaddr
.in6
.sin6_addr
, sizeof(a
->sockaddr
.in6
.sin6_addr
)) != 0)
390 if (a
->sockaddr
.in6
.sin6_port
!= b
->sockaddr
.in6
.sin6_port
)
396 if (a
->size
<= offsetof(struct sockaddr_un
, sun_path
) ||
397 b
->size
<= offsetof(struct sockaddr_un
, sun_path
))
400 if ((a
->sockaddr
.un
.sun_path
[0] == 0) != (b
->sockaddr
.un
.sun_path
[0] == 0))
403 if (a
->sockaddr
.un
.sun_path
[0]) {
404 if (!path_equal_or_files_same(a
->sockaddr
.un
.sun_path
, b
->sockaddr
.un
.sun_path
, 0))
407 if (a
->size
!= b
->size
)
410 if (memcmp(a
->sockaddr
.un
.sun_path
, b
->sockaddr
.un
.sun_path
, a
->size
) != 0)
417 if (a
->protocol
!= b
->protocol
)
420 if (a
->sockaddr
.nl
.nl_groups
!= b
->sockaddr
.nl
.nl_groups
)
426 if (a
->sockaddr
.vm
.svm_cid
!= b
->sockaddr
.vm
.svm_cid
)
429 if (a
->sockaddr
.vm
.svm_port
!= b
->sockaddr
.vm
.svm_port
)
435 /* Cannot compare, so we assume the addresses are different */
442 bool socket_address_is(const SocketAddress
*a
, const char *s
, int type
) {
443 struct SocketAddress b
;
448 if (socket_address_parse(&b
, s
) < 0)
453 return socket_address_equal(a
, &b
);
456 bool socket_address_is_netlink(const SocketAddress
*a
, const char *s
) {
457 struct SocketAddress b
;
462 if (socket_address_parse_netlink(&b
, s
) < 0)
465 return socket_address_equal(a
, &b
);
468 const char* socket_address_get_path(const SocketAddress
*a
) {
471 if (socket_address_family(a
) != AF_UNIX
)
474 if (a
->sockaddr
.un
.sun_path
[0] == 0)
477 return a
->sockaddr
.un
.sun_path
;
480 bool socket_ipv6_is_supported(void) {
481 if (access("/proc/net/if_inet6", F_OK
) != 0)
487 bool socket_address_matches_fd(const SocketAddress
*a
, int fd
) {
494 b
.size
= sizeof(b
.sockaddr
);
495 if (getsockname(fd
, &b
.sockaddr
.sa
, &b
.size
) < 0)
498 if (b
.sockaddr
.sa
.sa_family
!= a
->sockaddr
.sa
.sa_family
)
501 solen
= sizeof(b
.type
);
502 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &b
.type
, &solen
) < 0)
505 if (b
.type
!= a
->type
)
508 if (a
->protocol
!= 0) {
509 solen
= sizeof(b
.protocol
);
510 if (getsockopt(fd
, SOL_SOCKET
, SO_PROTOCOL
, &b
.protocol
, &solen
) < 0)
513 if (b
.protocol
!= a
->protocol
)
517 return socket_address_equal(a
, &b
);
520 int sockaddr_port(const struct sockaddr
*_sa
, unsigned *ret_port
) {
521 union sockaddr_union
*sa
= (union sockaddr_union
*) _sa
;
523 /* Note, this returns the port as 'unsigned' rather than 'uint16_t', as AF_VSOCK knows larger ports */
527 switch (sa
->sa
.sa_family
) {
530 *ret_port
= be16toh(sa
->in
.sin_port
);
534 *ret_port
= be16toh(sa
->in6
.sin6_port
);
538 *ret_port
= sa
->vm
.svm_port
;
542 return -EAFNOSUPPORT
;
546 int sockaddr_pretty(const struct sockaddr
*_sa
, socklen_t salen
, bool translate_ipv6
, bool include_port
, char **ret
) {
547 union sockaddr_union
*sa
= (union sockaddr_union
*) _sa
;
552 assert(salen
>= sizeof(sa
->sa
.sa_family
));
554 switch (sa
->sa
.sa_family
) {
559 a
= be32toh(sa
->in
.sin_addr
.s_addr
);
564 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF,
565 be16toh(sa
->in
.sin_port
));
569 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF);
576 static const unsigned char ipv4_prefix
[] = {
577 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
580 if (translate_ipv6
&&
581 memcmp(&sa
->in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0) {
582 const uint8_t *a
= sa
->in6
.sin6_addr
.s6_addr
+12;
586 a
[0], a
[1], a
[2], a
[3],
587 be16toh(sa
->in6
.sin6_port
));
591 a
[0], a
[1], a
[2], a
[3]);
595 char a
[INET6_ADDRSTRLEN
];
597 inet_ntop(AF_INET6
, &sa
->in6
.sin6_addr
, a
, sizeof(a
));
603 be16toh(sa
->in6
.sin6_port
));
617 if (salen
<= offsetof(struct sockaddr_un
, sun_path
)) {
618 p
= strdup("<unnamed>");
622 } else if (sa
->un
.sun_path
[0] == 0) {
625 /* FIXME: We assume we can print the
626 * socket path here and that it hasn't
627 * more than one NUL byte. That is
628 * actually an invalid assumption */
630 p
= new(char, sizeof(sa
->un
.sun_path
)+1);
635 memcpy(p
+1, sa
->un
.sun_path
+1, sizeof(sa
->un
.sun_path
)-1);
636 p
[sizeof(sa
->un
.sun_path
)] = 0;
639 p
= strndup(sa
->un
.sun_path
, sizeof(sa
->un
.sun_path
));
653 r
= asprintf(&p
, "vsock:%u", sa
->vm
.svm_cid
);
666 int getpeername_pretty(int fd
, bool include_port
, char **ret
) {
667 union sockaddr_union sa
;
668 socklen_t salen
= sizeof(sa
);
674 if (getpeername(fd
, &sa
.sa
, &salen
) < 0)
677 if (sa
.sa
.sa_family
== AF_UNIX
) {
678 struct ucred ucred
= {};
680 /* UNIX connection sockets are anonymous, so let's use
681 * PID/UID as pretty credentials instead */
683 r
= getpeercred(fd
, &ucred
);
687 if (asprintf(ret
, "PID "PID_FMT
"/UID "UID_FMT
, ucred
.pid
, ucred
.uid
) < 0)
693 /* For remote sockets we translate IPv6 addresses back to IPv4
694 * if applicable, since that's nicer. */
696 return sockaddr_pretty(&sa
.sa
, salen
, true, include_port
, ret
);
699 int getsockname_pretty(int fd
, char **ret
) {
700 union sockaddr_union sa
;
701 socklen_t salen
= sizeof(sa
);
706 if (getsockname(fd
, &sa
.sa
, &salen
) < 0)
709 /* For local sockets we do not translate IPv6 addresses back
710 * to IPv6 if applicable, since this is usually used for
711 * listening sockets where the difference between IPv4 and
714 return sockaddr_pretty(&sa
.sa
, salen
, false, true, ret
);
717 int socknameinfo_pretty(union sockaddr_union
*sa
, socklen_t salen
, char **_ret
) {
719 char host
[NI_MAXHOST
], *ret
;
723 r
= getnameinfo(&sa
->sa
, salen
, host
, sizeof(host
), NULL
, 0, IDN_FLAGS
);
725 int saved_errno
= errno
;
727 r
= sockaddr_pretty(&sa
->sa
, salen
, true, true, &ret
);
731 log_debug_errno(saved_errno
, "getnameinfo(%s) failed: %m", ret
);
742 int socket_address_unlink(SocketAddress
*a
) {
745 if (socket_address_family(a
) != AF_UNIX
)
748 if (a
->sockaddr
.un
.sun_path
[0] == 0)
751 if (unlink(a
->sockaddr
.un
.sun_path
) < 0)
757 static const char* const netlink_family_table
[] = {
758 [NETLINK_ROUTE
] = "route",
759 [NETLINK_FIREWALL
] = "firewall",
760 [NETLINK_INET_DIAG
] = "inet-diag",
761 [NETLINK_NFLOG
] = "nflog",
762 [NETLINK_XFRM
] = "xfrm",
763 [NETLINK_SELINUX
] = "selinux",
764 [NETLINK_ISCSI
] = "iscsi",
765 [NETLINK_AUDIT
] = "audit",
766 [NETLINK_FIB_LOOKUP
] = "fib-lookup",
767 [NETLINK_CONNECTOR
] = "connector",
768 [NETLINK_NETFILTER
] = "netfilter",
769 [NETLINK_IP6_FW
] = "ip6-fw",
770 [NETLINK_DNRTMSG
] = "dnrtmsg",
771 [NETLINK_KOBJECT_UEVENT
] = "kobject-uevent",
772 [NETLINK_GENERIC
] = "generic",
773 [NETLINK_SCSITRANSPORT
] = "scsitransport",
774 [NETLINK_ECRYPTFS
] = "ecryptfs",
775 [NETLINK_RDMA
] = "rdma",
778 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family
, int, INT_MAX
);
780 static const char* const socket_address_bind_ipv6_only_table
[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX
] = {
781 [SOCKET_ADDRESS_DEFAULT
] = "default",
782 [SOCKET_ADDRESS_BOTH
] = "both",
783 [SOCKET_ADDRESS_IPV6_ONLY
] = "ipv6-only"
786 DEFINE_STRING_TABLE_LOOKUP(socket_address_bind_ipv6_only
, SocketAddressBindIPv6Only
);
788 SocketAddressBindIPv6Only
socket_address_bind_ipv6_only_or_bool_from_string(const char *n
) {
791 r
= parse_boolean(n
);
793 return SOCKET_ADDRESS_IPV6_ONLY
;
795 return SOCKET_ADDRESS_BOTH
;
797 return socket_address_bind_ipv6_only_from_string(n
);
800 bool sockaddr_equal(const union sockaddr_union
*a
, const union sockaddr_union
*b
) {
804 if (a
->sa
.sa_family
!= b
->sa
.sa_family
)
807 if (a
->sa
.sa_family
== AF_INET
)
808 return a
->in
.sin_addr
.s_addr
== b
->in
.sin_addr
.s_addr
;
810 if (a
->sa
.sa_family
== AF_INET6
)
811 return memcmp(&a
->in6
.sin6_addr
, &b
->in6
.sin6_addr
, sizeof(a
->in6
.sin6_addr
)) == 0;
813 if (a
->sa
.sa_family
== AF_VSOCK
)
814 return a
->vm
.svm_cid
== b
->vm
.svm_cid
;
819 int fd_inc_sndbuf(int fd
, size_t n
) {
821 socklen_t l
= sizeof(value
);
823 r
= getsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &value
, &l
);
824 if (r
>= 0 && l
== sizeof(value
) && (size_t) value
>= n
*2)
827 /* If we have the privileges we will ignore the kernel limit. */
830 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUFFORCE
, &value
, sizeof(value
)) < 0)
831 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &value
, sizeof(value
)) < 0)
837 int fd_inc_rcvbuf(int fd
, size_t n
) {
839 socklen_t l
= sizeof(value
);
841 r
= getsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &value
, &l
);
842 if (r
>= 0 && l
== sizeof(value
) && (size_t) value
>= n
*2)
845 /* If we have the privileges we will ignore the kernel limit. */
848 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUFFORCE
, &value
, sizeof(value
)) < 0)
849 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &value
, sizeof(value
)) < 0)
854 static const char* const ip_tos_table
[] = {
855 [IPTOS_LOWDELAY
] = "low-delay",
856 [IPTOS_THROUGHPUT
] = "throughput",
857 [IPTOS_RELIABILITY
] = "reliability",
858 [IPTOS_LOWCOST
] = "low-cost",
861 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos
, int, 0xff);
863 bool ifname_valid(const char *p
) {
866 /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources
867 * but slightly stricter, as we only allow non-control, non-space ASCII characters in the interface name. We
868 * also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */
873 if (strlen(p
) >= IFNAMSIZ
)
876 if (dot_or_dot_dot(p
))
880 if ((unsigned char) *p
>= 127U)
883 if ((unsigned char) *p
<= 32U)
886 if (IN_SET(*p
, ':', '/'))
889 numeric
= numeric
&& (*p
>= '0' && *p
<= '9');
899 bool address_label_valid(const char *p
) {
904 if (strlen(p
) >= IFNAMSIZ
)
908 if ((uint8_t) *p
>= 127U)
911 if ((uint8_t) *p
<= 31U)
919 int getpeercred(int fd
, struct ucred
*ucred
) {
920 socklen_t n
= sizeof(struct ucred
);
927 r
= getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &u
, &n
);
931 if (n
!= sizeof(struct ucred
))
934 /* Check if the data is actually useful and not suppressed due to namespacing issues */
935 if (!pid_is_valid(u
.pid
))
938 /* Note that we don't check UID/GID here, as namespace translation works differently there: instead of
939 * receiving in "invalid" user/group we get the overflow UID/GID. */
945 int getpeersec(int fd
, char **ret
) {
946 _cleanup_free_
char *s
= NULL
;
957 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERSEC
, s
, &n
) >= 0)
974 int getpeergroups(int fd
, gid_t
**ret
) {
975 socklen_t n
= sizeof(gid_t
) * 64;
976 _cleanup_free_ gid_t
*d
= NULL
;
986 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERGROUPS
, d
, &n
) >= 0)
995 assert_se(n
% sizeof(gid_t
) == 0);
998 if ((socklen_t
) (int) n
!= n
)
1009 const struct sockaddr
*sa
, socklen_t len
,
1013 struct cmsghdr cmsghdr
;
1014 uint8_t buf
[CMSG_SPACE(sizeof(int))];
1016 struct msghdr mh
= {
1017 .msg_name
= (struct sockaddr
*) sa
,
1019 .msg_control
= &control
,
1020 .msg_controllen
= sizeof(control
),
1022 struct cmsghdr
*cmsg
;
1024 assert(transport_fd
>= 0);
1027 cmsg
= CMSG_FIRSTHDR(&mh
);
1028 cmsg
->cmsg_level
= SOL_SOCKET
;
1029 cmsg
->cmsg_type
= SCM_RIGHTS
;
1030 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
1031 memcpy(CMSG_DATA(cmsg
), &fd
, sizeof(int));
1033 mh
.msg_controllen
= CMSG_SPACE(sizeof(int));
1034 if (sendmsg(transport_fd
, &mh
, MSG_NOSIGNAL
| flags
) < 0)
1040 int receive_one_fd(int transport_fd
, int flags
) {
1042 struct cmsghdr cmsghdr
;
1043 uint8_t buf
[CMSG_SPACE(sizeof(int))];
1045 struct msghdr mh
= {
1046 .msg_control
= &control
,
1047 .msg_controllen
= sizeof(control
),
1049 struct cmsghdr
*cmsg
, *found
= NULL
;
1051 assert(transport_fd
>= 0);
1054 * Receive a single FD via @transport_fd. We don't care for
1055 * the transport-type. We retrieve a single FD at most, so for
1056 * packet-based transports, the caller must ensure to send
1057 * only a single FD per packet. This is best used in
1058 * combination with send_one_fd().
1061 if (recvmsg(transport_fd
, &mh
, MSG_CMSG_CLOEXEC
| flags
) < 0)
1064 CMSG_FOREACH(cmsg
, &mh
) {
1065 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1066 cmsg
->cmsg_type
== SCM_RIGHTS
&&
1067 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
1075 cmsg_close_all(&mh
);
1079 return *(int*) CMSG_DATA(found
);
1082 ssize_t
next_datagram_size_fd(int fd
) {
1086 /* This is a bit like FIONREAD/SIOCINQ, however a bit more powerful. The difference being: recv(MSG_PEEK) will
1087 * actually cause the next datagram in the queue to be validated regarding checksums, which FIONREAD doesn't
1088 * do. This difference is actually of major importance as we need to be sure that the size returned here
1089 * actually matches what we will read with recvmsg() next, as otherwise we might end up allocating a buffer of
1090 * the wrong size. */
1092 l
= recv(fd
, NULL
, 0, MSG_PEEK
|MSG_TRUNC
);
1094 if (IN_SET(errno
, EOPNOTSUPP
, EFAULT
))
1107 /* Some sockets (AF_PACKET) do not support null-sized recv() with MSG_TRUNC set, let's fall back to FIONREAD
1108 * for them. Checksums don't matter for raw sockets anyway, hence this should be fine. */
1110 if (ioctl(fd
, FIONREAD
, &k
) < 0)
1116 int flush_accept(int fd
) {
1118 struct pollfd pollfd
= {
1124 /* Similar to flush_fd() but flushes all incoming connection by accepting them and immediately closing them. */
1129 r
= poll(&pollfd
, 1, 0);
1139 cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
1144 if (errno
== EAGAIN
)
1154 struct cmsghdr
* cmsg_find(struct msghdr
*mh
, int level
, int type
, socklen_t length
) {
1155 struct cmsghdr
*cmsg
;
1159 CMSG_FOREACH(cmsg
, mh
)
1160 if (cmsg
->cmsg_level
== level
&&
1161 cmsg
->cmsg_type
== type
&&
1162 (length
== (socklen_t
) -1 || length
== cmsg
->cmsg_len
))
1168 int socket_ioctl_fd(void) {
1171 /* Create a socket to invoke the various network interface ioctl()s on. Traditionally only AF_INET was good for
1172 * that. Since kernel 4.6 AF_NETLINK works for this too. We first try to use AF_INET hence, but if that's not
1173 * available (for example, because it is made unavailable via SECCOMP or such), we'll fall back to the more
1174 * generic AF_NETLINK. */
1176 fd
= socket(AF_INET
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
1178 fd
= socket(AF_NETLINK
, SOCK_RAW
|SOCK_CLOEXEC
, NETLINK_GENERIC
);