The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
fd.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: d27c2582bfe727c2b497b6cdbf5ee738a1deeb29 $
19 * @file lib/bio/fd.c
20 * @brief BIO abstractions for file descriptors
21 *
22 * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
23 */
24
25#include <freeradius-devel/bio/fd_priv.h>
26#include <freeradius-devel/bio/fd_read.h>
27#include <freeradius-devel/bio/fd_write.h>
28#include <freeradius-devel/bio/null.h>
29#include <freeradius-devel/bio/network.h>
30
31/*
32 * More portability idiocy
33 * Mac OSX Lion doesn't define SOL_IP. But IPPROTO_IP works.
34 */
35#ifndef SOL_IP
36# define SOL_IP IPPROTO_IP
37#endif
38
39/*
40 * glibc 2.4 and uClibc 0.9.29 introduce IPV6_RECVPKTINFO etc. and
41 * change IPV6_PKTINFO This is only supported in Linux kernel >=
42 * 2.6.14
43 *
44 * This is only an approximation because the kernel version that libc
45 * was compiled against could be older or newer than the one being
46 * run. But this should not be a problem -- we just keep using the
47 * old kernel interface.
48 */
49#ifdef __linux__
50# ifdef IPV6_RECVPKTINFO
51# include <linux/version.h>
52# if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
53# ifdef IPV6_2292PKTINFO
54# undef IPV6_RECVPKTINFO
55# undef IPV6_PKTINFO
56# define IPV6_RECVPKTINFO IPV6_2292PKTINFO
57# define IPV6_PKTINFO IPV6_2292PKTINFO
58# endif
59# endif
60/* Fall back to the legacy socket option if IPV6_RECVPKTINFO isn't defined */
61# elif defined(IPV6_2292PKTINFO)
62# define IPV6_RECVPKTINFO IPV6_2292PKTINFO
63# endif
64#else
65
66/*
67 * For everything that's not Linux we assume RFC 3542 compliance
68 * - setsockopt() takes IPV6_RECVPKTINFO
69 * - cmsg_type is IPV6_PKTINFO (in sendmsg, recvmsg)
70 *
71 * If we don't have IPV6_RECVPKTINFO defined but do have IPV6_PKTINFO
72 * defined, chances are the API is RFC2292 compliant and we need to use
73 * IPV6_PKTINFO for both.
74 */
75# if !defined(IPV6_RECVPKTINFO) && defined(IPV6_PKTINFO)
76# define IPV6_RECVPKTINFO IPV6_PKTINFO
77
78/*
79 * Ensure IPV6_RECVPKTINFO is not defined somehow if we have we
80 * don't have IPV6_PKTINFO.
81 */
82# elif !defined(IPV6_PKTINFO)
83# undef IPV6_RECVPKTINFO
84# endif
85#endif
86
87#define ADDR_INIT do { \
88 addr->when = fr_time(); \
89 addr->socket.type = my->info.socket.type; \
90 addr->socket.fd = -1; \
91 addr->socket.inet.ifindex = my->info.socket.inet.ifindex; \
92 } while (0)
93
94/** Orderly shutdown.
95 *
96 */
98{
99 int rcode;
100 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
101
102 if (my->info.state == FR_BIO_FD_STATE_CLOSED) return 0;
103
104 FR_TIMER_DELETE(&my->connect.ev);
105 if (my->connect.el) {
107 my->connect.el = NULL;
108 }
109
110 rcode = fr_bio_fd_close(&my->bio);
111 if (rcode < 0) return rcode;
112
113 /*
114 * A Unix domain socket has a path in the file system, which has to be removed.
115 *
116 * This is done on shutdown rather than in fr_bio_fd_close(), because a close leaves the bio
117 * alive and able to be re-opened, and a shutdown destroys it.
118 */
119 if (my->info.socket.af == AF_LOCAL) return fr_bio_fd_unix_shutdown(&my->bio);
120
121 return 0;
122}
123
124static int fr_bio_fd_eof(fr_bio_t *bio)
125{
126 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
127
128 my->info.eof = true;
129
130 /*
131 * A read returns zero, which is what EOF means to the caller.
132 *
133 * A write returns "this BIO was shutdown", which is true for both BIOs at EOF, and BIOs which
134 * were shut down due to an error.
135 */
136 bio->read = fr_bio_null_read;
138
139 /*
140 * Nothing more for us to do, tell fr_bio_eof() that it can continue with poking other BIOs.
141 */
142 return 1;
143}
144
146{
147 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
148
149 my->info.write_blocked = false;
150 return 1;
151}
152
153/** Stream read.
154 *
155 * Stream sockets return 0 at EOF. However, we want to distinguish that from the case of datagram
156 * sockets, which return 0 when there's no data. So we return 0 to the caller for "no data", but also
157 * call the EOF function to tell all of the related BIOs that we're at EOF.
158 */
159static ssize_t fr_bio_fd_read_stream(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
160{
161 int tries = 0;
162 ssize_t rcode;
163 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
164
165 do {
166 rcode = read(my->info.socket.fd, buffer, size);
167 if (rcode == 0) {
168 fr_bio_t *head;
169
170 /*
171 * Flush any pending writes, shut down the BIO, and then mark it as EOF.
172 */
173 head = fr_bio_head(bio);
174
175 (void) fr_bio_write(head, NULL, NULL, SIZE_MAX);
176
177 /*
178 * Shut down through this bio, not through the head. fr_bio_shutdown() walks
179 * back up from here, so that any bio holding data can hand it to the
180 * application before the chain is torn down.
181 */
182 rcode = fr_bio_shutdown(bio);
183 if (rcode < 0) return rcode;
184
185 return 0;
186 }
187 } while (fr_bio_fd_read_retry(my, &rcode, &tries));
188
189 return rcode;
190}
191
192/** Connected datagram read.
193 *
194 * The difference between this and stream protocols is that for datagrams. a read of zero means "no packets",
195 * where a read of zero on a steam socket means "EOF".
196 *
197 * Connected sockets do _not_ update per-packet contexts.
198 */
200{
201 int tries = 0;
202 ssize_t rcode;
203 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
204 socklen_t salen;
205 struct sockaddr_storage sockaddr;
206
207#ifdef STATIC_ANALYZER
208 sockaddr = (struct sockaddr_storage) {};
209#endif
210
211 do {
212 salen = sizeof(sockaddr);
213
214 rcode = recvfrom(my->info.socket.fd, buffer, size, 0, (struct sockaddr *) &sockaddr, &salen);
215 if (rcode > 0) {
216 fr_assert(salen == my->remote_sockaddr_len);
217 fr_assert(sockaddr.ss_family == my->remote_sockaddr.ss_family);
218 fr_assert((sockaddr.ss_family == AF_INET) || (sockaddr.ss_family == AF_INET6)); /* datagram unix is not supported */
219 }
220
221 /*
222 * We do NOT call fr_bio_eof(), as 0 just means "no more data".
223 */
224 if (rcode == 0) return rcode;
225 } while (fr_bio_fd_read_retry(my, &rcode, &tries));
226
227 return rcode;
228}
229
230/** Return the datagram which fr_bio_fd_accept() read from the listening socket
231 *
232 * fr_bio_fd_accept_datagram() has to read the datagram in order to name the peer, so the datagram is no
233 * longer in the socket. The first read returns the saved datagram, and then switches to reading the
234 * connected socket.
235 *
236 * A datagram which is larger than the buffer is truncated, in the same way as a read of the socket.
237 */
239{
240 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
241 size_t len;
242
243 fr_assert(my->first_datagram != NULL);
244
246 if (len > size) len = size;
247
248 memcpy(buffer, my->first_datagram, len);
249
250 TALLOC_FREE(my->first_datagram);
251 my->first_datagram_len = 0;
252
254
255 return len;
256}
257
258/** Check if the packet is allowed.
259 *
260 * If there's a trie, and the packet is from a disallowed source, then discard it. In order to avoid
261 * bouncing through the event loop too many times, we retry the read, as if there was an EINTR.
262 *
263 * @todo - increment a "discarded packet" counter. Or maybe add a logging destination, where we log these
264 * kinds of issues.
265 */
266#define FR_BIO_FD_NETWORK_ALLOW(_my, _sockaddr, _socklen) \
267 do { \
268 if ((_my)->trie && !fr_bio_network_trie_allow((_my)->trie, (struct sockaddr *) &(_sockaddr), (_socklen))) { \
269 tries++; \
270 if (tries <= (_my)->max_tries) goto retry; \
271 return 0; \
272 } \
273 } while (0)
274
275/** Read from a UDP socket where we know our IP
276 */
277static ssize_t fr_bio_fd_recvfrom(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
278{
279 int tries = 0;
280 ssize_t rcode;
281 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
282 socklen_t salen;
283 struct sockaddr_storage sockaddr;
284
285 do {
286 retry:
287 salen = sizeof(sockaddr);
288
289 rcode = recvfrom(my->info.socket.fd, buffer, size, 0, (struct sockaddr *) &sockaddr, &salen);
290 if (rcode > 0) {
292
293 FR_BIO_FD_NETWORK_ALLOW(my, sockaddr, salen);
294
296
297 ADDR_INIT;
298
299 addr->socket.inet.dst_ipaddr = my->info.socket.inet.src_ipaddr;
300 addr->socket.inet.dst_port = my->info.socket.inet.src_port;
301
302 (void) fr_ipaddr_from_sockaddr(&addr->socket.inet.src_ipaddr, &addr->socket.inet.src_port,
303 &sockaddr, salen);
304 }
305
306 if (rcode == 0) return rcode;
307 } while (fr_bio_fd_read_retry(my, &rcode, &tries));
308
309 return rcode;
310}
311
312/** Write to fd.
313 *
314 * This function is used for connected sockets, where we ignore the packet_ctx.
315 */
316static ssize_t fr_bio_fd_write(fr_bio_t *bio, UNUSED void *packet_ctx, const void *buffer, size_t size)
317{
318 int tries = 0;
319 ssize_t rcode;
320 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
321
322 /*
323 * FD bios do nothing on flush.
324 */
325 if (!buffer) return 0;
326
327 do {
328 /*
329 * We could call send() instead of write()! Posix says:
330 *
331 * "A write was attempted on a socket that is shut down for writing, or is no longer
332 * connected. In the latter case, if the socket is of type SOCK_STREAM, a SIGPIPE signal shall
333 * also be sent to the thread."
334 *
335 * We can override this behavior by calling send(), and passing the special flag which says
336 * "don't do that!". The system call will then return EPIPE, which indicates that the socket is
337 * no longer usable.
338 *
339 * However, we also set the SO_NOSIGPIPE socket option, which means that we can just call write()
340 * here.
341 */
342 rcode = write(my->info.socket.fd, buffer, size);
343 } while (fr_bio_fd_write_retry(my, &rcode, &tries, size));
344
345 return rcode;
346}
347
348/** Write to a UDP socket where we know our IP
349 *
350 */
351static ssize_t fr_bio_fd_sendto(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size)
352{
353 int tries = 0;
354 ssize_t rcode;
355 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
357 socklen_t salen;
358 struct sockaddr_storage sockaddr;
359
360 /*
361 * FD bios do nothing on flush.
362 */
363 if (!buffer) return 0;
364
365 // get destination IP
366 (void) fr_ipaddr_to_sockaddr(&sockaddr, &salen, &addr->socket.inet.dst_ipaddr, addr->socket.inet.dst_port);
367
368 do {
369 rcode = sendto(my->info.socket.fd, buffer, size, 0, (struct sockaddr *) &sockaddr, salen);
370 } while (fr_bio_fd_write_retry(my, &rcode, &tries, size));
371
373
374 return rcode;
375}
376
377
378#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
379static ssize_t fr_bio_fd_recvfromto_common(fr_bio_fd_t *my, void *packet_ctx, void *buffer, size_t size)
380{
381 int tries = 0;
382 ssize_t rcode;
383 struct sockaddr_storage from;
385
386#ifdef STATIC_ANALYZER
387 from.ss_family = AF_UNSPEC;
388#endif
389
390retry:
391 memset(&my->cbuf, 0, sizeof(my->cbuf));
392 memset(&my->msgh, 0, sizeof(struct msghdr));
393
394 my->iov = (struct iovec) {
395 .iov_base = buffer,
396 .iov_len = size,
397 };
398
399 my->msgh = (struct msghdr) {
400 .msg_control = my->cbuf,
401 .msg_controllen = sizeof(my->cbuf),
402 .msg_name = &from,
403 .msg_namelen = sizeof(from),
404 .msg_iov = &my->iov,
405 .msg_iovlen = 1,
406 .msg_flags = 0,
407 };
408
409 do {
410 rcode = recvmsg(my->info.socket.fd, &my->msgh, 0);
411 if (rcode > 0) {
412 if ((my->msgh.msg_flags & MSG_TRUNC) != 0) return 0;
413 if ((my->msgh.msg_flags & MSG_CTRUNC) != 0) return 0;
414
415 FR_BIO_FD_NETWORK_ALLOW(my, from, my->msgh.msg_namelen);
416
417 ADDR_INIT;
418
419 (void) fr_ipaddr_from_sockaddr(&addr->socket.inet.src_ipaddr, &addr->socket.inet.src_port,
420 &from, my->msgh.msg_namelen);
421 }
422
423 if (rcode == 0) return rcode;
424 } while (fr_bio_fd_read_retry(my, &rcode, &tries));
425
426 return rcode;
427}
428#endif
429
430#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
431
432/** Read from a UDP socket where we can change our IP, IPv4 version.
433 */
434static ssize_t fr_bio_fd_recvfromto4(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
435{
436 ssize_t rcode;
437 struct cmsghdr *cmsg;
438 fr_time_t when = fr_time_wrap(0);
439 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
441
442 rcode = fr_bio_fd_recvfromto_common(my, packet_ctx, buffer, size);
443 if (rcode <= 0) return rcode;
444
445DIAG_OFF(sign-compare)
446 /* Process auxiliary received data in msgh */
447 for (cmsg = CMSG_FIRSTHDR(&my->msgh);
448 cmsg != NULL;
449 cmsg = CMSG_NXTHDR(&my->msgh, cmsg)) {
450DIAG_ON(sign-compare)
451
452#ifdef IP_PKTINFO
453 if ((cmsg->cmsg_level == SOL_IP) &&
454 (cmsg->cmsg_type == IP_PKTINFO)) {
455 struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
456 struct sockaddr_in to = {
457 .sin_family = AF_INET,
458 .sin_addr = i->ipi_addr,
459 .sin_port = htons(my->info.socket.inet.src_port),
460 };
461
462 (void) fr_ipaddr_from_sockaddr(&addr->socket.inet.dst_ipaddr, &addr->socket.inet.dst_port,
463 (struct sockaddr_storage *) &to, sizeof(struct sockaddr_in));
464 addr->socket.inet.ifindex = i->ipi_ifindex;
465 break;
466 }
467#endif
468
469#ifdef IP_RECVDSTADDR
470 if ((cmsg->cmsg_level == IPPROTO_IP) &&
471 (cmsg->cmsg_type == IP_RECVDSTADDR)) {
472 struct in_addr *i = (struct in_addr *) CMSG_DATA(cmsg);
473 struct sockaddr_in to = {
474 .sin_family = AF_INET,
475 .sin_addr = *i,
476 .sin_port = htons(my->info.socket.inet.src_port),
477 };
478
479 (void) fr_ipaddr_from_sockaddr(&addr->socket.inet.dst_ipaddr, &addr->socket.inet.dst_port,
480 (struct sockaddr_storage *) &to, sizeof(struct sockaddr_in));
481 break;
482 }
483#endif
484
485#ifdef SO_TIMESTAMPNS
486 if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMPNS)) {
487 when = fr_time_from_timespec((struct timespec *)CMSG_DATA(cmsg));
488 }
489
490#elif defined(SO_TIMESTAMP)
491 if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMP)) {
492 when = fr_time_from_timeval((struct timeval *)CMSG_DATA(cmsg));
493 }
494#endif
495 }
496
497 if fr_time_eq(when, fr_time_wrap(0)) when = fr_time();
498
499 addr->when = when;
500
501 return rcode;
502}
503
504/** Send to UDP socket where we can change our IP, IPv4 version.
505 */
506static ssize_t fr_bio_fd_sendfromto4(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size)
507{
508 int tries = 0;
509 ssize_t rcode;
510 struct cmsghdr *cmsg;
511 struct sockaddr_storage to;
512 socklen_t to_len;
513 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
515
516 memset(&my->cbuf, 0, sizeof(my->cbuf));
517 memset(&my->msgh, 0, sizeof(struct msghdr));
518
519 (void) fr_ipaddr_to_sockaddr(&to, &to_len, &addr->socket.inet.dst_ipaddr, addr->socket.inet.dst_port);
520
521 my->iov = (struct iovec) {
522 .iov_base = UNCONST(void *, buffer),
523 .iov_len = size,
524 };
525
526 my->msgh = (struct msghdr) {
527 .msg_control = my->cbuf,
528 // controllen is set below
529 .msg_name = &to,
530 .msg_namelen = to_len,
531 .msg_iov = &my->iov,
532 .msg_iovlen = 1,
533 .msg_flags = 0,
534 };
535
536 {
537#ifdef IP_PKTINFO
538 struct in_pktinfo *pkt;
539
540 my->msgh.msg_controllen = CMSG_SPACE(sizeof(*pkt));
541
542 cmsg = CMSG_FIRSTHDR(&my->msgh);
543 cmsg->cmsg_level = SOL_IP;
544 cmsg->cmsg_type = IP_PKTINFO;
545 cmsg->cmsg_len = CMSG_LEN(sizeof(*pkt));
546
547 pkt = (struct in_pktinfo *) CMSG_DATA(cmsg);
548 memset(pkt, 0, sizeof(*pkt));
549 pkt->ipi_spec_dst = addr->socket.inet.src_ipaddr.addr.v4;
550 pkt->ipi_ifindex = addr->socket.inet.ifindex;
551
552#elif defined(IP_SENDSRCADDR)
553 struct in_addr *in;
554
555 my->msgh.msg_controllen = CMSG_SPACE(sizeof(*in));
556
557 cmsg = CMSG_FIRSTHDR(&my->msgh);
558 cmsg->cmsg_level = IPPROTO_IP;
559 cmsg->cmsg_type = IP_SENDSRCADDR;
560 cmsg->cmsg_len = CMSG_LEN(sizeof(*in));
561
562 in = (struct in_addr *) CMSG_DATA(cmsg);
563 *in = addr->socket.inet.src_ipaddr.addr.v4;
564#endif
565 }
566
567 do {
568 rcode = sendmsg(my->info.socket.fd, &my->msgh, 0);
569 } while (fr_bio_fd_write_retry(my, &rcode, &tries, size));
570
572
573 return rcode;
574}
575
576static inline int fr_bio_fd_udpfromto_init4(int fd)
577{
578 int proto = 0, flag = 0, opt = 1;
579
580#ifdef HAVE_IP_PKTINFO
581 /*
582 * Linux
583 */
584 proto = SOL_IP;
585 flag = IP_PKTINFO;
586
587#elif defined(IP_RECVDSTADDR)
588 /*
589 * Set the IP_RECVDSTADDR option (BSD). Note:
590 * IP_RECVDSTADDR == IP_SENDSRCADDR
591 */
592 proto = IPPROTO_IP;
593 flag = IP_RECVDSTADDR;
594#endif
595
596 return setsockopt(fd, proto, flag, &opt, sizeof(opt));
597}
598#endif
599
600#if defined(IPV6_PKTINFO)
601/** Read from a UDP socket where we can change our IP, IPv4 version.
602 */
603static ssize_t fr_bio_fd_recvfromto6(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
604{
605 ssize_t rcode;
606 struct cmsghdr *cmsg;
607 fr_time_t when = fr_time_wrap(0);
608 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
610
611 rcode = fr_bio_fd_recvfromto_common(my, packet_ctx, buffer, size);
612 if (rcode <= 0) return rcode;
613
614DIAG_OFF(sign-compare)
615 /* Process auxiliary received data in msgh */
616 for (cmsg = CMSG_FIRSTHDR(&my->msgh);
617 cmsg != NULL;
618 cmsg = CMSG_NXTHDR(&my->msgh, cmsg)) {
619DIAG_ON(sign-compare)
620
621 if ((cmsg->cmsg_level == IPPROTO_IPV6) &&
622 (cmsg->cmsg_type == IPV6_PKTINFO)) {
623 struct in6_pktinfo *i = (struct in6_pktinfo *) CMSG_DATA(cmsg);
624 struct sockaddr_in6 to = {
625 .sin6_family = AF_INET6,
626 .sin6_addr = i->ipi6_addr,
627 .sin6_port = htons(my->info.socket.inet.src_port),
628 .sin6_scope_id = my->info.socket.inet.src_ipaddr.scope_id,
629 };
630
631 (void) fr_ipaddr_from_sockaddr(&addr->socket.inet.dst_ipaddr, &addr->socket.inet.dst_port,
632 (struct sockaddr_storage *) &to, sizeof(struct sockaddr_in6));
633 addr->socket.inet.ifindex = i->ipi6_ifindex;
634 break;
635 }
636
637#ifdef SO_TIMESTAMPNS
638 if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMPNS)) {
639 when = fr_time_from_timespec((struct timespec *)CMSG_DATA(cmsg));
640 }
641
642#elif defined(SO_TIMESTAMP)
643 if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMP)) {
644 when = fr_time_from_timeval((struct timeval *)CMSG_DATA(cmsg));
645 }
646#endif
647 }
648
649 if fr_time_eq(when, fr_time_wrap(0)) when = fr_time();
650
651 addr->when = when;
652
653 return rcode;
654}
655
656/** Send to UDP socket where we can change our IP, IPv6 version.
657 */
658static ssize_t fr_bio_fd_sendfromto6(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size)
659{
660 int tries = 0;
661 ssize_t rcode;
662 struct cmsghdr *cmsg;
663 struct sockaddr_storage to;
664 socklen_t to_len;
665 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
667
668 memset(&my->cbuf, 0, sizeof(my->cbuf));
669 memset(&my->msgh, 0, sizeof(struct msghdr));
670
671 (void) fr_ipaddr_to_sockaddr(&to, &to_len, &addr->socket.inet.dst_ipaddr, addr->socket.inet.dst_port);
672
673 my->iov = (struct iovec) {
674 .iov_base = UNCONST(void *, buffer),
675 .iov_len = size,
676 };
677
678 my->msgh = (struct msghdr) {
679 .msg_control = my->cbuf,
680 // controllen is set below
681 .msg_name = &to,
682 .msg_namelen = to_len,
683 .msg_iov = &my->iov,
684 .msg_iovlen = 1,
685 .msg_flags = 0,
686 };
687
688 {
689 struct in6_pktinfo *pkt;
690
691 my->msgh.msg_controllen = CMSG_SPACE(sizeof(*pkt));
692
693 cmsg = CMSG_FIRSTHDR(&my->msgh);
694 cmsg->cmsg_level = IPPROTO_IPV6;
695 cmsg->cmsg_type = IPV6_PKTINFO;
696 cmsg->cmsg_len = CMSG_LEN(sizeof(*pkt));
697
698 pkt = (struct in6_pktinfo *) CMSG_DATA(cmsg);
699 memset(pkt, 0, sizeof(*pkt));
700 pkt->ipi6_addr = addr->socket.inet.src_ipaddr.addr.v6;
701 pkt->ipi6_ifindex = addr->socket.inet.ifindex;
702 }
703
704 do {
705 rcode = sendmsg(my->info.socket.fd, &my->msgh, 0);
706 } while (fr_bio_fd_write_retry(my, &rcode, &tries, size));
707
709
710 return rcode;
711}
712
713
714static inline int fr_bio_fd_udpfromto_init6(int fd)
715{
716 int opt = 1;
717
718 return setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &opt, sizeof(opt));
719}
720#endif
721
722int fr_filename_to_sockaddr(struct sockaddr_un *sun, socklen_t *sunlen, char const *filename)
723{
724 size_t len;
725
726 len = strlen(filename);
727 if (len >= sizeof(sun->sun_path)) {
728 fr_strerror_const("Failed parsing unix domain socket filename: Name is too long");
729 return -1;
730 }
731
732 sun->sun_family = AF_LOCAL;
733 memcpy(sun->sun_path, filename, len + 1); /* SUN_LEN will do strlen */
734
735 *sunlen = SUN_LEN(sun);
736
737 return 0;
738}
739
741{
742 socklen_t salen;
743 struct sockaddr_storage salocal;
744
745 /*
746 * Already set: do nothing.
747 */
748 if (!fr_ipaddr_is_inaddr_any(&my->info.socket.inet.src_ipaddr) &&
749 (my->info.socket.inet.src_port != 0)) {
750 return 0;
751 }
752
753 /*
754 * FreeBSD jail issues. We bind to 0.0.0.0, but the
755 * kernel instead binds us to a 1.2.3.4. So once the
756 * socket is bound, ask it what it's IP address is.
757 */
758 salen = sizeof(salocal);
759 memset(&salocal, 0, salen);
760 if (getsockname(my->info.socket.fd, (struct sockaddr *) &salocal, &salen) < 0) {
761 fr_strerror_printf("Failed getting socket name: %s", fr_syserror(errno));
762 return -1;
763 }
764
765 if (fr_ipaddr_from_sockaddr(&my->info.socket.inet.src_ipaddr, &my->info.socket.inet.src_port, &salocal, salen) < 0) return -1;
766
767 fr_ipaddr_get_scope_id(&my->info.socket.inet.src_ipaddr);
768 my->info.socket.inet.ifindex = my->info.socket.inet.src_ipaddr.scope_id;
769
770 return 0;
771}
772
774{
776 my->info.eof = false;
777 my->info.read_blocked = false;
778 my->info.write_blocked = false;
779
780 if (my->connect.el) (void) fr_event_fd_delete(my->connect.el, my->info.socket.fd, FR_EVENT_FILTER_IO);
781
782 /*
783 * Run the connect success callback if defined.
784 */
785 if (my->connect.success) my->connect.success(&my->bio);
786
787 /*
788 * Tell the caller that the socket is ready for application data.
789 */
790 if (my->cb.connected) my->cb.connected(&my->bio);
791}
792
793
794/** Try to connect().
795 *
796 * If connect is blocking, we either succeed or error immediately. Otherwise, the caller has to select the
797 * socket for writeability, and then call fr_bio_fd_connect() as soon as the socket is writeable.
798 */
800{
801 int tries = 0;
802 int rcode;
803
804 /*
805 * Save a "sockaddr" version of the address of the other end.
806 */
807 if (my->info.socket.af != AF_LOCAL) {
809 &my->info.socket.inet.dst_ipaddr, my->info.socket.inet.dst_port);
810 } else {
811 rcode = fr_filename_to_sockaddr((struct sockaddr_un *) &my->remote_sockaddr, &my->remote_sockaddr_len,
812 my->info.socket.unix.path);
813 }
814
815 if (rcode < 0) {
816 my->info.connect_errno = errno;
817 (void) fr_bio_shutdown(&my->bio);
818 return fr_bio_error(GENERIC);
819 }
820
822
823retry:
824 if (connect(my->info.socket.fd, (struct sockaddr *) &my->remote_sockaddr, my->remote_sockaddr_len) == 0) {
825 connected:
826 /*
827 * The source IP may have changed, so get the new one.
828 */
829 if (fr_bio_fd_socket_name(my) < 0) goto fail;
830
831 /*
832 * Install the read / write routines, and then call fr_bio_fd_set_open().
833 */
834 if (fr_bio_fd_init_common(my) < 0) goto fail;
835
836 return 0;
837 }
838
839 switch (errno) {
840 /*
841 * When a connect is deferred, the event loop eventually calls us again when the socket
842 * is writable. If the handshake has finished, the second connect() returns EISCONN,
843 * which means that the socket is already connected. That indicates a success.
844 */
845 case EISCONN:
846 goto connected;
847
848 case EINTR:
849 tries++;
850 if (tries <= my->max_tries) goto retry;
852
853 /*
854 * This shouldn't happen, but we'll allow it
855 */
856 case EALREADY:
858
859 /*
860 * Once the socket is writable, it will be active, or in an error state. The caller has
861 * to call fr_bio_fd_connect() before calling write()
862 */
863 case EINPROGRESS:
864 my->info.connect_errno = errno;
865
866 if (!my->info.write_blocked) {
867 my->info.write_blocked = true;
868
869 rcode = fr_bio_write_blocked((fr_bio_t *) my);
870 if (rcode < 0) return rcode;
871 }
872
873 return fr_bio_error(IO_WOULD_BLOCK);
874
875 default:
876 break;
877 }
878
879 /*
880 * There was a fatal error in connect().
881 */
882 my->info.connect_errno = errno;
883 fr_strerror_printf("Failed connecting socket: %s", fr_syserror(errno));
884
885fail:
886 (void) fr_bio_shutdown(&my->bio);
887 return fr_bio_error(IO);
888}
889
890
891/** Files are a special case of connected sockets.
892 *
893 */
895{
896 /*
897 * Other flags may be O_CREAT, etc.
898 */
899 switch (my->info.cfg->flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
900 case O_RDONLY:
901 my->bio.read = fr_bio_fd_read_stream;
902 my->bio.write = fr_bio_fail_write;
903 break;
904
905 case O_WRONLY:
906 my->bio.read = fr_bio_fail_read;
907 my->bio.write = fr_bio_fd_write;
908 break;
909
910 case O_RDWR:
911 my->bio.read = fr_bio_fd_read_stream;
912 my->bio.write = fr_bio_fd_write;
913 break;
914
915 default:
916 fr_strerror_const("Invalid flag for opening file");
917 return -1;
918 }
919
921
922 return 0;
923}
924
925/** This FD isn't connected.
926 *
927 */
929{
930 errno = ENOTCONN;
931
932 return fr_bio_error(IO);
933}
934
935#ifdef SO_NOSIGPIPE
936/** Stop the kernel raising SIGPIPE when a write finds that the peer closed the socket
937 *
938 * Although the server ignores SIGPIPE, some operating systems like BSD and OSX ignore the ignoring.
939 *
940 * Fortunately, those operating systems usually support SO_NOSIGPIPE. We set that to prevent them
941 * raising the signal in the first place.
942 *
943 * Every stream socket needs this, including a socket which the BIO accepted. A TLS BIO can write a
944 * close_notify alert to a peer which has already closed the socket.
945 */
946static void fr_bio_fd_no_sigpipe(int fd)
947{
948 int on = 1;
949
950 (void) setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
951}
952#else
953# define fr_bio_fd_no_sigpipe(_x)
954#endif
955
957{
958 int rcode;
959
960 if (my->info.socket.af == AF_FR_FILENAME) return fr_bio_fd_init_file(my);
961
962 /*
963 * The source IP can be unspecified. It will get updated after we call connect().
964 */
965
966 /*
967 * All connected sockets must have a destination IP.
968 */
969 if (((my->info.socket.af == AF_INET) || (my->info.socket.af == AF_INET6)) &&
970 fr_ipaddr_is_inaddr_any(&my->info.socket.inet.dst_ipaddr)) {
971 fr_strerror_const("Destination IP address cannot be wildcard");
972 return -1;
973 }
974
975 /*
976 * Don't do any reads until we're connected.
977 */
978 my->bio.read = fr_bio_fd_notconn_read;
979 my->bio.write = fr_bio_null_write;
980
981 my->info.eof = false;
982
983 my->info.read_blocked = false;
984 my->info.write_blocked = false;
985
987
988 /*
989 * Call connect() for both synchronous and asynchronous sockets.
990 *
991 * A synchronous socket blocks until the connection succeeds or fails.
992 *
993 * An asynchronous socket kicks off the TCP side of connect(), and the answer will eventually
994 * come back.
995 */
996 rcode = fr_bio_fd_try_connect(my);
997 if (rcode == 0) return 0;
998
999 if (rcode != fr_bio_error(IO_WOULD_BLOCK)) return rcode;
1000
1001 /*
1002 * We got here via EINPROGRESS, so we must be blocked, and in the connecting state.
1003 */
1004 fr_assert(my->info.cfg->async);
1007
1008 return 0;
1009}
1010
1012{
1013 if (my->info.socket.type == SOCK_STREAM) { //!< stream socket
1014 my->bio.read = fr_bio_fd_read_stream;
1015 my->bio.write = fr_bio_fd_write;
1016
1017 } else if (my->info.type == FR_BIO_FD_CONNECTED) { //!< connected datagram
1018 my->bio.read = fr_bio_fd_read_connected_datagram;
1019 my->bio.write = fr_bio_fd_write;
1020
1021 } else if (!fr_ipaddr_is_inaddr_any(&my->info.socket.inet.src_ipaddr)) { //!< we know our IP address
1022 my->bio.read = fr_bio_fd_recvfrom;
1023 my->bio.write = fr_bio_fd_sendto;
1024
1025#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
1026 } else if (my->info.socket.inet.src_ipaddr.af == AF_INET) { //!< we don't know our IPv4
1027 if (fr_bio_fd_udpfromto_init4(my->info.socket.fd) < 0) return -1;
1028
1029 my->bio.read = fr_bio_fd_recvfromto4;
1030 my->bio.write = fr_bio_fd_sendfromto4;
1031#endif
1032
1033#if defined(IPV6_PKTINFO)
1034 } else if (my->info.socket.inet.src_ipaddr.af == AF_INET6) { //!< we don't know our IPv6
1035
1036 if (fr_bio_fd_udpfromto_init6(my->info.socket.fd) < 0) return -1;
1037
1038 my->bio.read = fr_bio_fd_recvfromto6;
1039 my->bio.write = fr_bio_fd_sendfromto6;
1040#endif
1041
1042 } else {
1043 fr_strerror_const("Failed initializing socket: cannot determine what to do");
1044 return -1;
1045 }
1046
1048
1049 return 0;
1050}
1051
1053{
1054 /*
1055 * A listening socket supports neither read nor write, so both routines return an error. The
1056 * caller calls fr_bio_fd_accept() to get each new connection.
1057 */
1058 my->bio.read = fr_bio_fail_read;
1059 my->bio.write = fr_bio_fail_write;
1060
1061 /*
1062 * A datagram socket has no queue of connections, so there is nothing to listen for.
1063 * fr_bio_fd_accept_datagram() reads the datagrams.
1064 */
1065 /*
1066 * fr_bio_fd_accept_datagram() reads each datagram into this buffer, which is allocated once.
1067 */
1068 if ((my->info.socket.type == SOCK_DGRAM) && !my->accept_buffer) {
1069 my->accept_buffer = talloc_array(my, uint8_t, FR_BIO_FD_MAX_DATAGRAM);
1070 if (!my->accept_buffer) return fr_bio_error(OOM);
1071 }
1072
1073 /*
1074 * A datagram listener on a wildcard address needs the destination address of each datagram,
1075 * so that fr_bio_fd_accept_datagram() can bind the new socket to that address.
1076 */
1077 if ((my->info.socket.type == SOCK_DGRAM) && fr_ipaddr_is_inaddr_any(&my->info.socket.inet.src_ipaddr)) {
1078#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
1079 if ((my->info.socket.inet.src_ipaddr.af == AF_INET) &&
1080 (fr_bio_fd_udpfromto_init4(my->info.socket.fd) < 0)) {
1081 fr_strerror_printf("Failed enabling destination addresses: %s", fr_syserror(errno));
1082 return fr_bio_error(IO);
1083 }
1084#endif
1085
1086#if defined(IPV6_PKTINFO)
1087 if ((my->info.socket.inet.src_ipaddr.af == AF_INET6) &&
1088 (fr_bio_fd_udpfromto_init6(my->info.socket.fd) < 0)) {
1089 fr_strerror_printf("Failed enabling destination addresses: %s", fr_syserror(errno));
1090 return fr_bio_error(IO);
1091 }
1092#endif
1093 }
1094
1095 if ((my->info.socket.type == SOCK_STREAM) &&
1096 (listen(my->info.socket.fd, my->info.cfg->backlog_is_set ? my->info.cfg->backlog : 8) < 0)) {
1097 fr_strerror_printf("Failed calling listen() %s", fr_syserror(errno));
1098 return fr_bio_error(IO);
1099 }
1100
1102
1103 return 0;
1104}
1105
1106/** Allocate a FD bio
1107 *
1108 * The caller is responsible for tracking the FD, and all associated management of it. The bio API is
1109 * intended to be simple, and does not provide wrapper functions for various ioctls. The caller should
1110 * instead do that work.
1111 *
1112 * Once the FD is give to the bio, its lifetime is "owned" by the bio. Calling talloc_free(bio) will close
1113 * the FD.
1114 *
1115 * The caller can still manage the FD for being readable / writeable. However, the caller should not call
1116 * this bio directly (unless it is the only one). Instead, the caller should read from / write to the
1117 * previous bio which will then eventually call this one.
1118 *
1119 * Before updating any event handler readable / writeable callbacks, the caller should check
1120 * fr_bio_fd_at_eof(). If true, then the handlers should not be inserted. The previous bios should still be
1121 * called to process any pending data, until they return EOF.
1122 *
1123 * The main purpose of an FD bio is to wrap the FD in a bio container. That, and handling retries on read /
1124 * write, along with returning EOF as an error instead of zero.
1125 *
1126 * Note that the read / write functions can return partial data. It is the callers responsibility to ensure
1127 * that any writes continue from where they left off (otherwise dat awill be missing). And any partial reads
1128 * should go to a memory bio.
1129 *
1130 * If a read returns EOF, then the FD remains open until talloc_free(bio) or fr_bio_fd_close() is called.
1131 *
1132 * The bio caches the cfg pointer, so the configuration has to live at least as long as the bio does.
1133 * See fr_bio_fd_open().
1134 *
1135 * @param ctx the talloc ctx
1136 * @param cfg structure holding the configuration information
1137 * @param offset only for unconnected datagram sockets, where #fr_bio_fd_packet_ctx_t is stored
1138 * @return
1139 * - NULL on error, memory allocation failed
1140 * - !NULL the bio
1141 */
1142fr_bio_t *fr_bio_fd_alloc(TALLOC_CTX *ctx, fr_bio_fd_config_t const *cfg, size_t offset)
1143{
1144 fr_bio_fd_t *my;
1145
1146 my = talloc_zero(ctx, fr_bio_fd_t);
1147 if (!my) return NULL;
1148
1149 my->max_tries = 4;
1150 my->offset = offset;
1151
1152 if (!cfg) {
1153 /*
1154 * Add place-holder information.
1155 */
1156 my->info = (fr_bio_fd_info_t) {
1157 .socket = {
1158 .af = AF_UNSPEC,
1159 .fd = -1,
1160 },
1161 .type = FR_BIO_FD_UNCONNECTED,
1162 .read_blocked = false,
1163 .write_blocked = false,
1164 .eof = false,
1165 .state = FR_BIO_FD_STATE_CLOSED,
1166 };
1167
1168 my->bio.read = fr_bio_null_read;
1169 my->bio.write = fr_bio_null_write;
1170 } else {
1172
1173 if (fr_bio_fd_open(&my->bio, cfg) < 0) {
1174 talloc_free(my);
1175 return NULL;
1176 }
1177 }
1178
1179 my->priv_cb.eof = fr_bio_fd_eof;
1180 my->priv_cb.write_resume = fr_bio_fd_write_resume;
1181 my->priv_cb.shutdown = fr_bio_fd_shutdown;
1182
1183 talloc_set_destructor((fr_bio_t *) my, fr_bio_destructor); /* always use a common destructor */
1184 return (fr_bio_t *) my;
1185}
1186
1187/** Close the FD, but leave the bio allocated and alive.
1188 *
1189 */
1191{
1192 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
1193
1194 if (my->info.state == FR_BIO_FD_STATE_CLOSED) return 0;
1195
1196 my->bio.read = fr_bio_shutdown_read;
1197 my->bio.write = fr_bio_shutdown_write;
1198
1199 /*
1200 * Shut down the connected socket. The only errors possible here are things we can't do anything
1201 * about.
1202 *
1203 * shutdown() will close ALL versions of this file descriptor, even if it's (somehow) used in
1204 * another process. shutdown() will also tell the kernel to gracefully close the connected
1205 * socket, so that it can signal the other end, instead of having the connection disappear.
1206 *
1207 * This shouldn't strictly be necessary, as no other processes should be sharing this file
1208 * descriptor. But it's the safe (and polite) thing to do.
1209 */
1211 (void) shutdown(my->info.socket.fd, SHUT_RDWR);
1212 }
1213
1214 if (my->info.socket.fd >= 0) (void) close(my->info.socket.fd);
1215
1217 my->info.read_blocked = true;
1218 my->info.write_blocked = true;
1219 my->info.eof = true;
1220 my->info.socket.fd = -1;
1221
1222 return 0;
1223}
1224
1225/** FD error when trying to connect, give up on the BIO.
1226 *
1227 */
1228static void fr_bio_fd_el_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
1229{
1230 fr_bio_fd_t *my = talloc_get_type_abort(uctx, fr_bio_fd_t);
1231
1232 my->info.connect_errno = fd_errno;
1233
1234 /*
1235 * The entire bio is unusable.
1236 */
1238 (void) fr_bio_shutdown(&my->bio);
1239 }
1240
1241 /*
1242 * It's possible that the bio may be freed in the error callback,
1243 * especially if it signals a connection to reconnect, potentially
1244 * feeing the original connection handle.
1245 */
1246 if (my->connect.error) {
1247 my->connect.error(&my->bio);
1248 }
1249}
1250
1251/** Connect callback for when the socket is writable.
1252 *
1253 * We try to connect the socket, and if so, call the application which should update the BIO status.
1254 */
1256{
1257 int rcode;
1258 fr_bio_fd_t *my = talloc_get_type_abort(uctx, fr_bio_fd_t);
1259
1262 fr_assert(my->connect.el == el); /* and not NULL */
1263 fr_assert(my->connect.success != NULL);
1264 fr_assert(my->info.socket.fd == fd);
1265
1266#ifndef NDEBUG
1267 /*
1268 * This check shouldn't be necessary, as we have a kqeueue error callback. That should be called
1269 * when there's a connect error.
1270 */
1271 {
1272 int error;
1273 socklen_t socklen = sizeof(error);
1274
1275 /*
1276 * The socket is writeable. Let's see if there's an error.
1277 *
1278 * Unix Network Programming says:
1279 *
1280 * ""If so_error is nonzero when the process calls write, -1 is returned with errno set to the
1281 * value of SO_ERROR (p. 495 of TCPv2) and SO_ERROR is reset to 0. We have to check for the
1282 * error, and if there's no error, set the state to "open". ""
1283 *
1284 * The same applies to connect(). If a non-blocking connect returns INPROGRESS, it may later
1285 * become writable. It will be writable even if the connection fails. Rather than writing some
1286 * random application data, we call SO_ERROR, and get the underlying error.
1287 */
1288 if (getsockopt(my->info.socket.fd, SOL_SOCKET, SO_ERROR, (void *)&error, &socklen) < 0) {
1289 fr_bio_fd_el_error(el, fd, flags, errno, uctx);
1290 return;
1291 }
1292
1293 /*
1294 * There was an error, we call the error handler.
1295 */
1296 if (error) {
1297 fr_bio_fd_el_error(el, fd, flags, error, uctx);
1298 return;
1299 }
1300 }
1301#endif
1302
1303 /*
1304 * Try to connect it. Any magic handling is done in the callbacks.
1305 *
1306 * On WOULD_BLOCK, leave the timers, etc. open.
1307 *
1308 * Otherwise the try_connect() function has shut down the BIO.
1309 */
1310 rcode = fr_bio_fd_try_connect(my);
1311 if (rcode == fr_bio_error(IO_WOULD_BLOCK)) return;
1312
1313 if (rcode < 0) {
1314 FR_TIMER_DELETE(&my->connect.ev);
1315 my->connect.el = NULL;
1316
1317 /*
1318 * Tell the application that the connect failed.
1319 *
1320 * fr_bio_fd_try_connect() has already recorded connect_errno and shut the bio down,
1321 * and the shutdown deleted the timer and the FD event. Reporting the failure is all
1322 * that is left, and without it a failed connect here is silent: the synchronous path
1323 * in fr_bio_fd_connect_full() reports it, and this path did not.
1324 *
1325 * Whether this runs depends on which event the event loop delivers. A connect which
1326 * fails usually arrives as EV_EOF, which event.c turns into the error callback, and
1327 * fr_bio_fd_el_error() reports it. This function runs when the socket is merely
1328 * writeable, and nothing else reports the failure for us.
1329 *
1330 * The callback may free the bio, so nothing below touches `my`.
1331 */
1332 if (my->connect.error) my->connect.error(&my->bio);
1333 return;
1334 }
1335
1336 /*
1337 * my->connect.success was already called from fr_bio_fd_set_open().
1338 */
1339
1340 FR_TIMER_DELETE(&my->connect.ev);
1341 my->connect.el = NULL;
1342}
1343
1344/** We have a timeout on the conenction
1345 *
1346 */
1348{
1349 fr_bio_fd_t *my = talloc_get_type_abort(uctx, fr_bio_fd_t);
1350
1351 fr_assert(my->connect.timeout);
1352
1353 my->connect.timeout(&my->bio);
1354
1355 (void) fr_bio_shutdown(&my->bio);
1356}
1357
1358
1359/** Finalize a connect()
1360 *
1361 * connect() said "come back when the socket is writeable". It's now writeable, so we check if there was a
1362 * connection error.
1363 *
1364 * @param bio the binary IO handler
1365 * @param el the event list
1366 * @param connected_cb callback to run when the BIO is connected
1367 * @param error_cb callback to run when the FD has an error
1368 * @param timeout when to time out the connect() attempt
1369 * @param timeout_cb to call when the timeout runs.
1370 * @return
1371 * - <0 on error
1372 * - 0 for "try again later". If callbacks are set, the callbacks will try again. Otherwise the application has to try again.
1373 * - 1 for "we are now connected".
1374 */
1376 fr_bio_callback_t error_cb,
1377 fr_time_delta_t *timeout, fr_bio_callback_t timeout_cb)
1378{
1379 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
1380
1381 /*
1382 * We shouldn't be connected an unconnected socket.
1383 */
1384 if (my->info.type == FR_BIO_FD_UNCONNECTED) {
1385 error:
1386#ifdef ECONNABORTED
1387 my->info.connect_errno = ECONNABORTED;
1388#else
1389 my->info.connect_errno = ECONNREFUSED;
1390#endif
1391 if (error_cb) error_cb(bio);
1392 (void) fr_bio_shutdown(&my->bio);
1393 return fr_bio_error(GENERIC);
1394 }
1395
1396 /*
1397 * The initial open may have succeeded in connecting the socket. In which case we just run the
1398 * callbacks and return.
1399 */
1400 if (my->info.state == FR_BIO_FD_STATE_OPEN) {
1401 connected:
1402 if (connected_cb) connected_cb(bio);
1403
1404 return 1;
1405 }
1406
1407 /*
1408 * The caller may just call us without caring about what the underlying BIO is. In which case we
1409 * need to be safe.
1410 */
1411 if ((my->info.socket.af == AF_FR_FILENAME) || (my->info.type == FR_BIO_FD_LISTEN)) {
1413 goto connected;
1414 }
1415
1416 /*
1417 * It must be in the connecting state, i.e. not INVALID or CLOSED.
1418 */
1419 if (my->info.state != FR_BIO_FD_STATE_CONNECTING) goto error;
1420
1421 /*
1422 * No callback
1423 */
1424 if (!connected_cb) {
1425 int rcode;
1426
1427 rcode = fr_bio_fd_try_connect(my);
1428 if (rcode < 0) {
1429 if (error_cb) error_cb(bio);
1430 return rcode; /* it already called shutdown */
1431 }
1432
1433 return 1;
1434 }
1435
1436 /*
1437 * It's not connected, the caller has to try again.
1438 */
1439 if (!el) return 0;
1440
1441 /*
1442 * Set the callbacks to run when something happens.
1443 */
1444 my->connect.success = connected_cb;
1445 my->connect.error = error_cb;
1446 my->connect.timeout = timeout_cb;
1447
1448 /*
1449 * Set the timeout callback if asked.
1450 */
1451 if (timeout_cb) {
1452 if (fr_timer_in(my, el->tl, &my->connect.ev, *timeout, false, fr_bio_fd_el_timeout, my) < 0) {
1453 goto error;
1454 }
1455 }
1456
1457 /*
1458 * Set the FD callbacks, and tell the caller that we're not connected.
1459 */
1460 if (fr_event_fd_insert(my, NULL, el, my->info.socket.fd, NULL,
1462 goto error;
1463 }
1464 my->connect.el = el;
1465
1466 return 0;
1467}
1468
1469/** Returns a pointer to the bio-specific information.
1470 *
1471 */
1473{
1474 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
1475
1476 return &my->info;
1477}
1478
1479
1480/** Discard all reads from a UDP socket.
1481 */
1483{
1484 int tries = 0;
1485 ssize_t rcode;
1486 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
1487
1488 do {
1489 rcode = read(my->info.socket.fd, buffer, size);
1490 if (rcode >= 0) return 0; /* always return that we read no data */
1491 } while (fr_bio_fd_errno_retry(my, &rcode, &tries, false));
1492
1493 return rcode;
1494}
1495
1496/** Discard all reads from a TCP socket.
1497 */
1499{
1500 int tries = 0;
1501 ssize_t rcode;
1502 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
1503
1504 do {
1505 rcode = read(my->info.socket.fd, buffer, size);
1506 if (rcode > 0 ) return 0; /* always return that we read no data */
1507 if (rcode == 0) {
1508 fr_bio_eof(bio);
1509 return 0;
1510 }
1511 } while (fr_bio_fd_errno_retry(my, &rcode, &tries, false));
1512
1513 return rcode;
1514}
1515
1516/** Mark up a bio as write-only
1517 *
1518 */
1520{
1521 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
1522
1523 switch (my->info.type) {
1524 case FR_BIO_FD_INVALID:
1525 return -1;
1526
1528 if (my->info.socket.type != SOCK_DGRAM) {
1529 fr_strerror_const("Only datagram sockets can be marked 'write-only'");
1530 return -1;
1531 }
1532 goto set_recv_buff_zero;
1533
1535 /*
1536 * Further reads are disallowed. However, this likely has no effect for UDP sockets.
1537 */
1539
1540 /*
1541 * Allow multiple shutdown().
1542 */
1543 if ((shutdown(my->info.socket.fd, SHUT_RD) < 0) && (errno != ENOTCONN)) {
1544 fr_strerror_printf("Failed shutting down connected socket - %s", fr_syserror(errno));
1545 return -1;
1546 }
1547
1548 set_recv_buff_zero:
1549#ifdef __linux__
1550#ifdef SO_RCVBUF
1551 /*
1552 * On Linux setting the receive buffer to zero has the effect of discarding all incoming
1553 * data in the kernel. With macOS and others it's an invalid value.
1554 */
1555 {
1556 int opt = 0;
1557
1558 if (setsockopt(my->info.socket.fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) < 0) {
1559 fr_strerror_printf("Failed setting SO_RCVBUF: %s", fr_syserror(errno));
1560 return -1;
1561 }
1562 }
1563#endif
1564#endif
1565 break;
1566
1567 case FR_BIO_FD_LISTEN:
1568 fr_strerror_const("Only unconnected sockets can be marked 'write-only'");
1569 return -1;
1570 }
1571
1572 /*
1573 * No matter what the possibilities above, we replace the read function with a "discard"
1574 * function.
1575 */
1576 my->bio.read = (my->info.socket.type == SOCK_DGRAM) ? fr_bio_fd_read_discard_datagram : fr_bio_fd_read_discard_stream;
1577 return 0;
1578}
1579
1580#if !defined(__linux__) && !defined(__FreeBSD__)
1581static int inline accept4(int fd, struct sockaddr *sockaddr, socklen_t *salen, UNUSED int flags)
1582{
1583 fd = accept(fd, sockaddr, salen);
1584 if (fd >= 0) {
1585 if ((fr_nonblock(fd) < 0) || (fr_cloexec(fd) < 0)) {
1586 int our_errno = errno;
1587
1588 /*
1589 * The caller reads errno to decide whether to
1590 * retry, and close() may overwrite it.
1591 */
1592 close(fd);
1593 errno = our_errno;
1594 return -1;
1595 }
1596 }
1597
1598 return fd;
1599}
1600#ifndef SOCK_NONBLOCK
1601#define SOCK_NONBLOCK 0
1602#endif
1603
1604#ifndef SOCK_CLOEXEC
1605#define SOCK_CLOEXEC 0
1606#endif
1607#endif
1608
1609/** Accept a stream socket and initialize its flags.
1610 *
1611 */
1613{
1614 int fd, tries = 0;
1615
1616 my->remote_sockaddr_len = sizeof(my->remote_sockaddr);
1617
1618retry:
1619 /*
1620 * Set these flags immediately on the new socket.
1621 */
1622 fd = accept4(parent->info.socket.fd, (struct sockaddr *) &my->remote_sockaddr, &my->remote_sockaddr_len,
1624 if (fd < 0) {
1625 switch (errno) {
1626#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1627 case EWOULDBLOCK:
1628#endif
1629 case EAGAIN:
1630 return 0;
1631
1632 /*
1633 * Try a few times before giving up.
1634 */
1635 case EINTR:
1636 tries++;
1637 if (tries <= my->max_tries) goto retry;
1638 break;
1639
1640 default:
1641 break;
1642 }
1643
1644 return fr_bio_error(IO);
1645 }
1646
1647 /*
1648 * The connection is from a source we don't accept. Close it.
1649 *
1650 * @todo - log message, increment counter, etc.
1651 */
1652 if (parent->trie &&
1653 !fr_bio_network_trie_allow(parent->trie, (struct sockaddr *) &my->remote_sockaddr, my->remote_sockaddr_len)) {
1654 close(fd);
1655 return 0;
1656 }
1657
1658 /*
1659 * The socket is now open. Save the new state.
1660 */
1661 my->info.socket.fd = fd;
1662
1663 return 1;
1664}
1665
1666
1667/** Receive a datagram, and find the address which the datagram was sent to
1668 *
1669 * A wildcard listener asks the kernel for the destination address of each datagram, which arrives as a
1670 * control message. A listener bound to one address has no need of the control message, as every datagram
1671 * was sent to that address.
1672 *
1673 * @param[in] parent the listening bio.
1674 * @param[in] buffer of #FR_BIO_FD_MAX_DATAGRAM octets.
1675 * @param[in] flags for recvmsg().
1676 * @param[out] from the source of the datagram.
1677 * @param[out] from_len length of the source.
1678 * @param[out] dst the destination of the datagram. `af` is AF_UNSPEC when the kernel did not
1679 * say.
1680 * @return
1681 * - >=0 size of the datagram.
1682 * - <0 on error, with `errno` set.
1683 */
1685 struct sockaddr_storage *from, socklen_t *from_len, fr_ipaddr_t *dst)
1686{
1687 ssize_t slen;
1688 struct iovec iov = { .iov_base = buffer, .iov_len = FR_BIO_FD_MAX_DATAGRAM };
1689 uint8_t cbuf[CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(struct timespec))];
1690 struct msghdr msgh = {
1691 .msg_control = cbuf,
1692 .msg_controllen = sizeof(cbuf),
1693 .msg_name = from,
1694 .msg_namelen = sizeof(*from),
1695 .msg_iov = &iov,
1696 .msg_iovlen = 1,
1697 };
1698 struct cmsghdr *cmsg;
1699
1700 *dst = (fr_ipaddr_t) { .af = AF_UNSPEC };
1701
1702 slen = recvmsg(parent->info.socket.fd, &msgh, flags);
1703 if (slen < 0) return slen;
1704
1705 *from_len = msgh.msg_namelen;
1706
1707 /*
1708 * A listener bound to one address.
1709 */
1710 if (!fr_ipaddr_is_inaddr_any(&parent->info.socket.inet.src_ipaddr)) {
1711 *dst = parent->info.socket.inet.src_ipaddr;
1712 return slen;
1713 }
1714
1715DIAG_OFF(sign-compare)
1716 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
1717DIAG_ON(sign-compare)
1718 struct sockaddr_storage to;
1719
1720#ifdef IP_PKTINFO
1721 if ((cmsg->cmsg_level == SOL_IP) && (cmsg->cmsg_type == IP_PKTINFO)) {
1722 struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
1723
1724 memset(&to, 0, sizeof(to));
1725 ((struct sockaddr_in *) &to)->sin_family = AF_INET;
1726 ((struct sockaddr_in *) &to)->sin_addr = i->ipi_addr;
1727 if (fr_ipaddr_from_sockaddr(dst, NULL, &to, sizeof(struct sockaddr_in)) == 0) break;
1728 }
1729#endif
1730
1731#ifdef IP_RECVDSTADDR
1732 if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_RECVDSTADDR)) {
1733 struct in_addr *i = (struct in_addr *) CMSG_DATA(cmsg);
1734
1735 memset(&to, 0, sizeof(to));
1736 ((struct sockaddr_in *) &to)->sin_family = AF_INET;
1737 ((struct sockaddr_in *) &to)->sin_addr = *i;
1738 if (fr_ipaddr_from_sockaddr(dst, NULL, &to, sizeof(struct sockaddr_in)) == 0) break;
1739 }
1740#endif
1741
1742#ifdef IPV6_PKTINFO
1743 if ((cmsg->cmsg_level == IPPROTO_IPV6) && (cmsg->cmsg_type == IPV6_PKTINFO)) {
1744 struct in6_pktinfo *i = (struct in6_pktinfo *) CMSG_DATA(cmsg);
1745
1746 memset(&to, 0, sizeof(to));
1747 ((struct sockaddr_in6 *) &to)->sin6_family = AF_INET6;
1748 ((struct sockaddr_in6 *) &to)->sin6_addr = i->ipi6_addr;
1749 ((struct sockaddr_in6 *) &to)->sin6_scope_id = i->ipi6_ifindex;
1750 if (fr_ipaddr_from_sockaddr(dst, NULL, &to, sizeof(struct sockaddr_in6)) == 0) break;
1751 }
1752#endif
1753 }
1754
1755 return slen;
1756}
1757
1758/** Accept a "connection" on a datagram listening socket
1759 *
1760 * This function is really only useful for DTLS. A datagram socket has no connections, so a connection is
1761 * made by taking the source IP and port of the first datagram, and opening a new socket which is connected
1762 * to that peer. The kernel then delivers the datagrams of that peer to the new socket in preference to the
1763 * listening socket. This only works when the peer sends one stream of datagrams. Other UDP applications
1764 * can have several datagrams from the same source IP waiting in the listening socket, and those datagrams
1765 * stay in the listening socket.
1766 *
1767 * The new bio is a copy of the listening bio, except that it is a connected socket. It has its own file
1768 * descriptor, as connect() is a property of the socket and not of the descriptor, so dup() would connect the
1769 * listening socket. The new socket needs SO_REUSEPORT, as the new socket is bound to the address which the
1770 * datagram was sent to, and the port of the listening socket. That address can be more specific than the
1771 * address of the listening socket, when the listening socket is bound to a wildcard.
1772 *
1773 * The datagram is read directly into a buffer which belongs to the listening bio, and which is allocated
1774 * once when the listening bio opens. The one read gives the datagram, the source, and the destination. The
1775 * datagram is out of the socket before the new socket exists, so if the new socket cannot be opened, the
1776 * datagram is lost. The peer retransmits, as a lost datagram looks like any other lost datagram.
1777 *
1778 * The datagram which named the peer is saved in the new bio. The first read of the new bio returns the
1779 * saved datagram. The datagram is no longer in the kernel, so no event loop will tell the application that
1780 * the new socket is readable. `info.read_blocked` is false, and the application MUST call fr_bio_read()
1781 * once after fr_bio_fd_accept() returns, and then it can wait for events as usual.
1782 *
1783 * @param[in] ctx to allocate the new bio in.
1784 * @param[out] out_p the new bio.
1785 * @param[in] parent the listening datagram bio.
1786 * @return
1787 * - <0 on error
1788 * - 0 for "no datagram is waiting"
1789 * - 1 for "the new bio is available"
1790 */
1791static int fr_bio_fd_accept_datagram(TALLOC_CTX *ctx, fr_bio_t **out_p, fr_bio_fd_t *parent)
1792{
1794 fr_bio_fd_config_t *cfg;
1795 struct sockaddr_storage peer;
1796 socklen_t peer_len;
1797 fr_ipaddr_t peer_ipaddr, dst_ipaddr;
1798 uint16_t peer_port;
1799 ssize_t slen;
1800 int tries = 0;
1801 int rcode;
1802
1803 fr_assert(parent->info.socket.type == SOCK_DGRAM);
1804 fr_assert(parent->accept_buffer != NULL);
1805
1806retry:
1807 /*
1808 * Read the datagram, with its source and destination. The buffer is big enough for any
1809 * datagram, so the return value is the real size.
1810 */
1811 slen = fr_bio_fd_recvmsg_dst(parent, parent->accept_buffer, 0, &peer, &peer_len, &dst_ipaddr);
1812 if (slen < 0) {
1813 switch (errno) {
1814#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1815 case EWOULDBLOCK:
1816#endif
1817 case EAGAIN:
1818 return 0;
1819
1820 case EINTR:
1821 tries++;
1822 if (tries <= parent->max_tries) goto retry;
1823 break;
1824
1825 default:
1826 break;
1827 }
1828
1829 fr_strerror_printf("Failed reading the first datagram: %s", fr_syserror(errno));
1830 return fr_bio_error(IO);
1831 }
1832
1833 if (dst_ipaddr.af == AF_UNSPEC) {
1834 fr_strerror_const("Cannot find the address which the first datagram was sent to");
1835 return fr_bio_error(GENERIC);
1836 }
1837
1838 if (fr_ipaddr_from_sockaddr(&peer_ipaddr, &peer_port, &peer, peer_len) < 0) {
1839 fr_strerror_const("Failed getting the address of the peer");
1840 return fr_bio_error(GENERIC);
1841 }
1842
1843 FR_BIO_FD_NETWORK_ALLOW(parent, peer, peer_len);
1844
1845 out = (fr_bio_fd_t *) fr_bio_fd_alloc(ctx, NULL, parent->offset);
1846 if (!out) return fr_bio_error(OOM);
1847
1848 /*
1849 * The configuration of the new bio belongs to the new bio. It is the configuration of the
1850 * listening bio, except that the new bio is connected to the peer, and bound to the address
1851 * which the datagram was sent to.
1852 */
1853 cfg = talloc_zero(out, fr_bio_fd_config_t);
1854 if (!cfg) {
1856 return fr_bio_error(OOM);
1857 }
1858
1859 *cfg = *parent->info.cfg;
1861 cfg->trie = NULL;
1862 cfg->server = false;
1863 cfg->reuse_port = true;
1864 cfg->src_ipaddr = dst_ipaddr;
1865 cfg->dst_ipaddr = peer_ipaddr;
1866 cfg->dst_port = peer_port;
1867
1868 /*
1869 * Open a new UDP socket with SO_REUSEPORT, bind it, and connect it to the peer.
1870 */
1871 rcode = fr_bio_fd_open(&out->bio, cfg);
1872 if (rcode < 0) {
1874 return rcode;
1875 }
1876
1877 /*
1878 * The buffer belongs to the listening bio, so the new bio gets a copy.
1879 */
1880 out->first_datagram = talloc_memdup(out, parent->accept_buffer, slen ? (size_t) slen : 1);
1881 if (!out->first_datagram) {
1883 return fr_bio_error(OOM);
1884 }
1885 out->first_datagram_len = (size_t) slen;
1886
1887 /*
1888 * The first read returns the saved datagram. The saved datagram is not in the socket, so the
1889 * application has to read the bio without waiting for an event.
1890 */
1892 out->info.read_blocked = false;
1893
1894 *out_p = (fr_bio_t *) out;
1895 return 1;
1896}
1897
1898/** Accept a new connection on a socket.
1899 *
1900 */
1901int fr_bio_fd_accept(TALLOC_CTX *ctx, fr_bio_t **out_p, fr_bio_t *bio)
1902{
1903 int rcode;
1904 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
1906
1907 *out_p = NULL;
1908
1910
1911 if (my->info.socket.type == SOCK_DGRAM) return fr_bio_fd_accept_datagram(ctx, out_p, my);
1912
1913 /*
1914 * Allocate the base BIO and set it up.
1915 */
1916 out = (fr_bio_fd_t *) fr_bio_fd_alloc(ctx, NULL, my->offset);
1917 if (!out) return fr_bio_error(OOM);
1918
1919 /*
1920 * Initialize the new BIO information.
1921 */
1922 out->info.cfg = my->info.cfg;
1923
1924 /*
1925 * Copy all socket fields, including ones which will be over-written later.
1926 */
1927 out->info.socket = my->info.socket;
1928 out->info.socket.fd = -1;
1929 out->info.type = FR_BIO_FD_CONNECTED;
1930
1931 /*
1932 * Accept the socket:
1933 *
1934 * - <0 for error
1935 * - 0 for "nothing to do", typically because of EAGAIN.
1936 * - 1 for "we have a new socket"
1937 */
1938 fr_assert(my->info.socket.type == SOCK_STREAM);
1939 rcode = fr_bio_fd_accept_stream(out, my);
1940 if (rcode <= 0) {
1942 return rcode;
1943 }
1944
1945 /*
1946 * If it's an IP socket, update the remote_sockaddr.
1947 */
1948 if ((out->info.socket.af == AF_INET) || (out->info.socket.af == AF_INET6)) {
1949 fr_ipaddr_from_sockaddr(&out->info.socket.inet.dst_ipaddr, &out->info.socket.inet.dst_port,
1950 &out->remote_sockaddr, out->remote_sockaddr_len);
1951 }
1952
1953 rcode = fr_bio_fd_init_common(out);
1954 if (rcode < 0) {
1955 fail:
1956 talloc_free(out); /* the destructor closes the FD */
1957 return rcode;
1958 }
1959
1960 fr_bio_fd_no_sigpipe(out->info.socket.fd);
1961
1963 if (!out->info.name) {
1964 rcode = fr_bio_error(OOM);
1965 goto fail;
1966 }
1967
1968 *out_p = (fr_bio_t *) out;
1969 return 1;
1970}
static int const char char buffer[256]
Definition acutest.h:641
fr_bio_write_t _CONST write
write to the underlying bio
Definition base.h:117
fr_bio_read_t _CONST read
read from the underlying bio
Definition base.h:116
static ssize_t fr_bio_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
Write raw data to a bio.
Definition base.h:184
static fr_bio_t * fr_bio_head(fr_bio_t *bio)
Definition base.h:140
void(* fr_bio_callback_t)(fr_bio_t *bio)
Definition base.h:86
#define fr_bio_error(_x)
Definition base.h:200
bool fr_bio_network_trie_allow(fr_trie_t const *trie, struct sockaddr const *sockaddr, socklen_t socklen)
Check if the source of a packet or connection is allowed by the network trie.
Definition network.c:39
static int packet_ctx
the tests pass only the address of packet_ctx
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:203
#define NDEBUG_UNUSED
Definition build.h:426
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:422
#define DIAG_ON(_x)
Definition build.h:582
#define UNUSED
Definition build.h:415
#define DIAG_OFF(_x)
Definition build.h:581
#define fr_event_fd_insert(...)
Definition event.h:251
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:87
int fr_bio_fd_connect_full(fr_bio_t *bio, fr_event_list_t *el, fr_bio_callback_t connected_cb, fr_bio_callback_t error_cb, fr_time_delta_t *timeout, fr_bio_callback_t timeout_cb)
Finalize a connect()
Definition fd.c:1375
static ssize_t fr_bio_fd_notconn_read(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void *buffer, UNUSED size_t size)
This FD isn't connected.
Definition fd.c:928
static ssize_t fr_bio_fd_write(fr_bio_t *bio, UNUSED void *packet_ctx, const void *buffer, size_t size)
Write to fd.
Definition fd.c:316
static ssize_t fr_bio_fd_read_first_datagram(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Return the datagram which fr_bio_fd_accept() read from the listening socket.
Definition fd.c:238
#define FR_BIO_FD_NETWORK_ALLOW(_my, _sockaddr, _socklen)
Check if the packet is allowed.
Definition fd.c:266
int fr_bio_fd_socket_name(fr_bio_fd_t *my)
Definition fd.c:740
static int fr_bio_fd_try_connect(fr_bio_fd_t *my)
Try to connect().
Definition fd.c:799
static void fr_bio_fd_el_connect(NDEBUG_UNUSED fr_event_list_t *el, NDEBUG_UNUSED int fd, NDEBUG_UNUSED int flags, void *uctx)
Connect callback for when the socket is writable.
Definition fd.c:1255
#define ADDR_INIT
Definition fd.c:87
static int fr_bio_fd_write_resume(fr_bio_t *bio)
Definition fd.c:145
fr_bio_t * fr_bio_fd_alloc(TALLOC_CTX *ctx, fr_bio_fd_config_t const *cfg, size_t offset)
Allocate a FD bio.
Definition fd.c:1142
static ssize_t fr_bio_fd_read_discard_stream(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Discard all reads from a TCP socket.
Definition fd.c:1498
static int accept4(int fd, struct sockaddr *sockaddr, socklen_t *salen, UNUSED int flags)
Definition fd.c:1581
int fr_bio_fd_init_connected(fr_bio_fd_t *my)
Definition fd.c:956
static ssize_t fr_bio_fd_read_connected_datagram(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Connected datagram read.
Definition fd.c:199
static ssize_t fr_bio_fd_recvfrom(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Read from a UDP socket where we know our IP.
Definition fd.c:277
#define fr_bio_fd_no_sigpipe(_x)
Definition fd.c:953
#define SOL_IP
Definition fd.c:36
static int fr_bio_fd_eof(fr_bio_t *bio)
Definition fd.c:124
static ssize_t fr_bio_fd_sendto(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size)
Write to a UDP socket where we know our IP.
Definition fd.c:351
int fr_filename_to_sockaddr(struct sockaddr_un *sun, socklen_t *sunlen, char const *filename)
Definition fd.c:722
int fr_bio_fd_init_listen(fr_bio_fd_t *my)
Definition fd.c:1052
int fr_bio_fd_accept(TALLOC_CTX *ctx, fr_bio_t **out_p, fr_bio_t *bio)
Accept a new connection on a socket.
Definition fd.c:1901
fr_bio_fd_info_t const * fr_bio_fd_info(fr_bio_t *bio)
Returns a pointer to the bio-specific information.
Definition fd.c:1472
static int fr_bio_fd_accept_datagram(TALLOC_CTX *ctx, fr_bio_t **out_p, fr_bio_fd_t *parent)
Accept a "connection" on a datagram listening socket.
Definition fd.c:1791
static int fr_bio_fd_shutdown(fr_bio_t *bio)
Orderly shutdown.
Definition fd.c:97
static int fr_bio_fd_accept_stream(fr_bio_fd_t *my, fr_bio_fd_t const *parent)
Accept a stream socket and initialize its flags.
Definition fd.c:1612
static int fr_bio_fd_init_file(fr_bio_fd_t *my)
Files are a special case of connected sockets.
Definition fd.c:894
static void fr_bio_fd_el_timeout(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
We have a timeout on the conenction.
Definition fd.c:1347
#define SOCK_CLOEXEC
Definition fd.c:1605
int fr_bio_fd_init_common(fr_bio_fd_t *my)
Definition fd.c:1011
static ssize_t fr_bio_fd_recvmsg_dst(fr_bio_fd_t *parent, uint8_t *buffer, int flags, struct sockaddr_storage *from, socklen_t *from_len, fr_ipaddr_t *dst)
Receive a datagram, and find the address which the datagram was sent to.
Definition fd.c:1684
int fr_bio_fd_write_only(fr_bio_t *bio)
Mark up a bio as write-only.
Definition fd.c:1519
static ssize_t fr_bio_fd_read_discard_datagram(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Discard all reads from a UDP socket.
Definition fd.c:1482
static void fr_bio_fd_el_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
FD error when trying to connect, give up on the BIO.
Definition fd.c:1228
#define SOCK_NONBLOCK
Definition fd.c:1601
int fr_bio_fd_close(fr_bio_t *bio)
Close the FD, but leave the bio allocated and alive.
Definition fd.c:1190
static void fr_bio_fd_set_open(fr_bio_fd_t *my)
Definition fd.c:773
static ssize_t fr_bio_fd_read_stream(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Stream read.
Definition fd.c:159
fr_socket_t socket
as connected socket
Definition fd.h:150
fr_trie_t * trie
network trie for filtering IPs. NULL for no "networks" subsection.
Definition fd.h:115
bool eof
are we at EOF?
Definition fd.h:159
fr_bio_fd_type_t type
type of the socket
Definition fd.h:152
fr_bio_fd_state_t state
connecting, open, closed, etc.
Definition fd.h:153
int connect_errno
from connect() or other APIs
Definition fd.h:161
fr_ipaddr_t dst_ipaddr
their IP address
Definition fd.h:103
fr_bio_fd_type_t type
accept, connected, unconnected, etc.
Definition fd.h:94
@ FR_BIO_FD_CONNECTED
connected client sockets (UDP or TCP)
Definition fd.h:64
@ FR_BIO_FD_INVALID
not set
Definition fd.h:60
@ FR_BIO_FD_UNCONNECTED
unconnected UDP / datagram, but also allowed
Definition fd.h:61
@ FR_BIO_FD_LISTEN
call fr_bio_fd_accept() for each new connection
Definition fd.h:65
fr_bio_fd_config_t const * cfg
so we know what was asked, vs what was granted.
Definition fd.h:163
bool read_blocked
did we block on read?
Definition fd.h:157
@ FR_BIO_FD_TRANSPORT_TCP
TCP over IPv4 or IPv6.
Definition fd.h:80
bool server
is this a client or a server?
Definition fd.h:99
uint32_t backlog
for listen()
Definition fd.h:117
uint16_t dst_port
their port
Definition fd.h:106
fr_bio_fd_transport_t transport_type
which transport, as a number instead of a name
Definition fd.h:98
int fr_bio_fd_open(fr_bio_t *bio, fr_bio_fd_config_t const *cfg)
Opens a socket and updates sock->fd.
Definition fd_open.c:1293
@ FR_BIO_FD_STATE_CLOSED
Definition fd.h:54
@ FR_BIO_FD_STATE_CONNECTING
Definition fd.h:56
@ FR_BIO_FD_STATE_OPEN
error states must be before this
Definition fd.h:55
bool reuse_port
whether or not we re-use the same destination port for datagram sockets
Definition fd.h:100
bool backlog_is_set
Whether we were provided with a backlog.
Definition fd.h:140
fr_socket_t socket
socket information, including FD.
Definition fd.h:48
bool async
is it async
Definition fd.h:131
bool write_blocked
did we block on write?
Definition fd.h:158
int flags
O_RDONLY, etc.
Definition fd.h:129
fr_ipaddr_t src_ipaddr
our IP address
Definition fd.h:102
Configuration for sockets.
Definition fd.h:93
Run-time status of the socket.
Definition fd.h:149
Per-packet context.
Definition fd.h:47
static bool fr_bio_fd_errno_retry(fr_bio_fd_t *my, ssize_t *rcode_p, int *tries_p, bool is_write)
Turn a failed read / write into a bio return code.
Definition fd_errno.h:41
#define FD_ENET_SUPPRESS
Definition fd_errno.h:157
int fr_bio_fd_unix_shutdown(fr_bio_t *bio)
Remove the Unix domain socket which this bio created.
Definition fd_open.c:499
void fr_bio_fd_name(fr_bio_fd_t *my)
Set the name of an FD BIO.
Definition fd_open.c:897
socklen_t remote_sockaddr_len
Definition fd_priv.h:46
struct fr_bio_fd_s::@18 connect
size_t offset
where fr_bio_fd_packet_ctx_t is stored
Definition fd_priv.h:66
fr_bio_fd_info_t info
Definition fd_priv.h:43
int max_tries
how many times we retry on EINTR
Definition fd_priv.h:65
uint8_t * first_datagram
The datagram which fr_bio_fd_accept() read to name the peer.
Definition fd_priv.h:58
uint8_t * accept_buffer
A LISTEN datagram bio reads datagrams into this buffer, which is allocated once by fr_bio_fd_init_lis...
Definition fd_priv.h:56
#define fr_bio_fd_packet_ctx(_my, _packet_ctx)
Definition fd_priv.h:83
#define FR_BIO_FD_MAX_DATAGRAM
The largest payload of a UDP datagram.
Definition fd_priv.h:35
size_t first_datagram_len
Length of first_datagram.
Definition fd_priv.h:60
struct sockaddr_storage remote_sockaddr
for connected UDP sockets.
Definition fd_priv.h:45
Our FD bio structure.
Definition fd_priv.h:40
static bool fr_bio_fd_read_retry(fr_bio_fd_t *my, ssize_t *rcode_p, int *tries_p)
Common finalization code for the read functions.
Definition fd_read.h:44
static bool fr_bio_fd_write_retry(fr_bio_fd_t *my, ssize_t *rcode_p, int *tries_p, size_t size)
Common finalization code for the write functions.
Definition fd_write.h:42
talloc_free(hp)
void fr_ipaddr_get_scope_id(fr_ipaddr_t *ipaddr)
Definition inet.c:1529
int fr_ipaddr_from_sockaddr(fr_ipaddr_t *ipaddr, uint16_t *port, struct sockaddr_storage const *sa, socklen_t salen)
Convert sockaddr to our internal ip address representation.
Definition inet.c:1448
int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr)
Determine if an address is the INADDR_ANY address for its address family.
Definition inet.c:62
int fr_ipaddr_to_sockaddr(struct sockaddr_storage *sa, socklen_t *salen, fr_ipaddr_t const *ipaddr, uint16_t port)
Convert our internal ip address representation to a sockaddr.
Definition inet.c:1399
int af
Address family.
Definition inet.h:64
#define AF_FR_FILENAME
Definition inet.h:105
IPv4/6 prefix.
int fr_bio_write_blocked(fr_bio_t *bio)
Internal BIO function to tell all BIOs that it's blocked.
Definition base.c:354
ssize_t fr_bio_shutdown_read(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void *buffer, UNUSED size_t size)
Definition base.c:74
int fr_bio_destructor(fr_bio_t *bio)
Free this bio.
Definition base.c:35
ssize_t fr_bio_shutdown_write(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, UNUSED size_t size)
Definition base.c:79
fr_bio_t * fr_bio_eof(fr_bio_t *bio)
Internal BIO function to run EOF callbacks.
Definition base.c:280
int fr_bio_shutdown(fr_bio_t *bio)
Shut down a set of BIOs.
Definition base.c:158
#define fr_time()
Definition event.c:61
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Definition event.c:1244
Stores all information relating to an event list.
Definition event.c:415
unsigned short uint16_t
long int ssize_t
unsigned char uint8_t
unsigned long int size_t
int fr_nonblock(UNUSED int fd)
Definition misc.c:293
int fr_cloexec(UNUSED int fd)
Definition misc.c:331
ssize_t fr_bio_null_write(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, UNUSED size_t size)
Always return 0 on write.
Definition null.c:39
ssize_t fr_bio_null_read(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void *buffer, UNUSED size_t size)
Always return 0 on read.
Definition null.c:31
ssize_t fr_bio_fail_read(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void *buffer, UNUSED size_t size)
Always return error on read.
Definition null.c:47
ssize_t fr_bio_fail_write(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, UNUSED size_t size)
Always return an error on write.
Definition null.c:56
#define fr_assert(_expr)
Definition rad_assert.h:37
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
static fr_time_t fr_time_from_timeval(struct timeval const *when_tv)
Convert a timeval (wallclock time) to a fr_time_t (internal time)
Definition time.h:914
#define fr_time_wrap(_time)
Definition time.h:145
#define fr_time_eq(_a, _b)
Definition time.h:241
static fr_time_t fr_time_from_timespec(struct timespec const *when_ts)
Convert a timespec (wallclock time) to a fr_time_t (internal time)
Definition time.h:896
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
An event timer list.
Definition timer.c:65
#define FR_TIMER_DELETE(_ev_p)
Definition timer.h:105
#define fr_timer_in(...)
Definition timer.h:87
static fr_event_list_t * el
static fr_slen_t head
Definition xlat.h:410
static fr_slen_t parent
Definition pair.h:864
int af
AF_INET, AF_INET6, or AF_UNIX.
Definition socket.h:83
int fd
File descriptor if this is a live socket.
Definition socket.h:86
int type
SOCK_STREAM, SOCK_DGRAM, etc.
Definition socket.h:84
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
static fr_sbuff_err_t char size_t fr_sbuff_t * in
Definition value.h:1061
static fr_sbuff_err_t char ** out
Definition value.h:1061
static fr_sbuff_err_t char size_t * len
Definition value.h:1061