Chinaunix首页 | 论坛 | 博客
  • 博客访问: 398959
  • 博文数量: 75
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2015-06-03 18:24
文章分类

全部博文(75)

文章存档

2019年(1)

2018年(20)

2017年(14)

2016年(10)

2015年(30)

分类: LINUX

2017-05-13 21:47:16


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/socket.h>
  4. #include <sys/types.h>
  5. #include <netinet/in.h>
  6. #include <stdlib.h>

  7. #define _IPV4_VAL_(addr3,addr2,addr1,addr0) ((((unsigned long)addr3 << 24) & 0xFF000000) | (((unsigned long)addr2 << 16) & 0xFF0000) | (((unsigned long)addr1 << 8) & 0xFF00) | (((unsigned long)addr0) & 0xFF))
  8. #define _VERSION_ "0321_1915_1913_5"
  9. #define _UID_ "952700000ABNIJJF"

  10. #define _HTTP_SERVER_PORT_ 80
  11. //unsigned char gUpdate_server_ip[4] = {52,20,136,123};
  12. unsigned char gUpdate_server_ip[4] = {54,172,84,104};


  13. /*
  14. http://api.reolink.com/v1.0/roms/upgrades/latest?build=0321_1915_1913_5&board=BI&type=BI&uid=952700000ABNIJJF
  15. */
  16. int Update_online(char *version, char *uid, unsigned long *update_flag)
  17. {
  18.     int ret_val = -1;
  19.     int sock_fd = -1;
  20.     int total_size = 0;
  21.     int recv_size = 0;
  22.     char *pXml_start = NULL;
  23.     socklen_t socklen = 0;
  24.     struct sockaddr_in server_addr;
  25.     char *pBuffer = NULL;
  26. #define _BUFFER_SIZE_ 512

  27.     /**/
  28.     pBuffer = (char *)malloc(_BUFFER_SIZE_);
  29.     if(pBuffer == NULL)
  30.     {
  31.         printf("malloc Failed\n");
  32.         goto _OVER_;
  33.     }

  34.     /**/
  35.     memset(&server_addr, 0, sizeof(struct sockaddr_in));
  36.     server_addr.sin_family = AF_INET;
  37.     server_addr.sin_port = htons(_HTTP_SERVER_PORT_);
  38.     server_addr.sin_addr.s_addr = htonl(_IPV4_VAL_(gUpdate_server_ip[0], gUpdate_server_ip[1], gUpdate_server_ip[2], gUpdate_server_ip[3]));


  39.     /**/
  40.     sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  41.     if(sock_fd < 0)
  42.     {
  43.         printf("socket Failed\n");
  44.         goto _OVER_;
  45.     }

  46.     /**/
  47.     socklen = sizeof(struct sockaddr_in);
  48.     ret_val = connect(sock_fd, (struct sockaddr *)&server_addr, socklen);
  49.     if(ret_val < 0)
  50.     {
  51.         printf("connect Failed\n");
  52.         goto _OVER_;
  53.     }

  54.     /**/
  55.     memset(pBuffer, 0, _BUFFER_SIZE_);
  56.     snprintf(pBuffer + strlen(pBuffer), _BUFFER_SIZE_ - strlen(pBuffer),
  57.                 "GET /v1.0/roms/upgrades/latest?build=%s&board=BI&type=BI&uid=%s HTTP/1.1\r\n", version, uid);

  58.     snprintf(pBuffer + strlen(pBuffer), _BUFFER_SIZE_ - strlen(pBuffer),
  59.                 "Host: %d.%d.&d.%d:%d\r\n", gUpdate_server_ip[0], gUpdate_server_ip[1],
  60.                 gUpdate_server_ip[2], gUpdate_server_ip[3], _HTTP_SERVER_PORT_);
  61.     snprintf(pBuffer + strlen(pBuffer), _BUFFER_SIZE_ - strlen(pBuffer), "\r\n");

  62.     printf("pBuffer:%s\n", pBuffer);
  63.     ret_val = send(sock_fd, pBuffer, strlen(pBuffer), 0);
  64.     if(ret_val != strlen(pBuffer))
  65.     {
  66.         printf("send Failed\n");
  67.         goto _OVER_;
  68.     }

  69.     total_size = 0;
  70.     while(1)
  71.     {
  72.         recv_size = recv(sock_fd, pBuffer + total_size, _BUFFER_SIZE_ - total_size - 1, 0);
  73.         printf("pBuffer:%s\n", pBuffer);
  74.         if(recv_size > 0)
  75.         {
  76.             total_size += recv_size;
  77.         }
  78.         else
  79.         {
  80.             printf("recv Failed\n");
  81.             goto _OVER_;
  82.         }

  83.         /**/
  84.         pBuffer[total_size] = '\0';

  85.         /**/
  86.         if(strstr(pBuffer, "/>") != NULL)
  87.         {
  88.             printf("Online_update_detect_ _OU_recv_ OK\n");
  89.             break;
  90.         }
  91.         
  92.         /**/
  93.         pXml_start = strstr(pBuffer, "<");
  94.         if(pXml_start != NULL)
  95.         {
  96.             if(pXml_start != pBuffer)
  97.             {
  98.                 total_size -= (pXml_start - pBuffer);
  99.                 memcpy(pBuffer, pXml_start, total_size);
  100.             }
  101.         }
  102.         else
  103.         {
  104.             total_size = 0;
  105.         }
  106.         
  107.         /**/
  108.         if((_BUFFER_SIZE_ - total_size - 1) <= 0)
  109.         {
  110.             printf("Online_update_detect_ message too long\n\r");
  111.             goto _OVER_;
  112.         }
  113.     }

  114.     /**/
  115.     if(strstr(pBuffer, "needupdate=\"1\"") != NULL)
  116.     {
  117.         *update_flag = 1;
  118.     }
  119.     else
  120.     {
  121.         *update_flag = 0;
  122.     }
  123.     
  124. _OVER_:
  125.     if(pBuffer != NULL)
  126.     {
  127.         free(pBuffer);
  128.     }
  129.     if(sock_fd >= 0)
  130.     {
  131.         close(sock_fd);
  132.     }

  133.     return ret_val;
  134. }

  135. int main(int argc,char **argv)
  136. {
  137.     unsigned long update_flag = 0;
  138.     Update_online(_VERSION_, _UID_, &update_flag);
  139.     printf("update_flag:%ld\n", update_flag);
  140.     return 0;
  141. }
运行代码的结果:
root@tay:/home/tay/code/update_online# ./a.out 
pBuffer:GET /v1.0/roms/upgrades/latest?build=0321_1915_1913_5&board=BI&type=BI&uid=952700000ABNIJJF HTTP/1.1
Host: 54.172.&d.84:104




pBuffer:HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: bc-access-key, bc-machine-code, bc-captcha, bc-captcha-id, bc-auth-code, bc-expires, bc-nonce, bc-timestamp, accept, Content-Type
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,PATCH,OPTIONS,HEAD
Access-Control-Max-Age: 1728000
Cache-Control: no-store, no-cache, must-revalidate
Content-Type: application/xml; charset=utf-8
Date: Sat, 13 May 2017 13:41:12 GMT
Expires: Mon, 26 Jul 1970 05:00:00 GMT
Server: Reolink
pBuffer: Cloud
X-Powered-By: Reolink-Cloud-API/1.0
Content-Length: 327
Connection: keep-alive


    needupdate="1"
    img_name=""
    type="BI"
    img_version="0328_1915_1945_7"
    board_name="BI"
    changelog="no change logs"
    filesize="4202064"
    crc="-898126697"
    forcerestore="1"
    burn="0"
/>Date: Sat, 13 May 2017 13:41:12 GMT
Expires: Mon, 26 Jul 1970 05:00:00 GMT
Server: Reolink
Online_update_detect_ _OU_recv_ OK
need update
update_flag:1

阅读(712) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~