1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 UPB
4  * Copyright (c) 2017 NITK Surathkal
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * Author: Radu Lupu <rlupu@elcom.pub.ro>
20  *         Ankit Deepak <adadeepak8@gmail.com>
21  *         Deepti Rajagopal <deeptir96@gmail.com>
22  *
23  */
24 
25 #ifndef DHCP_HEADER_H
26 #define DHCP_HEADER_H
27 
28 #include "ns3/header.h"
29 
30 namespace ns3 {
31 
32 /**
33  * \ingroup internet-apps
34  * \defgroup dhcp DHCPv4 Client and Server
35  */
36 
37 /**
38  * \ingroup dhcp
39  *
40  * \class DhcpHeader
41  * \brief BOOTP header with DHCP messages supports the following options:
42  *        Subnet Mask (1), Address Request (50), Refresh Lease Time (51),
43  *        DHCP Message Type (53), DHCP Server ID (54), Renew Time (58),
44  *        Rebind Time (59) and End (255) of BOOTP
45 
46   \verbatim
47     0                   1                   2                   3
48    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
49    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50    |     op (1)    |   htype (1)   |   hlen (1)    |   hops (1)    |
51    +---------------+---------------+---------------+---------------+
52    |                            xid (4)                            |
53    +-------------------------------+-------------------------------+
54    |           secs (2)            |           flags (2)           |
55    +-------------------------------+-------------------------------+
56    |                          ciaddr  (4)                          |
57    +---------------------------------------------------------------+
58    |                          yiaddr  (4)                          |
59    +---------------------------------------------------------------+
60    |                          siaddr  (4)                          |
61    +---------------------------------------------------------------+
62    |                          giaddr  (4)                          |
63    +---------------------------------------------------------------+
64    |                                                               |
65    |                          chaddr  (16)                         |
66    |                                                               |
67    |                                                               |
68    +---------------------------------------------------------------+
69    |                                                               |
70    |                          sname   (64)                         |
71    +---------------------------------------------------------------+
72    |                                                               |
73    |                          file    (128)                        |
74    +---------------------------------------------------------------+
75    |                                                               |
76    |                          options (variable)                   |
77    +---------------------------------------------------------------+
78   \endverbatim
79 
80  */
81 class DhcpHeader : public Header
82 {
83 public:
84   /**
85    * \brief Get the type ID.
86    * \return the object TypeId
87    */
88   static TypeId GetTypeId (void);
89 
90   /**
91    * \brief Constructor
92    */
93   DhcpHeader ();
94 
95   /**
96    * \brief Destructor
97    */
98   ~DhcpHeader ();
99 
100   /// BOOTP options
101   enum Options
102   {
103     OP_MASK = 1,        //!< BOOTP Option 1: Address Mask
104     OP_ROUTE = 3,       //!< BOOTP Option 3: Router Option
105     OP_ADDREQ = 50,     //!< BOOTP Option 50: Requested Address
106     OP_LEASE = 51,      //!< BOOTP Option 51: Address Lease Time
107     OP_MSGTYPE = 53,    //!< BOOTP Option 53: DHCP Message Type
108     OP_SERVID = 54,     //!< BOOTP Option 54: Server Identifier
109     OP_RENEW = 58,      //!< BOOTP Option 58: Address Renewal Time
110     OP_REBIND = 59,     //!< BOOTP Option 59: Address Rebind Time
111     OP_END = 255        //!< BOOTP Option 255: END
112   };
113 
114   /// DHCP messages
115   enum Messages
116   {
117     DHCPDISCOVER = 0,     //!< Code for DHCP Discover
118     DHCPOFFER = 1,        //!< Code for DHCP Offer
119     DHCPREQ = 2,          //!< Code for DHCP Request
120     DHCPACK = 4,          //!< Code for DHCP ACK
121     DHCPNACK = 5          //!< Code for DHCP NACK
122   };
123 
124   /**
125    * \brief Set the type of BOOTP and DHCP messages
126    * \param type The type of message
127    */
128   void SetType (uint8_t type);
129 
130   /**
131    * \brief Return the type of DHCP message
132    * \return The type of message
133    */
134   uint8_t GetType (void) const;
135 
136   /**
137    * \brief Set the hardware information
138    * \param htype Hardware type
139    * \param hlen Hardware length
140    */
141   void SetHWType (uint8_t htype, uint8_t hlen);
142 
143   /**
144    * \brief Set the transaction ID
145    * \param tran The transaction number
146    */
147   void SetTran (uint32_t tran);
148 
149   /**
150    * \brief Get the transaction id
151    * \return The transaction id
152    */
153   uint32_t GetTran (void) const;
154 
155   /**
156    * \brief Set the time when message is sent
157    */
158   void SetTime ();
159 
160   /**
161    * \brief Set the Address of the device.
162    *
163    * Only the relevant bits are considered (i.e., not the type and length)
164    *
165    * \param addr Address of the device
166    */
167   void SetChaddr (Address addr);
168 
169   /**
170    * \brief Set the Address of the device
171    * \param addr Address of the device
172    * \param len Address length
173    */
174   void SetChaddr (uint8_t* addr, uint8_t len);
175 
176   /**
177    * \brief Get the Address of the client.
178    *
179    * Note: the address is always 16-bytes long.
180    *
181    * \return Address of the client
182    */
183   Address GetChaddr (void);
184 
185   /**
186    * \brief Set the IPv4Address of the client
187    * \param addr The client Ipv4Address
188    */
189   void SetYiaddr (Ipv4Address addr);
190 
191   /**
192    * \brief Get the IPv4Address of the client
193    * \return IPv4Address of the client
194    */
195   Ipv4Address GetYiaddr (void) const;
196 
197   /**
198    * \brief Set the DHCP server information
199    * \param addr IPv4Address of the server
200    */
201   void SetDhcps (Ipv4Address addr);
202 
203   /**
204    * \brief Get the information about the DHCP server
205    * \return IPv4Address of DHCP server
206    */
207   Ipv4Address GetDhcps (void) const;
208 
209   /**
210    * \brief Set the Ipv4Address requested by the client
211    * \param addr Ipv4Address requested by the client
212    */
213   void SetReq (Ipv4Address addr);
214 
215   /**
216    * \brief Get the IPv4Address requested by the client
217    * \return IPv4Address requested by the client
218    */
219   Ipv4Address GetReq (void) const;
220 
221   /**
222    * \brief Set the mask of the IPv4Address
223    * \param addr 32 bit mask
224    */
225   void SetMask (uint32_t addr);
226 
227   /**
228    * \brief Return the mask of the network
229    * \return 32 bit mask
230    */
231   uint32_t GetMask (void) const;
232 
233   /**
234    * \brief Set the Ipv4Address of gateway to be used
235    * \param addr The Ipv4Address of the gateway
236    */
237   void SetRouter (Ipv4Address addr);
238 
239   /**
240    * \brief Return the Ipv4Address of gateway to be used
241    * \return The Ipv4Address of the gateway
242    */
243   Ipv4Address GetRouter (void) const;
244 
245   /**
246    * \brief Set the lease time of the IPv4Address
247    * \param time 32 bit time
248    */
249   void SetLease (uint32_t time);
250 
251   /**
252    * \brief Return the lease time of the IPv4Address
253    * \return 32 bit time
254    */
255   uint32_t GetLease (void) const;
256 
257   /**
258    * \brief Set the Renewal time of the IPv4Address
259    * \param time 32 bit time
260    */
261   void SetRenew (uint32_t time);
262 
263   /**
264    * \brief Return the Renewal time of the address
265    * \return 32 bit time
266    */
267   uint32_t GetRenew (void) const;
268 
269   /**
270    * \brief Set the Rebind time of the IPv4Address
271    * \param time 32 bit time
272    */
273   void SetRebind (uint32_t time);
274 
275   /**
276    * \brief Return the Rebind time of the address
277    * \return 32 bit time
278    */
279   uint32_t GetRebind (void) const;
280 
281   /**
282    * \brief Reset the BOOTP options
283    */
284   void ResetOpt ();
285 
286 private:
287   virtual TypeId GetInstanceTypeId (void) const;
288   virtual void Print (std::ostream &os) const;
289   virtual uint32_t GetSerializedSize (void) const;
290   virtual void Serialize (Buffer::Iterator start) const;
291   virtual uint32_t Deserialize (Buffer::Iterator start);
292 
293   uint8_t m_op;                          //!< The DHCP Message type
294   uint8_t m_bootp;                       //!< The BOOTP Message type
295   uint8_t m_hType;                       //!< The hardware type
296   uint8_t m_hLen;                        //!< The hardware length
297   uint8_t m_hops;                        //!< The number of hops covered by the message
298   uint32_t m_xid;                        //!< The transaction number
299   uint32_t m_mask;                       //!< The mask of the network
300   uint32_t m_len;                        //!< The length of the header
301   uint16_t m_secs;                       //!< Seconds elapsed
302   uint16_t m_flags;                      //!< BOOTP flags
303   uint8_t m_chaddr[16];                  //!< The address identifier
304   Ipv4Address m_yiAddr;                  //!< Your (client) IP address
305   Ipv4Address m_ciAddr;                  //!< The IP address of the client
306   Ipv4Address m_siAddr;                  //!< Next Server IP address
307   Ipv4Address m_giAddr;                  //!< Relay Agent IP address
308   Ipv4Address m_dhcps;                   //!< DHCP server IP address
309   Ipv4Address m_req;                     //!< Requested Address
310   Ipv4Address m_route;                   //!< Router Option Address
311   uint8_t m_sname[64];                   //!< Server name (Padded for now)
312   uint8_t m_file[128];                   //!< File name (Padded for now)
313   uint8_t m_magic_cookie[4];             //!< DHCP Magic Cookie
314   uint32_t m_lease;                      //!< The lease time of the address
315   uint32_t m_renew;                      //!< The renewal time for the client
316   uint32_t m_rebind;                     //!< The rebinding time for the client
317   bool m_opt[255];                       //!< BOOTP option list
318 };
319 
320 } // namespace ns3
321 
322 #endif /* DHCP_HEADER_H */
323