The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Data Structures | Macros | Functions
socket.h File Reference
#include <freeradius-devel/build.h>
#include <freeradius-devel/missing.h>
#include <freeradius-devel/util/inet.h>
#include <freeradius-devel/util/time.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/time.h>
+ Include dependency graph for socket.h:

Go to the source code of this file.

Data Structures

struct  fr_socket_t
 Holds information necessary for binding or connecting to a socket. More...
 
union  fr_socket_t.__unnamed139__
 
struct  fr_socket_t.__unnamed139__.inet
 
struct  fr_socket_t.__unnamed139__.unix
 

Macros

#define FR_SOCKET_ADDR_ALLOC_DEF_FUNC(_func, ...)
 

Functions

static fr_socket_tfr_socket_addr_alloc_inet (TALLOC_CTX *ctx, int proto, int ifindex, fr_ipaddr_t const *src_ipaddr, int src_port, fr_ipaddr_t const *dst_ipaddr, int dst_port)
 Initialise a fr_socket_t for connecting to a remote host using a specific src interface, address and port. More...
 
static fr_socket_tfr_socket_addr_alloc_inet_dst (TALLOC_CTX *ctx, int proto, fr_ipaddr_t const *ipaddr, int port)
 A variant of fr_socket_addr_alloc_inet_dst that will also allocates a fr_socket_t. More...
 
static fr_socket_tfr_socket_addr_alloc_inet_src (TALLOC_CTX *ctx, int proto, int ifindex, fr_ipaddr_t const *ipaddr, int port)
 A variant of fr_socket_addr_init_inet_src will also allocates a fr_socket_t. More...
 
static fr_socket_tfr_socket_addr_init_inet (fr_socket_t *addr, int proto, int ifindex, fr_ipaddr_t const *src_ipaddr, int src_port, fr_ipaddr_t const *dst_ipaddr, int dst_port)
 Initialise a fr_socket_t for connecting to a remote host using a specific src interface, address and port. More...
 
static fr_socket_tfr_socket_addr_init_inet_dst (fr_socket_t *addr, int proto, fr_ipaddr_t const *ipaddr, int port)
 Initialise a fr_socket_t for connecting to a remote host. More...
 
static fr_socket_tfr_socket_addr_init_inet_src (fr_socket_t *addr, int proto, int ifindex, fr_ipaddr_t const *ipaddr, int port)
 A variant of fr_socket_addr_alloc_inet will also allocates a fr_socket_t. More...
 
static void fr_socket_addr_swap (fr_socket_t *dst, fr_socket_t const *src)
 Swap src/dst information of a fr_socket_t. More...
 
int fr_socket_bind (int sockfd, char const *ifname, fr_ipaddr_t *src_ipaddr, uint16_t *src_port)
 Bind a UDP/TCP v4/v6 socket to a given ipaddr src port, and interface. More...
 
int fr_socket_client_tcp (char const *ifname, fr_ipaddr_t *src_ipaddr, fr_ipaddr_t const *dst_ipaddr, uint16_t dst_port, bool async)
 Establish a connected TCP socket. More...
 
int fr_socket_client_udp (char const *ifname, fr_ipaddr_t *src_ipaddr, uint16_t *src_port, fr_ipaddr_t const *dst_ipaddr, uint16_t dst_port, bool async)
 Establish a connected UDP socket. More...
 
int fr_socket_client_unix (char const *path, bool async)
 
static bool fr_socket_proto_is_known (int proto)
 Check the proto value is sane/supported. More...
 
int fr_socket_server_tcp (fr_ipaddr_t const *ipaddr, uint16_t *port, char const *port_name, bool async)
 Open an IPv4/IPv6 TCP socket. More...
 
int fr_socket_server_udp (fr_ipaddr_t const *ipaddr, uint16_t *port, char const *port_name, bool async)
 Open an IPv4/IPv6 unconnected UDP socket. More...
 
int fr_socket_wait_for_connect (int sockfd, fr_time_delta_t timeout)
 Wait for a socket to be connected, with an optional timeout. More...
 

Data Structure Documentation

◆ fr_socket_t

struct fr_socket_t

Holds information necessary for binding or connecting to a socket.

May also be used in protocol contexts to store information necessary for returning packets to their originators.

Definition at line 63 of file socket.h.

Data Fields
union fr_socket_t __unnamed__
int af AF_INET, AF_INET6, or AF_UNIX.
int fd File descriptor if this is a live socket.
int type SOCK_STREAM, SOCK_DGRAM, etc.

◆ fr_socket_t.__unnamed139__

union fr_socket_t.__unnamed139__

Definition at line 64 of file socket.h.

Data Fields
__unnamed139__ inet
__unnamed139__ unix

◆ fr_socket_t.__unnamed139__.inet

struct fr_socket_t.__unnamed139__.inet

Definition at line 65 of file socket.h.

Data Fields
fr_ipaddr_t dst_ipaddr IP address to connect to, or send the packet to.
uint16_t dst_port Port to connect to or send the packet to.
int ifindex Source interface to bind to or originate the packet from.
fr_ipaddr_t src_ipaddr IP address to bind to, or originate the packet from.
uint16_t src_port Port to bind to, or originate the packet from.

◆ fr_socket_t.__unnamed139__.unix

struct fr_socket_t.__unnamed139__.unix

Definition at line 74 of file socket.h.

Data Fields
char const * path Unix socket path.

Macro Definition Documentation

◆ FR_SOCKET_ADDR_ALLOC_DEF_FUNC

#define FR_SOCKET_ADDR_ALLOC_DEF_FUNC (   _func,
  ... 
)
Value:
fr_socket_t *addr; \
addr = talloc(ctx, fr_socket_t); \
if (unlikely(!addr)) return NULL; \
return _func(addr, ##__VA_ARGS__);
#define unlikely(_x)
Definition: build.h:378
Holds information necessary for binding or connecting to a socket.
Definition: socket.h:63

Definition at line 110 of file socket.h.

Function Documentation

◆ fr_socket_addr_alloc_inet()

static fr_socket_t* fr_socket_addr_alloc_inet ( TALLOC_CTX *  ctx,
int  proto,
int  ifindex,
fr_ipaddr_t const *  src_ipaddr,
int  src_port,
fr_ipaddr_t const *  dst_ipaddr,
int  dst_port 
)
inlinestatic

Initialise a fr_socket_t for connecting to a remote host using a specific src interface, address and port.

Can also be used to record information from an incoming packet so that we can identify the correct return path later.

Parameters
[in]ctxto allocate a new fr_socket_t struct in.
[in]protoone of the IPPROTO_* macros, i.e. IPPROTO_TCP, IPPROTO_UDP
[in]ifindexThe interface to originate the packet from Pass <= 0 to indicate an unknown or unspecified interface.
[in]src_ipaddrThe source IP address of the packet, or source interface for packets to egress out of.
[in]src_portThe source port of the packet or the source
[in]dst_ipaddrThe destination IP address of the packet.
[in]dst_portThe destination port of the packet.
Returns
  • NULL if invalid parameters are provided.
  • An initialised fr_socket_t struct.

Definition at line 191 of file socket.h.

+ Here is the call graph for this function:

◆ fr_socket_addr_alloc_inet_dst()

static fr_socket_t* fr_socket_addr_alloc_inet_dst ( TALLOC_CTX *  ctx,
int  proto,
fr_ipaddr_t const *  ipaddr,
int  port 
)
inlinestatic

A variant of fr_socket_addr_alloc_inet_dst that will also allocates a fr_socket_t.

Parameters
[in]ctxto allocate new fr_socket_t struct in.
[in]protoone of the IPPROTO_* macros, i.e. IPPROTO_TCP, IPPROTO_UDP
[in]ipaddrThe IP address to bind to. May be all zeros to bind to all addresses, but the AF must still be specified.
[in]portThe source port to bind to.
Returns
  • NULL if invalid parameters are provided.
  • An initialised fr_socket_t struct.

Definition at line 287 of file socket.h.

+ Here is the call graph for this function:

◆ fr_socket_addr_alloc_inet_src()

static fr_socket_t* fr_socket_addr_alloc_inet_src ( TALLOC_CTX *  ctx,
int  proto,
int  ifindex,
fr_ipaddr_t const *  ipaddr,
int  port 
)
inlinestatic

A variant of fr_socket_addr_init_inet_src will also allocates a fr_socket_t.

Parameters
[in]ctxto allocate a new fr_socket_t struct in.
[in]protoone of the IPPROTO_* macros, i.e. IPPROTO_TCP, IPPROTO_UDP
[in]ifindexThe interface to originate the packet from Pass <= 0 to indicate an unknown or unspecified interface.
[in]ipaddrThe IP address to bind to. May be all zeros to bind to all addresses, but the AF must still be specified.
[in]portThe source port to bind to.
Returns
  • NULL if invalid parameters are provided.
  • An initialised fr_socket_t struct.

Definition at line 244 of file socket.h.

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

◆ fr_socket_addr_init_inet()

static fr_socket_t* fr_socket_addr_init_inet ( fr_socket_t addr,
int  proto,
int  ifindex,
fr_ipaddr_t const *  src_ipaddr,
int  src_port,
fr_ipaddr_t const *  dst_ipaddr,
int  dst_port 
)
inlinestatic

Initialise a fr_socket_t for connecting to a remote host using a specific src interface, address and port.

Can also be used to record information from an incoming packet so that we can identify the correct return path later.

Parameters
[out]addrto initialise.
[in]protoone of the IPPROTO_* macros, i.e. IPPROTO_TCP, IPPROTO_UDP
[in]ifindexThe interface to originate the packet from Pass <= 0 to indicate an unknown or unspecified interface.
[in]src_ipaddrThe source IP address of the packet, or source interface for packets to egress out of.
[in]src_portThe source port of the packet or the source
[in]dst_ipaddrThe destination IP address of the packet.
[in]dst_portThe destination port of the packet.
Returns
  • NULL if invalid parameters are provided.
  • An initialised fr_socket_t struct.

Definition at line 151 of file socket.h.

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

◆ fr_socket_addr_init_inet_dst()

static fr_socket_t* fr_socket_addr_init_inet_dst ( fr_socket_t addr,
int  proto,
fr_ipaddr_t const *  ipaddr,
int  port 
)
inlinestatic

Initialise a fr_socket_t for connecting to a remote host.

Parameters
[out]addrto initialise.
[in]protoone of the IPPROTO_* macros, i.e. IPPROTO_TCP, IPPROTO_UDP
[in]ipaddrThe IP address to bind to. May be all zeros to bind to all addresses, but the AF must still be specified.
[in]portThe source port to bind to.
Returns
  • NULL if invalid parameters are provided.
  • An initialised fr_socket_t struct.

Definition at line 260 of file socket.h.

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

◆ fr_socket_addr_init_inet_src()

static fr_socket_t* fr_socket_addr_init_inet_src ( fr_socket_t addr,
int  proto,
int  ifindex,
fr_ipaddr_t const *  ipaddr,
int  port 
)
inlinestatic

A variant of fr_socket_addr_alloc_inet will also allocates a fr_socket_t.

Parameters
[out]addrto initialise.
[in]protoone of the IPPROTO_* macros, i.e. IPPROTO_TCP, IPPROTO_UDP
[in]ifindexThe interface to originate the packet from Pass <= 0 to indicate an unknown or unspecified interface.
[in]ipaddrThe IP address to bind to. May be all zeros to bind to all addresses, but the AF must still be specified.
[in]portThe source port to bind to.
Returns
  • NULL if invalid parameters are provided.
  • An initialised fr_socket_t struct.

Definition at line 213 of file socket.h.

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

◆ fr_socket_addr_swap()

static void fr_socket_addr_swap ( fr_socket_t dst,
fr_socket_t const *  src 
)
inlinestatic

Swap src/dst information of a fr_socket_t.

Parameters
[out]dstWhere to write the swapped addresses. May be the same as src.
[in]srcSocket address to swap.

Definition at line 121 of file socket.h.

+ Here is the caller graph for this function:

◆ fr_socket_bind()

int fr_socket_bind ( int  sockfd,
char const *  ifname,
fr_ipaddr_t src_ipaddr,
uint16_t src_port 
)

Bind a UDP/TCP v4/v6 socket to a given ipaddr src port, and interface.

Use one of:

  • fr_socket_server_udp - for non-connected socket.
  • fr_socket_server_tcp ...to open a file descriptor, then call this function to bind the socket to an IP address.
Parameters
[in]sockfdthe socket which opened by fr_socket_server_*.
[in]ifnameto bind to.
[in,out]src_ipaddrThe IP address to bind to. Will be updated to the IP address that was actually bound to. Pass NULL to just bind to an interface.
[in]src_portthe port to bind to. NULL if any port is allowed.
Returns
  • 0 on success
  • -1 on failure.

Definition at line 229 of file socket.c.

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

◆ fr_socket_client_tcp()

int fr_socket_client_tcp ( char const *  ifname,
fr_ipaddr_t src_ipaddr,
fr_ipaddr_t const *  dst_ipaddr,
uint16_t  dst_port,
bool  async 
)

Establish a connected TCP socket.

The following code demonstrates using this function with a connection timeout:

sockfd = fr_socket_client_tcp(NULL, NULL, ipaddr, port, true);
if (sockfd < 0) {
}
if ((errno == EINPROGRESS) && (fr_socket_wait_for_connect(sockfd, timeout) < 0)) {
error:
goto error;
}
//Optionally, if blocking operation is required
if (fr_blocking(sockfd) < 0) goto error;
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition: debug.h:232
static int sockfd
Definition: dhcpclient.c:56
static fr_time_delta_t timeout
Definition: dhcpclient.c:54
int fr_blocking(UNUSED int fd)
Definition: misc.c:289
int fr_socket_client_tcp(char const *ifname, fr_ipaddr_t *src_ipaddr, fr_ipaddr_t const *dst_ipaddr, uint16_t dst_port, bool async)
Establish a connected TCP socket.
Definition: socket.c:729
int fr_socket_wait_for_connect(int sockfd, fr_time_delta_t timeout)
Wait for a socket to be connected, with an optional timeout.
Definition: socket.c:803
close(uq->fd)
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition: strerror.c:733
Parameters
[in]ifnameIf non-NULL, bind the socket to this interface.
src_ipaddrto bind socket to, may be NULL if socket is not bound to any specific address.
dst_ipaddrWhere to connect to.
dst_portWhere to connect to.
asyncWhether to set the socket to nonblocking, allowing use of fr_socket_wait_for_connect.
Returns
  • FD on success
  • -1 on failure.

Definition at line 729 of file socket.c.

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

◆ fr_socket_client_udp()

int fr_socket_client_udp ( char const *  ifname,
fr_ipaddr_t src_ipaddr,
uint16_t src_port,
fr_ipaddr_t const *  dst_ipaddr,
uint16_t  dst_port,
bool  async 
)

Establish a connected UDP socket.

Connected UDP sockets can be used with write(), unlike unconnected sockets which must be used with sendto and recvfrom.

The following code demonstrates using this function with a connection timeout:

sockfd = fr_socket_client_udp(NULL, NULL, NULL, ipaddr, port, true);
if (sockfd < 0) {
}
if ((errno == EINPROGRESS) && (fr_socket_wait_for_connect(sockfd, timeout) < 0)) {
error:
goto error;
}
//Optionally, if blocking operation is required
if (fr_blocking(sockfd) < 0) goto error;
int fr_socket_client_udp(char const *ifname, fr_ipaddr_t *src_ipaddr, uint16_t *src_port, fr_ipaddr_t const *dst_ipaddr, uint16_t dst_port, bool async)
Establish a connected UDP socket.
Definition: socket.c:634
Parameters
[in]ifnameIf non-NULL, bind the socket to this interface.
[in,out]src_ipaddrto bind socket to, may be NULL if socket is not bound to any specific address. If non-null, the bound IP is copied here, too.
[out]src_portThe source port we were bound to, may be NULL.
[in]dst_ipaddrWhere to send datagrams.
[in]dst_portWhere to send datagrams.
[in]asyncWhether to set the socket to nonblocking, allowing use of fr_socket_wait_for_connect.
Returns
  • FD on success.
  • -1 on failure.

Definition at line 634 of file socket.c.

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

◆ fr_socket_client_unix()

int fr_socket_client_unix ( char const *  path,
bool  async 
)

◆ fr_socket_proto_is_known()

static bool fr_socket_proto_is_known ( int  proto)
inlinestatic

Check the proto value is sane/supported.

Parameters
[in]prototo check
Returns
  • true if it is.
  • false if it's not.

Definition at line 91 of file socket.h.

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

◆ fr_socket_server_tcp()

int fr_socket_server_tcp ( fr_ipaddr_t const *  src_ipaddr,
uint16_t src_port,
char const *  port_name,
bool  async 
)

Open an IPv4/IPv6 TCP socket.

Parameters
[in]src_ipaddrThe IP address to listen on
[in,out]src_portthe port to listen on. If *port == 0, the resolved service port will be written here. NULL if any port is allowed.
[in]port_nameif *port == 0, the name of the port
[in]asyncwhether we block or not on reads and writes
Returns
  • Socket FD on success.
  • -1 on failure.

Definition at line 969 of file socket.c.

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

◆ fr_socket_server_udp()

int fr_socket_server_udp ( fr_ipaddr_t const *  src_ipaddr,
uint16_t src_port,
char const *  port_name,
bool  async 
)

Open an IPv4/IPv6 unconnected UDP socket.

Function name is a bit of a misnomer as it can also be used to create client sockets too, such is the nature of UDP.

Parameters
[in]src_ipaddrThe IP address to listen on
[in,out]src_portthe port to listen on. If *port == 0, the resolved service port will be written here.
[in]port_nameif *port == 0, the name of the port
[in]asyncwhether we block or not on reads and writes
Returns
  • Socket FD on success.
  • -1 on failure.

Definition at line 867 of file socket.c.

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

◆ fr_socket_wait_for_connect()

int fr_socket_wait_for_connect ( int  sockfd,
fr_time_delta_t  timeout 
)

Wait for a socket to be connected, with an optional timeout.

Note
On error the caller is expected to close(sockfd).
Parameters
sockfdthe socket to wait on.
timeoutHow long to wait for socket to open.
Returns
  • 0 on success.
  • -1 on connection error.
  • -2 on timeout.
  • -3 on select error.

Definition at line 803 of file socket.c.

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