pkt6.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/pkt6.h>
  16. #include <dhcp/libdhcp++.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. // TODO: Once OutputBuffer is used here, some thing like this
  73. // will be used. Yikes! That's ugly.
  74. // bufferOut_.writeData(ciaddr_.getAddress().to_v6().to_bytes().data(), 16);
  75. // It is better to implement a method in IOAddress that extracts
  76. // vector<uint8_t>
  77. try {
  78. // DHCPv6 header: message-type (1 octect) + transaction id (3 octets)
  79. bufferOut_.writeUint8(msg_type_);
  80. // store 3-octet transaction-id
  81. bufferOut_.writeUint8( (transid_ >> 16) & 0xff );
  82. bufferOut_.writeUint8( (transid_ >> 8) & 0xff );
  83. bufferOut_.writeUint8( (transid_) & 0xff );
  84. // the rest are options
  85. LibDHCP::packOptions6(bufferOut_, options_);
  86. }
  87. catch (const Exception& e) {
  88. cout << "Packet build failed:" << e.what() << endl;
  89. return (false);
  90. }
  91. return (true);
  92. }
  93. bool
  94. Pkt6::packTCP() {
  95. /// TODO Implement this function.
  96. isc_throw(Unexpected, "DHCPv6 over TCP (bulk leasequery and failover)"
  97. "not implemented yet.");
  98. }
  99. bool
  100. Pkt6::unpack() {
  101. switch (proto_) {
  102. case UDP:
  103. return unpackUDP();
  104. case TCP:
  105. return unpackTCP();
  106. default:
  107. isc_throw(BadValue, "Invalid protocol specified (non-TCP, non-UDP)");
  108. }
  109. return (false); // never happens
  110. }
  111. bool
  112. Pkt6::unpackUDP() {
  113. if (data_.size() < 4) {
  114. std::cout << "DHCPv6 packet truncated. Only " << data_.size()
  115. << " bytes. Need at least 4." << std::endl;
  116. return (false);
  117. }
  118. msg_type_ = data_[0];
  119. transid_ = ( (data_[1]) << 16 ) +
  120. ((data_[2]) << 8) + (data_[3]);
  121. transid_ = transid_ & 0xffffff;
  122. try {
  123. OptionBuffer opt_buffer(data_.begin() + 4, data_.end());
  124. LibDHCP::unpackOptions6(opt_buffer, options_);
  125. } catch (const Exception& e) {
  126. cout << "Packet parsing failed:" << e.what() << endl;
  127. return (false);
  128. }
  129. return (true);
  130. }
  131. bool
  132. Pkt6::unpackTCP() {
  133. isc_throw(Unexpected, "DHCPv6 over TCP (bulk leasequery and failover) "
  134. "not implemented yet.");
  135. }
  136. std::string
  137. Pkt6::toText() {
  138. stringstream tmp;
  139. tmp << "localAddr=[" << local_addr_.toText() << "]:" << local_port_
  140. << " remoteAddr=[" << remote_addr_.toText()
  141. << "]:" << remote_port_ << endl;
  142. tmp << "msgtype=" << msg_type_ << ", transid=0x" << hex << transid_
  143. << dec << endl;
  144. for (isc::dhcp::Option::OptionCollection::iterator opt=options_.begin();
  145. opt != options_.end();
  146. ++opt) {
  147. tmp << opt->second->toText() << std::endl;
  148. }
  149. return tmp.str();
  150. }
  151. boost::shared_ptr<isc::dhcp::Option>
  152. Pkt6::getOption(uint16_t opt_type) {
  153. isc::dhcp::Option::OptionCollection::const_iterator x = options_.find(opt_type);
  154. if (x!=options_.end()) {
  155. return (*x).second;
  156. }
  157. return boost::shared_ptr<isc::dhcp::Option>(); // NULL
  158. }
  159. void
  160. Pkt6::addOption(boost::shared_ptr<Option> opt) {
  161. options_.insert(pair<int, boost::shared_ptr<Option> >(opt->getType(), opt));
  162. }
  163. bool
  164. Pkt6::delOption(uint16_t type) {
  165. isc::dhcp::Option::OptionCollection::iterator x = options_.find(type);
  166. if (x!=options_.end()) {
  167. options_.erase(x);
  168. return (true); // delete successful
  169. }
  170. return (false); // can't find option to be deleted
  171. }
  172. void Pkt6::repack() {
  173. cout << "Convering RX packet to TX packet: " << data_.size() << " bytes." << endl;
  174. bufferOut_.writeData(&data_[0], data_.size());
  175. }
  176. } // end of isc::dhcp namespace
  177. } // end of isc namespace