The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Macros | Functions
fd.c File Reference

BIO abstractions for file descriptors. More...

#include <freeradius-devel/bio/fd_priv.h>
#include <freeradius-devel/bio/null.h>
#include "fd_read.h"
#include "fd_write.h"
#include "fd_errno.h"
+ Include dependency graph for fd.c:

Go to the source code of this file.

Macros

#define ADDR_INIT
 
#define flag_blocked   read_blocked
 
#define SOL_IP   IPPROTO_IP
 

Functions

int fr_bio_fd_accept (TALLOC_CTX *ctx, fr_bio_t **out_p, fr_bio_t *bio)
 Alternative to calling fr_bio_read() on new socket. More...
 
fr_bio_tfr_bio_fd_alloc (TALLOC_CTX *ctx, fr_bio_fd_config_t const *cfg, size_t offset)
 Allocate a FD bio. More...
 
int fr_bio_fd_close (fr_bio_t *bio)
 Close the FD, but leave the bio allocated and alive. More...
 
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() More...
 
static int fr_bio_fd_destructor (fr_bio_fd_t *my)
 
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. More...
 
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. More...
 
static void fr_bio_fd_el_timeout (UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx)
 We have a timeout on the conenction. More...
 
static int fr_bio_fd_eof (fr_bio_t *bio)
 
fr_bio_fd_info_t const * fr_bio_fd_info (fr_bio_t *bio)
 Returns a pointer to the bio-specific information. More...
 
int fr_bio_fd_init_common (fr_bio_fd_t *my)
 
int fr_bio_fd_init_connected (fr_bio_fd_t *my)
 
static int fr_bio_fd_init_file (fr_bio_fd_t *my)
 Files are a special case of connected sockets. More...
 
int fr_bio_fd_init_listen (fr_bio_fd_t *my)
 
static ssize_t fr_bio_fd_read_accept (fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
 Return an fd on read() More...
 
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. More...
 
static ssize_t fr_bio_fd_read_discard (fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
 Discard all reads from a UDP socket. More...
 
static ssize_t fr_bio_fd_read_stream (fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
 Stream read. More...
 
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. More...
 
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. More...
 
static void fr_bio_fd_set_open (fr_bio_fd_t *my)
 
int fr_bio_fd_socket_name (fr_bio_fd_t *my)
 
static ssize_t fr_bio_fd_try_connect (fr_bio_fd_t *my)
 Try to connect(). More...
 
static ssize_t fr_bio_fd_write (fr_bio_t *bio, UNUSED void *packet_ctx, const void *buffer, size_t size)
 Write to fd. More...
 
int fr_bio_fd_write_only (fr_bio_t *bio)
 Mark up a bio as write-only. More...
 
static int fr_bio_fd_write_resume (fr_bio_t *bio)
 
int fr_filename_to_sockaddr (struct sockaddr_un *sun, socklen_t *sunlen, char const *filename)
 

Detailed Description

BIO abstractions for file descriptors.

Id
fe298d4430a53eb33a97ae310ff7bfb5b9b5891c

Definition in file fd.c.

Macro Definition Documentation

◆ ADDR_INIT

#define ADDR_INIT
Value:
do { \
addr->when = fr_time(); \
addr->socket.type = my->info.socket.type; \
addr->socket.fd = -1; \
addr->socket.inet.ifindex = my->info.socket.inet.ifindex; \
} while (0)
fr_bio_shutdown & my
Definition: fd_errno.h:59
#define fr_time()
Allow us to arbitrarily manipulate time.
Definition: state_test.c:8

Definition at line 84 of file fd.c.

◆ flag_blocked

#define flag_blocked   read_blocked

◆ SOL_IP

#define SOL_IP   IPPROTO_IP

Definition at line 33 of file fd.c.

Function Documentation

◆ fr_bio_fd_accept()

int fr_bio_fd_accept ( TALLOC_CTX *  ctx,
fr_bio_t **  out_p,
fr_bio_t bio 
)

Alternative to calling fr_bio_read() on new socket.

Definition at line 1388 of file fd.c.

+ Here is the call graph for this function:

◆ fr_bio_fd_alloc()

fr_bio_t* fr_bio_fd_alloc ( TALLOC_CTX *  ctx,
fr_bio_fd_config_t const *  cfg,
size_t  offset 
)

Allocate a FD bio.

The caller is responsible for tracking the FD, and all associated management of it. The bio API is intended to be simple, and does not provide wrapper functions for various ioctls. The caller should instead do that work.

Once the FD is give to the bio, its lifetime is "owned" by the bio. Calling talloc_free(bio) will close the FD.

The caller can still manage the FD for being readable / writeable. However, the caller should not call this bio directly (unless it is the only one). Instead, the caller should read from / write to the previous bio which will then eventually call this one.

Before updating any event handler readable / writeable callbacks, the caller should check fr_bio_fd_at_eof(). If true, then the handlers should not be inserted. The previous bios should still be called to process any pending data, until they return EOF.

The main purpose of an FD bio is to wrap the FD in a bio container. That, and handling retries on read / write, along with returning EOF as an error instead of zero.

Note that the read / write functions can return partial data. It is the callers responsibility to ensure that any writes continue from where they left off (otherwise dat awill be missing). And any partial reads should go to a memory bio.

If a read returns EOF, then the FD remains open until talloc_free(bio) or fr_bio_fd_close() is called.

Parameters
ctxthe talloc ctx
cfgstructure holding configuration information
offsetonly for unconnected datagram sockets, where fr_bio_fd_packet_ctx_t is stored
Returns
  • NULL on error, memory allocation failed
  • !NULL the bio

Definition at line 1012 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_close()

int fr_bio_fd_close ( fr_bio_t bio)

Close the FD, but leave the bio allocated and alive.

Definition at line 1058 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_connect_full()

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()

connect() said "come back when the socket is writeable". It's now writeable, so we check if there was a connection error.

Parameters
biothe binary IO handler
elthe event list
connected_cbcallback to run when the BIO is connected
error_cbcallback to run when the FD has an error
timeoutwhen to time out the connect() attempt
timeout_cbto call when the timeout runs.
Returns
  • <0 on error
  • 0 for "try again later". If callbacks are set, the callbacks will try again. Otherwise the application has to try again.
  • 1 for "we are now connected".

Definition at line 1235 of file fd.c.

+ Here is the call graph for this function:

◆ fr_bio_fd_destructor()

static int fr_bio_fd_destructor ( fr_bio_fd_t my)
static

Definition at line 94 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_el_connect()

static void fr_bio_fd_el_connect ( NDEBUG_UNUSED fr_event_list_t el,
NDEBUG_UNUSED int  fd,
NDEBUG_UNUSED int  flags,
void *  uctx 
)
static

Connect callback for when the socket is writable.

We try to connect the socket, and if so, call the application which should update the BIO status.

Definition at line 1136 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_el_error()

static void fr_bio_fd_el_error ( UNUSED fr_event_list_t el,
UNUSED int  fd,
UNUSED int  flags,
int  fd_errno,
void *  uctx 
)
static

FD error when trying to connect, give up on the BIO.

Definition at line 1119 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_el_timeout()

static void fr_bio_fd_el_timeout ( UNUSED fr_event_list_t el,
UNUSED fr_time_t  now,
void *  uctx 
)
static

We have a timeout on the conenction.

Definition at line 1207 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_eof()

static int fr_bio_fd_eof ( fr_bio_t bio)
static

Definition at line 118 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_info()

fr_bio_fd_info_t const* fr_bio_fd_info ( fr_bio_t bio)

Returns a pointer to the bio-specific information.

Definition at line 1323 of file fd.c.

+ Here is the caller graph for this function:

◆ fr_bio_fd_init_common()

int fr_bio_fd_init_common ( fr_bio_fd_t my)

< stream socket

< connected datagram

< we know our IP address

Definition at line 846 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_init_connected()

int fr_bio_fd_init_connected ( fr_bio_fd_t my)

Definition at line 783 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_init_file()

static int fr_bio_fd_init_file ( fr_bio_fd_t my)
static

Files are a special case of connected sockets.

Definition at line 752 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_init_listen()

int fr_bio_fd_init_listen ( fr_bio_fd_t my)

Definition at line 964 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_read_accept()

static ssize_t fr_bio_fd_read_accept ( fr_bio_t bio,
void *  packet_ctx,
void *  buffer,
size_t  size 
)
static

Return an fd on read()

With packet_ctx containing information about the socket.

Definition at line 891 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_read_connected_datagram()

static ssize_t fr_bio_fd_read_connected_datagram ( fr_bio_t bio,
UNUSED void *  packet_ctx,
void *  buffer,
size_t  size 
)
static

Connected datagram read.

The difference between this and stream protocols is that for datagrams. a read of zero means "no packets", where a read of zero on a steam socket means "EOF".

Connected sockets do not update per-packet contexts.

Definition at line 168 of file fd.c.

+ Here is the caller graph for this function:

◆ fr_bio_fd_read_discard()

static ssize_t fr_bio_fd_read_discard ( fr_bio_t bio,
UNUSED void *  packet_ctx,
void *  buffer,
size_t  size 
)
static

Discard all reads from a UDP socket.

Definition at line 1333 of file fd.c.

+ Here is the caller graph for this function:

◆ fr_bio_fd_read_stream()

static ssize_t fr_bio_fd_read_stream ( fr_bio_t bio,
UNUSED void *  packet_ctx,
void *  buffer,
size_t  size 
)
static

Stream read.

Stream sockets return 0 at EOF.  However, we want to distinguish that from the case of datagram
sockets, which return 0 when there's no data.  So we return 0 to the caller for "no data", but also
call the EOF function to tell all of the related BIOs that we're at EOF.

Definition at line 143 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_recvfrom()

static ssize_t fr_bio_fd_recvfrom ( fr_bio_t bio,
void *  packet_ctx,
void *  buffer,
size_t  size 
)
static

Read from a UDP socket where we know our IP.

Definition at line 185 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_sendto()

static ssize_t fr_bio_fd_sendto ( fr_bio_t bio,
void *  packet_ctx,
const void *  buffer,
size_t  size 
)
static

Write to a UDP socket where we know our IP.

Definition at line 256 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_set_open()

static void fr_bio_fd_set_open ( fr_bio_fd_t my)
static

Definition at line 660 of file fd.c.

+ Here is the caller graph for this function:

◆ fr_bio_fd_socket_name()

int fr_bio_fd_socket_name ( fr_bio_fd_t my)

Definition at line 630 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_try_connect()

static ssize_t fr_bio_fd_try_connect ( fr_bio_fd_t my)
static

Try to connect().

If connect is blocking, we either succeed or error immediately. Otherwise, the caller has to select the socket for writeability, and then call fr_bio_fd_connect() as soon as the socket is writeable.

Definition at line 679 of file fd.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_bio_fd_write()

static ssize_t fr_bio_fd_write ( fr_bio_t bio,
UNUSED void *  packet_ctx,
const void *  buffer,
size_t  size 
)
static

Write to fd.

This function is used for connected sockets, where we ignore the packet_ctx.

Definition at line 220 of file fd.c.

+ Here is the caller graph for this function:

◆ fr_bio_fd_write_only()

int fr_bio_fd_write_only ( fr_bio_t bio)

Mark up a bio as write-only.

Definition at line 1353 of file fd.c.

+ Here is the call graph for this function:

◆ fr_bio_fd_write_resume()

static int fr_bio_fd_write_resume ( fr_bio_t bio)
static

Definition at line 129 of file fd.c.

+ Here is the caller graph for this function:

◆ fr_filename_to_sockaddr()

int fr_filename_to_sockaddr ( struct sockaddr_un *  sun,
socklen_t *  sunlen,
char const *  filename 
)

Definition at line 612 of file fd.c.

+ Here is the caller graph for this function: