Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | |
2 /* | |
3 * Copyright (c) 2011 UPB | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License version 2 as | |
7 * published by the Free Software Foundation; | |
8 * | |
9 * This program is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with this program; if not, write to the Free Software | |
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 * | |
18 * Author: Radu Lupu <rlupu@elcom.pub.ro> for previous patch of DHCP on ns-3.12 | |
19 * Ankit Deepak <adadeepak8@gmail.com> and | |
20 * Deepti Rajagopal <deeptir96@gmail.com> for DHCP patch on ns-3.24 | |
21 * | |
22 */ | |
23 | |
24 #include "ns3/assert.h" | |
25 #include "ns3/log.h" | |
26 #include "ns3/header.h" | |
27 #include "ns3/simulator.h" | |
28 #include "dhcp-header.h" | |
29 #include "ns3/address-utils.h" | |
30 | |
31 namespace ns3 { | |
32 | |
33 NS_LOG_COMPONENT_DEFINE ("DhcpHeader"); | |
34 NS_OBJECT_ENSURE_REGISTERED (DhcpHeader); | |
35 | |
36 DhcpHeader::DhcpHeader () | |
37 { | |
38 m_hType = 1; | |
39 m_hLen = 6; | |
40 m_xid = 0; | |
41 m_secs = 0; | |
42 m_hops = 0; | |
43 m_flags = 0; | |
44 Ipv4Address addr = Ipv4Address ("0.0.0.0"); | |
45 m_yiAddr = addr; | |
46 m_ciAddr = addr; | |
47 m_siAddr = addr; | |
48 m_giAddr = addr; | |
49 m_dhcps = addr; | |
50 m_len = 240; | |
51 int i; | |
52 for (i = 0; i < 202; i++) | |
53 { | |
54 m_pad[i] = 0; | |
55 } | |
56 } | |
57 | |
58 void DhcpHeader::SetType (uint8_t type) | |
59 { | |
60 m_len += 3; | |
Tommaso Pecorella
2016/02/02 23:11:40
Adding 3 blindly is not wise. check if the type ca
adadeepak8
2016/02/04 10:33:40
Done.
| |
61 m_opt[53] = true; | |
62 m_op = type; | |
63 m_bootp = (m_op == 0||m_op == 2) ? 1 : 2; | |
64 } | |
65 | |
66 void DhcpHeader::SetHWType (uint8_t htype, uint8_t hlen) | |
67 { | |
68 m_hType = htype; | |
69 m_hLen = hlen; | |
70 } | |
71 | |
72 void DhcpHeader::SetTran (uint32_t tran) | |
73 { | |
74 m_xid = tran; | |
75 } | |
76 | |
77 uint32_t DhcpHeader::GetTran (void) const | |
78 { | |
79 return m_xid; | |
80 } | |
81 | |
82 void DhcpHeader::SetTime () | |
83 { | |
84 m_secs = (uint16_t) Simulator::Now ().GetSeconds (); | |
85 } | |
86 | |
87 void DhcpHeader::SetMagicCookie () | |
88 { | |
89 m_magic_cookie[0] = 99; | |
90 m_magic_cookie[1] = 130; | |
91 m_magic_cookie[2] = 83; | |
92 m_magic_cookie[3] = 99; | |
93 } | |
94 | |
95 uint8_t DhcpHeader::GetType (void) const | |
96 { | |
97 return m_op; | |
98 } | |
99 | |
100 void DhcpHeader::SetChaddr (Mac48Address addr) | |
101 { | |
102 m_chAddr = addr; | |
103 } | |
104 Mac48Address DhcpHeader::GetChaddr (void) | |
105 { | |
106 return m_chAddr; | |
107 } | |
108 | |
109 void DhcpHeader::SetYiaddr (Ipv4Address addr) | |
110 { | |
111 m_yiAddr = addr; | |
112 } | |
113 | |
114 Ipv4Address DhcpHeader::GetYiaddr (void) const | |
115 { | |
116 return m_yiAddr; | |
117 } | |
118 | |
119 void DhcpHeader::Setdhcps (Ipv4Address addr) | |
120 { | |
121 m_len += 6; | |
122 m_opt[54] = true; | |
123 m_dhcps = addr; | |
124 } | |
125 | |
126 void DhcpHeader::Setreq (Ipv4Address addr) | |
127 { | |
128 m_len += 6; | |
129 m_opt[50] = true; | |
130 m_req = addr; | |
131 } | |
132 | |
133 void DhcpHeader::Setmask (uint32_t addr) | |
134 { | |
135 m_len += 6; | |
136 m_opt[1] = true; | |
137 m_mask = addr; | |
138 } | |
139 | |
140 Ipv4Address DhcpHeader::Getdhcps (void) const | |
141 { | |
142 return m_dhcps; | |
143 } | |
144 | |
145 Ipv4Address DhcpHeader::Getreq (void) const | |
146 { | |
147 return m_req; | |
148 } | |
149 | |
150 void DhcpHeader::resetopt () | |
151 { | |
152 m_len = 241; | |
153 int i; | |
154 for (i = 0; i < 255; i++) | |
155 { | |
156 m_opt[i] = false; | |
157 } | |
158 } | |
159 | |
160 uint32_t DhcpHeader::Getmask (void) const | |
161 { | |
162 return m_mask; | |
163 } | |
164 | |
165 uint32_t DhcpHeader::GetSerializedSize (void) const | |
166 { | |
167 return m_len; | |
168 } | |
169 | |
170 TypeId DhcpHeader::GetTypeId (void) | |
171 { | |
172 static TypeId tid = TypeId ("ns3::DhcpHeader") | |
173 .SetParent<Header> () | |
174 .SetGroupName ("Applications") | |
175 .AddConstructor<DhcpHeader> () | |
176 ; | |
177 return tid; | |
178 } | |
179 TypeId DhcpHeader::GetInstanceTypeId (void) const | |
180 { | |
181 return GetTypeId (); | |
182 } | |
183 void DhcpHeader::Print (std::ostream &os) const | |
184 { | |
185 os << "(type=" << m_op << ")"; | |
186 } | |
187 | |
188 void | |
189 DhcpHeader::Serialize (Buffer::Iterator start) const | |
190 { | |
191 Buffer::Iterator i = start; | |
192 i.WriteU8 (m_bootp); | |
193 i.WriteU8 ( m_hType); | |
194 i.WriteU8 ( m_hLen); | |
195 i.WriteU8 ( m_hops); | |
196 i.WriteU32 ( m_xid); | |
197 i.WriteHtonU16 ( m_secs); | |
198 i.WriteU16 ( m_flags); | |
199 WriteTo (i, m_ciAddr); | |
200 WriteTo (i, m_yiAddr); | |
201 WriteTo (i, m_siAddr); | |
202 WriteTo (i, m_giAddr); | |
203 WriteTo (i, m_chAddr); | |
204 i.Write (m_pad,202); | |
Tommaso Pecorella
2016/02/02 23:11:40
RFC 2131, here it should be only the sname (64) an
adadeepak8
2016/02/04 10:33:40
Done.
| |
205 i.Write (m_magic_cookie,4); | |
206 if (m_opt[53]) | |
Tommaso Pecorella
2016/02/02 23:11:40
Supporting option 51 (IP Address Lease Time) could
adadeepak8
2016/02/04 10:33:40
Done.
| |
207 { | |
208 i.WriteU8 (53); | |
209 i.WriteU8 (1); | |
210 i.WriteU8 ((m_op + 1)); | |
211 } | |
212 if (m_opt[50]) | |
213 { | |
214 i.WriteU8 (50); | |
215 i.WriteU8 (4); | |
216 WriteTo (i, m_req); | |
217 } | |
218 if (m_opt[54]) | |
219 { | |
220 i.WriteU8 (54); | |
221 i.WriteU8 (4); | |
222 WriteTo (i, m_dhcps); | |
223 } | |
224 if (m_opt[1]) | |
225 { | |
226 i.WriteU8 (1); | |
227 i.WriteU8 (4); | |
228 i.WriteHtonU32 (m_mask); | |
229 } | |
230 i.WriteU8 (255); | |
231 } | |
232 | |
233 uint32_t DhcpHeader::Deserialize (Buffer::Iterator start) | |
234 { | |
235 uint32_t len; | |
236 Buffer::Iterator i = start; | |
237 m_bootp = i.ReadU8 (); | |
238 m_hType = i.ReadU8 (); | |
239 m_hLen = i.ReadU8 (); | |
240 m_hops = i.ReadU8 (); | |
241 m_xid = i.ReadU32 (); | |
242 m_secs = i.ReadNtohU16 (); | |
243 m_flags = i.ReadU16 (); | |
244 ReadFrom (i, m_ciAddr); | |
245 ReadFrom (i,m_yiAddr); | |
246 ReadFrom (i,m_siAddr); | |
247 ReadFrom (i,m_giAddr); | |
248 ReadFrom (i, m_chAddr); | |
249 i.Read (m_pad,202); | |
250 i.Read (m_magic_cookie,4); | |
251 len = 240; | |
252 uint8_t option; | |
253 bool loop = true; | |
254 do | |
255 { | |
256 option = i.ReadU8 (); | |
257 len += 1; | |
258 switch (option) | |
259 { | |
260 case 1: | |
261 i.ReadU8 (); | |
262 m_mask = i.ReadNtohU32 (); | |
263 len += 5; | |
264 break; | |
265 case 53: | |
266 i.ReadU8 (); | |
267 m_op = (i.ReadU8 () - 1); | |
268 len += 2; | |
269 break; | |
270 case 54: | |
271 i.ReadU8 (); | |
272 ReadFrom (i, m_dhcps); | |
273 len += 5; | |
274 break; | |
275 case 50: | |
276 i.ReadU8 (); | |
277 ReadFrom (i, m_req); | |
278 len += 5; | |
279 break; | |
280 case 255: | |
281 loop = false; | |
282 break; | |
Tommaso Pecorella
2016/02/02 23:11:40
Be prepared to heavy mistakes: check if the iterat
adadeepak8
2016/02/04 10:33:40
Sir,
While writing the packet to be sent, in the
| |
283 } | |
284 } | |
285 while (loop); | |
286 | |
287 m_len = len; | |
288 return len; | |
289 } | |
290 | |
291 } // namespace ns3 | |
292 | |
OLD | NEW |