2 # Copyright (C) 2005,2006 Mathieu Ignacio -- mignacio@april.org
4 # This file is part of pydhcplib.
5 # Pydhcplib is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 def __init__(self, listen_address="0.0.0.0", listen_port=67, emit_port=68):
27 self.listen_port = int(listen_port)
28 self.emit_port = int(emit_port)
29 self.listen_address = listen_address
31 def GetNextDhcpPacket(self):
35 data = self.dhcp_socket.recv(1024)
37 packet = dhcp_packet.DhcpPacket()
38 packet.DecodePacket(data)
40 self.HandleDhcpAll(packet)
42 if packet.IsDhcpDiscoverPacket():
43 self.HandleDhcpDiscover(packet)
44 elif packet.IsDhcpRequestPacket():
45 self.HandleDhcpRequest(packet)
46 elif packet.IsDhcpDeclinePacket():
47 self.HandleDhcpDecline(packet)
48 elif packet.IsDhcpReleasePacket():
49 self.HandleDhcpRelease(packet)
50 elif packet.IsDhcpInformPacket():
51 self.HandleDhcpInform(packet)
52 elif packet.IsDhcpOfferPacket():
53 self.HandleDhcpOffer(packet)
54 elif packet.IsDhcpAckPacket():
55 self.HandleDhcpAck(packet)
56 elif packet.IsDhcpNackPacket():
57 self.HandleDhcpNack(packet)
58 else: self.HandleDhcpUnknown(packet)
63 def SendDhcpPacketTo(self, To, packet):
64 return self.dhcp_socket.sendto(packet.EncodePacket(),(To,self.emit_port))
67 # Server side Handle methods
68 def HandleDhcpDiscover(self, packet):
69 print "HandleDhcpRequest : method not implemented"
71 def HandleDhcpRequest(self, packet):
72 print "HandleDhcpRequest : method not implemented"
74 def HandleDhcpDecline(self, packet):
75 print "HandleDhcpDecline : method not implemented"
77 def HandleDhcpRelease(self, packet):
78 print "HandleDhcpRelease : method not implemented"
80 def HandleDhcpInform(self, packet):
81 print "HandleDhcpInform : method not implemented"
84 # client-side Handle methods
85 def HandleDhcpOffer(self, packet):
86 print "HandleDhcpOffer : method not implemented"
88 def HandleDhcpAck(self, packet):
89 print "HandleDhcpAckhandling : method not implemented"
91 def HandleDhcpNack(self, packet):
92 print "HandleDhcpNack : method not implemented"
95 # Handle unknown options or all options
96 def HandleDhcpUnknown(self, packet):
97 print "HandleDhcpUnknown : method not implemented"
99 def HandleDhcpAll(self, packet):
103 class DhcpServer(DhcpNetwork) :
104 def __init__(self, listen_address="0.0.0.0", client_listen_port=67,server_listen_port=68) :
106 DhcpNetwork.__init__(self,listen_address,server_listen_port,client_listen_port)
108 self.dhcp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
109 self.dhcp_socket.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
110 self.dhcp_socket.bind((self.listen_address, self.listen_port))
113 class DhcpClient(DhcpNetwork) :
114 def __init__(self, listen_address="0.0.0.0",client_listen_port=67,server_listen_port=68) :
116 DhcpNetwork.__init__(self,listen_address,client_listen_port,server_listen_port)
118 self.dhcp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
119 self.dhcp_socket.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
120 self.dhcp_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
121 self.dhcp_socket.bind((self.listen_address, self.listen_port))
123 # Raw client permit to listen on network even if there is
124 # no interface set. Probably useful... :-)
125 class DhcpRawClient(DhcpNetwork) :
126 def __init__(self, interface="eth0", client_listen_port=67,server_listen_port=68) :
128 DhcpNetwork.__init__(self,interface,client_listen_port,server_listen_port)
130 # 0x800 : ETH_P_IP, 0x003 : ETH_P_ALL
131 # See Linux/if_ether.h
132 self.dhcp_socket = socket.socket(socket.AF_PACKET, socket.SOCK_DGRAM,socket.ntohs(0x0800))