pkt6.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 "asiolink/io_address.h"
  21. #include "dhcp/option.h"
  22. namespace isc {
  23. namespace dhcp {
  24. class Pkt6 {
  25. public:
  26. /// specifes DHCPv6 packet header length
  27. const static size_t DHCPV6_PKT_HDR_LEN = 4;
  28. /// DHCPv6 transport protocol
  29. enum DHCPv6Proto {
  30. UDP = 0, // most packets are UDP
  31. TCP = 1 // there are TCP DHCPv6 packets (bulk leasequery, failover)
  32. };
  33. /// Constructor, used in replying to a message
  34. ///
  35. /// @param msg_type type of message (SOLICIT=1, ADVERTISE=2, ...)
  36. /// @param transid transaction-id
  37. /// @param proto protocol (TCP or UDP)
  38. Pkt6(uint8_t msg_type,
  39. uint32_t transid,
  40. DHCPv6Proto proto = UDP);
  41. /// Constructor, used in message transmission
  42. ///
  43. /// Creates new message. Transaction-id will randomized.
  44. ///
  45. /// @param buf pointer to a buffer of received packet content
  46. /// @param len size of buffer of received packet content
  47. /// @param proto protocol (usually UDP, but TCP will be supported eventually)
  48. Pkt6(const uint8_t* buf, uint32_t len, DHCPv6Proto proto = UDP);
  49. /// @brief Prepares on-wire format.
  50. ///
  51. /// Prepares on-wire format of message and all its options.
  52. /// Options must be stored in options_ field.
  53. /// Output buffer will be stored in data_. Length
  54. /// will be set in data_len_.
  55. ///
  56. /// @return true if packing procedure was successful
  57. bool pack();
  58. /// @brief Dispatch method that handles binary packet parsing.
  59. ///
  60. /// This method calls appropriate dispatch function (unpackUDP or
  61. /// unpackTCP).
  62. ///
  63. /// @return true if parsing was successful
  64. bool unpack();
  65. /// @brief Returns reference to output buffer.
  66. ///
  67. /// Returned buffer will contain reasonable data only for
  68. /// output (TX) packet and after pack() was called. This buffer
  69. /// is only valid till Pkt6 object is valid.
  70. ///
  71. /// RX packet or TX packet before pack() will return buffer with
  72. /// zero length
  73. ///
  74. /// @return reference to output buffer
  75. const isc::util::OutputBuffer& getBuffer() const { return (bufferOut_); };
  76. /// @brief Returns reference to input buffer.
  77. ///
  78. /// @return reference to input buffer
  79. const OptionBuffer& getData() const { return(data_); }
  80. /// @brief Returns protocol of this packet (UDP or TCP).
  81. ///
  82. /// @return protocol type
  83. DHCPv6Proto getProto();
  84. /// Sets protocol of this packet.
  85. ///
  86. /// @param proto protocol (UDP or TCP)
  87. void setProto(DHCPv6Proto proto = UDP) { proto_ = proto; }
  88. /// @brief Returns text representation of the packet.
  89. ///
  90. /// This function is useful mainly for debugging.
  91. ///
  92. /// @return string with text representation
  93. std::string toText();
  94. /// @brief Returns length of the packet.
  95. ///
  96. /// This function returns size required to hold this packet.
  97. /// It includes DHCPv6 header and all options stored in
  98. /// options_ field.
  99. ///
  100. /// Note: It does not return proper length of incoming packets
  101. /// before they are unpacked.
  102. ///
  103. /// @return number of bytes required to assemble this packet
  104. uint16_t len();
  105. /// Returns message type (e.g. 1 = SOLICIT)
  106. ///
  107. /// @return message type
  108. uint8_t getType() { return (msg_type_); }
  109. /// Sets message type (e.g. 1 = SOLICIT)
  110. ///
  111. /// @param type message type to be set
  112. void setType(uint8_t type) { msg_type_=type; };
  113. /// Returns value of transaction-id field
  114. ///
  115. /// @return transaction-id
  116. uint32_t getTransid() { return (transid_); };
  117. /// Adds an option to this packet.
  118. ///
  119. /// @param opt option to be added.
  120. void addOption(OptionPtr opt);
  121. /// @brief Returns the first option of specified type.
  122. ///
  123. /// Returns the first option of specified type. Note that in DHCPv6 several
  124. /// instances of the same option are allowed (and frequently used).
  125. /// See getOptions().
  126. ///
  127. /// @param type option type we are looking for
  128. ///
  129. /// @return pointer to found option (or NULL)
  130. OptionPtr getOption(uint16_t type);
  131. /// Attempts to delete first suboption of requested type
  132. ///
  133. /// @param type Type of option to be deleted.
  134. ///
  135. /// @return true if option was deleted, false if no such option existed
  136. bool delOption(uint16_t type);
  137. /// @brief This method copies data from output buffer to input buffer
  138. ///
  139. /// This is useful only in testing
  140. void repack();
  141. /// @brief Sets remote address.
  142. ///
  143. /// @param remote specifies remote address
  144. void setRemoteAddr(const isc::asiolink::IOAddress& remote) { remote_addr_ = remote; }
  145. /// @brief Returns remote address
  146. ///
  147. /// @return remote address
  148. const isc::asiolink::IOAddress& getRemoteAddr() { return (remote_addr_); }
  149. /// @brief Sets local address.
  150. ///
  151. /// @param local specifies local address
  152. void setLocalAddr(const isc::asiolink::IOAddress& local) { local_addr_ = local; }
  153. /// @brief Returns local address.
  154. ///
  155. /// @return local address
  156. const isc::asiolink::IOAddress& getLocalAddr() { return (local_addr_); }
  157. /// @brief Sets local port.
  158. ///
  159. /// @param local specifies local port
  160. void setLocalPort(uint16_t local) { local_port_ = local; }
  161. /// @brief Returns local port.
  162. ///
  163. /// @return local port
  164. uint16_t getLocalPort() { return (local_port_); }
  165. /// @brief Sets remote port.
  166. ///
  167. /// @param remote specifies remote port
  168. void setRemotePort(uint16_t remote) { remote_port_ = remote; }
  169. /// @brief Returns remote port.
  170. ///
  171. /// @return remote port
  172. uint16_t getRemotePort() { return (remote_port_); }
  173. /// @brief Sets interface index.
  174. ///
  175. /// @param ifindex specifies interface index.
  176. void setIndex(uint32_t ifindex) { ifindex_ = ifindex; };
  177. /// @brief Returns interface index.
  178. ///
  179. /// @return interface index
  180. uint32_t getIndex() const { return (ifindex_); };
  181. /// @brief Returns interface name.
  182. ///
  183. /// Returns interface name over which packet was received or is
  184. /// going to be transmitted.
  185. ///
  186. /// @return interface name
  187. std::string getIface() const { return iface_; };
  188. /// @brief Returns packet timestamp.
  189. ///
  190. /// Returns packet timestamp value updated when
  191. /// packet is received or send.
  192. ///
  193. /// @return packet timestamp.
  194. timespec getTimestamp() const { return timestamp_; }
  195. /// @brief Sets interface name.
  196. ///
  197. /// Sets interface name over which packet was received or is
  198. /// going to be transmitted.
  199. ///
  200. /// @return interface name
  201. void setIface(const std::string& iface ) { iface_ = iface; };
  202. /// TODO Need to implement getOptions() as well
  203. /// collection of options present in this message
  204. isc::dhcp::Option::OptionCollection options_;
  205. /// @brief Update packet timestamp.
  206. ///
  207. /// Updates packet timestamp. This method is invoked
  208. /// by interface manager just before sending or
  209. /// just after receiving it.
  210. /// @throw isc::Unexpected if timestamp update failed
  211. void updateTimestamp();
  212. protected:
  213. /// Builds on wire packet for TCP transmission.
  214. ///
  215. /// TODO This function is not implemented yet.
  216. ///
  217. /// @return true, if build was successful
  218. bool packTCP();
  219. /// Builds on wire packet for UDP transmission.
  220. ///
  221. /// @return true, if build was successful
  222. bool packUDP();
  223. /// @brief Parses on-wire form of TCP DHCPv6 packet.
  224. ///
  225. /// Parses received packet, stored in on-wire format in data_.
  226. /// data_len_ must be set to indicate data length.
  227. /// Will create a collection of option objects that will
  228. /// be stored in options_ container.
  229. ///
  230. /// TODO This function is not implemented yet.
  231. ///
  232. /// @return true, if build was successful
  233. bool unpackTCP();
  234. /// @brief Parses on-wire form of UDP DHCPv6 packet.
  235. ///
  236. /// Parses received packet, stored in on-wire format in data_.
  237. /// data_len_ must be set to indicate data length.
  238. /// Will create a collection of option objects that will
  239. /// be stored in options_ container.
  240. ///
  241. /// @return true, if build was successful
  242. bool unpackUDP();
  243. /// UDP (usually) or TCP (bulk leasequery or failover)
  244. DHCPv6Proto proto_;
  245. /// DHCPv6 message type
  246. uint8_t msg_type_;
  247. /// DHCPv6 transaction-id
  248. uint32_t transid_;
  249. /// unparsed data (in received packets)
  250. OptionBuffer data_;
  251. /// name of the network interface the packet was received/to be sent over
  252. std::string iface_;
  253. /// @brief interface index
  254. ///
  255. /// interface index (each network interface has assigned unique ifindex
  256. /// it is functional equvalent of name, but sometimes more useful, e.g.
  257. /// when using crazy systems that allow spaces in interface names
  258. /// e.g. windows
  259. int ifindex_;
  260. /// local address (dst if receiving packet, src if sending packet)
  261. isc::asiolink::IOAddress local_addr_;
  262. /// remote address (src if receiving packet, dst if sending packet)
  263. isc::asiolink::IOAddress remote_addr_;
  264. /// local TDP or UDP port
  265. uint16_t local_port_;
  266. /// remote TCP or UDP port
  267. uint16_t remote_port_;
  268. /// output buffer (used during message transmission)
  269. isc::util::OutputBuffer bufferOut_;
  270. /// packet timestamp
  271. timespec timestamp_;
  272. }; // Pkt6 class
  273. typedef boost::shared_ptr<Pkt6> Pkt6Ptr;
  274. } // isc::dhcp namespace
  275. } // isc namespace
  276. #endif