/[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 4 - (show annotations)
Sun Jul 26 14:21:48 2009 UTC (10 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 2322 byte(s)
added next file support so we deliver first gpxe and than config
examples for tftp and http boot of tinycore

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 my $_ip_file;
35
36 sub client_ip {
37 my ( $mac ) = @_;
38
39 my $ip = $_mac2ip->{$mac};
40 return $ip if $ip;
41
42 $ip = "10.0.0.$_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 $packet = new Net::DHCP::Packet(
77 Op => BOOTREPLY(),
78 Hops => $dhcp->hops(),
79 Xid => $dhcp->xid(),
80 Flags => $dhcp->flags(),
81 Ciaddr => $dhcp->ciaddr(),
82 Yiaddr => $ip,
83 Siaddr => $server_ip,
84 Giaddr => $dhcp->giaddr(),
85 Chaddr => $dhcp->chaddr(),
86 File => $_ip_file->{$ip} || 'undionly.kpxe',
87 # DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
88 DHO_SUBNET_MASK() => '255.0.0.0',
89 );
90
91 $_ip_file->{$ip} = $next_file;
92
93 warn ">> $mac == $ip server $server_ip\n";
94
95 warn "## ",$packet->toString(),"\n" if $debug;
96
97 my $reply = IO::Socket::INET->new(
98 LocalAddr => $server_ip,
99 LocalPort => 67,
100 Proto => "udp",
101 Broadcast => 1,
102 PeerAddr => '255.255.255.255',
103 PeerPort => 68,
104 Reuse => 1,
105 ) or die "socket: $@";
106
107 my $buff = $packet->serialize();
108 $reply->send( $buff, 0 ) or die "Error sending: $!\n";
109
110 # system("arp -s $ip $mac"),
111
112 } else {
113 print "No bootp request.\n";
114 }
115
116 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26