/[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 12 - (show annotations)
Mon Jul 27 22:18:51 2009 UTC (11 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 2449 byte(s)
make first pxe payload configurable
(and use gPXE if there is no setup for $file)
beginning of symlink farm type configuration

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26