/[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 128 - (show annotations)
Mon Aug 3 19:59:08 2009 UTC (13 years, 6 months ago) by dpavlin
File size: 4864 byte(s)
added back refresh, this time in as server->refresh

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

  ViewVC Help
Powered by ViewVC 1.1.26