chiark / gitweb /
polypath-interface-monitor-linux: Debugging output
[secnet.git] / polypath-interface-monitor-linux
1 #!/usr/bin/perl -w
2 use strict;
3 use IO::Handle;
4
5 my $us = $0;
6 $us =~ s{.*/}{};
7
8 open DEBUG, ">/dev/null" or die $!;
9
10 if (@ARGV && $ARGV[0] eq '-D') {
11     shift @ARGV;
12     open DEBUG, ">&STDERR" or die $!;
13 }
14
15 die "$us: no arguments permitted\n" if @ARGV;
16
17 our ($monh,$monchild);
18
19 our %reported;
20 #  no entry: not reported, does not exist
21 #  /ry+/: reported, entry exists
22 # during processing only:
23 #  /r/: reported, may not still exist
24 #  /y+/: not reported, entry exists
25
26 sub killmonitor () {
27     return unless $monchild;
28     kill 9, $monchild
29         or warn "$us: cannot kill monitor child [$monchild]: $!\n";
30     $monchild=undef;
31     close $monh;
32 }
33
34 END { killmonitor(); }
35
36 my $restart;
37
38 for (;;) {
39     my $o;
40     eval {
41         if (!$monh) {
42             killmonitor();
43             $monh = new IO::File;
44             $monchild = open $monh, "-|", qw(ip -o monitor addr)
45                 or die "spawn monitor: $!\n";
46             sleep(1) if $restart++;
47         } else {
48             my $discard;
49             my $got = sysread $monh, $discard, 4096;
50             die "read monitor: $!\n" unless defined $got;
51             die "monitor failed\n" unless $got;
52         }
53         $_='r' foreach values %reported;
54         print DEBUG "#########################################\n";
55         foreach my $ip (qw(4 6)) {
56             print DEBUG "###### $ip:\n";
57             my $addrh = new IO::File;
58             open $addrh, "-|", qw(ip -o), "-$ip", qw(addr show)
59                 or die "spawn addr $ip show: $!\n";
60             my $afstr = $ip==4 ? 'inet' : $ip==6 ? 'inet6' : die;
61             while (<$addrh>) {
62                 print DEBUG "#$_";
63                 if (m{^\d+\:\s*(\S+)\s+$afstr\s+([0-9a-z.:]+)(?:/\d+)?\s}) {
64                     my $outline = "$ip $1 $2";
65                     $reported{$outline} .= "y";
66                 } else {
67                     chomp;
68                     warn "unexpected output from addr $ip show: $_\n";
69                 }
70             }
71             my $r = close $addrh;
72             die "addr $ip show failed $!\n" unless $r;
73             $o = '';
74         }
75         foreach my $k (keys %reported) {
76             local $_ = $reported{$k};
77             if (m/^r$/) {
78                 $o .= "-$k\n";
79                 delete $reported{$k};
80             } elsif (m/^y/) {
81                 $o .= "+$k\n";
82             }
83         }
84     };
85     if ($@) {
86         print STDERR "$us: $@";
87         sleep 5;
88         next;
89     }
90     print $o or die $!;
91     STDOUT->flush or die $!;
92 }