1
2
3 """Dynamic Host Configuration Protocol."""
4
5 import arp, dpkt
6
7 DHCP_OP_REQUEST = 1
8 DHCP_OP_REPLY = 2
9
10 DHCP_MAGIC = 0x63825363
11
12
13 DHCP_OPT_NETMASK = 1
14 DHCP_OPT_TIMEOFFSET = 2
15 DHCP_OPT_ROUTER = 3
16 DHCP_OPT_TIMESERVER = 4
17 DHCP_OPT_NAMESERVER = 5
18 DHCP_OPT_DNS_SVRS = 6
19 DHCP_OPT_LOGSERV = 7
20 DHCP_OPT_COOKIESERV = 8
21 DHCP_OPT_LPRSERV = 9
22 DHCP_OPT_IMPSERV = 10
23 DHCP_OPT_RESSERV = 11
24 DHCP_OPT_HOSTNAME = 12
25 DHCP_OPT_BOOTFILESIZE = 13
26 DHCP_OPT_DUMPFILE = 14
27 DHCP_OPT_DOMAIN = 15
28 DHCP_OPT_SWAPSERV = 16
29 DHCP_OPT_ROOTPATH = 17
30 DHCP_OPT_EXTENPATH = 18
31 DHCP_OPT_IPFORWARD = 19
32 DHCP_OPT_SRCROUTE = 20
33 DHCP_OPT_POLICYFILTER = 21
34 DHCP_OPT_MAXASMSIZE = 22
35 DHCP_OPT_IPTTL = 23
36 DHCP_OPT_MTUTIMEOUT = 24
37 DHCP_OPT_MTUTABLE = 25
38 DHCP_OPT_MTUSIZE = 26
39 DHCP_OPT_LOCALSUBNETS = 27
40 DHCP_OPT_BROADCASTADDR = 28
41 DHCP_OPT_DOMASKDISCOV = 29
42 DHCP_OPT_MASKSUPPLY = 30
43 DHCP_OPT_DOROUTEDISC = 31
44 DHCP_OPT_ROUTERSOLICIT = 32
45 DHCP_OPT_STATICROUTE = 33
46 DHCP_OPT_TRAILERENCAP = 34
47 DHCP_OPT_ARPTIMEOUT = 35
48 DHCP_OPT_ETHERENCAP = 36
49 DHCP_OPT_TCPTTL = 37
50 DHCP_OPT_TCPKEEPALIVE = 38
51 DHCP_OPT_TCPALIVEGARBAGE = 39
52 DHCP_OPT_NISDOMAIN = 40
53 DHCP_OPT_NISSERVERS = 41
54 DHCP_OPT_NISTIMESERV = 42
55 DHCP_OPT_VENDSPECIFIC = 43
56 DHCP_OPT_NBNS = 44
57 DHCP_OPT_NBDD = 45
58 DHCP_OPT_NBTCPIP = 46
59 DHCP_OPT_NBTCPSCOPE = 47
60 DHCP_OPT_XFONT = 48
61 DHCP_OPT_XDISPLAYMGR = 49
62 DHCP_OPT_REQ_IP = 50
63 DHCP_OPT_LEASE_SEC = 51
64 DHCP_OPT_OPTIONOVERLOAD = 52
65 DHCP_OPT_MSGTYPE = 53
66 DHCP_OPT_SERVER_ID = 54
67 DHCP_OPT_PARAM_REQ = 55
68 DHCP_OPT_MESSAGE = 56
69 DHCP_OPT_MAXMSGSIZE = 57
70 DHCP_OPT_RENEWTIME = 58
71 DHCP_OPT_REBINDTIME = 59
72 DHCP_OPT_VENDOR_ID = 60
73 DHCP_OPT_CLIENT_ID = 61
74 DHCP_OPT_NISPLUSDOMAIN = 64
75 DHCP_OPT_NISPLUSSERVERS = 65
76 DHCP_OPT_MOBILEIPAGENT = 68
77 DHCP_OPT_SMTPSERVER = 69
78 DHCP_OPT_POP3SERVER = 70
79 DHCP_OPT_NNTPSERVER = 71
80 DHCP_OPT_WWWSERVER = 72
81 DHCP_OPT_FINGERSERVER = 73
82 DHCP_OPT_IRCSERVER = 74
83 DHCP_OPT_STSERVER = 75
84 DHCP_OPT_STDASERVER = 76
85
86
87 DHCPDISCOVER = 1
88 DHCPOFFER = 2
89 DHCPREQUEST = 3
90 DHCPDECLINE = 4
91 DHCPACK = 5
92 DHCPNAK = 6
93 DHCPRELEASE = 7
94 DHCPINFORM = 8
95
96 -class DHCP(dpkt.Packet):
97 __hdr__ = (
98 ('op', 'B', DHCP_OP_REQUEST),
99 ('hrd', 'B', arp.ARP_HRD_ETH),
100 ('hln', 'B', 6),
101 ('hops', 'B', 0),
102 ('xid', 'I', 0xdeadbeefL),
103 ('secs', 'H', 0),
104 ('flags', 'H', 0),
105 ('ciaddr', 'I', 0),
106 ('yiaddr', 'I', 0),
107 ('siaddr', 'I', 0),
108 ('giaddr', 'I', 0),
109 ('chaddr', '16s', 16 * '\x00'),
110 ('sname', '64s', 64 * '\x00'),
111 ('file', '128s', 128 * '\x00'),
112 ('magic', 'I', DHCP_MAGIC),
113 )
114 opts = (
115 (DHCP_OPT_MSGTYPE, chr(DHCPDISCOVER)),
116 (DHCP_OPT_PARAM_REQ, ''.join(map(chr, (DHCP_OPT_REQ_IP,
117 DHCP_OPT_ROUTER,
118 DHCP_OPT_NETMASK,
119 DHCP_OPT_DNS_SVRS))))
120 )
121
125
128
130 """Return packed options string."""
131 if not self.opts:
132 return ''
133 l = []
134 for t, data in self.opts:
135 l.append('%s%s%s' % (chr(t), chr(len(data)), data))
136 l.append('\xff')
137 return ''.join(l)
138
140 dpkt.Packet.unpack(self, buf)
141 self.chaddr = self.chaddr[:self.hln]
142 buf = self.data
143 l = []
144 while buf:
145 t = ord(buf[0])
146 if t == 0xff:
147 buf = buf[1:]
148 break
149 elif t == 0:
150 buf = buf[1:]
151 else:
152 n = ord(buf[1])
153 l.append((t, buf[2:2+n]))
154 buf = buf[2+n:]
155 self.opts = l
156 self.data = buf
157
158 if __name__ == '__main__':
159 import unittest
160
163 s = '\x01\x01\x06\x00\xadS\xc8c\xb8\x87\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02U\x82\xf3\xa6\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00c\x82Sc5\x01\x01\xfb\x01\x01=\x07\x01\x00\x02U\x82\xf3\xa62\x04\n\x00\x01e\x0c\tGuinevere<\x08MSFT 5.07\n\x01\x0f\x03\x06,./\x1f!+\xff\x00\x00\x00\x00\x00'
164 dhcp = DHCP(s)
165 self.failUnless(s == str(dhcp))
166
167 unittest.main()
168