File:  [mozdev] / bitaacj / src / server.pl
Revision 1.19: download - view: text, annotated - select for diffs - revision graph
Thu Apr 6 06:30:05 2006 UTC (11 years, 6 months ago) by cherian
Branches: MAIN
CVS tags: HEAD
removed iplist cumulation problem

#27th march
#!/usr/bin/perl -w
#Lets catch the errors as soon as we can
use strict;
use warnings;
use IO::Socket;
use DBI;
use Sys::Hostname;
use Switch;
use POSIX;
use threads;
require Net::Daemon;
#use Thread::Pool;
STDOUT->autoflush(1);



#my $pid_daemon = fork;
#exit if $pid_daemon;
#die "Couldn't fork: $!" unless defined($pid_daemon);

#for my $handle ( *STDIN, *STDOUT, *STDERR){
#	open ($handle, "+<" ,"/dev/null")
#	|| die "can't reopen $handle to /dev/null : $!";
#}

#POSIX::setsid()
#    or die "Can't start a new session: $!";


#my $time_to_die = 0;

#sub signal_handler {
#    $time_to_die = 1;
#}

#$SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signal_handler;
# trap or ignore $SIG{PIPE}


#until ($time_to_die) {





my $buf;
my $sql;
#socket initialization 
my $sock = new IO::Socket::INET (
				#LocalHost => 'tracker',
                                LocalPort  =>   '27070',
                                Proto     =>       'tcp',
                                Listen     =>      1,
                                Reuse     =>       1,
				#Timeout   =>      2
                                  );
die "Could not create socket: $!\n" unless $sock;
print "\nProgram Starting...... \n";
#mysql connection initialization
#database design will be posted on the homepage soo. jus check it out
#print "\nTrying to connect to mysql database...... ";
#my $db_handle = DBI->connect("dbi:mysql:database=tracker;host=10.4.1.185;user=sprt;password=sprt")
#    or die "Couldn't connect to database: $DBI::errstr\n";
#print "connected \n";
#$new_sock->autoflush();



my $md5hash;
while(my ($new_sock, $s_addr)  = $sock->accept()){
	my $pid=fork();
	die "Cannnot fork" unless defined($pid);
	if($pid==0){
	#this is now child process
	#mysql connection initialization
	#database design will be posted on the homepage soo. jus check it out
	print "\nTrying to connect to mysql database...... ";
	my $db_handle = DBI->connect("dbi:mysql:database=tracker;host=10.4.1.185;user=sprt;password=sprt")
    		or die "Couldn't connect to database: $DBI::errstr\n";
	print "connected \n";
	my $peerid = $new_sock->peerhost();
	print "\n\n\npeer is " . $peerid. "\n";
	print "peer port is " . $new_sock->peerport() . "\n";
	

	my $count=0;
	my $buf_header;
	my $md5hashs;
	my $iplist_send = "";
	my $num_urls = 0;
	my $temp_iplist ="";
	my $debugtemp=0;


	$new_sock->recv($buf_header, 6);
	print "Recived raw bytestream is " . $buf_header . "\n";
	my $command= substr($buf_header, 0, 3);
	print "Recieved command is.... " . $command . "\n";
	

	switch($command) {
		case "GET"	{
			print "Recieved command inside switch is ....." .$command."\n";
			$num_urls = substr($buf_header, 3);
			print "No of hashes ...." . $num_urls . "\n";
			print"No .urls *32= ".$num_urls*32 ."\n";
			$new_sock->recv($md5hashs, $num_urls*32);
			my $leng = length($md5hashs);
			print "Length of the total string is ......" . $leng. "\n";
			print "Recieved hashs is...... $md5hashs\n\n\n";
			

			for($count=0; $count<$leng; $count+=32) {

				$md5hash = substr($md5hashs, $count, 32);
				print "extracted md5#is  ...... $md5hash \n";	

				# Get the I/P Address for the given hash
				$sql = "select ip from tab_ip_tim where url_hash='". $md5hash . "' order by tim desc limit 5";
				my $statement = $db_handle->prepare($sql) 
					or die "Couldn't prepare query '$sql': $DBI::errstr\n";
				$statement->execute() 
					or die "Couldn't execute query '$sql': $DBI::errstr\n";
				$$temp_iplist ="";
				while ( my $row = $statement->fetchrow_array ) {
					if ( $row eq $peerid ){
						next;}
					if($temp_iplist eq ""){
						$temp_iplist =$row;}
					else {
						$temp_iplist = $temp_iplist .",". $row; }
				}								
				$iplist_send = $iplist_send.$temp_iplist ."|";

				print "Recieved ip list without new line for  ".$md5hash."  is.... ".$iplist_send."\n\n";
				#print "\nDebug 1...... \n";

				#adding ip and hash to the mysql table 
				$sql= "insert into tab_ip_tim values ('$md5hash','$peerid',now()) on duplicate key update tim=now()"; 
				$statement = $db_handle->prepare($sql)
				or die "Couldn't prepare query '$sql': $DBI::errstr\n";
				$statement->execute()
				or die "Couldn't execute query '$sql': $DBI::errstr\n";

				#hitcounter processing 
				my $sql_hitcounter= "insert into hitcounter values('$md5hash','1',now()) on duplicate key update count=count+1"; 
				my $statement_hitcounter = $db_handle->prepare($sql_hitcounter)
					or die "Couldn't prepare query '$sql': $DBI::errstr\n";
				$statement_hitcounter->execute()
				or die "Couldn't execute query '$sql': $DBI::errstr\n";

			}

			# $+value will be added to the iplist to show how many peers are connected
			$sql= "select count(distinct ip) from tab_ip_tim"; 
			my $statement = $db_handle->prepare($sql)
				or die "Couldn't prepare query '$sql': $DBI::errstr\n";
			$statement->execute()
				or die "Couldn't execute query '$sql': $DBI::errstr\n";
			my $no_unique_ips = $statement->fetchrow_array;
			print"No of unique ips is ". $no_unique_ips . "\n";
			$iplist_send = $iplist_send . "&" .$no_unique_ips ;
			print "Final ip list to be send is   ". $iplist_send ."\n";
			print "Length of msg to be sent is.....". length($iplist_send)."\n";
			print ("Trying to send msg back.....\n");
			#this is jus for checking...delete it.
			#$new_sock->send(length($iplist_send)) or die "Cant send";
			#$new_sock->flush();
			$new_sock->send($iplist_send) or die "Cant send";
			$new_sock->flush();
			#close($new_sock);
			print ("Send msg back!!!\n");
		}
		case "STA"	{ #status checking of hashes in the toolbar 
			
		}
		else { 
			print"You have either done a port scan or this command is not supported right now...contact Bitaacj team\n\n";
		}
	}

	$new_sock->flush();
	exit(0);
	}
	
	

close($new_sock);
}

#}
print "\nProgram Ending...... \n";


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>