/[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 190 by dpavlin, Mon Aug 10 00:07:38 2009 UTC revision 391 by dpavlin, Mon Sep 7 21:53:45 2009 UTC
# Line 4  use warnings; Line 4  use warnings;
4  use strict;  use strict;
5  use autodie;  use autodie;
6    
 use server;  
7  use File::Slurp;  use File::Slurp;
8  use Net::Ping;  use Data::Dump qw/dump/;
9    
10    use server;
11    use format;
12    use ip;
13    use ping;
14    
15    our $debug = $server::debug;
16    
17    sub mkbasedir {
18            my $path = shift;
19            $path =~ s{(^.*)/[^/]+$}{$1};
20            mkdir $path unless -d $path;
21            return $path;
22    }
23    
24    sub mac_path { $server::conf . '/mac/' . $_[0] }
25    sub  ip_path { $server::conf . '/ip/'  . join('/', @_) }
26    sub conf_value {
27            my $path = shift;
28            my $value;
29            if ( -l $path ) {
30                    $value = readlink $path;
31                    $value =~ s{.*/([^/]+)$}{$1};
32            } elsif ( -f $path ) {
33                    $value = read_file $path;
34            } else {
35                    warn "W: $path not file or symlink\n";
36            }
37            return $value;
38    }
39    
40  sub conf {  sub conf {
41          my $ip  = shift;          my $ip  = shift;
# Line 18  sub conf { Line 47  sub conf {
47                  $default = $_[1]                  $default = $_[1]
48          }          }
49    
50          my $path ="$server::conf/ip/$ip";          my $path = ip_path $ip;
51          mkdir $path unless -d $path;          mkdir $path unless -d $path;
52          $path .= '/' . $name;          $path .= '/' . $name;
53    
54          if ( defined $value ) {          if ( defined $value ) {
55                    mkbasedir  $path;
56                  write_file $path, $value;                  write_file $path, $value;
57                  warn "update $path = $value";                  warn "update $path = $value";
58          } elsif ( ! -e $path && defined $default ) {          } elsif ( ! -e $path && defined $default ) {
59                    mkbasedir  $path;
60                  write_file $path, $default;                  write_file $path, $default;
61                  warn "default $path = $default";                  warn "default $path = $default";
62                  $value = $default;                  $value = $default;
63          } elsif ( -e $path ) {          } elsif ( -f $path ) {
64                  if ( -l $path ) {                  $value = read_file $path;
65                          $value = readlink $path;          } else {
66                          $value =~ s{.*/([^/]+)$}{$1};                  warn "# $name missing $path\n" if $debug;
                 } else {  
                         $value = read_file $path;  
                 }  
67          }          }
68          return $value;          return $value;
69  }  }
70    
71  sub mac {  sub all_conf {
72          my ( $ip, $op ) = @_;          my $ip = shift;
73          $op ||= 'html';          my $path = ip_path $ip || return;
74          my $mac = client::conf( $ip, 'mac' );          my $conf;
75          return '' unless $mac;          foreach my $file ( glob("$path/*") ) {
76          $mac =~ s{(..)}{$1:}g;                  my $name = $file;
77          $mac =~ s{:$}{};                  $name =~ s{^.+/([^/]+)$}{$1};
78          $mac = qq|<tt>$mac</tt>| if (caller(1))[3] =~ m{^httpd} && $op ne 'clean';                  $conf->{ $name } = read_file $file;
79          return uc($mac);          }
80            return $conf;
81  }  }
82    sub next_ip($) {
83  sub next_ip {          my $mac = shift;
84            $mac = format::mac($mac);
85          my $p = Net::Ping->new;  
86            if ( my $clients_left = server::shared( 'new_clients' ) ) {
87                    server::shared( 'new_clients', $clients_left - 1 );
88            } else {
89                    warn "W: no new clients accepted";
90                    return '0.0.0.0';
91            }
92    
93          my $prefix = $server::ip;          my $prefix = $server::ip;
94          $prefix =~ s{\.\d+$}{.};          $prefix =~ s{\.\d+$}{.};
95          my $addr = $server::ip_from || die;          my $addr = $server::ip_from || die;
96          my $ip = $prefix . $addr;          my $ip = $prefix . $addr;
97    
98          while ( -e "$server::conf/ip/$ip" || $p->ping( $ip, 0.7 ) ) {          while ( -e ip_path($ip) || ping::host($ip) ) {
99                  $ip = $prefix . $addr++;                  $ip = $prefix . $addr++;
100                  die "all addresses allocated!" if $addr == $server::ip_to;                  die "all addresses allocated!" if $addr == $server::ip_to;
101                  warn "skip $ip\n";                  warn "skip $ip\n";
102          }          }
103    
104          warn "next_ip $ip\n";          warn "next_ip $ip\n";
105    
106            save_ip_mac( $ip, $mac );
107    
108          return $ip;          return $ip;
109    }
110    
111    sub save_ip_mac {
112            my ($ip,$mac) = @_;
113            $mac = format::mac($mac);
114            return if $mac eq '00:00:00:00:00:00';
115    
116            mkdir ip_path($ip) unless -e ip_path($ip);
117    
118            my $mac_path = mac_path($mac);
119            unlink $mac_path if -l $mac_path;       # XXX audit?
120            symlink ip_path($ip), $mac_path;
121            write_file( ip_path($ip,'mac'), $mac );
122    }
123    
124    sub ip_from_mac($) {
125            my $mac = shift;
126            $mac = format::mac($mac);
127    
128            my $mac_path = mac_path $mac;
129            return unless -e $mac_path;
130    
131            my $ip;
132    
133            if ( -f $mac_path ) {
134                    $ip = read_file $mac_path;
135                    unlink $mac_path;
136                    symlink ip_path($ip), $mac_path;
137                    warn "I: upgrade to mac symlink $mac_path\n";
138            } elsif ( -l $mac_path ) {
139                    $ip = conf_value $mac_path;
140            } else {
141                    die "$mac_path not file or symlink";
142            }
143    
144            return $ip;
145    }
146    
147    sub mac_from_ip($) {
148            my $ip = shift;
149            conf_value ip_path($ip, 'mac');
150    }
151    
152    sub change_ip($$) {
153            my ($old, $new) = @_;
154            return if $old eq $new;
155            my $mac = mac_from_ip($old) || die "no mac for $old";
156            rename ip_path($old), ip_path($new);
157            unlink mac_path($mac);
158            symlink ip_path($new), mac_path($mac);
159            return $new;
160    }
161    
162    sub all_ips {
163            sort { ip::to_int($a) cmp ip::to_int($b) }
164            map {
165                    my $ip = $_;
166                    $ip =~ s{^.+/ip/}{};
167                    $ip;
168            } glob("$server::conf/ip/*")
169    }
170    
171    sub remove {
172            my $ip = shift;
173            unlink $_ foreach grep { -e $_ } ( glob "$server::conf/ip/$ip/*" );
174            if ( my $mac = mac_from_ip $ip ) {
175                    unlink "$server::conf/mac/$mac";
176            }
177            rmdir "$server::conf/ip/$ip";
178    }
179    
180    sub arp_mac_dev {
181            my $arp = {
182                    map {
183                            my @c = split(/\s+/,$_);
184                            if ( $#c == 5 ) {
185                                    client::save_ip_mac( $c[0], $c[3] );
186                                    ( uc $c[3] => $c[5] )
187                            } else {
188                            }
189                    } read_file('/proc/net/arp')
190            };
191    
192            warn "# arp ",dump( $arp );
193            return $arp;
194  }  }
195    
196  1;  1;

Legend:
Removed from v.190  
changed lines
  Added in v.391

  ViewVC Help
Powered by ViewVC 1.1.26