/[echolot]/trunk/Echolot/Pinger.pm
ViewVC logotype

Contents of /trunk/Echolot/Pinger.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (hide annotations) (download)
Thu Jun 13 15:26:50 2002 UTC (10 years, 11 months ago) by weasel
File size: 3246 byte(s)
Removed debug prints
1 weasel 9 package Echolot::Pinger;
2    
3     # (c) 2002 Peter Palfrader <peter@palfrader.org>
4 weasel 27 # $Id: Pinger.pm,v 1.5 2002/06/13 15:26:50 weasel Exp $
5 weasel 9 #
6    
7     =pod
8    
9     =head1 Name
10    
11     Echolot::Pinger - actual sending and receiving of Pings.
12    
13     =head1 DESCRIPTION
14    
15     This package provides functions for sending out and receiving pings.
16    
17     =cut
18    
19     use strict;
20     use warnings;
21     use Carp qw{cluck};
22     use English;
23     use Echolot::Pinger::Mix;
24    
25     sub makeHash($) {
26     my ($text) = @_;
27     my $sum = 0;
28     for (my $i=0; $i < length($text); $i++) {
29     $sum += ord( substr($text, $i, 1) )
30     };
31     return $sum;
32     };
33    
34     sub do_mix_ping($$$$$) {
35     my ($address, $keyid, $time, $to, $body) = @_;
36    
37     my %key = Echolot::Globals::get()->{'storage'}->get_key($address, 'mix', $keyid);
38     Echolot::Pinger::Mix::ping(
39     $body,
40     $to,
41     $key{'nick'},
42     { $keyid => \%key } ) or
43     return 0;
44    
45     return 1;
46     };
47    
48     sub do_ping($$$) {
49     my ($type, $address, $key) = @_;
50    
51     my $now = time();
52     my $token = $address.':'.$type.':'.$key.':'.$now;
53     my $mac = Echolot::Tools::make_mac($token);
54     my $body = "remailer: $address\n".
55     "type: $type\n".
56     "key: $key\n".
57     "sent: $now\n".
58     "mac: $mac\n";
59    
60     my $to = Echolot::Tools::make_address('ping');
61     if ($type eq 'mix') {
62     do_mix_ping($address, $key, $now, $to, $body);
63     } else {
64     cluck ("Don't know how to handle ping type $type");
65     return 0;
66     };
67    
68     Echolot::Globals::get()->{'storage'}->register_pingout($address, $type, $key, $now);
69     return 1;
70     };
71    
72 weasel 14 sub send_pings() {
73 weasel 16 my $call_intervall = Echolot::Config::get()->{'pinger_interval'};
74     my $send_every_n_calls = Echolot::Config::get()->{'ping_every_nth_time'};
75 weasel 9
76     my $now = time();
77    
78     my @remailers = Echolot::Globals::get()->{'storage'}->get_remailers();
79     for my $remailer (@remailers) {
80     my $timemod = ($now / $call_intervall);
81     my $this_call_id = $timemod % $send_every_n_calls;
82    
83     my $this_remailer_id = makeHash($remailer) % $send_every_n_calls;
84    
85     next unless ($this_call_id eq $this_remailer_id);
86    
87     for my $type (Echolot::Globals::get()->{'storage'}->get_types($remailer)) {
88     for my $key (Echolot::Globals::get()->{'storage'}->get_keys($remailer, $type)) {
89     do_ping($type, $remailer, $key);
90     }
91     };
92     };
93     return 1;
94     };
95    
96    
97     sub receive($$$) {
98     my ($body, $token, $timestamp) = @_;
99    
100     my $now = time();
101    
102     my ($addr) = $body =~ /^remailer: (.*)$/m;
103     my ($type) = $body =~ /^type: (.*)$/m;
104     my ($key) = $body =~ /^key: (.*)$/m;
105     my ($sent) = $body =~ /^sent: (.*)$/m;
106     my ($mac) = $body =~ /^mac: (.*)$/m;
107    
108     my $cleanstring = (defined $addr ? $addr : 'undef') . ':' .
109     (defined $type ? $type : 'undef') . ':' .
110     (defined $key ? $key : 'undef') . ':' .
111     (defined $sent ? $sent : 'undef') . ':' .
112     (defined $mac ? $mac : 'undef') . ':';
113    
114     (defined $addr && defined $type && defined $key && defined $sent && defined $mac) or
115     warn ("Received ping at $timestamp has undefined values: $cleanstring\n"), #FIXME: logging
116     return 0;
117    
118     Echolot::Tools::verify_mac($addr.':'.$type.':'.$key.':'.$sent, $mac) or
119     warn ("Received ping at $timestamp has wrong mac; $cleanstring\n"), #FIXME: logging
120     return 0;
121    
122     Echolot::Globals::get()->{'storage'}->register_pingdone($addr, $type, $key, $sent, $now - $sent) or
123     return 0;
124    
125     return 1;
126     };
127    
128     1;
129     # vim: set ts=4 shiftwidth=4:

  ViewVC Help
Powered by ViewVC 1.1.5