/[pxelator]/lib/PXElator/dhcpd.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

Contents of /lib/PXElator/dhcpd.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 110 - (show annotations)
Sun Aug 2 02:04:00 2009 UTC (11 years, 5 months ago) by dpavlin
File size: 4684 byte(s)
cleanup $file and move it to dhcpd, added config::ip_for to refresh it
1 package dhcpd;
2
3 =head1 dhcpd
4
5 start with:
6
7 perl -Ilib/PXElator -Ilib -Mdhcpd -e start
8
9 based on L<http://www.perlmonks.org/index.pl?node_id=325248>
10
11 =cut
12
13 use strict;
14 use warnings;
15
16 use autodie;
17
18 use IO::Socket::INET;
19 use File::Slurp;
20 use Data::Dump qw/dump/;
21 use Net::Ping;
22 use Module::Refresh;
23
24 use lib '..';
25 use Net::DHCP::Packet;
26 use Net::DHCP::Constants 0.67;
27
28 use server;
29 my $debug = server::debug;
30
31 if ( ! $server::ip ) {
32 my $server_ip = `/sbin/ifconfig`;
33 $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
34 $server::ip = $server_ip;
35 }
36
37 warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n";
38
39 my $addr = $server::ip_from;
40
41 sub client_ip {
42 my ( $mac ) = @_;
43
44 my $conf = "$server::base_dir/conf/$server::ip";
45 mkdir $conf unless -e $conf;
46
47 if ( -e "$conf/mac/$mac" ) {
48 my $ip = read_file "$conf/mac/$mac";
49 print "RENEW $mac $ip\n";
50 return $ip;
51 }
52
53 mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );
54
55 my $p = Net::Ping->new;
56
57 my $prefix = $server::ip;
58 $prefix =~ s{\.\d+$}{.};
59 my $ip = $prefix . $addr;
60 while ( -e "conf/ip/$ip" || $p->ping( $ip ) ) {
61 $ip = $prefix . $addr++;
62 die "all addresses allocated!" if $addr == $server::ip_to;
63 }
64
65 write_file "$conf/mac/$mac", $ip;
66 if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac") {
67 unlink "$conf/ip/$ip";
68 symlink "$conf/mac/$mac", "$conf/ip/$ip";
69 warn "$mac IP changed to $ip";
70 }
71
72 print "$mac NEW $ip\n";
73
74 return $ip;
75 }
76
77 use log;
78 use config;
79
80 our $file;
81 our $transaction = 0; # FIXME predictible transaction numbers
82
83 sub process_packet {
84 my $sock = shift;
85
86 my $buf;
87 $sock->recv($buf, 1024);
88 my $size = 'empty';
89 $size = length($buf) . ' bytes' if defined $buf;
90
91 print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n" if $debug;
92 return unless $buf;
93
94 my $dhcp = Net::DHCP::Packet->new($buf);
95 $dhcp->comment( $transaction++ );
96
97 warn "recv: ", $dhcp->toString if $debug;
98
99 my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
100 my $ip = client_ip($mac);
101
102 =for later
103
104 my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
105
106 if ( $user_class eq 'gPXE' ) {
107 $file = $gpxe_file;
108 } elsif ( ! $file ) {
109 $file = 'undionly.kpxe';
110 }
111
112 =cut
113
114 config::for_ip( $ip );
115
116 my $packet = {
117 Op => BOOTREPLY(),
118 Hops => $dhcp->hops(),
119 Xid => $dhcp->xid(),
120 Flags => $dhcp->flags(),
121 Ciaddr => $dhcp->ciaddr(),
122 Yiaddr => $ip,
123 Siaddr => $server::ip,
124 Giaddr => $dhcp->giaddr(),
125 Chaddr => $dhcp->chaddr(),
126 File => $file,
127 };
128
129 my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
130
131 if ($messagetype eq DHCPDISCOVER()) {
132 log::mac $mac, "DHCP DISCOVER";
133 $packet->{Comment} = $dhcp->comment();
134 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
135 } elsif ($messagetype eq DHCPREQUEST()) {
136 my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
137 log::mac $mac, "DHCP REQUEST $requested_ip $ip $file";
138 if ( $ip eq $requested_ip ) {
139 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK();
140 $packet->{DHO_DHCP_LEASE_TIME()} = 5 * 60; # 5 min
141 # $packet->{DHO_DHCP_SERVER_IDENTIFIER()} = $server::ip; # FIXME
142 $packet->{DHO_SUBNET_MASK()} = '255.255.255.0';
143 $packet->{DHO_ROUTERS()} = $server::ip;
144 # $packet->{DHO_DOMAIN_NAME()} = 'pxelator.lan';
145 # $packet->{DHO_NAME_SERVERS()} = $server::ip;
146 # $packet->{DHO_ROOT_PATH()} = '/exports/foobar';
147 } else {
148 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
149 $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
150 }
151 } elsif ($messagetype eq DHCPINFORM()) {
152 log::mac $mac, "DHCP INFORM ignored";
153 } else {
154 log::mac $mac, "$messagetype igored (bootp?)";
155 }
156
157 warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;
158
159 $packet = new Net::DHCP::Packet( %$packet );
160 warn "send ",$packet->toString() if $debug;
161
162 my $reply = IO::Socket::INET->new(
163 LocalAddr => $server::ip,
164 LocalPort => 67,
165 Proto => "udp",
166 Broadcast => 1,
167 PeerAddr => '255.255.255.255',
168 PeerPort => 68,
169 Reuse => 1,
170 ) or die "socket: $@";
171
172 my $buff = $packet->serialize();
173 $reply->send( $buff, 0 ) or die "Error sending: $!\n";
174
175 # system("arp -s $ip $mac"),
176
177 }
178
179 sub start {
180
181 my $sock = IO::Socket::INET->new(
182 LocalPort => 67,
183 # LocalAddr => 'localhost',
184 # LocalAddr => '10.0.0.100',
185 LocalAddr => '0.0.0.0',
186 Proto => 'udp',
187 ReuseAddr => 1,
188 # PeerPort => getservbyname('bootpc', 'udp'),
189 Broadcast => 1,
190 Type => SOCK_DGRAM,
191 ) or die "Failed to bind to socket: $@";
192
193 print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
194
195 while (1) {
196 Module::Refresh->refresh;
197 process_packet $sock;
198 }
199 }
200
201 warn "loaded";
202
203 1;

  ViewVC Help
Powered by ViewVC 1.1.26