0
votes

I am using asterisk version 13.5.0. In utils directory, I created a new module called curl_util with source code as below:

#include "asterisk/curl_utils.h"
HttpResponse * httpResponseNew()
{
    HttpResponse * response = calloc(1, sizeof(HttpResponse));

    return response;
error:
    return NULL;
}

HttpResponse * httpRequestPost(char *url, char *post_body, size_t size)
{
    struct MemoryStruct chunk;
    CURL *curl = curl_easy_init();

    chunk.memory = malloc(1);
    chunk.size = 0;

    HttpResponse *response = httpResponseNew();
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);
    curl_easy_setopt(curl, CURLOPT_POST, 1L);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_body);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, size);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HttpResponseMemoryWriter);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

    response->res = curl_easy_perform(curl);

    if (response->res != CURLE_OK)
    {
        response->OK = 0;
    }
    else
    {
        response->OK = 1;
        response->size = chunk.size;
        response->body = chunk.memory;
    }

    curl_easy_cleanup(curl);

    return response;

error:
    free(chunk.memory);
    if (curl) curl_easy_cleanup(curl);
    return response;
}

In chan_sip.c, I call above mentioned httpRequestPost function. Executing make and make install are OK. But, when starting dahdi and asterisk, I got the following error:

Error loading module chan_sip.so: undefined symbol: httpRequestPost

loader.c: module chan_sip.so could not be loaded.

Could you please give hints or advices to fix this error? Thank you in advance.

2
Maybe it has to do with module load order? see the book for info on load orderQuickDanger
Thank you. For my case, I guessed curl_util is not linked for make file to build it although in Makefile, the path to curl_util is included already. So, I tried moving the above source code to chan_sip.c then the error disappeared. However, I get another error: Error loading module chan_sip.so: undefined symbol: curl_easy_getinfo. loader.c: module chan_sip.so could not be loaded.Aloha
I wonder if asterisk has a function that parses xml content from a URL similar to function httpRequestPost() said above. Anybody know?Aloha
I would use an AGI and parse the XML outside of the dialplan. This way you could actually do the CURL request and XML parsing in a single line of dialplan, using an external script, which can send back Goto's, Dial's, etc to direct the call based on your XML.QuickDanger

2 Answers

1
votes

Doesn't need to edit the makefile. You just need to use the ASTLDFLAGS compiler option like :

./configure
make ASTLDFLAGS="-lcurl"
make install
make samples
make config
0
votes

I fixed the error by doing followings:

Remove libcurl4-openssl-dev

Download CURL from http://curl.haxx.se/, unzip, run commands ./configure, make, make install

Edit Makefile of asterisk, adding -lcurl for compile flags.