/[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

Annotation of /bin/dhcpd.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 22 - (hide annotations)
Tue Jul 28 16:35:49 2009 UTC (13 years, 4 months ago) by dpavlin
File MIME type: text/plain
File size: 3186 byte(s)
- correct handling of files and symlinks in conf/
- use autodie for errors
- move detection of server IP into dhcpd
- $gpxe_file defines payload for gPXE

1 dpavlin 1 #!/usr/bin/perl
2    
3     # based on http://www.perlmonks.org/index.pl?node_id=325248
4    
5     use strict;
6     use warnings;
7    
8 dpavlin 22 use autodie;
9    
10 dpavlin 1 use IO::Socket::INET;
11     use Net::DHCP::Packet;
12     use Net::DHCP::Constants;
13 dpavlin 17 use File::Slurp;
14 dpavlin 1 use Data::Dump qw/dump/;
15    
16     die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;
17    
18     my $debug = shift @ARGV;
19    
20 dpavlin 22 our ( $file, $gpxe_file );
21 dpavlin 17 our ( $ip_from, $ip_to ) = ( 10, 100 );
22 dpavlin 1
23 dpavlin 22 our $server_ip = readlink 'conf/server.ip' if -l 'conf/server.ip';
24 dpavlin 17
25 dpavlin 22 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 dpavlin 1 my $sock = IO::Socket::INET->new(
34     LocalPort => 67,
35     # LocalAddr => 'localhost',
36     # LocalAddr => '10.0.0.100',
37     LocalAddr => '0.0.0.0',
38     Proto => 'udp',
39     ReuseAddr => 1,
40     # PeerPort => getservbyname('bootpc', 'udp'),
41     Broadcast => 1,
42     Type => SOCK_DGRAM,
43     ) or die "Failed to bind to socket: $@";
44    
45    
46 dpavlin 17 my $addr = $ip_from;
47    
48 dpavlin 1 sub client_ip {
49     my ( $mac ) = @_;
50    
51 dpavlin 17 my $conf = "conf/$server_ip";
52     mkdir $conf unless -e $conf;
53 dpavlin 1
54 dpavlin 22 if ( -e "$conf/mac/$mac" ) {
55     my $ip = read_file "$conf/mac/$mac";
56 dpavlin 17 print "$mac old $ip\n";
57     return $ip;
58     }
59 dpavlin 1
60 dpavlin 17 mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );
61    
62     my $prefix = $server_ip;
63     $prefix =~ s{\.\d+$}{.};
64     my $ip = $prefix . $addr;
65     while ( -e "conf/ip/$ip" ) {
66     $ip = $prefix . $addr++;
67     die "all addresses allocated!" if $addr == $ip_to;
68 dpavlin 1 }
69    
70 dpavlin 17 write_file "$conf/mac/$mac", $ip;
71 dpavlin 22 unlink "$conf/ip/$ip" if -e "$conf/ip/$ip";
72     symlink "$conf/mac/$mac", "$conf/ip/$ip";
73 dpavlin 17
74     print "$mac NEW $ip\n";
75    
76 dpavlin 1 return $ip;
77     }
78    
79     while (1) {
80    
81 dpavlin 12 require "config.pl"; # refresh config
82    
83 dpavlin 1 print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";
84    
85     my $buf;
86     $sock->recv($buf, 1024);
87 dpavlin 17 print "<< ",$sock->peerhost,":",$sock->peerport,"\n";
88 dpavlin 1
89     if (defined $buf) {
90    
91     my $dhcp;
92    
93     eval { $dhcp = Net::DHCP::Packet->new($buf); };
94     die "can't use request", dump( $buf ) if $@;
95    
96     if ( $debug ) {
97     warn "recv: ", $dhcp->toString, "\n\n";
98     }
99    
100     my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
101     my $ip = client_ip($mac);
102 dpavlin 22 my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
103 dpavlin 1
104 dpavlin 22 if ( $user_class eq 'gPXE' ) {
105     $file = $gpxe_file;
106     } elsif ( ! $file ) {
107     $file = 'undionly.kpxe';
108 dpavlin 12 }
109 dpavlin 6
110 dpavlin 1 my $packet = new Net::DHCP::Packet(
111     Op => BOOTREPLY(),
112     Hops => $dhcp->hops(),
113     Xid => $dhcp->xid(),
114     Flags => $dhcp->flags(),
115     Ciaddr => $dhcp->ciaddr(),
116     Yiaddr => $ip,
117 dpavlin 3 Siaddr => $server_ip,
118 dpavlin 1 Giaddr => $dhcp->giaddr(),
119     Chaddr => $dhcp->chaddr(),
120 dpavlin 6 File => $file,
121 dpavlin 1 # DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
122     DHO_SUBNET_MASK() => '255.0.0.0',
123     );
124    
125 dpavlin 22 warn ">> $mac == $ip server: $server_ip file: $file\n";
126    
127 dpavlin 1 warn "## ",$packet->toString(),"\n" if $debug;
128    
129     my $reply = IO::Socket::INET->new(
130 dpavlin 3 LocalAddr => $server_ip,
131 dpavlin 1 LocalPort => 67,
132     Proto => "udp",
133     Broadcast => 1,
134     PeerAddr => '255.255.255.255',
135     PeerPort => 68,
136     Reuse => 1,
137     ) or die "socket: $@";
138    
139     my $buff = $packet->serialize();
140     $reply->send( $buff, 0 ) or die "Error sending: $!\n";
141    
142     # system("arp -s $ip $mac"),
143    
144     } else {
145     print "No bootp request.\n";
146     }
147    
148     }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26