]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
async_sock: move samba_socket_{poll,sock,poll_or_sock}_error() from util_net.c to...
authorStefan Metzmacher <metze@samba.org>
Thu, 22 May 2025 08:06:53 +0000 (10:06 +0200)
committerStefan Metzmacher <metze@samba.org>
Wed, 18 Jun 2025 17:52:37 +0000 (17:52 +0000)
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>
lib/async_req/async_sock.c
lib/async_req/async_sock.h
lib/async_req/wscript_build
lib/tsocket/tsocket_bsd.c
lib/tsocket/wscript
lib/util/util_net.c
lib/util/util_net.h
source3/smbd/smb2_server.c

index 06ffb9429678b98289b7c2e4fdb86795bfb47bd4..fadcb4f3daa310635d03c2d6815642cc78f22971 100644 (file)
@@ -24,6 +24,7 @@
 #include "replace.h"
 #include "system/network.h"
 #include "system/filesys.h"
+#include "system/select.h"
 #include <talloc.h>
 #include <tevent.h>
 #include "lib/async_req/async_sock.h"
 /* Note: lib/util/ is currently GPL */
 #include "lib/util/tevent_unix.h"
 #include "lib/util/samba_util.h"
+#include "lib/util/select.h"
+
+int samba_socket_poll_error(int fd)
+{
+       struct pollfd pfd = {
+               .fd = fd,
+#ifdef POLLRDHUP
+               .events = POLLRDHUP, /* POLLERR and POLLHUP are not needed */
+#endif
+       };
+       int ret;
+
+       errno = 0;
+       ret = sys_poll_intr(&pfd, 1, 0);
+       if (ret == 0) {
+               return 0;
+       }
+       if (ret != 1) {
+               return POLLNVAL;
+       }
+
+       if (pfd.revents & POLLERR) {
+               return POLLERR;
+       }
+       if (pfd.revents & POLLHUP) {
+               return POLLHUP;
+       }
+#ifdef POLLRDHUP
+       if (pfd.revents & POLLRDHUP) {
+               return POLLRDHUP;
+       }
+#endif
+
+       /* should never be reached! */
+       return POLLNVAL;
+}
+
+int samba_socket_sock_error(int fd)
+{
+       int ret, error = 0;
+       socklen_t len = sizeof(error);
+
+       /*
+        * if no data is available check if the socket is in error state. For
+        * dgram sockets it's the way to return ICMP error messages of
+        * connected sockets to the caller.
+        */
+       ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
+       if (ret == -1) {
+               return ret;
+       }
+       if (error != 0) {
+               errno = error;
+               return -1;
+       }
+       return 0;
+}
+
+int samba_socket_poll_or_sock_error(int fd)
+{
+       int ret;
+       int poll_error = 0;
+
+       poll_error = samba_socket_poll_error(fd);
+       if (poll_error == 0) {
+               return 0;
+       }
+
+#ifdef POLLRDHUP
+       if (poll_error == POLLRDHUP) {
+               errno = ECONNRESET;
+               return -1;
+       }
+#endif
+
+       if (poll_error == POLLHUP) {
+               errno = EPIPE;
+               return -1;
+       }
+
+       /*
+        * POLLERR and POLLNVAL fallback to
+        * getsockopt(fd, SOL_SOCKET, SO_ERROR)
+        * and force EPIPE as fallback.
+        */
+
+       errno = 0;
+       ret = samba_socket_sock_error(fd);
+       if (ret == 0) {
+               errno = EPIPE;
+       }
+
+       if (errno == 0) {
+               errno = EPIPE;
+       }
+
+       return -1;
+}
 
 struct async_connect_state {
        int fd;
index 780195e3725d983dc432919e94c51fd47d9c1e9a..2f84b7b8ec0e5ff0536b133885fe06102ef4bf20 100644 (file)
 #include <tevent.h>
 #include "system/network.h"
 
+/*
+ * check for POLLERR or POLL*HUP
+ */
+int samba_socket_poll_error(int fd);
+/*
+ * getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len)
+ */
+int samba_socket_sock_error(int fd);
+/*
+ * check for POLL*HUP and fallback to
+ * getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len)
+ */
+int samba_socket_poll_or_sock_error(int fd);
+
 struct tevent_req *async_connect_send(
        TALLOC_CTX *mem_ctx, struct tevent_context *ev, int fd,
        const struct sockaddr *address, socklen_t address_len,
index 4486a5b2f06e1d238a2c0505f87609102d0b4878..fbf712d57f2430d82c4a904a7f434304229a9606 100644 (file)
@@ -4,7 +4,7 @@
 bld.SAMBA_SUBSYSTEM('LIBASYNC_REQ',
        source='async_sock.c',
        public_deps='talloc tevent iov_buf',
-       deps='tevent-util socket-blocking'
+       deps='tevent-util samba-util socket-blocking'
        )
 
 bld.SAMBA_BINARY('async_connect_send_test',
index 83f2efcfc840a8205052e9fb404073436f6a202a..aab30f12cb54bbfa96d132372e11e85c79f1078f 100644 (file)
@@ -30,6 +30,7 @@
 #include "lib/util/blocking.h"
 #include "lib/util/util_net.h"
 #include "lib/util/samba_util.h"
+#include "lib/async_req/async_sock.h"
 
 static int tsocket_bsd_error_from_errno(int ret,
                                        int sys_errno,
index fa284a78f251035bacab2d5d823b3259bef9d048..f50d9c54483252d0f7764adb75aa01b8d57e362a 100644 (file)
@@ -13,7 +13,7 @@ def build(bld):
     bld.SAMBA_SUBSYSTEM(
         'LIBTSOCKET',
        source='tsocket.c tsocket_helpers.c tsocket_bsd.c',
-       public_deps='talloc tevent iov_buf socket-blocking',
+       public_deps='talloc tevent iov_buf socket-blocking LIBASYNC_REQ',
        public_headers='tsocket.h tsocket_internal.h'
     )
 
index 48c9552558b9d6df847244d11d8e3bf53212cced..5c19cc90003816d8b6de30b470dee60b3c8586b3 100644 (file)
@@ -27,8 +27,6 @@
 #include "system/network.h"
 #include "system/locale.h"
 #include "system/filesys.h"
-#include "system/select.h"
-#include "lib/util/select.h"
 #include "lib/util/util_net.h"
 
 #undef strcasecmp
@@ -1141,100 +1139,3 @@ bool samba_sockaddr_get_port(const struct samba_sockaddr *sa, uint16_t *port)
 #endif
        return false;
 }
-
-int samba_socket_poll_error(int fd)
-{
-       struct pollfd pfd = {
-               .fd = fd,
-#ifdef POLLRDHUP
-               .events = POLLRDHUP, /* POLLERR and POLLHUP are not needed */
-#endif
-       };
-       int ret;
-
-       errno = 0;
-       ret = sys_poll_intr(&pfd, 1, 0);
-       if (ret == 0) {
-               return 0;
-       }
-       if (ret != 1) {
-               return POLLNVAL;
-       }
-
-       if (pfd.revents & POLLERR) {
-               return POLLERR;
-       }
-       if (pfd.revents & POLLHUP) {
-               return POLLHUP;
-       }
-#ifdef POLLRDHUP
-       if (pfd.revents & POLLRDHUP) {
-               return POLLRDHUP;
-       }
-#endif
-
-       /* should never be reached! */
-       return POLLNVAL;
-}
-
-int samba_socket_sock_error(int fd)
-{
-       int ret, error = 0;
-       socklen_t len = sizeof(error);
-
-       /*
-        * if no data is available check if the socket is in error state. For
-        * dgram sockets it's the way to return ICMP error messages of
-        * connected sockets to the caller.
-        */
-       ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
-       if (ret == -1) {
-               return ret;
-       }
-       if (error != 0) {
-               errno = error;
-               return -1;
-       }
-       return 0;
-}
-
-int samba_socket_poll_or_sock_error(int fd)
-{
-       int ret;
-       int poll_error = 0;
-
-       poll_error = samba_socket_poll_error(fd);
-       if (poll_error == 0) {
-               return 0;
-       }
-
-#ifdef POLLRDHUP
-       if (poll_error == POLLRDHUP) {
-               errno = ECONNRESET;
-               return -1;
-       }
-#endif
-
-       if (poll_error == POLLHUP) {
-               errno = EPIPE;
-               return -1;
-       }
-
-       /*
-        * POLLERR and POLLNVAL fallback to
-        * getsockopt(fd, SOL_SOCKET, SO_ERROR)
-        * and force EPIPE as fallback.
-        */
-
-       errno = 0;
-       ret = samba_socket_sock_error(fd);
-       if (ret == 0) {
-               errno = EPIPE;
-       }
-
-       if (errno == 0) {
-               errno = EPIPE;
-       }
-
-       return -1;
-}
index 30399d8110556b191e62e1c5fa90d43293336e64..adfe749d4b76888a8cf443ddd6884c2b2eecfdd4 100644 (file)
@@ -128,18 +128,4 @@ bool sockaddr_storage_to_samba_sockaddr(
 bool samba_sockaddr_set_port(struct samba_sockaddr *sa, uint16_t port);
 bool samba_sockaddr_get_port(const struct samba_sockaddr *sa, uint16_t *port);
 
-/*
- * check for POLLERR or POLL*HUP
- */
-int samba_socket_poll_error(int fd);
-/*
- * getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len)
- */
-int samba_socket_sock_error(int fd);
-/*
- * check for POLL*HUP and fallback to
- * getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len)
- */
-int samba_socket_poll_or_sock_error(int fd);
-
 #endif /* _SAMBA_UTIL_NET_H_ */
index 3b692fdca87487d5192466f30865c7e52ab46d10..ff0e708e938d8ec5e41059da400c3d211a104096 100644 (file)
@@ -33,6 +33,7 @@
 #include "../lib/util/bitmap.h"
 #include "../librpc/gen_ndr/krb5pac.h"
 #include "lib/util/iov_buf.h"
+#include "lib/async_req/async_sock.h"
 #include "auth.h"
 #include "libcli/smb/smbXcli_base.h"
 #include "source3/lib/substitute.h"