2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
// ZoneMinder Remote Camera Class Implementation, $Date$, $Revision$
|
|
|
|
// Copyright (C) 2003 Philip Coombes
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2003-05-16 18:27:41 +08:00
|
|
|
#include "zm.h"
|
2003-03-26 19:57:29 +08:00
|
|
|
#include "zm_remote_camera.h"
|
|
|
|
|
2003-04-13 00:17:17 +08:00
|
|
|
RemoteCamera::RemoteCamera( const char *p_host, const char *p_port, const char *p_path, int p_width, int p_height, int p_palette, bool p_capture ) : Camera( REMOTE, p_width, p_height, p_palette, p_capture ), host( p_host ), port( p_port ), path( p_path )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2004-02-16 03:17:38 +08:00
|
|
|
auth = 0;
|
|
|
|
auth64[0] = '\0';
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
sd = -1;
|
|
|
|
hp = 0;
|
|
|
|
request[0] = '\0';
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
if ( capture )
|
|
|
|
{
|
|
|
|
Initialise();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteCamera::~RemoteCamera()
|
|
|
|
{
|
|
|
|
if ( capture )
|
|
|
|
{
|
|
|
|
Terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteCamera::Initialise()
|
|
|
|
{
|
2004-02-16 03:17:38 +08:00
|
|
|
if( !host )
|
|
|
|
{
|
|
|
|
Error(( "No host specified for remote get" ));
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !port )
|
|
|
|
{
|
|
|
|
Error(( "No port specified for remote get" ));
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !path )
|
|
|
|
{
|
|
|
|
Error(( "No path specified for remote get" ));
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
// Cache as much as we can to speed things up
|
2004-02-16 03:17:38 +08:00
|
|
|
char *auth_ptr = strchr( host, '@' );
|
|
|
|
|
|
|
|
if ( auth_ptr )
|
|
|
|
{
|
|
|
|
auth = host;
|
|
|
|
host = auth_ptr+1;
|
|
|
|
*auth_ptr = '\0';
|
|
|
|
Base64Encode( auth, auth64 );
|
|
|
|
}
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
if ( !hp )
|
|
|
|
{
|
|
|
|
if ( !(hp = gethostbyname(host)) )
|
|
|
|
{
|
|
|
|
Error(( "Can't gethostbyname(%s): %s", host, strerror(h_errno) ));
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
memcpy((char *)&sa.sin_addr, (char *)hp->h_addr, hp->h_length);
|
|
|
|
sa.sin_family = hp->h_addrtype;
|
|
|
|
sa.sin_port = htons(atoi(port));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !request[0] )
|
|
|
|
{
|
2003-07-04 20:31:36 +08:00
|
|
|
sprintf( request, "GET %s HTTP/%s\n", path, (const char *)config.Item( ZM_HTTP_VERSION ) );
|
|
|
|
sprintf( &(request[strlen(request)]), "User-Agent: %s/%s\n", (const char *)config.Item( ZM_HTTP_UA ), ZM_VERSION );
|
2004-02-16 03:17:38 +08:00
|
|
|
sprintf( &(request[strlen(request)]), "Host: %s\n", host );
|
|
|
|
sprintf( &(request[strlen(request)]), "Connection: Keep-Alive\n" );
|
|
|
|
if ( auth )
|
|
|
|
{
|
|
|
|
sprintf( &(request[strlen(request)]), "Authorization: Basic %s\n", auth64 );
|
|
|
|
}
|
|
|
|
sprintf( &(request[strlen(request)]), "\n" );
|
|
|
|
Info(( "Request: %s", request ));
|
2003-05-27 17:00:36 +08:00
|
|
|
Debug( 2, ( "Request: %s", request ));
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
if ( !timeout.tv_sec )
|
|
|
|
{
|
2003-07-04 20:31:36 +08:00
|
|
|
timeout.tv_sec = (int)config.Item( ZM_HTTP_TIMEOUT )/1000;
|
|
|
|
timeout.tv_usec = (int)config.Item( ZM_HTTP_TIMEOUT )%1000;
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int RemoteCamera::Connect()
|
|
|
|
{
|
|
|
|
if ( sd < 0 )
|
|
|
|
{
|
|
|
|
sd = socket(hp->h_addrtype, SOCK_STREAM, 0);
|
|
|
|
if ( sd < 0 )
|
|
|
|
{
|
|
|
|
Error(( "Can't create socket: %s", strerror(errno) ));
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( connect( sd, (struct sockaddr *)&sa, sizeof(sa) ) < 0 )
|
|
|
|
{
|
|
|
|
Error(( "Can't connect: %s", strerror(errno) ));
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Debug( 3, ( "Connected to host, socket = %d", sd ));
|
|
|
|
return( sd );
|
|
|
|
}
|
|
|
|
|
|
|
|
int RemoteCamera::Disconnect()
|
|
|
|
{
|
|
|
|
close( sd );
|
|
|
|
sd = -1;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int RemoteCamera::SendRequest()
|
|
|
|
{
|
|
|
|
if ( write( sd, request, strlen(request) ) < 0 )
|
|
|
|
{
|
|
|
|
Error(( "Can't write: %s", strerror(errno) ));
|
|
|
|
Disconnect();
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
Debug( 3, ( "Request sent" ));
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int RemoteCamera::GetHeader( const char *content, const char *header, char *value )
|
|
|
|
{
|
2003-05-27 17:00:36 +08:00
|
|
|
//char *header_string = (char *)malloc( strlen(header)+8 );
|
2003-11-05 22:49:55 +08:00
|
|
|
static char header_string[BUFSIZ];
|
2003-03-26 19:57:29 +08:00
|
|
|
strcpy( header_string, header );
|
|
|
|
strcat( header_string, ":" );
|
|
|
|
|
|
|
|
char *header_ptr = strstr( content, header_string );
|
2004-01-15 05:26:47 +08:00
|
|
|
int result = -1;
|
|
|
|
if ( header_ptr )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2004-01-15 05:26:47 +08:00
|
|
|
strcat( header_string, " %s" );
|
|
|
|
result = sscanf( header_ptr, header_string, value );
|
|
|
|
//Debug( 3, ( "R:%d, %s\n", result, value );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
return( result );
|
|
|
|
}
|
|
|
|
|
|
|
|
int RemoteCamera::GetResponse( unsigned char *&buffer, int &max_size )
|
|
|
|
{
|
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(sd, &rfds);
|
|
|
|
|
|
|
|
char *header = 0;
|
2003-06-04 05:23:16 +08:00
|
|
|
int header_length = 0;
|
|
|
|
unsigned char *content = buffer;
|
2003-03-26 19:57:29 +08:00
|
|
|
int content_length = 0;
|
|
|
|
|
2003-06-04 05:23:16 +08:00
|
|
|
char *content_ptr = 0;
|
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
struct timeval temp_timeout = timeout;
|
|
|
|
|
|
|
|
int n_found = select( sd+1, &rfds, NULL, NULL, &temp_timeout );
|
|
|
|
if( n_found == 0 )
|
|
|
|
{
|
|
|
|
Error(( "Select timed out" ));
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
else if ( n_found < 0)
|
|
|
|
{
|
|
|
|
Error(( "Select error: %s", strerror(errno) ));
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int bytes_to_read = 0;
|
|
|
|
if ( ioctl( sd, FIONREAD, &bytes_to_read ) < 0 )
|
|
|
|
{
|
|
|
|
Error(( "Can't ioctl(): %s", strerror(errno) ));
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
Debug( 3, ( "Expecting %d bytes", bytes_to_read ));
|
|
|
|
|
|
|
|
if ( bytes_to_read == 0 )
|
|
|
|
{
|
|
|
|
Debug( 3, ( "Socket closed" ));
|
|
|
|
Disconnect();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-06-04 05:23:16 +08:00
|
|
|
if ( !content_ptr )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2003-06-04 05:23:16 +08:00
|
|
|
if ( !header )
|
|
|
|
header = (char *)malloc( bytes_to_read );
|
|
|
|
else
|
|
|
|
header = (char *)realloc( header, header_length+bytes_to_read );
|
|
|
|
|
|
|
|
int n_bytes = read( sd, header+header_length, bytes_to_read );
|
2003-03-26 19:57:29 +08:00
|
|
|
if ( n_bytes < 0)
|
|
|
|
{
|
|
|
|
Error(( "Read error: %s", strerror(errno) ));
|
2003-05-27 17:00:36 +08:00
|
|
|
free( header );
|
2003-03-26 19:57:29 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
else if ( n_bytes < bytes_to_read )
|
|
|
|
{
|
|
|
|
Error(( "Incomplete read, expected %d, got %d", bytes_to_read, n_bytes ));
|
2003-05-27 17:00:36 +08:00
|
|
|
free( header );
|
2003-03-26 19:57:29 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
Debug( 3, ( "Read %d bytes of header/content", bytes_to_read ));
|
|
|
|
|
2003-06-04 05:23:16 +08:00
|
|
|
content_ptr = strstr( header, "\r\n\r\n" );
|
|
|
|
if ( !content_ptr )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2003-06-04 05:23:16 +08:00
|
|
|
header_length += bytes_to_read;
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2003-06-04 05:23:16 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*(content_ptr+2) = 0;
|
|
|
|
content_ptr += 4;
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2003-06-04 05:23:16 +08:00
|
|
|
Debug( 2, ( "Header: %s", header ));
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2003-06-04 05:23:16 +08:00
|
|
|
char version[4];
|
|
|
|
int code;
|
|
|
|
char message[64] = "";
|
|
|
|
int result = sscanf( header, "HTTP/%s %3d %[^\r\n]", version, &code, message );
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2003-06-04 05:23:16 +08:00
|
|
|
if ( result != 3 )
|
|
|
|
{
|
|
|
|
Error(( "Can't parse HTTP header" ));
|
|
|
|
free( header );
|
|
|
|
return( -1 );
|
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2003-06-04 05:23:16 +08:00
|
|
|
//printf( "R:%d, %s - %d - %s\n", result, version, code, message );
|
|
|
|
|
|
|
|
if ( code < 200 || code > 299 )
|
|
|
|
{
|
|
|
|
Error(( "Invalid response status %d: %s", code, message ));
|
|
|
|
free( header );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int expected_content_length = -1;
|
|
|
|
char header_string[32] = "";
|
|
|
|
if ( GetHeader( header, "Content-Length", header_string ) > 0 )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2003-06-04 05:23:16 +08:00
|
|
|
expected_content_length = atoi( header_string );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2003-06-04 05:23:16 +08:00
|
|
|
|
|
|
|
int excess_length = content_ptr-(header+header_length);
|
|
|
|
Debug( 3, ( "Excess length = %d", excess_length ));
|
|
|
|
content_length = bytes_to_read-excess_length;
|
|
|
|
Debug( 3, ( "Content length = %d", content_length ));
|
|
|
|
|
|
|
|
if ( content_length > max_size )
|
2003-03-26 19:57:29 +08:00
|
|
|
{
|
2003-06-04 05:23:16 +08:00
|
|
|
if ( expected_content_length > max_size )
|
|
|
|
{
|
|
|
|
max_size = expected_content_length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
max_size = 0x10000;
|
|
|
|
}
|
|
|
|
content = buffer = (unsigned char *)malloc( max_size );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2003-06-04 05:23:16 +08:00
|
|
|
memcpy( content, content_ptr, content_length );
|
|
|
|
content_ptr = (char *)(content + content_length);
|
2003-03-26 19:57:29 +08:00
|
|
|
|
2003-06-04 05:23:16 +08:00
|
|
|
free( header );
|
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( (content_length+bytes_to_read) > max_size )
|
|
|
|
{
|
|
|
|
max_size += 0x10000;
|
2003-06-04 05:23:16 +08:00
|
|
|
content = buffer = (unsigned char *)realloc( buffer, max_size );
|
|
|
|
content_ptr = (char *)buffer+content_length;
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
int n_bytes = read( sd, content_ptr, bytes_to_read );
|
|
|
|
if ( n_bytes < 0)
|
|
|
|
{
|
|
|
|
Error(( "Read error: %s", strerror(errno) ));
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
else if ( n_bytes < bytes_to_read )
|
|
|
|
{
|
|
|
|
Error(( "Incomplete read, expected %d, got %d", bytes_to_read, n_bytes ));
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
content_length += bytes_to_read;
|
|
|
|
content_ptr += bytes_to_read;
|
|
|
|
Debug( 3, ( "Read %d bytes of content, total = %d", bytes_to_read, content_length ));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return( content_length );
|
|
|
|
}
|
|
|
|
|
|
|
|
int RemoteCamera::PreCapture()
|
|
|
|
{
|
|
|
|
Connect();
|
|
|
|
if ( sd < 0 )
|
|
|
|
{
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( SendRequest() < 0 )
|
|
|
|
{
|
|
|
|
Disconnect();
|
|
|
|
return( -1 );
|
|
|
|
}
|
2003-04-07 18:56:38 +08:00
|
|
|
return( 0 );
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int RemoteCamera::PostCapture( Image &image )
|
|
|
|
{
|
|
|
|
int max_size = width*height*colours;
|
|
|
|
unsigned char *buffer = (unsigned char *)malloc( max_size );
|
|
|
|
int content_length = GetResponse( buffer, max_size );
|
|
|
|
if ( content_length < 0 )
|
|
|
|
{
|
2003-05-27 17:00:36 +08:00
|
|
|
free( buffer );
|
2003-03-26 19:57:29 +08:00
|
|
|
Disconnect();
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
image.DecodeJpeg( buffer, content_length );
|
|
|
|
|
2003-05-27 17:00:36 +08:00
|
|
|
free( buffer );
|
2003-03-26 19:57:29 +08:00
|
|
|
return( 0 );
|
|
|
|
}
|
2004-02-16 03:17:38 +08:00
|
|
|
|
|
|
|
void RemoteCamera::Base64Encode( const char *in_string, char *out_string )
|
|
|
|
{
|
|
|
|
static char base64_table[64] = { '\0' };
|
|
|
|
|
|
|
|
if ( !base64_table[0] )
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for ( char c = 'A'; c <= 'Z'; c++ )
|
|
|
|
base64_table[i++] = c;
|
|
|
|
for ( char c = 'a'; c <= 'z'; c++ )
|
|
|
|
base64_table[i++] = c;
|
|
|
|
for ( char c = '0'; c <= '9'; c++ )
|
|
|
|
base64_table[i++] = c;
|
|
|
|
base64_table[i++] = '+';
|
|
|
|
base64_table[i++] = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
int in_len = strlen( in_string );
|
|
|
|
const char *in_ptr = in_string;
|
|
|
|
char *out_ptr = out_string;
|
|
|
|
while( *in_ptr )
|
|
|
|
{
|
|
|
|
unsigned char selection = *in_ptr >> 2;
|
|
|
|
unsigned char remainder = (*in_ptr++ & 0x03) << 4;
|
|
|
|
*out_ptr++ = base64_table[selection];
|
|
|
|
|
|
|
|
if ( *in_ptr )
|
|
|
|
{
|
|
|
|
selection = remainder | (*in_ptr >> 4);
|
|
|
|
remainder = (*in_ptr++ & 0x0f) << 2;
|
|
|
|
*out_ptr++ = base64_table[selection];
|
|
|
|
|
|
|
|
if ( *in_ptr )
|
|
|
|
{
|
|
|
|
selection = remainder | (*in_ptr >> 6);
|
|
|
|
*out_ptr++ = base64_table[selection];
|
|
|
|
selection = (*in_ptr++ & 0x3f);
|
|
|
|
*out_ptr++ = base64_table[selection];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*out_ptr++ = base64_table[remainder];
|
|
|
|
*out_ptr++ = '=';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*out_ptr++ = base64_table[remainder];
|
|
|
|
*out_ptr++ = '=';
|
|
|
|
*out_ptr++ = '=';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*out_ptr = '\0';
|
|
|
|
}
|