The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
fd.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/**
19 * $Id: fc945a89844d2930c1f5a5133c5ce0f0f921f1cd $
20 * @file lib/bio/fd.h
21 * @brief Binary IO abstractions for file descriptors
22 *
23 * Allow reads and writes from file descriptors.
24 *
25 * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
26 */
27RCSIDH(lib_bio_fd_h, "$Id: fc945a89844d2930c1f5a5133c5ce0f0f921f1cd $")
28
29#include <freeradius-devel/bio/base.h>
30#include <freeradius-devel/util/socket.h>
31#include <freeradius-devel/util/event.h>
32
33#include <freeradius-devel/server/cf_parse.h>
34
35#include <fcntl.h>
36
37/*
38 * Local hack. AF_FILE is a synonym for AF_LOCAL on some platforms.
39 */
40#define AF_FILE_BIO (INT_MAX)
41
42/** Per-packet context
43 *
44 * For reading packets src_ip is *their* IP, and dst_ip is *our* IP.
45 *
46 * For writing packets, src_ip is *our* IP, and dst_ip is *their* IP.
47 *
48 * This context is returned only for datagram sockets. For stream sockets (TCP and Unix domain), it
49 * isn't used. The caller can look at the socket information to determine src/dst ip/port.
50 */
51typedef struct {
52 fr_socket_t socket; //!< socket information, including FD.
53 fr_time_t when; //!< when the packet was received
55
62
63typedef enum {
64 FR_BIO_FD_INVALID, //!< not set
65 FR_BIO_FD_UNCONNECTED, //!< unconnected UDP / datagram only
66 // updates #fr_bio_fd_packet_ctx_t for reads,
67 // uses #fr_bio_fd_packet_ctx_t for writes
68 FR_BIO_FD_CONNECTED, //!< connected client sockets (UDP or TCP)
69 FR_BIO_FD_LISTEN, //!< returns new fd in buffer on fr_bio_read() or fr_bio_fd_accept()
70 // updates #fr_bio_fd_packet_ctx_t on successful FD read.
71 FR_BIO_FD_ACCEPTED, //!< temporarily until it's connected.
73
74/** Configuration for sockets
75 *
76 * Each piece of information is broken out into a separate field, so that the configuration file parser can
77 * parse each field independently.
78 *
79 * We also include more information here than we need in an #fr_socket_t.
80 */
81typedef struct {
82 fr_bio_fd_type_t type; //!< accept, connected, unconnected, etc.
83
84 int socket_type; //!< SOCK_STREAM or SOCK_DGRAM
85 char const *transport; //!< name of the transport protocol
86 bool server; //!< is this a client or a server?
87
88 fr_ipaddr_t src_ipaddr; //!< our IP address
89 fr_ipaddr_t dst_ipaddr; //!< their IP address
90
91 uint16_t src_port; //!< our port
92 uint16_t dst_port; //!< their port
93
94 char const *interface; //!< for binding to an interface
95
96 uint32_t recv_buff; //!< How big the kernel's receive buffer should be.
97 uint32_t send_buff; //!< How big the kernel's send buffer should be.
98
99 char const *path; //!< for Unix domain sockets
100 mode_t perm; //!< permissions for domain sockets
101 uid_t uid; //!< who owns the socket
102 gid_t gid; //!< who owns the socket
103 bool mkdir; //!< make intermediate directories
104
105 char const *filename; //!< for files
106 int flags; //!< O_RDONLY, etc.
107
108 bool async; //!< is it async
109 bool tcp_delay; //!< We do tcp_nodelay by default.
110
111 /*
112 * Extra fields for conf_parser_t
113 */
114 bool recv_buff_is_set; //!< Whether we were provided with a recv_buf
115 bool send_buff_is_set; //!< Whether we were provided with a send_buf
117
120
121/** Run-time status of the socket.
122 *
123 */
124typedef struct {
125 fr_socket_t socket; //!< as connected socket
126
127 fr_bio_fd_type_t type; //!< type of the socket
128 fr_bio_fd_state_t state; //!< connecting, open, closed, etc.
129
130 char const *name; //!< printable name of this BIO
131
132 bool read_blocked; //!< did we block on read?
133 bool write_blocked; //!< did we block on write?
134 bool eof; //!< are we at EOF?
135
136 int connect_errno; //!< from connect() or other APIs
137
138 fr_bio_fd_config_t const *cfg; //!< so we know what was asked, vs what was granted.
140
141fr_bio_t *fr_bio_fd_alloc(TALLOC_CTX *ctx, fr_bio_fd_config_t const *cfg, size_t offset) CC_HINT(nonnull(1));
142
143int fr_bio_fd_close(fr_bio_t *bio) CC_HINT(nonnull);
144
146 fr_bio_callback_t connected_cb, fr_bio_callback_t error_cb,
147 fr_time_delta_t *timeout, fr_bio_callback_t timeout_cb) CC_HINT(nonnull(1));
148
149#define fr_bio_fd_connect(_x) fr_bio_fd_connect_full(_x, NULL, NULL, NULL, NULL, NULL)
150
151fr_bio_fd_info_t const *fr_bio_fd_info(fr_bio_t *bio) CC_HINT(nonnull);
152
153int fr_bio_fd_check_config(fr_bio_fd_config_t const *cfg) CC_HINT(nonnull);
154
155int fr_bio_fd_open(fr_bio_t *bio, fr_bio_fd_config_t const *cfg) CC_HINT(nonnull);
156
157int fr_bio_fd_write_only(fr_bio_t *bio) CC_HINT(nonnull);
158
159int fr_bio_fd_reopen(fr_bio_t *bio) CC_HINT(nonnull);
160
161int fr_bio_fd_accept(TALLOC_CTX *ctx, fr_bio_t **out, fr_bio_t *bio) CC_HINT(nonnull);
void(* fr_bio_callback_t)(fr_bio_t *bio)
Definition base.h:85
#define RCSIDH(h, id)
Definition build.h:484
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:579
const conf_parser_t fr_bio_fd_server_config[]
Definition fd_config.c:400
char const * transport
name of the transport protocol
Definition fd.h:85
fr_socket_t socket
as connected socket
Definition fd.h:125
char const * name
printable name of this BIO
Definition fd.h:130
uint16_t src_port
our port
Definition fd.h:91
bool eof
are we at EOF?
Definition fd.h:134
fr_bio_fd_type_t
Definition fd.h:63
@ FR_BIO_FD_ACCEPTED
temporarily until it's connected.
Definition fd.h:71
@ FR_BIO_FD_CONNECTED
connected client sockets (UDP or TCP)
Definition fd.h:68
@ FR_BIO_FD_INVALID
not set
Definition fd.h:64
@ FR_BIO_FD_UNCONNECTED
unconnected UDP / datagram only
Definition fd.h:65
@ FR_BIO_FD_LISTEN
returns new fd in buffer on fr_bio_read() or fr_bio_fd_accept()
Definition fd.h:69
fr_bio_fd_type_t type
type of the socket
Definition fd.h:127
fr_bio_fd_state_t state
connecting, open, closed, etc.
Definition fd.h:128
uint32_t recv_buff
How big the kernel's receive buffer should be.
Definition fd.h:96
bool mkdir
make intermediate directories
Definition fd.h:103
int fr_bio_fd_reopen(fr_bio_t *bio)
Reopen a file BIO.
Definition fd_open.c:1255
fr_bio_fd_state_t
Definition fd.h:56
@ FR_BIO_FD_STATE_CLOSED
Definition fd.h:58
@ FR_BIO_FD_STATE_INVALID
Definition fd.h:57
@ FR_BIO_FD_STATE_CONNECTING
Definition fd.h:60
@ FR_BIO_FD_STATE_OPEN
error states must be before this
Definition fd.h:59
int connect_errno
from connect() or other APIs
Definition fd.h:136
fr_ipaddr_t dst_ipaddr
their IP address
Definition fd.h:89
fr_bio_fd_type_t type
accept, connected, unconnected, etc.
Definition fd.h:82
const conf_parser_t fr_bio_fd_client_config[]
Definition fd_config.c:256
bool recv_buff_is_set
Whether we were provided with a recv_buf.
Definition fd.h:114
char const * path
for Unix domain sockets
Definition fd.h:99
uint32_t send_buff
How big the kernel's send buffer should be.
Definition fd.h:97
char const * filename
for files
Definition fd.h:105
uid_t uid
who owns the socket
Definition fd.h:101
fr_bio_fd_config_t const * cfg
so we know what was asked, vs what was granted.
Definition fd.h:138
bool read_blocked
did we block on read?
Definition fd.h:132
bool server
is this a client or a server?
Definition fd.h:86
gid_t gid
who owns the socket
Definition fd.h:102
char const * interface
for binding to an interface
Definition fd.h:94
mode_t perm
permissions for domain sockets
Definition fd.h:100
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:1338
int socket_type
SOCK_STREAM or SOCK_DGRAM.
Definition fd.h:84
uint16_t dst_port
their port
Definition fd.h:92
bool tcp_delay
We do tcp_nodelay by default.
Definition fd.h:109
int fr_bio_fd_write_only(fr_bio_t *bio)
Mark up a bio as write-only.
Definition fd.c:1368
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:908
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:1241
int fr_bio_fd_accept(TALLOC_CTX *ctx, fr_bio_t **out, fr_bio_t *bio)
Alternative to calling fr_bio_read() on new socket.
Definition fd.c:1428
int fr_bio_fd_check_config(fr_bio_fd_config_t const *cfg)
Checks the configuration without modifying anything.
Definition fd_open.c:834
fr_socket_t socket
socket information, including FD.
Definition fd.h:52
int fr_bio_fd_close(fr_bio_t *bio)
Close the FD, but leave the bio allocated and alive.
Definition fd.c:1064
bool async
is it async
Definition fd.h:108
fr_time_t when
when the packet was received
Definition fd.h:53
bool send_buff_is_set
Whether we were provided with a send_buf.
Definition fd.h:115
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:1018
bool write_blocked
did we block on write?
Definition fd.h:133
int flags
O_RDONLY, etc.
Definition fd.h:106
fr_ipaddr_t src_ipaddr
our IP address
Definition fd.h:88
Configuration for sockets.
Definition fd.h:81
Run-time status of the socket.
Definition fd.h:124
Per-packet context.
Definition fd.h:51
IPv4/6 prefix.
Stores all information relating to an event list.
Definition event.c:411
unsigned short uint16_t
unsigned int uint32_t
unsigned int mode_t
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
static fr_event_list_t * el
Holds information necessary for binding or connecting to a socket.
Definition socket.h:63
int nonnull(2, 5))
static size_t char ** out
Definition value.h:997