The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: 7c695251c409536b2789d34acdc02b71e13d741d $
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  */
27 RCSIDH(lib_bio_fd_h, "$Id: 7c695251c409536b2789d34acdc02b71e13d741d $")
28 
29 #include <freeradius-devel/bio/base.h>
30 #include <freeradius-devel/util/socket.h>
31 
32 #include <fcntl.h>
33 
34 /*
35  * Local hack. AF_FILE is a synonym for AF_LOCAL on some platforms.
36  */
37 #define AF_FILE_BIO (INT_MAX)
38 
39 /** Per-packet context
40  *
41  * For reading packets src_ip is *their* IP, and dst_ip is *our* IP.
42  *
43  * For writing packets, src_ip is *our* IP, and dst_ip is *their* IP.
44  *
45  * This context is returned only for datagram sockets. For stream sockets (TCP and Unix domain), it
46  * isn't used. The caller can look at the socket information to determine src/dst ip/port.
47  */
48 typedef struct {
49  fr_socket_t socket; //!< socket information, including FD.
50  fr_time_t when; //!< when the packet was received
52 
53 typedef enum {
56  FR_BIO_FD_STATE_OPEN, //!< error states must be before this
59 
60 typedef enum {
61  FR_BIO_FD_UNCONNECTED, //!< unconnected UDP / datagram only
62  // updates #fr_bio_fd_packet_ctx_t for reads,
63  // uses #fr_bio_fd_packet_ctx_t for writes
64  FR_BIO_FD_CONNECTED, //!< connected client sockets (UDP or TCP)
65  FR_BIO_FD_ACCEPT, //!< returns new fd in buffer on fr_bio_read()
66  // updates #fr_bio_fd_packet_ctx_t on successful FD read.
68 
69 /** Configuration for sockets
70  *
71  * Each piece of information is broken out into a separate field, so that the configuration file parser can
72  * parse each field independently.
73  *
74  * We also include more information here than we need in an #fr_socket_t.
75  */
76 typedef struct {
77  fr_bio_fd_type_t type; //!< accept, connected, unconnected, etc.
78 
79  int socket_type; //!< SOCK_STREAM or SOCK_DGRAM
80 
81  fr_ipaddr_t src_ipaddr; //!< our IP address
82  fr_ipaddr_t dst_ipaddr; //!< their IP address
83 
84  uint16_t src_port; //!< our port
85  uint16_t dst_port; //!< their port
86 
87  char const *interface; //!< for binding to an interface
88 
89  uint32_t recv_buff; //!< How big the kernel's receive buffer should be.
90  uint32_t send_buff; //!< How big the kernel's send buffer should be.
91 
92  char const *path; //!< for Unix domain sockets
93  mode_t perm; //!< permissions for domain sockets
94  uid_t uid; //!< who owns the socket
95  gid_t gid; //!< who owns the socket
96 
97  char const *filename; //!< for files
98  int flags; //!< O_RDONLY, etc.
99 
100  bool async; //!< is it async
101  bool tcp_delay; //!< We do tcp_nodelay by default.
103 
104 /** Run-time status of the socket.
105  *
106  */
107 typedef struct {
108  fr_socket_t socket; //!< as connected socket
109 
110  fr_bio_fd_type_t type; //!< type of the socket
111 
112  fr_bio_fd_state_t state; //!< connecting, open, closed, etc.
113 
114  bool read_blocked; //!< did we block on read?
115  bool write_blocked; //!< did we block on write?
116  bool eof; //!< are we at EOF?
117 
118  fr_bio_fd_config_t const *cfg; //!< so we know what was asked, vs what was granted.
120 
121 fr_bio_t *fr_bio_fd_alloc(TALLOC_CTX *ctx, fr_bio_cb_funcs_t *cb, fr_bio_fd_config_t const *cfg, size_t offset) CC_HINT(nonnull(1));
122 
123 int fr_bio_fd_close(fr_bio_t *bio) CC_HINT(nonnull);
124 
125 int fr_bio_fd_connect(fr_bio_t *bio) CC_HINT(nonnull);
126 
128 
129 int fr_bio_fd_open(fr_bio_t *bio, fr_bio_fd_config_t const *cfg) CC_HINT(nonnull);
130 
Definition: base.h:103
#define RCSIDH(h, id)
Definition: build.h:445
fr_bio_t * fr_bio_fd_alloc(TALLOC_CTX *ctx, fr_bio_cb_funcs_t *cb, fr_bio_fd_config_t const *cfg, size_t offset))
Allocate a FD bio.
Definition: fd.c:1018
fr_socket_t socket
as connected socket
Definition: fd.h:108
uint16_t src_port
our port
Definition: fd.h:84
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:1167
bool eof
are we at EOF?
Definition: fd.h:116
fr_bio_fd_type_t
Definition: fd.h:60
@ FR_BIO_FD_CONNECTED
connected client sockets (UDP or TCP)
Definition: fd.h:64
@ FR_BIO_FD_UNCONNECTED
unconnected UDP / datagram only
Definition: fd.h:61
@ FR_BIO_FD_ACCEPT
returns new fd in buffer on fr_bio_read()
Definition: fd.h:65
fr_bio_fd_type_t type
type of the socket
Definition: fd.h:110
fr_bio_fd_state_t state
connecting, open, closed, etc.
Definition: fd.h:112
uint32_t recv_buff
How big the kernel's receive buffer should be.
Definition: fd.h:89
fr_bio_fd_state_t
Definition: fd.h:53
@ FR_BIO_FD_STATE_CLOSED
Definition: fd.h:55
@ FR_BIO_FD_STATE_INVALID
Definition: fd.h:54
@ FR_BIO_FD_STATE_CONNECTING
Definition: fd.h:57
@ FR_BIO_FD_STATE_OPEN
error states must be before this
Definition: fd.h:56
fr_ipaddr_t dst_ipaddr
their IP address
Definition: fd.h:82
fr_bio_fd_type_t type
accept, connected, unconnected, etc.
Definition: fd.h:77
int fr_bio_fd_connect(fr_bio_t *bio)
Finalize a connect()
Definition: fd.c:1125
char const * path
for Unix domain sockets
Definition: fd.h:92
uint32_t send_buff
How big the kernel's send buffer should be.
Definition: fd.h:90
char const * filename
for files
Definition: fd.h:97
uid_t uid
who owns the socket
Definition: fd.h:94
fr_bio_fd_config_t const * cfg
so we know what was asked, vs what was granted.
Definition: fd.h:118
bool read_blocked
did we block on read?
Definition: fd.h:114
gid_t gid
who owns the socket
Definition: fd.h:95
char const * interface
for binding to an interface
Definition: fd.h:87
mode_t perm
permissions for domain sockets
Definition: fd.h:93
int socket_type
SOCK_STREAM or SOCK_DGRAM.
Definition: fd.h:79
uint16_t dst_port
their port
Definition: fd.h:85
bool tcp_delay
We do tcp_nodelay by default.
Definition: fd.h:101
int fr_bio_fd_write_only(fr_bio_t *bio)
Mark up a bio as write-only.
Definition: fd.c:1199
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:717
fr_socket_t socket
socket information, including FD.
Definition: fd.h:49
int fr_bio_fd_close(fr_bio_t *bio)
Close the FD, but leave the bio allocated and alive.
Definition: fd.c:1062
bool async
is it async
Definition: fd.h:100
fr_time_t when
when the packet was received
Definition: fd.h:50
bool write_blocked
did we block on write?
Definition: fd.h:115
int flags
O_RDONLY, etc.
Definition: fd.h:98
fr_ipaddr_t src_ipaddr
our IP address
Definition: fd.h:81
Configuration for sockets.
Definition: fd.h:76
Run-time status of the socket.
Definition: fd.h:107
Per-packet context.
Definition: fd.h:48
IPv4/6 prefix.
Definition: merged_model.c:272
unsigned short uint16_t
Definition: merged_model.c:31
unsigned int uint32_t
Definition: merged_model.c:33
unsigned int mode_t
Definition: merged_model.c:21
static fr_bio_t * bio
Definition: radclient-ng.c:86
"server local" time.
Definition: time.h:69
Holds information necessary for binding or connecting to a socket.
Definition: socket.h:63
int nonnull(2, 5))