The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
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: 1fb942dd15785b061b0253167cce72e8d2164052 $
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: 1fb942dd15785b061b0253167cce72e8d2164052 $")
28
29#include <freeradius-devel/bio/base.h>
30#include <freeradius-devel/util/socket.h>
31#include <freeradius-devel/util/event.h>
32#include <freeradius-devel/util/trie.h>
33
34#include <freeradius-devel/server/cf_parse.h>
35
36#include <fcntl.h>
37
38/** Per-packet context
39 *
40 * For reading packets src_ip is *their* IP, and dst_ip is *our* IP.
41 *
42 * For writing packets, src_ip is *our* IP, and dst_ip is *their* IP.
43 *
44 * This context is returned only for datagram sockets. For stream sockets (TCP and Unix domain), it
45 * isn't used. The caller can look at the socket information to determine src/dst ip/port.
46 */
47typedef struct {
48 fr_socket_t socket; //!< socket information, including FD.
49 fr_time_t when; //!< when the packet was received
51
58
59typedef enum : uint8_t {
60 FR_BIO_FD_INVALID, //!< not set
61 FR_BIO_FD_UNCONNECTED, //!< unconnected UDP / datagram, but also allowed
62 // for client TCP connections, when the destination
63 // is specified later.
64 FR_BIO_FD_CONNECTED, //!< connected client sockets (UDP or TCP)
65 FR_BIO_FD_LISTEN, //!< call fr_bio_fd_accept() for each new connection
66 // fr_bio_read() and fr_bio_write() return an error.
68
69/** The type of transport used by a FD BIO.
70 *
71 * The transport defines where the data goes (files, socket, etc), not the format of the transport (datagram
72 * or stream). It also is largely independent of connected vs unconnected. A TCP socket can be connected or
73 * listening, and a UDP socket can be connected or unconnected.
74 *
75 * This field is set automatically when parsing the configuration.
76 */
77typedef enum : uint8_t {
79 FR_BIO_FD_TRANSPORT_FILE, //!< regular file, named by 'filename'
80 FR_BIO_FD_TRANSPORT_TCP, //!< TCP over IPv4 or IPv6
81 FR_BIO_FD_TRANSPORT_UDP, //!< UDP over IPv4 or IPv6
82 FR_BIO_FD_TRANSPORT_UNIX, //!< Unix domain socket, named by 'path'
84#define FR_BIO_FD_TRANSPORT_SIZE (FR_BIO_FD_TRANSPORT_UNIX + 1)
85
86/** Configuration for sockets
87 *
88 * Each piece of information is broken out into a separate field, so that the configuration file parser can
89 * parse each field independently.
90 *
91 * We also include more information here than we need in an #fr_socket_t.
92 */
93typedef struct {
94 fr_bio_fd_type_t type; //!< accept, connected, unconnected, etc.
95
96 int socket_type; //!< SOCK_STREAM or SOCK_DGRAM
97 char const *transport; //!< name of the transport protocol
98 fr_bio_fd_transport_t transport_type; //!< which transport, as a number instead of a name
99 bool server; //!< is this a client or a server?
100 bool reuse_port; //!< whether or not we re-use the same destination port for datagram sockets
101
102 fr_ipaddr_t src_ipaddr; //!< our IP address
103 fr_ipaddr_t dst_ipaddr; //!< their IP address
104
105 uint16_t src_port; //!< our port
106 uint16_t dst_port; //!< their port
107
108 uint16_t src_port_start; //!< limit source port ranges for client BIOs
109 uint16_t src_port_end; //!< limit source port ranges for client BIOs
110
111 char const *interface; //!< for binding to an interface
112
113 fr_ipaddr_t *allow; //!< allowed networks, from the "networks" subsection
114 fr_ipaddr_t *deny; //!< denied networks, from the "networks" subsection
115 fr_trie_t *trie; //!< network trie for filtering IPs. NULL for no "networks" subsection.
116
117 uint32_t backlog; //!< for listen()
118
119 uint32_t recv_buff; //!< How big the kernel's receive buffer should be.
120 uint32_t send_buff; //!< How big the kernel's send buffer should be.
121
122 char const *path; //!< for Unix domain sockets
123 mode_t perm; //!< permissions for domain sockets
124 uid_t uid; //!< who owns the socket
125 gid_t gid; //!< who owns the socket
126 bool mkdir; //!< make intermediate directories
127
128 char const *filename; //!< for files
129 int flags; //!< O_RDONLY, etc.
130
131 bool async; //!< is it async
132 bool tcp_delay; //!< We do tcp_nodelay by default.
133 bool exceed_mtu; //!< Whether we allow packets which exceed the local MTU
134
135 /*
136 * Extra fields for conf_parser_t
137 */
138 bool recv_buff_is_set; //!< Whether we were provided with a recv_buf
139 bool send_buff_is_set; //!< Whether we were provided with a send_buf
140 bool backlog_is_set; //!< Whether we were provided with a backlog
142
145
146/** Run-time status of the socket.
147 *
148 */
149typedef struct {
150 fr_socket_t socket; //!< as connected socket
151
152 fr_bio_fd_type_t type; //!< type of the socket
153 fr_bio_fd_state_t state; //!< connecting, open, closed, etc.
154
155 char const *name; //!< printable name of this BIO
156
157 bool read_blocked; //!< did we block on read?
158 bool write_blocked; //!< did we block on write?
159 bool eof; //!< are we at EOF?
160
161 int connect_errno; //!< from connect() or other APIs
162
163 fr_bio_fd_config_t const *cfg; //!< so we know what was asked, vs what was granted.
165
166fr_bio_t *fr_bio_fd_alloc(TALLOC_CTX *ctx, fr_bio_fd_config_t const *cfg, size_t offset) CC_HINT(nonnull(1));
167
168int fr_bio_fd_close(fr_bio_t *bio) CC_HINT(nonnull);
169
171 fr_bio_callback_t connected_cb, fr_bio_callback_t error_cb,
172 fr_time_delta_t *timeout, fr_bio_callback_t timeout_cb) CC_HINT(nonnull(1));
173
174#define fr_bio_fd_connect(_x) fr_bio_fd_connect_full(_x, NULL, NULL, NULL, NULL, NULL)
175
176fr_bio_fd_info_t const *fr_bio_fd_info(fr_bio_t *bio) CC_HINT(nonnull);
177
178int fr_bio_fd_check_config(fr_bio_fd_config_t const *cfg) CC_HINT(nonnull);
179
180int fr_bio_fd_open(fr_bio_t *bio, fr_bio_fd_config_t const *cfg) CC_HINT(nonnull);
181
182int fr_bio_fd_write_only(fr_bio_t *bio) CC_HINT(nonnull);
183
184int fr_bio_fd_reopen(fr_bio_t *bio) CC_HINT(nonnull);
185
186int 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:86
#define RCSIDH(h, id)
Definition build.h:610
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:649
const conf_parser_t fr_bio_fd_server_config[]
Definition fd_config.c:614
char const * transport
name of the transport protocol
Definition fd.h:97
fr_socket_t socket
as connected socket
Definition fd.h:150
char const * name
printable name of this BIO
Definition fd.h:155
fr_trie_t * trie
network trie for filtering IPs. NULL for no "networks" subsection.
Definition fd.h:115
uint16_t src_port
our port
Definition fd.h:105
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_ipaddr_t * allow
allowed networks, from the "networks" subsection
Definition fd.h:113
fr_bio_fd_state_t state
connecting, open, closed, etc.
Definition fd.h:153
uint16_t src_port_start
limit source port ranges for client BIOs
Definition fd.h:108
uint32_t recv_buff
How big the kernel's receive buffer should be.
Definition fd.h:119
bool mkdir
make intermediate directories
Definition fd.h:126
int fr_bio_fd_reopen(fr_bio_t *bio)
Reopen a file BIO.
Definition fd_open.c:1505
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_ipaddr_t * deny
denied networks, from the "networks" subsection
Definition fd.h:114
const conf_parser_t fr_bio_fd_client_config[]
Definition fd_config.c:439
bool recv_buff_is_set
Whether we were provided with a recv_buf.
Definition fd.h:138
char const * path
for Unix domain sockets
Definition fd.h:122
uint32_t send_buff
How big the kernel's send buffer should be.
Definition fd.h:120
char const * filename
for files
Definition fd.h:128
uid_t uid
who owns the socket
Definition fd.h:124
fr_bio_fd_type_t
Definition fd.h:59
@ 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_t
The type of transport used by a FD BIO.
Definition fd.h:77
@ FR_BIO_FD_TRANSPORT_TCP
TCP over IPv4 or IPv6.
Definition fd.h:80
@ FR_BIO_FD_TRANSPORT_UDP
UDP over IPv4 or IPv6.
Definition fd.h:81
@ FR_BIO_FD_TRANSPORT_INVALID
not set
Definition fd.h:78
@ FR_BIO_FD_TRANSPORT_UNIX
Unix domain socket, named by 'path'.
Definition fd.h:82
@ FR_BIO_FD_TRANSPORT_FILE
regular file, named by 'filename'
Definition fd.h:79
bool server
is this a client or a server?
Definition fd.h:99
gid_t gid
who owns the socket
Definition fd.h:125
char const * interface
for binding to an interface
Definition fd.h:111
mode_t perm
permissions for domain sockets
Definition fd.h:123
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:1474
uint16_t src_port_end
limit source port ranges for client BIOs
Definition fd.h:109
int socket_type
SOCK_STREAM or SOCK_DGRAM.
Definition fd.h:96
uint32_t backlog
for listen()
Definition fd.h:117
uint16_t dst_port
their port
Definition fd.h:106
bool tcp_delay
We do tcp_nodelay by default.
Definition fd.h:132
fr_bio_fd_transport_t transport_type
which transport, as a number instead of a name
Definition fd.h:98
int fr_bio_fd_write_only(fr_bio_t *bio)
Mark up a bio as write-only.
Definition fd.c:1521
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_t
Definition fd.h:52
@ FR_BIO_FD_STATE_CLOSED
Definition fd.h:54
@ FR_BIO_FD_STATE_INVALID
Definition fd.h:53
@ 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
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:1377
int fr_bio_fd_accept(TALLOC_CTX *ctx, fr_bio_t **out, fr_bio_t *bio)
Accept a new connection on a socket.
Definition fd.c:1903
bool exceed_mtu
Whether we allow packets which exceed the local MTU.
Definition fd.h:133
int fr_bio_fd_check_config(fr_bio_fd_config_t const *cfg)
Checks the configuration without modifying anything.
Definition fd_open.c:996
fr_socket_t socket
socket information, including FD.
Definition fd.h:48
int fr_bio_fd_close(fr_bio_t *bio)
Close the FD, but leave the bio allocated and alive.
Definition fd.c:1192
bool async
is it async
Definition fd.h:131
fr_time_t when
when the packet was received
Definition fd.h:49
bool send_buff_is_set
Whether we were provided with a send_buf.
Definition fd.h:139
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:1144
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
IPv4/6 prefix.
Stores all information relating to an event list.
Definition event.c:415
unsigned short uint16_t
unsigned int uint32_t
unsigned char uint8_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:60
static fr_sbuff_err_t char ** out
Definition value.h:1061
int nonnull(2, 5))