/[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 72 - (show annotations)
Thu Jul 30 23:57:19 2009 UTC (11 years, 10 months ago) by dpavlin
File size: 4668 byte(s)
record and display log changes by mac addresses

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

  ViewVC Help
Powered by ViewVC 1.1.26