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

  ViewVC Help
Powered by ViewVC 1.1.26