pkt6.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 <iostream>
  17. #include <time.h>
  18. #include <boost/shared_ptr.hpp>
  19. #include <boost/shared_array.hpp>
  20. #include <boost/date_time/posix_time/posix_time.hpp>
  21. #include "asiolink/io_address.h"
  22. #include "dhcp/option.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() { 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() { 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. protected:
  225. /// Builds on wire packet for TCP transmission.
  226. ///
  227. /// TODO This function is not implemented yet.
  228. ///
  229. /// @return true, if build was successful
  230. bool packTCP();
  231. /// Builds on wire packet for UDP transmission.
  232. ///
  233. /// @return true, if build was successful
  234. bool packUDP();
  235. /// @brief Parses on-wire form of TCP DHCPv6 packet.
  236. ///
  237. /// Parses received packet, stored in on-wire format in data_.
  238. /// data_len_ must be set to indicate data length.
  239. /// Will create a collection of option objects that will
  240. /// be stored in options_ container.
  241. ///
  242. /// TODO This function is not implemented yet.
  243. ///
  244. /// @return true, if build was successful
  245. bool unpackTCP();
  246. /// @brief Parses on-wire form of UDP DHCPv6 packet.
  247. ///
  248. /// Parses received packet, stored in on-wire format in data_.
  249. /// data_len_ must be set to indicate data length.
  250. /// Will create a collection of option objects that will
  251. /// be stored in options_ container.
  252. ///
  253. /// @return true, if build was successful
  254. bool unpackUDP();
  255. /// UDP (usually) or TCP (bulk leasequery or failover)
  256. DHCPv6Proto proto_;
  257. /// DHCPv6 message type
  258. uint8_t msg_type_;
  259. /// DHCPv6 transaction-id
  260. uint32_t transid_;
  261. /// unparsed data (in received packets)
  262. ///
  263. /// @warning This protected member is accessed by derived
  264. /// classes directly. One of such derived classes is
  265. /// @ref perfdhcp::PerfPkt6. The impact on derived clasess'
  266. /// behavior must be taken into consideration before making
  267. /// changes to this member such as access scope restriction or
  268. /// data format change etc.
  269. OptionBuffer data_;
  270. /// name of the network interface the packet was received/to be sent over
  271. std::string iface_;
  272. /// @brief interface index
  273. ///
  274. /// interface index (each network interface has assigned unique ifindex
  275. /// it is functional equvalent of name, but sometimes more useful, e.g.
  276. /// when using crazy systems that allow spaces in interface names
  277. /// e.g. windows
  278. int ifindex_;
  279. /// local address (dst if receiving packet, src if sending packet)
  280. isc::asiolink::IOAddress local_addr_;
  281. /// remote address (src if receiving packet, dst if sending packet)
  282. isc::asiolink::IOAddress remote_addr_;
  283. /// local TDP or UDP port
  284. uint16_t local_port_;
  285. /// remote TCP or UDP port
  286. uint16_t remote_port_;
  287. /// output buffer (used during message transmission)
  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. isc::util::OutputBuffer bufferOut_;
  296. /// packet timestamp
  297. boost::posix_time::ptime timestamp_;
  298. }; // Pkt6 class
  299. typedef boost::shared_ptr<Pkt6> Pkt6Ptr;
  300. } // isc::dhcp namespace
  301. } // isc namespace
  302. #endif