/[pxelator]/lib/PXElator/dhcpd.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /lib/PXElator/dhcpd.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (hide annotations)
Sun Jul 26 13:58:39 2009 UTC (14 years, 8 months ago) by dpavlin
Original Path: bin/dhcpd.pl
File MIME type: text/plain
File size: 2252 byte(s)
rename IP address of current server to $server_ip and split it out in file

1 dpavlin 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 dpavlin 3 our $server_ip;
18     require "config.pl";
19 dpavlin 1
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 dpavlin 3 my $_ip_file;
35 dpavlin 1
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 dpavlin 3 Siaddr => $server_ip,
84 dpavlin 1 Giaddr => $dhcp->giaddr(),
85     Chaddr => $dhcp->chaddr(),
86     File => 'undionly.kpxe',
87     # DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
88     DHO_SUBNET_MASK() => '255.0.0.0',
89     );
90    
91 dpavlin 3 warn ">> $mac == $ip server $server_ip\n";
92 dpavlin 1
93     warn "## ",$packet->toString(),"\n" if $debug;
94    
95     my $reply = IO::Socket::INET->new(
96 dpavlin 3 LocalAddr => $server_ip,
97 dpavlin 1 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