CCTV: Motion и дурные IP-камеры

Привез с китая на попробовать внешнюю IP-камеру, все хорошо, и IR-подсветка будь здоров, и показывает в принципе не плохо, да вот беда — авторизация через Web-страничку. А motion умеет только через url авторизовать. Подумав недолго и посмотрев куки было решено научить motion отправлять куки из конфига. Правда на куках проблемы не кончились: китайский веб-сервер очень, очень хотел увидеть в HTTP заголовке Host соответствующий порт, если он отличается от стандартного. Тоже решилось.
motion-3.2.12 в конфиге добавлен параметр netcam_cookie в качестве значения — куки (в формате name1=value1; name2=value2)
Патч под катом

diff -ur motion-3.2.12/conf.c path/conf.c
--- motion-3.2.12/conf.c	2010-06-01 10:48:23.000000000 +0400
+++ path/conf.c	2011-10-03 16:51:51.000000000 +0400
@@ -130,6 +130,7 @@
     motionvidpipe:              NULL,
     netcam_url:                 NULL,
     netcam_userpass:            NULL,
+    netcam_cookie:              NULL,
     netcam_http:                "1.0",    /* Choices: 1.0, 1.1, or keep_alive */
     netcam_proxy:               NULL,
     netcam_tolerant_check:      0,
@@ -336,6 +337,15 @@
     copy_string,
     print_string
     },
+    {
+    "netcam_cookie",
+    "# HTTP Cookie string for network camera (only if required). Default: not defined\n"
+    "# Syntax is name1=value1;name2=value2",
+    0,
+    CONF_OFFSET(netcam_cookie),
+    copy_string,
+    print_string
+    },
     {
     "netcam_http",
     "# The setting for keep-alive of network socket, should improve performance on compatible net cameras.\n"
diff -ur motion-3.2.12/conf.h path/conf.h
--- motion-3.2.12/conf.h	2010-06-01 10:48:23.000000000 +0400
+++ path/conf.h	2011-10-03 16:48:38.000000000 +0400
@@ -105,6 +105,7 @@
     const char *motionvidpipe;
     const char *netcam_url;
     const char *netcam_userpass;
+    const char *netcam_cookie;
     const char *netcam_http;
     const char *netcam_proxy;
     unsigned int netcam_tolerant_check;
Только в path: config.h
Только в path: config.log
diff -ur motion-3.2.12/netcam.c path/netcam.c
--- motion-3.2.12/netcam.c	2010-06-01 10:48:23.000000000 +0400
+++ path/netcam.c	2011-10-03 17:36:35.000000000 +0400
@@ -69,11 +69,13 @@
 static const char    *connect_req;
 
 static const char    *connect_req_http10 = "GET %s HTTP/1.0\r\n"
-                      "Host: %s\r\n"
+                      "Host: %s%s\r\n"
+                      "%s" //cookie string
                       "User-Agent: Motion-netcam/" VERSION "\r\n";
 
 static const char    *connect_req_http11 = "GET %s HTTP/1.1\r\n"
-                      "Host: %s\r\n"
+                      "Host: %s%s\r\n"
+                      "%s"//cookie string
                       "User-Agent: Motion-netcam/" VERSION "\r\n";
 
 static const char    *connect_req_close = "Connection: close\r\n";
@@ -1751,8 +1753,19 @@
     char *userpass;                   /* temp pointer to config value */
     char *encuserpass;                /* temp storage for encoded ver */
     char *request_pass = NULL;        /* temp storage for base64 conv */
+    char *http_cookie = NULL;         /* HTTP req cookie string */
+    const char *http_cookie_header = "Cookie: %s\r\n";
+    char *http_host_port = NULL;      /* if port isn't 80 it must be in HTTP Host header */
+
     int ix;
 
+    if(url->port!=80){
+        char buff[6];
+        sprintf(buff,"%d", url->port);
+        http_host_port = malloc(1+ strlen(buff));
+        sprintf(http_host_port, ":%d", url->port);    
+    }
+
     /* First the http context structure */
     netcam->response = (struct rbuf *) mymalloc(sizeof(struct rbuf));
     memset(netcam->response, 0, sizeof(struct rbuf));
@@ -1775,6 +1788,7 @@
     else
         ptr = url->userpass;
 
+    
     /* base64_encode needs up to 3 additional chars */
     if (ptr) {
         userpass = mymalloc(strlen(ptr) + 3);
@@ -1809,6 +1823,10 @@
         free(encuserpass);
     }
 
+    if(cnt->conf.netcam_cookie){
+        http_cookie =malloc(strlen(http_cookie_header) + strlen(cnt->conf.netcam_cookie));
+        sprintf(http_cookie, http_cookie_header, cnt->conf.netcam_cookie);
+    }
     /*
      * We are now ready to set up the netcam's "connect request".  Most of
      * this comes from the (preset) string 'connect_req', but additional
@@ -1828,6 +1846,7 @@
          * Allocate space for a working string to contain the path.
          * The extra 4 is for "://" and string terminator.
          */
+        http_host_port = NULL;
         ptr = mymalloc(strlen(url->service) + strlen(url->host)
                        + strlen(url->path) + 4);
         sprintf((char *)ptr, "http://%s%s", url->host, url->path);
@@ -1847,7 +1866,7 @@
         url->path = NULL;
     }
 
-    ix += strlen(ptr);
+    ix += strlen(ptr)+strlen(http_host_port);
 
        /* Now add the required number of characters for the close header
         * or Keep-Alive header.  We test the flag which can be unset if
@@ -1873,11 +1892,11 @@
      * for the connect-request string.
      */
     netcam->connect_request = mymalloc(strlen(connect_req) + ix +
-                              strlen(netcam->connect_host));
+                              strlen(netcam->connect_host) + strlen(http_cookie));
 
     /* Now create the request string with an sprintf */
     sprintf(netcam->connect_request, connect_req, ptr,
-            netcam->connect_host); 
+            netcam->connect_host,http_host_port,http_cookie); 
 
     if (netcam->connect_keepalive)  
         strcat(netcam->connect_request, connect_req_keepalive);

Добавить комментарий