3 # This file is part of secnet.
4 # See README for full list of copyright holders.
6 # secnet is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # secnet is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # version 3 along with secnet; if not, see
18 # https://www.gnu.org/licenses/gpl.html.
26 open DEBUG, ">/dev/null" or die $!;
28 if (@ARGV && $ARGV[0] eq '-D') {
30 open DEBUG, ">&STDERR" or die $!;
33 die "$us: no arguments permitted\n" if @ARGV;
35 our ($monh,$monchild);
38 # no entry: not reported, does not exist
39 # /ry+/: reported, entry exists
40 # during processing only:
41 # /r/: reported, may not still exist
42 # /y+/: not reported, entry exists
45 return unless $monchild;
47 or warn "$us: cannot kill monitor child [$monchild]: $!\n";
52 END { killmonitor(); }
62 $monchild = open $monh, "-|", qw(ip -o monitor addr)
63 or die "spawn monitor: $!\n";
64 sleep(1) if $restart++;
67 my $got = sysread $monh, $discard, 4096;
68 die "read monitor: $!\n" unless defined $got;
69 die "monitor failed\n" unless $got;
71 $_='r' foreach values %reported;
72 print DEBUG "#########################################\n";
73 foreach my $ip (qw(4 6)) {
74 print DEBUG "###### $ip:\n";
75 my $addrh = new IO::File;
76 open $addrh, "-|", qw(ip -o), "-$ip", qw(addr show)
77 or die "spawn addr $ip show: $!\n";
78 my $afstr = $ip==4 ? 'inet' : $ip==6 ? 'inet6' : die;
81 if (m{^\d+\:\s*(\S+)\s+$afstr\s+([0-9a-z.:]+)(?:/\d+)?\s}) {
83 my $outline = "$ip $1 $2";
84 # "ip -o addr show" has a ridiculous output format. In
85 # particular, it mixes output keywords which introduce
86 # values with ones which don't, and there seems to be
87 # no way to tell without knowing all the possible
88 # keywords. We hope that before the \ there is nothing
89 # which contains arbitrary text (specifically, which
90 # might be `tentative' other than to specify IPv6
91 # tentativeness). We have to do this for IPv6 only
92 # because in the IPv4 output, the interface name
94 next if $ip==6 && $rhs=~m{[^\\]* tentative\s};
95 $reported{$outline} .= "y";
98 warn "unexpected output from addr $ip show: $_\n";
101 my $r = close $addrh;
102 die "addr $ip show failed $!\n" unless $r;
105 foreach my $k (keys %reported) {
106 local $_ = $reported{$k};
109 delete $reported{$k};
116 print STDERR "$us: $@";
121 STDOUT->flush or die $!;