pkt6.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright (C) 2011 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 <boost/shared_ptr.hpp>
  18. #include <boost/shared_array.hpp>
  19. #include "asiolink/io_address.h"
  20. #include "dhcp/option.h"
  21. namespace isc {
  22. namespace dhcp {
  23. class Pkt6 {
  24. public:
  25. /// specifes DHCPv6 packet header length
  26. const static size_t DHCPV6_PKT_HDR_LEN = 4;
  27. /// DHCPv6 transport protocol
  28. enum DHCPv6Proto {
  29. UDP = 0, // most packets are UDP
  30. TCP = 1 // there are TCP DHCPv6 packets (bulk leasequery, failover)
  31. };
  32. /// Constructor, used in replying to a message
  33. ///
  34. /// @param msg_type type of message (SOLICIT=1, ADVERTISE=2, ...)
  35. /// @param transid transaction-id
  36. /// @param proto protocol (TCP or UDP)
  37. Pkt6(unsigned char msg_type,
  38. unsigned int transid,
  39. DHCPv6Proto proto = UDP);
  40. /// Constructor, used in message transmission
  41. ///
  42. /// Creates new message. Transaction-id will randomized.
  43. ///
  44. /// @param len size of buffer to be allocated for this packet.
  45. /// @param proto protocol (usually UDP, but TCP will be supported eventually)
  46. Pkt6(unsigned int len, DHCPv6Proto proto = UDP);
  47. /// @brief Prepares on-wire format.
  48. ///
  49. /// Prepares on-wire format of message and all its options.
  50. /// Options must be stored in options_ field.
  51. /// Output buffer will be stored in data_. Length
  52. /// will be set in data_len_.
  53. ///
  54. /// @return true if packing procedure was successful
  55. bool
  56. pack();
  57. /// @brief Dispatch method that handles binary packet parsing.
  58. ///
  59. /// This method calls appropriate dispatch function (unpackUDP or
  60. /// unpackTCP).
  61. ///
  62. /// @return true if parsing was successful
  63. bool
  64. unpack();
  65. /// Returns protocol of this packet (UDP or TCP)
  66. ///
  67. /// @return protocol type
  68. DHCPv6Proto
  69. getProto();
  70. /// Sets protocol of this packet.
  71. ///
  72. /// @param proto protocol (UDP or TCP)
  73. ///
  74. void
  75. setProto(DHCPv6Proto proto = UDP) { proto_ = proto; }
  76. /// @brief Returns text representation of the packet.
  77. ///
  78. /// This function is useful mainly for debugging.
  79. ///
  80. /// @return string with text representation
  81. std::string
  82. toText();
  83. /// @brief Returns calculated length of the packet.
  84. ///
  85. /// This function returns size of required buffer to buld this packet.
  86. /// To use that function, options_ field must be set.
  87. ///
  88. /// @return number of bytes required to build this packet
  89. unsigned short
  90. len();
  91. /// Returns message type (e.g. 1 = SOLICIT)
  92. ///
  93. /// @return message type
  94. unsigned char
  95. getType() { return (msg_type_); }
  96. /// Sets message type (e.g. 1 = SOLICIT)
  97. ///
  98. /// @param type message type to be set
  99. void setType(unsigned char type) { msg_type_=type; };
  100. /// Returns value of transaction-id field
  101. ///
  102. /// @return transaction-id
  103. unsigned int getTransid() { return (transid_); };
  104. /// Adds an option to this packet.
  105. ///
  106. /// @param opt option to be added.
  107. void addOption(boost::shared_ptr<isc::dhcp::Option> opt);
  108. /// @brief Returns the first option of specified type.
  109. ///
  110. /// Returns the first option of specified type. Note that in DHCPv6 several
  111. /// instances of the same option are allowed (and frequently used).
  112. /// See getOptions().
  113. ///
  114. /// @param opt_type option type we are looking for
  115. ///
  116. /// @return pointer to found option (or NULL)
  117. boost::shared_ptr<isc::dhcp::Option>
  118. getOption(unsigned short type);
  119. /// Attempts to delete first suboption of requested type
  120. ///
  121. /// @param type Type of option to be deleted.
  122. ///
  123. /// @return true if option was deleted, false if no such option existed
  124. bool
  125. delOption(unsigned short type);
  126. /// TODO need getter/setter wrappers
  127. /// and hide following fields as protected
  128. /// buffer that holds memory. It is shared_array as options may
  129. /// share pointer to this buffer
  130. boost::shared_array<uint8_t> data_;
  131. /// length of the data
  132. unsigned int data_len_;
  133. /// local address (dst if receiving packet, src if sending packet)
  134. isc::asiolink::IOAddress local_addr_;
  135. /// remote address (src if receiving packet, dst if sending packet)
  136. isc::asiolink::IOAddress remote_addr_;
  137. /// name of the network interface the packet was received/to be sent over
  138. std::string iface_;
  139. /// @brief interface index
  140. ///
  141. /// interface index (each network interface has assigned unique ifindex
  142. /// it is functional equvalent of name, but sometimes more useful, e.g.
  143. /// when using crazy systems that allow spaces in interface names
  144. /// e.g. windows
  145. int ifindex_;
  146. /// local TDP or UDP port
  147. int local_port_;
  148. /// remote TCP or UDP port
  149. int remote_port_;
  150. /// TODO Need to implement getOptions() as well
  151. /// collection of options present in this message
  152. isc::dhcp::Option::OptionCollection options_;
  153. protected:
  154. /// Builds on wire packet for TCP transmission.
  155. ///
  156. /// TODO This function is not implemented yet.
  157. ///
  158. /// @return true, if build was successful
  159. bool packTCP();
  160. /// Builds on wire packet for UDP transmission.
  161. ///
  162. /// @return true, if build was successful
  163. bool packUDP();
  164. /// @brief Parses on-wire form of TCP DHCPv6 packet.
  165. ///
  166. /// Parses received packet, stored in on-wire format in data_.
  167. /// data_len_ must be set to indicate data length.
  168. /// Will create a collection of option objects that will
  169. /// be stored in options_ container.
  170. ///
  171. /// TODO This function is not implemented yet.
  172. ///
  173. /// @return true, if build was successful
  174. bool unpackTCP();
  175. /// @brief Parses on-wire form of UDP DHCPv6 packet.
  176. ///
  177. /// Parses received packet, stored in on-wire format in data_.
  178. /// data_len_ must be set to indicate data length.
  179. /// Will create a collection of option objects that will
  180. /// be stored in options_ container.
  181. ///
  182. /// @return true, if build was successful
  183. bool unpackUDP();
  184. /// UDP (usually) or TCP (bulk leasequery or failover)
  185. DHCPv6Proto proto_;
  186. /// DHCPv6 message type
  187. int msg_type_;
  188. /// DHCPv6 transaction-id
  189. unsigned int transid_;
  190. }; // Pkt6 class
  191. } // isc::dhcp namespace
  192. } // isc namespace
  193. #endif