/[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 44 - (show annotations)
Wed Jul 29 20:55:44 2009 UTC (12 years, 3 months ago) by dpavlin
File size: 4514 byte(s)
cleanup dhcpd server

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
30 my $debug = 1;
31
32 our ( $file, $gpxe_file );
33 our ( $ip_from, $ip_to ) = ( 10, 100 );
34
35 if ( ! $server::ip ) {
36 my $server_ip = `/sbin/ifconfig`;
37 $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
38 $server::ip = $server_ip;
39 }
40
41 warn "server ip $server::ip\n";
42
43 my $addr = $ip_from;
44
45 sub client_ip {
46 my ( $mac ) = @_;
47
48 my $conf = "conf/$server::ip";
49 mkdir $conf unless -e $conf;
50
51 if ( -e "$conf/mac/$mac" ) {
52 my $ip = read_file "$conf/mac/$mac";
53 print "$mac old $ip\n";
54 return $ip;
55 }
56
57 mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );
58
59 my $p = Net::Ping->new;
60
61 my $prefix = $server::ip;
62 $prefix =~ s{\.\d+$}{.};
63 my $ip = $prefix . $addr;
64 while ( -e "conf/ip/$ip" || $p->ping( $ip ) ) {
65 $ip = $prefix . $addr++;
66 die "all addresses allocated!" if $addr == $ip_to;
67 }
68
69 write_file "$conf/mac/$mac", $ip;
70 if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac") {
71 unlink "$conf/ip/$ip";
72 symlink "$conf/mac/$mac", "$conf/ip/$ip";
73 warn "$mac IP changed to $ip";
74 }
75
76 print "$mac NEW $ip\n";
77
78 return $ip;
79 }
80
81 our $transaction = 0; # FIXME predictible transaction numbers
82
83 sub process_packet {
84 my $sock = shift;
85
86 my $buf;
87 $sock->recv($buf, 1024);
88 my $size = 'empty';
89 $size = length($buf) . ' bytes' if defined $buf;
90
91 print "packet from ",$sock->peerhost,":",$sock->peerport," $size\n";
92 return unless $buf;
93
94 my $dhcp = Net::DHCP::Packet->new($buf);
95 $dhcp->comment( $transaction++ );
96
97 warn "recv: ", $dhcp->toString;
98
99 my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
100 my $ip = client_ip($mac);
101
102 =for later
103
104 my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
105
106 if ( $user_class eq 'gPXE' ) {
107 $file = $gpxe_file;
108 } elsif ( ! $file ) {
109 $file = 'undionly.kpxe';
110 }
111
112 =cut
113
114 my $packet = {
115 Op => BOOTREPLY(),
116 Hops => $dhcp->hops(),
117 Xid => $dhcp->xid(),
118 Flags => $dhcp->flags(),
119 Ciaddr => $dhcp->ciaddr(),
120 Yiaddr => $ip,
121 Siaddr => $server::ip,
122 Giaddr => $dhcp->giaddr(),
123 Chaddr => $dhcp->chaddr(),
124 File => $file,
125 };
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", $file ? " file: $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 1;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26