pkt6.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #include <dhcp/dhcp6.h>
  15. #include <dhcp/libdhcp++.h>
  16. #include <dhcp/pkt6.h>
  17. #include <exceptions/exceptions.h>
  18. #include <iostream>
  19. #include <sstream>
  20. using namespace std;
  21. namespace isc {
  22. namespace dhcp {
  23. Pkt6::Pkt6(const uint8_t* buf, uint32_t buf_len, DHCPv6Proto proto /* = UDP */) :
  24. proto_(proto),
  25. msg_type_(0),
  26. transid_(rand()%0xffffff),
  27. iface_(""),
  28. ifindex_(-1),
  29. local_addr_("::"),
  30. remote_addr_("::"),
  31. local_port_(0),
  32. remote_port_(0),
  33. bufferOut_(0) {
  34. data_.resize(buf_len);
  35. memcpy(&data_[0], buf, buf_len);
  36. }
  37. Pkt6::Pkt6(uint8_t msg_type, uint32_t transid, DHCPv6Proto proto /*= UDP*/) :
  38. proto_(proto),
  39. msg_type_(msg_type),
  40. transid_(transid),
  41. iface_(""),
  42. ifindex_(-1),
  43. local_addr_("::"),
  44. remote_addr_("::"),
  45. local_port_(0),
  46. remote_port_(0),
  47. bufferOut_(0) {
  48. }
  49. uint16_t Pkt6::len() {
  50. uint16_t length = DHCPV6_PKT_HDR_LEN; // DHCPv6 header
  51. for (Option::OptionCollection::iterator it = options_.begin();
  52. it != options_.end();
  53. ++it) {
  54. length += (*it).second->len();
  55. }
  56. return (length);
  57. }
  58. bool
  59. Pkt6::pack() {
  60. switch (proto_) {
  61. case UDP:
  62. return packUDP();
  63. case TCP:
  64. return packTCP();
  65. default:
  66. isc_throw(BadValue, "Invalid protocol specified (non-TCP, non-UDP)");
  67. }
  68. return (false); // never happens
  69. }
  70. bool
  71. Pkt6::packUDP() {
  72. try {
  73. // DHCPv6 header: message-type (1 octect) + transaction id (3 octets)
  74. bufferOut_.writeUint8(msg_type_);
  75. // store 3-octet transaction-id
  76. bufferOut_.writeUint8( (transid_ >> 16) & 0xff );
  77. bufferOut_.writeUint8( (transid_ >> 8) & 0xff );
  78. bufferOut_.writeUint8( (transid_) & 0xff );
  79. // the rest are options
  80. LibDHCP::packOptions6(bufferOut_, options_);
  81. }
  82. catch (const Exception& e) {
  83. /// @todo: throw exception here once we turn this function to void.
  84. return (false);
  85. }
  86. return (true);
  87. }
  88. bool
  89. Pkt6::packTCP() {
  90. /// TODO Implement this function.
  91. isc_throw(Unexpected, "DHCPv6 over TCP (bulk leasequery and failover)"
  92. "not implemented yet.");
  93. }
  94. bool
  95. Pkt6::unpack() {
  96. switch (proto_) {
  97. case UDP:
  98. return unpackUDP();
  99. case TCP:
  100. return unpackTCP();
  101. default:
  102. isc_throw(BadValue, "Invalid protocol specified (non-TCP, non-UDP)");
  103. }
  104. return (false); // never happens
  105. }
  106. bool
  107. Pkt6::unpackUDP() {
  108. if (data_.size() < 4) {
  109. // @todo: throw exception here informing that packet is truncated
  110. // once we turn this function to void.
  111. return (false);
  112. }
  113. msg_type_ = data_[0];
  114. transid_ = ( (data_[1]) << 16 ) +
  115. ((data_[2]) << 8) + (data_[3]);
  116. transid_ = transid_ & 0xffffff;
  117. try {
  118. OptionBuffer opt_buffer(data_.begin() + 4, data_.end());
  119. LibDHCP::unpackOptions6(opt_buffer, options_);
  120. } catch (const Exception& e) {
  121. // @todo: throw exception here once we turn this function to void.
  122. return (false);
  123. }
  124. return (true);
  125. }
  126. bool
  127. Pkt6::unpackTCP() {
  128. isc_throw(Unexpected, "DHCPv6 over TCP (bulk leasequery and failover) "
  129. "not implemented yet.");
  130. }
  131. std::string
  132. Pkt6::toText() {
  133. stringstream tmp;
  134. tmp << "localAddr=[" << local_addr_.toText() << "]:" << local_port_
  135. << " remoteAddr=[" << remote_addr_.toText()
  136. << "]:" << remote_port_ << endl;
  137. tmp << "msgtype=" << msg_type_ << ", transid=0x" << hex << transid_
  138. << dec << endl;
  139. for (isc::dhcp::Option::OptionCollection::iterator opt=options_.begin();
  140. opt != options_.end();
  141. ++opt) {
  142. tmp << opt->second->toText() << std::endl;
  143. }
  144. return tmp.str();
  145. }
  146. OptionPtr
  147. Pkt6::getOption(uint16_t opt_type) {
  148. isc::dhcp::Option::OptionCollection::const_iterator x = options_.find(opt_type);
  149. if (x!=options_.end()) {
  150. return (*x).second;
  151. }
  152. return OptionPtr(); // NULL
  153. }
  154. isc::dhcp::Option::OptionCollection
  155. Pkt6::getOptions(uint16_t opt_type) {
  156. isc::dhcp::Option::OptionCollection found;
  157. for (Option::OptionCollection::const_iterator x = options_.begin();
  158. x != options_.end(); ++x) {
  159. if (x->first == opt_type) {
  160. found.insert(make_pair(opt_type, x->second));
  161. }
  162. }
  163. return (found);
  164. }
  165. void
  166. Pkt6::addOption(const OptionPtr& opt) {
  167. options_.insert(pair<int, boost::shared_ptr<Option> >(opt->getType(), opt));
  168. }
  169. bool
  170. Pkt6::delOption(uint16_t type) {
  171. isc::dhcp::Option::OptionCollection::iterator x = options_.find(type);
  172. if (x!=options_.end()) {
  173. options_.erase(x);
  174. return (true); // delete successful
  175. }
  176. return (false); // can't find option to be deleted
  177. }
  178. void Pkt6::repack() {
  179. bufferOut_.writeData(&data_[0], data_.size());
  180. }
  181. void
  182. Pkt6::updateTimestamp() {
  183. timestamp_ = boost::posix_time::microsec_clock::universal_time();
  184. }
  185. const char*
  186. Pkt6::getName(uint8_t type) {
  187. static const char* CONFIRM = "CONFIRM";
  188. static const char* DECLINE = "DECLINE";
  189. static const char* INFORMATION_REQUEST = "INFORMATION_REQUEST";
  190. static const char* REBIND = "REBIND";
  191. static const char* RELEASE = "RELEASE";
  192. static const char* RENEW = "RENEW";
  193. static const char* REQUEST = "REQUEST";
  194. static const char* SOLICIT = "SOLICIT";
  195. static const char* UNKNOWN = "UNKNOWN";
  196. switch (type) {
  197. case DHCPV6_CONFIRM:
  198. return (CONFIRM);
  199. case DHCPV6_DECLINE:
  200. return (DECLINE);
  201. case DHCPV6_INFORMATION_REQUEST:
  202. return (INFORMATION_REQUEST);
  203. case DHCPV6_REBIND:
  204. return (REBIND);
  205. case DHCPV6_RELEASE:
  206. return (RELEASE);
  207. case DHCPV6_RENEW:
  208. return (RENEW);
  209. case DHCPV6_REQUEST:
  210. return (REQUEST);
  211. case DHCPV6_SOLICIT:
  212. return (SOLICIT);
  213. default:
  214. ;
  215. }
  216. return (UNKNOWN);
  217. }
  218. const char* Pkt6::getName() const {
  219. return (getName(getType()));
  220. }
  221. } // end of isc::dhcp namespace
  222. } // end of isc namespace