pkt6.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #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. using namespace isc::dhcp;
  22. namespace isc {
  23. Pkt6::Pkt6(unsigned int dataLen, DHCPv6Proto proto /* = UDP */)
  24. :data_len_(dataLen),
  25. local_addr_("::"),
  26. remote_addr_("::"),
  27. iface_(""),
  28. ifindex_(-1),
  29. local_port_(-1),
  30. remote_port_(-1),
  31. proto_(proto),
  32. msg_type_(-1),
  33. transid_(rand()%0xffffff)
  34. {
  35. data_ = boost::shared_array<uint8_t>(new uint8_t[dataLen]);
  36. data_len_ = dataLen;
  37. }
  38. Pkt6::Pkt6(uint8_t msg_type,
  39. unsigned int transid,
  40. DHCPv6Proto proto /*= UDP*/)
  41. :local_addr_("::"),
  42. remote_addr_("::"),
  43. iface_(""),
  44. ifindex_(-1),
  45. local_port_(-1),
  46. remote_port_(-1),
  47. proto_(proto),
  48. msg_type_(msg_type),
  49. transid_(transid) {
  50. data_ = boost::shared_array<uint8_t>(new uint8_t[4]);
  51. data_len_ = 4;
  52. }
  53. unsigned short
  54. Pkt6::len() {
  55. unsigned int length = DHCPV6_PKT_HDR_LEN; // DHCPv6 header
  56. for (Option::Option6Collection::iterator it = options_.begin();
  57. it != options_.end();
  58. ++it) {
  59. length += (*it).second->len();
  60. }
  61. return (length);
  62. }
  63. bool
  64. Pkt6::pack() {
  65. switch (proto_) {
  66. case UDP:
  67. return packUDP();
  68. case TCP:
  69. return packTCP();
  70. default:
  71. isc_throw(BadValue, "Invalid protocol specified (non-TCP, non-UDP)");
  72. }
  73. return (false); // never happens
  74. }
  75. bool
  76. Pkt6::packUDP() {
  77. // TODO: Once OutputBuffer is used here, some thing like this
  78. // will be used. Yikes! That's ugly.
  79. // bufferOut_.writeData(ciaddr_.getAddress().to_v6().to_bytes().data(), 16);
  80. // It is better to implement a method in IOAddress that extracts
  81. // vector<uint8_t>
  82. unsigned short length = len();
  83. if (data_len_ < length) {
  84. cout << "Previous len=" << data_len_ << ", allocating new buffer: len="
  85. << length << endl;
  86. // May throw exception if out of memory. That is rather fatal,
  87. // so we don't catch this
  88. data_ = boost::shared_array<uint8_t>(new uint8_t[length]);
  89. data_len_ = length;
  90. }
  91. data_len_ = length;
  92. try {
  93. // DHCPv6 header: message-type (1 octect) + transaction id (3 octets)
  94. data_[0] = msg_type_;
  95. // store 3-octet transaction-id
  96. data_[1] = (transid_ >> 16) & 0xff;
  97. data_[2] = (transid_ >> 8) & 0xff;
  98. data_[3] = (transid_) & 0xff;
  99. // the rest are options
  100. unsigned short offset = LibDHCP::packOptions6(data_, length,
  101. 4/*offset*/,
  102. options_);
  103. // sanity check
  104. if (offset != length) {
  105. isc_throw(OutOfRange, "Packet build failed: expected size="
  106. << length << ", actual len=" << offset);
  107. }
  108. }
  109. catch (const Exception& e) {
  110. cout << "Packet build failed:" << e.what() << endl;
  111. return (false);
  112. }
  113. // Limited verbosity of this method
  114. // cout << "Packet built, len=" << len() << endl;
  115. return (true);
  116. }
  117. bool
  118. Pkt6::packTCP() {
  119. /// TODO Implement this function.
  120. isc_throw(Unexpected, "DHCPv6 over TCP (bulk leasequery and failover)"
  121. "not implemented yet.");
  122. }
  123. bool
  124. Pkt6::unpack() {
  125. switch (proto_) {
  126. case UDP:
  127. return unpackUDP();
  128. case TCP:
  129. return unpackTCP();
  130. default:
  131. isc_throw(BadValue, "Invalid protocol specified (non-TCP, non-UDP)");
  132. }
  133. return (false); // never happens
  134. }
  135. bool
  136. Pkt6::unpackUDP() {
  137. if (data_len_ < 4) {
  138. std::cout << "DHCPv6 packet truncated. Only " << data_len_
  139. << " bytes. Need at least 4." << std::endl;
  140. return (false);
  141. }
  142. msg_type_ = data_[0];
  143. transid_ = ( (data_[1]) << 16 ) +
  144. ((data_[2]) << 8) + (data_[3]);
  145. transid_ = transid_ & 0xffffff;
  146. unsigned int offset = LibDHCP::unpackOptions6(data_,
  147. data_len_,
  148. 4, //offset
  149. data_len_ - 4,
  150. options_);
  151. if (offset != data_len_) {
  152. cout << "DHCPv6 packet contains trailing garbage. Parsed "
  153. << offset << " bytes, packet is " << data_len_ << " bytes."
  154. << endl;
  155. // just a warning. Ignore trailing garbage and continue
  156. }
  157. return (true);
  158. }
  159. bool
  160. Pkt6::unpackTCP() {
  161. isc_throw(Unexpected, "DHCPv6 over TCP (bulk leasequery and failover) "
  162. "not implemented yet.");
  163. }
  164. std::string
  165. Pkt6::toText() {
  166. stringstream tmp;
  167. tmp << "localAddr=[" << local_addr_.toText() << "]:" << local_port_
  168. << " remoteAddr=[" << remote_addr_.toText()
  169. << "]:" << remote_port_ << endl;
  170. tmp << "msgtype=" << msg_type_ << ", transid=0x" << hex << transid_
  171. << dec << endl;
  172. for (isc::dhcp::Option::Option6Collection::iterator opt=options_.begin();
  173. opt != options_.end();
  174. ++opt) {
  175. tmp << opt->second->toText() << std::endl;
  176. }
  177. return tmp.str();
  178. }
  179. boost::shared_ptr<isc::dhcp::Option>
  180. Pkt6::getOption(unsigned short opt_type) {
  181. isc::dhcp::Option::Option6Collection::const_iterator x = options_.find(opt_type);
  182. if (x!=options_.end()) {
  183. return (*x).second;
  184. }
  185. return boost::shared_ptr<isc::dhcp::Option>(); // NULL
  186. }
  187. void
  188. Pkt6::addOption(boost::shared_ptr<Option> opt) {
  189. options_.insert(pair<int, boost::shared_ptr<Option> >(opt->getType(), opt));
  190. }
  191. bool
  192. Pkt6::delOption(unsigned short type) {
  193. isc::dhcp::Option::Option6Collection::iterator x = options_.find(type);
  194. if (x!=options_.end()) {
  195. options_.erase(x);
  196. return (true); // delete successful
  197. }
  198. return (false); // can't find option to be deleted
  199. }
  200. };