From 72f48d9e8a8f2fc81dcb09569263539a11810edf Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 23 May 2002 09:07:12 +0000 Subject: [PATCH] * assuan-util.c (assuan_set_io_func): New. * assuan-buffer.c (writen, readline): Use the new functions instead of pth. * assuan-socket-server.c (accept_connection): Don't use the pth_accept - using the assuan included accept code would be a bad idea within Pth so we don't need a replacement function. --- assuan/ChangeLog | 15 +++++++ assuan/assuan-buffer.c | 20 +++------ assuan/assuan-defs.h | 5 +++ assuan/assuan-pipe-server.c | 2 +- assuan/assuan-socket-server.c | 80 ++++++++++++++++++++++++----------- assuan/assuan-util.c | 14 ++++++ assuan/assuan.h | 4 ++ 7 files changed, 101 insertions(+), 39 deletions(-) diff --git a/assuan/ChangeLog b/assuan/ChangeLog index 2b95d56d3..bb74b7cda 100644 --- a/assuan/ChangeLog +++ b/assuan/ChangeLog @@ -1,3 +1,18 @@ +2002-05-23 Werner Koch + + * assuan-util.c (assuan_set_io_func): New. + * assuan-buffer.c (writen, readline): Use the new functions + instead of pth. + * assuan-socket-server.c (accept_connection): Don't use the + pth_accept - using the assuan included accept code would be a bad + idea within Pth so we don't need a replacement function. + +2002-05-22 Werner Koch + + * assuan-socket-server.c (assuan_init_connected_socket_server): New. + (accept_connection): Factored most code out to.. + (accept_connection_bottom): .. new function. + 2002-04-04 Werner Koch * assuan-buffer.c (my_log_prefix): New. Use it for all i/o debug diff --git a/assuan/assuan-buffer.c b/assuan/assuan-buffer.c index 29f94794c..da6b201e4 100644 --- a/assuan/assuan-buffer.c +++ b/assuan/assuan-buffer.c @@ -25,9 +25,6 @@ #include #include #include -#ifdef USE_GNU_PTH -# include -#endif #include "assuan-defs.h" #ifdef HAVE_JNLIB_LOGGING @@ -51,11 +48,9 @@ writen ( int fd, const char *buffer, size_t length ) { while (length) { -#ifdef USE_GNU_PTH - int nwritten = pth_write (fd, buffer, length); -#else - int nwritten = write (fd, buffer, length); -#endif + int nwritten = _assuan_write_wrapper? + _assuan_write_wrapper (fd, buffer, length): + write (fd, buffer, length); if (nwritten < 0) { @@ -80,11 +75,10 @@ readline (int fd, char *buf, size_t buflen, int *r_nread, int *eof) *r_nread = 0; while (nleft > 0) { -#ifdef USE_GNU_PTH - int n = pth_read (fd, buf, nleft); -#else - int n = read (fd, buf, nleft); -#endif + int n = _assuan_read_wrapper? + _assuan_read_wrapper (fd, buf, nleft): + read (fd, buf, nleft); + if (n < 0) { if (errno == EINTR) diff --git a/assuan/assuan-defs.h b/assuan/assuan-defs.h index 6c502bf9c..5185ad198 100644 --- a/assuan/assuan-defs.h +++ b/assuan/assuan-defs.h @@ -76,6 +76,7 @@ struct assuan_context_s { pid_t pid; /* In pipe mode, the pid of the child server process. In socket mode, the pid of the server */ int listen_fd; /* The fd we are listening on (used by socket servers) */ + int connected_fd; /* helper */ pid_t client_pid; /* for a socket server the PID of the client or -1 if not available */ @@ -101,6 +102,7 @@ struct assuan_context_s { }; + /*-- assuan-pipe-server.c --*/ int _assuan_new_context (ASSUAN_CONTEXT *r_ctx); void _assuan_release_context (ASSUAN_CONTEXT ctx); @@ -119,6 +121,9 @@ AssuanError _assuan_read_from_server (ASSUAN_CONTEXT ctx, int *okay, int *off); /*-- assuan-util.c --*/ +extern ssize_t (*_assuan_read_wrapper)(int,void*,size_t); +extern ssize_t (*_assuan_write_wrapper)(int,const void*,size_t); + void *_assuan_malloc (size_t n); void *_assuan_calloc (size_t n, size_t m); void *_assuan_realloc (void *p, size_t n); diff --git a/assuan/assuan-pipe-server.c b/assuan/assuan-pipe-server.c index 5c5d1248c..07373e12d 100644 --- a/assuan/assuan-pipe-server.c +++ b/assuan/assuan-pipe-server.c @@ -46,7 +46,7 @@ finish_connection (ASSUAN_CONTEXT ctx) /* Create a new context. Note that the handlers are set up for a pipe - server/client - this wau we don't need extra dummy functions */ + server/client - this way we don't need extra dummy functions */ int _assuan_new_context (ASSUAN_CONTEXT *r_ctx) { diff --git a/assuan/assuan-socket-server.c b/assuan/assuan-socket-server.c index 39dd84a13..e4a11b0df 100644 --- a/assuan/assuan-socket-server.c +++ b/assuan/assuan-socket-server.c @@ -25,31 +25,15 @@ #include #include #include -#ifdef USE_GNU_PTH -# include -#endif #include "assuan-defs.h" static int -accept_connection (ASSUAN_CONTEXT ctx) +accept_connection_bottom (ASSUAN_CONTEXT ctx) { - int fd; - struct sockaddr_un clnt_addr; - size_t len = sizeof clnt_addr; + int fd = ctx->connected_fd; ctx->client_pid = (pid_t)-1; -#ifdef USE_GNU_PTH - fd = pth_accept (ctx->listen_fd, (struct sockaddr*)&clnt_addr, &len ); -#else - fd = accept (ctx->listen_fd, (struct sockaddr*)&clnt_addr, &len ); -#endif - if (fd == -1) - { - ctx->os_errno = errno; - return ASSUAN_Accept_Failed; - } - #ifdef HAVE_SO_PEERCRED { struct ucred cr; @@ -75,6 +59,26 @@ accept_connection (ASSUAN_CONTEXT ctx) return 0; } + +static int +accept_connection (ASSUAN_CONTEXT ctx) +{ + int fd; + struct sockaddr_un clnt_addr; + size_t len = sizeof clnt_addr; + + ctx->client_pid = (pid_t)-1; + fd = accept (ctx->listen_fd, (struct sockaddr*)&clnt_addr, &len ); + if (fd == -1) + { + ctx->os_errno = errno; + return ASSUAN_Accept_Failed; + } + + ctx->connected_fd = fd; + return accept_connection_bottom (ctx); +} + static int finish_connection (ASSUAN_CONTEXT ctx) { @@ -116,6 +120,7 @@ assuan_init_socket_server (ASSUAN_CONTEXT *r_ctx, int listen_fd) ctx->outbound.fd = -1; ctx->listen_fd = listen_fd; + ctx->connected_fd = -1; ctx->deinit_handler = deinit_socket_server; ctx->accept_handler = accept_connection; ctx->finish_handler = finish_connection; @@ -128,12 +133,37 @@ assuan_init_socket_server (ASSUAN_CONTEXT *r_ctx, int listen_fd) return rc; } - - - - - - - +/* Initialize a server using the already accepted socket FD. */ +int +assuan_init_connected_socket_server (ASSUAN_CONTEXT *r_ctx, int fd) +{ + ASSUAN_CONTEXT ctx; + int rc; + + *r_ctx = NULL; + ctx = xtrycalloc (1, sizeof *ctx); + if (!ctx) + return ASSUAN_Out_Of_Core; + ctx->is_server = 1; + ctx->pipe_mode = 1; /* we wan't a second accept to indicate EOF */ + ctx->input_fd = -1; + ctx->output_fd = -1; + + ctx->inbound.fd = -1; + ctx->outbound.fd = -1; + + ctx->listen_fd = -1; + ctx->connected_fd = fd; + ctx->deinit_handler = deinit_socket_server; + ctx->accept_handler = accept_connection_bottom; + ctx->finish_handler = finish_connection; + + rc = _assuan_register_std_commands (ctx); + if (rc) + xfree (ctx); + else + *r_ctx = ctx; + return rc; +} diff --git a/assuan/assuan-util.c b/assuan/assuan-util.c index 4153ef8db..15b9fe5eb 100644 --- a/assuan/assuan-util.c +++ b/assuan/assuan-util.c @@ -29,6 +29,9 @@ #include "../jnlib/logging.h" #endif +ssize_t (*_assuan_read_wrapper)(int,void*,size_t) = NULL; +ssize_t (*_assuan_write_wrapper)(int,const void*,size_t) = NULL; + static void *(*alloc_func)(size_t n) = malloc; static void *(*realloc_func)(void *p, size_t n) = realloc; @@ -74,6 +77,17 @@ _assuan_free (void *p) free_func (p); } +/* For use with Pth it is required to have special read and write + functions. We can't assume an ELF based system so we have to + explicitly set them if we are going to use Pth. */ +void +assuan_set_io_func (ssize_t (*r)(int,void*,size_t), + ssize_t (*w)(int,const void*,size_t)) +{ + _assuan_read_wrapper = r; + _assuan_write_wrapper = w; +} + /* Store the error in the context so that the error sending function diff --git a/assuan/assuan.h b/assuan/assuan.h index a9340019f..dae8ed74c 100644 --- a/assuan/assuan.h +++ b/assuan/assuan.h @@ -23,6 +23,7 @@ #include #include +#include /* for ssize_t */ #ifdef __cplusplus extern "C" { @@ -170,6 +171,7 @@ void assuan_deinit_server (ASSUAN_CONTEXT ctx); /*-- assuan-socket-server.c --*/ int assuan_init_socket_server (ASSUAN_CONTEXT *r_ctx, int listen_fd); +int assuan_init_connected_socket_server (ASSUAN_CONTEXT *r_ctx, int fd); /*-- assuan-pipe-connect.c --*/ @@ -212,6 +214,8 @@ AssuanError assuan_send_data (ASSUAN_CONTEXT ctx, void assuan_set_malloc_hooks ( void *(*new_alloc_func)(size_t n), void *(*new_realloc_func)(void *p, size_t n), void (*new_free_func)(void*) ); +void assuan_set_io_func (ssize_t (*r)(int,void*,size_t), + ssize_t (*w)(int,const void*,size_t)); void assuan_set_log_stream (ASSUAN_CONTEXT ctx, FILE *fp); int assuan_set_error (ASSUAN_CONTEXT ctx, int err, const char *text); void assuan_set_pointer (ASSUAN_CONTEXT ctx, void *pointer);