/[pxelator]/bin/dhcpd.pl
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 /bin/dhcpd.pl

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

revision 17 by dpavlin, Tue Jul 28 10:29:33 2009 UTC revision 22 by dpavlin, Tue Jul 28 16:35:49 2009 UTC
# Line 5  Line 5 
5  use strict;  use strict;
6  use warnings;  use warnings;
7    
8    use autodie;
9    
10  use IO::Socket::INET;  use IO::Socket::INET;
11  use Net::DHCP::Packet;  use Net::DHCP::Packet;
12  use Net::DHCP::Constants;  use Net::DHCP::Constants;
# Line 15  die "need to run $0 as root like this\ns Line 17  die "need to run $0 as root like this\ns
17    
18  my $debug = shift @ARGV;  my $debug = shift @ARGV;
19    
20  our ( $file, $next_file );  our ( $file, $gpxe_file );
21  our ( $ip_from, $ip_to ) = ( 10, 100 );  our ( $ip_from, $ip_to ) = ( 10, 100 );
22    
23  our $server_ip = readlink 'conf/server.ip';  our $server_ip = readlink 'conf/server.ip' if -l 'conf/server.ip';
24    
25    if ( ! $server_ip ) {
26            $server_ip = `/sbin/ifconfig`;
27            $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
28            warn "auto-configure server ip to $server_ip\n";
29    } else {
30            warn "server ip $server_ip\n";
31    }
32    
33  my $sock = IO::Socket::INET->new(  my $sock = IO::Socket::INET->new(
34          LocalPort       => 67,          LocalPort       => 67,
# Line 41  sub client_ip { Line 51  sub client_ip {
51          my $conf = "conf/$server_ip";          my $conf = "conf/$server_ip";
52          mkdir $conf unless -e $conf;          mkdir $conf unless -e $conf;
53    
54          if ( -e "$conf/$mac" ) {          if ( -e "$conf/mac/$mac" ) {
55                  my $ip = read_file "conf/mac/$mac";                  my $ip = read_file "$conf/mac/$mac";
56                  print "$mac old $ip\n";                  print "$mac old $ip\n";
57                  return $ip;                  return $ip;
58          }          }
# Line 58  sub client_ip { Line 68  sub client_ip {
68          }          }
69    
70          write_file "$conf/mac/$mac", $ip;          write_file "$conf/mac/$mac", $ip;
71          symlink    "$conf/mac/$mac", "conf/ip/$ip";          unlink     "$conf/ip/$ip" if -e "$conf/ip/$ip";
72            symlink    "$conf/mac/$mac", "$conf/ip/$ip";
73    
74          print "$mac NEW $ip\n";          print "$mac NEW $ip\n";
75    
# Line 88  while (1) { Line 99  while (1) {
99    
100                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);                  my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
101                  my $ip = client_ip($mac);                  my $ip = client_ip($mac);
102                    my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
103    
104                  if ( ! $file ) {                  if ( $user_class eq 'gPXE' ) {
105                          if ( $dhcp->getOptionValue(DHO_USER_CLASS()) ne 'gPXE' ) {                          $file = $gpxe_file;
106                                  $file = 'undionly.kpxe';                  } elsif ( ! $file ) {
107                          } else {                          $file = 'undionly.kpxe';
                                 $file = $next_file;  
                         }  
108                  }                  }
109    
110                  my $packet = new Net::DHCP::Packet(                  my $packet = new Net::DHCP::Packet(
# Line 112  while (1) { Line 122  while (1) {
122                  DHO_SUBNET_MASK() => '255.0.0.0',                  DHO_SUBNET_MASK() => '255.0.0.0',
123                  );                  );
124    
125                  warn ">> $mac == $ip server $server_ip\n";                  warn ">> $mac == $ip server: $server_ip file: $file\n";
126                    
127                  warn "## ",$packet->toString(),"\n" if $debug;                  warn "## ",$packet->toString(),"\n" if $debug;
128    
129                  my $reply = IO::Socket::INET->new(                  my $reply = IO::Socket::INET->new(

Legend:
Removed from v.17  
changed lines
  Added in v.22

  ViewVC Help
Powered by ViewVC 1.1.26