moved rtp stuff to anyrtpproxy and removed it from anytun
[anytun.git] / src / cryptinit.hpp
blob567a37470965a20e686408f68c2308b5e514848b
1 #ifndef _CRYPTINIT_HPP
2 #define _CRYPTINIT_HPP
3 #ifndef NOCRYPT
5 // boost thread callbacks for libgcrypt
6 #if defined(BOOST_HAS_PTHREADS)
8 static int boost_mutex_init(void **priv)
10 boost::mutex *lock = new boost::mutex();
11 if (!lock)
12 return ENOMEM;
13 *priv = lock;
14 return 0;
17 static int boost_mutex_destroy(void **lock)
19 delete reinterpret_cast<boost::mutex*>(*lock);
20 return 0;
23 static int boost_mutex_lock(void **lock)
25 reinterpret_cast<boost::mutex*>(*lock)->lock();
26 return 0;
29 static int boost_mutex_unlock(void **lock)
31 reinterpret_cast<boost::mutex*>(*lock)->unlock();
32 return 0;
35 static struct gcry_thread_cbs gcry_threads_boost =
36 { GCRY_THREAD_OPTION_USER, NULL,
37 boost_mutex_init, boost_mutex_destroy,
38 boost_mutex_lock, boost_mutex_unlock };
39 #else
40 #error this libgcrypt thread callbacks only work with pthreads
41 #endif
44 #define MIN_GCRYPT_VERSION "1.2.0"
46 bool initLibGCrypt()
48 // make libgcrypt thread safe
49 // this must be called before any other libgcrypt call
50 gcry_control( GCRYCTL_SET_THREAD_CBS, &gcry_threads_boost );
52 // this must be called right after the GCRYCTL_SET_THREAD_CBS command
53 // no other function must be called till now
54 if( !gcry_check_version( MIN_GCRYPT_VERSION ) ) {
55 std::cout << "initLibGCrypt: Invalid Version of libgcrypt, should be >= " << MIN_GCRYPT_VERSION << std::endl;
56 return false;
59 gcry_error_t err = gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
60 if( err ) {
61 char buf[STERROR_TEXT_MAX];
62 buf[0] = 0;
63 std::cout << "initLibGCrypt: Failed to disable secure memory: " << gpg_strerror_r(err, buf, STERROR_TEXT_MAX) << std::endl;
64 return false;
67 // Tell Libgcrypt that initialization has completed.
68 err = gcry_control(GCRYCTL_INITIALIZATION_FINISHED);
69 if( err ) {
70 char buf[STERROR_TEXT_MAX];
71 buf[0] = 0;
72 std::cout << "initLibGCrypt: Failed to finish initialization: " << gpg_strerror_r(err, buf, STERROR_TEXT_MAX) << std::endl;
73 return false;
76 cLog.msg(Log::PRIO_NOTICE) << "initLibGCrypt: libgcrypt init finished";
77 return true;
80 #endif
81 #endif