Created
April 24, 2014 16:32
-
-
Save gabonator/11260828 to your computer and use it in GitHub Desktop.
IP9100/Yoics frame grabber/uploader running on router wl500g premium (linux)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
$post = file_get_contents('php://input'); | |
if ( strlen($post) > 10 ) | |
{ | |
file_put_contents("cam", $post); | |
die(); | |
} | |
if ( $_SERVER["QUERY_STRING"] == "" ) | |
{ | |
?> | |
<html> | |
<style> | |
body { margin: 0px 0px 0px 0px; } | |
img {width:100%;} | |
</style> | |
<body> | |
<table cellspacing="0" cellpadding="0" border="0" style="width:100%;"> | |
<tr><td width="50%"><img src="" id="ima0"><img src="" id="imb0" style="display:none;"></td> | |
<td width="50%"><img src="" id="ima1"><img src="" id="imb1" style="display:none;"></td></tr> | |
<tr><td><img src="" id="ima2"><img src="" id="imb2" style="display:none;"></td> | |
<td><img src="" id="ima3"><img src="" id="imb3" style="display:none;"></td></tr> | |
</table> | |
</body> | |
<script language="javascript"> | |
var links = ["http://pub.valky.eu/online/?0", "http://pub.valky.eu/online/?1", "http://pub.valky.eu/online/?2", "http://pub.valky.eu/online/?3"] | |
setInterval("upd()", 1000); | |
var t="a"; | |
function upd() | |
{ | |
var to = t; | |
t = (t=="a") ? "b" : "a"; | |
for (var i=0; i<4; i++) | |
{ | |
document.getElementById("im"+t+i).src = links[i]+"."+Math.floor(Math.random()*100000); | |
document.getElementById("im"+t+i).obj = {i:i, t:t, o:to}; | |
document.getElementById("im"+t+i).onload = function() | |
{ | |
var sThis = "im"+this.obj.t+this.obj.i; | |
var sThat = "im"+this.obj.o+this.obj.i; | |
document.getElementById(sThat).style.display = "none"; | |
document.getElementById(sThis).style.display = ""; | |
}; | |
} | |
} | |
</script> | |
</html> | |
<? | |
die(); | |
} | |
$f = fopen("cam", "r"); | |
$token = fread($f, 8); | |
if ( $token != "natorG10" ) | |
die(); | |
$timestamp = fread($f, 32); | |
$nCount = getDW( fread($f, 4) ); | |
$arrLength = array(); | |
$arrSeek = array(); | |
for ($i=0; $i<$nCount; $i++) | |
{ | |
$arrSeek[$i] = $nTotal; | |
$curSize = getDW( fread($f, 4 ) ); | |
$nTotal += $curSize; | |
$arrLength[$i] = $curSize; | |
} | |
header('Content-Type: image/jpeg'); | |
$i = floor($_SERVER["QUERY_STRING"]); | |
fseek($f, $arrSeek[$i], SEEK_CUR); | |
echo ( fread($f, $arrLength[$i]) ); | |
fclose($f); | |
function getDW( $dw ) | |
{ | |
$aux = 0; | |
$aux |= ord($dw[3]); $aux <<= 8; | |
$aux |= ord($dw[2]); $aux <<= 8; | |
$aux |= ord($dw[1]); $aux <<= 8; | |
$aux |= ord($dw[0]); | |
return $aux; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <sys/vfs.h> | |
#include <sys/stat.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <time.h> | |
#include <signal.h> | |
#define FILE_STAT "/tmp/mnt/flash/recorder/frames/recorder.log" | |
#define SRC_LINK0 "http://192.168.1.55/usr/yoics0.jpg" | |
#define SRC_LINK1 "http://192.168.1.55/usr/yoics1.jpg" | |
#define SRC_LINK2 "http://192.168.1.55/usr/yoics2.jpg" | |
#define SRC_LINK3 "http://192.168.1.55/usr/yoics3.jpg" | |
#define PATH_ARCHIVE "/tmp/mnt/flash/recorder/frames/" | |
#define DIVIDE0 1 | |
#define DIVIDE1 1 | |
#define DIVIDE2 2 | |
#define DIVIDE3 2 | |
#define INTERVAL 5 | |
#define ERROR(m) {printf(m); printf("\n"); ADDSTAT(m) } | |
#define ADDSTAT(m) { man_statopen(); fprintf( man_stat, "%s %s\n", man_timestamp(), m ); man_statclose(); } | |
#define DBG(m) printf("%s\n", m) | |
#define DBR(m) DBG(m) | |
#define DEBUG(d) {d} | |
#define DBG0(d) | |
#define DEBUG0(d) | |
int gTerminate = 0; | |
FILE *man_stat; | |
char *man_timestamp(); | |
int man_statopen(); | |
int man_statclose(); | |
void tim_format(char *name, char *fmt); | |
int http_open( char *strRemote ) | |
{ | |
struct sockaddr_in serv_name; | |
int sockd; | |
int status = 0; | |
/* parse link */ | |
char ip[32]; | |
char *slash; | |
strcpy( ip, strRemote+7 ); /* skip "http://" */ | |
slash = strstr( ip, "/" ); | |
if (slash) | |
*slash = 0; | |
/* create socket */ | |
sockd = socket(AF_INET, SOCK_STREAM, 0); | |
if (sockd == -1) | |
{ | |
ERROR("Socket creation"); | |
exit(1); | |
} | |
/* server address */ | |
serv_name.sin_family = AF_INET; | |
inet_aton(ip, &serv_name.sin_addr); | |
serv_name.sin_port = htons(80); | |
/* connect to the server */ | |
status = connect(sockd, (struct sockaddr*)&serv_name, sizeof(serv_name)); | |
if (status == -1) | |
{ | |
ERROR("Connection error"); | |
return 0; | |
} | |
return sockd; | |
} | |
int http_read(int sock, char *buf, int len) | |
{ | |
int rlen = read(sock, buf, len); | |
if ( rlen > len ) | |
rlen = len; | |
return rlen; | |
} | |
int http_skipheader(int sock) | |
{ | |
int i; | |
char ch; | |
char fifo[4] = {0}; | |
for (i=0; i<512; i++) | |
{ | |
read( sock, &ch, 1 ); | |
fifo[0] = fifo[1]; | |
fifo[1] = fifo[2]; | |
fifo[2] = fifo[3]; | |
fifo[3] = ch; | |
if (fifo[0] == '\r' && fifo[1] == '\n' && fifo[2] == '\r' && fifo[3] == '\n') | |
return 1; | |
} | |
return 0; | |
} | |
int http_request( int sockd, char *strRemote ) | |
{ | |
char query[512]; | |
/* parse link */ | |
char ip[32]; | |
char *path; | |
char *slash; | |
int ret; | |
strcpy( ip, strRemote+7 ); | |
slash = strstr( ip, "/" ); | |
if (slash) | |
*slash = 0; | |
path = strstr( strRemote+7, "/" ); | |
strcpy( query, "GET " ); | |
strcat( query, path ); | |
strcat( query, " HTTP/1.0\r\nHost: " ); | |
strcat( query, strRemote ); | |
strcat( query, "\r\n\r\n" ); | |
DBG0("http_request:write"); | |
ret = write( sockd, query, strlen(query) ); | |
DBG0("http_request:skipheader"); | |
http_skipheader( sockd ); | |
return 1; | |
} | |
void http_close( int handle ) | |
{ | |
close( handle ); | |
} | |
int tar_padright( char *f, char *string, char pad, int len ) | |
{ | |
int stringlen = strlen(string); | |
memset( f, pad, len ); | |
memcpy( f, string, stringlen ); | |
return len; | |
} | |
int tar_checksum(char *buf) | |
{ | |
int chksum = 0; | |
int i; | |
for (i=0; i<512; i++) | |
chksum += buf[i]; | |
chksum += ' ' * 8; | |
return chksum; | |
} | |
fpos_t tar_pushpos; | |
//long tar_pushpos; | |
void tar_writedummy( FILE *f ) | |
{ | |
char header[512] = {0}; | |
fwrite( header, 512, 1, f ); | |
} | |
void tar_push( FILE *f ) | |
{ | |
fgetpos (f,&tar_pushpos); | |
} | |
void tar_pop( FILE *f ) | |
{ | |
fsetpos (f,&tar_pushpos); | |
} | |
void tar_rewind( FILE *f ) | |
{ | |
fseek( f, -512, SEEK_END ); | |
} | |
void tar_writeheader( FILE *f, char *filename, int size ) | |
{ | |
char header[512] = {0}; | |
int mode = 0100644; | |
int user_id = 0000764; | |
int group_id = 0001040; | |
unsigned long timestamp = 011065464715; | |
char user_name[] = "Administrator"; | |
char group_name[] = "Administrators"; | |
time_t t; | |
time (&t); | |
timestamp = t; | |
sprintf( header+0, "%s", filename ); | |
sprintf( header+100, "%07o", mode ); | |
sprintf( header+108, "%07o", user_id ); | |
sprintf( header+116, "%07o", group_id ); | |
sprintf( header+124, "%011o", size ); | |
sprintf( header+136, "%011o", (unsigned int)timestamp ); | |
sprintf( header+257, "ustar " ); | |
sprintf( header+265, user_name ); | |
sprintf( header+281, group_name ); | |
header[156] = '0'; | |
sprintf( header+148, "%06o", tar_checksum(header) ); | |
header[155] = ' '; | |
fwrite( header, 512, 1, f ); | |
} | |
void tar_finish(FILE *f, int nlen) | |
{ | |
char header[512] = {0}; | |
int nBlock = nlen & 511; | |
if (nBlock != 0) | |
{ | |
nBlock = 512-nBlock; | |
fwrite( header, nBlock, 1, f ); | |
} | |
fwrite( header, 512, 1, f ); | |
} | |
FILE *tar_open(char *name) | |
{ | |
FILE *f = fopen(name, "r+"); | |
if ( !f ) | |
f = fopen(name, "w+"); | |
return f; | |
} | |
void tar_close(FILE *f) | |
{ | |
fclose(f); | |
} | |
void regHand(int signalType, void (*func)(int)) | |
{ | |
struct sigaction sa; | |
sa.sa_handler = func; | |
sa.sa_flags = 0; | |
sa.sa_restorer = NULL; | |
sigaction(signalType, &sa, NULL); | |
} | |
void handler(int status) | |
{ | |
ERROR("TERM signal received"); | |
//man_clean(); | |
//exit(1); | |
gTerminate = 1; | |
} | |
// fs | |
#define FS_HANDLES 8 | |
typedef char _fs_name[128]; | |
_fs_name fs_names[FS_HANDLES]; | |
FILE *fs_handles[FS_HANDLES]; | |
int fs_init() | |
{ | |
int i; | |
for (i=0; i<FS_HANDLES; i++) | |
{ | |
memset( fs_names[i], 0, sizeof(_fs_name) ); | |
fs_handles[i] = NULL; | |
} | |
return 1; | |
} | |
int fs_cleanup() | |
{ | |
int i; | |
for (i=0; i<FS_HANDLES; i++) | |
{ | |
if (fs_handles[i]) | |
{ | |
fclose( fs_handles[i] ); | |
fs_handles[i] = NULL; | |
} | |
} | |
return 1; | |
} | |
FILE *fs_open( char *name ) | |
{ | |
int i; | |
for (i=0; i<FS_HANDLES; i++) | |
{ | |
if (fs_handles[i]) | |
{ | |
if ( strcmp( name, fs_names[i] ) == 0 ) | |
return fs_handles[i]; | |
} else | |
break; | |
} | |
if (i<FS_HANDLES) | |
{ | |
fs_handles[i] = tar_open(name); | |
strcpy(fs_names[i], name); | |
return fs_handles[i]; | |
} | |
ERROR("no mem to alloc new file"); | |
return NULL; | |
} | |
long fs_free(char *path) | |
{ | |
struct stat stst; | |
struct statfs stfs; | |
long llSize; | |
if ( stat( path, &stst ) == -1 ) | |
return 0; | |
if ( statfs( path, &stfs ) == -1 ) | |
return 0; | |
// printf("bavail=%d, blksize=%d, MB free ", stfs.f_bavail, stst.st_blksize); | |
llSize = stfs.f_bavail >> 10; | |
llSize *= stst.st_blksize; | |
// printf("%d\n", (int)llSize); | |
return (long) llSize; | |
//tootal = stfs.f_blocks * ( stst.st_blksize / fKB ); | |
} | |
// man | |
char g_buffer[256]; | |
char g_lastfile[256]; | |
char g_lasttar[256]; | |
char man_timestampbuf[32]; | |
char *man_timestamp() | |
{ | |
tim_format( man_timestampbuf, "%Y/%m/%d %H:%M:%S" ); | |
return man_timestampbuf; | |
} | |
int man_appendfile( char *weblink, char *tararchive, char *saveas ) | |
{ | |
int fweb = 0; | |
FILE *ftar = NULL; | |
long lSize = 0; | |
long lRead = 0; | |
tim_format( g_lasttar, tararchive ); | |
DEBUG0( printf("Append: '%s' into '%s' as '%s'\n", weblink, g_lasttar, saveas); ); | |
ftar = fs_open( g_lasttar ); | |
if (!ftar) | |
{ | |
ERROR("cant lookup tar archive"); | |
return 0; | |
} | |
tar_rewind(ftar); | |
tar_push(ftar); | |
tar_writedummy(ftar); | |
fweb = http_open( weblink ); | |
if (!http_request( fweb, weblink )) | |
{ | |
ERROR("http request error"); | |
return 0; | |
} | |
while( ( lRead = http_read( fweb, g_buffer, 256) ) > 0 ) | |
{ | |
fwrite( g_buffer, lRead, 1, ftar ); | |
lSize += lRead; | |
} | |
tar_finish( ftar, lSize ); | |
tar_pop(ftar); | |
tim_format( g_lastfile, saveas ); | |
tar_writeheader( ftar, g_lastfile, lSize ); | |
http_close(fweb); | |
return 1; | |
} | |
int man_init() | |
{ | |
if ( !man_statopen() ) | |
return 0; | |
man_statclose(); | |
ADDSTAT("Starting"); | |
g_lastfile[0] = 0; | |
return fs_init(); | |
} | |
int man_cleanup() | |
{ | |
// if (man_stat) | |
// fclose(man_stat); | |
// man_stat = NULL; | |
ADDSTAT("Stopping"); | |
return fs_cleanup(); | |
} | |
int man_statopen() | |
{ | |
man_stat = fopen(FILE_STAT, "a"); | |
if (!man_stat) | |
return 0; | |
return 1; | |
} | |
int man_statclose() | |
{ | |
fclose(man_stat); | |
man_stat = NULL; | |
return 1; | |
} | |
// tim | |
char tim_lasthour[32] = ""; | |
char tim_lastday[32] = ""; | |
int tim_passed(char *old, char *format) | |
{ | |
char current[32]; | |
tim_format(current, format); | |
if ( old[0] ) | |
{ | |
if ( strcmp( old, current ) == 0 ) | |
return 0; | |
else | |
{ | |
strcpy( old, current ); | |
return 1; | |
} | |
} | |
strcpy( old, current ); | |
return 0; | |
} | |
void tim_format(char *name, char *fmt) | |
{ | |
time_t t; | |
struct tm *tmp; | |
t = time(NULL); | |
tmp = localtime(&t); | |
if (tmp == NULL) { | |
ERROR("localtime"); | |
exit(1); | |
} | |
if ( !fmt ) | |
{ | |
if (strftime(name, 64, "%Y%m%d_%H%M%S.jpg", tmp) == 0) | |
{ | |
ERROR("strftime returned 0"); | |
} | |
} else | |
if (strftime(name, 64, fmt, tmp) == 0) | |
{ | |
ERROR("strftime returned 0"); | |
} | |
} | |
int main(int argc, char* argv[]) | |
{ | |
int i = 0; | |
char lastDay[32]; | |
regHand(SIGTERM, handler); | |
regHand(SIGINT, handler); | |
if (!man_init()) | |
{ | |
printf("Cannot start\n"); | |
return 1; | |
} | |
DEBUG( printf("starting, free %d MB\n", (int)( fs_free(PATH_ARCHIVE)) ); ); | |
while (!gTerminate) | |
{ | |
DEBUG0( printf("pass %d, free %d MB\n", i, (int)( fs_free(PATH_ARCHIVE)) ); ); | |
i++; | |
if ( fs_free(PATH_ARCHIVE) < 128 ) | |
{ | |
ERROR("free space < 128 MB"); | |
break; | |
} | |
if ( tim_passed( tim_lasthour, "%H" ) ) | |
{ | |
fs_cleanup(); | |
strcpy( lastDay, tim_lastday ); | |
//ADDSTAT("dbg hour passed"); | |
if ( tim_passed( tim_lastday, "%Y%m%d" ) ) | |
{ | |
man_statopen(); | |
fprintf( man_stat, "%s Finished %s\n", man_timestamp(), lastDay ); | |
man_statclose(); | |
} | |
} | |
if ( (i % DIVIDE0) == 0 ) | |
if ( !man_appendfile( SRC_LINK0, PATH_ARCHIVE "%Y%m%d_a.tar", "%Y%m%d_%H%M%S.jpg" ) ) | |
break; | |
if ( (i % DIVIDE1) == 0 ) | |
if ( !man_appendfile( SRC_LINK1, PATH_ARCHIVE "%Y%m%d_b.tar", "%Y%m%d_%H%M%S.jpg" ) ) | |
break; | |
if ( (i % DIVIDE2) == 0 ) | |
if ( !man_appendfile( SRC_LINK2, PATH_ARCHIVE "%Y%m%d_c.tar", "%Y%m%d_%H%M%S.jpg" ) ) | |
break; | |
if ( (i % DIVIDE3) == 0 ) | |
if ( !man_appendfile( SRC_LINK3, PATH_ARCHIVE "%Y%m%d_d.tar", "%Y%m%d_%H%M%S.jpg" ) ) | |
break; | |
sleep(INTERVAL); | |
if (gTerminate) | |
break; | |
} | |
man_cleanup(); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <sys/vfs.h> | |
#include <sys/stat.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <time.h> | |
#include <signal.h> | |
#include <netdb.h> | |
//#define TARGET_LINK "http://192.168.1.2/gabo/cam/index.php" | |
#define TARGET_LINK "http://pub.valky.eu/online/index.php" | |
#define FILE_STAT "/tmp/mnt/flash/devel/camrec.log" | |
#define SRC_LINK0 "http://192.168.1.55/usr/yoics0.jpg" | |
#define SRC_LINK1 "http://192.168.1.55/usr/yoics1.jpg" | |
#define SRC_LINK2 "http://192.168.1.55/usr/yoics2.jpg" | |
#define SRC_LINK3 "http://192.168.1.55/usr/yoics3.jpg" | |
#define PATH_ARCHIVE "/tmp/mnt/flash/recorder/frames/" | |
#define DIVIDE0 1 | |
#define DIVIDE1 1 | |
#define DIVIDE2 2 | |
#define DIVIDE3 2 | |
#define INTERVAL 2 | |
#define ERROR(m) {printf(m); printf("\n"); ADDSTAT(m) } | |
#define ADDSTAT(m) { man_statopen(); fprintf( man_stat, "%s %s\n", man_timestamp(), m ); man_statclose(); } | |
#define DBG(m) printf("%s\n", m) | |
#define DBR(m) DBG(m) | |
#define DEBUG(d) {d} | |
#define DBG0(d) | |
#define DEBUG0(d) | |
int gTerminate = 0; | |
FILE *man_stat; | |
char *man_timestamp(); | |
int man_statopen(); | |
int man_statclose(); | |
void tim_format(char *name, char *fmt); | |
struct in_addr *atoaddr(char *address) { | |
struct hostent *host; | |
static struct in_addr saddr; | |
/* First try it as aaa.bbb.ccc.ddd. */ | |
saddr.s_addr = inet_addr(address); | |
if (saddr.s_addr != -1) { | |
return &saddr; | |
} | |
host = gethostbyname(address); | |
if (host != NULL) { | |
return (struct in_addr *) *host->h_addr_list; | |
} | |
return NULL; | |
} | |
int http_open( char *strRemote ) | |
{ | |
struct sockaddr_in serv_name; | |
int sockd; | |
int status = 0; | |
/* parse link */ | |
char ip[32]; | |
char *slash; | |
strcpy( ip, strRemote+7 ); /* skip "http://" */ | |
slash = strstr( ip, "/" ); | |
if (slash) | |
*slash = 0; | |
// hostent host = gethostbyname(ip); | |
struct in_addr host = *atoaddr(ip); | |
/* create socket */ | |
sockd = socket(AF_INET, SOCK_STREAM, 0); | |
if (sockd == -1) | |
{ | |
ERROR("Socket creation"); | |
exit(1); | |
} | |
/* server address */ | |
serv_name.sin_family = AF_INET; | |
// inet_aton(ip, &serv_name.sin_addr); | |
serv_name.sin_addr = host; | |
serv_name.sin_port = htons(80); | |
/* connect to the server */ | |
status = connect(sockd, (struct sockaddr*)&serv_name, sizeof(serv_name)); | |
if (status == -1) | |
{ | |
ERROR("Connection error"); | |
return 0; | |
} | |
return sockd; | |
} | |
int http_read(int sock, char *buf, int len) | |
{ | |
int rlen = read(sock, buf, len); | |
if ( rlen > len ) | |
rlen = len; | |
return rlen; | |
} | |
int http_write(int sock, char *buf, int len) | |
{ | |
return write(sock, buf, len); | |
} | |
int http_contentlength = 0; | |
int http_skipheader(int sock) | |
{ | |
int i; | |
char ch; | |
char fifo[4] = {0}; | |
http_contentlength = 0; | |
for (i=0; i<512; i++) | |
{ | |
read( sock, &ch, 1 ); | |
fifo[0] = fifo[1]; | |
fifo[1] = fifo[2]; | |
fifo[2] = fifo[3]; | |
fifo[3] = ch; | |
if ( memcmp(fifo, "gth:", 4) == 0 ) | |
{ | |
char strLen[16]; | |
char *plen = strLen; | |
do { | |
read( sock, &ch, 1); | |
*plen++ = ch; | |
} while (ch != '\r' && ch != '\n'); | |
plen[-1] = 0; | |
http_contentlength = atoi(strLen); | |
} | |
if (fifo[0] == '\r' && fifo[1] == '\n' && fifo[2] == '\r' && fifo[3] == '\n') | |
return 1; | |
} | |
return 0; | |
} | |
int http_get( int sockd, char *strRemote ) | |
{ | |
char query[512]; | |
/* parse link */ | |
char ip[32]; | |
char *path; | |
char *slash; | |
int ret; | |
strcpy( ip, strRemote+7 ); | |
slash = strstr( ip, "/" ); | |
if (slash) | |
*slash = 0; | |
path = strstr( strRemote+7, "/" ); | |
strcpy( query, "GET " ); | |
strcat( query, path ); | |
strcat( query, " HTTP/1.0\r\nHost: " ); | |
strcat( query, strRemote ); | |
strcat( query, "\r\n\r\n" ); | |
DBG0("http_request:write"); | |
ret = write( sockd, query, strlen(query) ); | |
DBG0("http_request:skipheader"); | |
http_skipheader( sockd ); | |
return 1; | |
} | |
void http_close( int handle ) | |
{ | |
close( handle ); | |
} | |
void regHand(int signalType, void (*func)(int)) | |
{ | |
struct sigaction sa; | |
sa.sa_handler = func; | |
sa.sa_flags = 0; | |
sa.sa_restorer = NULL; | |
sigaction(signalType, &sa, NULL); | |
} | |
void handler(int status) | |
{ | |
ERROR("TERM signal received"); | |
//man_clean(); | |
//exit(1); | |
gTerminate = 1; | |
} | |
char g_buffer[256]; | |
char g_lastfile[256]; | |
char g_lasttar[256]; | |
char man_timestampbuf[32]; | |
char *man_timestamp() | |
{ | |
tim_format( man_timestampbuf, "%Y/%m/%d %H:%M:%S" ); | |
return man_timestampbuf; | |
} | |
int man_init() | |
{ | |
if ( !man_statopen() ) | |
return 0; | |
man_statclose(); | |
ADDSTAT("Starting"); | |
g_lastfile[0] = 0; | |
return 1; | |
} | |
int man_cleanup() | |
{ | |
// if (man_stat) | |
// fclose(man_stat); | |
// man_stat = NULL; | |
ADDSTAT("Stopping"); | |
return 1; | |
} | |
int man_statopen() | |
{ | |
man_stat = fopen(FILE_STAT, "a"); | |
if (!man_stat) | |
return 0; | |
return 1; | |
} | |
int man_statclose() | |
{ | |
fclose(man_stat); | |
man_stat = NULL; | |
return 1; | |
} | |
// tim | |
void tim_format(char *name, char *fmt) | |
{ | |
time_t t; | |
struct tm *tmp; | |
t = time(NULL); | |
tmp = localtime(&t); | |
if (tmp == NULL) { | |
ERROR("localtime"); | |
exit(1); | |
} | |
if ( !fmt ) | |
{ | |
if (strftime(name, 64, "%Y%m%d_%H%M%S.jpg", tmp) == 0) | |
{ | |
ERROR("strftime returned 0"); | |
} | |
} else | |
if (strftime(name, 64, fmt, tmp) == 0) | |
{ | |
ERROR("strftime returned 0"); | |
} | |
} | |
int http_post( int sockd, char *strRemote, int nLength ) | |
{ | |
char host[128]; | |
char query[512]; | |
/* parse link */ | |
char ip[32]; | |
char *path; | |
char *slash; | |
int ret; | |
strcpy( ip, strRemote+7 ); | |
slash = strstr( ip, "/" ); | |
if (slash) | |
*slash = 0; | |
path = strstr( strRemote+7, "/" ); | |
memcpy(host, strRemote, path-strRemote); | |
host[path-strRemote] = 0; | |
sprintf(query, | |
"POST %s HTTP/1.1\r\n" | |
"Host: %s\r\n" | |
"Content-Type: application/x-www-form-urlencoded\r\n" | |
"Content-Length: %d\r\n" | |
"\r\n", | |
path, host+7, nLength); | |
DBG0("http_request:write"); | |
ret = write( sockd, query, strlen(query) ); | |
//printf("{%s}\n", query); | |
// DBG0("http_request:skipheader"); | |
// http_skipheader( sockd ); | |
return 1; | |
} | |
int man_transfer( char *weblink ) | |
{ | |
int fweb_get[5] = {0}; | |
int fweb_post = 0; | |
long lRead = 0; | |
int lengths[5]; | |
int total = 0; | |
int targetlen = 0; | |
int i; | |
int count; | |
#define bufsize 1024*128 | |
char buf[bufsize]; | |
char* source[] = {SRC_LINK0, SRC_LINK1, SRC_LINK2, SRC_LINK3, 0}; | |
fweb_post = http_open( TARGET_LINK ); | |
if ( !fweb_post) | |
{ | |
ERROR("cannot contact post server"); | |
sleep(30); | |
return 1; | |
} | |
for (i=0; source[i]; i++) | |
{ | |
fweb_get[i] = http_open( source[i] ); | |
if (!fweb_get[i]) | |
{ | |
ERROR("http open error"); | |
sleep(30); | |
http_close(fweb_post); | |
while (--i>0) | |
http_close(fweb_get[i]); | |
return 1; | |
} | |
if (!http_get( fweb_get[i], source[i] )) | |
{ | |
ERROR("http get request error"); | |
sleep(30); | |
http_close(fweb_post); | |
while (--i>0) | |
http_close(fweb_get[i]); | |
return 1; | |
} | |
lengths[i] = http_contentlength; | |
total += http_contentlength; | |
} | |
count = i; | |
// char buf[256]; | |
char* strToken = "natorG10"; | |
targetlen = strlen(strToken) + 4 + count * 4 + total+32; | |
if (!http_post( fweb_post, TARGET_LINK, targetlen )) | |
{ | |
ERROR("http post request error"); | |
sleep(30); | |
return 1; | |
} | |
char* pbuf=buf; | |
memcpy(pbuf, strToken, strlen(strToken)); pbuf += strlen(strToken); | |
memset(man_timestampbuf, 0, sizeof(man_timestampbuf)); | |
man_timestamp(); | |
memcpy(pbuf, man_timestampbuf, 32); pbuf+=32; | |
memcpy(pbuf, &count, 4); pbuf+=4; | |
for (i=0; source[i]; i++) | |
{ | |
memcpy(pbuf, &lengths[i], 4); pbuf+=4; | |
} | |
http_write( fweb_post, buf, pbuf-buf ); | |
for (i=0; source[i]; i++) | |
{ | |
while( ( lRead = http_read( fweb_get[i], buf, bufsize) ) > 0 ) | |
{ | |
http_write( fweb_post, buf, lRead ); | |
} | |
http_close(fweb_get[i]); | |
} | |
/* | |
printf("post reply='"); | |
while( ( lRead = http_read( fweb_post, g_buffer, 256) ) > 0 ) | |
{ | |
fwrite( g_buffer, lRead, 1, stdout ); | |
} | |
printf("'\n"); | |
*/ | |
http_close(fweb_post); | |
return 1; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
regHand(SIGTERM, handler); | |
regHand(SIGINT, handler); | |
if (!man_init()) | |
{ | |
printf("Cannot start\n"); | |
return 1; | |
} | |
DEBUG( printf("starting\n"); ); | |
while (!gTerminate) | |
{ | |
DEBUG0( printf("pass\n"); ); | |
if ( !man_transfer( SRC_LINK0 ) ) | |
break; | |
sleep(INTERVAL); | |
if (gTerminate) | |
break; | |
// break; | |
} | |
man_cleanup(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment