X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fnetwork.c;h=1b6cf1ea9713e1add30e345f8f26b0ec0f486553;hb=7a762196fa7a4024c19ceac9b0c6918a5d8f62d5;hp=8a5bc6af67a02ce4770a1af14bc82433cc507610;hpb=8a9ac11ae8f0f9999f75508b4413fe5c95bfe497;p=collectd.git diff --git a/src/network.c b/src/network.c index 8a5bc6af..1b6cf1ea 100644 --- a/src/network.c +++ b/src/network.c @@ -1,6 +1,6 @@ /** * collectd - src/network.c - * Copyright (C) 2005-2010 Florian octo Forster + * Copyright (C) 2005-2013 Florian octo Forster * Copyright (C) 2009 Aman Gupta * * This program is free software; you can redistribute it and/or modify it @@ -18,7 +18,7 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian octo Forster + * Florian octo Forster * Aman Gupta **/ @@ -493,6 +493,27 @@ static int network_dispatch_notification (notification_t *n) /* {{{ */ } /* }}} int network_dispatch_notification */ #if HAVE_LIBGCRYPT +static void network_init_gcrypt (void) /* {{{ */ +{ + /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html + * Because you can't know in a library whether another library has + * already initialized the library */ + if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P)) + return; + + /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html + * To ensure thread-safety, it's important to set GCRYCTL_SET_THREAD_CBS + * *before* initalizing Libgcrypt with gcry_check_version(), which itself must + * be called before any other gcry_* function. GCRYCTL_ANY_INITIALIZATION_P + * above doesn't count, as it doesn't implicitly initalize Libgcrypt. + * + * tl;dr: keep all these gry_* statements in this exact order please. */ + gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + gcry_check_version (NULL); + gcry_control (GCRYCTL_INIT_SECMEM, 32768); + gcry_control (GCRYCTL_INITIALIZATION_FINISHED); +} /* }}} void network_init_gcrypt */ + static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */ const void *iv, size_t iv_size, const char *username) { @@ -2029,6 +2050,8 @@ static int sockent_open (sockent_t *se) /* {{{ */ { if (se->data.client.security_level > SECURITY_LEVEL_NONE) { + network_init_gcrypt (); + if ((se->data.client.username == NULL) || (se->data.client.password == NULL)) { @@ -2047,6 +2070,8 @@ static int sockent_open (sockent_t *se) /* {{{ */ { if (se->data.server.security_level > SECURITY_LEVEL_NONE) { + network_init_gcrypt (); + if (se->data.server.auth_file == NULL) { ERROR ("network plugin: Server socket with " @@ -3370,17 +3395,7 @@ static int network_init (void) have_init = 1; #if HAVE_LIBGCRYPT - /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html - * Because you can't know in a library whether another library has - * already initialized the library - */ - if (!gcry_control (GCRYCTL_ANY_INITIALIZATION_P)) - { - gcry_check_version(NULL); /* before calling any other functions */ - gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); - gcry_control (GCRYCTL_INIT_SECMEM, 32768, 0); - gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); - } + network_init_gcrypt (); #endif if (network_config_stats != 0)