[PATCH] assuan_socket_connect_fd().

Ben Kibbey bjk at luxsci.net
Thu Feb 3 02:36:23 CET 2011


---
 src/assuan-socket-connect.c |   98 ++++++++++++++++++++++++++++---------------
 src/assuan.h.in             |    4 ++
 src/libassuan.def           |    1 +
 src/libassuan.vers          |    1 +
 4 files changed, 70 insertions(+), 34 deletions(-)

diff --git a/src/assuan-socket-connect.c b/src/assuan-socket-connect.c
index 609813d..1fdba6c 100644
--- a/src/assuan-socket-connect.c
+++ b/src/assuan-socket-connect.c
@@ -97,6 +97,69 @@ parse_portno (const char *str, uint16_t *r_port)
 }
 
 
+static gpg_error_t
+_assuan_connect_finalize(assuan_context_t ctx, int fd, unsigned int flags)
+{
+  gpg_error_t err;
+
+  ctx->engine.release = _assuan_client_release;
+  ctx->engine.readfnc = _assuan_simple_read;
+  ctx->engine.writefnc = _assuan_simple_write;
+  ctx->engine.sendfd = NULL;
+  ctx->engine.receivefd = NULL;
+  ctx->finish_handler = _assuan_client_finish;
+  ctx->inbound.fd = fd;
+  ctx->outbound.fd = fd;
+  ctx->max_accepts = -1;
+
+  if (flags & ASSUAN_SOCKET_CONNECT_FDPASSING)
+    _assuan_init_uds_io (ctx);
+
+  /* initial handshake */
+  {
+    assuan_response_t response;
+    int off;
+
+    err = _assuan_read_from_server (ctx, &response, &off, 0);
+    if (err)
+      TRACE1 (ctx, ASSUAN_LOG_SYSIO, "assuan_socket_connect", ctx,
+	      "can't connect to server: %s\n", gpg_strerror (err));
+    else if (response != ASSUAN_RESPONSE_OK)
+      {
+	char *sname = _assuan_encode_c_string (ctx, ctx->inbound.line);
+	if (sname)
+	  {
+	    TRACE1 (ctx, ASSUAN_LOG_SYSIO, "assuan_socket_connect", ctx,
+		    "can't connect to server: %s", sname);
+	    _assuan_free (ctx, sname);
+	  }
+	err = _assuan_error (ctx, GPG_ERR_ASS_CONNECT_FAILED);
+      }
+  }
+
+  return err;
+}
+
+
+/* Attach an existing connected file descriptor FD to an allocated handle CTX
+ * and initialize the connection.
+ */
+gpg_error_t
+assuan_socket_connect_fd (assuan_context_t ctx, int fd, unsigned int flags)
+{
+  gpg_error_t err;
+
+  if (!ctx || fd < 0)
+    return GPG_ERR_INV_ARG;
+
+  err = _assuan_connect_finalize(ctx, fd, flags);
+
+  if (err)
+    _assuan_reset (ctx);
+
+  return err;
+}
+
 
 /* Make a connection to the Unix domain socket NAME and return a new
    Assuan context in CTX.  SERVER_PID is currently not used but may
@@ -268,41 +331,8 @@ assuan_socket_connect (assuan_context_t ctx, const char *name,
       return _assuan_error (ctx, GPG_ERR_ASS_CONNECT_FAILED);
     }
  
-  ctx->engine.release = _assuan_client_release;
-  ctx->engine.readfnc = _assuan_simple_read;
-  ctx->engine.writefnc = _assuan_simple_write;
-  ctx->engine.sendfd = NULL;
-  ctx->engine.receivefd = NULL;
-  ctx->finish_handler = _assuan_client_finish;
-  ctx->inbound.fd = fd;
-  ctx->outbound.fd = fd;
-  ctx->max_accepts = -1;
+  err = _assuan_connect_finalize(ctx, fd, flags);
 
-  if (flags & ASSUAN_SOCKET_CONNECT_FDPASSING)
-    _assuan_init_uds_io (ctx);
-
-  /* initial handshake */
-  {
-    assuan_response_t response;
-    int off;
-
-    err = _assuan_read_from_server (ctx, &response, &off, 0);
-    if (err)
-      TRACE1 (ctx, ASSUAN_LOG_SYSIO, "assuan_socket_connect", ctx,
-	      "can't connect to server: %s\n", gpg_strerror (err));
-    else if (response != ASSUAN_RESPONSE_OK)
-      {
-	char *sname = _assuan_encode_c_string (ctx, ctx->inbound.line);
-	if (sname)
-	  {
-	    TRACE1 (ctx, ASSUAN_LOG_SYSIO, "assuan_socket_connect", ctx,
-		    "can't connect to server: %s", sname);
-	    _assuan_free (ctx, sname);
-	  }
-	err = _assuan_error (ctx, GPG_ERR_ASS_CONNECT_FAILED);
-      }
-  }
-  
   if (err)
     _assuan_reset (ctx);
 
diff --git a/src/assuan.h.in b/src/assuan.h.in
index 018d5a1..e686c5a 100644
--- a/src/assuan.h.in
+++ b/src/assuan.h.in
@@ -356,6 +356,10 @@ gpg_error_t assuan_pipe_connect (assuan_context_t ctx,
 gpg_error_t assuan_socket_connect (assuan_context_t ctx, const char *name,
 				   pid_t server_pid, unsigned int flags);
 
+/*-- assuan-socket-connect.c --*/
+gpg_error_t assuan_socket_connect_fd (assuan_context_t ctx, int fd,
+				   unsigned int flags);
+
 /*-- context.c --*/
 pid_t assuan_get_pid (assuan_context_t ctx);
 struct _assuan_peercred
diff --git a/src/libassuan.def b/src/libassuan.def
index a2e7b61..b2ddc25 100644
--- a/src/libassuan.def
+++ b/src/libassuan.def
@@ -103,6 +103,7 @@ EXPORTS
     _assuan_w32ce_finish_pipe           @82
     __assuan_socket			@83
     __assuan_connect			@84
+    assuan_socket_connect_fd		@85
 
 ; END
 
diff --git a/src/libassuan.vers b/src/libassuan.vers
index b14a940..b179828 100644
--- a/src/libassuan.vers
+++ b/src/libassuan.vers
@@ -24,6 +24,7 @@ LIBASSUAN_1.1 {
   global:
     __assuan_socket;
     __assuan_connect;
+    assuan_socket_connect_fd;
 };
 
 LIBASSUAN_1.0 {
-- 
1.7.2.3




More information about the Gnupg-devel mailing list