diff options
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | src/ChangeLog | 13 | ||||
-rw-r--r-- | src/assuan-defs.h | 4 | ||||
-rw-r--r-- | src/assuan-io-pth.c | 25 | ||||
-rw-r--r-- | src/assuan-io.c | 51 | ||||
-rw-r--r-- | src/assuan-util.c | 19 | ||||
-rw-r--r-- | src/assuan.h | 15 |
7 files changed, 121 insertions, 10 deletions
@@ -1,9 +1,11 @@ Noteworthy changes in version 1.0.4 ------------------------------------------------ - * New socket wrapper fucntions to support Unix domain sockets under + * New socket wrapper functions to support Unix domain sockets under Windows. + * New hook feature to enhance the internal I/O functions. + Noteworthy changes in version 1.0.3 (2007-08-24) ------------------------------------------------ diff --git a/src/ChangeLog b/src/ChangeLog index d3202c4..fa9bd23 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,16 @@ +2007-10-08 Werner Koch <[email protected]> + + * assuan-util.c (assuan_set_io_hooks): New. + * assuan.h (struct assuan_io_hooks): New. + (assuan_set_io_hooks, _assuan_io_hooks): Add prefix macros. + * assuan-defs.h (_assuan_io_hooks): New. + * assuan-io.c (do_io_read): Take all code from _assuan_io_read. + (_assuan_io_read, _assuan_simple_read): Add hook feature. + (do_io_write): Take all code from _assuan_io_write. + (_assuan_io_write, _assuan_simple_write): Add hook feature. + * assuan-io-pth.c (_assuan_simple_read, _assuan_simple_write) + (_assuan_io_read, _assuan_io_write): Add hook feature. + 2007-10-05 Marcus Brinkmann <[email protected]> * assuan.h (_assuan_error_is_eagain): Add prefix macro. diff --git a/src/assuan-defs.h b/src/assuan-defs.h index f17e906..b299435 100644 --- a/src/assuan-defs.h +++ b/src/assuan-defs.h @@ -75,6 +75,10 @@ struct assuan_io }; +/* The global variable with the optional hook fucntions. */ +extern struct assuan_io_hooks _assuan_io_hooks; + + /* The context we use with most functions. */ struct assuan_context_s { diff --git a/src/assuan-io-pth.c b/src/assuan-io-pth.c index 6b287c0..5b60cc7 100644 --- a/src/assuan-io-pth.c +++ b/src/assuan-io-pth.c @@ -1,5 +1,5 @@ /* assuan-io-pth.c - Pth version of assua-io.c. - * Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc. + * Copyright (C) 2002, 2004, 2006, 2007 Free Software Foundation, Inc. * * This file is part of Assuan. * @@ -56,24 +56,47 @@ _assuan_simple_read (assuan_context_t ctx, void *buffer, size_t size) { /* Fixme: For W32 we should better not cast the HANDLE type to int. However, this requires changes in w32pth too. */ + ssize_t retval; + + if (_assuan_io_hooks.read_hook + && _assuan_io_hooks.read_hook (ctx, ctx->inbound.fd, + buffer, size, &retval) == 1) + return retval; + return _assuan_io_read (ctx->inbound.fd, buffer, size); } ssize_t _assuan_simple_write (assuan_context_t ctx, const void *buffer, size_t size) { + ssize_t retval; + + if (_assuan_io_hooks.write_hook + && _assuan_io_hooks.write_hook (ctx, ctx->outbound.fd, + buffer, size, &retval) == 1) + return retval; return _assuan_io_write (ctx->outbound.fd, buffer, size); } ssize_t _assuan_io_read (assuan_fd_t fd, void *buffer, size_t size) { + ssize_t retval; + + if (_assuan_io_hooks.read_hook + && _assuan_io_hooks.read_hook (NULL, fd, buffer, size, &retval) == 1) + return retval; return pth_read ((int)fd, buffer, size); } ssize_t _assuan_io_write (assuan_fd_t fd, const void *buffer, size_t size) { + ssize_t retval; + + if (_assuan_io_hooks.write_hook + && _assuan_io_hooks.write_hook (NULL, fd, buffer, size, &retval) == 1) + return retval; return pth_write ((int)fd, buffer, size); } diff --git a/src/assuan-io.c b/src/assuan-io.c index eb50f7a..1ff4ecb 100644 --- a/src/assuan-io.c +++ b/src/assuan-io.c @@ -1,5 +1,5 @@ /* assuan-io.c - Wraps the read and write functions. - * Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc. + * Copyright (C) 2002, 2004, 2006, 2007 Free Software Foundation, Inc. * * This file is part of Assuan. * @@ -46,8 +46,8 @@ _assuan_waitpid (pid_t pid, int *status, int options) #endif -ssize_t -_assuan_io_read (assuan_fd_t fd, void *buffer, size_t size) +static ssize_t +do_io_read (assuan_fd_t fd, void *buffer, size_t size) { #ifdef HAVE_W32_SYSTEM /* Due to the peculiarities of the W32 API we can't use read for a @@ -92,14 +92,34 @@ _assuan_io_read (assuan_fd_t fd, void *buffer, size_t size) ssize_t +_assuan_io_read (assuan_fd_t fd, void *buffer, size_t size) +{ + ssize_t retval; + + if (_assuan_io_hooks.read_hook + && _assuan_io_hooks.read_hook (NULL, fd, buffer, size, &retval) == 1) + return retval; + + return do_io_read (fd, buffer, size); +} + +ssize_t _assuan_simple_read (assuan_context_t ctx, void *buffer, size_t size) { - return _assuan_io_read (ctx->inbound.fd, buffer, size); + ssize_t retval; + + if (_assuan_io_hooks.read_hook + && _assuan_io_hooks.read_hook (ctx, ctx->inbound.fd, + buffer, size, &retval) == 1) + return retval; + + return do_io_read (ctx->inbound.fd, buffer, size); } -ssize_t -_assuan_io_write (assuan_fd_t fd, const void *buffer, size_t size) + +static ssize_t +do_io_write (assuan_fd_t fd, const void *buffer, size_t size) { #ifdef HAVE_W32_SYSTEM /* Due to the peculiarities of the W32 API we can't use write for a @@ -132,11 +152,28 @@ _assuan_io_write (assuan_fd_t fd, const void *buffer, size_t size) #endif /*!HAVE_W32_SYSTEM*/ } +ssize_t +_assuan_io_write (assuan_fd_t fd, const void *buffer, size_t size) +{ + ssize_t retval; + + if (_assuan_io_hooks.write_hook + && _assuan_io_hooks.write_hook (NULL, fd, buffer, size, &retval) == 1) + return retval; + return do_io_write (fd, buffer, size); +} ssize_t _assuan_simple_write (assuan_context_t ctx, const void *buffer, size_t size) { - return _assuan_io_write (ctx->outbound.fd, buffer, size); + ssize_t retval; + + if (_assuan_io_hooks.write_hook + && _assuan_io_hooks.write_hook (ctx, ctx->outbound.fd, + buffer, size, &retval) == 1) + return retval; + + return do_io_write (ctx->outbound.fd, buffer, size); } diff --git a/src/assuan-util.c b/src/assuan-util.c index 430b346..cefefcb 100644 --- a/src/assuan-util.c +++ b/src/assuan-util.c @@ -30,6 +30,10 @@ static void *(*alloc_func)(size_t n) = malloc; static void *(*realloc_func)(void *p, size_t n) = realloc; static void (*free_func)(void*) = free; +struct assuan_io_hooks _assuan_io_hooks; + + + void assuan_set_malloc_hooks ( void *(*new_alloc_func)(size_t n), void *(*new_realloc_func)(void *p, size_t n), @@ -40,6 +44,20 @@ assuan_set_malloc_hooks ( void *(*new_alloc_func)(size_t n), free_func = new_free_func; } + +void +assuan_set_io_hooks (assuan_io_hooks_t io_hooks) +{ + _assuan_io_hooks.read_hook = NULL; + _assuan_io_hooks.write_hook = NULL; + if (io_hooks) + { + _assuan_io_hooks.read_hook = io_hooks->read_hook; + _assuan_io_hooks.write_hook = io_hooks->write_hook; + } +} + + void * _assuan_malloc (size_t n) { @@ -168,4 +186,3 @@ assuan_get_flag (assuan_context_t ctx, assuan_flag_t flag) return 0; } - diff --git a/src/assuan.h b/src/assuan.h index 6ea41d9..d1a72c6 100644 --- a/src/assuan.h +++ b/src/assuan.h @@ -121,6 +121,7 @@ #define assuan_sendfd _ASSUAN_PREFIX(assuan_sendfd) #define assuan_receivefd _ASSUAN_PREFIX(assuan_receivefd) #define assuan_set_malloc_hooks _ASSUAN_PREFIX(assuan_set_malloc_hooks) +#define assuan_set_io_hooks _ASSUAN_PREFIX(assuan_set_io_hooks) #define assuan_set_log_stream _ASSUAN_PREFIX(assuan_set_log_stream) #define assuan_set_error _ASSUAN_PREFIX(assuan_set_error) #define assuan_set_pointer _ASSUAN_PREFIX(assuan_set_pointer) @@ -162,6 +163,7 @@ #define _assuan_simple_write _ASSUAN_PREFIX(_assuan_simple_write) #define _assuan_io_read _ASSUAN_PREFIX(_assuan_io_read) #define _assuan_io_write _ASSUAN_PREFIX(_assuan_io_write) +#define _assuan_io_hooks _ASSUAN_PREFIX(_assuan_io_hooks) #define _assuan_new_context _ASSUAN_PREFIX(_assuan_new_context) #define _assuan_release_context _ASSUAN_PREFIX(_assuan_release_context) #define _assuan_malloc _ASSUAN_PREFIX(_assuan_malloc) @@ -420,6 +422,18 @@ struct sockaddr_un #endif +/* Definition of hook functions used to conditionally replace the + default I/O functions. */ +struct assuan_io_hooks +{ + int (*read_hook)(assuan_context_t, assuan_fd_t, void *, size_t, ssize_t *); + int (*write_hook)(assuan_context_t, assuan_fd_t fd, + const void *, size_t, ssize_t *); +}; +typedef struct assuan_io_hooks *assuan_io_hooks_t; + + + /*-- assuan-handler.c --*/ int assuan_register_command (assuan_context_t ctx, const char *cmd_string, @@ -560,6 +574,7 @@ assuan_error_t assuan_receivefd (assuan_context_t ctx, assuan_fd_t *fd); 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_hooks (assuan_io_hooks_t io_hooks); void assuan_set_log_stream (assuan_context_t ctx, FILE *fp); int assuan_set_error (assuan_context_t ctx, int err, const char *text); void assuan_set_pointer (assuan_context_t ctx, void *pointer); |