aboutsummaryrefslogtreecommitdiffstats
path: root/src/socket.c
AgeCommit message (Collapse)AuthorFilesLines
2018-10-26add osmo_sock_get_{local,remote}_ip{,_port}()osmith/new-osmo-sock-functionsOliver 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.
2012-04-18doc: Fix the Doxygen section endingsSylvain Munaut1-1/+1
Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
2012-04-08sync prototype of osmo_sockaddr_is_local with declarationHarald Welte1-1/+1
in e476442cf0e84c65565ace545f5b73602b5f0ffc we changed from sockle_t to unsigned int, but only in the header, not in the implementation!
2011-11-09freebsd: Fixes for the compilation of libosmocore on FreeBSDHolger Hans Peter Freyther1-0/+2
alloca.h is not available on FreeBSD, use the default autoconf function to check for it, there is a complete list[1] of what to do for using alloca but let us see how far we get with this test. Include netinet/in.h for the IPv4 and IPv6 socket address. Check for dlopen in libraries and use this instead of linking -dl. [1] http://www.gnu.org/s/hello/manual/autoconf/Particular-Functions.html
2011-08-17some more doxygen work (include the notion of modules)Harald Welte1-1/+48
2011-06-13socket: add OSMO_SOCK_F_[CONNECT|BIND|NON_BLOCK] flagsPablo Neira Ayuso1-10/+28
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-05-31socket: getaddrinfo(): set AI_PASSIVE if we want to bindHarald Welte1-0/+3
This will tell getaddrinfo() that we want a INADDR_ANY style socket
2011-05-29socket: remove unused IPv4 related header includesHarald Welte1-2/+0
2011-05-24socket: Skip ifa's without ifa->ifa_addrHarald Welte1-0/+2
Apparently getifaddrs() returns ifa's without an ifa_addr set.
2011-05-22socket: use listen() and SO_REUSEADDR, new osmo_sock_init_ofd() functionHarald Welte1-1/+34
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-22remove debug printf from socket.cHarald Welte1-1/+0
2011-05-22GSMTAP/socket code: Check for sys/socket.h and conditionally compileHarald Welte1-1/+5