#!/usr/bin/php -q =$haystacklen) break; } return -2; } // // mainline // echo "9100a Daemon started \n"; // Main Loop while(1) { // Get the IP address for the target host. $address = gethostbyname($server); // Create a TCP/IP socket. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket < 0) { // This should go into a log echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; break; } else { if(1==$debug) echo "OK.\n"; } if(1==$debug) echo "Attempting to connect to '$address' on port '$port'..."; $result = socket_connect($socket, $address, $port); if (false == $result) { echo "socket_connect() failed.\nReason: (" . socket_strerror(socket_last_error()) . ")\n"; break; } else { if(1==$debug) echo "OK.\n"; } $out = "GET /GetData.cgi HTTP/1.0\r\n\r\n"; if(1==$debug) echo "Sending HTTP request to 9100..."; socket_write($socket, $out, strlen($out)); if(1==$debug) { echo "OK.\n"; echo "Reading response:\n\n"; } // // Read loop, we should stay in this loop forever reading jpgs from the socket and writing them Look for the image and dump it. // $boundry_len=strlen($boundry); $mode=0; // Mode 0=search 1=dump $top_off=0; $j=''; while($i=socket_read($socket,$buffer_len, PHP_BINARY_READ)) { // top the buffer off if flag set if(1==$top_off) { $i=$j.$i; $top_off=0; $j=''; } // Get the number of bytes read by socket_read $real_len=strlen($i); // search for the jpeg boundry in the stream. $c=binarysearch($boundry,$boundry_len, $i,$real_len); // If we found a match but it was at the end of the buffer, copy this tail end // of this buffer to the beginning of the next one and continue. Also do this // if it is a possible partial match. if( ( ($real_len-$c)<60) )//&& (0==$mode) ) { $j=substr($i, -60);//$i; $top_off=1; continue; } // If no match, action depending on mode if(0>$c) { // No header found in this chunk // // Check mode, if mode 1 write data to file, else just drop it (do nothing) if(1==$mode) { dump($tfile,$i,0,$real_len); $ecount=0; } else if(0==$mode) { // Do nothing } } else if($c>0) { // first if found at end of file append and try again. // We found the boundry // check mode if(1==$mode) { // we are currently dumping data, return data up until the new header dump($tfile,$i,0,$c);//$real_len); //c+1); // // Now close file and copy it to the correct filename. // fclose($tfile); rename($tmpfile,$outfile.$pindex.".jpg" ); } // Ok now ger ready for the next file // Build offsets $start=$c+$boundry_len+30; // Index into the start of jpg $cport=$start+14; // location of port ID $pindex=bin2hex($i[$cport]); // save the picture index echo "writing file ".$pindex."\r"; //tdump($i,$start,0x30); // Open the Temp file up $tfile = fopen($tmpfile, "wb"); // Dump the new part of jpg dump($tfile,$i,$start,$real_len-($start)); $mode=1; } } // While echo "sleep \n\r"; // We had an error, sleep and try up to error count sleep($error_wait); } ?>