source: trunk/packages/python-pydhcplib/pydhcplib/dhcp_network.py @ 1704

Last change on this file since 1704 was 1191, checked in by broder, 16 years ago

Strip the sipb-xen prefix from packages we didn't write

File size: 4.9 KB
RevLine 
[361]1# pydhcplib
2# Copyright (C) 2005,2006 Mathieu Ignacio -- mignacio@april.org
3#
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.
9#
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.
14#
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
18
19import sys
20import socket
21import dhcp_packet
22
23
24class DhcpNetwork:
25    def __init__(self, listen_address="0.0.0.0", listen_port=67, emit_port=68):
26
27        self.listen_port = int(listen_port)
28        self.emit_port = int(emit_port)
29        self.listen_address = listen_address
30       
31    def GetNextDhcpPacket(self):
32        data =""
33
34        while data == "" :
35            data = self.dhcp_socket.recv(1024)
36            if data != "" :
37                packet = dhcp_packet.DhcpPacket()
38                packet.DecodePacket(data)
39
40                self.HandleDhcpAll(packet)
41               
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)
59
60                return packet
61
62
63    def SendDhcpPacketTo(self, To, packet):
64        return self.dhcp_socket.sendto(packet.EncodePacket(),(To,self.emit_port))
65
66
67    # Server side Handle methods
68    def HandleDhcpDiscover(self, packet):
69        print "HandleDhcpRequest : method not implemented"
70
71    def HandleDhcpRequest(self, packet):
72        print "HandleDhcpRequest : method not implemented"
73
74    def HandleDhcpDecline(self, packet):
75        print "HandleDhcpDecline : method not implemented"
76
77    def HandleDhcpRelease(self, packet):
78        print "HandleDhcpRelease : method not implemented"
79
80    def HandleDhcpInform(self, packet):
81        print "HandleDhcpInform : method not implemented"
82
83
84    # client-side Handle methods
85    def HandleDhcpOffer(self, packet):
86        print "HandleDhcpOffer : method not implemented"
87       
88    def HandleDhcpAck(self, packet):
89        print "HandleDhcpAckhandling : method not implemented"
90
91    def HandleDhcpNack(self, packet):
92        print "HandleDhcpNack : method not implemented"
93
94
95    # Handle unknown options or all options
96    def HandleDhcpUnknown(self, packet):
97        print "HandleDhcpUnknown : method not implemented"
98
99    def HandleDhcpAll(self, packet):
100        pass
101
102
103class DhcpServer(DhcpNetwork) :
104    def __init__(self, listen_address="0.0.0.0", client_listen_port=67,server_listen_port=68) :
105       
106        DhcpNetwork.__init__(self,listen_address,server_listen_port,client_listen_port)
107       
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))
111
112
113class DhcpClient(DhcpNetwork) :
114    def __init__(self, listen_address="0.0.0.0",client_listen_port=67,server_listen_port=68) :
115
116        DhcpNetwork.__init__(self,listen_address,client_listen_port,server_listen_port)
117
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))
122
123# Raw client permit to listen on network even if there is
124# no interface set. Probably useful... :-)
125class DhcpRawClient(DhcpNetwork) :
126    def __init__(self, interface="eth0", client_listen_port=67,server_listen_port=68) :
127
128        DhcpNetwork.__init__(self,interface,client_listen_port,server_listen_port)
129        print interface
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))
133
134           
Note: See TracBrowser for help on using the repository browser.