libosmo-netif  1.4.0.49-5fec.202404162026
Osmocom network interface library
stream.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <unistd.h>
6 
7 #include <osmocom/core/msgb.h>
8 
12 #define OSMO_STREAM_SCTP_MSG_FLAGS_NOTIFICATION 0x80 /* sctp_recvmsg() flags=MSG_NOTIFICATION, msgb_data() contains "union sctp_notification*" */
13 #define msgb_sctp_msg_flags(msg) (msg)->cb[2]
14 
16 #define msgb_sctp_ppid(msg) (msg)->cb[3]
18 #define msgb_sctp_stream(msg) (msg)->cb[4]
19 
63 struct osmo_stream_srv_link;
64 
65 struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx);
66 void osmo_stream_srv_link_destroy(struct osmo_stream_srv_link *link);
67 
68 void osmo_stream_srv_link_set_name(struct osmo_stream_srv_link *link, const char *name);
69 const char *osmo_stream_srv_link_get_name(const struct osmo_stream_srv_link *link);
70 void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay);
71 void osmo_stream_srv_link_set_addr(struct osmo_stream_srv_link *link, const char *addr);
72 int osmo_stream_srv_link_set_addrs(struct osmo_stream_srv_link *link, const char **addr, size_t addrcnt);
73 void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port);
74 void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto);
75 int osmo_stream_srv_link_set_type(struct osmo_stream_srv_link *link, int type);
76 int osmo_stream_srv_link_set_domain(struct osmo_stream_srv_link *link, int domain);
77 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));
78 void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data);
79 void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link);
80 char *osmo_stream_srv_link_get_sockname(const struct osmo_stream_srv_link *link);
81 struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link);
82 int osmo_stream_srv_link_get_fd(const struct osmo_stream_srv_link *link);
83 bool osmo_stream_srv_link_is_opened(const struct osmo_stream_srv_link *link);
84 int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link);
85 void osmo_stream_srv_link_close(struct osmo_stream_srv_link *link);
86 
87 enum osmo_stream_srv_link_param {
88  OSMO_STREAM_SRV_LINK_PAR_SCTP_SOCKOPT_AUTH_SUPPORTED, /* uint8_t: 0 disable, 1 enable, 2 force disable, 3 force enable */
89  OSMO_STREAM_SRV_LINK_PAR_SCTP_SOCKOPT_ASCONF_SUPPORTED, /* uint8_t: 0 disable, 1 enable, 2 force disable, 3 force enable */
90  OSMO_STREAM_SRV_LINK_PAR_SCTP_INIT_NUM_OSTREAMS, /* uint16_t: amount of streams */
91  OSMO_STREAM_SRV_LINK_PAR_SCTP_INIT_MAX_INSTREAMS, /* uint16_t: amount of streams */
92 };
93 
94 int osmo_stream_srv_link_set_param(struct osmo_stream_srv_link *link, enum osmo_stream_srv_link_param par,
95  void *val, size_t val_len);
96 
99 struct osmo_stream_srv;
100 
101 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);
102 struct osmo_stream_srv *osmo_stream_srv_create2(void *ctx, struct osmo_stream_srv_link *link, int fd, void *data);
103 void osmo_stream_srv_set_name(struct osmo_stream_srv *conn, const char *name);
104 const char *osmo_stream_srv_get_name(const struct osmo_stream_srv *conn);
105 void osmo_stream_srv_set_read_cb(struct osmo_stream_srv *conn, int (*read_cb)(struct osmo_stream_srv *conn, struct msgb *msg));
106 void osmo_stream_srv_set_closed_cb(struct osmo_stream_srv *conn, int (*closed_cb)(struct osmo_stream_srv *conn));
107 void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn);
108 struct osmo_stream_srv_link *osmo_stream_srv_get_master(struct osmo_stream_srv *conn);
109 const char *osmo_stream_srv_get_sockname(const struct osmo_stream_srv *conn);
110 struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv);
111 int osmo_stream_srv_get_fd(const struct osmo_stream_srv *srv);
112 struct osmo_io_fd *osmo_stream_srv_get_iofd(const struct osmo_stream_srv *srv);
113 void osmo_stream_srv_destroy(struct osmo_stream_srv *conn);
114 
115 void osmo_stream_srv_set_flush_and_destroy(struct osmo_stream_srv *conn);
116 void osmo_stream_srv_set_data(struct osmo_stream_srv *conn, void *data);
117 
118 void osmo_stream_srv_set_segmentation_cb(struct osmo_stream_srv *conn,
119  int (*segmentation_cb)(struct msgb *msg));
120 
121 void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg);
122 int osmo_stream_srv_recv(struct osmo_stream_srv *conn, struct msgb *msg);
123 
124 void osmo_stream_srv_clear_tx_queue(struct osmo_stream_srv *conn);
125 
161 struct osmo_stream_cli;
162 
163 void osmo_stream_cli_set_name(struct osmo_stream_cli *cli, const char *name);
164 const char *osmo_stream_cli_get_name(const struct osmo_stream_cli *cli);
165 void osmo_stream_cli_set_nodelay(struct osmo_stream_cli *cli, bool nodelay);
166 void osmo_stream_cli_set_addr(struct osmo_stream_cli *cli, const char *addr);
167 int osmo_stream_cli_set_addrs(struct osmo_stream_cli *cli, const char **addr, size_t addrcnt);
168 void osmo_stream_cli_set_port(struct osmo_stream_cli *cli, uint16_t port);
169 int osmo_stream_cli_set_type(struct osmo_stream_cli *cli, int type);
170 int osmo_stream_cli_set_domain(struct osmo_stream_cli *cli, int domain);
171 void osmo_stream_cli_set_proto(struct osmo_stream_cli *cli, uint16_t proto);
172 void osmo_stream_cli_set_local_addr(struct osmo_stream_cli *cli, const char *addr);
173 int osmo_stream_cli_set_local_addrs(struct osmo_stream_cli *cli, const char **addr, size_t addrcnt);
174 void osmo_stream_cli_set_local_port(struct osmo_stream_cli *cli, uint16_t port);
175 void osmo_stream_cli_set_data(struct osmo_stream_cli *cli, void *data);
176 void osmo_stream_cli_set_reconnect_timeout(struct osmo_stream_cli *cli, int timeout);
177 void *osmo_stream_cli_get_data(struct osmo_stream_cli *cli);
178 char *osmo_stream_cli_get_sockname(const struct osmo_stream_cli *cli);
179 struct osmo_fd *osmo_stream_cli_get_ofd(struct osmo_stream_cli *cli);
180 int osmo_stream_cli_get_fd(const struct osmo_stream_cli *cli);
181 struct osmo_io_fd *osmo_stream_cli_get_iofd(const struct osmo_stream_cli *cli);
182 void osmo_stream_cli_set_connect_cb(struct osmo_stream_cli *cli, int (*connect_cb)(struct osmo_stream_cli *cli));
183 void osmo_stream_cli_set_disconnect_cb(struct osmo_stream_cli *cli, int (*disconnect_cb)(struct osmo_stream_cli *cli));
184 void osmo_stream_cli_set_read_cb(struct osmo_stream_cli *cli, int (*read_cb)(struct osmo_stream_cli *cli));
185 void osmo_stream_cli_set_read_cb2(struct osmo_stream_cli *cli, int (*read_cb)(struct osmo_stream_cli *cli, struct msgb *msg));
186 void osmo_stream_cli_set_segmentation_cb(struct osmo_stream_cli *cli, int (*segmentation_cb)(struct msgb *msg));
187 void osmo_stream_cli_reconnect(struct osmo_stream_cli *cli);
188 bool osmo_stream_cli_is_connected(struct osmo_stream_cli *cli);
189 
190 struct osmo_stream_cli *osmo_stream_cli_create(void *ctx);
191 void osmo_stream_cli_destroy(struct osmo_stream_cli *cli);
192 
193 int osmo_stream_cli_open(struct osmo_stream_cli *cli);
194 int osmo_stream_cli_open2(struct osmo_stream_cli *cli, int reconnect) \
195  OSMO_DEPRECATED("Use osmo_stream_cli_set_reconnect_timeout() or osmo_stream_cli_reconnect() instead");
196 void osmo_stream_cli_close(struct osmo_stream_cli *cli);
197 
198 void osmo_stream_cli_send(struct osmo_stream_cli *cli, struct msgb *msg);
199 int osmo_stream_cli_recv(struct osmo_stream_cli *cli, struct msgb *msg);
200 
201 void osmo_stream_cli_clear_tx_queue(struct osmo_stream_cli *cli);
202 
203 enum osmo_stream_cli_param {
204  OSMO_STREAM_CLI_PAR_SCTP_SOCKOPT_AUTH_SUPPORTED, /* uint8_t: 0 disable, 1 enable, 2 force disable, 3 force enable */
205  OSMO_STREAM_CLI_PAR_SCTP_SOCKOPT_ASCONF_SUPPORTED, /* uint8_t: 0 disable, 1 enable, 2 force disable, 3 force enable */
206  OSMO_STREAM_CLI_PAR_SCTP_INIT_NUM_OSTREAMS, /* uint16_t: amount of streams */
207  OSMO_STREAM_CLI_PAR_SCTP_INIT_MAX_INSTREAMS, /* uint16_t: amount of streams */
208  OSMO_STREAM_CLI_PAR_SCTP_INIT_MAX_ATTEMPTS, /* uint16_t: amount of attempts */
209  OSMO_STREAM_CLI_PAR_SCTP_INIT_TIMEOUT, /* uint16_t: milliseconds */
210 };
211 
212 int osmo_stream_cli_set_param(struct osmo_stream_cli *cli, enum osmo_stream_cli_param par,
213  void *val, size_t val_len);
214 
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:559
struct osmo_fd * osmo_stream_cli_get_ofd(struct osmo_stream_cli *cli)
Retrieve Osmocom File Descriptor of the stream client socket.
Definition: stream_cli.c:775
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.
Definition: stream_cli.c:1166
int osmo_stream_cli_recv(struct osmo_stream_cli *cli, struct msgb *msg)
Receive data via an Osmocom stream client in osmo_fd mode.
Definition: stream_cli.c:1089
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.
Definition: stream_cli.c:632
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()).
Definition: stream_cli.c:548
void osmo_stream_cli_reconnect(struct osmo_stream_cli *cli)
Re-connect an Osmocom Stream Client.
Definition: stream_cli.c:128
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!...
Definition: stream_cli.c:844
struct osmo_stream_cli * osmo_stream_cli_create(void *ctx)
Create an Osmocom stream client.
Definition: stream_cli.c:418
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:796
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:713
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:608
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:693
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:742
void * osmo_stream_cli_get_data(struct osmo_stream_cli *cli)
Retrieve application private data of the stream client socket.
Definition: stream_cli.c:750
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:620
bool osmo_stream_cli_is_connected(struct osmo_stream_cli *cli)
Check if Osmocom Stream Client is in connected state.
Definition: stream_cli.c:147
int osmo_stream_cli_open(struct osmo_stream_cli *cli)
Open connection of an Osmocom stream client.
Definition: stream_cli.c:918
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:657
void osmo_stream_cli_destroy(struct osmo_stream_cli *cli)
Destroy a Osmocom stream client (includes close).
Definition: stream_cli.c:830
void osmo_stream_cli_set_nodelay(struct osmo_stream_cli *cli, bool nodelay)
Set the NODELAY socket option to avoid Nagle-like behavior.
Definition: stream_cli.c:904
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.
Definition: stream_cli.c:571
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:1037
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:787
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:596
struct osmo_io_fd * osmo_stream_cli_get_iofd(const struct osmo_stream_cli *cli)
Retrieve osmo_io descriptor of the stream client socket.
Definition: stream_cli.c:231
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.
Definition: stream_cli.c:807
void osmo_stream_cli_close(struct osmo_stream_cli *cli)
Close an Osmocom Stream Client.
Definition: stream_cli.c:174
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.
Definition: stream_cli.c:820
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:733
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:679
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:537
char * osmo_stream_cli_get_sockname(const struct osmo_stream_cli *cli)
Retrieve the stream client socket description.
Definition: stream_cli.c:760
void osmo_stream_cli_clear_tx_queue(struct osmo_stream_cli *cli)
Clear the transmit queue of the stream client.
Definition: stream_cli.c:1142
int osmo_stream_cli_get_fd(const struct osmo_stream_cli *cli)
Retrieve file descriptor of the stream client socket.
Definition: stream_cli.c:211
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:296
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:901
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:441
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 legacy osmo_fd mode Stream Server inside the specified link.
Definition: stream_srv.c:814
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:282
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.
Definition: stream_srv.c:976
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:513
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.
Definition: stream_srv.c:219
struct osmo_fd * osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv)
Retrieve Osmocom File Descriptor of a stream server in osmo_fd mode.
Definition: stream_srv.c:1016
void osmo_stream_srv_destroy(struct osmo_stream_srv *conn)
Destroy given Stream Server.
Definition: stream_srv.c:1064
const char * osmo_stream_srv_get_sockname(const struct osmo_stream_srv *conn)
Retrieve the stream server socket description.
Definition: stream_srv.c:1002
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:336
int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link)
Open the stream server link.
Definition: stream_srv.c:460
const char * osmo_stream_srv_link_get_name(const struct osmo_stream_srv_link *link)
Retrieve name previously set on the srv_link object (see osmo_stream_srv_link_set_name()).
Definition: stream_srv.c:207
int osmo_stream_srv_link_set_param(struct osmo_stream_srv_link *link, enum osmo_stream_srv_link_param par, void *val, size_t val_len)
Set given parameter of stream_srv_link to given value.
Definition: stream_srv.c:543
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:269
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:938
struct osmo_fd * osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link)
Retrieve Osmocom File Descriptor of the stream server link.
Definition: stream_srv.c:422
char * osmo_stream_srv_link_get_sockname(const struct osmo_stream_srv_link *link)
Retrieve description of the stream server link e.
Definition: stream_srv.c:409
int osmo_stream_srv_get_fd(const struct osmo_stream_srv *srv)
Retrieve File Descriptor of the stream server.
Definition: stream_srv.c:1026
void osmo_stream_srv_set_flush_and_destroy(struct osmo_stream_srv *conn)
Prepare to send out all pending messages on the connection's Tx queue.
Definition: stream_srv.c:949
void osmo_stream_srv_link_destroy(struct osmo_stream_srv_link *link)
Destroy the stream server link.
Definition: stream_srv.c:450
struct osmo_stream_srv * osmo_stream_srv_create2(void *ctx, struct osmo_stream_srv_link *link, int fd, void *data)
Create an osmo_iofd mode Stream Server inside the specified link.
Definition: stream_srv.c:857
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:316
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:958
struct osmo_stream_srv_link * osmo_stream_srv_get_master(struct osmo_stream_srv *conn)
Retrieve the master (Link) from a Stream Server.
Definition: stream_srv.c:1054
struct osmo_io_fd * osmo_stream_srv_get_iofd(const struct osmo_stream_srv *srv)
Retrieve osmo_io descriptor of the stream server socket.
Definition: stream_srv.c:1045
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:1088
struct osmo_stream_srv_link * osmo_stream_srv_link_create(void *ctx)
Create an Osmocom Stream Server Link.
Definition: stream_srv.c:175
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.
Definition: stream_srv.c:245
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 for incoming data on an osmo_io stream_srv.
Definition: stream_srv.c:926
int osmo_stream_srv_recv(struct osmo_stream_srv *conn, struct msgb *msg)
Receive data via an Osmocom stream server in osmo_fd mode.
Definition: stream_srv.c:1137
void * osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link)
Retrieve application private data of the stream server link.
Definition: stream_srv.c:345
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:232
void osmo_stream_srv_link_close(struct osmo_stream_srv_link *link)
Close the stream server link and unregister from select loop.
Definition: stream_srv.c:527
const char * osmo_stream_srv_get_name(const struct osmo_stream_srv *conn)
Retrieve name previously set on the srv object (see osmo_stream_srv_set_name()).
Definition: stream_srv.c:912
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:198
void * osmo_stream_srv_get_data(struct osmo_stream_srv *conn)
Retrieve application private data of the stream server.
Definition: stream_srv.c:993
int osmo_stream_srv_link_get_fd(const struct osmo_stream_srv_link *link)
Retrieve File Descriptor of the stream server link.
Definition: stream_srv.c:430