/[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 45 - (show annotations)
Wed Jul 29 22:04:58 2009 UTC (12 years, 1 month ago) by dpavlin
File size: 4564 byte(s)
dump all other peaces in, first configuration which can again boot
(wrong image, that is :-)

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26