summaryrefslogtreecommitdiff
path: root/src/basic/socket-util.c
Commit message (Collapse)AuthorAgeFilesLines
* socket-util: introduce CMSG_FIND_AND_COPY_DATA()Yu Watanabe2023-04-161-0/+18
| | | | | | | | | | | | The cmd(3) man page says about CMSG_DATA(): > The pointer returned cannot be assumed to be suitably aligned for > accessing arbitrary payload data types. Applications should not cast > it to a pointer type matching the payload, but should instead use > memcpy(3) to copy data to or from a suitably declared object. Hence, if we want to use unaligned data in cmsg, we need to copy it before use. That's typically important for reading timestamps in RISCV32, as the time_t is 64bit and size_t is 32bit on the system.
* tree-wide: port more code over to CMSG_TYPED_DATA()Lennart Poettering2023-04-131-1/+1
|
* socket-util: fix socket_get_family()Lennart Poettering2023-03-101-17/+13
| | | | Function didn't actually return anything useful. Quite a shame.
* socket-util: make connect_unix_path() work with a NULL pathLennart Poettering2023-02-171-28/+36
|
* src/shared/: split AF_UNIX/AF_VSOCK address parsing into src/basic/Luca Boccassi2023-01-051-0/+68
| | | | | We'll use it from libsystemd0 later, but AF_INET/6 requires some netlink calls and thus the additional library dependency
* tree-wide: use -EBADF for fd initializationZbigniew Jędrzejewski-Szmek2022-12-191-2/+2
| | | | | | | | | | | | | | | | -1 was used everywhere, but -EBADF or -EBADFD started being used in various places. Let's make things consistent in the new style. Note that there are two candidates: EBADF 9 Bad file descriptor EBADFD 77 File descriptor in bad state Since we're initializating the fd, we're just assigning a value that means "no fd yet", so it's just a bad file descriptor, and the first errno fits better. If instead we had a valid file descriptor that became invalid because of some operation or state change, the other errno would fit better. In some places, initialization is dropped if unnecessary.
* tree-wide: add global ascii_isdigit() + ascii_isalpha()Lennart Poettering2022-07-051-1/+1
| | | | | | | | We now have a local implementation in string-util-fundamental.c, but it's useful at a lot of other places, hence let's give it a more expressive name and share it across the tree. Follow-up for: 8d9156660d6958c8d63b1d44692968f1b5d33920
* Merge pull request #23616 from keszybz/in-addr-to-string-formattingYu Watanabe2022-06-091-3/+1
|\ | | | | Add macros that allocate a fixed buffer for in_addr_to_string(), in_addr_prefix+to_string()
| * tree-wide: convert inet_ntop() calls to anonymous-buffer macrosZbigniew Jędrzejewski-Szmek2022-06-061-3/+1
| |
* | basic/socket-util: align tablesZbigniew Jędrzejewski-Szmek2022-06-071-20/+20
|/
* socket-util: add new connect_unix_path() helperLennart Poettering2022-05-141-0/+48
| | | | | | | | | This is a short helper for connecting to AF_UNIX sockets in the file system. It works around the 108ch limit of sockaddr_un, and supports "at" style fds. This doesn't come with a test of its own, but the next patch will add that.
* socket-util: change sockaddr_un_set_path() to return recognizable error on ↵Lennart Poettering2022-05-131-1/+4
| | | | | | | | | 108ch limit This way we can implement nice fallbacks later on. While we are at it, provide a test for this (one that is a bit over the top, but then again, we can never have enough tests).
* socket-util: add helper for generically initializing sockaddr_union from ↵Lennart Poettering2021-11-221-0/+35
| | | | in_addr_union
* tree-wide: use new RET_NERRNO() helper at various placesLennart Poettering2021-11-161-18/+5
|
* util: define initializer for 'struct ucred' that properly invalidates all fieldsLennart Poettering2021-10-111-1/+1
| | | | | i.e. let's make sure to invalid uid/gid to UID_INVAID + GID_INVALID instead of zero.
* tree-wide: make format_ifname() or friends return negative errno on failureYu Watanabe2021-09-291-10/+11
| | | | | | | | Also, - drop unnecessary +1 from buffer size, as IF_NAMESIZE or IFNAMSIZ includes the nul at the end. - format_ifname() does not update buffer on failure, - introduces format_ifname_alloc(), FORMAT_IFNAME(), and their friends.
* socket-util: const-ify iov parameter in send_one_fd_iov_saLuca Boccassi2021-09-201-2/+2
|
* sysctl-util: make sysctl_write_ip_property() a wrapper around sysctl_write()Lennart Poettering2021-09-151-1/+1
| | | | | | | | It does the same stuff, let's use the same codepaths as much as we can. And while we are at it, let's generate good error codes in case we are called with unsupported parameters/let's validate stuff more that might originate from user input.
* socket-util: split out checking valid character for ifname into ↵Yu Watanabe2021-06-231-10/+17
| | | | ifname_valid_char()
* tree-wide: add missing whitespace at the end of commentsYu Watanabe2021-06-151-2/+2
|
* socket-util: refuse ifnames with embedded '%' as invalidLennart Poettering2021-03-101-1/+4
| | | | | | | | | | | | | | | | So Linux has this (insane — in my opinion) "feature" that if you name a network interface "foo%d" then it will automatically look for the interface starting with "foo…" with the lowest number that is not used yet and allocates that. We should never clash with this "magic" handling of ifnames, hence refuse this, since otherwise we never know what the name is we end up with. We should probably switch things from a deny list to an allow list sooner or later and be much stricter. Since the kernel directly enforces only very few rules on the names, we'd need to do some research what is safe and what is not first, though.
* socket-util: initialize variable with cleanup attributeYu Watanabe2021-03-071-1/+1
| | | | | | Follow-up for 83e03c4fc23dae0cbb3fd4e7c2f9ef533fc26160. Fixes CID#1448460.
* Merge pull request #18896 from poettering/no-localhost-ipv6Yu Watanabe2021-03-061-2/+41
|\ | | | | if ipv6 is turned off, don't synthesize localhost as ::1 ever
| * socket-util: add helper for checking if IPv6 is enabledLennart Poettering2021-03-051-0/+26
| |
| * socket-util: cache result of socket_ipv6_is_supported()Lennart Poettering2021-03-051-3/+16
| | | | | | | | And while we are at it, log about unexpected errors.
* | socket-util: refuse "all" and "default" as valid ifnamesLennart Poettering2021-03-061-0/+5
|/ | | | Let's avoid collisions with special sysctls.
* socket-util: tighten parsing of ifnamesLennart Poettering2021-02-151-8/+8
| | | | | | | Numeric ifnames should be acceptable only if that's enabled by flag, and refused otherwise. Hence, let's parse as ifindex first, and if that works decide. Finally, let's refuse any numeric ifnames that are not valid ifindexs, but look like them.
* socket-util: add common API for querying socket MTULennart Poettering2020-12-071-0/+32
|
* socket-util: make several socket_set_xxx() functions inlineYu Watanabe2020-12-041-94/+3
|
* socket-util: add sockaddr_in_addr() helperLennart Poettering2020-12-021-1/+20
| | | | | This extracts the IP address (as union in_addr_union) from a socket address (i.e. a struct sockaddr).
* license: LGPL-2.1+ -> LGPL-2.1-or-laterYu Watanabe2020-11-091-1/+1
|
* socket-util: make socket_set_recvpktinfo control PACKET_AUXDATA sockopt on ↵Lennart Poettering2020-10-271-0/+3
| | | | | | AF_PACKET Just for the sake of completness.
* socket-util: reset length argument for second getsockopt call in ↵Christian Göttsche2020-09-151-0/+2
| | | | | | fd_[gs]et_rcvbuf In case the first getsockopt() call changes the value.
* tree-wide: add helper for IPv4/IPv6 sockoptsLennart Poettering2020-09-111-1/+156
| | | | | | | | | | A variety of sockopts exist both for IPv4 and IPv6 but require a different pair of sockopt level/option number. Let's add helpers for these that internally determine the right sockopt to call. This should shorten code that generically wants to support both ipv4 + ipv6 and for the first time adds correct support for some cases where we only called the ipv4 versions, and not the ipv6 options.
* Minor simplification in sockaddr_un_set_path()Zbigniew Jędrzejewski-Szmek2020-09-101-3/+1
|
* shared: don't unconditionally set SOCK_STREAM as type in socket_address_parse()Zbigniew Jędrzejewski-Szmek2020-09-101-5/+5
| | | | | | | | | | We would set .type to a fake value. All real callers (outside of tests) immediately overwrite .type with a proper value after calling socket_address_parse(). So let's not set it and adjust the few places that relied on it being set to the fake value. socket_address_parse() is modernized to only set the output argument on success.
* basic: show interface scope in sockaddr_pretty()Zbigniew Jędrzejewski-Szmek2020-09-101-4/+8
| | | | | | | | If the interface scope is specified, this changes the meaning of the address quite significantly. Let's show the IPv6 scope_id if present. Sadly we don't even have a test for sockaddr_pretty() output :( This will be implicitly tested through socket_address_parse() later on.
* basic: convert ifname_valid_full() to take flags and allow numeric interfacesZbigniew Jędrzejewski-Szmek2020-09-101-10/+17
|
* util: introduce fd_set_{snd,rcv}buf()Yu Watanabe2020-09-091-6/+6
|
* util: try to set with SO_{RCV,SND}BUFFORCE when requested size is larger ↵Yu Watanabe2020-09-091-12/+28
| | | | | | | | | | | | | | | | | | than the kernel limit The commit 10ce2e0681ac16e7bb3619b7bb1a72a6f98a2f2c inverts the order of SO_{RCV,SND}BUFFORCE and SO_{RCV,SND}BUF. However, setting buffer size with SO_{RCV,SND}BUF does not fail even if the requested size is larger than the kernel limit. Hence, SO_{RCV,SND}BUFFORCE will not use anymore and the buffer size is always limited by the kernel limit even if we have the priviledge to ignore the limit. This makes the buffer size is checked after configuring it with SO_{RCV,SND}BUF, and if it is still not sufficient, then try to set it with FORCE command. With this commit, if we have enough priviledge, the requested buffer size is correctly set. Hopefully fixes #14417.
* util: refuse to set too large value for socket buffer sizeYu Watanabe2020-09-091-0/+6
|
* all: fix minor typosYuri Chornoivan2020-07-071-1/+1
| | | | | | [thaller@redhat.com: original patch by Yuri, extracted from [1]] [1] https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/565
* util: use setsockopt_int() at one more placeYu Watanabe2020-07-021-4/+4
|
* tree-wide: port to fd_wait_for_event()Lennart Poettering2020-06-101-10/+4
| | | | | | | | Prompted by the discussion on #16110, let's migrate more code to fd_wait_for_event(). This only leaves 7 places where we call into poll()/poll() directly in our entire codebase. (one of which is fd_wait_for_event() itself)
* tree-wide: check POLLNVAL everywhereLennart Poettering2020-06-101-0/+3
| | | | | | | | | | | | | poll() sets POLLNVAL inside of the poll structures if an invalid fd is passed. So far we generally didn't check for that, thus not taking notice of the error. Given that this specific kind of error is generally indication of a programming error, and given that our code is embedded into our projects via NSS or because people link against our library, let's explicitly check for this and convert it to EBADF. (I ran into a busy loop because of this missing check when some of my test code accidentally closed an fd it shouldn't close, so this is a real thing)
* socket-util: add generic socket_pass_pktinfo() helperLennart Poettering2020-05-271-0/+22
| | | | | The helper turns on the protocol specific "packet info" structure cmsg for three relevant protocols we know.
* tree-wide: make sure our control buffers are properly alignedLennart Poettering2020-05-071-8/+2
| | | | | | | | | | | | | We always need to make them unions with a "struct cmsghdr" in them, so that things properly aligned. Otherwise we might end up at an unaligned address and the counting goes all wrong, possibly making the kernel refuse our buffers. Also, let's make sure we initialize the control buffers to zero when sending, but leave them uninitialized when reading. Both the alignment and the initialization thing is mentioned in the cmsg(3) man page.
* tree-wide: remove redundant assignmentsLennart Poettering2020-05-071-2/+0
| | | | | We already initialize the fields a few lines up to the very same values, hence remove this.
* tree-wide: use cmsg_find() helper at various places where appropriateLennart Poettering2020-04-231-11/+2
|
* tree-wide: use recvmsg_safe() at various placesLennart Poettering2020-04-231-6/+7
| | | | | | | | | | | | | | | | | Let's be extra careful whenever we return from recvmsg() and see MSG_CTRUNC set. This generally means we ran into a programming error, as we didn't size the control buffer large enough. It's an error condition we should at least log about, or propagate up. Hence do that. This is particularly important when receiving fds, since for those the control data can be of any size. In particular on stream sockets that's nasty, because if we miss an fd because of control data truncation we cannot recover, we might not even realize that we are one off. (Also, when failing early, if there's any chance the socket might be AF_UNIX let's close all received fds, all the time. We got this right most of the time, but there were a few cases missing. God, UNIX is hard to use)