diff options
-rw-r--r-- | include/libssh/threads.h | 38 | ||||
-rw-r--r-- | libssh/CMakeLists.txt | 1 | ||||
-rw-r--r-- | libssh/dh.c | 9 | ||||
-rw-r--r-- | libssh/init.c | 4 | ||||
-rw-r--r-- | libssh/threads.c | 104 |
5 files changed, 147 insertions, 9 deletions
diff --git a/include/libssh/threads.h b/include/libssh/threads.h new file mode 100644 index 0000000..6f82ee3 --- /dev/null +++ b/include/libssh/threads.h @@ -0,0 +1,38 @@ +/* + * This file is part of the SSH Library + * + * Copyright (c) 2010 by Aris Adamantiadis + * + * The SSH Library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at your + * option) any later version. + * + * The SSH Library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the SSH Library; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +#ifndef THREADS_H_ +#define THREADS_H_ + +typedef int (*ssh_thread_callback) (void **lock); +struct ssh_threads_callbacks_struct { + ssh_thread_callback mutex_init; + ssh_thread_callback mutex_destroy; + ssh_thread_callback mutex_lock; + ssh_thread_callback mutex_unlock; +}; + +int ssh_threads_init(void); +int ssh_init_set_threads_callbacks(struct ssh_threads_callbacks_struct + *cb); +int ssh_init_set_threads_pthreads(void); + +#endif /* THREADS_H_ */ diff --git a/libssh/CMakeLists.txt b/libssh/CMakeLists.txt index 9baea9f..b158ae6 100644 --- a/libssh/CMakeLists.txt +++ b/libssh/CMakeLists.txt @@ -108,6 +108,7 @@ set(libssh_SRCS scp.c socket.c string.c + threads.c wrapper.c ) diff --git a/libssh/dh.c b/libssh/dh.c index d6166a0..c81ed8f 100644 --- a/libssh/dh.c +++ b/libssh/dh.c @@ -104,13 +104,6 @@ int ssh_get_random(void *where, int len, int strong){ return 1; } -#ifdef HAVE_LIBGCRYPT -#include <errno.h> -#include <pthread.h> -GCRY_THREAD_OPTION_PTHREAD_IMPL; -#endif - - /* * This inits the values g and p which are used for DH key agreement * FIXME: Make the function thread safe by adding a semaphore or mutex. @@ -119,8 +112,6 @@ int ssh_crypto_init(void) { if (ssh_crypto_initialized == 0) { #ifdef HAVE_LIBGCRYPT gcry_check_version(NULL); - gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); - if (!gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P,0)) { gcry_control(GCRYCTL_INIT_SECMEM, 4096); gcry_control(GCRYCTL_INITIALIZATION_FINISHED,0); diff --git a/libssh/init.c b/libssh/init.c index 15a2aea..d8e7722 100644 --- a/libssh/init.c +++ b/libssh/init.c @@ -26,6 +26,8 @@ #include "libssh/socket.h" #include "libssh/dh.h" #include "libssh/poll.h" +#include "libssh/threads.h" + #ifdef _WIN32 #include <winsock2.h> #endif @@ -49,6 +51,8 @@ * @returns 0 on success, -1 if an error occured. */ int ssh_init(void) { + if(ssh_threads_init()) + return -1; if(ssh_crypto_init()) return -1; if(ssh_socket_init()) diff --git a/libssh/threads.c b/libssh/threads.c new file mode 100644 index 0000000..24826e1 --- /dev/null +++ b/libssh/threads.c @@ -0,0 +1,104 @@ +/* + * This file is part of the SSH Library + * + * Copyright (c) 2010 by Aris Adamantiadis + * + * The SSH Library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at your + * option) any later version. + * + * The SSH Library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the SSH Library; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/** + * @defgroup libssh_threads Threading with libssh + * @ingroup libssh + * + * Threading with libssh + * @{ + */ + +#include "libssh/priv.h" +#include "libssh/threads.h" + + +#ifdef HAVE_LIBGCRYPT +#include <errno.h> +#include <pthread.h> + +static int gcry_pthread_mutex_init (void **priv){ + int err = 0; + pthread_mutex_t *lock = malloc (sizeof (pthread_mutex_t)); + + if (!lock) + err = ENOMEM; + if (!err) + { + err = pthread_mutex_init (lock, NULL); + if (err) + free (lock); + else + *priv = lock; + } + return err; +} + +static int gcry_pthread_mutex_destroy (void **lock) { + int err = pthread_mutex_destroy ((pthread_mutex_t*)*lock); + free (*lock); + return err; +} +static int gcry_pthread_mutex_lock (void **lock) { + return pthread_mutex_lock ((pthread_mutex_t*)*lock); +} +static int gcry_pthread_mutex_unlock (void **lock){ + return pthread_mutex_unlock ((pthread_mutex_t*)*lock); +} + + +static struct gcry_thread_cbs gcrypt_threads= +{ + .option=GCRY_THREAD_OPTION_VERSION << 8 || GCRY_THREAD_OPTION_PTHREAD, + .mutex_init=gcry_pthread_mutex_init, + .mutex_destroy=gcry_pthread_mutex_destroy, + .mutex_lock=gcry_pthread_mutex_lock, + .mutex_unlock=gcry_pthread_mutex_unlock +}; + +#endif + +static struct ssh_threads_callbacks_struct *user_callbacks; +/** @internal + * @brief inits the threading with the backend cryptographic libraries + */ + +int ssh_threads_init(void){ +#ifdef HAVE_LIBGCRYPT + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcrypt_threads); +#else + + +#endif + return 0; +} + +int ssh_init_set_threads_callbacks(struct ssh_threads_callbacks_struct *cb){ + user_callbacks=cb; + return SSH_OK; +} + +int ssh_init_set_threads_pthreads(void){ + return SSH_OK; +} +/** + * @} + */ |