diff options
author | NIIBE Yutaka <gniibe@fsij.org> | 2022-04-06 14:00:29 +0900 |
---|---|---|
committer | NIIBE Yutaka <gniibe@fsij.org> | 2022-04-06 14:00:29 +0900 |
commit | c93eb901e58d5b31294c2d452659b5150d95ec59 (patch) | |
tree | 508cbfa4daa82543aa06374059c9a755aa180ab9 /src | |
parent | 5b77d39672aca33416be95c685295e49ecb9f457 (diff) | |
download | libassuan-c93eb901e58d5b31294c2d452659b5150d95ec59.tar.gz |
w32: Store a flag if it's socket or not in Assuan CTX.
* src/assuan-defs.h (struct assuan_context_s): Add is_socket flag.
* src/assuan-socket.c (assuan_sock_init): Set the flag.
* src/assuan-socket-connect.c (_assuan_connect_finalize): Likewise.
* src/assuan-socket-server.c (assuan_init_socket_server): Likewise.
* src/system-w32.c (__assuan_close): Use the flag.
(is_socket): Remove.
(__assuan_read, __assuan_write): Use the flag.
--
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/assuan-defs.h | 2 | ||||
-rw-r--r-- | src/assuan-socket-connect.c | 1 | ||||
-rw-r--r-- | src/assuan-socket-server.c | 1 | ||||
-rw-r--r-- | src/assuan-socket.c | 1 | ||||
-rw-r--r-- | src/system-w32.c | 45 |
5 files changed, 16 insertions, 34 deletions
diff --git a/src/assuan-defs.h b/src/assuan-defs.h index fc2fdeb..1d1617f 100644 --- a/src/assuan-defs.h +++ b/src/assuan-defs.h @@ -96,6 +96,8 @@ struct assuan_context_s unsigned int convey_comments : 1; unsigned int no_logging : 1; unsigned int force_close : 1; + /* From here, it's internal flag. */ + unsigned int is_socket : 1; } flags; /* If set, this is called right before logging an I/O line. */ diff --git a/src/assuan-socket-connect.c b/src/assuan-socket-connect.c index 5423963..019a41c 100644 --- a/src/assuan-socket-connect.c +++ b/src/assuan-socket-connect.c @@ -113,6 +113,7 @@ _assuan_connect_finalize (assuan_context_t ctx, assuan_fd_t fd, ctx->inbound.fd = fd; ctx->outbound.fd = fd; ctx->max_accepts = -1; + ctx->flags.is_socket = 1; if (flags & ASSUAN_SOCKET_CONNECT_FDPASSING) _assuan_init_uds_io (ctx); diff --git a/src/assuan-socket-server.c b/src/assuan-socket-server.c index e1e0c60..8b20718 100644 --- a/src/assuan-socket-server.c +++ b/src/assuan-socket-server.c @@ -201,6 +201,7 @@ assuan_init_socket_server (assuan_context_t ctx, assuan_fd_t fd, TRACE_BEG2 (ctx, ASSUAN_LOG_CTX, "assuan_init_socket_server", ctx, "fd=0x%x, flags=0x%x", fd, flags); + ctx->flags.is_socket = 1; rc = _assuan_register_std_commands (ctx); if (rc) return TRACE_ERR (rc); diff --git a/src/assuan-socket.c b/src/assuan-socket.c index e841cda..3fefc39 100644 --- a/src/assuan-socket.c +++ b/src/assuan-socket.c @@ -1425,6 +1425,7 @@ assuan_sock_init () #endif err = assuan_new (&sock_ctx); + sock_ctx->flags.is_socket = 1; #ifdef HAVE_W32_SYSTEM if (! err) diff --git a/src/system-w32.c b/src/system-w32.c index c49d485..cfc1d61 100644 --- a/src/system-w32.c +++ b/src/system-w32.c @@ -147,10 +147,15 @@ __assuan_pipe (assuan_context_t ctx, assuan_fd_t fd[2], int inherit_idx) int __assuan_close (assuan_context_t ctx, assuan_fd_t fd) { - int rc = closesocket (HANDLE2SOCKET(fd)); - if (rc) - gpg_err_set_errno ( _assuan_sock_wsa2errno (WSAGetLastError ()) ); - if (rc && WSAGetLastError () == WSAENOTSOCK) + int rc; + + if (ctx->flags.is_socket) + { + rc = closesocket (HANDLE2SOCKET(fd)); + if (rc) + gpg_err_set_errno ( _assuan_sock_wsa2errno (WSAGetLastError ()) ); + } + else { rc = CloseHandle (fd); if (rc) @@ -162,41 +167,13 @@ __assuan_close (assuan_context_t ctx, assuan_fd_t fd) -/* Return true if HD refers to a socket. */ -static int -is_socket (HANDLE hd) -{ - /* We need to figure out whether we are working on a socket or on a - handle. A trivial way would be to check for the return code of - recv and see if it is WSAENOTSOCK. However the recv may block - after the server process died and thus the destroy_reader will - hang. Another option is to use getsockopt to test whether it is - a socket. The bug here is that once a socket with a certain - values has been opened, closed and later a CreatePipe returned - the same value (i.e. handle), getsockopt still believes it is a - socket. What we do now is to use a combination of GetFileType - and GetNamedPipeInfo. The specs say that the latter may be used - on anonymous pipes as well. Note that there are claims that - since winsocket version 2 ReadFile may be used on a socket but - only if it is supported by the service provider. Tests on a - stock XP using a local TCP socket show that it does not work. */ - DWORD dummyflags, dummyoutsize, dummyinsize, dummyinst; - if (GetFileType (hd) == FILE_TYPE_PIPE - && !GetNamedPipeInfo (hd, &dummyflags, &dummyoutsize, - &dummyinsize, &dummyinst)) - return 1; /* Function failed; thus we assume it is a socket. */ - else - return 0; /* Success; this is not a socket. */ -} - - ssize_t __assuan_read (assuan_context_t ctx, assuan_fd_t fd, void *buffer, size_t size) { int res; int ec = 0; - if (is_socket (fd)) + if (ctx->flags.is_socket) { int tries = 3; @@ -265,7 +242,7 @@ __assuan_write (assuan_context_t ctx, assuan_fd_t fd, const void *buffer, int res; int ec = 0; - if (is_socket (fd)) + if (ctx->flags.is_socket) { int tries = 3; |