/[pxelator]/lib/PXElator/client.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/PXElator/client.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 219 by dpavlin, Sat Aug 15 13:44:13 2009 UTC revision 330 by dpavlin, Fri Aug 28 22:29:08 2009 UTC
# Line 5  use strict; Line 5  use strict;
5  use autodie;  use autodie;
6    
7  use File::Slurp;  use File::Slurp;
8  use Net::Ping;  use Data::Dump qw/dump/;
 use Carp qw/confess/;  
9    
10  use server;  use server;
11  use format;  use format;
12    use ip;
13    use ping;
14    
15    our $debug = $server::debug;
16    
17  sub mkbasedir {  sub mkbasedir {
18          my $path = shift;          my $path = shift;
# Line 29  sub conf_value { Line 32  sub conf_value {
32          } elsif ( -f $path ) {          } elsif ( -f $path ) {
33                  $value = read_file $path;                  $value = read_file $path;
34          } else {          } else {
35                  confess "$path not file or symlink";                  warn "W: $path not file or symlink\n";
36          }          }
37          return $value;          return $value;
38  }  }
# Line 60  sub conf { Line 63  sub conf {
63          } elsif ( -f $path ) {          } elsif ( -f $path ) {
64                  $value = read_file $path;                  $value = read_file $path;
65          } else {          } else {
66                  confess "conf $name";                  warn "# $name missing $path\n" if $debug;
67          }          }
68          return $value;          return $value;
69  }  }
70    
71    sub all_conf {
72            my $ip = shift;
73            my $path = ip_path $ip || return;
74            my $conf;
75            foreach my $file ( glob("$path/*") ) {
76                    my $name = $file;
77                    $name =~ s{^.+/([^/]+)$}{$1};
78                    $conf->{ $name } = read_file $file;
79            }
80            return $conf;
81    }
82  sub next_ip($) {  sub next_ip($) {
83          my $mac = shift;          my $mac = shift;
84            $mac = format::mac($mac);
         my $p = Net::Ping->new;  
85    
86          my $prefix = $server::ip;          my $prefix = $server::ip;
87          $prefix =~ s{\.\d+$}{.};          $prefix =~ s{\.\d+$}{.};
88          my $addr = $server::ip_from || die;          my $addr = $server::ip_from || die;
89          my $ip = $prefix . $addr;          my $ip = $prefix . $addr;
90    
91          while ( -e ip_path($ip) || $p->ping( $ip, 0.7 ) ) {          while ( -e ip_path($ip) || ping::host($ip) ) {
92                  $ip = $prefix . $addr++;                  $ip = $prefix . $addr++;
93                  die "all addresses allocated!" if $addr == $server::ip_to;                  die "all addresses allocated!" if $addr == $server::ip_to;
94                  warn "skip $ip\n";                  warn "skip $ip\n";
# Line 83  sub next_ip($) { Line 96  sub next_ip($) {
96    
97          warn "next_ip $ip\n";          warn "next_ip $ip\n";
98    
99          mkdir ip_path($ip);          save_ip_mac( $ip, $mac );
   
         my $mac_path = mac_path($mac);  
         unlink $mac_path if -e $mac_path;       # XXX audit?  
         symlink ip_path($ip), $mac_path;  
         write_file ip_path($ip,'mac'), $mac;  
100    
101          return $ip;          return $ip;
102    }
103    
104    sub save_ip_mac {
105            my ($ip,$mac) = @_;
106            $mac = format::mac($mac);
107            return if $mac eq '00:00:00:00:00:00';
108    
109            mkdir ip_path($ip) unless -e ip_path($ip);
110    
111            my $mac_path = mac_path($mac);
112            unlink $mac_path if -l $mac_path;       # XXX audit?
113            symlink ip_path($ip), $mac_path;
114            write_file( ip_path($ip,'mac'), $mac );
115  }  }
116    
117  sub ip_from_mac($) {  sub ip_from_mac($) {
118          my $mac = shift;          my $mac = shift;
119            $mac = format::mac($mac);
         $mac = lc $mac;  
         $mac =~ s{:}{}g;  
120    
121          my $mac_path = mac_path $mac;          my $mac_path = mac_path $mac;
122          return unless -e $mac_path;          return unless -e $mac_path;
# Line 126  sub mac_from_ip($) { Line 144  sub mac_from_ip($) {
144    
145  sub change_ip($$) {  sub change_ip($$) {
146          my ($old, $new) = @_;          my ($old, $new) = @_;
147          my $mac = mac_from_ip($old);          return if $old eq $new;
148            my $mac = mac_from_ip($old) || die "no mac for $old";
149          rename ip_path($old), ip_path($new);          rename ip_path($old), ip_path($new);
150          unlink mac_path($mac);          unlink mac_path($mac);
151          symlink ip_path($new), mac_path($mac);          symlink ip_path($new), mac_path($mac);
152            return $new;
153    }
154    
155    sub all_ips {
156            sort { ip::to_int($a) cmp ip::to_int($b) }
157            map {
158                    my $ip = $_;
159                    $ip =~ s{^.+/ip/}{};
160                    $ip;
161            } glob("$server::conf/ip/*")
162    }
163    
164    sub remove {
165            my $ip = shift;
166            unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
167            if ( my $mac = mac_from_ip $ip ) {
168                    unlink "$server::conf/mac/$mac";
169            }
170            rmdir "$server::conf/ip/$ip";
171    }
172    
173    sub arp_mac_dev {
174            my $arp = {
175                    map {
176                            my @c = split(/\s+/,$_);
177                            if ( $#c == 5 ) {
178                                    client::save_ip_mac( $c[0], $c[3] );
179                                    ( uc $c[3] => $c[5] )
180                            } else {
181                            }
182                    } read_file('/proc/net/arp')
183            };
184    
185            warn "# arp ",dump( $arp );
186            return $arp;
187  }  }
188    
189  1;  1;

Legend:
Removed from v.219  
changed lines
  Added in v.330

  ViewVC Help
Powered by ViewVC 1.1.26