The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Functions
socket.c File Reference

Functions for establishing and managing low level sockets. More...

#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/util/misc.h>
#include <freeradius-devel/util/socket.h>
#include <freeradius-devel/util/strerror.h>
#include <freeradius-devel/util/syserror.h>
#include <freeradius-devel/util/udpfromto.h>
#include <freeradius-devel/util/value.h>
#include <freeradius-devel/util/cap.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <ifaddrs.h>
+ Include dependency graph for socket.c:

Go to the source code of this file.

Functions

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 (UNUSED char const *path, UNUSED bool async)
 
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. More...
 
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. 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...
 
static int socket_bind_ifname (UNUSED int sockfd, UNUSED char const *ifname)
 
static int socket_dont_fragment (UNUSED int sockfd, UNUSED int af)
 
static socket_dont_inherit (UNUSED int sockfd)
 
static int socket_inaddr_any_v6only (UNUSED int sockfd, UNUSED fr_ipaddr_t const *ipaddr)
 
static int socket_port_from_service (int proto, char const *port_name)
 Resolve a named service to a port. More...
 

Detailed Description

Functions for establishing and managing low level sockets.

Author
Arran Cudbard-Bell (a.cud.nosp@m.bard.nosp@m.b@fre.nosp@m.erad.nosp@m.ius.o.nosp@m.rg)
Alan DeKok (aland.nosp@m.@fre.nosp@m.eradi.nosp@m.us.o.nosp@m.rg)

Definition in file socket.c.

Function Documentation

◆ 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 ( UNUSED char const *  path,
UNUSED bool  async 
)

Definition at line 564 of file socket.c.

+ 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:

◆ socket_bind_ifname()

static int socket_bind_ifname ( UNUSED int  sockfd,
UNUSED char const *  ifname 
)
inlinestatic

Definition at line 574 of file socket.c.

+ Here is the call graph for this function:

◆ socket_dont_fragment()

static int socket_dont_fragment ( UNUSED int  sockfd,
UNUSED int  af 
)
static

Definition at line 206 of file socket.c.

+ Here is the caller graph for this function:

◆ socket_dont_inherit()

static socket_dont_inherit ( UNUSED int  sockfd)
static

Definition at line 107 of file socket.c.

+ Here is the caller graph for this function:

◆ socket_inaddr_any_v6only()

static int socket_inaddr_any_v6only ( UNUSED int  sockfd,
UNUSED fr_ipaddr_t const *  ipaddr 
)
static

Definition at line 151 of file socket.c.

+ Here is the caller graph for this function:

◆ socket_port_from_service()

static int socket_port_from_service ( int  proto,
char const *  port_name 
)
static

Resolve a named service to a port.

Parameters
[in]protoThe protocol. Either IPPROTO_TCP or IPPROTO_UDP.
[in]port_nameThe service name, i.e. "radius".
Returns
  • > 0 the port port_name resolves to.
  • < 0 on error.

Definition at line 48 of file socket.c.

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