-
Notifications
You must be signed in to change notification settings - Fork 5
/
dhcp.py
58 lines (48 loc) · 1.99 KB
/
dhcp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from ryu.lib import addrconv
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ipv4
from ryu.lib.packet import udp
from ryu.lib.packet import dhcp
class Config():
controller_macAddr = '7e:49:b3:f0:f9:99' # don't modify, a dummy mac address for fill the mac enrty
dns = '8.8.8.8' # don't modify, just for the dns entry
start_ip = '192.168.1.2' # can be modified
end_ip = '192.168.1.100' # can be modified
netmask = '255.255.255.0' # can be modified
# You may use above attributes to configure your DHCP server.
# You can also add more attributes like "lease_time" to support bouns function.
class DHCPServer():
hardware_addr = Config.controller_macAddr
start_ip = Config.start_ip
end_ip = Config.end_ip
netmask = Config.netmask
dns = Config.dns
@classmethod
def assemble_ack(cls, pkt, datapath, port):
# TODO: Generate DHCP ACK packet here
return ack_pkt
@classmethod
def assemble_offer(cls, pkt, datapath):
# TODO: Generate DHCP OFFER packet here
@classmethod
def handle_dhcp(cls, datapath, port, pkt):
# TODO: Specify the type of received DHCP packet
# You may choose a valid IP from IP pool and genereate DHCP OFFER packet
# Or generate a DHCP ACK packet
# Finally send the generated packet to the host by using _send_packet method
@classmethod
def _send_packet(cls, datapath, port, pkt):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
if isinstance(pkt, str):
pkt = pkt.encode()
pkt.serialize()
data = pkt.data
actions = [parser.OFPActionOutput(port=port)]
out = parser.OFPPacketOut(datapath=datapath,
buffer_id=ofproto.OFP_NO_BUFFER,
in_port=ofproto.OFPP_CONTROLLER,
actions=actions,
data=data)
datapath.send_msg(out)