aboutsummaryrefslogtreecommitdiffstats
path: root/src/socket.c
AgeCommit message (Collapse)AuthorFilesLines
2023-01-18Move src/*.{c,h} to src/core/Pau Espin Pedrol1-2062/+0
This way we have all libosmocore.so in an own subdir instead of having lots of files in the parent dir, which also contains subdirs to other libraries. This also matches the schema under include/osmocom/. Change-Id: I6c76fafebdd5e961aed88bbecd2c16bc69d580e2
2023-01-17socket.h: Introduce API osmo_sockaddr_netmask_to_prefixlen()Pau Espin Pedrol1-0/+57
Implementation is imported from osmo-ggsn.git 97f60e3dca581797007524e0006ca9fafad59713 in46a_netmasklen() and adapter to work with an osmo_sockaddr. This will be used by osmocom-bb's "modem" app. Change-Id: I75e75e251c6776801fffdde745aebedf21c68799
2022-10-04socket: Introduce API osmo_sockaddr_is_anyPau Espin Pedrol1-0/+20
Change-Id: I2810a889fc14052d0e0be6a2b500ad4e5088ffa9
2022-03-01add osmo_sockaddr_set_port()Neels Hofmeyr1-0/+17
Do it like osmo_sockaddr_port() in reverse. Related: SYS#5599 Change-Id: I9512e44c3203daebb3fe3435fceef167613c1a73
2022-02-07follow-up to osmo_sockaddr_from/to_octets()Neels Hofmeyr1-2/+2
Forgot to apply one code review change before merge. Related: OS#5599 Change-Id: Id202f3fda51bea9149ab3462a2efe35cf08d8030
2022-02-07add osmo_sockaddr_from/to_octets()Neels Hofmeyr1-0/+59
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-07log: socket.c: rather use the osmo_sockaddr_str _FMTNeels Hofmeyr1-13/+12
The OSMO_SOCKADDR_STR_FMT() and _ARGS() macros properly place square braces around IPv6 addresses, so that the port nr is clearly distinguishable. before: 1:2::3:4:5 after: [1:2::3:4]:5 When using a struct reference, the macro resolves to '(&sastr) ? .. : ..', which the compiler complains about as "condition is always true". Shim around that error with a pointer variable. I considered using osmo_sockaddr_to_str_c() instead, but here in socket.c we cannot assume that osmo_select_main_ctx() is being used and hence can't just use OTC_SELECT for log string composition. The struct osmo_sockaddr_str is a string representation in a local variable, and hence doesn't need talloc for log strings. I considered adding log_check_level() around the log string conversion, but since all of these instances are on LOGL_ERROR, I didn't bother. Related: SYS#5599 Change-Id: Idbe7582b2b7f14540919e911dad08af6d491f68f
2022-01-31add osmo_sockaddr_to_str_c(), osmo_sockaddr_to_str_buf2()Neels Hofmeyr1-28/+44
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-10socket, select: Fix -Wsign-compare warningsHarald Welte1-4/+5
Change-Id: Idf6213e66e9bf609e930c6134292458a7d521871
2021-12-14treewide: remove FSF addressOliver Smith1-4/+0
Remove the paragraph about writing to the Free Software Foundation's mailing address. The FSF has changed addresses in the past, and may do so again. In 2021 this is not useful, let's rather have a bit less boilerplate at the start of source files. Change-Id: I5050285e75cf120407a1d883e99b3c4bcae8ffd7
2021-10-06gsmtap: allow 127.0.0.x local listenersEric1-0/+22
Even if not bound to a IF they just exist and work as expected, and make distinguishing traffic for local setups easy. Change-Id: I1043dfd8075f14481011f43db45c943e9320413c
2021-07-16Make gcc 11.1.0 false positivies happyPau Espin Pedrol1-1/+3
After my system's gcc was upgraded, I get false positivies in a couple places. Let's initialize those to make gcc happy. """ /git/libosmocore/src/socket.c: In function ‘osmo_sock_init’: /git/libosmocore/src/socket.c:958:25: error: ‘sfd’ may be used uninitialized in this function [-Werror=maybe-uninitialized] 958 | close(sfd); | ^~~~~~~~~~ /git/libosmocore/src/gsm/gsm48.c: In function ‘osmo_mobile_identity_decode’: /git/libosmocore/src/gsm/gsm48.c:690:20: error: ‘str_size’ may be used uninitialized in this function [-Werror=maybe-uninitialized] 690 | if (rc < 1 || rc >= str_size) { | ~~~~~~~^~~~~~~~~~~~~~~~~ /git/libosmocore/src/gsm/gsm48.c:679:22: error: ‘str’ may be used uninitialized in this function [-Werror=maybe-uninitialized] 679 | rc = osmo_bcd2str(str, str_size, mi_data, 1, 1 + nibbles_len, allow_hex); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ Change-Id: I8aacfbc21e23f63a65e8baee3fd536a1fe1bdd8a
2021-05-08osmo_sock_*_ofd(): Mark OSMO_FD_WRITE on non-blocking connect()Harald Welte1-5/+14
When we perform a non-blocking connect, the completion of the connect will be signaled by marking the fd as WRITE-able. So we should automatically set OSMO_FD_WRITE to make sure the user gets notified on completion. Change-Id: I22964c5d5da849abcd97a900bd86ab5b4ada05da
2021-04-28socket: IPv6 support for osmo_sock_set_dscp()Harald Welte1-7/+36
IPv6 has the analogous to DSCP: The "traffic class" field. See https://tools.ietf.org/html/draft-itojun-ipv6-tclass-api-03 Change-Id: Ib31b977f67d60aa7f30ca4ab6eceba3d1d5eeee1 Related: SYS#5427
2021-04-28socket: QoS support for all our socket init functionsHarald Welte1-1/+21
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-28socket: reduce code duplication, introduce socket_helper_tail()Harald Welte1-39/+38
Common bits shared by various functions (currently setting non-blocking) should not be copy+pasted around. Change-Id: I95056940ddc26b65f63eedaeaab6882edaef6317
2021-04-27socket: Introduce osmo_sock_set_priority() helper functionHarald Welte1-0/+8
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-27socket: Introduce osmo_sock_set_dscp() to set socket DSCP valueHarald Welte1-0/+26
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
2020-10-12add osmo_sockaddr_to_str_buf/osmo_sockaddr_to_strAlexander Couzens1-0/+62
Add helper to format osmo_sockaddr into a string. Change-Id: I917f25ebd1239eae5855d973ced15b93731e33a0
2020-10-09socket: make the arguments of osmo_sockaddr_cmp() constVadim Yanitskiy1-1/+2
Change-Id: Ibfdfdd40c52709b32ac934974cc78ee821fa83ba
2020-09-07add osmo_sockaddr_local_ip() to determine the local address for a remote.Alexander Couzens1-0/+23
Similiar to osmo_sock_local_ip but for osmo_sockaddr. Change-Id: I9cd2c5ceb28183e2fd2d28f9c9088c3fcac643d2
2020-09-02socket: add osmo_sockaddr_cmp()Alexander Couzens1-0/+29
Compare two osmo_sockaddr. Change-Id: I2d12ebae2710ffd17cf071e6ada0804e73f87dd6
2020-09-02socket: introduce osmo_sock_init_osa & osmo_sock_init_osa_ofdAlexander Couzens1-0/+159
osmo_sock_init_osa() takes osmo_sockaddr* as local and remote endpoints to setup a socket. Change-Id: I1eece543e3241ef0e095eb63bb831f7c15a16794
2020-08-31socket: Fix bug in osmo_sock_init2(AF_UNSPEC) matching IP versionsPau Espin Pedrol1-13/+21
See previous commit for a bug description. Fixes: 2c962f5de1eeea119cfac7d9d92db31c570353b9 Change-Id: I59bf4b4b3ed14766a5a5285923d1ffa9fc8b2294
2020-08-31socket: Add some osmo_sockaddr print helpersPau Espin Pedrol1-0/+32
These are APIs useful to inline in log calls. Change-Id: Ie07a38b05b7888885dba4ae795e9f3d9a561543d
2020-08-31socket: fix wrong ipv6 dst buf size passed in osmo_sock_local_ipPau Espin Pedrol1-1/+1
Change-Id: I13939fda9b3d642f9fa1413e59458aba0628a4a6
2020-08-31socket: Fix stack-buffer-overflow in osmo_sock_local_ip()Pau Espin Pedrol1-6/+17
On IPv6 sockets, getsockname() and inet_ntop() would act upon a structure struct sockaddr_in. First getsockname() would succeed but truncate the address, and later on inet_ntop would read out of the scope of the structure. Change-Id: If781d56680758a97643b1b38e78d3431ea649020
2020-08-31socket: Use AF_UNSPEC instead of PF_UNSPEC calling getaddrinfoPau Espin Pedrol1-1/+1
man getaddrinfo entry doesn't mention PF_UNSPEC, but AF_UNSPEC. Change-Id: Icab676ff7ee9d813d7b7fe807fbe172512f5e397
2020-08-31socket: Add support for AF_INET6 in osmo_sockaddr_to_str_and_uint()Pau Espin Pedrol1-2/+15
Related: SYS#4915 Change-Id: I439c7fa52a3a30eebc3d35e78be7f1724fb69294
2020-08-30socket: Allow binding to :: (IPv6) and connecting to IPv4-only on the remotePau Espin Pedrol1-0/+17
sctp_bindx() fails if passed both "0.0.0.0" and "::", only "::" must be passed instead, which covers both. As a result, it is fine in this case and makes sense having only IPv6 formatted IPs in the local side (which actually also includes all IPv4 ones in th system) and IPv4-only addresses on the remote side. Change-Id: I0b590113e5def20edcbcb098426b19cd504eabff
2020-08-25socker: Remove AI_ADDRCONFIG from getaddrinfo flagsPau Espin Pedrol1-1/+1
The flag was added recently in cd133316cfc735a3006d499dae6b2f693c3c741c, and it is causing issues while running unit tests in environement like OBS where probably there's no non-loopback address configured. Change-Id: I47c31953f1db39fcd2870bde0b984057b8e7b4c4
2020-08-25osmo_sock_inti2_multiaddr: Fix memleak and free uninitialized memPau Espin Pedrol1-6/+13
Under some specific cases, res_loc could be leaked. Under some others, res_loc and res_rem were freed without being initialized previously. Fixes: CID#212863 Fixes: CID#212861 Change-Id: Id9c4eda6fd1172e7324aa23c81e8658967a8dd0b
2020-08-24osmo_sock_get_ip_and_port(): Support IPv6 socketsPau Espin Pedrol1-3/+3
Change-Id: I255a71cf047fd022285fc9aa2f24e5070aa3920b
2020-08-24socket: Log proper getaddrinfo() errorPau Espin Pedrol1-6/+3
Drop one of the two log lines since it would be a duplicated and anyway it misses context (return code). Change-Id: I4620bf86f4bcda58ae85209278ac9ae9a0f3012a
2020-08-24socket: multiaddr: Support IPv4 + IPv6 addresses in SCTP associationsPau Espin Pedrol1-65/+86
The function is improved to support AF_INET:v4->v4, AF_INET6:v6->v6 and AF_UNSPEC:v4+v6->v4+v6. Unit tests for the function are added to make sure function behaves correctly in several scenarios. Change-Id: I36d8ab85d92bba4d6adb83bc1875eb61094ed2ef
2020-08-24sock: osmo_sock_init2_multiaddr: decouple addr resolution from socket creationPau Espin Pedrol1-46/+43
Address resolution is done first and once we have the information, it proceeds to create the socket. This separation in steps will help when adding support for IPv6+IPv4 sets, where AF_UNSPEC is passed and created socket needs to be AF_INET6 in order to handle addr of both versions. Change-Id: I03147e3033a0c1fd04c9ac61d2ffbd78a1bb784a
2020-08-06osmo_sock_init2: improve support for AF_UNSPECAlexander Couzens1-14/+78
osmo_sock_init2 abstract two calls of getaddrinfo into one. While there aren't problems with AF_INET or AF_INET6. When using AF_UNSPEC there are corner cases when this fails. E.g. calling local_host with "" and remote_host with an IPv6 only address results in setting up a local socket with AF_INET while trying to connect from there towards AF_INET6 will most likely fail. To prevent such cases with AF_UNSPEC, search prio calling any syscalls if local and remote site supports AF_INET or AF_INET6. In case both supported, prefer AF_INET6 Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766
2020-07-28socket: osmo_sock_local_ip: correct doxygen commentAlexander Couzens1-1/+0
There is no parameter fd. Change-Id: I1e31fcbf40d8abf99214b87e70e867b5ab66bd75
2020-03-10socket: Add osmo_sock_mcast_iface_set() to bind multicast to deviceHarald Welte1-0/+22
Change-Id: Ib52d22710020b56965aefcef09bde8247ace4a9c Related: OS#2966
2019-10-24socket.c: build multiaddr socket API helpers only if used by public APIsPau Espin Pedrol1-0/+4
Those two functions are only used by osmo_sock_init2_multiaddr(), which is only built if HAVE_LIBSCTP is defined. Avoid compiler warning about unusued function helpers if osmo_sock_init2_multiaddr() is not being built. Change-Id: I52769d6b8f70af1a8bda23d60b3230a932e71fab
2019-10-21socket: Remove unneeded condition check in osmo_sock_init2_multiaddr()Pau Espin Pedrol1-15/+15
Since we return error at the start of the function if proto != IPPROTO_SCTP, it makes no sense to check for proto != IPPROTO_UDP later on. Fixes: CID#205088 Change-Id: Ibba7eacaa9debb77d536d47dc85170c5ee79e479
2019-10-18socket: Introduce API osmo_sock_init2_multiaddr()Pau Espin Pedrol1-0/+286
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-10socket.c: Move glibc workarounds to same place in addrinfo_helper()Pau Espin Pedrol1-19/+10
Change-Id: Ifc3a30881f865f88bcfc1307a3c89c1ab79eecd4
2019-07-21tell ubsan to ignore SUN_LENEric Wild1-0/+3
ubsan will report undefined behavior due to the SUN_LEN macros interaction with a null pointer, so let's tell ubsan to ignore this function. After carefully reviewing the final publically availlable drafts of the C99,C11 and C18 standards I can confirm that dereferencing null pointers is still undefined behavior, as such ubsan will always warn with absolutely every existing compiler version. Since the sanitizers are periodically synced between llvm and gcc I'm also fairly confident that rebuilding everything with compiler_rt to use the integrated sanitizers would result in the same message. I sincerly hope that this explanation provides to be sufficient, If not I'd be willing to show up at the next llvm dev meeting to provide quotes from actual sanitizer developers to back up these claims. Change-Id: I0ff445072f1b46390c9f70b21d61c789e39358d5
2019-06-04make all library-internal static buffers thread-localHarald Welte1-1/+1
We have a number of library-internal static global buffers which are mainly used for various stringification functions. This worked as all of the related Osmocom programs were strictly single-threaded. Let's make those buffers at least thread-local. This way every thread gets their own set of buffers, and it's safe for multiple threads to execute the same functions once. They're of course still not re-entrant. If you need re-entrancy, you will need to use the _c() or _buf() suffix version of those functions and work with your own (stack or heap) buffers. Change-Id: I50eb2436a7c1261d79a9d2955584dce92780ca07
2019-06-01socket.c: fix Doxygen doc for osmo_sock_unix_init_ofd()Vadim Yanitskiy1-2/+2
One typo s/optionall/optionally/, and a few incorrect references. Change-Id: Iab42aa376b5cf4cf36413fede46e001c6b2d1525
2019-05-10osmo_sock_get_name_*: Ensure string is returned in error caseHarald Welte1-1/+3
osmo_sock_get_name_buf(): In case the getsockname() call is failing for some weird reason, we shouldn't return an uninitialized, non-zero-terminated string buffer to the caller, as most callers will be too lazy to test the return value. This holds even more true for users of the internal osmo_sock_get_name2() and osmo_sock_get_name2_c() functions which indeed very much ignore the return value of osmo_sock_get_name_buf(). Change-Id: I2d56327e96b7a6783cca38b828c5ee74aed776ae
2019-04-12Fix incorrect buffer size calculationVadim Yanitskiy1-1/+1
Calling sizeof() on a pointer to dynamically allocated memory would result in getting size of the pointer (usually 4 or 8 bytes) itself, but not the size of allocated memory. Change-Id: I8ffda4dea2b7f9b4b76dfeecad1fab6384c5a62c Fixes: CID#197629, CID#197628, CID#197627 Fixes: CID#197626, CID#197625, CID#197624
2019-04-10Add _c versions of functions that otherwise return static buffersHarald Welte1-0/+14
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-03-21select: Rename BSC_FD_* constants to OSMO_FD_*Harald Welte1-1/+1
The naming of these constants dates back to when the code was private within OpenBSC. Everything else was renamed (bsc_fd -> osmo_fd) at the time, but somehow the BSC_FD_* defines have been missed at the time. Keep compatibility #defines around, but allow us to migrate the applications to a less confusing naming meanwhile. Change-Id: Ifae33ed61a7cf0ae54ad487399e7dd2489986436