SHARE
TWEET

Untitled

a guest Jun 5th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <string>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <pthread.h>
  5.  
  6. #include <mariadb/mysql.h>
  7.  
  8. #define OPENSSL_THREAD_DEFINES
  9. #include <openssl/ssl.h>
  10. #include <openssl/crypto.h>
  11. #include <openssl/opensslconf.h>
  12.  
  13. #include "config.hh"
  14.  
  15. using std::cout;
  16. using std::endl;
  17.  
  18. static std::string user, password, host;
  19. static int port = 0, nthreads = 1, nqueries = 10000;
  20.  
  21.  
  22. static pthread_mutex_t* lock = NULL;
  23.  
  24. static void ssl_locking_function(int mode, int n, const char* file, int line)
  25. {
  26.     if (mode & CRYPTO_LOCK)
  27.     {
  28.         pthread_mutex_lock(&lock[n]);
  29.     }
  30.     else
  31.     {
  32.         pthread_mutex_unlock(&lock[n]);
  33.     }
  34. }
  35.  
  36. struct CRYPTO_dynlock_value
  37. {
  38.     pthread_mutex_t lock;
  39. };
  40.  
  41. static struct CRYPTO_dynlock_value *ssl_create_dynlock(const char* file, int line)
  42. {
  43.     CRYPTO_dynlock_value* lock = new CRYPTO_dynlock_value;
  44.     pthread_mutex_init(&lock->lock, NULL);
  45.     return lock;
  46. }
  47.  
  48. static void ssl_lock_dynlock(int mode, struct CRYPTO_dynlock_value * n, const char* file, int line)
  49. {
  50.     if (mode & CRYPTO_LOCK)
  51.     {
  52.         pthread_mutex_lock(&n->lock);
  53.     }
  54.     else
  55.     {
  56.         pthread_mutex_unlock(&n->lock);
  57.     }
  58. }
  59.  
  60. static void ssl_free_dynlock(struct CRYPTO_dynlock_value * n, const char* file, int line)
  61. {
  62.     delete n;
  63. }
  64.  
  65. void ssl_threadid(CRYPTO_THREADID* id)
  66. {
  67.     CRYPTO_THREADID_set_numeric(id, pthread_self());
  68. }
  69.  
  70. void* do_test(void* data)
  71. {
  72.     mysql_thread_init();
  73.     MYSQL* conn = mysql_init(NULL);
  74.  
  75.     if (mysql_ssl_set(conn, SSL_CLIENT_KEY, SSL_CLIENT_CERT, SSL_CA, NULL, NULL) == 0 &&
  76.         mysql_real_connect(conn, host.c_str(), user.c_str(), password.c_str(), NULL, port, NULL, 0))
  77.     {
  78.         for (int i = 0; i < nqueries; i++)
  79.         {
  80.             if (mysql_query(conn, "SELECT 1") == 0)
  81.             {
  82.                 MYSQL_RES* res = mysql_store_result(conn);
  83.  
  84.                 if (res)
  85.                 {
  86.                     mysql_free_result(res);
  87.                 }
  88.             }
  89.             else
  90.             {
  91.                 break;
  92.             }
  93.         }
  94.     }
  95.     else
  96.     {
  97.         cout << mysql_error(conn) << endl;
  98.     }
  99.  
  100.     mysql_close(conn);
  101.  
  102.     return NULL;
  103. }
  104.  
  105. void prep()
  106. {
  107.     mysql_thread_init();
  108.     MYSQL* conn = mysql_init(NULL);
  109.  
  110.     if (mysql_ssl_set(conn, SSL_CLIENT_KEY, SSL_CLIENT_CERT, SSL_CA, NULL, NULL) == 0 &&
  111.         mysql_real_connect(conn, host.c_str(), user.c_str(), password.c_str(), NULL, port, NULL, 0))
  112.     {
  113.     }
  114.     else
  115.     {
  116.         cout << mysql_error(conn) << endl;
  117.     }
  118.  
  119.     mysql_close(conn);
  120. }
  121.  
  122. int main(int argc, char** argv)
  123. {
  124.     if (argc < 5)
  125.     {
  126.         cout << "Usage: " << argv[0] << " USER PASSWORD HOST PORT [THREADS]" << endl;
  127.         return 1;
  128.     }
  129.     else if (argc > 5)
  130.     {
  131.         nthreads = atoi(argv[5]);
  132.     }
  133.  
  134.     user = argv[1];
  135.     password = argv[2];
  136.     host = argv[3];
  137.     port = atoi(argv[4]);
  138.  
  139.     SSL_library_init();
  140.     SSL_load_error_strings();
  141.     OPENSSL_add_all_algorithms_noconf();
  142.     OpenSSL_add_all_ciphers();
  143.     OpenSSL_add_all_digests();
  144.  
  145.     int nlocks = CRYPTO_num_locks();
  146.     lock = new pthread_mutex_t[nlocks];
  147.     for (int i = 0; i < nlocks; i++)
  148.     {
  149.         pthread_mutex_init(&lock[i], NULL);
  150.     }
  151.  
  152.     CRYPTO_set_locking_callback(ssl_locking_function);
  153.     CRYPTO_THREADID_set_callback(ssl_threadid);
  154.  
  155.     CRYPTO_set_dynlock_create_callback(ssl_create_dynlock);
  156.     CRYPTO_set_dynlock_destroy_callback(ssl_free_dynlock);
  157.     CRYPTO_set_dynlock_lock_callback(ssl_lock_dynlock);
  158.  
  159.     prep();
  160.  
  161.     pthread_t threads[nthreads];
  162.  
  163.     for (int i = 0; i < nthreads; i++)
  164.     {
  165.         pthread_create(&threads[i], NULL, do_test, NULL);
  166.     }
  167.  
  168.     for (int i = 0; i < nthreads; i++)
  169.     {
  170.         pthread_join(threads[i], NULL);
  171.     }
  172.  
  173.     return 0;
  174. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top