pkt_transform.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <iostream>
  15. #include <exceptions/exceptions.h>
  16. #include <dhcp/option.h>
  17. #include <dhcp/libdhcp++.h>
  18. #include <dhcp/dhcp6.h>
  19. #include "pkt_transform.h"
  20. #include "localized_option.h"
  21. using namespace std;
  22. using namespace isc;
  23. using namespace dhcp;
  24. namespace isc {
  25. namespace perfdhcp {
  26. bool
  27. PktTransform::pack(const Option::Universe universe,
  28. const OptionBuffer& in_buffer,
  29. const Option::OptionCollection& options,
  30. const size_t transid_offset,
  31. const uint32_t transid,
  32. util::OutputBuffer& out_buffer) {
  33. // Always override the packet if function is called.
  34. out_buffer.clear();
  35. // Write whole buffer to output buffer.
  36. out_buffer.writeData(&in_buffer[0], in_buffer.size());
  37. uint8_t transid_len = (universe == Option::V6) ? 3 : 4;
  38. if ((transid_offset + transid_len + 1 > in_buffer.size()) ||
  39. (transid_offset == 0)) {
  40. cout << "Failed to build packet: provided transaction id offset: "
  41. << transid_offset << " is out of bounds (expected 1.."
  42. << in_buffer.size()-1 << ")." << endl;
  43. return (false);
  44. }
  45. // Seek to the transaction id position in output buffer.
  46. out_buffer.clear();
  47. out_buffer.skip(transid_offset);
  48. try {
  49. if (universe == Option::V6) {
  50. out_buffer.writeUint8(transid >> 24 & 0xFF);
  51. }
  52. out_buffer.writeUint8(transid >> 16 & 0xFF);
  53. out_buffer.writeUint8(transid >> 8 & 0xFF);
  54. out_buffer.writeUint8(transid & 0xFF);
  55. // Buffer pointer is at the end of transaction id.
  56. // We have to seek to the end of buffer so as data don't
  57. // get truncated.
  58. out_buffer.skip(in_buffer.size() - out_buffer.getLength());
  59. // We already have packet template stored in output buffer
  60. // but still some options have to be updated if client
  61. // specified them along with their offsets in the buffer.
  62. PktTransform::packOptions(in_buffer, out_buffer, options);
  63. } catch (const isc::BadValue& e) {
  64. cout << "Building packet failed: " << e.what() << endl;
  65. return (false);
  66. }
  67. return (true);
  68. }
  69. bool
  70. PktTransform::unpack(const Option::Universe universe,
  71. const OptionBuffer& in_buffer,
  72. const Option::OptionCollection& options,
  73. const size_t transid_offset,
  74. uint32_t& transid) {
  75. uint8_t transid_len = (universe == Option::V6) ? 3 : 4;
  76. // Validate transaction id offset.
  77. if ((transid_offset + transid_len + 1 > in_buffer.size()) ||
  78. (transid_offset == 0)) {
  79. cout << "Failed to parse packet: provided transaction id offset: "
  80. << transid_offset << " is out of bounds (expected 1.."
  81. << in_buffer.size()-1 << ")." << endl;
  82. return (false);
  83. }
  84. if (universe == Option::V6) {
  85. transid = ((in_buffer[transid_offset] << 16) +
  86. (in_buffer[transid_offset + 1] << 8) +
  87. (in_buffer[transid_offset + 2]))
  88. & 0xFFFFFF;
  89. } else {
  90. transid = ((in_buffer[transid_offset] << 24) +
  91. (in_buffer[transid_offset +1 ] << 16) +
  92. (in_buffer[transid_offset + 2] << 8) +
  93. (in_buffer[transid_offset + 3]));
  94. }
  95. try {
  96. PktTransform::unpackOptions(universe, in_buffer, options);
  97. } catch (const isc::BadValue& e) {
  98. cout << "Packet parsing failed: " << e.what() << endl;
  99. return (false);
  100. }
  101. return (true);
  102. }
  103. void
  104. PktTransform::packOptions(const OptionBuffer& in_buffer,
  105. util::OutputBuffer& out_buffer,
  106. const Option::OptionCollection& options) {
  107. try {
  108. // If there are any options on the list, we will use provided
  109. // options offsets to override them in the output buffer
  110. // with new contents.
  111. for (Option::OptionCollection::const_iterator it = options.begin();
  112. it != options.end(); ++it) {
  113. // Get options with their position (offset).
  114. boost::shared_ptr<LocalizedOption> option =
  115. boost::dynamic_pointer_cast<LocalizedOption>(it->second);
  116. uint32_t offset = option->getOffset();
  117. if ((offset == 0) ||
  118. (offset + option->len() > in_buffer.size())) {
  119. isc_throw(isc::BadValue,
  120. "option offset for option: " << option->getType()
  121. << " is out of bounds (expected 1.."
  122. << in_buffer.size() - option->len() << ")");
  123. }
  124. out_buffer.clear();
  125. out_buffer.skip(offset);
  126. // Replace existing option with new value.
  127. option->pack(out_buffer);
  128. }
  129. // Seek to the end of the buffer to make sure its size is correct.
  130. out_buffer.skip(in_buffer.size() - out_buffer.getLength());
  131. }
  132. catch (const Exception&) {
  133. isc_throw(isc::BadValue, "failed to pack options into buffer.");
  134. }
  135. }
  136. void
  137. PktTransform::unpackOptions(const Option::Universe universe,
  138. const OptionBuffer& in_buffer,
  139. const Option::OptionCollection& options) {
  140. for (Option::OptionCollection::const_iterator it = options.begin();
  141. it != options.end(); ++it) {
  142. boost::shared_ptr<LocalizedOption> option =
  143. boost::dynamic_pointer_cast<LocalizedOption>(it->second);
  144. size_t opt_pos = option->getOffset();
  145. if (opt_pos == 0) {
  146. isc_throw(isc::BadValue, "failed to unpack packet from raw buffer "
  147. "(Option position not specified)");
  148. } else if (opt_pos + 4 > in_buffer.size()) {
  149. isc_throw(isc::BadValue,
  150. "failed to unpack options from from raw buffer "
  151. "(Option position out of bounds)");
  152. }
  153. size_t offset = opt_pos;
  154. size_t offset_step = 1;
  155. uint16_t opt_type = 0;
  156. if (universe == Option::V6) {
  157. offset_step = 2;
  158. // For DHCPv6 option type is in first two octets.
  159. opt_type = in_buffer[offset] * 256 + in_buffer[offset + 1];
  160. } else {
  161. // For DHCPv4 option type is in first octet.
  162. opt_type = in_buffer[offset];
  163. }
  164. // Check if we got expected option type.
  165. if (opt_type != option->getType()) {
  166. isc_throw(isc::BadValue,
  167. "failed to unpack option from raw buffer "
  168. "(option type mismatch)");
  169. }
  170. // Get option length which is supposed to be after option type.
  171. offset += offset_step;
  172. uint16_t opt_len = in_buffer[offset] * 256 + in_buffer[offset + 1];
  173. if (universe == Option::V6) {
  174. opt_len = in_buffer[offset] * 256 + in_buffer[offset + 1];
  175. } else {
  176. opt_len = in_buffer[offset];
  177. }
  178. // Check if packet is not truncated.
  179. if (offset + opt_len > in_buffer.size()) {
  180. isc_throw(isc::BadValue,
  181. "failed to unpack option from raw buffer "
  182. "(option truncated)");
  183. }
  184. // Seek to actual option data and replace it.
  185. offset += offset_step;
  186. option->setData(in_buffer.begin() + offset,
  187. in_buffer.begin() + offset + opt_len);
  188. }
  189. }
  190. } // namespace perfdhcp
  191. } // namespace isc