30 const size_t transid_offset,
31 const uint32_t transid,
37 out_buffer.
writeData(&in_buffer[0], in_buffer.size());
39 uint8_t transid_len = (universe == Option::V6) ? 3 : 4;
41 if ((transid_offset + transid_len >= in_buffer.size()) ||
42 (transid_offset == 0)) {
43 cout <<
"Failed to build packet: provided transaction id offset: " 44 << transid_offset <<
" is out of bounds (expected 1.." 45 << in_buffer.size()-1 <<
")." << endl;
50 size_t offset_ptr = transid_offset;
51 if (universe == Option::V4) {
52 out_buffer.
writeUint8At(transid >> 24 & 0xFF, offset_ptr++);
54 out_buffer.
writeUint8At(transid >> 16 & 0xFF, offset_ptr++);
55 out_buffer.
writeUint8At(transid >> 8 & 0xFF, offset_ptr++);
61 PktTransform::packOptions(in_buffer, options, out_buffer);
63 cout <<
"Building packet failed: " << e.
what() << endl;
73 const size_t transid_offset,
76 uint8_t transid_len = (universe == Option::V6) ? 3 : 4;
79 if ((transid_offset + transid_len + 1 > in_buffer.size()) ||
80 (transid_offset == 0)) {
81 cout <<
"Failed to parse packet: provided transaction id offset: " 82 << transid_offset <<
" is out of bounds (expected 1.." 83 << in_buffer.size()-1 <<
")." << endl;
92 for (
int i = 0; i < transid_len; ++i, ++it) {
95 transid += *it << (transid_len - i - 1) * 8;
99 PktTransform::unpackOptions(in_buffer, options);
101 cout <<
"Packet parsing failed: " << e.
what() << endl;
109 PktTransform::packOptions(
const OptionBuffer& in_buffer,
116 for (OptionCollection::const_iterator it = options.begin();
117 it != options.end(); ++it) {
119 boost::shared_ptr<LocalizedOption> option =
120 boost::dynamic_pointer_cast<LocalizedOption>(it->second);
121 if (option == NULL) {
124 uint32_t offset = option->getOffset();
126 (offset + option->len() > in_buffer.size())) {
128 "option offset for option: " << option->getType()
129 <<
" is out of bounds (expected 1.." 130 << in_buffer.size() - option->len() <<
")");
140 const uint8_t *buf_data =
141 static_cast<const uint8_t*>(buf.getData());
142 for (
size_t i = 0; i < buf.getLength(); ++i) {
153 PktTransform::unpackOptions(
const OptionBuffer& in_buffer,
155 for (OptionCollection::const_iterator it = options.begin();
156 it != options.end(); ++it) {
158 boost::shared_ptr<LocalizedOption> option =
159 boost::dynamic_pointer_cast<LocalizedOption>(it->second);
160 if (option == NULL) {
163 size_t opt_pos = option->getOffset();
166 "(Option position not specified)");
167 }
else if (opt_pos + option->getHeaderLen() > in_buffer.size()) {
169 "failed to unpack options from from raw buffer " 170 "(Option position out of bounds)");
173 size_t offset = opt_pos;
174 size_t offset_step = 1;
175 uint16_t opt_type = 0;
176 if (option->getUniverse() == Option::V6) {
179 opt_type = in_buffer[offset] * 256 + in_buffer[offset + 1];
182 opt_type = in_buffer[offset];
185 if (opt_type != option->getType()) {
187 "failed to unpack option from raw buffer " 188 "(option type mismatch)");
192 offset += offset_step;
193 const uint16_t opt_len =
194 (option->getUniverse() == Option::V6) ?
195 in_buffer[offset] * 256 + in_buffer[offset + 1] :
199 if (offset + option->getHeaderLen() + opt_len > in_buffer.size()) {
201 "failed to unpack option from raw buffer " 202 "(option truncated)");
206 offset += offset_step;
207 option->setData(in_buffer.begin() + offset,
208 in_buffer.begin() + offset + opt_len);
214 dhcp::OptionBuffer::iterator first,
215 dhcp::OptionBuffer::iterator last) {
216 memcpy(&in_buffer[dest_pos], &(*first), std::distance(first, last));
Universe
defines option universe DHCPv4 or DHCPv6
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void clear()
Clear buffer content.
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
void writeUint8At(uint8_t data, size_t pos)
Write an unsigned 8-bit integer into the buffer.