1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-12-05 01:02:39 +03:00

Begin of a solution for threading

This commit is contained in:
Aris Adamantiadis
2010-08-30 23:44:03 +02:00
parent e34da1b94d
commit bcc2d8474c
5 changed files with 147 additions and 9 deletions

38
include/libssh/threads.h Normal file
View File

@@ -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_ */

View File

@@ -108,6 +108,7 @@ set(libssh_SRCS
scp.c scp.c
socket.c socket.c
string.c string.c
threads.c
wrapper.c wrapper.c
) )

View File

@@ -104,13 +104,6 @@ int ssh_get_random(void *where, int len, int strong){
return 1; 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 * 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. * 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) { if (ssh_crypto_initialized == 0) {
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
gcry_check_version(NULL); gcry_check_version(NULL);
gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
if (!gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P,0)) { if (!gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P,0)) {
gcry_control(GCRYCTL_INIT_SECMEM, 4096); gcry_control(GCRYCTL_INIT_SECMEM, 4096);
gcry_control(GCRYCTL_INITIALIZATION_FINISHED,0); gcry_control(GCRYCTL_INITIALIZATION_FINISHED,0);

View File

@@ -26,6 +26,8 @@
#include "libssh/socket.h" #include "libssh/socket.h"
#include "libssh/dh.h" #include "libssh/dh.h"
#include "libssh/poll.h" #include "libssh/poll.h"
#include "libssh/threads.h"
#ifdef _WIN32 #ifdef _WIN32
#include <winsock2.h> #include <winsock2.h>
#endif #endif
@@ -49,6 +51,8 @@
* @returns 0 on success, -1 if an error occured. * @returns 0 on success, -1 if an error occured.
*/ */
int ssh_init(void) { int ssh_init(void) {
if(ssh_threads_init())
return -1;
if(ssh_crypto_init()) if(ssh_crypto_init())
return -1; return -1;
if(ssh_socket_init()) if(ssh_socket_init())

104
libssh/threads.c Normal file
View File

@@ -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;
}
/**
* @}
*/