aboutsummaryrefslogtreecommitdiffstats
path: root/src/socket.c
AgeCommit message (Collapse)AuthorFilesLines
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
2019-03-19socket: osmo_sock_get_name() Use "const void *" as talloc contextHarald Welte1-1/+1
Change-Id: Ie6877277cddb0a9e049449c260afe3314ba65050
2019-01-17socket: add define for socket name lengthPhilipp Maier1-4/+2
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
2018-12-19add to osmo_sock_get_name*() APINeels Hofmeyr1-17/+47
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-07Add osmo_sockaddr_in_to_str_and_uint()Max1-5/+20
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-11-06socket.c: fix IP and port buffer sizesOliver Smith1-4/+4
Use INET6_ADDRSTRLEN (46) instead of 64 for IP address buffers, and 6 instead of 16 for port buffers (the highest possible port number is 65535). Change-Id: Ia25e2f3277ad2f60df31c08d12f42c1e6d2a14a6
2018-10-26add osmo_sock_get_{local,remote}_ip{,_port}()Oliver Smith1-25/+86
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-25cosmetic: fix typo (soocket -> socket)Oliver Smith1-1/+1
Change-Id: I2184bf12398902d933f3744bc094418cc6961e86
2018-09-26ensure unix socket paths are NUL-terminated for bind/connectStefan Sperling1-7/+5
The unix(7) man page recommends that sun_path is NUL-terminated when struct sockaddr_un is passed to a bind() or connect() call. Non-NUL-terminated paths only need to be dealt with at the receiving end of a UNIX domain socket. Commit 896ff6d erroneously assumed otherwise. This commit almost reverts 896ff6d: It only leaves the added osmo_strlcpy() overflow check in place. Change-Id: I6c4ac6b0a0eef4842beae4107f6f09f6cd29172a Fixes: 896ff6db161465d506bb9bb5bee2cdeef220dd2e Related: OS#2673
2018-08-29Properly deal with sockaddr_un socket path length limitations.Stefan Sperling1-4/+11
In osmo_sock_unix_init(), add support for non-NUL-terminated unix socket paths and return an error if the supplied socket path exceeds the maximum socket path length supported by the operating system. Change-Id: I19d935e5e3dd7928e6e153c6f5ad7044de726016 Related: OS#2673
2018-08-23socket: add flag to enforce SO_REUSEADDR on UDP socketsPhilipp Maier1-3/+3
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-02socket: do not set SO_REUSEADDR for IPPROTO_UDPPhilipp Maier1-25/+33
When UDP is used as protocol (proto=IPPROTO_DUP), then we should not set SO_REUSEADDR in the socket option. Because if we do, we allow two processes to bind on the same UDP port. The errornous situation will be undetectable to both applications. So lets only set SO_REUSEADDR when we do not use UDP. - Add check if we use UDP, if yes do not set SO_REUSEADDR Change-Id: I4a8ffb8d598aca88801a4a0322944d7cdd8d4047 Related: OS#3441
2018-08-02socket: check return code of setsockoptPhilipp Maier1-1/+8
the return code of the last setsockopt() call in osmo_sock_init() is not checked. Since all other calls to setsockopt are checked, lets check this one as well. - check return code of setsockopt() and close the socket on failure Change-Id: I96dbccc3bcff35bf39979dbe0c44aadc8ce20c83
2018-07-27fix strncpy bugs in socket.cNeels Hofmeyr1-2/+1
Change-Id: Iddf36d26b23dcef4f9b291fd7ead1907e38c3486
2018-07-20add osmo_sockaddr_to_str_and_uint()Neels Hofmeyr1-0/+20
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-04-18osmo_sockaddr_is_local: Fix memleakPau Espin Pedrol1-1/+4
Catched by AddressSanitizer in osmo-bts-trx while running tests in osmo-gsm-tester: ==31738==ERROR: LeakSanitizer: detected memory leaks Direct leak of 5744 byte(s) in 1 object(s) allocated from: #0 0x7ff7ec789ed0 in calloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc1ed0) #1 0x7ff7e952697c (/lib/x86_64-linux-gnu/libc.so.6+0x10297c) #2 0x7ff7e95274df in getifaddrs (/lib/x86_64-linux-gnu/libc.so.6+0x1034df) #3 0x7ff7eadcdc8f in osmo_sockaddr_is_local libosmocore/src/socket.c:537 Change-Id: I778d3c1f162abce0595e62670c29c5134bccd28d
2018-04-05socket.c: osmo_sock_init: Several logic fixes and log improvementsPau Espin Pedrol1-8/+16
See explanations in previous commit. Change-Id: I4889e777d8627fdfb52c97ab3ab353b6ed34aab2
2018-04-05socket.c: osmo_sock_init2: connect: Several logic fixes and log improvementsPau Espin Pedrol1-8/+17
See explanations in previous commits. Change-Id: Ib2f7577b9f498ae9d388ed1f79f6ca0ec6f09664
2018-04-05socket.c: osmo_sock_init2: bind: Several logic fixes and log improvementsPau Espin Pedrol1-7/+19
After investigating osmo-msc showing this log message and looking at the code, it's a bit difficult to find out what's going on in the code: socket.c:224 unable to bind socket: (null):0: Protocol not supported The root cause was not yet found, but probably SCTP is not enabled in the kernel of the host running it. The cod eis most probably failing during socket() and not due to bind error as the log says, so let's print an error if socket() fails. Then, if setsockopt fails, we want to still keep trying in case an extra addr was offered by addrinfo_helper. It is definetly wrong to continue if setsockopt fails, because then we are skipping the bind(), which is a fundamental part of what osmo_sock_init2 does. Then, let's print the bind error when it really happens, and re-write the extra log at the end if we reach the point at which no suitable addr is found. Change-Id: I1854422ad92dadf33ed4d849e15c0380c3bf1626
2018-01-22socket: use inet_ntop() instead of inet_ntoa() in osmo_sock_local_ip()Philipp Maier1-2/+2
The function inet_ntoa() stores its result in a static buffer and returns the pointer. When inet_ntoa() is called subsequently it overwrite the content of its static buffer with the new result. Since we osmo_sock_local_ip() is a library function we should use the more safe variant inet_ntop() in order to prevent unintentionally overwriting data that the caller might still need. Such an error would be hard to find. - Use the more safe inet_ntop() inestead of inet_ntoa() Change-Id: I9852b57736432032542bd96b6fdd4a2f08fc1f64
2018-01-22socket: fix socket leak in osmo_sock_local_ip()Philipp Maier1-0/+1
The socket that is opend to probe the correct local ip-address is not closed when the test is done. - Close socket when it is not needed anymore Change-Id: I7f3562a344b58f6298d2068314be1626a96e1b1d
2017-11-13Fix/Update copyright notices; Add SPDX annotationHarald Welte1-0/+2
Let's fix some erroneous/accidential references to wrong license, update copyright information where applicable and introduce a SPDX-License-Identifier to all files. Change-Id: I39af26c6aaaf5c926966391f6565fc5936be21af
2017-10-23socket: add function osmo_sock_local_ip() to query local ipPhilipp Maier1-0/+53
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-07-21osmo_sock_init2(): Fix creation of non-bound socketsHarald Welte1-1/+1
If osmo_sock_init2() was used with CONNECT flag but without BIND flag, an invalid check for "did we create a socket yet" caused the socket to never be created, and subsequently the entire function to return an error. Change-Id: I0206dbb9c5b8f74d7fb088576941b092acd2ca22
2017-07-13socket: Add new OSMO_SOCK_F_NO_MCAST_ALL optionHarald Welte1-0/+10
Using this option at socket creation, the caller can request disabling the IP_MULTICAST_ALL socket option. Change-Id: I5ab5de45c0b64ceb3636ea98245a23defa24ffd4
2017-07-13socket: Allow disabling multicast loop on socket creationHarald Welte1-4/+16
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-13socket: Unify listen() calls and check for erroneous returnsHarald Welte1-20/+33
We had three places at the end of socket initialization functions calling listen(). Let's unify that and fix some bugs: * close + return error in case of bad listen() result * don't call listen() on AF_UNIX SOCK_DGRAM sockets Change-Id: I7e8dbe3c0486bb3b9810b0add1331e93fc106d82
2017-07-13socket.c: Add multicast related convenience functionsHarald Welte1-0/+135
Change-Id: Id703e7a7a1e065181a4c76c088b8dcc1b7fe15a2
2017-07-03osmo_sock_get_name(): clearly indicate local and remoteNeels Hofmeyr1-2/+2
In a string like 127.0.0.1:2905<->127.0.0.1:60661 it is hard to tell which is the local part. I'd have expected it on the left, but it is actually on the right. To avoid doubt and bypass bikesheds on which side should be what, clearly mark the two sides as remote and local. (r=127.0.0.1:2905<->l=127.0.0.1:60661) Change-Id: I43dcc6a1906429bd0955fd7fe2eb5b8495b592d8
2017-06-23doxygen: unify use of \file across the boardNeels Hofmeyr1-4/+3
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-23doxygen: enable AUTOBRIEF, drop \briefNeels Hofmeyr1-11/+11
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-12update/extend doxygen documentationHarald Welte1-3/+2
It's a pity that even with this patch we still are fare away from having the whole API documented. However, at least we have a more solid foundation. Updates not only extend the documentation, but also make sure it is rendered properly in the doxygen HTML. Change-Id: I1344bd1a6869fb00de7c1899a8db93bba9bafce3
2017-05-26socket: Add osmo_sock_init2_ofd() functionPau Espin Pedrol1-0/+23
Will be used by osmo-bts-trx Change-Id: I3c655a4af64fb80497a5aaa811cce8005dba9cd9
2017-04-09Add osmo_sock_init2() function, allowing both BIND *and* CONNECTHarald Welte1-33/+187
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-02-09cosmetic: replace fprintf with LOGPPhilipp Maier1-8/+12
socket.c still uses fprintf to output error messages. This commit replaces the fprintf with proper LOGP messages. Change-Id: Ia2993415d5f5c33ccd719af239ff59252d11b764
2017-01-27socket: Introduce function to obtain socket nameHarald Welte1-1/+43
Using this function, one can obtain a human-readable string identifying the host and port names of the socket. Change-Id: Ib5de5c7b9effe1b0a363e4473a7be7fa38ca6ef3
2016-11-26osmo_sock_init(): Fix memory leak in error pathHarald Welte1-0/+1
We forgot to call freeaddrinfo() in an error path. Change-Id: Iccbd3beef4c4a70dc443131b909c45e650d8c6a2 Fixes: Coverity CID 135217
2016-08-29fix printf format for commit 'osmo_sock_init(): include host and port in ↵Neels Hofmeyr1-1/+1
error messages' Related: CID#143566 Change-Id: I75c542089749a0875d3d1913151fe838d7722ff2
2016-08-27osmo_sock_init(): include host and port in error messagesNeels Hofmeyr1-5/+16
For programs like osmo-hnbgw with numerous sockets, the message that some unspecified connection was refused is not very helpful. Also output the host and port where an error occured. Instead of perror, use fprintf(stderr, ..., strerror()) to be able to include a format string and print host and port as passed to osmo_sock_init(). Change-Id: I8d0343f51310699b78fcb83fd76fd93764acf3dc
2016-05-05Update doxygen annotations in libosmocoreHarald Welte1-0/+6
This adds and improves doxygen API descriptions all over libosmocore, reducing the 'white spots' that don't have any documentation.
2014-10-13Move common socket routine into separate functionMax1-29/+24
Signed-off-by: Max <max.suraev@fairwaves.co>
2014-09-11add missing copyright statements to source code filesHarald Welte1-0/+21
Some source code files didn't have the usual copyright and licence statement at their top. I'm adding them baesed on information in the commitlog.
2014-03-26src/socket: Adding unix domain socket supportÁlvaro Neira Ayuso1-0/+102
Added some function for adding the unix domain socket support. Signed-off-by: Alvaro Neira Ayuso <anayuso@sysmocom.de>
2013-02-15misc: Doxygen tweaks: fixed some typos and minor errorsKaterina Barone-Adesi1-1/+1
Doxygen generates quite a lot of warnings on libosmocore. Some of them are obvious typos - this patch aims to fix such low-hanging fruit.
2013-01-15socket: fix osmo_sock_init with SOCK_RAW and IPPROTO_RAWPablo Neira Ayuso1-3/+16
getaddrinfo returns EAI_SERVICE (-8) if that combination is used. More information available in here: http://sourceware.org/bugzilla/show_bug.cgi?id=15015 Reported by Holger Hans Peter Freyther. While at it, this patch also removes hints.ai_flags = 0 as memset to zero already happened just a bit before that.