Commit Graph

68 Commits

Author SHA1 Message Date
Pau Espin 3fc969e3d6 stream: Drop data during write() while in state NONE
It should not happen anyway because no fd should be active if state is
NONE, but still it's an extra check.

Change-Id: I6d58762b7d10078eb8d0981c13d35cb6f85cfe86
2020-01-28 12:30:24 +01:00
Pau Espin 5ed1a3c85c stream.c: Improve logging during sock send()
Change-Id: Iff275c809ec2bb34f471d15bfdc92296566b76a7
2020-01-09 20:38:05 +01:00
Pau Espin d1b1ff77a6 configure.ac: Introduce --{enable,disable}-libsctp configure flag
Similar to what we do in libosmocore already, we want to
deterministically enable or disable support for the feature without
having into account if the system has a libsctp. If libsctp is missing
and support is enabled, then fail.

Extra checks are also added:
* Check netinet/sctp.h header
* Check libosmocore was built with libsctp support (API
osmo_sock_init2_multiaddr() we require).
* In stream.c make sure it can be built without HAVE_LIBSCTP, and that
set_addrs() fails for more than 1 address (since that feature is only
supported through osmo_sock_init2_multiaddrs()).

Change-Id: I4b3e1f1894f13ac1175a71a5139c02a2633be26d
2020-01-09 14:10:20 +01:00
Pau Espin ee5c860268 stream: Fix fd param passed to close() in error conditon
Fixes: CID#205089, CID#205087
Change-Id: I65714f214b9962862cda01605c7c2c578c78d3c7
2019-10-21 11:27:36 +02:00
Pau Espin c3a6c75cea stream: osmo_stream_cli: Support setting multiple addr
This API will be later used to set multiple addresses for SCTP sockets.

Depends: libosmocore.git Ic8681d9e093216c99c6bca4be81c31ef83688ed1
Related: OS#3608

Change-Id: I09f0d989f2309abdeb304fe570355abed2cd107b
2019-10-15 13:11:54 +02:00
Pau Espin f254ef80f2 stream: osmo_stream_srv_link: Support setting multiple addr
This API will be later used to set multiple addresses for SCTP sockets.

Depends: libosmocore.git Ic8681d9e093216c99c6bca4be81c31ef83688ed1
Related: OS#3608
Change-Id: I0fe62f518e195db4e34f3b0ad1762bb57ba9d92a
2019-10-15 13:11:51 +02:00
Pau Espin c25285f40e stream.c: remove duplicated line setting variable
Change-Id: I03b05179f5656ab81e5e14cd146f82a471f7b071
2019-10-10 12:26:30 +02:00
Pau Espin a1e9de1ec4 stream: Fix scheduling of queued messages during connecting state
If messages are sent using osmo_stream_cli_send() while the stream
is still (re)connecting, they won't have a chance to be sent until the
stream is connected, and hence they are queued until
CONNECTING->CONNECTED is done. However, at that time
(osmo_stream_cli_fd_cb), the WRITE flag was dropped unconditionally,
which meant already queued packets didn't have the opportunity to be
sent by the same callback until first message is enqueued and WRITE flag
is set (again by osmo_stream_cli_send()).
Let's make them be sent as soon as possible once the connection is
available.

Related: OS#4188
Change-Id: I289495f9aad6389c5f2623fb072d676235b7d24c
2019-09-04 17:40:22 +02:00
Pau Espin 962bf9a48e stream: Introduce API osmo_stream_cli_is_connected
Can be used by users to fetch current status of the stream.

Change-Id: I5402430e5f39eef22dfa18f33807ab6b1e771f1b
2019-09-04 17:04:18 +02:00
Max b3e34435b3 Deprecate osmo_stream_cli_open2()
This supposed to be variant of osmo_stream_cli_open() with explicit
control over reconnection logic but it's plain broken: doxygen docs
contradict the code, actual reconnection logic is affected by timeout
parameter directly which is set in different function.

It seems like we haven't been affected by this so far because we always
use it in auto-reconnection mode which is triggered by default due to
positive reconnection timeout value (5 sec) automatically used in the
absense of explicitly set timeout.

Looking at commit history, this function already been source of
confusion in the past. Instead of trying to fix this mess, let's just
deprecate it entirely and properly document use of
osmo_stream_cli_set_reconnect_timeout() to control reconnection logic.

The only known user is libosmo-sccp which won't use it as of
0a93a683f3cb8e5977eb4a666ab207db6e7d7af9 commit.

Change-Id: Id988ed0274b363db049f59cbf6a193727c8c3c8a
2019-03-19 13:40:55 +00:00
Max af63d87a76 Stream client: add disconnect callback
It's similar to connect_cb() but called once client has been
disconnected.

Change-Id: I905adb2d6191216551a3bcdcd1aec1f96f01612a
2019-02-20 17:33:32 +01:00
Max 1a357720b5 Stream client: report reconnection event as INFO
This helps to avoid unnecessary debug output in reconnection logic tests
in follow-up patches.

Change-Id: Ic96430a9e9294e72de23b0bbacdbf3e99a453f1d
2019-02-07 13:38:22 +01:00
Max 827d693b50 Stream client: fix disconnection logic
Previously closing the client did not alter its state, so we might
end-up with a client without any file descriptors, but being in state
STREAM_CLI_STATE_CONNECTED. Fix this inconsistency by setting
appropriate state.

Related issue is that reconnect function, which is always (at least in
the library and examples) called when some problem with the connection
is detected, closed the connection only after checking whether
reconnection is enabled. This might result in another inconsistency
fixed in this patch by moving the check below connection cleanup.

While at it, also move connection close logging to appropriate place:
it's confusing to see logs about connection being closed while in
reality it wasn't even established.

Change-Id: If41ed60bd625488c283d1e8a2b078e640f04c78e
2019-02-05 16:26:49 +00:00
Max 732652b5b7 Add socket name functions to stream client/server
Add functions to get the description of a server link or client
connection which examine data on corresponding socket.

Those functions use static buffers and intended for single use in
log/printf statements as illustarted by corresponding example changes.

Change-Id: If9a8e211da85956781479862a63c4fc6e53ed6be
2019-02-05 16:25:20 +00:00
Max a93bb2ca34 Stream client: update logging
Introduce logging macro wrapper to properly log current client state and
function to aid in debugging.

Change-Id: Ie22a80dcec95998cce0b25053fdf74f23eab6e53
2019-02-05 16:21:26 +00:00
Max e3366cf144 Stream client: fix doxygen typo
Change-Id: Ic45d3e0520db4060c5c24e5ef5a766a52f060a34
2019-02-04 13:23:27 +01:00
Neels Hofmeyr dc8fcf5a69 logging: fix typo in stream.c
Change-Id: I5dcae1f19e18f04709ce7585943af1d582ebc7ed
2018-12-10 13:11:41 +01:00
Stefan Sperling a555a1fca2 detect freed connections in osmo_stream_srv_read()
While we are processing a read event, the connection's
callback might free the connection. Check for this and don't
attempt to process further events on an already freed connection.

Change-Id: I0a9c7d8e3263c73440f7084dbb1792a4ca5038f0
Related: OS#3685
Depends: g#11704 (for libosmo-sccp)
2018-11-09 15:33:19 +01:00
Harald Welte ac33f6fbd2 stream.c: Use non-blocking connect in osmo_stream_cli
When establishing a client-side stream connection via libosmo-netif,
we must using non-blocking connect if we want to avoid blocking/stalling
the entire process.  The libosmocore socket API provides the
OSMO_SOCK_F_NONBLOCK flag for this.  Make use of it!

Change-Id: I9bfcb39b5801a36ef32ca0d1f3eb8236687d7ed6
Related: OS#3383
2018-07-05 14:07:25 +02:00
Pau Espin c71c9516dd stream.c: osmo_stream_cli_open2: Remove wrong assumption in reconnect decision
Documentation of osmo_sock_init2 doesn't provide information of any
specific value of errno set/expected after running the function. It is
incorrect to expect a specific value of errno and looking at the
implementation it is actually not a good idea to check it.

If reconnect flag is set, let's reconnect always instead of looking at
errno to decide.

Change-Id: I25b33f4cdc496ae31ff240d445b9b2805091845c
2018-04-05 18:07:41 +02:00
Stefan Sperling f27fdf89dc add support for flushing and destroying a server-side stream
Introduce osmo_stream_srv_set_flush_and_destroy() which marks a
stream to be 'flushed and destroyed'. No new messages will be
received on this stream, and no new messages can be queued.
Once the Tx queue has been drained, the connection is destroyed.

The API user is given a chance to perform cleanup operations
in the closed_cb() callback for the connection.

The same mechanism will be added for client-side connections
in a follow-up patch.

Change-Id: I8ed78fe39c463e9018756700d13ee5ebe003b57f
Related: OS#2789
Suggested-by: Harald Welte
2018-02-22 19:48:36 +01:00
Neels Hofmeyr c72be77aa3 osmo_stream_{cli,srv}_destroy: fix mem leak: empty msgb queue
On destroying a client or server stream, deallocate any msgbs that are still
pending in the queue.

In libosmo-sccp, the ss7_test.c in test_as(), messages are queued and were,
before this, left floating after the stream was destroyed, causing a sanitizer
memory leak. This patch fixes the leak.

Depends: Ia291832ca445d4071f0ed9a01730d945ff691cf7 (libosmocore)
Change-Id: Iaad35f03e3bdfabf3ba82b16e563c0a5d1f03639
2017-11-20 16:23:59 +00:00
Pau Espin 6d4acefb69 stream: Avoid re-creating and leaking socket when unneeded
In previous implementation, if no reconfiguring is needed, a new socket
would be created without closing the old one, leaking the previous
socket. Instead, if we don't need reconfiguring, we return 0 as no
operation is required.

Change-Id: I6c1a7fff63e44840fb5e2bc7ace5e9a61e304987
2017-11-17 15:17:07 +01:00
Harald Welte bea215a565 Add SPDX-License-Identifier + Copyright statements
Change-Id: I43bb1c4a889421907a1a08eb29c96f2330ab00ec
2017-11-13 01:21:03 +09:00
Harald Welte 61b5b65deb some more Doxygen header text for datagram, osmux and stream module
Change-Id: Ied83e6b117a420d734a88e3aff925a874c3dd520
2017-10-27 19:57:37 +02:00
Pablo Neira Ayuso 57b5f82491 src: use osmo_timer_setup()
Use new function available in libosmocore to set up timers. Compile
tested only.

Change-Id: Id3dd32102c7362f3b280d2c058c2decebccb357a
2017-06-07 18:40:35 +02:00
Harald Welte 31b1dbd894 somo_stream_srv_fd_cb(): close socket if NODELAY fails
We didn't check for cases where setsockopt_nodelay() fails.  Let's check
for that and bail out + close the socket.

Change-Id: I0adbc4cec35be7c36bdf01d4d8fefd6097e9be5d
Fixes: coverity CID#166970
2017-05-14 21:51:26 +02:00
Harald Welte c108c9db96 SCTP: Don't enable sender_dry_event for SCTP, as it breaks Linux SCTP
Enabling sender_dry_event in SCTP_FLAGS breaks reliable delivery of DATA
chunks to the scoket user on Linux.  Let's avoid enabling that, while
still enabling various other interesting events.

See https://bugzilla.redhat.com/show_bug.cgi?id=1442784 for all related
details.

Change-Id: Ib616cd07a6044ca2ee7e05093b22df3369c62b56
2017-04-17 16:05:22 +02:00
Harald Welte c200a95615 stream.c: Fix SCTP_NODELAY setting
We were using the wrong variable when setting the SCTP_NODELAY,
resulting in the setsoctkopt() fialing.

Change-Id: Ic04cb8bb5ff41f177f7f5b7f7e2a2ecc686dd4c0
2017-04-16 23:34:28 +02:00
Neels Hofmeyr 719d1a38c6 osmo_stream_srv_fd_cb(): don't leak socket FDs on errors
So far we seem to assume that the accept_cb does all handling of socket
fd cleanup. However, there are cases where there is no accept_cb set,
the accept_cb returns error, or an earlier sctp_sock_activate_events()
or the activation of non-blocking mode fails.

For those cases, close the socket and return an error code.

Fixes: CID#57915
Change-Id: I3a3ce9194ab7ca5c1921fc79c2a1c9e10a552cf0
2017-04-14 11:06:09 +02:00
Harald Welte 5ae42bca91 SCTP: Fix PPID byte width
In a659590e29 we fixed endianness issues
with the Stream ID field, but at the same time mistook the PPID field
for 16bits.  In reality it is 32bits, and hence our 'htons' is rendering
wrong PPID values.

Change-Id: I1b60523044835ee630dba9a43d26af4f1ebd1ced
2017-04-12 12:08:45 +02:00
Harald Welte 9ec26583cd Add osmo_stream_{cli,srv_link}_set_nodelay() function
Using this function, the user can configure if sockets related to the
respective stream client or server should have the NODELAY socket
option set in order to avoid Nagle algorithm or related algorithms
that may introduce packet delay on the transmitter side.

Change-Id: Ibeb9ba227bab18f7f4f16518c0022c4f003cc8e9
2017-04-11 19:54:07 +02:00
Harald Welte a659590e29 stream.c: Fix endianness handling of PPID and STREAM_ID
In their infinite wisdom, the inventors of SCTP designed an API (the
sockets API described in RFC6458), where some members are in host byte
order (like the stream identifier), while other members are in network
byte order (like the PPID).

Let's handle this properly (we assumed both are network byte order), and
also use 16-bit htons/ntohs fo the PPID, rather than htonl/ntohl.

Change-Id: I777174ca2915c6de0063db41a745c71b4a09bbec
2017-04-10 17:48:42 +02:00
Harald Welte 7cee4b67e9 stream/datagram: Ensure reliable close/destroy
* when using osmo_*_destroy(), always call *_close() internally to
  make sure we don't free memory holding references to sockets that are
  still open
* when closing the socket, always make sure to set the fd to -1 in all
  cases, to avoid attempts to avoid later close() on a new file using
  the same fd number as the socket closed previously.

Change-Id: I29c37da6e8f5be8ab030e68952a8f92add146821
2017-04-10 06:52:59 +00:00
Harald Welte 0bacc71fc2 stream/datagram: Consistently use osmo_talloc_replace_string()
during osmo_*_set_addr(), we must make sure to talloc_free() any old
address before copying in the new address.  Not all functions did this,
and those that did implemented it manually.  Let's use
osmo_talloc_replace_string() which is exactly intended for this case.

Change-Id: Ie1b140a160c66e8b62c745174865d5ba525cb2c2
2017-04-10 06:47:28 +00:00
Harald Welte a2c2b59165 stream+datagram: Allow local bind + connect for client sockets
This uses the new osmo_sock_init2() features introduced in libosmocore
Change-Id Idab124bcca47872f55311a82d6818aed590965e6 to bind *and*
connect a given socket during creation.

Change-Id: I013f4cc10b26d332d52d231f252bb0f03df8c54b
2017-04-10 06:40:14 +00:00
Harald Welte d0f9bd600c Add minimal doxygen documentation for stream + datagram modules
We should have doxygen documentation for all libosmo-* APIs.

libosmo-netif is currently devoid of any API docs. Let's start with the
stream and datagram socket related functions.

Change-Id: I589a5e60d9df2b8a65fbaf68f80e3ae0039d8c2a
2017-04-08 20:13:14 +02:00
Daniel Willmann 5fe77a4656 stream.c: Handle SCTP in osmo_stream_srv_recv()
Change-Id: If0875cfa3eba9ef36acc0c4bfd0d168a45c380b9
2017-03-29 15:23:52 +02:00
Harald Welte db9808e883 stream client: Ensure client is notified on reconnection
without setting the BSC_FD_* flags prior to reconnection, the re-connect
would happen silently and the client program would not be notified via the
connect_cb().

Change-Id: Iaf8ec8662cf83476eee1b76fa41dc57f063f0ad3
2017-03-20 01:32:06 +00:00
Harald Welte d8c820570b stream: Export osmo_stream_cli_reconnect()
In case the application is using the read-callback and a read returns 0,
then the application itself would want to trigger the reconnect.  This
is different from the osmo_stream_cli_recv() case where the reconnect
can be handled internally to the library.

Change-Id: I41314bad4a9f44e8a61b9d2ba33d1a76b25bd145
2017-03-17 12:07:19 +01:00
Harald Welte f2174ef1a2 stream: When destroying, make sure to kill reconnect timer
if osmo_stream_cli_destroy() is called while the reconnect timer is
running, we would end up in a crash.

Change-Id: If6597130f472f1e2b8d9682002250ecd54675bb0
2017-03-17 12:07:19 +01:00
Harald Welte a4c1a73250 stream: don't crash in _close() when fd is not initialized
We use the magic value '-1' in case the file descriptor is not yet
initialized.  If somebody calls osmo_stream_*_close() before this
changes, we used to crash.  Let's check for this and avoid a crash.

Also, after close let's change the fd to -1 again to mark the fd
invalidity.

Change-Id: I3aa04999ab01cb7971ee2dad45dfc31ab4142868
2017-03-17 12:06:24 +01:00
Harald Welte 781e7b38f8 stream_client: Actually use/honor the reconect parameter
The reconnect behavior was likely broken in commit
de3f57a8293a5b39435d6f283da23e0172bad8bb

If the user requests a re-connect, we should start it. Not only in case
the connection drops later, but also if the initial connection itself
fails.

Change-Id: I817e026404cbd9145cae2ce90bc57a1db1d2e12b
2017-03-17 00:24:23 +01:00
Harald Welte 29d6cd4134 osmo_stream_cli_open2(): Fix bogus EINPROGRESS handling
osmo_sock_init() never returns -1 + errno EINPROGRESS. It will return a
positive fd in case the connect operation is in progress. Therefore,
the related code in osmo_stream_cli_open2() was impossible to execute.

Change-Id: Id3483d1d1d4d2eabba94729ea29e5c93b33abff0
Fixes: Coverity CID 57861
2016-12-01 15:52:54 +01:00
Harald Welte b2b2fe643f sctp_sock_activate_events(): Print error message on error
When the setsockopt() in sctp_sock_activate_events() indicates an error,
let's print an error message in the log about this.

Change-Id: I5920154e23debe6d01eaa156005db0842f1a18cc
Fixes: Coverity CID 57634
2016-11-26 16:41:06 +00:00
Arran Cudbard-Bell 5b0ad8bd85
Set SO_NOSIGPIPE on SCTP connections, for patforms which support it (macOS, FreeBSD etc...)
Change-Id: If4f2a99b2baf143551a7122f40ed1f9088c5936c
Signed-off-by: Arran Cudbard-Bell <a.cudbardb@freeradius.org>
2016-11-02 13:22:24 -04:00
Daniel Willmann 10f981a4a1 stream: Use macros to access data and length 2016-02-19 09:23:31 +01:00
Harald Welte 2b03b8c07c stream.c: ensure to zero-initialize sctp_sndrcvinfo
caught by valgrind:
==3150== Syscall param sendmsg(msg.msg_control) points to uninitialised byte(s)
==3150==    at 0x60DC0D0: __sendmsg_nocancel (syscall-template.S:81)
==3150==    by 0x5DF3413: sctp_send (in /usr/lib/x86_64-linux-gnu/libsctp.so.1.0.16)
==3150==    by 0x65AA066: osmo_stream_srv_write (stream.c:540)
2015-12-25 17:47:19 +01:00
Harald Welte cc4ce39fc6 fix/complete stream client re-connect logic
So far, when the first connection attempt failed in
osmo_stream_cli_open(), we returned a terminal errro without any
re-connection attempts.  While this may be useful in some cases, our
general idea of the stream client logic is to handle the reconnection
attempts insid the library.  We introduce a new osmo_stream_cli_open2()
function while keping the old behavior for backwards compatibility.
2015-12-22 23:54:21 +01:00
Harald Welte b60edf05a6 stream.c: Fix another 'link' variable / symbol mismatch 2015-12-22 23:53:55 +01:00