Guest User

main.cpp - curl_easy_perform() -> segmentation fault

a guest
Apr 23rd, 2010
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.77 KB | None | 0 0
  1. /*
  2. interviewer an/abmelden, pausieren/fortfahren
  3. evtl. funktion um zu letzt angemeldeten abzumelden
  4. evtl. funktion um alle an/abzumelden
  5. -> woher session_ids und agent_log_ids?
  6.     - gespeichert in globalen variablen: session, agent_log_id
  7.     - oder via parameter übergeben an die entsprechende Funktion
  8.  
  9.     Anmerkung: in misc.cpp Funktionen zum allozieren und freigeben von Speicher
  10.    
  11.     jetzt curl_easy_init()/curl_easy_cleanup(curl) nur 1x in main(),
  12.     vorher bei jeder funktion 1x am anfang + 1x am ende
  13.    
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. /* for regex */
  20. #include "misc.h"
  21. /* for debugging */
  22. #include <assert.h>
  23. #include <signal.h>
  24. /* compile error, if included */
  25. #include "memwatch.h"
  26.  
  27. /* cURL includes */
  28. #include <curl/curl.h>
  29. #include <curl/types.h>
  30. #include <curl/easy.h>
  31.  
  32. struct MemoryStruct {
  33.     char *memory;
  34.     size_t size;
  35. };
  36.  
  37. CURL *curl;
  38. CURLcode res;
  39. struct MemoryStruct chunk;
  40. struct curl_httppost *formpost = NULL;
  41. struct curl_httppost *lastptr = NULL;
  42.  
  43. int i,n,m = 0;
  44. int choice = 0;
  45.  
  46. char *data = NULL;
  47. char *session = NULL ;
  48. char *agent_log_id = NULL;
  49. char *user = NULL;
  50. char *pass = NULL;
  51. char *phone = NULL;
  52. char *phone_pass = NULL;
  53. char *campaign = NULL;
  54. char *temp = NULL;
  55. char *temp2 = NULL;
  56. char *conf_exten = NULL;
  57. char *zap_extension = NULL;
  58.  
  59. static const char buf[] = "Expect:";
  60. static void *myrealloc(void *ptr, size_t size);
  61. static void *myrealloc(void *ptr, size_t size) {
  62.     /* There might be a realloc() out there that doesn't like reallocing
  63.      NULL pointers, so we take care of it here */
  64.     if(ptr)
  65.         return realloc(ptr, size);
  66.     else
  67.         return malloc(size);
  68. }
  69.  
  70. /* WriteMemory for retrieving the HTTP response */
  71. static size_t
  72. WriteMemory(void *ptr, size_t size, size_t nmemb, void *data) {
  73.     size_t realsize = size * nmemb;
  74.     struct MemoryStruct *mem = (struct MemoryStruct *)data;
  75.     mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
  76.     if (mem->memory) {
  77.         memcpy(&(mem->memory[mem->size]), ptr, realsize);
  78.         mem->size += realsize;
  79.         mem->memory[mem->size] = 0;
  80.     }  
  81.     return realsize;
  82. }
  83.  
  84. /*
  85. WICHTIG: phone_pass und pass werden x-fach hintereinander gehangen,
  86. somit mehrere anmeldungen noch unmöglich
  87. -> z.b. phone_pass = 400140014001
  88. */
  89. void login() {
  90.     /* 1 Mal je Transfer, mit dazugehörigem curl_easy_cleanup(curl); */
  91.     curl = curl_easy_init();
  92.     if(curl) {
  93.         printf("Welchen Interviewer moechten Sie anmelden? ");
  94.         scanf("%4s", user);
  95.         printf("\n user = ");
  96.         printf(user);
  97.         printf ("\nAn welchem Telefon moechten Sie den Interviewer anmelden? ");
  98.         scanf("%5s", phone);
  99.         strcat(data, "DB=&phone_login=");
  100.         strcat(data, phone);
  101.         strcat(data, "&phone_pass=");
  102.         /* auto get phone_pass with the help of phone (phone == phone_pass) */
  103.         /* einfach phone_pass = &phone!!! */
  104.         if (strlen(phone_pass) < 4) {
  105.             phone_pass = phone;
  106.             printf("PHONEPASS = ");
  107.             printf(phone_pass);
  108.             /*
  109.             for (i=4001; i<=4003; i++) {
  110.                 n = sprintf(temp,"%d",i);
  111.                 if (strcmp(temp,phone) == 0) {
  112.                     printf("\n");
  113.                     strcat(phone_pass,temp);
  114.                     printf("PHONEPASS = ");
  115.                     printf(phone_pass);
  116.                     printf("\n");
  117.                 }
  118.             }
  119.             */
  120.         }
  121.         strcat(data, phone_pass);
  122.         strcat(data, "&VD_login=");
  123.         strcat(data, user);
  124.         strcat(data, "&VD_pass=");
  125.         /* auto get pass with the help of user (user == pass) */
  126.         /* einfach pass = &user!!! */
  127.         if (strlen(pass) < 3) {
  128.             pass = user;
  129.             printf("PASS = ");
  130.             printf(pass);
  131.             printf("\n");
  132.             /*
  133.             for (i=101; i<=103; i++) {
  134.                 n = sprintf(temp,"%d",i);
  135.                 if (strcmp(temp,user) == 0) {
  136.                     printf("\n");
  137.                     strcat(pass,temp);
  138.                     printf("PASS = ");
  139.                     printf(pass);
  140.                     printf("\n");
  141.                 }
  142.             }
  143.             */
  144.         }
  145.         strcat(data, pass);
  146.         strcat(data, "&VD_campaign=111&SUBMIT=SUBMIT");
  147.         printf("\nDATA = ");
  148.         printf(data);
  149.         printf("\n");
  150.         /* what URL receives this POST */
  151.         curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.101.10/agc/vicidial.php");
  152.         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  153.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemory);
  154.         /* we pass our 'chunk' struct to the callback function */
  155.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
  156.         /* post away! */
  157.         res = curl_easy_perform(curl);
  158.         if(res != CURLE_OK) {
  159.             curl_easy_cleanup(curl);
  160.             printf("Error: ");
  161.             printf(curl_easy_strerror(res));
  162.             printf("\n");
  163.         }
  164.         if (chunk.memory) {
  165.             printf("\n\nETWAS IN CHUNK.MEMORY!!!!\n\n");
  166.             //printf(chunk.memory);
  167.         }
  168.         else {
  169.             printf("\n\nNICHTS IN CHUNK.MEMORY!!!!\n\n");
  170.             printf("\n\nEVTL. KEINE LEADS IN CAMPAIGN!!!!\n\n");
  171.         }
  172.        
  173.     }
  174.     /* get session from chunk.memory via regex */
  175.     session = pcre_extract(chunk.memory, "var session_name = '([0-9_]+)'" );
  176.     printf("\n\nSESSION = ");
  177.     printf(session);
  178.     printf("\n");
  179.     /* GET agent_log_id from chunk.memory */
  180.     agent_log_id = pcre_extract(chunk.memory, "var agent_log_id = '([0-9]+)'" );
  181.     printf("\n\nAGENT LOG ID = ");
  182.     printf(agent_log_id);
  183.     printf("\n");
  184.     /* var extension = '1'; */
  185.     /* GET zap_extension from chunk.memory */
  186.     zap_extension = pcre_extract(chunk.memory, "var extension = '([0-9])'" );
  187.     printf("\n\nZAP_EXTENSION = ");
  188.     printf(zap_extension);
  189.     printf("\n");
  190.     /* var session_id = '8600051'; */
  191.     /* get conf_exten(sion) from session_id */
  192.     conf_exten = pcre_extract(chunk.memory, "var session_id = '([0-9]+)'" );
  193.     printf("\n\nCONF_EXTENSION = ");
  194.     printf(conf_exten);
  195.     printf("\n");
  196.     printf("curl_easy_cleanup(curl);");
  197.     curl_easy_cleanup(curl);
  198.     printf("\n");
  199.     /* free memory */
  200.     /*
  201.     if(chunk.memory) free(chunk.memory);
  202.     if(data) free(data);
  203.     if (temp) free(temp);
  204.     */
  205. }
  206.  
  207. /* aktiviert den zuletzt angemeldeten Interviewer */
  208. /* resume und pause via JavaScript, rufen vdc_db_query.php mit entsprechenden Daten auf  */
  209. void resume () {
  210.     curl = curl_easy_init();
  211.     if(curl) {
  212.         strcat(data, "server_ip=192.168.101.10&session_name=");
  213.         if (session && agent_log_id) {
  214.             printf("SESSION = ");
  215.             printf(session);
  216.             printf("\nagent_log_id = ");
  217.             printf(agent_log_id);
  218.             printf("\n");
  219.             strcat(data, session);
  220.             strcat(data, "&ACTION=VDADready&user=");
  221.             strcat(data, user);
  222.             strcat(data, "&pass=");
  223.             strcat(data, pass);
  224.             strcat(data, "&stage=READY&agent_log_id=");
  225.             strcat(data, agent_log_id);
  226.             strcat(data, "&agent_log=undefined&wrapup=undefined&campaign=111");
  227.             printf("TEST12\n");
  228.             /* what URL receives this POST */
  229.             curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.101.10/agc/vdc_db_query.php");
  230.             curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  231.             //curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, temp2);
  232.             printf("TEST13\n");
  233.             printf("TEST14\n");
  234.             //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemory);
  235.             printf("TEST15\n");
  236.             /* we pass our 'chunk' struct to the callback function */
  237.             //curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
  238.             printf("TEST16\n");
  239.             // SEGMENTATION FAULT!!!!!!!!!!!!!11
  240.             /* post away! */
  241.             res = curl_easy_perform(curl);
  242.             if(res != CURLE_OK) {
  243.                 //curl_easy_cleanup(curl);
  244.                 printf("Error: ");
  245.                 printf(curl_easy_strerror(res));
  246.                 printf("\n");
  247.         }    
  248.             //printf("CURLOPT_ERRORBUFFER - temp2 = ");
  249.             //printf(temp2);
  250.             printf("\nTEST17\n");
  251.         }
  252.         /* session, id etc. aus vars/params oder kommandozeile holen */
  253.         else {
  254.            
  255.         }
  256.         // 1x weiter als TEST16
  257.         // Interviewer resume OK, aber double free or corruption (out): 0x0805d7c8
  258.         // curl_easy_cleanup+0x21
  259.         printf("\nTEST18\n");
  260.         printf("\n");
  261.         printf("curl_easy_cleanup(curl);");
  262.         curl_easy_cleanup(curl);
  263.         printf("\nTEST19 - nach easy cleanup in resume()\n");
  264.         printf("\n");
  265.     }
  266.    
  267. }
  268. /* aktiviert/deaktiviert interviewer mit übergebener session, user, pass, ...
  269. int resume_pause (char *session, char *user, char *pass, char *agent_log_id, char *campaign, char *action) { }
  270. */
  271.  
  272. /* deaktiviert den zuletzt angemeldeten Interviewer
  273. wie resume nur mit ACTION=VDADpause und stage=PAUSED
  274. */
  275. void pause () {
  276.     curl = curl_easy_init();
  277.     if(curl) {
  278.         strcat(data, "server_ip=192.168.101.10&session_name=");
  279.         if (session && agent_log_id) {
  280.             printf("SESSION = ");
  281.             printf(session);
  282.             printf("\nagent_log_id = ");
  283.             printf(agent_log_id);
  284.             printf("\n");
  285.             strcat(data, session);
  286.             strcat(data, "&ACTION=VDADpause&user=");
  287.             strcat(data, user);
  288.             strcat(data, "&pass=");
  289.             strcat(data, pass);
  290.             strcat(data, "&stage=PAUSED&agent_log_id=");
  291.             strcat(data, agent_log_id);
  292.             strcat(data, "&agent_log=undefined&wrapup=undefined&campaign=111");
  293.             printf("TEST12\n");
  294.             /* what URL receives this POST */
  295.             curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.101.10/agc/vdc_db_query.php");
  296.             curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  297.             //curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, temp2);
  298.             printf("TEST13\n");
  299.             printf("TEST14\n");
  300.             //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemory);
  301.             printf("TEST15\n");
  302.             /* we pass our 'chunk' struct to the callback function */
  303.             //curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
  304.             printf("TEST16\n");
  305.             /* post away! */
  306.             curl_easy_perform(curl);
  307.             res = curl_easy_perform(curl);
  308.             if(res != CURLE_OK) {
  309.                 curl_easy_cleanup(curl);
  310.                 printf("Error: ");
  311.                 printf(curl_easy_strerror(res));
  312.                 printf("\n");
  313.         }    
  314.             //printf("CURLOPT_ERRORBUFFER - temp2 = ");
  315.             //printf(temp2);
  316.             printf("\nTEST17\n");
  317.            
  318.         }
  319.         /* session, id etc. aus vars/params oder von kommandozeile holen */
  320.         else {
  321.            
  322.         }
  323.         printf("\nTEST18\n");
  324.         printf("\n");
  325.         printf("curl_easy_cleanup(curl);");
  326.         curl_easy_cleanup(curl);
  327.         printf("\nTEST19 - nach easy cleanup in pause()\n");
  328.         printf("\n");
  329.     }
  330. }
  331.  
  332. /* meldet den zuletzt angemeldeten Interviewer ab */
  333. void logout () {
  334.     curl = curl_easy_init();
  335.     if(curl) {
  336.         strcat(data, "ACTION=userLOGout&LogouTKicKAlL=1&agent_log_id=");
  337.         if (session && agent_log_id) {
  338.             printf("\nagent_log_id = ");
  339.             printf(agent_log_id);
  340.             printf("\n");
  341.             strcat(data, agent_log_id);
  342.             strcat(data, "&campaign=");
  343.             strcat(data, campaign);
  344.             strcat(data, "&conf_exten=");
  345.             // how to get conf_exten???
  346.             strcat(data, conf_exten);
  347.             strcat(data, "&enable_sipsak_messages=0&ext_context=default&extension=");
  348.             // how to get zap extension???
  349.             strcat(data, zap_extension);
  350.             strcat(data, "&format=text&no_delete_sessions=0&pass=");
  351.             strcat(data, pass);
  352.             strcat(data, "&phone_ip=&protocol=zap&server_ip=192.168.101.10&session_name=");
  353.             printf("SESSION = ");
  354.             printf(session);
  355.             strcat(data, session);
  356.             strcat(data, "&user=");
  357.             strcat(data, user);
  358.             printf("TEST12\n");
  359.             /* what URL receives this POST */
  360.             curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.101.10/agc/vicidial.php");
  361.             curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
  362.             //curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, temp2);
  363.             printf("TEST13\n");
  364.             printf("TEST14\n");
  365.             //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemory);
  366.             printf("TEST15\n");
  367.             /* we pass our 'chunk' struct to the callback function */
  368.             //curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
  369.             printf("TEST16\n");
  370.             /* post away! */
  371.             res = curl_easy_perform(curl);
  372.             if(res != CURLE_OK) {
  373.                 curl_easy_cleanup(curl);
  374.                 printf("Error: ");
  375.                 printf(curl_easy_strerror(res));
  376.                 printf("\n");
  377.             }
  378.         printf("\nTEST17\n");
  379.            
  380.         }
  381.         /* session, id etc. aus vars/params oder von kommandozeile holen */
  382.         else {
  383.            
  384.         }
  385.         printf("\nTEST18\n");
  386.         printf("\n");
  387.         printf("curl_easy_cleanup(curl);");
  388.         curl_easy_cleanup(curl);
  389.         printf("\nTEST19 - nach easy cleanup in logout()\n");
  390.         printf("\n");
  391.     }
  392. }
  393.  
  394. /*int main(int argc, char *argv[])*/
  395. int main() {   
  396.     agent_log_id = (char*)calloc(4, sizeof(char));
  397.     user = (char*)calloc(4, sizeof(char));
  398.     pass = (char*)calloc(4, sizeof(char));
  399.     phone = (char*)calloc(5, sizeof(char));
  400.     phone_pass = (char*)calloc(5, sizeof(char));
  401.     conf_exten = (char*)calloc(8, sizeof(char));
  402.     zap_extension = (char*)calloc(4, sizeof(char));
  403.     /* ein Mal je Programm mit dazugehörigem curl_global_cleanup(); */
  404.     curl_global_init(CURL_GLOBAL_ALL);
  405.     // 1 mal je transfer, somit mehrfach, z.b. 1x je funktion
  406.     //curl = curl_easy_init();
  407.     chunk.memory = NULL; /* we expect realloc(NULL, size) to work */
  408.     chunk.size = 0;    /* no data at this point */
  409.        
  410.     while (choice != 5) {
  411.        
  412.         /*chunk.memory = NULL;  we expect realloc(NULL, size) to work */
  413.         /*chunk.size = 0;     no data at this point */
  414.        
  415.         /* somit sind folgende variablen vor jedem funktionsaufruf leer */
  416.         data = (char*)calloc(100, sizeof(char));
  417.         temp = (char*)calloc(5, sizeof(char));
  418.         temp2 = (char*)calloc(500, sizeof(char));
  419.         printf ("\n\n");
  420.         printf ("----------------------");
  421.         printf ("Interviewer-Verwaltung");
  422.         printf ("----------------------\n\n");
  423.         printf ("1 - Einen Interviewer anmelden \n");
  424.         printf ("2 - Den aktuellen Interviewer als bereit melden \n");
  425.         printf ("3 - Den aktuellen Interviewer als nicht bereit melden \n");
  426.         printf ("4 - Den aktuellen Interviewer vom System abmelden \n");
  427.         printf ("----------------------\n");
  428.         printf ("5 - Programm beenden\n\n");
  429.         scanf("%d", &choice);
  430.         switch (choice) {
  431.             case 1:
  432.                 printf ("1 - Login Interviewer - gewaehlt\n");
  433.                 login();
  434.                 break;
  435.             case 2:
  436.                 printf ("2 - Resume Interviewer - gewaehlt\n");
  437.                 resume();
  438.                 break;
  439.             case 3:
  440.                 printf ("3 - Pause Interviewer - gewaehlt\n");
  441.                 pause();
  442.                 break;
  443.             case 4:
  444.                 printf ("4 - Logout Interviewer - gewaehlt\n");
  445.                 logout();
  446.                 break;
  447.             case 5:
  448.                 printf ("5 - Programm beenden - gewaehlt\n");
  449.                 break;
  450.             default:
  451.                 printf ("Keine gueltige Eingabe! \n");
  452.                 printf ("ABBRUCH! \n");
  453.                 choice = 5;
  454.         }
  455.     }
  456.     /* we're done with libcurl, so clean it up */
  457.     printf("VOR!!!!!! CURL GLOBAL CLEAN UP!\n");
  458.     if (curl) {
  459.         printf("CURL GLOBAL CLEAN UP!\n");
  460.         curl_global_cleanup();
  461.     }
  462.     return 0;
  463. }
Advertisement
Add Comment
Please, Sign In to add comment