pkt6.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. unsigned short length = len();
  78. if (data_len_ < length) {
  79. cout << "Previous len=" << data_len_ << ", allocating new buffer: len="
  80. << length << endl;
  81. // May throw exception if out of memory. That is rather fatal,
  82. // so we don't catch this
  83. data_ = boost::shared_array<uint8_t>(new uint8_t[length]);
  84. data_len_ = length;
  85. }
  86. data_len_ = length;
  87. try {
  88. // DHCPv6 header: message-type (1 octect) + transaction id (3 octets)
  89. data_[0] = msg_type_;
  90. // store 3-octet transaction-id
  91. data_[1] = (transid_ >> 16) & 0xff;
  92. data_[2] = (transid_ >> 8) & 0xff;
  93. data_[3] = (transid_) & 0xff;
  94. // the rest are options
  95. unsigned short offset = LibDHCP::packOptions6(data_, length,
  96. 4/*offset*/,
  97. options_);
  98. // sanity check
  99. if (offset != length) {
  100. isc_throw(OutOfRange, "Packet build failed: expected size="
  101. << length << ", actual len=" << offset);
  102. }
  103. }
  104. catch (Exception e) {
  105. cout << "Packet build failed:" << e.what() << endl;
  106. return (false);
  107. }
  108. cout << "Packet built, len=" << len() << endl;
  109. return (true);
  110. }
  111. bool
  112. Pkt6::packTCP() {
  113. /// TODO Implement this function.
  114. isc_throw(Unexpected, "DHCPv6 over TCP (bulk leasequery and failover)"
  115. "not implemented yet.");
  116. }
  117. bool
  118. Pkt6::unpack() {
  119. switch (proto_) {
  120. case UDP:
  121. return unpackUDP();
  122. case TCP:
  123. return unpackTCP();
  124. default:
  125. isc_throw(BadValue, "Invalid protocol specified (non-TCP, non-UDP)");
  126. }
  127. return (false); // never happens
  128. }
  129. bool
  130. Pkt6::unpackUDP() {
  131. if (data_len_ < 4) {
  132. std::cout << "DHCPv6 packet truncated. Only " << data_len_
  133. << " bytes. Need at least 4." << std::endl;
  134. return (false);
  135. }
  136. msg_type_ = data_[0];
  137. transid_ = ( (data_[1]) << 16 ) +
  138. ((data_[2]) << 8) + (data_[3]);
  139. transid_ = transid_ & 0xffffff;
  140. unsigned int offset = LibDHCP::unpackOptions6(data_,
  141. data_len_,
  142. 4, //offset
  143. data_len_ - 4,
  144. options_);
  145. if (offset != data_len_) {
  146. cout << "DHCPv6 packet contains trailing garbage. Parsed "
  147. << offset << " bytes, packet is " << data_len_ << " bytes."
  148. << endl;
  149. // just a warning. Ignore trailing garbage and continue
  150. }
  151. return (true);
  152. }
  153. bool
  154. Pkt6::unpackTCP() {
  155. isc_throw(Unexpected, "DHCPv6 over TCP (bulk leasequery and failover) "
  156. "not implemented yet.");
  157. }
  158. std::string
  159. Pkt6::toText() {
  160. stringstream tmp;
  161. tmp << "localAddr=[" << local_addr_.toText() << "]:" << local_port_
  162. << " remoteAddr=[" << remote_addr_.toText()
  163. << "]:" << remote_port_ << endl;
  164. tmp << "msgtype=" << msg_type_ << ", transid=0x" << hex << transid_
  165. << dec << endl;
  166. for (isc::dhcp::Option::Option6Collection::iterator opt=options_.begin();
  167. opt != options_.end();
  168. ++opt) {
  169. tmp << opt->second->toText() << std::endl;
  170. }
  171. return tmp.str();
  172. }
  173. boost::shared_ptr<isc::dhcp::Option>
  174. Pkt6::getOption(unsigned short opt_type) {
  175. isc::dhcp::Option::Option6Collection::const_iterator x = options_.find(opt_type);
  176. if (x!=options_.end()) {
  177. return (*x).second;
  178. }
  179. return boost::shared_ptr<isc::dhcp::Option>(); // NULL
  180. }
  181. void
  182. Pkt6::addOption(boost::shared_ptr<Option> opt) {
  183. options_.insert(pair<int, boost::shared_ptr<Option> >(opt->getType(), opt));
  184. }
  185. bool
  186. Pkt6::delOption(unsigned short type) {
  187. isc::dhcp::Option::Option6Collection::iterator x = options_.find(type);
  188. if (x!=options_.end()) {
  189. options_.erase(x);
  190. return (true); // delete successful
  191. }
  192. return (false); // can't find option to be deleted
  193. }
  194. };