index dfec8d8961720339fc886a09cdd68c2ed6e86c3f..002308e537d574d997c2268e4eeaea10f99f5e31 100644 (file)
use Exporter;
@ISA = qw(Exporter);
-@EXPORT = qw(create_xml_hash send_msg_hash2address get_content_from_xml_hash add_content2xml_hash create_xml_string encrypt_msg decrypt_msg create_ciphering transform_msg2hash);
+@EXPORT = qw(create_xml_hash send_msg_hash2address get_content_from_xml_hash add_content2xml_hash create_xml_string encrypt_msg decrypt_msg create_ciphering transform_msg2hash get_time send_msg);
use strict;
use warnings;
my $xml = new XML::Simple();
-
sub process_incoming_msg {
return;
}
}
-##=== FUNCTION ================================================================
-## NAME: logging
-## PARAMETERS: level - string - default 'info'
-## msg - string -
-## facility - string - default 'LOG_DAEMON'
-## RETURNS: nothing
-## DESCRIPTION: function for logging
-##===============================================================================
-#my $log_file = $main::log_file;
-#my $verbose = $main::verbose;
-#my $foreground = $main::forground;
-#sub daemon_log {
-# # log into log_file
-# my( $msg, $level ) = @_;
-# if(not defined $msg) { return }
-# if(not defined $level) { $level = 1 }
-# if(defined $log_file){
-# open(LOG_HANDLE, ">>$log_file");
-# if(not defined open( LOG_HANDLE, ">>$log_file" )) {
-# print STDERR "cannot open $log_file: $!";
-# return }
-# chomp($msg);
-# if($level <= $verbose){
-# print LOG_HANDLE "$level $msg\n";
-# if(defined $foreground) { print $msg."\n" }
-# }
-# }
-# close( LOG_HANDLE );
-##log into syslog
-## my ($msg, $level, $facility) = @_;
-## if(not defined $msg) {return}
-## if(not defined $level) {$level = "info"}
-## if(not defined $facility) {$facility = "LOG_DAEMON"}
-## openlog($0, "pid,cons,", $facility);
-## syslog($level, $msg);
-## closelog;
-## return;
-#}
-
-
#=== FUNCTION ================================================================
# NAME: create_xml_hash
# PARAMETERS: header - string - message header (required)
target => [$target],
$header => [$header_value],
};
- #daemon_log("create_xml_hash:", 7),
- #chomp(my $tmp = Dumper $hash);
- #daemon_log("\t$tmp", 7);
return $hash
}
sub transform_msg2hash {
my ($msg) = @_ ;
-
my $hash = $xml->XMLin($msg, ForceArray=>1);
return $hash;
}
# RETURNS: nothing
# DESCRIPTION: ????
#===============================================================================
-sub send_msg_hash2address {
+sub send_msg_hash2address ($$$){
my ($msg_hash, $address, $passwd) = @_ ;
# fetch header for logging
# generate xml string
my $msg_xml = &create_xml_string($msg_hash);
- # fetch the appropriated passwd from hash
- if(not defined $passwd) {
- if(exists $main::known_daemons->{$address}) {
- $passwd = $main::known_daemons->{$address}->{passwd};
- } elsif(exists $main::known_clients->{$address}) {
- $passwd = $main::known_clients->{$address}->{passwd};
-
- } else {
- daemon_log("$address not known, neither as server nor as client", 1);
- return 1;
- }
- }
-
# create ciphering object
my $act_cipher = &create_ciphering($passwd);
# opensocket
my $socket = &open_socket($address);
if(not defined $socket){
- daemon_log("cannot send '$header'-msg to $address , server not reachable",
- 5);
-
- if (exists $main::known_clients->{$address}) {
- if ($main::known_clients->{$address}->{status} eq "down") {
- # if status of not reachable client is already 'down',
- # then delete client from known_clients
- &clean_up_known_clients($address);
-
- } else {
- # update status to 'down'
- &update_known_clients(hostname=>$address, status=>"down");
-
- }
- }
+ daemon_log("cannot send '$header'-msg to $address , server not reachable", 5);
return 1;
}
close $socket;
daemon_log("send '$header'-msg to $address", 1);
-
- daemon_log("$msg_xml", 5);
-
- #daemon_log("crypted message:",7);
- #daemon_log("\t$crypted_msg", 7);
-
- # update status of client in known_clients with last send msg
- if(exists $main::known_daemons->{$address}) {
- #&update_known_daemons();
- } elsif(exists $main::known_clients->{$address}) {
- &main::update_known_clients(hostname=>$address, status=>$header);
- }
-
+ daemon_log("message:\n$msg_xml", 8);
return 0;
}
my ($msg, $my_cipher) = @_;
if(not defined $my_cipher) { print "no cipher object\n"; }
$msg = "\0"x(16-length($msg)%16).$msg;
- my $crypted_msg = $my_cipher->encrypt($msg);
- chomp($crypted_msg = &encode_base64($crypted_msg));
- return $crypted_msg;
+ $msg = $my_cipher->encrypt($msg);
+ chomp($msg = &encode_base64($msg));
+ return $msg;
}
# DESCRIPTION: decrypts the incoming message with the Crypt::Rijndael module
#===============================================================================
sub decrypt_msg {
- my ($crypted_msg, $my_cipher) = @_ ;
- $crypted_msg = &decode_base64($crypted_msg);
- my $msg = $my_cipher->decrypt($crypted_msg);
+ my ($msg, $my_cipher) = @_ ;
+ if(defined $msg && defined $my_cipher) {
+ $msg = &decode_base64($msg);
+ }
+ $msg = $my_cipher->decrypt($msg);
$msg =~ s/\0*//g;
return $msg;
}
if(not defined $socket) {
return;
}
- &daemon_log("open_socket:", 7);
- &daemon_log("\t$PeerAddr", 7);
+ &daemon_log("open_socket: $PeerAddr", 7);
return $socket;
}
+sub get_time {
+ my ($seconds, $minutes, $hours, $monthday, $month,
+ $year, $weekday, $yearday, $sommertime) = localtime(time);
+ $hours = $hours < 10 ? $hours = "0".$hours : $hours;
+ $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
+ $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
+ $month+=1;
+ $month = $month < 10 ? $month = "0".$month : $month;
+ $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
+ $year+=1900;
+ return "$year$month$monthday$hours$minutes$seconds";
+
+}
+
+
#=== FUNCTION ================================================================
# NAME: send_msg
# DESCRIPTION: Send a message to a destination
# package
# RETURNS: nothing
#===============================================================================
-sub send_msg ($$$$) {
- my ($header, $from, $to, $data) = @_;
+sub send_msg ($$$$$) {
+ my ($header, $from, $to, $data, $hostkey) = @_;
my $out_hash = &create_xml_hash($header, $from, $to);
}
}
- &send_msg_hash2address($out_hash, $address);
+ &send_msg_hash2address($out_hash, $to, $hostkey);
}
1;