/********* Sample code generated by the curl command line tool **********
 * Lines with [REMARK] below might need to be modified to make this code 
 * usable. Add error code checking where appropriate.
 * Compile this with a suitable header include path. Then link with 
 * libcurl.
 * If you use any *_LARGE options, make sure your compiler figure
 * out the correct size for the curl_off_t variable.
 * Read the details for all curl_easy_setopt() options online on:
 * http://curlm.haxx.se/libcurl/c/curl_easy_setopt.html
 ************************************************************************/
#define _FILE_OFFSET_BITS 64 /* for pre libcurl 7.19.0 curl_off_t magic */
#include <curl/curl.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>

enum {
    HEADER,
    WRITE,
    READ
};
struct __data {
    int timeout_count;
    int callback_type;
    char function_name[128];
};
typedef struct __data data_t;

static CURL *hnd; 
static data_t timer_data, hdr_cb, write_cb, read_cb;

static gboolean pause_timeoutadd(gpointer data)
{
    data_t* timerdata = (data_t*)data;
    if(timerdata->timeout_count < 4) {
        timerdata->timeout_count++;
        g_timeout_add(1000, pause_timeoutadd, (gpointer)timerdata);
        printf("---- added the timer for 1sec again\n");
    }
    else {
        curl_easy_pause(hnd, CURLPAUSE_CONT);
        printf("---- continued the curl handle for perform \n");
  //curl_easy_perform(hnd);
    }
    return FALSE;
}

static void printstruct(data_t* curr)
{
    printf("current data_t ptr %x\ntimeout count: %d\ncallback type: %d\nfunction name: %s\n",
            curr, curr->timeout_count, curr->callback_type, curr->function_name);
}

static size_t curl_headercallback( void *ptr, size_t size, size_t nmemb, void *stream)
{
    data_t *data = (data_t *)stream;
    printf("---- came to curl header callback \n");
    //printf("---- came to curl callback with data: \n timer_count: %d\n callback type: %d\n function_name: %s\n", 
    //        data->timeout_count, data->callback_type, data->function_name);
    return size*nmemb;
}

static size_t curl_writecallback( void *ptr, size_t size, size_t nmemb, void *stream)
{
    data_t *data = (data_t *)stream;
    printf("---- came to curl callback with data: \n timer_count: %d\n callback type: %d\n function_name: %s\n", 
            data->timeout_count, data->callback_type, data->function_name);
    return size*nmemb;
}

static size_t curl_readcallback( void *ptr, size_t size, size_t nmemb, void *stream)
{
    data_t *data = (data_t *)stream;
    printf("---- came to curl callback with data: \n timer_count: %d\n callback type: %d\n function_name: %s\n", 
            data->timeout_count, data->callback_type, data->function_name);
    return size*nmemb;
}

int main(int argc, char *argv[])
{
  CURLcode ret;
  hnd = curl_easy_init();
  gtk_init(&argc, &argv);

  memset(&hdr_cb, 0, sizeof(data_t));
  hdr_cb.callback_type = HEADER;
  strcpy(hdr_cb.function_name, "curl_headercallback");
  printstruct(&hdr_cb);
  curl_easy_setopt(hnd, CURLOPT_WRITEHEADER, &hdr_cb);
  curl_easy_setopt(hnd, CURLOPT_HEADERFUNCTION, curl_headercallback);

  memset(&write_cb, 0, sizeof(data_t));
  write_cb.callback_type = WRITE;
  strcpy(write_cb.function_name, "curl_writecallback");
  printstruct(&write_cb);
  curl_easy_setopt(hnd, CURLOPT_WRITEDATA, &write_cb);
  curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, curl_writecallback);

  memset(&read_cb, 0, sizeof(data_t));
  read_cb.callback_type = READ;
  strcpy(read_cb.function_name, "curl_readcallback");
  printstruct(&read_cb);
  curl_easy_setopt(hnd, CURLOPT_READDATA, &read_cb);
  curl_easy_setopt(hnd, CURLOPT_READFUNCTION, curl_readcallback);
  /* curl_easy_setopt(hnd, CURLOPT_SEEKDATA, 0xbf95af74); [REMARK] */
  /* curl_easy_setopt(hnd, CURLOPT_SEEKFUNCTION, 0x804deb0); [REMARK] */
  curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, (curl_off_t)-1);
  curl_easy_setopt(hnd, CURLOPT_URL, "www.rediff.com");
  curl_easy_setopt(hnd, CURLOPT_PROXY, "172.17.1.16:8080");
  curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1);
  curl_easy_setopt(hnd, CURLOPT_HEADER, 0);
  curl_easy_setopt(hnd, CURLOPT_FAILONERROR, 0);
  curl_easy_setopt(hnd, CURLOPT_UPLOAD, 0);
  curl_easy_setopt(hnd, CURLOPT_DIRLISTONLY, 0);
  curl_easy_setopt(hnd, CURLOPT_APPEND, 0);
  curl_easy_setopt(hnd, CURLOPT_NETRC, 0);
  curl_easy_setopt(hnd, CURLOPT_FOLLOWLOCATION, 0);
  curl_easy_setopt(hnd, CURLOPT_UNRESTRICTED_AUTH, 0);
  curl_easy_setopt(hnd, CURLOPT_TRANSFERTEXT, 0);
  curl_easy_setopt(hnd, CURLOPT_USERPWD, NULL);
  curl_easy_setopt(hnd, CURLOPT_PROXYUSERPWD, NULL);
  curl_easy_setopt(hnd, CURLOPT_NOPROXY, NULL);
  curl_easy_setopt(hnd, CURLOPT_RANGE, NULL);
  /* curl_easy_setopt(hnd, CURLOPT_ERRORBUFFER, 0xbf95b0a4); [REMARK] */
  curl_easy_setopt(hnd, CURLOPT_TIMEOUT, 0);
  curl_easy_setopt(hnd, CURLOPT_REFERER, NULL);
  curl_easy_setopt(hnd, CURLOPT_AUTOREFERER, 0);
  curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.19.6 (i686-pc-linux-gnu) libcurl/7.19.6 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1");
  curl_easy_setopt(hnd, CURLOPT_FTPPORT, NULL);
  curl_easy_setopt(hnd, CURLOPT_LOW_SPEED_LIMIT, 0);
  curl_easy_setopt(hnd, CURLOPT_LOW_SPEED_TIME, 0);
  curl_easy_setopt(hnd, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t)0);
  curl_easy_setopt(hnd, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)0);
  curl_easy_setopt(hnd, CURLOPT_RESUME_FROM_LARGE, (curl_off_t)0);
  curl_easy_setopt(hnd, CURLOPT_COOKIE, NULL);
  curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, NULL);
  curl_easy_setopt(hnd, CURLOPT_SSLCERT, NULL);
  curl_easy_setopt(hnd, CURLOPT_SSLCERTTYPE, NULL);
  curl_easy_setopt(hnd, CURLOPT_SSLKEY, NULL);
  curl_easy_setopt(hnd, CURLOPT_SSLKEYTYPE, NULL);
  curl_easy_setopt(hnd, CURLOPT_KEYPASSWD, NULL);
  curl_easy_setopt(hnd, CURLOPT_SSH_PRIVATE_KEYFILE, NULL);
  curl_easy_setopt(hnd, CURLOPT_SSH_PUBLIC_KEYFILE, NULL);
  curl_easy_setopt(hnd, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5, NULL);
  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 2);
  curl_easy_setopt(hnd, CURLOPT_SSH_KNOWNHOSTS, "/home/azingo/.ssh/known_hosts");
  curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50);
  curl_easy_setopt(hnd, CURLOPT_CRLF, 0);
  curl_easy_setopt(hnd, CURLOPT_QUOTE, NULL);
  curl_easy_setopt(hnd, CURLOPT_POSTQUOTE, NULL);
  curl_easy_setopt(hnd, CURLOPT_PREQUOTE, NULL);
  curl_easy_setopt(hnd, CURLOPT_WRITEHEADER, NULL);
  curl_easy_setopt(hnd, CURLOPT_COOKIEFILE, NULL);
  curl_easy_setopt(hnd, CURLOPT_COOKIESESSION, 0);
  curl_easy_setopt(hnd, CURLOPT_SSLVERSION, 0);
  curl_easy_setopt(hnd, CURLOPT_TIMECONDITION, 0);
  curl_easy_setopt(hnd, CURLOPT_TIMEVALUE, 0);
  curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, NULL);
  /* curl_easy_setopt(hnd, CURLOPT_STDERR, 0xb7d21580); [REMARK] */
  curl_easy_setopt(hnd, CURLOPT_HTTPPROXYTUNNEL, 0);
  curl_easy_setopt(hnd, CURLOPT_INTERFACE, NULL);
  curl_easy_setopt(hnd, CURLOPT_KRBLEVEL, NULL);
  curl_easy_setopt(hnd, CURLOPT_TELNETOPTIONS, NULL);
  curl_easy_setopt(hnd, CURLOPT_RANDOM_FILE, NULL);
  curl_easy_setopt(hnd, CURLOPT_EGDSOCKET, NULL);
  curl_easy_setopt(hnd, CURLOPT_CONNECTTIMEOUT, 0);
  curl_easy_setopt(hnd, CURLOPT_ENCODING, NULL);
  curl_easy_setopt(hnd, CURLOPT_FTP_CREATE_MISSING_DIRS, 0);
  curl_easy_setopt(hnd, CURLOPT_IPRESOLVE, 0);
  curl_easy_setopt(hnd, CURLOPT_FTP_ACCOUNT, NULL);
  curl_easy_setopt(hnd, CURLOPT_IGNORE_CONTENT_LENGTH, 0);
  curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 0);
  curl_easy_setopt(hnd, CURLOPT_FTP_FILEMETHOD, 0);
  curl_easy_setopt(hnd, CURLOPT_FTP_ALTERNATIVE_TO_USER, NULL);
  curl_easy_setopt(hnd, CURLOPT_SSL_SESSIONID_CACHE, 1);
  /* curl_easy_setopt(hnd, CURLOPT_SOCKOPTFUNCTION, 0x804d760); [REMARK] */
  /* curl_easy_setopt(hnd, CURLOPT_SOCKOPTDATA, 0xbf95acf0); [REMARK] */
  curl_easy_setopt(hnd, CURLOPT_POSTREDIR, 0);
  
  ret = curl_easy_pause(hnd, CURLPAUSE_ALL);
  printf("---- paused the curl handle for perform \n");
  if(ret == CURLE_OK) {
      memset(&timer_data, 0, sizeof(data_t));
      timer_data.timeout_count = 1;
      printstruct(&timer_data);
      g_timeout_add(1000, pause_timeoutadd, &timer_data);
      printf("---- added the timer for 1sec \n");
      //int timer_id = g_timeout_add(1000, pause_timeoutadd, &timer_data);
      //if(timer_id)
      //    sleep(5000);
  }
  ret = curl_easy_perform(hnd);

  gtk_main();
  curl_easy_cleanup(hnd);
  return (int)ret;
}
/**** End of sample code ****/
