[svn] assuan - r245 - in trunk: . doc src

svn author wk cvs at cvs.gnupg.org
Thu Jul 12 12:06:56 CEST 2007


Author: wk
Date: 2007-07-12 12:06:26 +0200 (Thu, 12 Jul 2007)
New Revision: 245

Modified:
   trunk/NEWS
   trunk/TODO
   trunk/configure.ac
   trunk/doc/ChangeLog
   trunk/doc/assuan.texi
   trunk/src/ChangeLog
   trunk/src/assuan-buffer.c
   trunk/src/assuan-defs.h
   trunk/src/assuan-handler.c
   trunk/src/assuan-io-pth.c
   trunk/src/assuan-io.c
   trunk/src/assuan-listen.c
   trunk/src/assuan-pipe-connect.c
   trunk/src/assuan-pipe-server.c
   trunk/src/assuan-socket-connect.c
   trunk/src/assuan-socket-server.c
   trunk/src/assuan-socket.c
   trunk/src/assuan-uds.c
   trunk/src/assuan.h
Log:
Introduced new type for better support of W32 HADNLES vs. file descriptors.


Modified: trunk/NEWS
===================================================================
--- trunk/NEWS	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/NEWS	2007-07-12 10:06:26 UTC (rev 245)
@@ -1,6 +1,14 @@
-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.
 
  * New flag ASSUAN_CONFIDENTIAL to return the state of

Modified: trunk/TODO
===================================================================
--- trunk/TODO	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/TODO	2007-07-12 10:06:26 UTC (rev 245)
@@ -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

Modified: trunk/configure.ac
===================================================================
--- trunk/configure.ac	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/configure.ac	2007-07-12 10:06:26 UTC (rev 245)
@@ -24,8 +24,8 @@
 # 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;}')]))

Modified: trunk/doc/ChangeLog
===================================================================
--- trunk/doc/ChangeLog	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/doc/ChangeLog	2007-07-12 10:06:26 UTC (rev 245)
@@ -1,3 +1,8 @@
+2007-07-12  Werner Koch  <wk at g10code.com>
+
+	* assuan.texi (Utilities): Document that under W32 we return a
+	system handle.
+
 2007-07-05  Werner Koch  <wk at g10code.com>
 
 	* 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

Modified: trunk/doc/assuan.texi
===================================================================
--- trunk/doc/assuan.texi	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/doc/assuan.texi	2007-07-12 10:06:26 UTC (rev 245)
@@ -1207,6 +1207,12 @@
 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
+ at 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 @@
 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
 
 

Modified: trunk/src/ChangeLog
===================================================================
--- trunk/src/ChangeLog	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/ChangeLog	2007-07-12 10:06:26 UTC (rev 245)
@@ -1,3 +1,19 @@
+2007-07-12  Werner Koch  <wk at g10code.com>
+
+	* 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  <marcus at g10code.de>
 
 	* assuan-defs.h (struct assuan_context_s): Have full peercred

Modified: trunk/src/assuan-buffer.c
===================================================================
--- trunk/src/assuan-buffer.c	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan-buffer.c	2007-07-12 10:06:26 UTC (rev 245)
@@ -135,7 +135,7 @@
       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 @@
       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 @@
 	{
 	  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 @@
       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 @@
         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 @@
     {
       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 @@
     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 @@
             {
 	      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 @@
 	{
 	  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_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_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,

Modified: trunk/src/assuan-defs.h
===================================================================
--- trunk/src/assuan-defs.h	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan-defs.h	2007-07-12 10:06:26 UTC (rev 245)
@@ -80,9 +80,9 @@
   /* 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 @@
   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 @@
   } inbound;
 
   struct {
-    int fd;
+    assuan_fd_t fd;
     struct {
       FILE *fp;
       char line[LINELENGTH];
@@ -137,8 +137,9 @@
   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 @@
     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 @@
                              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 @@
 #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 @@
 #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*/

Modified: trunk/src/assuan-handler.c
===================================================================
--- trunk/src/assuan-handler.c	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan-handler.c	2007-07-12 10:06:26 UTC (rev 245)
@@ -135,8 +135,9 @@
   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 @@
       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 @@
     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 @@
 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 @@
  **/
 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 @@
 
   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;

Modified: trunk/src/assuan-io-pth.c
===================================================================
--- trunk/src/assuan-io-pth.c	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan-io-pth.c	2007-07-12 10:06:26 UTC (rev 245)
@@ -54,13 +54,15 @@
 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

Modified: trunk/src/assuan-io.c
===================================================================
--- trunk/src/assuan-io.c	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan-io.c	2007-07-12 10:06:26 UTC (rev 245)
@@ -55,12 +55,12 @@
      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 @@
      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.  */

Modified: trunk/src/assuan-listen.c
===================================================================
--- trunk/src/assuan-listen.c	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan-listen.c	2007-07-12 10:06:26 UTC (rev 245)
@@ -114,17 +114,17 @@
 
 
 
-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_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_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;
 }
 

Modified: trunk/src/assuan-pipe-connect.c
===================================================================
--- trunk/src/assuan-pipe-connect.c	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan-pipe-connect.c	2007-07-12 10:06:26 UTC (rev 245)
@@ -56,18 +56,7 @@
 #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
 fix_signals (void)
@@ -120,25 +109,25 @@
 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 @@
 #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 @@
       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 @@
                   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 @@
   
   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 @@
   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 @@
   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 @@
       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 @@
       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 @@
                       ))
     {
       _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 @@
       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", */

Modified: trunk/src/assuan-pipe-server.c
===================================================================
--- trunk/src/assuan-pipe-server.c	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan-pipe-server.c	2007-07-12 10:06:26 UTC (rev 245)
@@ -67,14 +67,14 @@
   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 @@
 #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_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];

Modified: trunk/src/assuan-socket-connect.c
===================================================================
--- trunk/src/assuan-socket-connect.c	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan-socket-connect.c	2007-07-12 10:06:26 UTC (rev 245)
@@ -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_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 @@
   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 @@
   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));

Modified: trunk/src/assuan-socket-server.c
===================================================================
--- trunk/src/assuan-socket-server.c	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan-socket-server.c	2007-07-12 10:06:26 UTC (rev 245)
@@ -45,7 +45,7 @@
 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 @@
 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 @@
 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 @@
 /* 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 @@
 /* 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 @@
               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 @@
   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)

Modified: trunk/src/assuan-socket.c
===================================================================
--- trunk/src/assuan-socket.c	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan-socket.c	2007-07-12 10:06:26 UTC (rev 245)
@@ -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 @@
 #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 @@
   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 @@
   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 @@
       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 @@
       
       return 0;
     }
-  return bind (sockfd, addr, addrlen);
+  return bind (HANDLE2SOCKET(sockfd), addr, addrlen);
 #endif
 }
 

Modified: trunk/src/assuan-uds.c
===================================================================
--- trunk/src/assuan-uds.c	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan-uds.c	2007-07-12 10:06:26 UTC (rev 245)
@@ -141,7 +141,7 @@
 
 #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 @@
 #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 @@
 
 
 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 @@
 
 
 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;

Modified: trunk/src/assuan.h
===================================================================
--- trunk/src/assuan.h	2007-07-08 15:53:35 UTC (rev 244)
+++ trunk/src/assuan.h	2007-07-12 10:06:26 UTC (rev 245)
@@ -337,6 +337,18 @@
 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_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 @@
 /* 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 @@
 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 @@
 /* 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),




More information about the Gnupg-commits mailing list