/[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 177 - (show annotations)
Fri Aug 7 14:08:21 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 5280 byte(s)
fix mac symlink creation
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
22 use lib '..';
23 use Net::DHCP::Packet;
24 use Net::DHCP::Constants 0.67;
25
26 use server;
27 my $debug = server::debug;
28
29 if ( ! $server::ip ) {
30 my $server_ip = `/sbin/ifconfig`;
31 $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
32 $server::ip = $server_ip;
33 }
34
35 warn "server ip $server::ip range: $server::ip_from - $server::ip_to\n";
36
37 use client;
38
39 sub client_ip {
40 my ( $mac ) = @_;
41
42 my $conf = $server::conf;
43 mkdir $conf unless -e $conf;
44
45 my $ip;
46
47 if ( -e "$conf/mac/$mac" ) {
48 $ip = read_file "$conf/mac/$mac";
49 print "RENEW $mac $ip\n";
50 return $ip;
51 } else {
52 $ip = client::next_ip;
53 print "NEW $mac $ip\n";
54 write_file "$conf/mac/$mac", $ip;
55 }
56
57 my $ip_path = "$conf/ip/$ip";
58 mkdir $ip_path unless -e $ip_path;
59
60 if ( -l "$ip_path/mac" && readlink "$ip_path/mac" ne "$conf/mac/$mac" ) {
61 warn "$mac IP changed from ", readlink "$ip_path/mac", " to $ip";
62 unlink "$ip_path/mac";
63 };
64
65 symlink "$conf/mac/$mac", "$ip_path/mac";
66
67 return $ip;
68 }
69
70 use log;
71 use config;
72 use pxelinux;
73 use client;
74
75 our $file;
76 our $transaction = 0; # FIXME predictible transaction numbers
77
78 sub process_packet {
79 my $sock = shift;
80
81 server->refresh;
82
83 my $buf;
84 $sock->recv($buf, 1024);
85 my $size = 'empty';
86 $size = length($buf) . ' bytes' if defined $buf;
87
88 print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n" if $debug;
89 return unless $buf;
90
91 my $dhcp = Net::DHCP::Packet->new($buf);
92 $dhcp->comment( $transaction++ );
93
94 warn "recv: ", $dhcp->toString if $debug;
95
96 my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
97 my $ip = client_ip($mac);
98
99 =for later
100
101 my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
102
103 if ( $user_class eq 'gPXE' ) {
104 $file = $gpxe_file;
105 } elsif ( ! $file ) {
106 $file = 'undionly.kpxe';
107 }
108
109 =cut
110
111 config::for_ip( $ip );
112
113 my $packet = {
114 Op => BOOTREPLY(),
115 Hops => $dhcp->hops(),
116 Xid => $dhcp->xid(),
117 Flags => $dhcp->flags(),
118 Ciaddr => $dhcp->ciaddr(),
119 Yiaddr => $ip,
120 Siaddr => $server::ip,
121 Giaddr => $dhcp->giaddr(),
122 Chaddr => $dhcp->chaddr(),
123 File => $file,
124 DHO_DHCP_SERVER_IDENTIFIER() => $server::ip, # busybox/udhcpc needs it but doesn't request
125 };
126
127 my $options = {
128 DHO_SUBNET_MASK() => $server::netmask,
129 DHO_ROUTERS() => $server::ip,
130 DHO_DOMAIN_NAME() => $server::domain_name,
131 DHO_NAME_SERVERS() => $server::ip,
132 DHO_DOMAIN_NAME_SERVERS() => $server::ip,
133 DHO_HOST_NAME() => client::conf( $ip, 'hostname' ),
134 DHO_BROADCAST_ADDRESS() => $server::bcast,
135 # DHO_NTP_SERVERS() => '',
136 };
137
138 my @requested = split(/\s/, $dhcp->getOptionValue(DHO_DHCP_PARAMETER_REQUEST_LIST));
139 warn "options ",dump( $options ), ' requested: ',dump( @requested ) if $debug;
140
141 my @missing;
142 foreach ( @requested ) {
143 if ( defined $options->{$_} ) {
144 $packet->{$_} = $options->{$_};
145 } else {
146 push @missing, $_;
147 }
148 }
149
150 warn "W: options requested but missing: ",dump( @missing ),$/;
151
152 foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) {
153 my $DH0 = eval 'DHO_PXELINUX_' . uc $opt;
154 warn "DH0: $@" if $@;
155 my $v = eval "\$pxelinux::$opt";
156 warn "v: $@" if $@;
157 next unless defined $v;
158 warn "pxelinux dhcp option $opt = $DH0 = $v\n" if $debug;
159 $packet->{ $DH0 } = $v;
160 }
161
162 my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
163
164 if ($messagetype eq DHCPDISCOVER()) {
165 log::mac $mac, "DHCP DISCOVER";
166 $packet->{Comment} = $dhcp->comment();
167 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
168 } elsif ($messagetype eq DHCPREQUEST()) {
169 my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
170 log::mac $mac, "DHCP REQUEST $requested_ip $ip $file";
171 if ( $ip eq $requested_ip ) {
172 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK();
173 $packet->{DHO_DHCP_LEASE_TIME()} = 5 * 60; # 5 min
174 # $packet->{DHO_ROOT_PATH()} = '/exports/foobar';
175 } else {
176 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
177 $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
178 }
179 } elsif ($messagetype eq DHCPINFORM()) {
180 log::mac $mac, "DHCP INFORM ignored";
181 } else {
182 log::mac $mac, "$messagetype igored (bootp?)";
183 }
184
185 warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug;
186
187 $packet = new Net::DHCP::Packet( %$packet );
188 warn "send ",$packet->toString() if $debug;
189
190 my $reply = IO::Socket::INET->new(
191 LocalAddr => $server::ip,
192 LocalPort => 67,
193 Proto => "udp",
194 Broadcast => 1,
195 PeerAddr => '255.255.255.255',
196 PeerPort => 68,
197 Reuse => 1,
198 ) or die "socket: $@";
199
200 my $buff = $packet->serialize();
201 $reply->send( $buff, 0 ) or die "Error sending: $!\n";
202
203 # system("arp -s $ip $mac"),
204
205 }
206
207 sub start {
208
209 my $sock = IO::Socket::INET->new(
210 LocalPort => 67,
211 # LocalAddr => 'localhost',
212 # LocalAddr => '10.0.0.100',
213 LocalAddr => '0.0.0.0',
214 Proto => 'udp',
215 ReuseAddr => 1,
216 # PeerPort => getservbyname('bootpc', 'udp'),
217 Broadcast => 1,
218 Type => SOCK_DGRAM,
219 ) or die "Failed to bind to socket: $@";
220
221 print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
222
223 while (1) {
224 process_packet $sock;
225 }
226 }
227
228 warn "loaded";
229
230 1;

  ViewVC Help
Powered by ViewVC 1.1.26