/[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 8 - (show annotations)
Mon Jul 27 11:46:44 2009 UTC (10 years ago) by dpavlin
File MIME type: text/plain
File size: 2385 byte(s)
use $server_ip as base for IP class to hand out

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 IO::Socket::INET;
9 use Net::DHCP::Packet;
10 use Net::DHCP::Constants;
11 use Data::Dump qw/dump/;
12
13 die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;
14
15 my $debug = shift @ARGV;
16
17 our ( $server_ip, $next_file );
18 require "config.pl";
19
20 my $sock = IO::Socket::INET->new(
21 LocalPort => 67,
22 # LocalAddr => 'localhost',
23 # LocalAddr => '10.0.0.100',
24 LocalAddr => '0.0.0.0',
25 Proto => 'udp',
26 ReuseAddr => 1,
27 # PeerPort => getservbyname('bootpc', 'udp'),
28 Broadcast => 1,
29 Type => SOCK_DGRAM,
30 ) or die "Failed to bind to socket: $@";
31
32 my $_ip = 10;
33 my $_mac2ip;
34
35 sub client_ip {
36 my ( $mac ) = @_;
37
38 my $ip = $_mac2ip->{$mac};
39 return $ip if $ip;
40
41 $ip = $server_ip;
42 $ip =~ s{\.\d+$}{.$_ip};
43 $_mac2ip->{$mac} = $ip;
44
45 $_ip++;
46 if ( $_ip == 100 ) {
47 warn "IP roll-over to 10\n";
48 $_ip = 10;
49 }
50
51 return $ip;
52 }
53
54 while (1) {
55
56 print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";
57
58 my $buf;
59 $sock->recv($buf, 1024);
60 print "<< peer:",$sock->peerhost,":",$sock->peerport,"\n";
61
62 if (defined $buf) {
63
64 my $dhcp;
65
66 eval { $dhcp = Net::DHCP::Packet->new($buf); };
67 die "can't use request", dump( $buf ) if $@;
68
69 if ( $debug ) {
70 warn "recv: ", $dhcp->toString, "\n\n";
71 }
72
73 my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
74 my $ip = client_ip($mac);
75
76 my $file = $next_file;
77 $file = 'undionly.kpxe' if ! $file || $dhcp->getOptionValue(DHO_USER_CLASS()) ne 'gPXE';
78
79 my $packet = new Net::DHCP::Packet(
80 Op => BOOTREPLY(),
81 Hops => $dhcp->hops(),
82 Xid => $dhcp->xid(),
83 Flags => $dhcp->flags(),
84 Ciaddr => $dhcp->ciaddr(),
85 Yiaddr => $ip,
86 Siaddr => $server_ip,
87 Giaddr => $dhcp->giaddr(),
88 Chaddr => $dhcp->chaddr(),
89 File => $file,
90 # DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
91 DHO_SUBNET_MASK() => '255.0.0.0',
92 );
93
94 warn ">> $mac == $ip server $server_ip\n";
95
96 warn "## ",$packet->toString(),"\n" if $debug;
97
98 my $reply = IO::Socket::INET->new(
99 LocalAddr => $server_ip,
100 LocalPort => 67,
101 Proto => "udp",
102 Broadcast => 1,
103 PeerAddr => '255.255.255.255',
104 PeerPort => 68,
105 Reuse => 1,
106 ) or die "socket: $@";
107
108 my $buff = $packet->serialize();
109 $reply->send( $buff, 0 ) or die "Error sending: $!\n";
110
111 # system("arp -s $ip $mac"),
112
113 } else {
114 print "No bootp request.\n";
115 }
116
117 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26