libcurl获取sip号源码

相关概念参考博文

https://blog.csdn.net/qq_37059136/article/details/83820414

源码

#include "include/upload_sip.h"

#define DEBUG 0

char poststring[512] = {0};
char postbind[100] = {0};

char ip[128] = {0};
char mac[128] = {0};
char geturl[128] = {0};
char port[128] = {0};

int  timeinterval = 10; 

CURL *curlbind = NULL;
CURL *curl = NULL;

int get_ip_mac()
{

    FILE *info = NULL;
    char line[128] = {0};
     /*** 获取ip ***/
    info = popen("ifconfig br-lan|grep 'inet addr'|awk -F ':' '{print $2}'|awk '{print $1}'", "r");
    if (fgets(line, 128, info) != NULL)
    {
        sprintf(ip, "%s", line);
        ip[strlen(ip) - 1] = '\0';
    }
    /*** 获取mac地址 ***/
    info = popen("ifconfig mesh0|grep 'HWaddr'|awk -F ' ' '{print $5}'", "r");
    if (fgets(line, 128, info) != NULL)
    {
        sprintf(mac, "%s", line);
        mac[strlen(mac) - 1] = '\0';
    }                                                                                                                                                                                                       
    /*** 服务器ip ***/
    info = popen("uci get runaiot.sip.url", "r");
    if (fgets(line, 128, info) != NULL)
    {
        sprintf(geturl, "%s", line);
        geturl[strlen(geturl) - 1] = '\0';
    }
    /*** 服务端端口 ***/
    info = popen("uci get runaiot.sip.port", "r");
    if (fgets(line, 128, info) != NULL)
    {
        sprintf(port, "%s", line);
        port[strlen(port) - 1] = '\0';
    }
    pclose(info);

    return 0;
}

static size_t cb(void *data, size_t size, size_t nmemb, void *userp)
{
    size_t realsize = size * nmemb;
    struct memory *mem = (struct memory *)userp;

    char *ptr = realloc(mem->response, mem->size + realsize + 1);
    if(ptr == NULL)
        return 0;  /* Out of memory! */

    mem->response = ptr;
    memcpy(&(mem->response[mem->size]), data, realsize);
    mem->size += realsize;
    mem->response[mem->size] = 0;

    return realsize;
}

int main(void)
{
    char *sipstring = NULL;
    char *tmpp = NULL;
    int sipok = 1;
    int datalen = 100;
    int ret = -1;
    char tmp[100] = {0};

    char urlbind[50] = {0};
    char url[50] = {0};

    /*Get ip,mac,timeinterval,url*/
    get_ip_mac();

    bzero(tmp, 100);
    sprintf(tmp, "%s:%s/api/Terminal/Bind", geturl, port);
    //strcpy(tmp,"http://172.16.29.44:5060/api/Terminal/Bind");
    //strcpy(tmp,"http://172.16.29.44:5060");
    strcpy(urlbind, tmp);
    strcpy(urlbind, "http://172.16.29.70:7013/api/Terminal/Bind");

#if DEBUG
    printf("urlbind =: %s\n", urlbind);
#endif

        for(; sipok > 0; sipok--)
        {
 CURLcode curlbindRes = CURLE_OK;
            curl_global_init(CURL_GLOBAL_ALL);
            
            /* Get a curl handle */
            curlbind = curl_easy_init();

            if (!curlbind) 
            {
                sipok = sipok + 1;
                continue;
            }

            bzero(postbind, 100);
            sprintf(postbind,"{\"deviceType\": \"Single\",\"ip\": \"%s\",\"mac\": \"%s\"}", ip, mac);
#if DEBUG
            printf("postbind =: %s\n", postbind);
#endif          
            struct curl_slist *chunkbind = NULL;

            struct memory backdata = {0};
            
            /* Will be grown as needed by the realloc above */
            backdata.response = malloc(1);
            if (backdata.response == NULL)
            {
                printf("Out of memory malloc failed backdata.response \n");
                exit(-1);
            }

            /* No data at this point */
            backdata.size = 0;
/* Find target api post */
            chunkbind = curl_slist_append(chunkbind, "Content-Type: application/json;charset=utf-8");
            chunkbind = curl_slist_append(chunkbind, "Bind: text/json;charset=utf-8");                                                                                                                      
            curlbindRes = curl_easy_setopt(curlbind, CURLOPT_HTTPHEADER, chunkbind);

            if (curlbindRes != CURLE_OK)
            {
                printf("Failed to set post httpheader with 0x%x", curlbindRes);
            }

            curl_easy_setopt(curlbind, CURLOPT_URL, urlbind);

            /* Secify we want to POST data */
            curl_easy_setopt(curlbind, CURLOPT_POST, 0L);

            /* Set the expected POST size */
            curl_easy_setopt(curlbind, CURLOPT_POSTFIELDSIZE, datalen);

            /* Set the expected POST data */
            curl_easy_setopt(curlbind, CURLOPT_POSTFIELDS, postbind);
            curl_easy_setopt(curlbind, CURLOPT_SSLCERTTYPE, "PEM");
            curl_easy_setopt(curlbind, CURLOPT_CAINFO, NULL);
            curl_easy_setopt(curlbind, CURLOPT_CAPATH, NULL);

            curl_easy_setopt (curlbind,CURLOPT_SSL_VERIFYPEER, 0L);
            curl_easy_setopt (curlbind,CURLOPT_SSL_VERIFYHOST, 0L);

            curl_easy_setopt(curlbind, CURLOPT_VERBOSE, 0L);

            /* Send all data to this function */
            curl_easy_setopt(curlbind, CURLOPT_WRITEFUNCTION, cb);
            /* Ass our 'backdata' struct to the callback function */
            curl_easy_setopt(curlbind, CURLOPT_WRITEDATA, (void *)&backdata);
/* Post data buffer */
            curlbindRes = curl_easy_perform(curlbind);
            if (curlbindRes != CURLE_OK) 
            {
                sipok = sipok + 1;
                printf("curl_easy_performbind() failed: %d\n",curlbindRes);
            } 
            else 
            {
                strtok(backdata.response, ":");
                strtok(NULL, ":");
                strtok(NULL, ":");
                sipstring = strtok(NULL, ":");
                sipstring = strtok(sipstring, ",");
                sipstring = strtok(sipstring, "\"");

                if(sipstring)
                {
                    bzero(tmp, 100);
                    printf("*****sipid=%s****\n",sipstring);
                    sprintf(tmp, "uci set runaiot.sip.sipid=%s", sipstring);
                    system(tmp);
                    system("uci commit runaiot");
                }
            }

       /* Free resource */
            curl_slist_free_all(chunkbind);
            curl_easy_cleanup(curlbind);
    
            free(backdata.response);
    
            if (backdata.response != NULL)
                backdata.response = NULL;

            curl_global_cleanup();
            sleep(3);
        }
   
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值