/[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 63 - (show annotations)
Thu Jul 30 16:16:18 2009 UTC (12 years, 1 month ago) by dpavlin
File size: 4592 byte(s)
create pre IP configuration for pxelinux

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 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 pxe::config_for_ip( $ip );
126
127 my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
128
129 if ($messagetype eq DHCPDISCOVER()) {
130 warn "DHCP DISCOVER";
131 $packet->{Comment} = $dhcp->comment();
132 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
133 } elsif ($messagetype eq DHCPREQUEST()) {
134 my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
135 warn "DHCP REQUEST $requested_ip";
136 if ( $ip eq $requested_ip ) {
137 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK();
138 $packet->{DHO_DHCP_LEASE_TIME()} = 5 * 60; # 5 min
139 # $packet->{DHO_DHCP_SERVER_IDENTIFIER()} = $server::ip; # FIXME
140 $packet->{DHO_SUBNET_MASK()} = '255.255.255.0';
141 $packet->{DHO_ROUTERS()} = $server::ip;
142 # $packet->{DHO_DOMAIN_NAME()} = 'pxelator.lan';
143 # $packet->{DHO_NAME_SERVERS()} = $server::ip;
144 # $packet->{DHO_ROOT_PATH()} = '/exports/foobar';
145 } else {
146 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
147 $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
148 }
149 } elsif ($messagetype eq DHCPINFORM()) {
150 warn "DHCP INFORM ignored";
151 } else {
152 warn "$messagetype igored (bootp?)";
153 }
154
155 warn ">> $mac == $ip server: $server::ip", $pxe::file ? " pxe file: $pxe::file\n" : "\n";
156
157 $packet = new Net::DHCP::Packet( %$packet );
158 warn "## ",$packet->toString(),"\n" if $debug;
159
160 my $reply = IO::Socket::INET->new(
161 LocalAddr => $server::ip,
162 LocalPort => 67,
163 Proto => "udp",
164 Broadcast => 1,
165 PeerAddr => '255.255.255.255',
166 PeerPort => 68,
167 Reuse => 1,
168 ) or die "socket: $@";
169
170 my $buff = $packet->serialize();
171 $reply->send( $buff, 0 ) or die "Error sending: $!\n";
172
173 # system("arp -s $ip $mac"),
174
175 }
176
177 sub start {
178
179 my $sock = IO::Socket::INET->new(
180 LocalPort => 67,
181 # LocalAddr => 'localhost',
182 # LocalAddr => '10.0.0.100',
183 LocalAddr => '0.0.0.0',
184 Proto => 'udp',
185 ReuseAddr => 1,
186 # PeerPort => getservbyname('bootpc', 'udp'),
187 Broadcast => 1,
188 Type => SOCK_DGRAM,
189 ) or die "Failed to bind to socket: $@";
190
191 print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n";
192
193 while (1) {
194 Module::Refresh->refresh;
195 process_packet $sock;
196 }
197 }
198
199 warn "loaded";
200
201 1;

  ViewVC Help
Powered by ViewVC 1.1.26