summaryrefslogtreecommitdiff
path: root/lib/tsocket
AgeCommit message (Collapse)AuthorFilesLines
2026-02-15lib: Remove very little used wrapper conv_str_u64()Volker Lendecke1-5/+8
Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Anoop C S <anoopcs@samba.org>
2025-07-17lib/tsocket: optimize tdgram_recvfrom_done() into tdgram_recvfrom_send()Stefan Metzmacher1-0/+16
For callers using tdgram_bsd_optimize_recvfrom() it is every useful to know it data was already waiting in the socket. In that case the result from tdgram_bsd_recvfrom_send() would already report tevent_req_is_in_progress() as false. But the result of tdgram_recvfrom_send() available to the caller would see tevent_req_is_in_progress() reporting true. With this change also the result of tdgram_recvfrom_send() would report tevent_req_is_in_progress() as false, which will be useful for callers, which would otherwise set a timeout on the request. Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org>
2025-06-18async_sock: move samba_socket_{poll,sock,poll_or_sock}_error() from ↵Stefan Metzmacher2-1/+2
util_net.c to async_sock.c The change to LGPL is also intended. It will be used in the the next commit that adds wait_for_error_send/recv. Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Volker Lendecke <vl@samba.org>
2025-06-18lib/tsocket: implement tstream_bsd_monitor_send/recvStefan Metzmacher1-4/+177
Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Volker Lendecke <vl@samba.org>
2025-06-18lib/tsocket: add tstream_monitor_send/recv infrastructureStefan Metzmacher3-1/+206
Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Volker Lendecke <vl@samba.org>
2025-06-18lib/tsocket: allow only one tstream_disconnect_send/recvStefan Metzmacher1-0/+40
And don't allow readv and writev anymore. Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Volker Lendecke <vl@samba.org>
2025-06-18lib/tsocket: make use of tevent_req_set_cleanup_fn() instead of ↵Stefan Metzmacher2-45/+91
talloc_set_destructor() Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Volker Lendecke <vl@samba.org>
2024-08-06tsocket: Use iov_buflenVolker Lendecke1-23/+11
Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2023-10-24lib/tsocket: add tstream_bsd_fail_readv_first_error()Stefan Metzmacher2-2/+45
This gives the caller the option to fail immediately if TEVENT_FD_ERROR appear even with pending bytes in the recv queue. Servers typically want to activate this in order to avoid pointless work, while clients typically want to read pending responses from the recv queue. Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24lib/tsocket: make use of TEVENT_FD_ERROR in tstream_bsd_fde_handler()Stefan Metzmacher1-111/+116
This makes the logic introduced to fix bug #15202 simpler. While developing this I noticed that a lot of callers rely on the fact that they can read the pending bytes out of the recv queue before EOF is reported. So I changed the code handle TEVENT_FD_ERROR together with TEVENT_FD_READ in a way that keep the existing callers happy. In the next step we'll add a way to let callers opt-in in order to fail immediately if TEVENT_FD_ERROR appears (even if there are pending bytes remaining in the recv queue). BUG: https://bugzilla.samba.org/show_bug.cgi?id=15202 Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24lib/tsocket: let tstream_bsd_connect_send() use TEVENT_FD_ERROR instead of ↵Stefan Metzmacher1-1/+1
TEVENT_FD_READ This mostly cosmetic, but now that we have TEVENT_FD_ERROR we should use it. Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24lib/tsocket: make use of samba_socket_sock_error()Stefan Metzmacher1-9/+1
This is nicer than calling getsockopt(state->fd, SOL_SOCKET, SO_ERROR) directly. Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24lib/tsocket: make use of samba_socket_poll_or_sock_error()Stefan Metzmacher1-101/+2
This is just a copy of the existing code... Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-08-14tsocket: Fix code spellingJoseph Sutton1-7/+7
Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-04-14lib:tsocket: Fix code spellingAndreas Schneider2-4/+4
Signed-off-by: Andreas Schneider <asn@samba.org> Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
2023-03-14tsocket: Increase tcp_user_timeout max_loopsAndrew Bartlett1-1/+1
Often, on rackspace GitLab CI runners, we get: UNEXPECTED(failure): samba.unittests.tsocket_tstream.test_tstream_more_tcp_user_timeout_spin(none) REASON: Exception: Exception: 0xf == 0xf ../../lib/tsocket/tests/test_tstream.c:405: error: Failure! This allows us more spins before we fail the test. BUG: https://bugzilla.samba.org/show_bug.cgi?id=15328 Signed-off-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
2023-01-17lib/tsocket: fix a typo in the tsocket guide docBjörn Baumbach1-1/+1
Signed-off-by: Björn Baumbach <bb@sernet.de> Reviewed-by: Ralph Boehme <slow@samba.org> Autobuild-User(master): Björn Baumbach <bb@sernet.de> Autobuild-Date(master): Tue Jan 17 18:23:18 UTC 2023 on sn-devel-184
2022-12-12tsocket: Fix the build on FreeBSDVolker Lendecke2-21/+36
FreeBSD does not have TCP_USER_TIMEOUT Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2022-10-19lib/tsocket: avoid endless cpu-spinning in tstream_bsd_fde_handler()Stefan Metzmacher1-5/+116
There were some reports that strace output an LDAP server socket is in CLOSE_WAIT state, returning EAGAIN for writev over and over (after a call to epoll() each time). In the tstream_bsd code the problem happens when we have a pending writev_send, while there's no readv_send pending. In that case we still ask for TEVENT_FD_READ in order to notice connection errors early, so we try to call writev even if the socket doesn't report TEVENT_FD_WRITE. And there are situations where we do that over and over again. It happens like this with a Linux kernel: tcp_fin() has this: struct tcp_sock *tp = tcp_sk(sk); inet_csk_schedule_ack(sk); sk->sk_shutdown |= RCV_SHUTDOWN; sock_set_flag(sk, SOCK_DONE); switch (sk->sk_state) { case TCP_SYN_RECV: case TCP_ESTABLISHED: /* Move to CLOSE_WAIT */ tcp_set_state(sk, TCP_CLOSE_WAIT); inet_csk_enter_pingpong_mode(sk); break; It means RCV_SHUTDOWN gets set as well as TCP_CLOSE_WAIT, but sk->sk_err is not changed to indicate an error. tcp_sendmsg_locked has this: ... err = -EPIPE; if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) goto do_error; while (msg_data_left(msg)) { int copy = 0; skb = tcp_write_queue_tail(sk); if (skb) copy = size_goal - skb->len; if (copy <= 0 || !tcp_skb_can_collapse_to(skb)) { bool first_skb; new_segment: if (!sk_stream_memory_free(sk)) goto wait_for_space; ... wait_for_space: set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); if (copied) tcp_push(sk, flags & ~MSG_MORE, mss_now, TCP_NAGLE_PUSH, size_goal); err = sk_stream_wait_memory(sk, &timeo); if (err != 0) goto do_error; It means if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) doesn't hit as we only have RCV_SHUTDOWN and sk_stream_wait_memory returns -EAGAIN. tcp_poll has this: if (sk->sk_shutdown & RCV_SHUTDOWN) mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; So we'll get EPOLLIN | EPOLLRDNORM | EPOLLRDHUP triggering TEVENT_FD_READ and writev/sendmsg keeps getting EAGAIN. So we need to always clear TEVENT_FD_READ if we don't have readable handler in order to avoid burning cpu. But we turn it on again after a timeout of 1 second in order to monitor the error state of the connection. And now that our tsocket_bsd_error() helper checks for POLLRDHUP, we can check if the socket is in an error state before calling the writable handler when TEVENT_FD_READ was reported. Only on error we'll call the writable handler, which will pick the error without calling writev(). BUG: https://bugzilla.samba.org/show_bug.cgi?id=15202 Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org>
2022-10-19lib/tsocket: remember the first error as tstream_bsd->errorStefan Metzmacher1-0/+39
If we found that the connection is broken, there's no point in trying to use it anymore, so just return the first error we detected. BUG: https://bugzilla.samba.org/show_bug.cgi?id=15202 Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org>
2022-10-19lib/tsocket: check for errors indicated by poll() before getsockopt(fd, ↵Stefan Metzmacher1-1/+79
SOL_SOCKET, SO_ERROR) This also returns an error if we got TCP_FIN from the peer, which is only reported by an explicit POLLRDHUP check. Also on FreeBSD getsockopt(fd, SOL_SOCKET, SO_ERROR) fetches and resets the error, so a 2nd call no longer returns an error. BUG: https://bugzilla.samba.org/show_bug.cgi?id=15202 Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org>
2022-10-19lib/tsocket: split out tsocket_bsd_error() from tsocket_bsd_pending()Stefan Metzmacher1-19/+23
This will be used on its own soon. BUG: https://bugzilla.samba.org/show_bug.cgi?id=15202 Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org>
2022-10-19lib/tsocket: Add tests for loop on EAGAINAndrew Bartlett4-0/+641
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15202 Pair-Programmed-With: Stefan Metzmacher <metze@samba.org> Signed-off-by: Andrew Bartlett <abartlet@samba.org> Signed-off-by: Stefan Metzmacher <metze@samba.org>
2021-09-28selftest: test tsocket_address_inet_from_hostport_stringsUri Simchoni1-0/+143
Signed-off-by: Uri Simchoni <uri@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Autobuild-User(master): Andrew Bartlett <abartlet@samba.org> Autobuild-Date(master): Tue Sep 28 10:34:12 UTC 2021 on sn-devel-184
2021-09-28selftest: add more tests for test_address_inet_from_stringsUri Simchoni1-0/+43
Test the case of NULL address as input Signed-off-by: Uri Simchoni <uri@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2021-09-28lib/tsocket: new function to parse host port strs.Matthew Grant2-0/+143
tsocket_address_inet_from_hostport_strings() on top of tsocket_address_inet_from_strings(), implementing the ability to parse a port number appended to an IPv6 or IPv4 address. IPv6 addresses can also optionally have square brackets around them, but these are needed to specify the port number as colon is used to delimit port from the IP address in the string. Note that this code just recognises and parses the strings with port given, or just IPv6 with square brackets. The rest of the parsing is passed on to tsocket_address_inet_from strings(), and errors from there passed back up the stack. Signed-off-by: Matthew Grant <grantma@mattgrant.net.nz> Reviewed-by: Uri Simchoni <uri@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2021-09-16lib/tsocket: Fix build on FreebsdAmitay Isaacs1-0/+2
This fixes the following build error on freebsd. [1567/3959] Compiling lib/tsocket/tsocket_bsd.c ../../lib/tsocket/tsocket_bsd.c:415:8: error: use of undeclared identifier 'EAI_ADDRFAMILY' case EAI_ADDRFAMILY: ^ On FreeBSD EAI_ADDRFAMILY is obsoleted. Here's the relevant excerpt from netdb.h on FreeBSD 13. ----------------------------------------------------------------- /* * Error return codes from gai_strerror(3), see RFC 3493. */ #if 0 /* Obsoleted on RFC 2553bis-02 */ #define EAI_ADDRFAMILY 1 /* address family for hostname not supported */ #endif ----------------------------------------------------------------- Signed-off-by: Amitay Isaacs <amitay@gmail.com> Reviewed-by: Uri Simchoni <uri@samba.org> Autobuild-User(master): Jeremy Allison <jra@samba.org> Autobuild-Date(master): Thu Sep 16 19:42:19 UTC 2021 on sn-devel-184
2021-09-13tsocket: set errno on some failures of tsocket_address_inet_from_stringsUri Simchoni1-0/+2
Fix setting errno on all failure modes of tsocket_address_inet_from_strings. Signed-off-by: Uri Simchoni <uri@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org> Autobuild-User(master): Jeremy Allison <jra@samba.org> Autobuild-Date(master): Mon Sep 13 22:27:59 UTC 2021 on sn-devel-184
2021-09-13selftest: add a unit test for tsocket_address_inet_from_stringsUri Simchoni2-0/+189
Signed-off-by: Uri Simchoni <uri@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2021-07-08lib/tsocket: Free subreq as soon as possibleSamuel Cabrero1-0/+1
This is not a memory leak as it is freed when the parent req's state is freed, but will help in low memory situations. Signed-off-by: Samuel Cabrero <scabrero@samba.org> Reviewed-by: Stefan Metzmacher <metze@samba.org> Autobuild-User(master): Stefan Metzmacher <metze@samba.org> Autobuild-Date(master): Thu Jul 8 10:21:25 UTC 2021 on sn-devel-184
2021-01-14tsocket: Fix a few typosVolker Lendecke1-3/+3
Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Samuel Cabrero <scabrero@samba.org>
2020-06-15tsocket: Fix a typoVolker Lendecke1-1/+1
Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2020-02-28Rename macro argument s_addr due to it already being definedPeter Eriksson2-7/+7
Signed-off-by: Peter Eriksson <pen@lysator.liu.se> Reviewed-by: Jeremy Allison <jra@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2020-02-26lib/tsocket: add a comment regarding TEVENT_FD_READ in ↵Stefan Metzmacher1-0/+8
tstream_bsd_connect_send() This is different compared to the raw usage of [e]poll where [E]POLLOUT is enough to see errors via POLLERR/POLLHUP. Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2019-09-01Spelling fixes s/implentation/implementation/Mathieu Parent1-1/+1
Signed-off-by: Mathieu Parent <math.parent@gmail.com> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
2019-07-22lib:tsocket: New function to build a tsocket_context from samba_addressSamuel Cabrero2-0/+39
Signed-off-by: Samuel Cabrero <scabrero@suse.de> Reviewed-by: Stefan Metzmacher <metze@samba.org>
2019-07-22s3:utils: New struct to fix strict aliasing issues with sockets APISamuel Cabrero1-59/+47
Signed-off-by: Samuel Cabrero <scabrero@suse.de> Reviewed-by: Andreas Schneider <asn@samba.org>
2019-03-22tsocket: Simplify tsocket.hVolker Lendecke1-1/+0
tevent.h already includes talloc.h Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2018-12-17lib:tsocket: Check for DOXYGEN as a #defineAndreas Schneider1-1/+1
Signed-off-by: Andreas Schneider <asn@samba.org> Reviewed-by: Gary Lockyer <gary@catalyst.net.nz> Autobuild-User(master): Gary Lockyer <gary@samba.org> Autobuild-Date(master): Mon Dec 17 00:10:10 CET 2018 on sn-devel-144
2018-01-04tsocket: Fix typosVolker Lendecke1-2/+2
Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2016-06-30tsocket: Do not dereference a NULL pointerAndreas Schneider1-4/+8
Make sure the lrbsda pointer is not allocated and we will not end up dereferencing a NULL pointer. In practice this can't happen, but this change links the pointer with the code that uses it. Found by Coverity. Signed-off-by: Andreas Schneider <asn@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org> Autobuild-User(master): Jeremy Allison <jra@samba.org> Autobuild-Date(master): Thu Jun 30 02:53:02 CEST 2016 on sn-devel-144
2016-02-10lib/tsocket: workaround sockets not supporting FIONREADRalph Boehme1-1/+61
Netlink sockets don't support querying pending bytes with ioctl(fd, FIONREAD, ...) and would return EOPNOTSUPP, so use recvmsg() with MSG_PEEK|MSG_TRUNC as a fallback. The MSG_TRUNC flag to recvmsg() is Linux only, but netlink is as well, so we're safe for now. Bug: https://bugzilla.samba.org/show_bug.cgi?id=11714 Signed-off-by: Ralph Boehme <slow@samba.org> Reviewed-by: Amitay Isaacs <amitay@gmail.com> Reviewed-by: Martin Schwenke <martin@meltin.net> Autobuild-User(master): Ralph Böhme <slow@samba.org> Autobuild-Date(master): Wed Feb 10 10:30:24 CET 2016 on sn-devel-144
2015-10-21lib/tsocket: fix non-blockging connect() error handlingRalph Boehme1-8/+5
Non-blockging connect() either returns immediate success, or -1 with errno EINPROGESS as indication that the connection is pending. All other errnos indicate immediate failure. Signed-off-by: Ralph Boehme <slow@samba.org> Reviewed-by: Stefan Metzmacher <metze@samba.org>
2015-06-19Fix a typoVolker Lendecke1-2/+2
Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Ira Cooper <ira@samba.org> Autobuild-User(master): Volker Lendecke <vl@samba.org> Autobuild-Date(master): Fri Jun 19 01:05:17 CEST 2015 on sn-devel-104
2015-06-12lib/tsocket: add tdgram_inet_udp_broadcast_socket()Stefan Metzmacher2-0/+59
This is similar to tdgram_inet_udp_socket(), but it allows the use of ipv4 broadcast traffic. BUG: https://bugzilla.samba.org/show_bug.cgi?id=11316 Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2015-06-12lib/tsocket: add tdgram_bsd_existing_socket() helper functionStefan Metzmacher2-0/+66
This is similar to tstream_bsd_existing_socket(). Both help to migrate strange code path to using the tstream or tdgram abstractions. BUG: https://bugzilla.samba.org/show_bug.cgi?id=11316 Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2015-06-05tsocket: Use common code in tsocket_bsd_common_prepare_fdVolker Lendecke2-30/+8
Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: "Stefan (metze) Metzmacher" <metze@samba.org>
2015-02-24tsocket: Use iov_advanceVolker Lendecke2-51/+18
Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2015-02-24tsocket: Fix a typoVolker Lendecke1-1/+1
Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2013-07-01tsocket: Pass the full port number to getaddrinfo().Andreas Schneider1-1/+1
The code stripped port numbers above 9999 down to 4 digits. Signed-off-by: Andreas Schneider <asn@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org> Autobuild-User(master): Jeremy Allison <jra@samba.org> Autobuild-Date(master): Mon Jul 1 21:10:53 CEST 2013 on sn-devel-104