32 const size_t transid_offset,
33 const uint32_t transid,
38 if (in_buffer.empty()) {
42 out_buffer.
writeData(&in_buffer[0], in_buffer.size());
44 uint8_t transid_len = (universe ==
Option::V6) ? 3 : 4;
46 if ((transid_offset + transid_len >= in_buffer.size()) ||
47 (transid_offset == 0)) {
48 cout <<
"Failed to build packet: provided transaction id offset: "
49 << transid_offset <<
" is out of bounds (expected 1.."
50 << in_buffer.size()-1 <<
")." << endl;
55 size_t offset_ptr = transid_offset;
57 out_buffer.
writeUint8At(transid >> 24 & 0xFF, offset_ptr++);
59 out_buffer.
writeUint8At(transid >> 16 & 0xFF, offset_ptr++);
60 out_buffer.
writeUint8At(transid >> 8 & 0xFF, offset_ptr++);
66 PktTransform::packOptions(in_buffer, options, out_buffer);
68 cout <<
"Building packet failed: " << e.
what() << endl;
78 const size_t transid_offset,
81 uint8_t transid_len = (universe ==
Option::V6) ? 3 : 4;
84 if ((transid_offset + transid_len + 1 > in_buffer.size()) ||
85 (transid_offset == 0)) {
86 cout <<
"Failed to parse packet: provided transaction id offset: "
87 << transid_offset <<
" is out of bounds (expected 1.."
88 << in_buffer.size()-1 <<
")." << endl;
97 for (
int i = 0; i < transid_len; ++i, ++it) {
100 transid += *it << (transid_len - i - 1) * 8;
104 PktTransform::unpackOptions(in_buffer, options);
107 cout <<
"Packet parsing failed: " << e.
what() << endl;
122 for (
auto const& it : options) {
124 boost::shared_ptr<LocalizedOption> option =
125 boost::dynamic_pointer_cast<LocalizedOption>(it.second);
126 if (option == NULL) {
129 uint32_t offset = option->getOffset();
131 (offset + option->len() > in_buffer.size())) {
133 "option offset for option: " << option->getType()
134 <<
" is out of bounds (expected 1.."
135 << in_buffer.size() - option->len() <<
")");
145 const uint8_t *buf_data = buf.getData();
146 for (
size_t i = 0; i < buf.getLength(); ++i) {
150 }
catch (
const Exception&) {
151 isc_throw(isc::BadValue,
"failed to pack options into buffer.");
156PktTransform::unpackOptions(
const OptionBuffer& in_buffer,
158 for (
auto const& it : options) {
160 boost::shared_ptr<LocalizedOption> option =
161 boost::dynamic_pointer_cast<LocalizedOption>(it.second);
162 if (option == NULL) {
163 isc_throw(isc::BadValue,
"option is null");
165 size_t opt_pos = option->getOffset();
167 isc_throw(isc::BadValue,
"failed to unpack packet from raw buffer "
168 "(Option position not specified)");
169 }
else if (opt_pos + option->getHeaderLen() > in_buffer.size()) {
171 "failed to unpack options from from raw buffer "
172 "(Option position out of bounds)");
175 size_t offset = opt_pos;
176 size_t offset_step = 1;
177 uint16_t opt_type = 0;
181 opt_type = in_buffer[offset] * 256 + in_buffer[offset + 1];
184 opt_type = in_buffer[offset];
187 if (opt_type != option->getType()) {
189 "failed to unpack option from raw buffer "
190 "(option type mismatch)");
194 offset += offset_step;
195 const uint16_t opt_len =
197 in_buffer[offset] * 256 + in_buffer[offset + 1] :
201 if (offset + option->getHeaderLen() + opt_len > in_buffer.size()) {
203 "failed to unpack option from raw buffer "
204 "(option truncated)");
208 offset += offset_step;
209 option->setData(in_buffer.begin() + offset,
210 in_buffer.begin() + offset + opt_len);
216 dhcp::OptionBuffer::iterator first,
217 dhcp::OptionBuffer::iterator last) {
218 auto length = std::distance(first, last);
222 if (in_buffer.size() < dest_pos + length) {
224 << length <<
", size=" << in_buffer.size());
226 memcpy(&in_buffer[dest_pos], &(*first), length);
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
A generic exception that is thrown when an unexpected error condition occurs.
Universe
defines option universe DHCPv4 or DHCPv6
static int malformed_pkts_
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
void clear()
Clear buffer content.
void writeUint8At(uint8_t data, size_t position)
Write an unsigned 8-bit integer into the buffer.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Defines the logger used by the top-level component of kea-lfc.