networkclient
Socket handling

Functions

ssize_t nr_socket_recv (RADIUS_PACKET *packet)
 
int nr_socket_send (RADIUS_PACKET *packet)
 
ssize_t nr_socket_recv_response (RADIUS_PACKET *response, const RADIUS_PACKET *request)
 
int nr_socket_send_response (RADIUS_PACKET *packet, const RADIUS_PACKET *original)
 
struct sockaddr_storage * nr_ipv42sockaddr (uint32_t ipaddr, int port, struct sockaddr_storage *s)
 
int sockaddr_cmp (const struct sockaddr_storage *a, const struct sockaddr_storage *b)
 
int nr_socket_open (struct sockaddr_storage *s)
 
int nr_track_id_socket (nr_track_id_t *s, struct sockaddr_storage *src, struct sockaddr_storage *dst)
 

Detailed Description

These routines send / receive packets, and do other socket-related functions. These routines are not part of the BSD licensed RADIUS library.

Function Documentation

struct sockaddr_storage* nr_ipv42sockaddr ( uint32_t  ipaddr,
int  port,
struct sockaddr_storage *  s 
)

Converts an IP address and port into a sockaddr structure..

Parameters
[in]ipaddrThe IPv4 address to use.
[in]portThe port to use
[out]sThe initialized sockaddr_storage structure.
Returns
Pointer to s, or NULL on invalid arguments

Referenced by main(), and nr_socket_open().

int nr_socket_open ( struct sockaddr_storage *  s)

Create a socket using the given IP information.

Note
The input sockaddr_storage pointer is updated before this function returns. It contains the IP and port to which the socket was bound. This lets the caller pass "any" for an address and port, and then discover which IP and port was chosen by the kernel.

IPv4 sockets can easily be created by calling nr_socket_open(nr_ipv42sockaddr(ip, port, sockaddr_storage));

The parameter 's' can be NULL, in which case it binds to IPv4 0.0.0.0, port 0. Since 's' is NULL, the function cannot return the IP and port to which the socket was bound.

The file descriptor returned can be released with a call to close().

Parameters
[in,out]sThe structure where the socket information is stored.
Returns
<0 on error, or the file descriptor handle for the socket.

References nr_ipv42sockaddr(), nr_strerror_printf(), return_NR_ERR, and UNUSED.

Referenced by main(), and nr_track_id_socket().

Here is the call graph for this function:

ssize_t nr_socket_recv ( RADIUS_PACKET packet)

Receives a response to a request.

This function reads a packet from the RADIUS_PACKET::sockfd, by calling recv().

Only valid RADIUS packets are returned from this function.

When data is read from a socket, that data might not be a valid RADIUS packet. This function ensures that the all non-RADIUS data is ignored and discarded from the socket.

When a valid packet is read, the function returns the total number of bytes received (>0).

Many of the fields of the RADIUS_PACKET structure are filled in with the relevant data from the "raw" packet. (e.g. RADIUS_PACKET::code, RADIUS_PACKET::id, RADIUS_PACKET::length, RADIUS_PACKET::vector, and RADIUS_PACKET::src)

This function is blocking. You will need to ensure that data is ready before calling it, otherwise it will block until there is data available on the socket.

The "raw" packet data is written to RADIUS_PACKET::data, up to RADIUS_PACKET::length. If RADIUS_PACKET::data is NULL, then the memory is allocated.

See also
nr_socket_send() for the counterpart to this function
Parameters
[in]packetThe RADIUS packet to receive
Returns
<0 on error, 0 for no data, 1 for got packet, but it's not a RADIUS packet >20 for length of valid RADIUS packet

References radius_packet::code, radius_packet::data, radius_packet::id, radius_packet::length, nr_debug_error, NR_MAX_PACKET_LEN, nr_packet_ok(), return_NR_ERR, radius_packet::sizeof_addr, radius_packet::sizeof_data, radius_packet::sockfd, radius_packet::src, radius_packet::vector, and radius_packet::vps.

Referenced by nr_socket_recv_response().

Here is the call graph for this function:

ssize_t nr_socket_recv_response ( RADIUS_PACKET response,
const RADIUS_PACKET request 
)

Receives a response to a request.

This function reads a packet from the RADIUS_PACKET::sockfd, using nr_socket_recv(). The return value is the same as nr_socket_recv().

This function should be used when there is only one request being sent on a particular socket. In that case, the only possible data to receive on that socket is a valid response to the request.

The response packet is verified, and decoded.

See also
nr_socket_send_response() for the counterpart to this function
Parameters
[in]responseThe RADIUS packet to receive
[in]requestThe original request
Returns
<0 on error, 0 for no data, 1 for got data, but it's not a valid response >20 for length of valid RADIUS response

References radius_packet::length, nr_packet_decode(), nr_packet_response_ok(), nr_packet_verify(), nr_socket_recv(), return_NR_ERR, radius_packet::sockfd, and radius_packet::timestamp.

Referenced by main().

Here is the call graph for this function:

int nr_socket_send ( RADIUS_PACKET packet)

Sends a packet.

This function sends a packet using the RADIUS_PACKET::sockfd field. Request packets are automatically encoded and signed, if necessary. Response packets must be already signed via nr_packet_sign(), otherwise NR_ERR_PACKET_UNSIGNED is returned.

See also
nr_socket_recv() for the counterpart to this function
Parameters
[in]packetThe RADIUS packet to send
Returns
<0 on error, >= 0 on success.

References radius_packet::attempts, radius_packet::data, radius_packet::dst, radius_packet::flags, radius_packet::length, nr_debug_error, nr_packet_can_encode(), nr_packet_sign(), NR_PACKET_SIGNED, return_NR_ERR, radius_packet::sizeof_addr, radius_packet::sockfd, and radius_packet::timestamp.

Referenced by main(), and nr_socket_send_response().

Here is the call graph for this function:

int nr_socket_send_response ( RADIUS_PACKET packet,
const RADIUS_PACKET original 
)

Sends a response to a request.

If RADIUS_PACKET::encoded is 0, then nr_packet_encode() is called to encode the packet. Otherwise, it assumes that nr_packet_encode() has already been called, and that the RADIUS_PACKET::data and RADIUS_PACKET::length fields are valid. See nr_packet_encode() for other requirements on the fields of the RADIUS_PACKET structure.

When a valid packet is sent, the function returns the total number of bytes sent (>0), even if RADIUS_PACKET::data is NULL.

See also
nr_socket_recv_response() for the counterpart to this function
Parameters
[in]packetThe RADIUS packet to encode
[in]originalThe original request
Returns
<0 on error, >= 0 on success.

References nr_packet_ok(), nr_packet_sign(), nr_socket_send(), and return_NR_ERR.

Here is the call graph for this function:

int nr_track_id_socket ( nr_track_id_t s,
struct sockaddr_storage *  src,
struct sockaddr_storage *  dst 
)

Set a nr_track_id_t to a src/dst address/port

This function intializes nr_track_id_t::sockfd, nr_track_id_t::src, and nr_track_id_t::dst.

Parameters
[in,out]sThe server structure to initialize
[in]srcSource address and port
[in]dstDestination address and port
Returns
<0 for error, 0 for success

References nr_track_id_t::ctx, nr_track_id_t::dst, nr_socket_open(), nr_track_id_t::packet_send, return_NR_ERR, nr_track_id_t::secret, nr_track_id_t::sizeof_secret, nr_track_id_t::sockfd, and nr_track_id_t::src.

Referenced by main().

Here is the call graph for this function:

int sockaddr_cmp ( const struct sockaddr_storage *  a,
const struct sockaddr_storage *  b 
)

Compares two sockaddrs..

Parameters
[in]aOne sockaddr_storage
[in]bAnother one
Returns
a <=> b