/[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

Annotation of /bin/dhcpd.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (hide annotations)
Wed Jul 29 00:46:29 2009 UTC (11 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 3994 byte(s)
really implement DHCP protocol, so this server now has correct name :-)

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 dpavlin 22 use autodie;
9    
10 dpavlin 1 use IO::Socket::INET;
11 dpavlin 17 use File::Slurp;
12 dpavlin 1 use Data::Dump qw/dump/;
13    
14 dpavlin 27 use lib 'lib';
15     use Net::DHCP::Packet;
16     use Net::DHCP::Constants 0.67;
17 dpavlin 1 die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;
18    
19     my $debug = shift @ARGV;
20    
21 dpavlin 22 our ( $file, $gpxe_file );
22 dpavlin 17 our ( $ip_from, $ip_to ) = ( 10, 100 );
23 dpavlin 1
24 dpavlin 22 our $server_ip = readlink 'conf/server.ip' if -l 'conf/server.ip';
25 dpavlin 17
26 dpavlin 22 if ( ! $server_ip ) {
27     $server_ip = `/sbin/ifconfig`;
28     $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
29     warn "auto-configure server ip to $server_ip\n";
30     } else {
31     warn "server ip $server_ip\n";
32     }
33    
34 dpavlin 1 my $sock = IO::Socket::INET->new(
35     LocalPort => 67,
36     # LocalAddr => 'localhost',
37     # LocalAddr => '10.0.0.100',
38     LocalAddr => '0.0.0.0',
39     Proto => 'udp',
40     ReuseAddr => 1,
41     # PeerPort => getservbyname('bootpc', 'udp'),
42     Broadcast => 1,
43     Type => SOCK_DGRAM,
44     ) or die "Failed to bind to socket: $@";
45    
46    
47 dpavlin 17 my $addr = $ip_from;
48    
49 dpavlin 1 sub client_ip {
50     my ( $mac ) = @_;
51    
52 dpavlin 17 my $conf = "conf/$server_ip";
53     mkdir $conf unless -e $conf;
54 dpavlin 1
55 dpavlin 22 if ( -e "$conf/mac/$mac" ) {
56     my $ip = read_file "$conf/mac/$mac";
57 dpavlin 17 print "$mac old $ip\n";
58     return $ip;
59     }
60 dpavlin 1
61 dpavlin 17 mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );
62    
63     my $prefix = $server_ip;
64     $prefix =~ s{\.\d+$}{.};
65     my $ip = $prefix . $addr;
66     while ( -e "conf/ip/$ip" ) {
67     $ip = $prefix . $addr++;
68     die "all addresses allocated!" if $addr == $ip_to;
69 dpavlin 1 }
70    
71 dpavlin 17 write_file "$conf/mac/$mac", $ip;
72 dpavlin 22 unlink "$conf/ip/$ip" if -e "$conf/ip/$ip";
73     symlink "$conf/mac/$mac", "$conf/ip/$ip";
74 dpavlin 17
75     print "$mac NEW $ip\n";
76    
77 dpavlin 1 return $ip;
78     }
79    
80 dpavlin 27 my $transaction = 0; # FIXME predictible transaction numbers
81    
82 dpavlin 1 while (1) {
83    
84 dpavlin 12 require "config.pl"; # refresh config
85    
86 dpavlin 1 print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";
87    
88     my $buf;
89     $sock->recv($buf, 1024);
90 dpavlin 17 print "<< ",$sock->peerhost,":",$sock->peerport,"\n";
91 dpavlin 1
92     if (defined $buf) {
93    
94 dpavlin 27 my $dhcp = Net::DHCP::Packet->new($buf);
95     $dhcp->comment( $transaction++ );
96 dpavlin 1
97 dpavlin 27 warn "recv: ", $dhcp->toString, "\n\n";
98 dpavlin 1
99     my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
100     my $ip = client_ip($mac);
101 dpavlin 22 my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
102 dpavlin 1
103 dpavlin 22 if ( $user_class eq 'gPXE' ) {
104     $file = $gpxe_file;
105     } elsif ( ! $file ) {
106     $file = 'undionly.kpxe';
107 dpavlin 12 }
108 dpavlin 6
109 dpavlin 27 my $packet = {
110 dpavlin 1 Op => BOOTREPLY(),
111     Hops => $dhcp->hops(),
112     Xid => $dhcp->xid(),
113     Flags => $dhcp->flags(),
114     Ciaddr => $dhcp->ciaddr(),
115     Yiaddr => $ip,
116 dpavlin 3 Siaddr => $server_ip,
117 dpavlin 1 Giaddr => $dhcp->giaddr(),
118     Chaddr => $dhcp->chaddr(),
119 dpavlin 6 File => $file,
120 dpavlin 1 # DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
121 dpavlin 27 # DHO_SUBNET_MASK() => '255.255.255.0',
122     };
123 dpavlin 1
124 dpavlin 27 my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
125 dpavlin 22
126 dpavlin 27 if ($messagetype eq DHCPDISCOVER()) {
127     warn "DHCP DISCOVER";
128     $packet->{Comment} = $dhcp->comment();
129     $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
130     } elsif ($messagetype eq DHCPREQUEST()) {
131     my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
132     warn "DHCP REQUEST $requested_ip";
133     if ( $ip eq $requested_ip ) {
134     $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK();
135     } else {
136     $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
137     $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
138     }
139     } elsif ($messagetype eq DHCPINFORM()) {
140     warn "DHCP INFORM ignored";
141     } else {
142     warn "$messagetype igored (bootp?)";
143     }
144    
145    
146     warn ">> $mac == $ip server: $server_ip", $file ? " file: $file\n" : "\n";
147    
148     $packet = new Net::DHCP::Packet( %$packet );
149 dpavlin 1 warn "## ",$packet->toString(),"\n" if $debug;
150    
151     my $reply = IO::Socket::INET->new(
152 dpavlin 3 LocalAddr => $server_ip,
153 dpavlin 1 LocalPort => 67,
154     Proto => "udp",
155     Broadcast => 1,
156     PeerAddr => '255.255.255.255',
157     PeerPort => 68,
158     Reuse => 1,
159     ) or die "socket: $@";
160    
161     my $buff = $packet->serialize();
162     $reply->send( $buff, 0 ) or die "Error sending: $!\n";
163    
164     # system("arp -s $ip $mac"),
165    
166     } else {
167     print "No bootp request.\n";
168     }
169    
170     }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26