The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
|
#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>
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.__unnamed148__ |
struct | fr_socket_t.__unnamed148__.inet |
struct | fr_socket_t.__unnamed148__.unix |
Macros | |
#define | FR_SOCKET_ADDR_ALLOC_DEF_FUNC(_func, ...) |
Functions | |
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) |
Initialise a fr_socket_t for connecting to a remote host using a specific src interface, address and port. | |
static fr_socket_t * | fr_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. | |
static fr_socket_t * | fr_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. | |
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) |
Initialise a fr_socket_t for connecting to a remote host using a specific src interface, address and port. | |
static fr_socket_t * | fr_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. | |
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) |
A variant of fr_socket_addr_alloc_inet will also allocates a fr_socket_t. | |
static void | fr_socket_addr_swap (fr_socket_t *dst, fr_socket_t const *src) |
Swap src/dst information of a fr_socket_t. | |
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. | |
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. | |
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. | |
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. | |
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. | |
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. | |
int | fr_socket_wait_for_connect (int sockfd, fr_time_delta_t timeout) |
Wait for a socket to be connected, with an optional timeout. | |
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.
Data Fields | ||
---|---|---|
union fr_socket_t.__unnamed148__ | __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. |
union fr_socket_t.__unnamed148__ |
Data Fields | ||
---|---|---|
struct fr_socket_t.__unnamed148__.inet | inet | |
struct fr_socket_t.__unnamed148__.unix | unix |
struct fr_socket_t.__unnamed148__.inet |
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. |
struct fr_socket_t.__unnamed148__.unix |
#define FR_SOCKET_ADDR_ALLOC_DEF_FUNC | ( | _func, | |
... | |||
) |
|
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.
[in] | ctx | to allocate a new fr_socket_t struct in. |
[in] | proto | one of the IPPROTO_* macros, i.e. IPPROTO_TCP, IPPROTO_UDP |
[in] | ifindex | The interface to originate the packet from Pass <= 0 to indicate an unknown or unspecified interface. |
[in] | src_ipaddr | The source IP address of the packet, or source interface for packets to egress out of. |
[in] | src_port | The source port of the packet or the source |
[in] | dst_ipaddr | The destination IP address of the packet. |
[in] | dst_port | The destination port of the packet. |
Definition at line 191 of file socket.h.
|
inlinestatic |
A variant of fr_socket_addr_alloc_inet_dst that will also allocates a fr_socket_t.
[in] | ctx | to allocate new fr_socket_t struct in. |
[in] | proto | one of the IPPROTO_* macros, i.e. IPPROTO_TCP, IPPROTO_UDP |
[in] | ipaddr | The IP address to bind to. May be all zeros to bind to all addresses, but the AF must still be specified. |
[in] | port | The source port to bind to. |
Definition at line 287 of file socket.h.
|
inlinestatic |
A variant of fr_socket_addr_init_inet_src will also allocates a fr_socket_t.
[in] | ctx | to allocate a new fr_socket_t struct in. |
[in] | proto | one of the IPPROTO_* macros, i.e. IPPROTO_TCP, IPPROTO_UDP |
[in] | ifindex | The interface to originate the packet from Pass <= 0 to indicate an unknown or unspecified interface. |
[in] | ipaddr | The IP address to bind to. May be all zeros to bind to all addresses, but the AF must still be specified. |
[in] | port | The source port to bind to. |
Definition at line 244 of file socket.h.
|
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.
[out] | addr | to initialise. |
[in] | proto | one of the IPPROTO_* macros, i.e. IPPROTO_TCP, IPPROTO_UDP |
[in] | ifindex | The interface to originate the packet from Pass <= 0 to indicate an unknown or unspecified interface. |
[in] | src_ipaddr | The source IP address of the packet, or source interface for packets to egress out of. |
[in] | src_port | The source port of the packet or the source |
[in] | dst_ipaddr | The destination IP address of the packet. |
[in] | dst_port | The destination port of the packet. |
Definition at line 151 of file socket.h.
|
inlinestatic |
Initialise a fr_socket_t for connecting to a remote host.
[out] | addr | to initialise. |
[in] | proto | one of the IPPROTO_* macros, i.e. IPPROTO_TCP, IPPROTO_UDP |
[in] | ipaddr | The IP address to bind to. May be all zeros to bind to all addresses, but the AF must still be specified. |
[in] | port | The source port to bind to. |
Definition at line 260 of file socket.h.
|
inlinestatic |
A variant of fr_socket_addr_alloc_inet will also allocates a fr_socket_t.
[out] | addr | to initialise. |
[in] | proto | one of the IPPROTO_* macros, i.e. IPPROTO_TCP, IPPROTO_UDP |
[in] | ifindex | The interface to originate the packet from Pass <= 0 to indicate an unknown or unspecified interface. |
[in] | ipaddr | The IP address to bind to. May be all zeros to bind to all addresses, but the AF must still be specified. |
[in] | port | The source port to bind to. |
Definition at line 213 of file socket.h.
|
inlinestatic |
Swap src/dst information of a fr_socket_t.
[out] | dst | Where to write the swapped addresses. May be the same as src. |
[in] | src | Socket address to swap. |
Definition at line 121 of file socket.h.
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:
[in] | sockfd | the socket which opened by fr_socket_server_*. |
[in] | ifname | to bind to. |
[in,out] | src_ipaddr | The 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_port | the port to bind to. NULL if any port is allowed. |
Definition at line 229 of file socket.c.
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:
[in] | ifname | If non-NULL, bind the socket to this interface. |
src_ipaddr | to bind socket to, may be NULL if socket is not bound to any specific address. | |
dst_ipaddr | Where to connect to. | |
dst_port | Where to connect to. | |
async | Whether to set the socket to nonblocking, allowing use of fr_socket_wait_for_connect. |
Definition at line 729 of file socket.c.
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:
[in] | ifname | If non-NULL, bind the socket to this interface. |
[in,out] | src_ipaddr | to 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_port | The source port we were bound to, may be NULL. |
[in] | dst_ipaddr | Where to send datagrams. |
[in] | dst_port | Where to send datagrams. |
[in] | async | Whether to set the socket to nonblocking, allowing use of fr_socket_wait_for_connect. |
Definition at line 634 of file socket.c.
int fr_socket_client_unix | ( | char const * | path, |
bool | async | ||
) |
|
inlinestatic |
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.
[in] | src_ipaddr | The IP address to listen on |
[in,out] | src_port | the port to listen on. If *port == 0, the resolved service port will be written here. NULL if any port is allowed. |
[in] | port_name | if *port == 0, the name of the port |
[in] | async | whether we block or not on reads and writes |
Definition at line 969 of file socket.c.
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.
[in] | src_ipaddr | The IP address to listen on |
[in,out] | src_port | the port to listen on. If *port == 0, the resolved service port will be written here. |
[in] | port_name | if *port == 0, the name of the port |
[in] | async | whether we block or not on reads and writes |
Definition at line 867 of file socket.c.
int fr_socket_wait_for_connect | ( | int | sockfd, |
fr_time_delta_t | timeout | ||
) |
Wait for a socket to be connected, with an optional timeout.
close(sockfd)
.sockfd | the socket to wait on. |
timeout | How long to wait for socket to open. |
Definition at line 803 of file socket.c.