libosmo-netif 1.6.0
Osmocom network interface library
|
This code is intended to abstract any client use of stream-type sockets, such as TCP and SCTP. More...
Typedefs | |
typedef int(* | osmo_stream_cli_connect_cb_t) (struct osmo_stream_cli *cli) |
typedef int(* | osmo_stream_cli_disconnect_cb_t) (struct osmo_stream_cli *cli) |
typedef int(* | osmo_stream_cli_read_cb_t) (struct osmo_stream_cli *cli) |
typedef int(* | osmo_stream_cli_read_cb2_t) (struct osmo_stream_cli *cli, int res, struct msgb *msg) |
Completion call-back function when something was read from from the stream client socket. More... | |
typedef int(* | osmo_stream_cli_segmentation_cb_t) (struct msgb *msg) |
typedef int(* | osmo_stream_cli_segmentation_cb2_t) (struct osmo_stream_cli *cli, struct msgb *msg) |
Functions | |
void | osmo_stream_cli_set_name (struct osmo_stream_cli *cli, const char *name) |
Set a name on the cli object (used during logging). More... | |
const char * | osmo_stream_cli_get_name (const struct osmo_stream_cli *cli) |
Retrieve name previously set on the cli object (see osmo_stream_cli_set_name()). More... | |
void | osmo_stream_cli_set_nodelay (struct osmo_stream_cli *cli, bool nodelay) |
Set the NODELAY socket option to avoid Nagle-like behavior. More... | |
int | osmo_stream_cli_set_priority (struct osmo_stream_cli *cli, int sk_prio) |
Set the priority value of the stream socket. More... | |
int | osmo_stream_cli_set_ip_dscp (struct osmo_stream_cli *cli, uint8_t ip_dscp) |
Set the DSCP (differentiated services code point) of the stream socket. More... | |
void | osmo_stream_cli_set_addr (struct osmo_stream_cli *cli, const char *addr) |
Set the remote address to which we connect. More... | |
int | osmo_stream_cli_set_addrs (struct osmo_stream_cli *cli, const char **addr, size_t addrcnt) |
Set the remote address set to which we connect. More... | |
void | osmo_stream_cli_set_port (struct osmo_stream_cli *cli, uint16_t port) |
Set the remote port number to which we connect. More... | |
int | osmo_stream_cli_set_type (struct osmo_stream_cli *cli, int type) |
Set the socket type for the stream server link. More... | |
int | osmo_stream_cli_set_domain (struct osmo_stream_cli *cli, int domain) |
Set the socket type for the stream server link. More... | |
void | osmo_stream_cli_set_proto (struct osmo_stream_cli *cli, uint16_t proto) |
Set the protocol for the stream client socket. More... | |
void | osmo_stream_cli_set_local_addr (struct osmo_stream_cli *cli, const char *addr) |
Set the local address for the socket (to be bound to). More... | |
int | osmo_stream_cli_set_local_addrs (struct osmo_stream_cli *cli, const char **addr, size_t addrcnt) |
Set the local address set to which we bind. More... | |
void | osmo_stream_cli_set_local_port (struct osmo_stream_cli *cli, uint16_t port) |
Set the local port number for the socket (to be bound to). More... | |
void | osmo_stream_cli_set_data (struct osmo_stream_cli *cli, void *data) |
Set application private data of the stream client socket. More... | |
void | osmo_stream_cli_set_reconnect_timeout (struct osmo_stream_cli *cli, int timeout) |
Set the reconnect time of the stream client socket. More... | |
void * | osmo_stream_cli_get_data (struct osmo_stream_cli *cli) |
Retrieve application private data of the stream client socket. More... | |
int | osmo_stream_cli_set_tx_queue_max_length (struct osmo_stream_cli *cli, unsigned int size) |
Set the maximum length queue of the stream client. More... | |
char * | osmo_stream_cli_get_sockname (const struct osmo_stream_cli *cli) |
Retrieve the stream client socket description. More... | |
struct osmo_fd * | osmo_stream_cli_get_ofd (struct osmo_stream_cli *cli) |
Retrieve Osmocom File Descriptor of the stream client socket. More... | |
int | osmo_stream_cli_get_fd (const struct osmo_stream_cli *cli) |
Retrieve file descriptor of the stream client socket. More... | |
struct osmo_io_fd * | osmo_stream_cli_get_iofd (const struct osmo_stream_cli *cli) |
Retrieve osmo_io descriptor of the stream client socket. More... | |
void | osmo_stream_cli_set_connect_cb (struct osmo_stream_cli *cli, osmo_stream_cli_connect_cb_t connect_cb) |
Set the call-back function called on connect of the stream client socket. More... | |
void | osmo_stream_cli_set_disconnect_cb (struct osmo_stream_cli *cli, osmo_stream_cli_disconnect_cb_t disconnect_cb) |
Set the call-back function called on disconnect of the stream client socket. More... | |
void | osmo_stream_cli_set_read_cb (struct osmo_stream_cli *cli, osmo_stream_cli_read_cb_t read_cb) |
Set the call-back function called to read from the stream client socket. More... | |
void | osmo_stream_cli_set_read_cb2 (struct osmo_stream_cli *cli, osmo_stream_cli_read_cb2_t read_cb) |
Set the call-back function called to read from the stream client socket. More... | |
void | osmo_stream_cli_set_segmentation_cb (struct osmo_stream_cli *cli, osmo_stream_cli_segmentation_cb_t segmentation_cb) |
Set the segmentation callback for the client. More... | |
void | osmo_stream_cli_set_segmentation_cb2 (struct osmo_stream_cli *cli, osmo_stream_cli_segmentation_cb2_t segmentation_cb2) |
Set the segmentation callback for the client. More... | |
void | osmo_stream_cli_reconnect (struct osmo_stream_cli *cli) |
Re-connect an Osmocom Stream Client. More... | |
bool | osmo_stream_cli_is_connected (struct osmo_stream_cli *cli) |
Check if Osmocom Stream Client is in connected state. More... | |
struct osmo_stream_cli * | osmo_stream_cli_create (void *ctx) |
Create an Osmocom stream client. More... | |
void | osmo_stream_cli_destroy (struct osmo_stream_cli *cli) |
Destroy a Osmocom stream client (includes close). More... | |
int | osmo_stream_cli_open (struct osmo_stream_cli *cli) |
Open connection of an Osmocom stream client. More... | |
int | osmo_stream_cli_open2 (struct osmo_stream_cli *cli, int reconnect) |
DEPRECATED: use osmo_stream_cli_set_reconnect_timeout() or osmo_stream_cli_reconnect() instead! Open connection of an Osmocom stream client. More... | |
void | osmo_stream_cli_close (struct osmo_stream_cli *cli) |
Close an Osmocom Stream Client. More... | |
void | osmo_stream_cli_send (struct osmo_stream_cli *cli, struct msgb *msg) |
Enqueue data to be sent via an Osmocom stream client. More... | |
int | osmo_stream_cli_recv (struct osmo_stream_cli *cli, struct msgb *msg) |
Receive data via an Osmocom stream client in osmo_fd mode. More... | |
void | osmo_stream_cli_clear_tx_queue (struct osmo_stream_cli *cli) |
Clear the transmit queue of the stream client. More... | |
int | osmo_stream_cli_set_param (struct osmo_stream_cli *cli, enum osmo_stream_cli_param par, void *val, size_t val_len) |
Set given parameter of stream client to given value. More... | |
This code is intended to abstract any client use of stream-type sockets, such as TCP and SCTP.
An osmo_stream_cli represents a client implementation of a SOCK_STREAM or SOCK_SEQPACKET socket. It contains all the common logic like non-blocking outbound connect to a remote server, re-connecting after disconnect or connect failure, etc.
osmo_stream_cli can operate in two different modes:
For any new applications, you definitely should use the modern mode, as it provides you with a higher layer of abstraction and allows you to perform efficient I/O using the io_uring backend of osmo_io.
The modern mode is chosen by invoking osmo_stream_cli_set_read_cb2(). The legacy mode is chosen by invoking the older osmo_stream_cli_set_read_cb().
A typical usage of osmo_stream_cli would look as follows:
Once the connection is established, your connect_cb is called to notify you.
You may send data to the connection using osmo_tream_cli_send().
Any received inbound data on the connection is reported vie the read_cb.
typedef int(* osmo_stream_cli_read_cb2_t) (struct osmo_stream_cli *cli, int res, struct msgb *msg) |
Completion call-back function when something was read from from the stream client socket.
[in] | cli | Stream Client that got receive event. |
[in] | res | return value of the read()/recvmsg()/... call, or -errno in case of error. |
[in] | msg | message buffer containing the read data. Ownership is transferred to the call-back, and it must make sure to msgb_free() it eventually! |
void osmo_stream_cli_clear_tx_queue | ( | struct osmo_stream_cli * | cli | ) |
Clear the transmit queue of the stream client.
Calling this function wil clear (delete) any pending, not-yet transmitted data from the transmit queue.
void osmo_stream_cli_close | ( | struct osmo_stream_cli * | cli | ) |
Close an Osmocom Stream Client.
[in] | cli | Osmocom Stream Client to be closed We unregister the socket fd from the osmocom select() loop abstraction and close the socket |
struct osmo_stream_cli * osmo_stream_cli_create | ( | void * | ctx | ) |
Create an Osmocom stream client.
[in] | ctx | talloc context from which to allocate memory This function allocates a new osmo_stream_cli and initializes it with default values (5s reconnect timer, TCP protocol) |
void osmo_stream_cli_destroy | ( | struct osmo_stream_cli * | cli | ) |
Destroy a Osmocom stream client (includes close).
[in] | cli | Stream Client to destroy |
void * osmo_stream_cli_get_data | ( | struct osmo_stream_cli * | cli | ) |
Retrieve application private data of the stream client socket.
[in] | cli | Stream Client to modify |
int osmo_stream_cli_get_fd | ( | const struct osmo_stream_cli * | cli | ) |
Retrieve file descriptor of the stream client socket.
[in] | cli | Stream Client of which we want to obtain the file descriptor |
Referenced by osmo_stream_cli_get_sockname().
struct osmo_io_fd * osmo_stream_cli_get_iofd | ( | const struct osmo_stream_cli * | cli | ) |
Retrieve osmo_io descriptor of the stream client socket.
This function must not be called on a stream client in legacy osmo_fd mode! The iofd is only valid once/after osmo_stream_cli_open() has successfully returned.
[in] | cli | Stream Client of which we want to obtain the file descriptor |
const char * osmo_stream_cli_get_name | ( | const struct osmo_stream_cli * | cli | ) |
Retrieve name previously set on the cli object (see osmo_stream_cli_set_name()).
[in] | cli | stream_cli whose name is to be retrieved |
struct osmo_fd * osmo_stream_cli_get_ofd | ( | struct osmo_stream_cli * | cli | ) |
Retrieve Osmocom File Descriptor of the stream client socket.
This function only works in case you operate osmo_stream_cli in osmo_fd mode!
[in] | cli | Stream Client to modify |
char * osmo_stream_cli_get_sockname | ( | const struct osmo_stream_cli * | cli | ) |
Retrieve the stream client socket description.
Calling this function will build a string that describes the socket in terms of its local/remote address/port. The returned name is stored in a static buffer; it is hence not re-entrant or thread-safe.
[in] | cli | Stream Client to examine |
References osmo_stream_cli_get_fd().
bool osmo_stream_cli_is_connected | ( | struct osmo_stream_cli * | cli | ) |
Check if Osmocom Stream Client is in connected state.
[in] | cli | Osmocom Stream Client |
Referenced by osmo_stream_cli_send().
int osmo_stream_cli_open | ( | struct osmo_stream_cli * | cli | ) |
Open connection of an Osmocom stream client.
This will initiate an non-blocking outbound connect to the configured destination (server) address. By default the client will automatically attempt to reconnect after default timeout. To disable this, use osmo_stream_cli_set_reconnect_timeout() before calling this function.
[in] | cli | Stream Client to connect |
int osmo_stream_cli_open2 | ( | struct osmo_stream_cli * | cli, |
int | reconnect | ||
) |
DEPRECATED: use osmo_stream_cli_set_reconnect_timeout() or osmo_stream_cli_reconnect() instead! Open connection of an Osmocom stream client.
[in] | cli | Stream Client to connect |
[in] | reconect | 1 if we should not automatically reconnect |
void osmo_stream_cli_reconnect | ( | struct osmo_stream_cli * | cli | ) |
Re-connect an Osmocom Stream Client.
If re-connection is enabled for this client (which is the case unless negative timeout was explicitly set via osmo_stream_cli_set_reconnect_timeout() call), we close any existing connection (if any) and schedule a re-connect timer
int osmo_stream_cli_recv | ( | struct osmo_stream_cli * | cli, |
struct msgb * | msg | ||
) |
Receive data via an Osmocom stream client in osmo_fd mode.
[in] | cli | Stream Client through which we want to send |
msg | pre-allocate message buffer to which received data is appended |
Application programs using the legacy osmo_fd mode of osmo_stream_cli will use this function to read/receive from a stream client socket after they have been notified that it is readable (via select/poll).
If conn is an SCTP connection, additional specific considerations shall be taken:
void osmo_stream_cli_send | ( | struct osmo_stream_cli * | cli, |
struct msgb * | msg | ||
) |
Enqueue data to be sent via an Osmocom stream client.
This is the function you use for writing/sending/transmitting data via the osmo_stream_cli.
[in] | cli | Stream Client through which we want to send |
[in] | msg | Message buffer to enqueue in transmit queue |
References osmo_stream_cli_is_connected().
void osmo_stream_cli_set_addr | ( | struct osmo_stream_cli * | cli, |
const char * | addr | ||
) |
Set the remote address to which we connect.
Any changes to this setting will only become active upon next (re)connect.
[in] | cli | Stream Client to modify |
[in] | addr | Remote IP address |
References osmo_stream_cli_set_addrs().
int osmo_stream_cli_set_addrs | ( | struct osmo_stream_cli * | cli, |
const char ** | addr, | ||
size_t | addrcnt | ||
) |
Set the remote address set to which we connect.
Useful for protocols allowing connecting to more than one address (such as SCTP) Any changes to this setting will only become active upon next (re)connect.
[in] | cli | Stream Client to modify |
[in] | addr | Remote IP address set |
Referenced by osmo_stream_cli_set_addr().
void osmo_stream_cli_set_connect_cb | ( | struct osmo_stream_cli * | cli, |
osmo_stream_cli_connect_cb_t | connect_cb | ||
) |
Set the call-back function called on connect of the stream client socket.
The call-back function registered via this function will be called upon completion of the non-blocking outbound connect operation.
[in] | cli | Stream Client to modify |
[in] | connect_cb | Call-back function to be called upon connect |
void osmo_stream_cli_set_data | ( | struct osmo_stream_cli * | cli, |
void * | data | ||
) |
Set application private data of the stream client socket.
[in] | cli | Stream Client to modify |
[in] | data | User-specific data (available in call-back functions) |
void osmo_stream_cli_set_disconnect_cb | ( | struct osmo_stream_cli * | cli, |
osmo_stream_cli_disconnect_cb_t | disconnect_cb | ||
) |
Set the call-back function called on disconnect of the stream client socket.
[in] | cli | Stream Client to modify |
[in] | disconnect_cb | Call-back function to be called upon disconnect |
int osmo_stream_cli_set_domain | ( | struct osmo_stream_cli * | cli, |
int | domain | ||
) |
Set the socket type for the stream server link.
Any changes to this setting will only become active upon next (re)connect.
[in] | cli | Stream Client to modify |
[in] | type | Socket Domain (like AF_UNSPEC (default for IP), AF_UNIX, AF_INET, ...) |
int osmo_stream_cli_set_ip_dscp | ( | struct osmo_stream_cli * | cli, |
uint8_t | ip_dscp | ||
) |
Set the DSCP (differentiated services code point) of the stream socket.
Setting this will automatically set the IP DSCP option on any socket established via osmo_stream_cli_open or any re-connect. This can be set either before or after opening the socket.
[in] | cli | Stream client whose sockets are to be configured |
[in] | ip_dscp | DSCP value. Value range 0..63. |
void osmo_stream_cli_set_local_addr | ( | struct osmo_stream_cli * | cli, |
const char * | addr | ||
) |
Set the local address for the socket (to be bound to).
Any changes to this setting will only become active upon next (re)connect.
[in] | cli | Stream Client to modify |
[in] | port | Local host name |
References osmo_stream_cli_set_local_addrs().
int osmo_stream_cli_set_local_addrs | ( | struct osmo_stream_cli * | cli, |
const char ** | addr, | ||
size_t | addrcnt | ||
) |
Set the local address set to which we bind.
Useful for protocols allowing bind to more than one address (such as SCTP) Any changes to this setting will only become active upon next (re)connect.
[in] | cli | Stream Client to modify |
[in] | addr | Local IP address set |
Referenced by osmo_stream_cli_set_local_addr().
void osmo_stream_cli_set_local_port | ( | struct osmo_stream_cli * | cli, |
uint16_t | port | ||
) |
Set the local port number for the socket (to be bound to).
Any changes to this setting will only become active upon next (re)connect.
[in] | cli | Stream Client to modify |
[in] | port | Local port number |
void osmo_stream_cli_set_name | ( | struct osmo_stream_cli * | cli, |
const char * | name | ||
) |
Set a name on the cli object (used during logging).
[in] | cli | stream_cli whose name is to be set |
[in] | name | the name to be set on cli |
void osmo_stream_cli_set_nodelay | ( | struct osmo_stream_cli * | cli, |
bool | nodelay | ||
) |
Set the NODELAY socket option to avoid Nagle-like behavior.
Setting this to nodelay=true will automatically set the NODELAY socket option on any socket established via osmo_stream_cli_open or any re-connect. This can be set either before or after opening the socket.
[in] | cli | Stream client whose sockets are to be configured |
[in] | nodelay | whether to set (true) NODELAY before connect() |
int osmo_stream_cli_set_param | ( | struct osmo_stream_cli * | cli, |
enum osmo_stream_cli_param | par, | ||
void * | val, | ||
size_t | val_len | ||
) |
Set given parameter of stream client to given value.
[in] | cli | stream client on which to set parameter. |
[in] | par | identifier of the parameter to be set. |
[in] | val | value of the parameter to be set. |
[in] | val_len | length of the parameter value. |
void osmo_stream_cli_set_port | ( | struct osmo_stream_cli * | cli, |
uint16_t | port | ||
) |
Set the remote port number to which we connect.
Any changes to this setting will only become active upon next (re)connect.
[in] | cli | Stream Client to modify |
[in] | port | Remote port number |
int osmo_stream_cli_set_priority | ( | struct osmo_stream_cli * | cli, |
int | sk_prio | ||
) |
Set the priority value of the stream socket.
Setting this will automatically set the socket priority option on any socket established via osmo_stream_cli_open or any re-connect. This can be set either before or after opening the socket.
[in] | cli | Stream client whose sockets are to be configured |
[in] | sk_prio | priority value. Values outside 0..6 require CAP_NET_ADMIN. |
void osmo_stream_cli_set_proto | ( | struct osmo_stream_cli * | cli, |
uint16_t | proto | ||
) |
Set the protocol for the stream client socket.
Any changes to this setting will only become active upon next (re)connect.
[in] | cli | Stream Client to modify |
[in] | proto | Protocol (like IPPROTO_TCP (default), IPPROTO_SCTP, ...) |
void osmo_stream_cli_set_read_cb | ( | struct osmo_stream_cli * | cli, |
osmo_stream_cli_read_cb_t | read_cb | ||
) |
Set the call-back function called to read from the stream client socket.
This function will implicitly configure osmo_stream_cli to use legacy osmo_ofd mode.
[in] | cli | Stream Client to modify |
[in] | read_cb | Call-back function to be called when we want to read |
void osmo_stream_cli_set_read_cb2 | ( | struct osmo_stream_cli * | cli, |
osmo_stream_cli_read_cb2_t | read_cb | ||
) |
Set the call-back function called to read from the stream client socket.
This function will implicitly configure osmo_stream_cli to use osmo_iofd mode.
[in] | cli | Stream Client to modify |
[in] | read_cb | Call-back function to be called when data was read from the socket |
void osmo_stream_cli_set_reconnect_timeout | ( | struct osmo_stream_cli * | cli, |
int | timeout | ||
) |
Set the reconnect time of the stream client socket.
[in] | cli | Stream Client to modify |
[in] | timeout | Re-connect timeout in seconds or negative value to disable auto-reconnection |
void osmo_stream_cli_set_segmentation_cb | ( | struct osmo_stream_cli * | cli, |
osmo_stream_cli_segmentation_cb_t | segmentation_cb | ||
) |
Set the segmentation callback for the client.
[in,out] | cli | Stream Client to modify |
[in] | segmentation_cb | Target segmentation callback |
A segmentation call-back can optionally be used when a packet based protocol (like TCP) is used within a STREAM style socket that does not preserve message boundaries within the stream. If a segmentation call-back is given, the osmo_stream_srv library code will makes sure that the read_cb called only for complete single messages, and not arbitrary segments of the stream.
void osmo_stream_cli_set_segmentation_cb2 | ( | struct osmo_stream_cli * | cli, |
osmo_stream_cli_segmentation_cb2_t | segmentation_cb2 | ||
) |
Set the segmentation callback for the client.
[in,out] | cli | Stream Client to modify |
[in] | segmentation_cb2 | Target segmentation callback |
Same as osmo_stream_cli_set_segmentation_cb(), but a osmo_stream_cli_segmentation_cb2_t is called instead which allows access to the related cli object.
int osmo_stream_cli_set_tx_queue_max_length | ( | struct osmo_stream_cli * | cli, |
unsigned int | size | ||
) |
Set the maximum length queue of the stream client.
[in] | cli | Stream Client to modify |
[in] | size | maximum amount of msgbs which can be queued in the internal tx queue. |
The maximum length queue default value is 1024 msgbs.
int osmo_stream_cli_set_type | ( | struct osmo_stream_cli * | cli, |
int | type | ||
) |
Set the socket type for the stream server link.
Any changes to this setting will only become active upon next (re)connect.
[in] | cli | Stream Client to modify |
[in] | type | Socket Type (like SOCK_STREAM (default), SOCK_SEQPACKET, ...) |