Commit Graph

43 Commits

Author SHA1 Message Date
Neels Hofmeyr cf7f7924d2 add osmo_sockaddr_from/to_octets()
Shorthand for the INET/INET6 switch() to get/put the addr part, useful
for encoding and decoding message buffers.

Related: OS#5599
Change-Id: Ie9e33bfac525c59c30714663d2bfcc62ec9eeb81
2022-02-07 22:13:20 +01:00
Neels Hofmeyr 09d6574a45 add osmo_sockaddr_to_str_c(), osmo_sockaddr_to_str_buf2()
To easily log and print a sockaddr using OTC_SELECT, add
osmo_sockaddr_to_str_c().

Implement osmo_sockaddr_to_str_buf2() using osmo_strbuf, so that we can
return the chars_needed which osmo_sockaddr_to_str_c() uses.

From previous osmo_sockaddr_to_str_buf(), call
osmo_sockaddr_to_str_buf2() and return NULL if the buf_len was
insufficient, to mimick previous behavior. This makes it more
consistently returning NULL for insufficient buf_len, as shown in the
tweak that is needed in socket_test.c. Before osmo_sockaddr_to_str_buf()
would return a truncated port number, now it's all or NULL.

I will use osmo_sockaddr_to_str_c() in the new osmo-upf implementation.

Related: SYS#5599
Change-Id: I12771bf8a021e6785217b1faad03c09ec1cfef0e
2022-01-31 15:03:53 +00:00
Harald Welte c545ff6f3e socket: QoS support for all our socket init functions
Every socket function that can be passed a 'flags' argument now
supports the following two additional macros that can be or-ed in
with the flags:
* OSMO_SOCK_F_DSCP(x) -- specify the IP DSCP of the socket
* OSMO_SOCK_F_PRIO(x) -- specify the priority of the socket

The existing osmo_sock_set_{dscp,priority}() functions are useful,
but  you cannot call them in between the socket creation and the
connect() operation when using our socket helpers.  This means that
the first packet sent will have the default DSCP/priority, and only
later packets would have the desired values.

When using the functionality introduced by this patch, we can ensure
that even the very first packet of e.g. a TCP or SCTP connect()
will have the correct DSCP/priority applied.

Change-Id: If22988735fe05e51226c6b091a5348dcf1208cdf
Related: SYS#5427
2021-04-28 13:15:20 +02:00
Harald Welte ecc0bd8d42 socket: Introduce osmo_sock_set_priority() helper function
In some situations we want to set the SO_PRIORITY socket option
to determine the in-kernel priority of packets generated by this
socket.

Change-Id: I89abffcd125e6d073338a5c6437b9433220e1823
Related: SYS#5427
2021-04-27 22:25:13 +02:00
Harald Welte ce53e03dc9 socket: Introduce osmo_sock_set_dscp() to set socket DSCP value
At least on Linux, sockets have a IP_TOS socket option that can be
configured to set the TOS.  However, TOS (of RFC791) was replaced
by the DSCP (of RFC2474) in 1998.

As the DCSP bits are only the upper 6 bits of the TOS bits, let's
introduce a helper to get, mask and set the DSCP values in the TOS
bits.

Related: OS#5136, SYS#5427
Change-Id: Ia4ba389a5b7e3e9d5f17a742a900d6fd68c08e40
2021-04-27 21:53:32 +02:00
Alexander Couzens 80788fac9e add osmo_sockaddr_to_str_buf/osmo_sockaddr_to_str
Add helper to format osmo_sockaddr into a string.

Change-Id: I917f25ebd1239eae5855d973ced15b93731e33a0
2020-10-12 15:13:43 +00:00
Vadim Yanitskiy def5a407c1 socket: make the arguments of osmo_sockaddr_cmp() const
Change-Id: Ibfdfdd40c52709b32ac934974cc78ee821fa83ba
2020-10-09 15:22:49 +00:00
Alexander Couzens 0f36421ba8 add osmo_sockaddr_local_ip() to determine the local address for a remote.
Similiar to osmo_sock_local_ip but for osmo_sockaddr.

Change-Id: I9cd2c5ceb28183e2fd2d28f9c9088c3fcac643d2
2020-09-07 16:22:21 +02:00
Alexander Couzens e4181ea8a2 socket: add osmo_sockaddr_cmp()
Compare two osmo_sockaddr.

Change-Id: I2d12ebae2710ffd17cf071e6ada0804e73f87dd6
2020-09-02 18:42:21 +02:00
Alexander Couzens 43957e69ca socket: introduce osmo_sock_init_osa & osmo_sock_init_osa_ofd
osmo_sock_init_osa() takes osmo_sockaddr* as local and remote endpoints
to setup a socket.

Change-Id: I1eece543e3241ef0e095eb63bb831f7c15a16794
2020-09-02 18:42:21 +02:00
Pau Espin 5cc4fe4706 socket: Add some osmo_sockaddr print helpers
These are APIs useful to inline in log calls.

Change-Id: Ie07a38b05b7888885dba4ae795e9f3d9a561543d
2020-08-31 12:52:09 +02:00
Alexander Couzens 8419bb2e9e socket.h: introduce osmo_sockaddr to hold v4 and v6 endpoints
When dealing with IPv4 and IPv6 address, the struct sockaddr
allows to hold IPv4 and IPv6.
However even when struct sockaddr is being used, a cast to the
IPv4 or IPv6 family must happen. To work around this additional code,
use a union for the most common types.

Change-Id: If80172373735193401af872b18e1ff00c93880e7
2020-08-10 23:29:21 +02:00
Harald Welte 44b9926c84 socket: Add osmo_sock_mcast_iface_set() to bind multicast to device
Change-Id: Ib52d22710020b56965aefcef09bde8247ace4a9c
Related: OS#2966
2020-03-10 19:08:51 +01:00
Pau Espin 3f464fc007 socket: Introduce API osmo_sock_init2_multiaddr()
This API will be used by libosmo-netif's osmo_stream for SCTP sockets,
which in turn will be used by libosmo-sccp to support multi-homed
connections.

Related: OS#3608
Change-Id: Ic8681d9e093216c99c6bca4be81c31ef83688ed1
2019-10-18 09:21:48 +00:00
Harald Welte 179f35702e Add _c versions of functions that otherwise return static buffers
We have a habit of returning static buffers from some functions,
particularly when generating some kind of string values.  This is
convenient in terms of memory management, but it comes at the expense
of not being thread-safe, and not allowing for two calls of the
related function within one printf() statement.

Let's introduce _c suffix versions of those functions where the
caller passes in a talloc context from which the output buffer shall
be allocated.

Change-Id: I8481c19b68ff67cfa22abb93c405ebcfcb0ab19b
2019-04-10 22:42:32 +00:00
Harald Welte c0dfc9d885 socket: osmo_sock_get_name() Use "const void *" as talloc context
Change-Id: Ie6877277cddb0a9e049449c260afe3314ba65050
2019-03-19 14:04:00 +00:00
Max b3e9b31383 socket.h: add missing include
socket.h uses INET6_ADDRSTRLEN without including arpa/inet.h where it's
defined which might break external users of socket.h

Fix this by adding missing include. The error was introduced in
64b51eb68b

Change-Id: I2883addcb81cec038577e401e356e8f07a947d4c
2019-02-04 13:48:22 +00:00
Philipp Maier 64b51eb68b socket: add define for socket name length
The function osmo_sock_get_name_buf() can be used to write a string
representation to a user provided memory. Unfortunately the proper
length for the user provided memory is not obvious. To make using
osmo_sock_get_name_buf() more practical, add a define constant that
defines the length of the required memory. Also use this define in
socket.c.

Change-Id: If8be8c2c0d4935da17ab13b2c2127b719ceefbcc
2019-01-17 17:46:08 +01:00
Neels Hofmeyr 0145751a97 add to osmo_sock_get_name*() API
Basically, I am applying code review that I would have given had I not been on
vacation when the last osmo_sock_get_name* stuff was merged.

osmo_sock_get_name2() is so far a static internal function. However, it is
nothing like osmo_sock_get_name(), so instead rename it to
osmo_sock_get_ip_and_port(). Also make it public API, no need to hide it.  I'm
adding an "and" in the name to hopefully clarify: "ip_port" vs. "ip_and_port"
-- there already are _get_X_ip_port() functions that only return the port
string, despite "ip" in the name.

Add new public osmo_sock_get_name2(), which is like osmo_sock_get_name(),
except it uses a static string instead of talloc, and omits the braces. This
is most convenient for log statement formats, avoiding dyn allocations.

Add new osmo_sock_get_name_buf(), which is like osmo_sock_get_name2() but
writes to a caller provided char buffer.

Use osmo_sock_get_name_buf() in the implementation of osmo_sock_get_name(),
but use another (non-static) local string buffer, because adding braces is too
complex without talloc_snprintf().

Rationale:

I want to improve the logging of socket errors, e.g. change

  DLMGCP ERROR Failed to read: 111/Connection refused (mgcp_client.c:720)

to

  DLMGCP ERROR Failed to read: r=10.0.99.2:2427<->l=10.0.99.2:2728: 111='Connection refused' (mgcp_client.c:721)

but it is just not handy to compose logging with the current API:

- osmo_sock_get_name() requires a talloc_free().
- all the others require output buffers.
- the only way to conveniently compose a logging string and,
- notably, the only trivial way to skip the string composition if the logging
  level is currently muted, is to have a function that returns a static string:
  the new osmo_sock_get_name2().
- (I think the osmo_sock_get_{local,remote}_* convenience wrappers should never
  have been added, because they encourage the caller to invoke the same code
  twice, for IP addr and port, and throw away one half each time.)

Related: Iae728192f499330d16836d9435648f6b8ed213b6 (osmo-mgw)
Change-Id: I8ad89ac447c9c582742e70d082072bdd40a5a398
2018-12-19 03:25:55 +01:00
Max 9d7a247da4 Add osmo_sockaddr_in_to_str_and_uint()
It's similar to osmo_sockaddr_to_str_and_uint() but does not require odd
typecasting for AF_INET case. Make osmo_sockaddr_to_str_and_uint() into
wrapper around new function and make sure to check for address family
before typecasting. Also use proper return type.

Change-Id: Ie384483124d407a960ab6732e6a7fd90554389d2
2018-12-07 10:33:29 +00:00
Oliver Smith 7acd5d0394 add osmo_sock_get_{local,remote}_ip{,_port}()
Return only the IP or port of either the local or remote connection,
not the whole set of IP and port of both the local and remote
connection like osmo_sock_get_name() does it. This is needed for
OS#2841, where we only want to print the remote IP.

Related: OS#2841
Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1
2018-10-26 17:54:16 +00:00
Philipp Maier 73196e77fb socket: add flag to enforce SO_REUSEADDR on UDP sockets
When IPPROTO_UDP is used then SO_REUSEADDR omitted since UDP is
connection less we do not have to wait until lingering connections time
out. There were also negative effects such as that two applicatications
could use the same UDP port, normally one of the two applications would
get an error, but with SO_REUSEADDR this is supressed. However, there
are applications (UDP MULTICAST) where two applications must be able to
use the same port. In the osmocom project those are osmo-bts-virtual,
virtphy and gsmtap in general.

Lets introduce a flag that the API user can supply in order to have
SO_REUSEADDR applied.

- Add new flag OSMO_SOCK_F_UDP_REUSEADDR

Change-Id: I94aaf6d5224ab23bde5ea5c4a83569b6145ab32b
Related: OS#3497
2018-08-23 20:17:45 +02:00
Neels Hofmeyr 59f4caf4ef add osmo_sockaddr_to_str_and_uint()
This came from osmo-bsc refactoring patch I82e3f918295daa83274a4cf803f046979f284366
https://gerrit.osmocom.org/#/c/osmo-bsc/+/9671/6/src/osmo-bsc/gsm_data.c@1708

Add regression test in utils_test.c.

Change-Id: I1f2918418c38918c5ac70acaa51a47adfca12b5e
2018-07-20 14:02:21 +00:00
Philipp Maier 2d2490ed4e socket: add function osmo_sock_local_ip() to query local ip
In some cases it is required to know the ip-address of the interface
through that a given remote IP-Address can be reached.

Add function osmo_sock_local_ip() to determine the local ip-address
for a given remote ip-address

Change-Id: I2988cc52b196fc8476703d1287e24cb4a48491c2
2017-10-23 08:47:38 +00:00
Harald Welte 37d204a120 socket: Add new OSMO_SOCK_F_NO_MCAST_ALL option
Using this option at socket creation, the caller can request disabling
the IP_MULTICAST_ALL socket option.

Change-Id: I5ab5de45c0b64ceb3636ea98245a23defa24ffd4
2017-07-13 16:54:23 +02:00
Harald Welte bc43a62f41 socket: Allow disabling multicast loop on socket creation
This introduces a new flag OSMO_SOCK_F_NO_MCAST_LOOP, which can be used
to disable the looping back of multicast packets transmitted throug this
socket to other local sockets on the machine.

As this looping-back is active by default, a single option to deviate
from the default is deemed sufficient.

Change-Id: I24a5b1ebc3f84d2d5d4734e54df50efaea26490b
2017-07-13 16:53:41 +02:00
Harald Welte e30d7e6018 socket.c: Add multicast related convenience functions
Change-Id: Id703e7a7a1e065181a4c76c088b8dcc1b7fe15a2
2017-07-13 16:53:37 +02:00
Neels Hofmeyr 17518fe393 doxygen: unify use of \file across the board
Considering the various styles and implications found in the sources, edit
scores of files to follow the same API doc guidelines around the doxygen
grouping and the \file tag.

Many files now show a short description in the generated API doc that was so
far only available as C comment.

The guidelines and reasoning behind it is documented at
https://osmocom.org/projects/cellular-infrastructure/wiki/Guidelines_for_API_documentation

In some instances, remove file comments and add to the corresponding group
instead, to be shared among several files (e.g. bitvec).

Change-Id: Ifa70e77e90462b5eb2b0457c70fd25275910c72b
2017-06-23 00:18:23 +00:00
Neels Hofmeyr 87e4550585 doxygen: enable AUTOBRIEF, drop \brief
Especially for short descriptions, it is annoying to have to type \brief for
every single API doc.

Drop all \brief and enable the AUTOBRIEF feature of doxygen, which always takes
the first sentence of an API doc as the brief description.

Change-Id: I11a8a821b065a128108641a2a63fb5a2b1916e87
2017-06-23 00:18:22 +00:00
Pau Espin 75989e677d socket: Add osmo_sock_init2_ofd() function
Will be used by osmo-bts-trx

Change-Id: I3c655a4af64fb80497a5aaa811cce8005dba9cd9
2017-05-26 12:56:07 +02:00
Harald Welte dda70fca79 Add osmo_sock_init2() function, allowing both BIND *and* CONNECT
The old osmo_sock_init() function allows only either a bind (for a
server socket), or a connect (for a client socket), but not both
together.  So there's no way to have a client socket that is bound to a
specific local IP and/or port, which is needed for some use cases.

Change-Id: Idab124bcca47872f55311a82d6818aed590965e6
2017-04-09 21:46:21 +02:00
Harald Welte 48f5583347 socket: Introduce function to obtain socket name
Using this function, one can obtain a human-readable string identifying
the host and port names of the socket.

Change-Id: Ib5de5c7b9effe1b0a363e4473a7be7fa38ca6ef3
2017-01-27 10:28:58 +01:00
Harald Welte 2d2e2cca0d Update doxygen annotations in libosmocore
This adds and improves doxygen API descriptions all over libosmocore,
reducing the 'white spots' that don't have any documentation.
2016-05-05 18:49:27 +02:00
Sylvain Munaut 12ba778afd include: Switch to #pragma once pattern
Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
2014-06-16 10:17:27 +02:00
Álvaro Neira Ayuso 5ade61a4f4 src/socket: Adding unix domain socket support
Added some function for adding the unix domain socket support.

Signed-off-by: Alvaro Neira Ayuso <anayuso@sysmocom.de>
2014-03-26 19:50:46 +01:00
Sylvain Munaut dca7d2caaa doc: Fix the Doxygen section endings
Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
2012-04-18 21:53:23 +02:00
Holger Hans Peter Freyther 58d3153249 misc: Fix warning when compiling telnet_interface.c
In file included from telnet_interface.c:30:0:
osmocom/core/socket.h:25:4: warning: 'struct osmo_fd' declared inside parameter list [enabled by default]
telnet_interface.c: In function 'telnet_init_dynif':
telnet_interface.c:84:4: warning: passing argument 1 of 'osmo_sock_init_ofd' from incompatible pointer type [enabled by default]
osmocom/core/socket.h:24:5: note: expected 'struct osmo_fd *' but argument is of type 'struct osmo_fd *'
2012-03-16 09:18:12 +01:00
Harald Welte ba6988bd89 some more doxygen work (include the notion of modules) 2011-08-17 17:14:11 +02:00
Harald Welte bd598e3c5e start to add doxygen documentation to libosmocore headers 2011-08-16 23:26:52 +02:00
Pablo Neira Ayuso 0849c9af2a socket: add OSMO_SOCK_F_[CONNECT|BIND|NON_BLOCK] flags
This extends the socket infrastructure in libosmocore to allow
to create non-blocking sockets.

Basically, it replaces the connect0_bind1 parameter by one
flags parameter.
2011-06-13 19:15:59 +02:00
Harald Welte 68b1574257 socket: use listen() and SO_REUSEADDR, new osmo_sock_init_ofd() function
osmo_sock_init_ofd() is a wrapper around osmo_sock_init() which will
take care of initializing and registering a 'struct osmo_fd' for the
newly-created socket.
2011-05-22 21:47:29 +02:00
Harald Welte e476442cf0 GSMTAP/socket code: Check for sys/socket.h and conditionally compile 2011-05-22 12:25:57 +02:00
Harald Welte 33cb71ac91 gsmtap: rework GSMTAP API to be more future-proof
* use write_queue where applicable
* provide functions that work on raw FD and those with osmo_fd
* add support for multiple gsmtap instances (no global variables)
2011-05-22 11:43:23 +02:00