cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: a question regarding SFTP using curl

From: Zhonggui Li <zhongguili_at_gmail.com>
Date: Wed, 8 Apr 2009 11:03:11 +0200

>
>
> If you're using OpenSSL in a multithreaded program, you need to install the
> multithreaded callback functions: see
> http://www.openssl.org/docs/crypto/threads.html#DESCRIPTION
>
> >>> Dan

Thanks! I was trying the following code from an example and as far as I can
see, even with mutex locks I can't establish ssh seesion as shown below. Any
help is appreciated.

Thread 0, gets sftp://10.112.74.122/export/home/test/
Thread 1, gets sftp://10.112.74.122/export/home/test/
Thread 2, gets sftp://10.112.74.122/export/home/test/
Thread 3, gets sftp://10.112.74.122/export/home/test/
* About to connect() to 10.112.74.122 port 22 (#0)
* Trying 10.112.74.122... * About to connect() to 10.112.74.122 port 22
(#0)
* Trying 10.112.74.122... * About to connect() to 10.112.74.122 port 22
(#0)
* Trying 10.112.74.122... * About to connect() to 10.112.74.122 port 22
(#0)
* Trying 10.112.74.122... * connected
* connected
* Connected to 10.112.74.122 (10.112.74.122) port 22 (#0)
* Connected to 10.112.74.122 (10.112.74.122) port 22 (#0)
* connected
* Connected to 10.112.74.122 (10.112.74.122) port 22 (#0)
* Failure establishing ssh session
* Failure establishing ssh session
* Closing connection #0
* Failed initialization
* Failure establishing ssh session
* * Closing connection #0
* Failed initialization
Closing connection #0
* * Failed initialization
connected
* Connected to 10.112.74.122 (10.112.74.122) port 22 (#0)
* Failure establishing ssh session
* Closing connection #0
* Failed initialization
Thread 0 terminated
Thread 1 terminated
Thread 2 terminated
Thread 3 terminated

  #define USE_OPENSSL /* or USE_GNUTLS accordingly */

  #include <stdio.h>
  #include <pthread.h>
  #include <curl/curl.h>

  #define NUMT 4

  /* we have this global to let the callback get easy access to it */
  static pthread_mutex_t *lockarray;

  #ifdef USE_OPENSSL
  #include <openssl/crypto.h>
  static void lock_callback(int mode, int type, const char *file, int line)
  {
    (void)file;
    (void)line;
    if (mode & CRYPTO_LOCK) {
      pthread_mutex_lock(&(lockarray[type]));
    }
    else {
      pthread_mutex_unlock(&(lockarray[type]));
    }
  }

  static unsigned long thread_id(void)
  {
    unsigned long ret;

    ret=(unsigned long)pthread_self();
    return(ret);
  }

  static void init_locks(void)
  {
    int i;

    lockarray=(pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *
                                              sizeof(pthread_mutex_t));
    for (i=0; i<CRYPTO_num_locks(); i++) {
      pthread_mutex_init(&(lockarray[i]),NULL);
    }

    CRYPTO_set_id_callback((unsigned long (*)())thread_id);
    CRYPTO_set_locking_callback((void (*)(int, int, const char*,
int))lock_callback);
  }

  static void kill_locks(void)
  {
    int i;

    CRYPTO_set_locking_callback(NULL);
    for (i=0; i<CRYPTO_num_locks(); i++)
      pthread_mutex_destroy(&(lockarray[i]));

    OPENSSL_free(lockarray);
  }
  #endif

  #ifdef USE_GNUTLS
  #include <gcrypt.h>
  #include <errno.h>

  GCRY_THREAD_OPTION_PTHREAD_IMPL;

  void init_locks(void)
  {
    gcry_control(GCRYCTL_SET_THREAD_CBS);
  }

  #define kill_locks()
  #endif

  /* List of URLs to fetch.*/
  const char * const urls[]= {
    "sftp://10.112.74.122/export/home/test/",
    "sftp://10.112.74.122/export/home/test/",
    "sftp://10.112.74.122/export/home/test/",
    "sftp://10.112.74.122/export/home/test/"
  };
static void *pull_one_url(void *url)
{
  CURL *curl;
  curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_URL, url);
  /* this example doesn't verify the server's certificate, which means we
     might be downloading stuff from an impostor */
  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  curl_easy_perform(curl); /* ignores error */
  curl_easy_cleanup(curl);
  return NULL;
}
int main(int argc, char **argv)
{
  pthread_t tid[NUMT];
  int i;
  int error;
  (void)argc; /* we don't use any arguments in this example */
  (void)argv;
  /* Must initialize libcurl before any threads are started */
  curl_global_init(CURL_GLOBAL_ALL);
  init_locks();
  for(i=0; i< NUMT; i++) {
    error = pthread_create(&tid[i],
                           NULL, /* default attributes please */
                           pull_one_url,
                           (void *)urls[i]);
    if(0 != error)
      fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i,
error);
    else
      fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
  }
  /* now wait for all threads to terminate */
  for(i=0; i< NUMT; i++) {
    error = pthread_join(tid[i], NULL);
    fprintf(stderr, "Thread %d terminated\n", i);
  }
  kill_locks();
  return 0;
}
Received on 2009-04-08