/[mdap]/bin/bootpd.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/bootpd.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 68 - (show annotations)
Sun Nov 18 01:29:04 2007 UTC (11 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 2252 byte(s)
 r97@brr:  dpavlin | 2007-11-18 02:29:00 +0100
 - can be started with any parametar to produce debug output
 - make IP pool 10.0.0.10-10.0.0.99
 - report mac addess for assigned IP

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 my $sock = IO::Socket::INET->new(
18 LocalPort => 67,
19 # LocalAddr => 'localhost',
20 # LocalAddr => '10.0.0.100',
21 LocalAddr => '0.0.0.0',
22 Proto => 'udp',
23 ReuseAddr => 1,
24 # PeerPort => getservbyname('bootpc', 'udp'),
25 Broadcast => 1,
26 Type => SOCK_DGRAM,
27 ) or die "Failed to bind to socket: $@";
28
29 my $device = 'eth1';
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 = "10.0.0.$_ip";
41 $_mac2ip->{$mac} = $ip;
42
43 $_ip++;
44 if ( $_ip == 100 ) {
45 warn "IP roll-over to 10\n";
46 $_ip = 10;
47 }
48
49 return $ip;
50 }
51
52 my $ip_server = '10.0.0.100';
53
54 while (1) {
55
56 print "waiting for BOOTP 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 => $ip_server,
84 Giaddr => $dhcp->giaddr(),
85 Chaddr => $dhcp->chaddr(),
86 File => $dhcp->file(),
87 # DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
88 DHO_SUBNET_MASK() => '255.0.0.0',
89 );
90
91 warn ">> $mac == $ip server $ip_server\n";
92
93 warn "## ",$packet->toString(),"\n" if $debug;
94
95 my $reply = IO::Socket::INET->new(
96 LocalAddr => $ip_server,
97 LocalPort => 67,
98 Proto => "udp",
99 Broadcast => 1,
100 PeerAddr => '255.255.255.255',
101 PeerPort => 68,
102 Reuse => 1,
103 ) or die "socket: $@";
104
105 my $buff = $packet->serialize();
106 $reply->send( $buff, 0 ) or die "Error sending: $!\n";
107
108 # system("arp -s $ip $mac"),
109
110 } else {
111 print "No bootp request.\n";
112 }
113
114 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26