/[pxelator]/bin/dhcpd.pl
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 /bin/dhcpd.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 40 - (show annotations)
Wed Jul 29 14:29:55 2009 UTC (10 years ago) by dpavlin
File MIME type: text/plain
File size: 4505 byte(s)
added a bunch of dhcp options

1 #!/usr/bin/perl
2
3 # based on http://www.perlmonks.org/index.pl?node_id=325248
4
5 use strict;
6 use warnings;
7
8 use autodie;
9
10 use IO::Socket::INET;
11 use File::Slurp;
12 use Data::Dump qw/dump/;
13 use Net::Ping;
14
15 use lib 'lib';
16 use Net::DHCP::Packet;
17 use Net::DHCP::Constants 0.67;
18 die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;
19
20 my $debug = shift @ARGV;
21
22 our ( $file, $gpxe_file );
23 our ( $ip_from, $ip_to ) = ( 10, 100 );
24
25 our $server_ip = readlink 'conf/server.ip' if -l 'conf/server.ip';
26
27 if ( ! $server_ip ) {
28 $server_ip = `/sbin/ifconfig`;
29 $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
30 warn "auto-configure server ip to $server_ip\n";
31 } else {
32 warn "server ip $server_ip\n";
33 }
34
35 my $sock = IO::Socket::INET->new(
36 LocalPort => 67,
37 # LocalAddr => 'localhost',
38 # LocalAddr => '10.0.0.100',
39 LocalAddr => '0.0.0.0',
40 Proto => 'udp',
41 ReuseAddr => 1,
42 # PeerPort => getservbyname('bootpc', 'udp'),
43 Broadcast => 1,
44 Type => SOCK_DGRAM,
45 ) or die "Failed to bind to socket: $@";
46
47
48 my $addr = $ip_from;
49
50 sub client_ip {
51 my ( $mac ) = @_;
52
53 my $conf = "conf/$server_ip";
54 mkdir $conf unless -e $conf;
55
56 if ( -e "$conf/mac/$mac" ) {
57 my $ip = read_file "$conf/mac/$mac";
58 print "$mac old $ip\n";
59 return $ip;
60 }
61
62 mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );
63
64 my $p = Net::Ping->new;
65
66 my $prefix = $server_ip;
67 $prefix =~ s{\.\d+$}{.};
68 my $ip = $prefix . $addr;
69 while ( -e "conf/ip/$ip" || $p->ping( $ip ) ) {
70 $ip = $prefix . $addr++;
71 die "all addresses allocated!" if $addr == $ip_to;
72 }
73
74 write_file "$conf/mac/$mac", $ip;
75 if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac") {
76 unlink "$conf/ip/$ip";
77 symlink "$conf/mac/$mac", "$conf/ip/$ip";
78 warn "$mac IP changed to $ip";
79 }
80
81 print "$mac NEW $ip\n";
82
83 return $ip;
84 }
85
86 my $transaction = 0; # FIXME predictible transaction numbers
87
88 while (1) {
89
90 require "config.pl"; # refresh config
91
92 print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";
93
94 my $buf;
95 $sock->recv($buf, 1024);
96 print "<< ",$sock->peerhost,":",$sock->peerport,"\n";
97
98 if (defined $buf) {
99
100 my $dhcp = Net::DHCP::Packet->new($buf);
101 $dhcp->comment( $transaction++ );
102
103 warn "recv: ", $dhcp->toString, "\n\n";
104
105 my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
106 my $ip = client_ip($mac);
107 my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
108
109 if ( $user_class eq 'gPXE' ) {
110 $file = $gpxe_file;
111 } elsif ( ! $file ) {
112 $file = 'undionly.kpxe';
113 }
114
115 my $packet = {
116 Op => BOOTREPLY(),
117 Hops => $dhcp->hops(),
118 Xid => $dhcp->xid(),
119 Flags => $dhcp->flags(),
120 Ciaddr => $dhcp->ciaddr(),
121 Yiaddr => $ip,
122 Siaddr => $server_ip,
123 Giaddr => $dhcp->giaddr(),
124 Chaddr => $dhcp->chaddr(),
125 File => $file,
126 };
127
128 my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
129
130 if ($messagetype eq DHCPDISCOVER()) {
131 warn "DHCP DISCOVER";
132 $packet->{Comment} = $dhcp->comment();
133 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
134 } elsif ($messagetype eq DHCPREQUEST()) {
135 my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
136 warn "DHCP REQUEST $requested_ip";
137 if ( $ip eq $requested_ip ) {
138 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK();
139 $packet->{DHO_DHCP_LEASE_TIME()} = 5 * 60; # 5 min
140 # $packet->{DHO_DHCP_SERVER_IDENTIFIER()} = $server_ip; # FIXME
141 $packet->{DHO_SUBNET_MASK()} = '255.255.255.0';
142 $packet->{DHO_ROUTERS()} = $server_ip;
143 # $packet->{DHO_DOMAIN_NAME()} = 'pxelator.lan';
144 # $packet->{DHO_NAME_SERVERS()} = $server_ip;
145 # $packet->{DHO_ROOT_PATH()} = '/exports/foobar';
146 } else {
147 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
148 $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
149 }
150 } elsif ($messagetype eq DHCPINFORM()) {
151 warn "DHCP INFORM ignored";
152 } else {
153 warn "$messagetype igored (bootp?)";
154 }
155
156
157 warn ">> $mac == $ip server: $server_ip", $file ? " file: $file\n" : "\n";
158
159 $packet = new Net::DHCP::Packet( %$packet );
160 warn "## ",$packet->toString(),"\n" if $debug;
161
162 my $reply = IO::Socket::INET->new(
163 LocalAddr => $server_ip,
164 LocalPort => 67,
165 Proto => "udp",
166 Broadcast => 1,
167 PeerAddr => '255.255.255.255',
168 PeerPort => 68,
169 Reuse => 1,
170 ) or die "socket: $@";
171
172 my $buff = $packet->serialize();
173 $reply->send( $buff, 0 ) or die "Error sending: $!\n";
174
175 # system("arp -s $ip $mac"),
176
177 } else {
178 print "No bootp request.\n";
179 }
180
181 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26