Advertisement
tantraputra

daemon

Oct 19th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 8.95 KB | None | 0 0
  1. #!/opt/perl5/perls/perl-5.14.2/bin/perl5.14.2
  2.  
  3. use Modern::Perl '2012';
  4. use Socket;
  5. use POE qw(
  6.   Wheel::SocketFactory
  7.   Wheel::ReadWrite
  8.   Driver::SysRW
  9.   Filter::Stream
  10. );
  11. use Log::Log4perl;
  12. use bytes;
  13. use Nohmad;
  14. use Database;
  15. use Data::Dumper;
  16.  
  17. #####
  18. # MAIN
  19. #####
  20.  
  21. local $|        = 0;    #flush after every write or print
  22. our $debug      = 1;    #be very very noisy
  23. our $serverport = 8888; #daemon port
  24.  
  25. #-------------------------------------------------------------------------------
  26. #  Logger
  27. #-------------------------------------------------------------------------------
  28. my $log_conf = "log4perl.conf";
  29. Log::Log4perl::init($log_conf);
  30. my $logger = Log::Log4perl->get_logger();
  31. $Log::Log4perl::JOIN_MSG_ARRAY_CHAR = " <*> ";
  32.  
  33. fork and exit unless $debug;
  34.  
  35. #Connected devices
  36. my $devices = {};
  37.  
  38. POE::Session->create(
  39.     inline_states => {
  40.         _start => \&parent_start,
  41.         _stop  => \&parent_stop,
  42.  
  43.         socket_birth => \&socket_birth,
  44.         socket_death => \&socket_death,
  45.     }
  46. );
  47.  
  48. # $poe_kernel is exported from POE
  49. $poe_kernel->run();
  50.  
  51. exit;
  52.  
  53. ####################################
  54.  
  55. sub parent_start {
  56.     my $heap = $_[HEAP];
  57.  
  58.     $logger->debug( "= L = Listener birth", "" );
  59.  
  60.     $heap->{listener} = POE::Wheel::SocketFactory->new(
  61.         BindPort     => $serverport,
  62.         Reuse        => 'yes',
  63.         SuccessEvent => 'socket_birth',
  64.         FailureEvent => 'socket_death',
  65.     );
  66. }
  67.  
  68. sub parent_stop {
  69.     my $heap = $_[HEAP];
  70.     delete $heap->{listener};
  71.     delete $heap->{session};
  72.     $logger->debug( "= L = Listener death", "" );
  73. }
  74.  
  75. ##########
  76. # SOCKET #
  77. ##########
  78.  
  79. sub socket_birth {
  80.     my ( $socket, $address, $port ) = @_[ ARG0, ARG1, ARG2 ];
  81.     $address = inet_ntoa($address);
  82.  
  83.     $logger->debug( "= S = Socket birth", "" );
  84.  
  85.     POE::Session->create(
  86.         inline_states => {
  87.             _start => \&socket_success,
  88.             _stop  => \&socket_death,
  89.  
  90.             #Remote enrolling request
  91.             remote_enrol => \&remote_enrol,
  92.  
  93.             socket_input => \&socket_input,
  94.             socket_death => \&socket_death,
  95.         },
  96.         args => [ $socket, $address, $port ],
  97.     );
  98.  
  99. }
  100.  
  101. sub socket_death {
  102.     my $heap = $_[HEAP];
  103.     my ( $operation, $errnum, $errstr, $wheel_id ) = @_[ ARG0 .. ARG3 ];
  104.  
  105.     if ( defined $wheel_id ) {
  106.         $logger->error(
  107.             "Wheel $wheel_id generated $operation error $errnum: $errstr", "" );
  108.     }
  109.  
  110.     if ( $heap->{socket_wheel} ) {
  111.         $logger->debug( "= S = Socket death", "" );
  112.         delete $heap->{socket_wheel};
  113.  
  114.         #Remove device from active sessions
  115.         delete $devices->{ $heap->{imei} } if exists $heap->{imei};
  116.     }
  117. }
  118.  
  119. sub socket_success {
  120.     my ( $heap, $connected_socket, $address, $port ) =
  121.       @_[ HEAP, ARG0, ARG1, ARG2 ];
  122.  
  123.     $logger->debug( "= I = CONNECTION from $address : $port", "" );
  124.  
  125.     $heap->{socket_wheel} = POE::Wheel::ReadWrite->new(
  126.         Handle => $connected_socket,
  127.         Driver => POE::Driver::SysRW->new(),
  128.         Filter => POE::Filter::Stream->new(),
  129.  
  130.         InputEvent => 'socket_input',
  131.         ErrorEvent => 'socket_death',
  132.     );
  133. }
  134.  
  135. #===  FUNCTION  ================================================================
  136. #         NAME: find_package_length
  137. #      PURPOSE:
  138. #   PARAMETERS: ????
  139. #      RETURNS: ????
  140. #  DESCRIPTION: ????
  141. #       THROWS: no exceptions
  142. #     COMMENTS: none
  143. #     SEE ALSO: n/a
  144. #===============================================================================
  145. sub find_package_length {
  146.     my ( $buf ) = @_;
  147.  
  148.     my ( $length, $pkg_id, $pkg_type ) = unpack("H2H2H2", $buf);
  149.     print Dumper(unpack("H2H2H2", $buf));
  150.     $length = (( hex $length ) + 1);
  151.     say $length;
  152.  
  153.     if ( $pkg_type eq '21' ) {
  154.         say 'Dreaded 21->', $length;
  155.         my @l = unpack("H2H2H2H2v", $buf);
  156.         print Dumper(unpack("H2H2H2H2v", $buf));
  157.  
  158.         $length += $l[4];
  159.         say "New length:", $length;
  160.         say "BufLen:", length($buf);
  161.  
  162.     }
  163.  
  164.     return $length;
  165. } ## --- end sub find_package_length
  166.  
  167. #===  FUNCTION  ================================================================
  168. #         NAME: post_package
  169. #      PURPOSE:
  170. #   PARAMETERS: ????
  171. #      RETURNS: undef
  172. #  DESCRIPTION: ????
  173. #       THROWS: no exceptions
  174. #     COMMENTS: none
  175. #     SEE ALSO: n/a
  176. #===============================================================================
  177. sub post_package {
  178.     my ( $kernel, $heap, $package, $sessionID ) = @_;
  179.  
  180.     my $reply = Nohmad::packet( $heap, $package, $devices, $sessionID );
  181.     if ( ref $reply eq "ARRAY" && $reply->[0] eq "A" ) {
  182.         # :TODO      :03/31/2012 11:35:11 AM:: admin_id=256, change it
  183.         $kernel->post( $reply->[1], "remote_enrol", 1, $reply->[2], 256 );
  184.     }
  185.     elsif ( ref $reply eq "HASH" ) {
  186.         my $package_content = Database::_assemble_package( $heap, $reply, "N" );
  187.         $heap->{socket_wheel}->put($package_content);
  188.     }
  189.     return;
  190. } ## --- end sub post_package
  191.  
  192. sub find_package {
  193. #===  FUNCTION  ================================================================
  194. #         NAME: find_package
  195. #      PURPOSE: Verify completeness and assemble if necessary
  196. #   PARAMETERS: $heap - POE heap
  197. #               $buf - read chunk from socket
  198. #      RETURNS: undef
  199. #  DESCRIPTION: Assembly of packages
  200. #       THROWS: no exceptions
  201. #     COMMENTS: none
  202. #     SEE ALSO: n/a
  203. #===============================================================================
  204.     my ( $kernel, $heap, $buf, $sessionID ) = @_;
  205.  
  206.     if ( exists $heap->{trailing} ) {
  207.  # :TODO      :05/05/2012 11:35:38 PM:: $heap->{pkg} puudub?
  208.         if ( length($buf) + length( $heap->{pkg} ) == $heap->{pkg_len} ) {
  209.  
  210.             $logger->debug(
  211.                 "PackageASS["
  212.                   . join( ' ', unpack( "(H2)*", $heap->{pkg} . $buf ) ) . "]",
  213.                 $heap->{imei} || ''
  214.             );
  215.             post_package( $kernel, $heap, $heap->{pkg} . $buf, $sessionID );
  216.             delete $heap->{trailing};
  217.             delete $heap->{pkg};
  218.             delete $heap->{pkg_len};
  219.  
  220.             return;
  221.         }
  222.  # :TODO      :05/05/2012 11:36:32 PM:: $heap->{pkg} puudub?
  223.         elsif ( length($buf) + length( $heap->{pkg} ) > $heap->{pkg_len} )
  224.         {
  225.             my $missing_part =
  226.               substr( $buf, 0, $heap->{pkg_len} - length( $heap->{pkg} ), '' );
  227.             $logger->debug(
  228.                 "PackageASS["
  229.                   . join( ' ', unpack( "(H2)*", $heap->{pkg} . $missing_part ) )
  230.                   . "]",
  231.                 $heap->{imei} || ''
  232.             );
  233.             post_package( $kernel, $heap, $heap->{pkg} . $missing_part,
  234.                 $sessionID );
  235.  
  236.             delete $heap->{trailing};
  237.             delete $heap->{pkg};
  238.             delete $heap->{pkg_len};
  239.  
  240.             find_package( $kernel, $heap, $buf, $sessionID );
  241.         }
  242.         else {
  243.             $heap->{pkg} .= $buf;
  244.  
  245.             return;
  246.         }
  247.     }
  248. #-------------------------------------------------------------------------------
  249. #  No pending data
  250. #-------------------------------------------------------------------------------
  251.     else {
  252.         my $length = find_package_length( $buf );
  253.         print "LENGTH:(buf, calc)", length( $buf ), "|", $length, "\n";
  254.         if ( length($buf) == $length ) {
  255.             $logger->debug(
  256.                 "PackageASS[" . join( ' ', unpack( "(H2)*", $buf ) ) . "]",
  257.                 $heap->{imei} || '' );
  258.             post_package( $kernel, $heap, $buf, $sessionID );
  259.  
  260.             return;
  261.         }
  262.         else {
  263.             $heap->{trailing} = 1;
  264.             $heap->{pkg_len}  = $length;
  265.             $heap->{pkg} = "";
  266.             find_package( $kernel, $heap, $buf, $sessionID );
  267.         }
  268.     }
  269. }
  270.  
  271. sub socket_input {
  272.     my ( $kernel, $heap, $session, $buf ) = @_[ KERNEL, HEAP, SESSION, ARG0 ];
  273.     #Package ID for sequence
  274.     my ( $pkg_len, $pkg_id, $pkg_type ) = unpack( "H2" x 3, $buf );
  275.     $heap->{package_id} = hex $pkg_id;
  276. #    open FILE, ">", $heap->{package_id}. ".pkg";
  277. #    binmode FILE;
  278. #    print FILE $buf;
  279. #    close FILE;
  280.  
  281.     $logger->debug(
  282.         "PackageIN["
  283.           . join( ' ', unpack( "(H2)*", $buf ) ) . "]",
  284.             $heap->{imei} || '' );
  285.  
  286.     find_package( $kernel, $heap, $buf, $session->ID );
  287.  
  288.     if ( !$heap->{once} ) {
  289.         $heap->{socket_wheel}->put( Nohmad::_update_date_time($heap) );
  290.         $heap->{socket_wheel}->put( Nohmad::_update_locale($heap) );
  291.     }
  292.     $heap->{once} = 1;
  293. }
  294.  
  295. sub remote_enrol {
  296.     my ( $heap, $command, $finger_count, $admin_id ) =
  297.       @_[ HEAP, ARG0, ARG1, ARG2 ];
  298.  
  299.     my $reply =
  300.       Nohmad::_start_enrol( $command, $finger_count, $admin_id, 0, $heap );
  301.  
  302.     my $package_content = Database::_assemble_package( $heap, $reply, "Y" );
  303.  
  304.     if ( exists $heap->{pending_processes} ) {
  305.         $heap->{blocked_payload} = $package_content;
  306.     }
  307.     else {
  308.         $heap->{socket_wheel}->put( $package_content );
  309.     }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement