All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Functions
socket.c File Reference

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

#include <freeradius-devel/libradius.h>
#include <freeradius-devel/udpfromto.h>
#include <fcntl.h>
+ Include dependency graph for socket.c:

Go to the source code of this file.

Functions

int fr_socket_client_tcp (fr_ipaddr_t *src_ipaddr, fr_ipaddr_t *dst_ipaddr, uint16_t dst_port, bool async)
 Establish a connected TCP socket. More...
 
int fr_socket_client_udp (fr_ipaddr_t *src_ipaddr, fr_ipaddr_t *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_base (int proto, fr_ipaddr_t *ipaddr, int *port, char const *port_name, bool async)
 Open an IPv4 / IPv6, and UDP / TCP socket, server side. More...
 
int fr_socket_server_bind (int sockfd, fr_ipaddr_t *ipaddr, int *port, char const *interface)
 Bind to an IPv4 / IPv6, and UDP / TCP socket, server side. More...
 
int fr_socket_wait_for_connect (int sockfd, struct timeval const *timeout)
 Wait for a socket to be connected, with an optional timeout. More...
 

Detailed Description

Functions for establishing and managing low level sockets.

Id:
0afa8c54eebbea5596422eac72e0c0c5a9bf0e3b
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

int fr_socket_client_tcp ( fr_ipaddr_t src_ipaddr,
fr_ipaddr_t 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, ipaddr, port, true);
if (sockfd < 0) {
exit(1);
}
if ((errno == EINPROGRESS) && (fr_socket_wait_for_connect(sockfd, timeout) < 0)) {
error:
close(sockfd);
goto error;
}
//Optionally, if blocking operation is required
if (fr_blocking(sockfd) < 0) goto error;
Parameters
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 167 of file socket.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_socket_client_udp ( fr_ipaddr_t src_ipaddr,
fr_ipaddr_t 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, ipaddr, port, true);
if (sockfd < 0) {
exit(1);
}
if ((errno == EINPROGRESS) && (fr_socket_wait_for_connect(sockfd, timeout) < 0)) {
error:
close(sockfd);
goto error;
}
//Optionally, if blocking operation is required
if (fr_blocking(sockfd) < 0) goto error;
Parameters
src_ipaddrto bind socket to, may be NULL if socket is not bound to any specific address.
dst_ipaddrWhere to send datagrams.
dst_portWhere to send datagrams.
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 273 of file socket.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_socket_client_unix ( UNUSED char const *  path,
UNUSED bool  async 
)

Definition at line 131 of file socket.c.

int fr_socket_server_base ( int  proto,
fr_ipaddr_t ipaddr,
int *  port,
char const *  port_name,
bool  async 
)

Open an IPv4 / IPv6, and UDP / TCP socket, server side.

Parameters
[in]protoIPPROTO_UDP or IPPROTO_TCP
[in]ipaddrThe IP address to listen on
[in,out]portthe port to listen on
[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 422 of file socket.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_socket_server_bind ( int  sockfd,
fr_ipaddr_t ipaddr,
int *  port,
char const *  interface 
)

Bind to an IPv4 / IPv6, and UDP / TCP socket, server side.

Parameters
[in]sockfdthe socket which was opened via fr_socket_server_base()
[in,out]ipaddrThe IP address to bind to
[in]portthe port to bind to
[in]interfacethe interface name to bind to
Returns
  • 0 on success
  • -1 on failure.

Definition at line 615 of file socket.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_socket_wait_for_connect ( int  sockfd,
struct timeval const *  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 359 of file socket.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function: