libdhcp++.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <boost/shared_array.hpp>
  15. #include <boost/shared_ptr.hpp>
  16. #include <util/buffer.h>
  17. #include <exceptions/exceptions.h>
  18. #include <dhcp/libdhcp++.h>
  19. #include "config.h"
  20. #include <dhcp/dhcp4.h>
  21. #include <dhcp/dhcp6.h>
  22. #include <dhcp/option.h>
  23. #include <dhcp/option6_ia.h>
  24. #include <dhcp/option6_iaaddr.h>
  25. using namespace std;
  26. using namespace isc::dhcp;
  27. using namespace isc::util;
  28. // static array with factories for options
  29. std::map<unsigned short, Option::Factory*> LibDHCP::v4factories_;
  30. // static array with factories for options
  31. std::map<unsigned short, Option::Factory*> LibDHCP::v6factories_;
  32. OptionPtr
  33. LibDHCP::optionFactory(Option::Universe u,
  34. uint16_t type,
  35. const OptionBuffer& buf) {
  36. FactoryMap::iterator it;
  37. if (u == Option::V4) {
  38. it = v4factories_.find(type);
  39. if (it == v4factories_.end()) {
  40. isc_throw(BadValue, "factory function not registered "
  41. "for DHCP v4 option type " << type);
  42. }
  43. } else if (u == Option::V6) {
  44. it = v6factories_.find(type);
  45. if (it == v6factories_.end()) {
  46. isc_throw(BadValue, "factory function not registered "
  47. "for DHCPv6 option type " << type);
  48. }
  49. } else {
  50. isc_throw(BadValue, "invalid universe specified (expected "
  51. "Option::V4 or Option::V6");
  52. }
  53. return (it->second(u, type, buf));
  54. }
  55. size_t LibDHCP::unpackOptions6(const OptionBuffer& buf,
  56. isc::dhcp::Option::OptionCollection& options) {
  57. size_t offset = 0;
  58. size_t end = buf.size();
  59. while (offset +4 <= end) {
  60. uint16_t opt_type = buf[offset] * 256 + buf[offset + 1];
  61. offset += 2;
  62. uint16_t opt_len = buf[offset] * 256 + buf[offset + 1];
  63. offset += 2;
  64. if (offset + opt_len > end) {
  65. // @todo: consider throwing exception here.
  66. return (offset);
  67. }
  68. OptionPtr opt;
  69. switch (opt_type) {
  70. case D6O_IA_NA:
  71. case D6O_IA_PD:
  72. opt = OptionPtr(new Option6IA(opt_type,
  73. buf.begin() + offset,
  74. buf.begin() + offset + opt_len));
  75. break;
  76. case D6O_IAADDR:
  77. opt = OptionPtr(new Option6IAAddr(opt_type,
  78. buf.begin() + offset,
  79. buf.begin() + offset + opt_len));
  80. break;
  81. default:
  82. opt = OptionPtr(new Option(Option::V6,
  83. opt_type,
  84. buf.begin() + offset,
  85. buf.begin() + offset + opt_len));
  86. break;
  87. }
  88. // add option to options
  89. options.insert(std::make_pair(opt_type, opt));
  90. offset += opt_len;
  91. }
  92. return (offset);
  93. }
  94. size_t LibDHCP::unpackOptions4(const OptionBuffer& buf,
  95. isc::dhcp::Option::OptionCollection& options) {
  96. size_t offset = 0;
  97. // 2 byte - header of DHCPv4 option
  98. while (offset + 1 <= buf.size()) {
  99. uint8_t opt_type = buf[offset++];
  100. // DHO_END is a special, one octet long option
  101. if (opt_type == DHO_END)
  102. return (offset); // just return. Don't need to add DHO_END option
  103. // DHO_PAD is just a padding after DHO_END. Let's continue parsing
  104. // in case we receive a message without DHO_END.
  105. if (opt_type == DHO_PAD)
  106. continue;
  107. if (offset + 1 >= buf.size()) {
  108. isc_throw(OutOfRange, "Attempt to parse truncated option "
  109. << opt_type);
  110. }
  111. uint8_t opt_len = buf[offset++];
  112. if (offset + opt_len > buf.size()) {
  113. isc_throw(OutOfRange, "Option parse failed. Tried to parse "
  114. << offset + opt_len << " bytes from " << buf.size()
  115. << "-byte long buffer.");
  116. }
  117. OptionPtr opt;
  118. switch(opt_type) {
  119. default:
  120. opt = OptionPtr(new Option(Option::V4, opt_type,
  121. buf.begin()+offset,
  122. buf.begin()+offset+opt_len));
  123. }
  124. options.insert(std::make_pair(opt_type, opt));
  125. offset += opt_len;
  126. }
  127. return (offset);
  128. }
  129. void LibDHCP::packOptions6(isc::util::OutputBuffer &buf,
  130. const isc::dhcp::Option::OptionCollection& options) {
  131. for (Option::OptionCollection::const_iterator it = options.begin();
  132. it != options.end(); ++it) {
  133. it->second->pack(buf);
  134. }
  135. }
  136. void
  137. LibDHCP::packOptions(isc::util::OutputBuffer& buf,
  138. const Option::OptionCollection& options) {
  139. for (Option::OptionCollection::const_iterator it = options.begin();
  140. it != options.end(); ++it) {
  141. it->second->pack4(buf);
  142. }
  143. }
  144. void LibDHCP::OptionFactoryRegister(Option::Universe u,
  145. uint16_t opt_type,
  146. Option::Factory* factory) {
  147. switch (u) {
  148. case Option::V6: {
  149. if (v6factories_.find(opt_type) != v6factories_.end()) {
  150. isc_throw(BadValue, "There is already DHCPv6 factory registered "
  151. << "for option type " << opt_type);
  152. }
  153. v6factories_[opt_type]=factory;
  154. return;
  155. }
  156. case Option::V4:
  157. {
  158. // Option 0 is special (a one octet-long, equal 0) PAD option. It is never
  159. // instantiated as an Option object, but rather consumed during packet parsing.
  160. if (opt_type == 0) {
  161. isc_throw(BadValue, "Cannot redefine PAD option (code=0)");
  162. }
  163. // Option 255 is never instantiated as an option object. It is special
  164. // (a one-octet equal 255) option that is added at the end of all options
  165. // during packet assembly. It is also silently consumed during packet parsing.
  166. if (opt_type > 254) {
  167. isc_throw(BadValue, "Too big option type for DHCPv4, only 0-254 allowed.");
  168. }
  169. if (v4factories_.find(opt_type)!=v4factories_.end()) {
  170. isc_throw(BadValue, "There is already DHCPv4 factory registered "
  171. << "for option type " << opt_type);
  172. }
  173. v4factories_[opt_type]=factory;
  174. return;
  175. }
  176. default:
  177. isc_throw(BadValue, "Invalid universe type specified.");
  178. }
  179. return;
  180. }