1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/*
* Copyright 2006-2008 Ondrej Jirman <ondrej.jirman@zonio.net>
*
* This file is part of libxr.
*
* Libxr 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 of the License, or (at your option) any
* later version.
*
* Libxr 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 libxr. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#ifndef WIN32
#include <signal.h>
#endif
#include "xr-lib.h"
#include "xr-http.h"
int xr_debug_enabled = 0;
void _xr_debug(const char* loc, const char* fmt, ...)
{
va_list ap;
char* msg;
if (fmt == NULL)
return;
va_start(ap, fmt);
msg = g_strdup_vprintf(fmt, ap);
va_end(ap);
g_printerr("%s%s\n", loc ? loc : "", msg);
g_free(msg);
}
static GMutex** _ssl_mutexes = NULL;
static int _ssl_initialized = 0;
static void _ssl_locking_callback(int mode, int type, const char *file, int line)
{
if (mode & CRYPTO_LOCK)
g_mutex_lock(_ssl_mutexes[type]);
else
g_mutex_unlock(_ssl_mutexes[type]);
}
static unsigned long _ssl_thread_id_callback()
{
unsigned long ret;
ret = (unsigned long)g_thread_self();
return ret;
}
G_LOCK_DEFINE_STATIC(init);
void xr_init()
{
int i;
if (!g_thread_supported())
g_error("xr_init(): You must call g_thread_init() to allow libxr to work correctly.");
G_LOCK(init);
if (_ssl_initialized)
{
G_UNLOCK(init);
return;
}
xr_http_init();
#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
#endif
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
/* add zlib SSL compresssion */
COMP_METHOD *cm = COMP_zlib();
if (cm->type != NID_undef)
SSL_COMP_add_compression_method(1, cm); /* 1 is DEFALTE according to the http://www.ietf.org/rfc/rfc3749.txt */
_ssl_mutexes = g_new(GMutex*, CRYPTO_num_locks());
for (i=0; i<CRYPTO_num_locks(); i++)
_ssl_mutexes[i] = g_mutex_new();
CRYPTO_set_id_callback(_ssl_thread_id_callback);
CRYPTO_set_locking_callback(_ssl_locking_callback);
_ssl_initialized = 1;
G_UNLOCK(init);
}
void xr_fini()
{
if (!_ssl_initialized)
return;
#ifndef WIN32
signal(SIGPIPE, SIG_DFL);
#endif
if (_ssl_mutexes)
{
CRYPTO_set_id_callback(NULL);
CRYPTO_set_locking_callback(NULL);
int i;
for (i=0; i<CRYPTO_num_locks(); i++)
g_mutex_free(_ssl_mutexes[i]);
g_free(_ssl_mutexes);
_ssl_mutexes = NULL;
}
_ssl_initialized = 0;
}
|