libosmo-netif  1.4.0.10-8601.202311102026
Osmocom network interface library
stream.h
1 #pragma once
2 
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <unistd.h>
6 
7 #include <osmocom/core/msgb.h>
8 
14 #define OSMO_STREAM_SCTP_MSG_FLAGS_NOTIFICATION 0x80 /* sctp_recvmsg() flags=MSG_NOTIFICATION, msgb_data() contains "union sctp_notification*" */
15 #define msgb_sctp_msg_flags(msg) (msg)->cb[2]
16 
18 #define msgb_sctp_ppid(msg) (msg)->cb[3]
19 
20 #define msgb_sctp_stream(msg) (msg)->cb[4]
21 
24 
27 
28 void osmo_stream_srv_link_set_name(struct osmo_stream_srv_link *link, const char *name);
29 void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay);
30 void osmo_stream_srv_link_set_addr(struct osmo_stream_srv_link *link, const char *addr);
31 int osmo_stream_srv_link_set_addrs(struct osmo_stream_srv_link *link, const char **addr, size_t addrcnt);
32 void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port);
33 void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto);
34 int osmo_stream_srv_link_set_type(struct osmo_stream_srv_link *link, int type);
35 int osmo_stream_srv_link_set_domain(struct osmo_stream_srv_link *link, int domain);
36 void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)(struct osmo_stream_srv_link *link, int fd));
37 void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data);
40 struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link);
44 
45 enum osmo_stream_srv_link_param {
46  OSMO_STREAM_SRV_LINK_PAR_SCTP_SOCKOPT_AUTH_SUPPORTED, /* uint8_t: 0 disable, 1 enable, 2 force disable, 3 force enable */
47  OSMO_STREAM_SRV_LINK_PAR_SCTP_SOCKOPT_ASCONF_SUPPORTED, /* uint8_t: 0 disable, 1 enable, 2 force disable, 3 force enable */
48  OSMO_STREAM_SRV_LINK_PAR_SCTP_INIT_NUM_OSTREAMS, /* uint16_t: amount of streams */
49  OSMO_STREAM_SRV_LINK_PAR_SCTP_INIT_MAX_INSTREAMS, /* uint16_t: amount of streams */
50 };
51 
52 int osmo_stream_srv_link_set_param(struct osmo_stream_srv_link *link, enum osmo_stream_srv_link_param par,
53  void *val, size_t val_len);
54 
57 struct osmo_stream_srv;
58 
59 struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link, int fd, int (*read_cb)(struct osmo_stream_srv *conn), int (*closed_cb)(struct osmo_stream_srv *conn), void *data);
60 struct osmo_stream_srv *osmo_stream_srv_create2(void *ctx, struct osmo_stream_srv_link *link, int fd, void *data);
61 void osmo_stream_srv_set_name(struct osmo_stream_srv *conn, const char *name);
62 void osmo_stream_srv_set_read_cb(struct osmo_stream_srv *conn, int (*read_cb)(struct osmo_stream_srv *conn, struct msgb *msg));
63 void osmo_stream_srv_set_closed_cb(struct osmo_stream_srv *conn, int (*closed_cb)(struct osmo_stream_srv *conn));
64 void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn);
66 struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv);
67 void osmo_stream_srv_destroy(struct osmo_stream_srv *conn);
68 
70 void osmo_stream_srv_set_data(struct osmo_stream_srv *conn, void *data);
71 
73  int (*segmentation_cb)(struct msgb *msg));
74 
75 void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg);
76 int osmo_stream_srv_recv(struct osmo_stream_srv *conn, struct msgb *msg);
77 
78 void osmo_stream_srv_clear_tx_queue(struct osmo_stream_srv *conn);
79 
81 struct osmo_stream_cli;
82 
83 void osmo_stream_cli_set_name(struct osmo_stream_cli *cli, const char *name);
84 void osmo_stream_cli_set_nodelay(struct osmo_stream_cli *cli, bool nodelay);
85 void osmo_stream_cli_set_addr(struct osmo_stream_cli *cli, const char *addr);
86 int osmo_stream_cli_set_addrs(struct osmo_stream_cli *cli, const char **addr, size_t addrcnt);
87 void osmo_stream_cli_set_port(struct osmo_stream_cli *cli, uint16_t port);
88 int osmo_stream_cli_set_type(struct osmo_stream_cli *cli, int type);
89 int osmo_stream_cli_set_domain(struct osmo_stream_cli *cli, int domain);
90 void osmo_stream_cli_set_proto(struct osmo_stream_cli *cli, uint16_t proto);
91 void osmo_stream_cli_set_local_addr(struct osmo_stream_cli *cli, const char *addr);
92 int osmo_stream_cli_set_local_addrs(struct osmo_stream_cli *cli, const char **addr, size_t addrcnt);
93 void osmo_stream_cli_set_local_port(struct osmo_stream_cli *cli, uint16_t port);
94 void osmo_stream_cli_set_data(struct osmo_stream_cli *cli, void *data);
95 void osmo_stream_cli_set_reconnect_timeout(struct osmo_stream_cli *cli, int timeout);
96 void *osmo_stream_cli_get_data(struct osmo_stream_cli *cli);
97 char *osmo_stream_cli_get_sockname(const struct osmo_stream_cli *cli);
98 struct osmo_fd *osmo_stream_cli_get_ofd(struct osmo_stream_cli *cli);
99 void osmo_stream_cli_set_connect_cb(struct osmo_stream_cli *cli, int (*connect_cb)(struct osmo_stream_cli *cli));
100 void osmo_stream_cli_set_disconnect_cb(struct osmo_stream_cli *cli, int (*disconnect_cb)(struct osmo_stream_cli *cli));
101 void osmo_stream_cli_set_read_cb(struct osmo_stream_cli *cli, int (*read_cb)(struct osmo_stream_cli *cli));
102 void osmo_stream_cli_set_read_cb2(struct osmo_stream_cli *cli, int (*read_cb)(struct osmo_stream_cli *cli, struct msgb *msg));
103 void osmo_stream_cli_set_segmentation_cb(struct osmo_stream_cli *cli, int (*segmentation_cb)(struct msgb *msg));
106 
107 struct osmo_stream_cli *osmo_stream_cli_create(void *ctx);
108 void osmo_stream_cli_destroy(struct osmo_stream_cli *cli);
109 
110 int osmo_stream_cli_open(struct osmo_stream_cli *cli);
111 int osmo_stream_cli_open2(struct osmo_stream_cli *cli, int reconnect) \
112  OSMO_DEPRECATED("Use osmo_stream_cli_set_reconnect_timeout() or osmo_stream_cli_reconnect() instead");
113 void osmo_stream_cli_close(struct osmo_stream_cli *cli);
114 
115 void osmo_stream_cli_send(struct osmo_stream_cli *cli, struct msgb *msg);
116 int osmo_stream_cli_recv(struct osmo_stream_cli *cli, struct msgb *msg);
117 
118 void osmo_stream_cli_clear_tx_queue(struct osmo_stream_cli *cli);
119 
120 enum osmo_stream_cli_param {
121  OSMO_STREAM_CLI_PAR_SCTP_SOCKOPT_AUTH_SUPPORTED, /* uint8_t: 0 disable, 1 enable, 2 force disable, 3 force enable */
122  OSMO_STREAM_CLI_PAR_SCTP_SOCKOPT_ASCONF_SUPPORTED, /* uint8_t: 0 disable, 1 enable, 2 force disable, 3 force enable */
123  OSMO_STREAM_CLI_PAR_SCTP_INIT_NUM_OSTREAMS, /* uint16_t: amount of streams */
124  OSMO_STREAM_CLI_PAR_SCTP_INIT_MAX_INSTREAMS, /* uint16_t: amount of streams */
125  OSMO_STREAM_CLI_PAR_SCTP_INIT_MAX_ATTEMPTS, /* uint16_t: amount of attempts */
126  OSMO_STREAM_CLI_PAR_SCTP_INIT_TIMEOUT, /* uint16_t: milliseconds */
127 };
128 
129 int osmo_stream_cli_set_param(struct osmo_stream_cli *cli, enum osmo_stream_cli_param par,
130  void *val, size_t val_len);
131 
int osmo_stream_srv_link_set_addrs(struct osmo_stream_srv_link *link, const char **addr, size_t addrcnt)
Set the local address set to which we bind. Useful for protocols allowing bind on more than one addre...
Definition: stream_srv.c:240
Definition: stream_cli.c:97
void osmo_stream_cli_set_read_cb(struct osmo_stream_cli *cli, int(*read_cb)(struct osmo_stream_cli *cli))
Set the call-back function called to read from the stream client socket This function will configure ...
Definition: stream_cli.c:725
char * osmo_stream_cli_get_sockname(const struct osmo_stream_cli *cli)
Get the stream client socket description.
Definition: stream_cli.c:682
int osmo_stream_cli_set_type(struct osmo_stream_cli *cli, int type)
Set the socket type for the stream server link.
Definition: stream_cli.c:618
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...
Definition: stream_cli.c:503
int osmo_stream_cli_set_domain(struct osmo_stream_cli *cli, int domain)
Set the socket type for the stream server link.
Definition: stream_cli.c:637
void * osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link)
Get application private data of the stream server link.
Definition: stream_srv.c:336
void osmo_stream_cli_close(struct osmo_stream_cli *cli)
Close an Osmocom Stream Client.
Definition: stream_cli.c:180
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 connect. Useful for protocols allowing bind to more than one ad...
Definition: stream_cli.c:560
void osmo_stream_cli_set_port(struct osmo_stream_cli *cli, uint16_t port)
Set the remote port number to which we connect.
Definition: stream_cli.c:527
int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link)
Open the stream server link. This actually initializes the underlying socket and binds it to the conf...
Definition: stream_srv.c:391
Definition: stream_srv.c:513
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)
Definition: stream_cli.c:549
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 automati...
Definition: stream_cli.c:822
char * osmo_stream_srv_link_get_sockname(const struct osmo_stream_srv_link *link)
Get description of the stream server link e. g. 127.0.0.1:1234.
Definition: stream_srv.c:344
void osmo_stream_cli_set_reconnect_timeout(struct osmo_stream_cli *cli, int timeout)
Set the reconnect time of the stream client socket.
Definition: stream_cli.c:657
void osmo_stream_srv_destroy(struct osmo_stream_srv *conn)
Destroy given Stream Server This function closes the Stream Server socket, unregisters from select lo...
Definition: stream_srv.c:854
void * osmo_stream_cli_get_data(struct osmo_stream_cli *cli)
Get application private data of the stream client socket.
Definition: stream_cli.c:674
void osmo_stream_srv_link_close(struct osmo_stream_srv_link *link)
Close the stream server link and unregister from select loop Does not destroy the server link...
Definition: stream_srv.c:458
void osmo_stream_cli_set_disconnect_cb(struct osmo_stream_cli *cli, int(*disconnect_cb)(struct osmo_stream_cli *cli))
Set the call-back function called on disconnect of the stream client socket.
Definition: stream_cli.c:714
struct osmo_fd * osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link)
Get Osmocom File Descriptor of the stream server link.
Definition: stream_srv.c:364
void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data)
Set application private data of the stream server link.
Definition: stream_srv.c:327
void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto)
Set the protocol for the stream server link.
Definition: stream_srv.c:275
void osmo_stream_srv_set_read_cb(struct osmo_stream_srv *conn, int(*read_cb)(struct osmo_stream_srv *conn, struct msgb *msg))
Set the call-back function when data was read from the stream server socket Only for osmo_stream_srv ...
Definition: stream_srv.c:771
void * osmo_stream_srv_get_data(struct osmo_stream_srv *conn)
Get application private data of the stream server.
Definition: stream_srv.c:826
void osmo_stream_cli_send(struct osmo_stream_cli *cli, struct msgb *msg)
Enqueue data to be sent via an Osmocom stream client.
Definition: stream_cli.c:929
void osmo_stream_srv_set_closed_cb(struct osmo_stream_srv *conn, int(*closed_cb)(struct osmo_stream_srv *conn))
Set the call-back function called when the stream server socket was closed.
Definition: stream_srv.c:780
void osmo_stream_srv_link_destroy(struct osmo_stream_srv_link *link)
Destroy the stream server link. Closes + Releases Memory.
Definition: stream_srv.c:381
struct osmo_fd * osmo_stream_cli_get_ofd(struct osmo_stream_cli *cli)
Get Osmocom File Descriptor of the stream client socket.
Definition: stream_cli.c:695
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 un...
Definition: stream_cli.c:135
void osmo_stream_srv_set_name(struct osmo_stream_srv *conn, const char *name)
Set a name on the srv object (used during logging)
Definition: stream_srv.c:760
void osmo_stream_cli_set_segmentation_cb(struct osmo_stream_cli *cli, int(*segmentation_cb)(struct msgb *msg))
Set the segmentation callback for the client.
Definition: stream_cli.c:605
void osmo_stream_cli_set_addr(struct osmo_stream_cli *cli, const char *addr)
Set the remote address to which we connect.
Definition: stream_cli.c:492
void osmo_stream_srv_set_data(struct osmo_stream_srv *conn, void *data)
Set application private data of the stream server.
Definition: stream_srv.c:800
void osmo_stream_cli_set_read_cb2(struct osmo_stream_cli *cli, int(*read_cb)(struct osmo_stream_cli *cli, struct msgb *msg))
Set the call-back function called to read from the stream client socket This function will configure ...
Definition: stream_cli.c:738
struct osmo_stream_srv * osmo_stream_srv_create2(void *ctx, struct osmo_stream_srv_link *link, int fd, void *data)
Create a Stream Server inside the specified link.
Definition: stream_srv.c:724
struct osmo_stream_cli * osmo_stream_cli_create(void *ctx)
Create an Osmocom stream client.
Definition: stream_cli.c:407
int osmo_stream_srv_link_set_domain(struct osmo_stream_srv_link *link, int domain)
Set the socket type for the stream server link.
Definition: stream_srv.c:307
int osmo_stream_srv_link_set_type(struct osmo_stream_srv_link *link, int type)
Set the socket type for the stream server link.
Definition: stream_srv.c:288
bool osmo_stream_cli_is_connected(struct osmo_stream_cli *cli)
Check if Osmocom Stream Client is in connected state.
Definition: stream_cli.c:154
void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay)
Set the NODELAY socket option to avoid Nagle-like behavior Setting this to nodelay=true will automati...
Definition: stream_srv.c:216
void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg)
Enqueue data to be sent via an Osmocom stream server.
Definition: stream_srv.c:878
void osmo_stream_cli_destroy(struct osmo_stream_cli *cli)
Destroy a Osmocom stream client (includes close)
Definition: stream_cli.c:748
int osmo_stream_cli_open2(struct osmo_stream_cli *cli, int reconnect) OSMO_DEPRECATED("Use osmo_stream_cli_set_reconnect_timeout() or osmo_stream_cli_reconnect() instead")
DEPRECATED: use osmo_stream_cli_set_reconnect_timeout() or osmo_stream_cli_reconnect() instead! Open ...
Definition: stream_cli.c:762
void osmo_stream_cli_set_name(struct osmo_stream_cli *cli, const char *name)
Set a name on the cli object (used during logging)
Definition: stream_cli.c:480
struct osmo_stream_srv_link * osmo_stream_srv_get_master(struct osmo_stream_srv *conn)
Get the master (Link) from a Stream Server.
Definition: stream_srv.c:843
void osmo_stream_srv_link_set_name(struct osmo_stream_srv_link *link, const char *name)
Set a name on the srv_link object (used during logging)
Definition: stream_srv.c:204
struct osmo_stream_srv * osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link, int fd, int(*read_cb)(struct osmo_stream_srv *conn), int(*closed_cb)(struct osmo_stream_srv *conn), void *data)
Create a Stream Server inside the specified link.
Definition: stream_srv.c:686
void osmo_stream_cli_set_connect_cb(struct osmo_stream_cli *cli, int(*connect_cb)(struct osmo_stream_cli *cli))
Set the call-back function called on connect of the stream client socket.
Definition: stream_cli.c:705
void osmo_stream_srv_set_flush_and_destroy(struct osmo_stream_srv *conn)
Prepare to send out all pending messages on the connection&#39;s Tx queue and then automatically destroy ...
Definition: stream_srv.c:791
struct osmo_fd * osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv)
Get Osmocom File Descriptor of the stream server.
Definition: stream_srv.c:835
void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int(*accept_cb)(struct osmo_stream_srv_link *link, int fd))
Set the accept() call-back of the stream server link.
Definition: stream_srv.c:372
void osmo_stream_srv_set_segmentation_cb(struct osmo_stream_srv *conn, int(*segmentation_cb)(struct msgb *msg))
Set the segmentation callback for target osmo_stream_srv structure. The connection has to have been e...
Definition: stream_srv.c:810
int osmo_stream_cli_open(struct osmo_stream_cli *cli)
Open connection of an Osmocom stream client By default the client will automatically reconnect after ...
Definition: stream_cli.c:835
int osmo_stream_cli_recv(struct osmo_stream_cli *cli, struct msgb *msg)
Receive data via an Osmocom stream client.
Definition: stream_cli.c:962
void osmo_stream_srv_link_set_addr(struct osmo_stream_srv_link *link, const char *addr)
Set the local address to which we bind.
Definition: stream_srv.c:228
int osmo_stream_srv_recv(struct osmo_stream_srv *conn, struct msgb *msg)
Receive data via Osmocom stream server.
Definition: stream_srv.c:916
bool osmo_stream_srv_link_is_opened(const struct osmo_stream_srv_link *link)
Check whether the stream server link is opened.
Definition: stream_srv.c:444
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)
Definition: stream_cli.c:538
void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port)
Set the local port number to which we bind.
Definition: stream_srv.c:263
void osmo_stream_cli_set_data(struct osmo_stream_cli *cli, void *data)
Set application private data of the stream client socket.
Definition: stream_cli.c:666
void osmo_stream_cli_set_proto(struct osmo_stream_cli *cli, uint16_t proto)
Set the protocol for the stream client socket.
Definition: stream_cli.c:584
struct osmo_stream_srv_link * osmo_stream_srv_link_create(void *ctx)
Create an Osmocom Stream Server Link A Stream Server Link is the listen()+accept() "parent" to indivi...
Definition: stream_srv.c:182