pkt6.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // Copyright (C) 2011-2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef PKT6_H
  15. #define PKT6_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/option.h>
  18. #include <boost/date_time/posix_time/posix_time.hpp>
  19. #include <boost/shared_array.hpp>
  20. #include <boost/shared_ptr.hpp>
  21. #include <iostream>
  22. #include <time.h>
  23. namespace isc {
  24. namespace dhcp {
  25. class Pkt6 {
  26. public:
  27. /// specifes DHCPv6 packet header length
  28. const static size_t DHCPV6_PKT_HDR_LEN = 4;
  29. /// DHCPv6 transport protocol
  30. enum DHCPv6Proto {
  31. UDP = 0, // most packets are UDP
  32. TCP = 1 // there are TCP DHCPv6 packets (bulk leasequery, failover)
  33. };
  34. /// Constructor, used in replying to a message
  35. ///
  36. /// @param msg_type type of message (SOLICIT=1, ADVERTISE=2, ...)
  37. /// @param transid transaction-id
  38. /// @param proto protocol (TCP or UDP)
  39. Pkt6(uint8_t msg_type,
  40. uint32_t transid,
  41. DHCPv6Proto proto = UDP);
  42. /// Constructor, used in message transmission
  43. ///
  44. /// Creates new message. Transaction-id will randomized.
  45. ///
  46. /// @param buf pointer to a buffer of received packet content
  47. /// @param len size of buffer of received packet content
  48. /// @param proto protocol (usually UDP, but TCP will be supported eventually)
  49. Pkt6(const uint8_t* buf, uint32_t len, DHCPv6Proto proto = UDP);
  50. /// @brief Prepares on-wire format.
  51. ///
  52. /// Prepares on-wire format of message and all its options.
  53. /// Options must be stored in options_ field.
  54. /// Output buffer will be stored in data_. Length
  55. /// will be set in data_len_.
  56. ///
  57. /// @return true if packing procedure was successful
  58. bool pack();
  59. /// @brief Dispatch method that handles binary packet parsing.
  60. ///
  61. /// This method calls appropriate dispatch function (unpackUDP or
  62. /// unpackTCP).
  63. ///
  64. /// @return true if parsing was successful
  65. bool unpack();
  66. /// @brief Returns reference to output buffer.
  67. ///
  68. /// Returned buffer will contain reasonable data only for
  69. /// output (TX) packet and after pack() was called. This buffer
  70. /// is only valid till Pkt6 object is valid.
  71. ///
  72. /// RX packet or TX packet before pack() will return buffer with
  73. /// zero length
  74. ///
  75. /// @return reference to output buffer
  76. const isc::util::OutputBuffer& getBuffer() const { return (bufferOut_); };
  77. /// @brief Returns reference to input buffer.
  78. ///
  79. /// @return reference to input buffer
  80. const OptionBuffer& getData() const { return(data_); }
  81. /// @brief Returns protocol of this packet (UDP or TCP).
  82. ///
  83. /// @return protocol type
  84. DHCPv6Proto getProto();
  85. /// Sets protocol of this packet.
  86. ///
  87. /// @param proto protocol (UDP or TCP)
  88. void setProto(DHCPv6Proto proto = UDP) { proto_ = proto; }
  89. /// @brief Returns text representation of the packet.
  90. ///
  91. /// This function is useful mainly for debugging.
  92. ///
  93. /// @return string with text representation
  94. std::string toText();
  95. /// @brief Returns length of the packet.
  96. ///
  97. /// This function returns size required to hold this packet.
  98. /// It includes DHCPv6 header and all options stored in
  99. /// options_ field.
  100. ///
  101. /// Note: It does not return proper length of incoming packets
  102. /// before they are unpacked.
  103. ///
  104. /// @return number of bytes required to assemble this packet
  105. uint16_t len();
  106. /// Returns message type (e.g. 1 = SOLICIT)
  107. ///
  108. /// @return message type
  109. uint8_t getType() const { return (msg_type_); }
  110. /// Sets message type (e.g. 1 = SOLICIT)
  111. ///
  112. /// @param type message type to be set
  113. void setType(uint8_t type) { msg_type_=type; };
  114. /// @brief Sets transaction-id value
  115. ///
  116. /// @param transid transaction-id to be set.
  117. void setTransid(uint32_t transid) { transid_ = transid; }
  118. /// Returns value of transaction-id field
  119. ///
  120. /// @return transaction-id
  121. uint32_t getTransid() const { return (transid_); };
  122. /// Adds an option to this packet.
  123. ///
  124. /// @param opt option to be added.
  125. void addOption(OptionPtr opt);
  126. /// @brief Returns the first option of specified type.
  127. ///
  128. /// Returns the first option of specified type. Note that in DHCPv6 several
  129. /// instances of the same option are allowed (and frequently used).
  130. /// See getOptions().
  131. ///
  132. /// @param type option type we are looking for
  133. ///
  134. /// @return pointer to found option (or NULL)
  135. OptionPtr getOption(uint16_t type);
  136. /// Attempts to delete first suboption of requested type
  137. ///
  138. /// @param type Type of option to be deleted.
  139. ///
  140. /// @return true if option was deleted, false if no such option existed
  141. bool delOption(uint16_t type);
  142. /// @brief This method copies data from output buffer to input buffer
  143. ///
  144. /// This is useful only in testing
  145. void repack();
  146. /// @brief Sets remote address.
  147. ///
  148. /// @param remote specifies remote address
  149. void setRemoteAddr(const isc::asiolink::IOAddress& remote) { remote_addr_ = remote; }
  150. /// @brief Returns remote address
  151. ///
  152. /// @return remote address
  153. const isc::asiolink::IOAddress& getRemoteAddr() { return (remote_addr_); }
  154. /// @brief Sets local address.
  155. ///
  156. /// @param local specifies local address
  157. void setLocalAddr(const isc::asiolink::IOAddress& local) { local_addr_ = local; }
  158. /// @brief Returns local address.
  159. ///
  160. /// @return local address
  161. const isc::asiolink::IOAddress& getLocalAddr() { return (local_addr_); }
  162. /// @brief Sets local port.
  163. ///
  164. /// @param local specifies local port
  165. void setLocalPort(uint16_t local) { local_port_ = local; }
  166. /// @brief Returns local port.
  167. ///
  168. /// @return local port
  169. uint16_t getLocalPort() { return (local_port_); }
  170. /// @brief Sets remote port.
  171. ///
  172. /// @param remote specifies remote port
  173. void setRemotePort(uint16_t remote) { remote_port_ = remote; }
  174. /// @brief Returns remote port.
  175. ///
  176. /// @return remote port
  177. uint16_t getRemotePort() { return (remote_port_); }
  178. /// @brief Sets interface index.
  179. ///
  180. /// @param ifindex specifies interface index.
  181. void setIndex(uint32_t ifindex) { ifindex_ = ifindex; };
  182. /// @brief Returns interface index.
  183. ///
  184. /// @return interface index
  185. uint32_t getIndex() const { return (ifindex_); };
  186. /// @brief Returns interface name.
  187. ///
  188. /// Returns interface name over which packet was received or is
  189. /// going to be transmitted.
  190. ///
  191. /// @return interface name
  192. std::string getIface() const { return iface_; };
  193. /// @brief Returns packet timestamp.
  194. ///
  195. /// Returns packet timestamp value updated when
  196. /// packet is received or send.
  197. ///
  198. /// @return packet timestamp.
  199. const boost::posix_time::ptime& getTimestamp() const { return timestamp_; }
  200. /// @brief Sets interface name.
  201. ///
  202. /// Sets interface name over which packet was received or is
  203. /// going to be transmitted.
  204. ///
  205. /// @return interface name
  206. void setIface(const std::string& iface ) { iface_ = iface; };
  207. /// TODO Need to implement getOptions() as well
  208. /// collection of options present in this message
  209. ///
  210. /// @warning This protected member is accessed by derived
  211. /// classes directly. One of such derived classes is
  212. /// @ref perfdhcp::PerfPkt6. The impact on derived clasess'
  213. /// behavior must be taken into consideration before making
  214. /// changes to this member such as access scope restriction or
  215. /// data format change etc.
  216. isc::dhcp::Option::OptionCollection options_;
  217. /// @brief Update packet timestamp.
  218. ///
  219. /// Updates packet timestamp. This method is invoked
  220. /// by interface manager just before sending or
  221. /// just after receiving it.
  222. /// @throw isc::Unexpected if timestamp update failed
  223. void updateTimestamp();
  224. /// @brief Return textual type of packet.
  225. ///
  226. /// Returns the name of valid packet received by the server (e.g. SOLICIT).
  227. /// If the packet is unknown - or if it is a valid DHCP packet but not one
  228. /// expected to be received by the server (such as an ADVERTISE), the string
  229. /// "UNKNOWN" is returned. This method is used in debug messages.
  230. ///
  231. /// As the operation of the method does not depend on any server state, it
  232. /// is declared static. There is also non-static getName() method that
  233. /// works on Pkt6 objects.
  234. ///
  235. /// @param type DHCPv6 packet type
  236. ///
  237. /// @return Pointer to "const" string containing the packet name.
  238. /// Note that this string is statically allocated and MUST NOT
  239. /// be freed by the caller.
  240. static const char* getName(uint8_t type);
  241. /// @brief returns textual representation of packet type.
  242. ///
  243. /// This method requires an object. There is also static version, which
  244. /// requires one parameter (type).
  245. ///
  246. /// @return Pointer to "const" string containing packet name.
  247. /// Note that this string is statically allocated and MUST NOT
  248. /// be freed by the caller.
  249. const char* getName() const;
  250. protected:
  251. /// Builds on wire packet for TCP transmission.
  252. ///
  253. /// TODO This function is not implemented yet.
  254. ///
  255. /// @return true, if build was successful
  256. bool packTCP();
  257. /// Builds on wire packet for UDP transmission.
  258. ///
  259. /// @return true, if build was successful
  260. bool packUDP();
  261. /// @brief Parses on-wire form of TCP DHCPv6 packet.
  262. ///
  263. /// Parses received packet, stored in on-wire format in data_.
  264. /// data_len_ must be set to indicate data length.
  265. /// Will create a collection of option objects that will
  266. /// be stored in options_ container.
  267. ///
  268. /// TODO This function is not implemented yet.
  269. ///
  270. /// @return true, if build was successful
  271. bool unpackTCP();
  272. /// @brief Parses on-wire form of UDP DHCPv6 packet.
  273. ///
  274. /// Parses received packet, stored in on-wire format in data_.
  275. /// data_len_ must be set to indicate data length.
  276. /// Will create a collection of option objects that will
  277. /// be stored in options_ container.
  278. ///
  279. /// @return true, if build was successful
  280. bool unpackUDP();
  281. /// UDP (usually) or TCP (bulk leasequery or failover)
  282. DHCPv6Proto proto_;
  283. /// DHCPv6 message type
  284. uint8_t msg_type_;
  285. /// DHCPv6 transaction-id
  286. uint32_t transid_;
  287. /// unparsed data (in received packets)
  288. ///
  289. /// @warning This protected member is accessed by derived
  290. /// classes directly. One of such derived classes is
  291. /// @ref perfdhcp::PerfPkt6. The impact on derived clasess'
  292. /// behavior must be taken into consideration before making
  293. /// changes to this member such as access scope restriction or
  294. /// data format change etc.
  295. OptionBuffer data_;
  296. /// name of the network interface the packet was received/to be sent over
  297. std::string iface_;
  298. /// @brief interface index
  299. ///
  300. /// interface index (each network interface has assigned unique ifindex
  301. /// it is functional equvalent of name, but sometimes more useful, e.g.
  302. /// when using crazy systems that allow spaces in interface names
  303. /// e.g. windows
  304. int ifindex_;
  305. /// local address (dst if receiving packet, src if sending packet)
  306. isc::asiolink::IOAddress local_addr_;
  307. /// remote address (src if receiving packet, dst if sending packet)
  308. isc::asiolink::IOAddress remote_addr_;
  309. /// local TDP or UDP port
  310. uint16_t local_port_;
  311. /// remote TCP or UDP port
  312. uint16_t remote_port_;
  313. /// output buffer (used during message transmission)
  314. ///
  315. /// @warning This protected member is accessed by derived
  316. /// classes directly. One of such derived classes is
  317. /// @ref perfdhcp::PerfPkt6. The impact on derived clasess'
  318. /// behavior must be taken into consideration before making
  319. /// changes to this member such as access scope restriction or
  320. /// data format change etc.
  321. isc::util::OutputBuffer bufferOut_;
  322. /// packet timestamp
  323. boost::posix_time::ptime timestamp_;
  324. }; // Pkt6 class
  325. typedef boost::shared_ptr<Pkt6> Pkt6Ptr;
  326. } // isc::dhcp namespace
  327. } // isc namespace
  328. #endif