diff options
author | Werner Koch <[email protected]> | 2007-07-12 10:06:26 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2007-07-12 10:06:26 +0000 |
commit | 35fedf1acdf8aabea7a897dad06672cce10b6a3d (patch) | |
tree | 899f9afec6ecbd4eb8fb20b8669448730cac2b7c | |
parent | 2007-07-08 Marcus Brinkmann <[email protected]> (diff) | |
download | libassuan-35fedf1acdf8aabea7a897dad06672cce10b6a3d.tar.gz libassuan-35fedf1acdf8aabea7a897dad06672cce10b6a3d.zip |
Introduced new type for better support of W32 HADNLES vs. file descriptors.
-rw-r--r-- | NEWS | 10 | ||||
-rw-r--r-- | TODO | 3 | ||||
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | doc/ChangeLog | 7 | ||||
-rw-r--r-- | doc/assuan.texi | 9 | ||||
-rw-r--r-- | src/ChangeLog | 16 | ||||
-rw-r--r-- | src/assuan-buffer.c | 24 | ||||
-rw-r--r-- | src/assuan-defs.h | 37 | ||||
-rw-r--r-- | src/assuan-handler.c | 26 | ||||
-rw-r--r-- | src/assuan-io-pth.c | 6 | ||||
-rw-r--r-- | src/assuan-io.c | 8 | ||||
-rw-r--r-- | src/assuan-listen.c | 16 | ||||
-rw-r--r-- | src/assuan-pipe-connect.c | 73 | ||||
-rw-r--r-- | src/assuan-pipe-server.c | 19 | ||||
-rw-r--r-- | src/assuan-socket-connect.c | 12 | ||||
-rw-r--r-- | src/assuan-socket-server.c | 33 | ||||
-rw-r--r-- | src/assuan-socket.c | 28 | ||||
-rw-r--r-- | src/assuan-uds.c | 8 | ||||
-rw-r--r-- | src/assuan.h | 34 |
19 files changed, 225 insertions, 148 deletions
@@ -1,4 +1,12 @@ -Noteworthy changes in version 1.0.2 (2007-07-05) +Noteworthy changes in version 1.0.3 +------------------------------------------------ + + * New type assuan_fd_t and constant ASSUAN_INVALID_FD for better W32 + support. it does not change teh semantics under Unix. Under W32 + all file descriptors are now guaranteed to be system handles. + + +Noteworthy changes in version 1.0.2 (2007-07-05 ------------------------------------------------ * Changed license to LGPLv3. @@ -11,3 +11,6 @@ * XOPEN_SOURCE and snprintf See Peter O'Gorman's mail. + +* W32 +** Check what kind of fd we use with inbound.fd etc.
\ No newline at end of file diff --git a/configure.ac b/configure.ac index dc48ba3..0400f2d 100644 --- a/configure.ac +++ b/configure.ac @@ -24,8 +24,8 @@ min_automake_version="1.10" # Remember to change the version number immediately *after* a release. # Set my_issvn to "yes" for non-released code. Remember to run an # "svn up" and "autogen.sh" right before creating a distribution. -m4_define([my_version], [1.0.2]) -m4_define([my_issvn], [no]) +m4_define([my_version], [1.0.3]) +m4_define([my_issvn], [yes]) m4_define([svn_revision], m4_esyscmd([echo -n $( (svn info 2>/dev/null \ || echo 'Revision: 0')|sed -n '/^Revision:/ {s/[^0-9]//gp;q;}')])) diff --git a/doc/ChangeLog b/doc/ChangeLog index 3804a8d..3f76d12 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,8 @@ +2007-07-12 Werner Koch <[email protected]> + + * assuan.texi (Utilities): Document that under W32 we return a + system handle. + 2007-07-05 Werner Koch <[email protected]> * lgpl.texi: New. Taken from COPYING.LIB and reformatted. @@ -36,7 +41,7 @@ * assuan.texi: Imported from newpg. - Copyright 2003 Free Software Foundation, Inc. + Copyright 2003, 2007 Free Software Foundation, Inc. This file is free software; as a special exception the author gives unlimited permission to copy and/or distribute it, with or without diff --git a/doc/assuan.texi b/doc/assuan.texi index 2531d8c..f22a7f9 100644 --- a/doc/assuan.texi +++ b/doc/assuan.texi @@ -1207,6 +1207,12 @@ If @var{line} contains @code{FD=@var{n}}, it returns @var{n} in just @code{FD} it returns a file descriptor at @var{rfd}; this file descriptor needs to have been sent by the client right before using @code{assuan_sendfd}. + +On W32 systems the returned file descriptor is a system handle and not a +libc low level I/O file descriptor. Thus applications need to use +@code{_open_osfhandle} before they can pass this descriptor to standard +functions like @code{fdopen} or @code{dup}. + @end deftypefun @deftypefun int assuan_get_input_fd (@w{assuan_context_t @var{ctx}}) @@ -1331,6 +1337,9 @@ writing. @var{fdarray} is an array of integers provided by the caller; On success the number of active descriptors are returned. These active descriptors are then stored in @var{fdarray}. On error @code{-1} is returned; the most likely reason for this is a too small @var{fdarray}. + +Note that on W32 systems the returned file descriptor is a system handle +and not a libc low level I/O file descriptor. @end deftypefun diff --git a/src/ChangeLog b/src/ChangeLog index 9911d55..a9616e6 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,19 @@ +2007-07-12 Werner Koch <[email protected]> + + * assuan.h (assuan_fd_t): New. + (ASSUAN_INVALID_FD): New. Use it everywhere. + * assuan-defs.h (SOCKET2HANDLE, HANDLE2SOCKET) [W32]: New. Use + them to cast descriptors for socket fucntions. + * assuan-pipe-connect.c (fd_to_handle, handle_to_fd): Remove + definition and all uses. + (pid_to_handle, handle_to_pid): Remove as they are ununsed. + * assuan-io.c (_assuan_simple_write, _assuan_simple_read) [W32]: + Make use of HANDLE2SOCKET. + * assuan-socket.c (_assuan_close) [W32]: Use CloseHandle and not + close. + * assuan-handler.c (assuan_get_active_fds) [W32]: Use + _get_osfhandle for the data fp. + 2007-07-08 Marcus Brinkmann <[email protected]> * assuan-defs.h (struct assuan_context_s): Have full peercred diff --git a/src/assuan-buffer.c b/src/assuan-buffer.c index 05cf4df..b05f2d2 100644 --- a/src/assuan-buffer.c +++ b/src/assuan-buffer.c @@ -135,7 +135,7 @@ _assuan_read_line (assuan_context_t ctx) if (ctx->log_fp) fprintf (ctx->log_fp, "%s[%u.%d] DBG: <- [Error: %s]\n", assuan_get_assuan_log_prefix (), - (unsigned int)getpid (), ctx->inbound.fd, + (unsigned int)getpid (), (int)ctx->inbound.fd, strerror (errno)); return _assuan_error (ASSUAN_Read_Error); } @@ -145,7 +145,7 @@ _assuan_read_line (assuan_context_t ctx) if (ctx->log_fp) fprintf (ctx->log_fp, "%s[%u.%d] DBG: <- [EOF]\n", assuan_get_assuan_log_prefix (), - (unsigned int)getpid (), ctx->inbound.fd); + (unsigned int)getpid (), (int)ctx->inbound.fd); return _assuan_error (-1); } @@ -189,7 +189,7 @@ _assuan_read_line (assuan_context_t ctx) { fprintf (ctx->log_fp, "%s[%u.%d] DBG: <- ", assuan_get_assuan_log_prefix (), - (unsigned int)getpid (), ctx->inbound.fd); + (unsigned int)getpid (), (int)ctx->inbound.fd); if (ctx->confidential) fputs ("[Confidential data not shown]", ctx->log_fp); else @@ -205,7 +205,7 @@ _assuan_read_line (assuan_context_t ctx) if (ctx->log_fp) fprintf (ctx->log_fp, "%s[%u.%d] DBG: <- [Invalid line]\n", assuan_get_assuan_log_prefix (), - (unsigned int)getpid (), ctx->inbound.fd); + (unsigned int)getpid (), (int)ctx->inbound.fd); *line = 0; ctx->inbound.linelen = 0; return _assuan_error (ctx->inbound.eof @@ -263,7 +263,7 @@ _assuan_write_line (assuan_context_t ctx, const char *prefix, fprintf (ctx->log_fp, "%s[%u.%d] DBG: -> " "[supplied line too long -truncated]\n", assuan_get_assuan_log_prefix (), - (unsigned int)getpid (), ctx->inbound.fd); + (unsigned int)getpid (), (int)ctx->inbound.fd); if (prefixlen > 5) prefixlen = 5; if (len > ASSUAN_LINELENGTH - prefixlen - 2) @@ -279,7 +279,7 @@ _assuan_write_line (assuan_context_t ctx, const char *prefix, { fprintf (ctx->log_fp, "%s[%u.%d] DBG: -> ", assuan_get_assuan_log_prefix (), - (unsigned int)getpid (), ctx->inbound.fd); + (unsigned int)getpid (), (int)ctx->inbound.fd); if (ctx->confidential) fputs ("[Confidential data not shown]", ctx->log_fp); else @@ -331,7 +331,7 @@ assuan_write_line (assuan_context_t ctx, const char *line) fprintf (ctx->log_fp, "%s[%u.%d] DBG: -> " "[supplied line contained a LF - truncated]\n", assuan_get_assuan_log_prefix (), - (unsigned int)getpid (), ctx->inbound.fd); + (unsigned int)getpid (), (int)ctx->inbound.fd); return _assuan_write_line (ctx, NULL, line, len); } @@ -396,7 +396,7 @@ _assuan_cookie_write_data (void *cookie, const char *buffer, size_t orig_size) { fprintf (ctx->log_fp, "%s[%u.%d] DBG: -> ", assuan_get_assuan_log_prefix (), - (unsigned int)getpid (), ctx->inbound.fd); + (unsigned int)getpid (), (int)ctx->inbound.fd); if (ctx->confidential) fputs ("[Confidential data not shown]", ctx->log_fp); @@ -452,7 +452,7 @@ _assuan_cookie_write_flush (void *cookie) { fprintf (ctx->log_fp, "%s[%u.%d] DBG: -> ", assuan_get_assuan_log_prefix (), - (unsigned int)getpid (), ctx->inbound.fd); + (unsigned int)getpid (), (int)ctx->inbound.fd); if (ctx->confidential) fputs ("[Confidential data not shown]", ctx->log_fp); else @@ -519,11 +519,11 @@ assuan_send_data (assuan_context_t ctx, const void *buffer, size_t length) } assuan_error_t -assuan_sendfd (assuan_context_t ctx, int fd) +assuan_sendfd (assuan_context_t ctx, assuan_fd_t fd) { /* It is explicitly allowed to use (NULL, -1) as a runtime test to check whether descriptor passing is available. */ - if (!ctx && fd == -1) + if (!ctx && fd == ASSUAN_INVALID_FD) #ifdef USE_DESCRIPTOR_PASSING return 0; #else @@ -538,7 +538,7 @@ assuan_sendfd (assuan_context_t ctx, int fd) } assuan_error_t -assuan_receivefd (assuan_context_t ctx, int *fd) +assuan_receivefd (assuan_context_t ctx, assuan_fd_t *fd) { if (! ctx->io->receivefd) return set_error (ctx, Not_Implemented, diff --git a/src/assuan-defs.h b/src/assuan-defs.h index b1d9f3e..d1037c8 100644 --- a/src/assuan-defs.h +++ b/src/assuan-defs.h @@ -80,9 +80,9 @@ struct assuan_io /* Routine to write to output_fd. */ ssize_t (*writefnc) (assuan_context_t, const void *, size_t); /* Send a file descriptor. */ - assuan_error_t (*sendfd) (assuan_context_t, int); + assuan_error_t (*sendfd) (assuan_context_t, assuan_fd_t); /* Receive a file descriptor. */ - assuan_error_t (*receivefd) (assuan_context_t, int *); + assuan_error_t (*receivefd) (assuan_context_t, assuan_fd_t *); }; @@ -111,7 +111,7 @@ struct assuan_context_s FILE *log_fp; struct { - int fd; + assuan_fd_t fd; int eof; char line[LINELENGTH]; int linelen; /* w/o CR, LF - might not be the same as @@ -125,7 +125,7 @@ struct assuan_context_s } inbound; struct { - int fd; + assuan_fd_t fd; struct { FILE *fp; char line[LINELENGTH]; @@ -137,8 +137,9 @@ struct assuan_context_s int pipe_mode; /* We are in pipe mode, i.e. we can handle just one connection and must terminate then. */ pid_t pid; /* The pid of the peer. */ - int listen_fd; /* The fd we are listening on (used by socket servers) */ - int connected_fd; /* helper */ + assuan_fd_t listen_fd; /* The fd we are listening on (used by + socket servers) */ + assuan_fd_t connected_fd; /* helper */ struct { int valid; /* Whether this structure has valid information. */ @@ -162,7 +163,7 @@ struct assuan_context_s int bufferoffset; /* Offset of start of buffer. */ int buffersize; /* Bytes buffered. */ - int pendingfds[5]; /* Array to save received descriptors. */ + assuan_fd_t pendingfds[5]; /* Array to save received descriptors. */ int pendingfdscount; /* Number of received descriptors. */ } uds; @@ -195,8 +196,8 @@ struct assuan_context_s const char *line, size_t linelen); - int input_fd; /* set by INPUT command */ - int output_fd; /* set by OUTPUT command */ + assuan_fd_t input_fd; /* Set by the INPUT command. */ + assuan_fd_t output_fd; /* Set by the OUTPUT command. */ /* io routines. */ struct assuan_io *io; @@ -293,10 +294,11 @@ ssize_t _assuan_simple_recvmsg (assuan_context_t ctx, struct msghdr *msg); #endif /*-- assuan-socket.c --*/ -int _assuan_close (int fd); -int _assuan_sock_new (int domain, int type, int proto); -int _assuan_sock_bind (int sockfd, struct sockaddr *addr, int addrlen); -int _assuan_sock_connect (int sockfd, struct sockaddr *addr, int addrlen); +int _assuan_close (assuan_fd_t fd); +assuan_fd_t _assuan_sock_new (int domain, int type, int proto); +int _assuan_sock_bind (assuan_fd_t sockfd, struct sockaddr *addr, int addrlen); +int _assuan_sock_connect (assuan_fd_t sockfd, + struct sockaddr *addr, int addrlen); #ifdef HAVE_FOPENCOOKIE /* We have to implement funopen in terms of glibc's fopencookie. */ @@ -329,4 +331,13 @@ int putc_unlocked (int c, FILE *stream); #define DIMof(type,member) DIM(((type *)0)->member) +#if HAVE_W32_SYSTEM +#define SOCKET2HANDLE(s) ((void *)(s)) +#define HANDLE2SOCKET(h) ((unsigned int)(h)) +#else +#define SOCKET2HANDLE(s) (s) +#define HANDLE2SOCKET(h) (h) +#endif + + #endif /*ASSUAN_DEFS_H*/ diff --git a/src/assuan-handler.c b/src/assuan-handler.c index a4d549f..c940c36 100644 --- a/src/assuan-handler.c +++ b/src/assuan-handler.c @@ -135,8 +135,9 @@ std_handler_end (assuan_context_t ctx, char *line) return set_error (ctx, Not_Implemented, NULL); } + assuan_error_t -assuan_command_parse_fd (assuan_context_t ctx, char *line, int *rfd) +assuan_command_parse_fd (assuan_context_t ctx, char *line, assuan_fd_t *rfd) { char *endp; @@ -149,7 +150,13 @@ assuan_command_parse_fd (assuan_context_t ctx, char *line, int *rfd) line ++; if (!digitp (*line)) return set_error (ctx, Syntax_Error, "number required"); +#ifdef HAVE_W32_SYSTEM + /* Fixme: For a W32/64bit system we will need to change the cast + and the conversion fucntion. */ + *rfd = (void*)strtoul (line, &endp, 10); +#else *rfd = strtoul (line, &endp, 10); +#endif /* Remove that argument so that a notify handler won't see it. */ memset (line, ' ', endp? (endp-line):strlen(line)); @@ -164,11 +171,13 @@ assuan_command_parse_fd (assuan_context_t ctx, char *line, int *rfd) return assuan_receivefd (ctx, rfd); } + /* Format is INPUT FD=<n> */ static int std_handler_input (assuan_context_t ctx, char *line) { - int rc, fd; + int rc; + assuan_fd_t fd; rc = assuan_command_parse_fd (ctx, line, &fd); if (rc) @@ -183,7 +192,8 @@ std_handler_input (assuan_context_t ctx, char *line) static int std_handler_output (assuan_context_t ctx, char *line) { - int rc, fd; + int rc; + assuan_fd_t fd; rc = assuan_command_parse_fd (ctx, line, &fd); if (rc) @@ -644,7 +654,7 @@ assuan_process_next (assuan_context_t ctx) **/ int assuan_get_active_fds (assuan_context_t ctx, int what, - int *fdarray, int fdarraysize) + assuan_fd_t *fdarray, int fdarraysize) { int n = 0; @@ -653,15 +663,19 @@ assuan_get_active_fds (assuan_context_t ctx, int what, if (!what) { - if (ctx->inbound.fd != -1) + if (ctx->inbound.fd != ASSUAN_INVALID_FD) fdarray[n++] = ctx->inbound.fd; } else { - if (ctx->outbound.fd != -1) + if (ctx->outbound.fd != ASSUAN_INVALID_FD) fdarray[n++] = ctx->outbound.fd; if (ctx->outbound.data.fp) +#ifdef HAVE_W32_SYSTEM + fdarray[n++] = (void*)_get_osfhandle (fileno (ctx->outbound.data.fp)); +#else fdarray[n++] = fileno (ctx->outbound.data.fp); +#endif } return n; diff --git a/src/assuan-io-pth.c b/src/assuan-io-pth.c index 67a0e9d..ff6f10d 100644 --- a/src/assuan-io-pth.c +++ b/src/assuan-io-pth.c @@ -54,13 +54,15 @@ _assuan_waitpid (pid_t pid, int *status, int options) ssize_t _assuan_simple_read (assuan_context_t ctx, void *buffer, size_t size) { - return pth_read (ctx->inbound.fd, buffer, size); + /* Fixme: For W32 we ehsould better not cast the HANDLE type to int. + However, this requires changes in w32pth too. */ + return pth_read ((int)ctx->inbound.fd, buffer, size); } ssize_t _assuan_simple_write (assuan_context_t ctx, const void *buffer, size_t size) { - return pth_write (ctx->outbound.fd, buffer, size); + return pth_write ((int)ctx->outbound.fd, buffer, size); } #ifdef HAVE_W32_SYSTEM diff --git a/src/assuan-io.c b/src/assuan-io.c index c0d33ee..7ce43cb 100644 --- a/src/assuan-io.c +++ b/src/assuan-io.c @@ -55,12 +55,12 @@ _assuan_simple_read (assuan_context_t ctx, void *buffer, size_t size) read if recv detects that it is not a network socket. */ int n; - n = recv (ctx->inbound.fd, buffer, size, 0); + n = recv (HANDLE2SOCKET(ctx->inbound.fd), buffer, size, 0); if (n == -1 && WSAGetLastError () == WSAENOTSOCK) { DWORD nread = 0; - n = ReadFile ((HANDLE)ctx->inbound.fd, buffer, size, &nread, NULL); + n = ReadFile (ctx->inbound.fd, buffer, size, &nread, NULL); if (!n) { errno = EIO; /* FIXME: We should have a proper mapping. */ @@ -84,12 +84,12 @@ _assuan_simple_write (assuan_context_t ctx, const void *buffer, size_t size) write if send detects that it is not a network socket. */ int n; - n = send (ctx->outbound.fd, buffer, size, 0); + n = send (HANDLE2SOCKET(ctx->outbound.fd), buffer, size, 0); if (n == -1 && WSAGetLastError () == WSAENOTSOCK) { DWORD nwrite; - n = WriteFile ((HANDLE)ctx->outbound.fd, buffer, size, &nwrite, NULL); + n = WriteFile (ctx->outbound.fd, buffer, size, &nwrite, NULL); if (!n) { errno = EIO; /* FIXME: We should have a proper mapping. */ diff --git a/src/assuan-listen.c b/src/assuan-listen.c index eb6d1c8..cc86395 100644 --- a/src/assuan-listen.c +++ b/src/assuan-listen.c @@ -114,17 +114,17 @@ assuan_accept (assuan_context_t ctx) -int +assuan_fd_t assuan_get_input_fd (assuan_context_t ctx) { - return ctx? ctx->input_fd : -1; + return ctx? ctx->input_fd : ASSUAN_INVALID_FD; } -int +assuan_fd_t assuan_get_output_fd (assuan_context_t ctx) { - return ctx? ctx->output_fd : -1; + return ctx? ctx->output_fd : ASSUAN_INVALID_FD; } @@ -133,10 +133,10 @@ assuan_get_output_fd (assuan_context_t ctx) assuan_error_t assuan_close_input_fd (assuan_context_t ctx) { - if (!ctx || ctx->input_fd == -1) + if (!ctx || ctx->input_fd == ASSUAN_INVALID_FD) return _assuan_error (ASSUAN_Invalid_Value); _assuan_close (ctx->input_fd); - ctx->input_fd = -1; + ctx->input_fd = ASSUAN_INVALID_FD; return 0; } @@ -145,11 +145,11 @@ assuan_close_input_fd (assuan_context_t ctx) assuan_error_t assuan_close_output_fd (assuan_context_t ctx) { - if (!ctx || ctx->output_fd == -1) + if (!ctx || ctx->output_fd == ASSUAN_INVALID_FD) return _assuan_error (ASSUAN_Invalid_Value); _assuan_close (ctx->output_fd); - ctx->output_fd = -1; + ctx->output_fd = ASSUAN_INVALID_FD; return 0; } diff --git a/src/assuan-pipe-connect.c b/src/assuan-pipe-connect.c index 8eabdf0..791fcac 100644 --- a/src/assuan-pipe-connect.c +++ b/src/assuan-pipe-connect.c @@ -56,17 +56,6 @@ #define MAX_OPEN_FDS 20 #endif -#ifdef HAVE_W32_SYSTEM -/* We assume that a HANDLE can be represented by an int which should - be true for all i386 systems (HANDLE is defined as void *) and - these are the only systems for which Windows is available. Further - we assume that -1 denotes an invalid handle. */ -#define fd_to_handle(a) ((HANDLE)(a)) -#define handle_to_fd(a) ((int)(a)) -#define pid_to_handle(a) ((HANDLE)(a)) -#define handle_to_pid(a) ((int)(a)) -#endif /*HAVE_W32_SYSTEM*/ - /* This should be called to make sure that SIGPIPE gets ignored. */ static void @@ -120,25 +109,25 @@ writen (int fd, const char *buffer, size_t length) static int do_finish (assuan_context_t ctx) { - if (ctx->inbound.fd != -1) + if (ctx->inbound.fd != ASSUAN_INVALID_FD) { _assuan_close (ctx->inbound.fd); if (ctx->inbound.fd == ctx->outbound.fd) - ctx->outbound.fd = -1; - ctx->inbound.fd = -1; + ctx->outbound.fd = ASSUAN_INVALID_FD; + ctx->inbound.fd = ASSUAN_INVALID_FD; } - if (ctx->outbound.fd != -1) + if (ctx->outbound.fd != ASSUAN_INVALID_FD) { _assuan_close (ctx->outbound.fd); - ctx->outbound.fd = -1; + ctx->outbound.fd = ASSUAN_INVALID_FD; } - if (ctx->pid != -1 && ctx->pid) + if (ctx->pid != (pid_t)(-1) && ctx->pid) { #ifndef HAVE_W32_SYSTEM #ifndef _ASSUAN_USE_DOUBLE_FORK if (!ctx->flags.no_waitpid) _assuan_waitpid (ctx->pid, NULL, 0); - ctx->pid = -1; + ctx->pid =(pid_t)(-1); #endif #endif /*!HAVE_W32_SYSTEM*/ } @@ -604,7 +593,7 @@ build_w32_commandline (const char * const *argv, char **cmdline) #ifdef HAVE_W32_SYSTEM /* Create pipe where one end end is inheritable. */ static int -create_inheritable_pipe (int filedes[2], int for_write) +create_inheritable_pipe (assuan_fd_t filedes[2], int for_write) { HANDLE r, w, h; SECURITY_ATTRIBUTES sec_attr; @@ -639,8 +628,8 @@ create_inheritable_pipe (int filedes[2], int for_write) w = h; } - filedes[0] = handle_to_fd (r); - filedes[1] = handle_to_fd (w); + filedes[0] = r; + filedes[1] = w; return 0; } #endif /*HAVE_W32_SYSTEM*/ @@ -657,8 +646,8 @@ pipe_connect_w32 (assuan_context_t *ctx, void *atforkvalue) { assuan_error_t err; - int rp[2]; - int wp[2]; + assuan_fd_t rp[2]; + assuan_fd_t wp[2]; char mypidstr[50]; char *cmdline; SECURITY_ATTRIBUTES sec_attr; @@ -693,8 +682,8 @@ pipe_connect_w32 (assuan_context_t *ctx, if (create_inheritable_pipe (wp, 1)) { - CloseHandle (fd_to_handle (rp[0])); - CloseHandle (fd_to_handle (rp[1])); + CloseHandle (rp[0]); + CloseHandle (rp[1]); xfree (cmdline); return _assuan_error (ASSUAN_General_Error); } @@ -703,10 +692,10 @@ pipe_connect_w32 (assuan_context_t *ctx, err = _assuan_new_context (ctx); if (err) { - CloseHandle (fd_to_handle (rp[0])); - CloseHandle (fd_to_handle (rp[1])); - CloseHandle (fd_to_handle (wp[0])); - CloseHandle (fd_to_handle (wp[1])); + CloseHandle (rp[0]); + CloseHandle (rp[1]); + CloseHandle (wp[0]); + CloseHandle (wp[1]); xfree (cmdline); return _assuan_error (ASSUAN_General_Error); } @@ -733,8 +722,8 @@ pipe_connect_w32 (assuan_context_t *ctx, memset (&si, 0, sizeof si); si.cb = sizeof (si); si.dwFlags = STARTF_USESTDHANDLES; - si.hStdInput = fd_to_handle (wp[0]); - si.hStdOutput = fd_to_handle (rp[1]); + si.hStdInput = wp[0]; + si.hStdOutput = rp[1]; /* Dup stderr to /dev/null unless it is in the list of FDs to be passed to the child. */ @@ -753,10 +742,10 @@ pipe_connect_w32 (assuan_context_t *ctx, if (nullfd == INVALID_HANDLE_VALUE) { _assuan_log_printf ("can't open `nul': %s\n", w32_strerror (-1)); - CloseHandle (fd_to_handle (rp[0])); - CloseHandle (fd_to_handle (rp[1])); - CloseHandle (fd_to_handle (wp[0])); - CloseHandle (fd_to_handle (wp[1])); + CloseHandle (rp[0]); + CloseHandle (rp[1]); + CloseHandle (wp[0]); + CloseHandle (wp[1]); xfree (cmdline); _assuan_release_context (*ctx); return -1; @@ -764,7 +753,7 @@ pipe_connect_w32 (assuan_context_t *ctx, si.hStdError = nullfd; } else - si.hStdError = fd_to_handle (_get_osfhandle (fd)); + si.hStdError = (void*)_get_osfhandle (fd); /* Note: We inherit all handles flagged as inheritable. This seems @@ -787,10 +776,10 @@ pipe_connect_w32 (assuan_context_t *ctx, )) { _assuan_log_printf ("CreateProcess failed: %s\n", w32_strerror (-1)); - CloseHandle (fd_to_handle (rp[0])); - CloseHandle (fd_to_handle (rp[1])); - CloseHandle (fd_to_handle (wp[0])); - CloseHandle (fd_to_handle (wp[1])); + CloseHandle (rp[0]); + CloseHandle (rp[1]); + CloseHandle (wp[0]); + CloseHandle (wp[1]); if (nullfd != INVALID_HANDLE_VALUE) CloseHandle (nullfd); xfree (cmdline); @@ -805,8 +794,8 @@ pipe_connect_w32 (assuan_context_t *ctx, nullfd = INVALID_HANDLE_VALUE; } - CloseHandle (fd_to_handle (rp[1])); - CloseHandle (fd_to_handle (wp[0])); + CloseHandle (rp[1]); + CloseHandle (wp[0]); /* _assuan_log_printf ("CreateProcess ready: hProcess=%p hThread=%p" */ /* " dwProcessID=%d dwThreadId=%d\n", */ diff --git a/src/assuan-pipe-server.c b/src/assuan-pipe-server.c index 0ced662..96bdfcc 100644 --- a/src/assuan-pipe-server.c +++ b/src/assuan-pipe-server.c @@ -67,14 +67,14 @@ _assuan_new_context (assuan_context_t *r_ctx) ctx = xtrycalloc (1, sizeof *ctx); if (!ctx) return _assuan_error (ASSUAN_Out_Of_Core); - ctx->input_fd = -1; - ctx->output_fd = -1; + ctx->input_fd = ASSUAN_INVALID_FD; + ctx->output_fd = ASSUAN_INVALID_FD; - ctx->inbound.fd = -1; - ctx->outbound.fd = -1; + ctx->inbound.fd = ASSUAN_INVALID_FD; + ctx->outbound.fd = ASSUAN_INVALID_FD; ctx->io = &io; - ctx->listen_fd = -1; + ctx->listen_fd = ASSUAN_INVALID_FD; /* Use the pipe server handler as a default. */ ctx->deinit_handler = deinit_pipe_server; ctx->accept_handler = accept_connection; @@ -119,11 +119,11 @@ assuan_init_pipe_server (assuan_context_t *r_ctx, int filedes[2]) #ifdef HAVE_W32_SYSTEM /* MS Windows has so many different types of handle that one needs to tranlsate them at many place forth and back. Also - make sure that the fiel descriptos are in binary mode. */ + make sure that the file descriptors are in binary mode. */ setmode (filedes[0], O_BINARY); setmode (filedes[1], O_BINARY); - ctx->inbound.fd = _get_osfhandle (filedes[0]); - ctx->outbound.fd = _get_osfhandle (filedes[1]); + ctx->inbound.fd = (void*)_get_osfhandle (filedes[0]); + ctx->outbound.fd = (void*)_get_osfhandle (filedes[1]); #else s = getenv ("_assuan_connection_fd"); if (s && *s && is_valid_socket (s) ) @@ -135,7 +135,8 @@ assuan_init_pipe_server (assuan_context_t *r_ctx, int filedes[2]) _assuan_init_uds_io (ctx); ctx->deinit_handler = _assuan_uds_deinit; } - else if (filedes && filedes[0] != -1 && filedes[1] != -1 ) + else if (filedes && filedes[0] != ASSUAN_INVALID_FD + && filedes[1] != ASSUAN_INVALID_FD ) { /* Standard pipe server. */ ctx->inbound.fd = filedes[0]; diff --git a/src/assuan-socket-connect.c b/src/assuan-socket-connect.c index 1151215..c77586d 100644 --- a/src/assuan-socket-connect.c +++ b/src/assuan-socket-connect.c @@ -55,12 +55,12 @@ static int do_finish (assuan_context_t ctx) { - if (ctx->inbound.fd != -1) + if (ctx->inbound.fd != ASSUAN_INVALID_FD) { _assuan_close (ctx->inbound.fd); } - ctx->inbound.fd = -1; - ctx->outbound.fd = -1; + ctx->inbound.fd = ASSUAN_INVALID_FD; + ctx->outbound.fd = ASSUAN_INVALID_FD; return 0; } @@ -96,7 +96,7 @@ assuan_socket_connect_ext (assuan_context_t *r_ctx, assuan_error_t err; assuan_context_t ctx; - int fd; + assuan_fd_t fd; struct sockaddr_un srvr_addr; size_t len; const char *s; @@ -124,7 +124,7 @@ assuan_socket_connect_ext (assuan_context_t *r_ctx, ctx->finish_handler = do_finish; fd = _assuan_sock_new (PF_LOCAL, SOCK_STREAM, 0); - if (fd == -1) + if (fd == ASSUAN_INVALID_FD) { _assuan_log_printf ("can't create socket: %s\n", strerror (errno)); _assuan_release_context (ctx); @@ -138,7 +138,7 @@ assuan_socket_connect_ext (assuan_context_t *r_ctx, len = SUN_LEN (&srvr_addr); - if (_assuan_sock_connect (fd, (struct sockaddr *) &srvr_addr, len) == -1) + if ( _assuan_sock_connect (fd, (struct sockaddr *) &srvr_addr, len) == -1 ) { _assuan_log_printf ("can't connect to `%s': %s\n", name, strerror (errno)); diff --git a/src/assuan-socket-server.c b/src/assuan-socket-server.c index abd4e56..97cddec 100644 --- a/src/assuan-socket-server.c +++ b/src/assuan-socket-server.c @@ -45,7 +45,7 @@ static struct assuan_io io = { _assuan_simple_read, static int accept_connection_bottom (assuan_context_t ctx) { - int fd = ctx->connected_fd; + assuan_fd_t fd = ctx->connected_fd; ctx->peercred.valid = 0; #ifdef HAVE_SO_PEERCRED @@ -87,12 +87,13 @@ accept_connection_bottom (assuan_context_t ctx) static int accept_connection (assuan_context_t ctx) { - int fd; + assuan_fd_t fd; struct sockaddr_un clnt_addr; socklen_t len = sizeof clnt_addr; - fd = accept (ctx->listen_fd, (struct sockaddr*)&clnt_addr, &len ); - if (fd == -1) + fd = SOCKET2HANDLE(accept (HANDLE2SOCKET(ctx->listen_fd), + (struct sockaddr*)&clnt_addr, &len )); + if (fd == ASSUAN_INVALID_FD) { ctx->os_errno = errno; return _assuan_error (ASSUAN_Accept_Failed); @@ -105,12 +106,12 @@ accept_connection (assuan_context_t ctx) static int finish_connection (assuan_context_t ctx) { - if (ctx->inbound.fd != -1) + if (ctx->inbound.fd != ASSUAN_INVALID_FD) { _assuan_close (ctx->inbound.fd); } - ctx->inbound.fd = -1; - ctx->outbound.fd = -1; + ctx->inbound.fd = ASSUAN_INVALID_FD; + ctx->outbound.fd = ASSUAN_INVALID_FD; return 0; } @@ -124,7 +125,7 @@ deinit_socket_server (assuan_context_t ctx) /* Initialize a server for the socket LISTEN_FD which has already be put into listen mode */ int -assuan_init_socket_server (assuan_context_t *r_ctx, int listen_fd) +assuan_init_socket_server (assuan_context_t *r_ctx, assuan_fd_t listen_fd) { return assuan_init_socket_server_ext (r_ctx, listen_fd, 0); } @@ -132,7 +133,7 @@ assuan_init_socket_server (assuan_context_t *r_ctx, int listen_fd) /* Initialize a server using the already accepted socket FD. This fucntion is deprecated. */ int -assuan_init_connected_socket_server (assuan_context_t *r_ctx, int fd) +assuan_init_connected_socket_server (assuan_context_t *r_ctx, assuan_fd_t fd) { return assuan_init_socket_server_ext (r_ctx, fd, 2); } @@ -143,7 +144,7 @@ assuan_init_connected_socket_server (assuan_context_t *r_ctx, int fd) 1 - FD has already been accepted. */ int -assuan_init_socket_server_ext (assuan_context_t *r_ctx, int fd, +assuan_init_socket_server_ext (assuan_context_t *r_ctx, assuan_fd_t fd, unsigned int flags) { assuan_context_t ctx; @@ -156,21 +157,21 @@ assuan_init_socket_server_ext (assuan_context_t *r_ctx, int fd, ctx->is_server = 1; if ((flags & 2)) ctx->pipe_mode = 1; /* We want a second accept to indicate EOF. */ - ctx->input_fd = -1; - ctx->output_fd = -1; + ctx->input_fd = ASSUAN_INVALID_FD; + ctx->output_fd = ASSUAN_INVALID_FD; - ctx->inbound.fd = -1; - ctx->outbound.fd = -1; + ctx->inbound.fd = ASSUAN_INVALID_FD; + ctx->outbound.fd = ASSUAN_INVALID_FD; if ((flags & 2)) { - ctx->listen_fd = -1; + ctx->listen_fd = ASSUAN_INVALID_FD; ctx->connected_fd = fd; } else { ctx->listen_fd = fd; - ctx->connected_fd = -1; + ctx->connected_fd = ASSUAN_INVALID_FD; } ctx->deinit_handler = (flags & 1)? _assuan_uds_deinit:deinit_socket_server; ctx->accept_handler = ((flags & 2) diff --git a/src/assuan-socket.c b/src/assuan-socket.c index 3cb49b2..c3fcf0c 100644 --- a/src/assuan-socket.c +++ b/src/assuan-socket.c @@ -41,20 +41,23 @@ #endif int -_assuan_close (int fd) +_assuan_close (assuan_fd_t fd) { #ifndef HAVE_W32_SYSTEM return close (fd); #else - int rc = closesocket (fd); + int rc = closesocket (HANDLE2SOCKET(fd)); if (rc && WSAGetLastError () == WSAENOTSOCK) - rc = close (fd); + rc = CloseHandle (fd); return rc; #endif } -int +/* Return a new socket. Note that under W32 we consider a socket the + same as an System Handle; all functions using such a handle know + about this dual use and act accordingly. */ +assuan_fd_t _assuan_sock_new (int domain, int type, int proto) { #ifndef HAVE_W32_SYSTEM @@ -62,13 +65,13 @@ _assuan_sock_new (int domain, int type, int proto) #else if (domain == AF_UNIX || domain == AF_LOCAL) domain = AF_INET; - return socket (domain, type, proto); + return SOCKET2HANDLE(socket (domain, type, proto)); #endif } int -_assuan_sock_connect (int sockfd, struct sockaddr * addr, int addrlen) +_assuan_sock_connect (assuan_fd_t sockfd, struct sockaddr *addr, int addrlen) { #ifndef HAVE_W32_SYSTEM return connect (sockfd, addr, addrlen); @@ -86,7 +89,7 @@ _assuan_sock_connect (int sockfd, struct sockaddr * addr, int addrlen) fclose (fp); /* XXX: set errno in this case */ if (port < 0 || port > 65535) - return -1; + return -1; myaddr.sin_family = AF_INET; myaddr.sin_port = port; @@ -97,13 +100,13 @@ _assuan_sock_connect (int sockfd, struct sockaddr * addr, int addrlen) unaddr->sun_port = myaddr.sin_port; unaddr->sun_addr.s_addr = myaddr.sin_addr.s_addr; - return connect (sockfd, (struct sockaddr *)&myaddr, sizeof myaddr); + return connect (HANDLE2SOCKET(sockfd), (struct sockaddr *)&myaddr, sizeof myaddr); #endif } int -_assuan_sock_bind (int sockfd, struct sockaddr * addr, int addrlen) +_assuan_sock_bind (assuan_fd_t sockfd, struct sockaddr * addr, int addrlen) { #ifndef HAVE_W32_SYSTEM return bind (sockfd, addr, addrlen); @@ -120,10 +123,11 @@ _assuan_sock_bind (int sockfd, struct sockaddr * addr, int addrlen) myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); - rc = bind (sockfd, (struct sockaddr *)&myaddr, len); + rc = bind (HANDLE2SOCKET(sockfd), (struct sockaddr *)&myaddr, len); if (rc) return rc; - rc = getsockname (sockfd, (struct sockaddr *)&myaddr, &len); + rc = getsockname (HANDLE2SOCKET(sockfd), + (struct sockaddr *)&myaddr, &len); if (rc) return rc; unaddr = (struct sockaddr_un *)addr; @@ -140,7 +144,7 @@ _assuan_sock_bind (int sockfd, struct sockaddr * addr, int addrlen) return 0; } - return bind (sockfd, addr, addrlen); + return bind (HANDLE2SOCKET(sockfd), addr, addrlen); #endif } diff --git a/src/assuan-uds.c b/src/assuan-uds.c index 9f4b16b..123a3ad 100644 --- a/src/assuan-uds.c +++ b/src/assuan-uds.c @@ -141,7 +141,7 @@ uds_reader (assuan_context_t ctx, void *buf, size_t buflen) #else /*HAVE_W32_SYSTEM*/ - len = recvfrom (ctx->inbound.fd, buf, buflen, 0, NULL, NULL); + len = recvfrom (HANDLE2SOCKET(ctx->inbound.fd), buf, buflen, 0, NULL, NULL); #endif /*HAVE_W32_SYSTEM*/ @@ -182,7 +182,7 @@ uds_writer (assuan_context_t ctx, const void *buf, size_t buflen) #else /*HAVE_W32_SYSTEM*/ int len; - len = sendto (ctx->outbound.fd, buf, buflen, 0, + len = sendto (HANDLE2SOCKET(ctx->outbound.fd), buf, buflen, 0, (struct sockaddr *)&ctx->serveraddr, sizeof (struct sockaddr_in)); #endif /*HAVE_W32_SYSTEM*/ @@ -191,7 +191,7 @@ uds_writer (assuan_context_t ctx, const void *buf, size_t buflen) static assuan_error_t -uds_sendfd (assuan_context_t ctx, int fd) +uds_sendfd (assuan_context_t ctx, assuan_fd_t fd) { #ifdef USE_DESCRIPTOR_PASSING struct msghdr msg; @@ -241,7 +241,7 @@ uds_sendfd (assuan_context_t ctx, int fd) static assuan_error_t -uds_receivefd (assuan_context_t ctx, int *fd) +uds_receivefd (assuan_context_t ctx, assuan_fd_t *fd) { #ifdef USE_DESCRIPTOR_PASSING int i; diff --git a/src/assuan.h b/src/assuan.h index cab0275..5acf08c 100644 --- a/src/assuan.h +++ b/src/assuan.h @@ -337,6 +337,18 @@ typedef struct assuan_context_s *assuan_context_t; typedef struct assuan_context_s *ASSUAN_CONTEXT _ASSUAN_DEPRECATED; #endif /*_ASSUAN_ONLY_GPG_ERRORS*/ +/* Because we use system handles and not libc low level file + descriptors on W32, we need to declare them as HANDLE (which + actually is a plain pointer). This is required to eventually + support 64 bits Windows systems. */ +#ifdef _WIN32 +typedef void * assuan_fd_t; +#define ASSUAN_INVALID_FD ((void*)(-1)) +#else +typedef int assuan_fd_t; +#define ASSUAN_INVALID_FD (-1) +#endif + /*-- assuan-handler.c --*/ int assuan_register_command (assuan_context_t ctx, const char *cmd_string, @@ -361,7 +373,7 @@ int assuan_register_option_handler (assuan_context_t ctx, int assuan_process (assuan_context_t ctx); int assuan_process_next (assuan_context_t ctx); int assuan_get_active_fds (assuan_context_t ctx, int what, - int *fdarray, int fdarraysize); + assuan_fd_t *fdarray, int fdarraysize); FILE *assuan_get_data_fp (assuan_context_t ctx); @@ -372,15 +384,17 @@ assuan_error_t assuan_write_status (assuan_context_t ctx, /* Negotiate a file descriptor. If LINE contains "FD=N", returns N assuming a local file descriptor. If LINE contains "FD" reads a file descriptor via CTX and stores it in *RDF (the CTX must be - capable of passing file descriptors). */ + capable of passing file descriptors). Under W32 the returned FD is + a libc-type one. */ assuan_error_t assuan_command_parse_fd (assuan_context_t ctx, char *line, - int *rfd); + assuan_fd_t *rfd); + /*-- assuan-listen.c --*/ assuan_error_t assuan_set_hello_line (assuan_context_t ctx, const char *line); assuan_error_t assuan_accept (assuan_context_t ctx); -int assuan_get_input_fd (assuan_context_t ctx); -int assuan_get_output_fd (assuan_context_t ctx); +assuan_fd_t assuan_get_input_fd (assuan_context_t ctx); +assuan_fd_t assuan_get_output_fd (assuan_context_t ctx); assuan_error_t assuan_close_input_fd (assuan_context_t ctx); assuan_error_t assuan_close_output_fd (assuan_context_t ctx); @@ -390,10 +404,10 @@ int assuan_init_pipe_server (assuan_context_t *r_ctx, int filedes[2]); void assuan_deinit_server (assuan_context_t ctx); /*-- assuan-socket-server.c --*/ -int assuan_init_socket_server (assuan_context_t *r_ctx, int listen_fd); +int assuan_init_socket_server (assuan_context_t *r_ctx, assuan_fd_t listen_fd); int assuan_init_connected_socket_server (assuan_context_t *r_ctx, - int fd) _ASSUAN_DEPRECATED; -int assuan_init_socket_server_ext (assuan_context_t *r_ctx, int fd, + assuan_fd_t fd) _ASSUAN_DEPRECATED; +int assuan_init_socket_server_ext (assuan_context_t *r_ctx, assuan_fd_t fd, unsigned int flags); /*-- assuan-pipe-connect.c --*/ @@ -460,8 +474,8 @@ assuan_error_t assuan_send_data (assuan_context_t ctx, /* The file descriptor must be pending before assuan_receivefd is called. This means that assuan_sendfd should be called *before* the trigger is sent (normally via assuan_write_line ("INPUT FD")). */ -assuan_error_t assuan_sendfd (assuan_context_t ctx, int fd); -assuan_error_t assuan_receivefd (assuan_context_t ctx, int *fd); +assuan_error_t assuan_sendfd (assuan_context_t ctx, assuan_fd_t fd); +assuan_error_t assuan_receivefd (assuan_context_t ctx, assuan_fd_t *fd); /*-- assuan-util.c --*/ void assuan_set_malloc_hooks ( void *(*new_alloc_func)(size_t n), |