libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
rawpdu.h
1 /*
2  * Copyright (c) 2016, Matias Fontanini
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef TINS_RAWPDU_H
31 #define TINS_RAWPDU_H
32 
33 #include <vector>
34 #include <string>
35 #include "pdu.h"
36 #include "macros.h"
37 #include "cxxstd.h"
38 
39 namespace Tins {
40 
64 class TINS_API RawPDU : public PDU {
65 public:
69  typedef std::vector<uint8_t> payload_type;
70 
74  static const PDU::PDUType pdu_flag = PDU::RAW;
75 
84  RawPDU(const uint8_t* pload, uint32_t size);
85 
95  template<typename ForwardIterator>
96  RawPDU(ForwardIterator start, ForwardIterator end)
97  : payload_(start, end) { }
98 
99  #if TINS_IS_CXX11
100 
108  : payload_(move(data)) { }
109  #endif // TINS_IS_CXX11
110 
116  RawPDU(const std::string& data);
117 
122  void payload(const payload_type& pload);
123 
129  template<typename ForwardIterator>
130  void payload(ForwardIterator start, ForwardIterator end) {
131  payload_.assign(start, end);
132  }
133 
138  const payload_type& payload() const {
139  return payload_;
140  }
141 
147  return payload_;
148  }
149 
157  uint32_t header_size() const;
158 
164  uint32_t payload_size() const {
165  return static_cast<uint32_t>(payload_.size());
166  }
167 
178  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
179 
184  PDUType pdu_type() const {
185  return pdu_flag;
186  }
187 
192  template<typename T>
193  T to() const {
194  return T(&payload_[0], static_cast<uint32_t>(payload_.size()));
195  }
196 
200  RawPDU* clone() const {
201  return new RawPDU(*this);
202  }
203 private:
204  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent);
205 
206  payload_type payload_;
207 };
208 
209 } // Tins
210 
211 #endif // TINS_RAWPDU_H
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
std::vector< uint8_t > payload_type
Definition: rawpdu.h:69
T to() const
Constructs the given PDU type from the raw data stored in this RawPDU.
Definition: rawpdu.h:193
uint32_t payload_size() const
Returns the payload size.
Definition: rawpdu.h:164
PDUType pdu_type() const
Getter for the PDU's type.
Definition: rawpdu.h:184
RawPDU(ForwardIterator start, ForwardIterator end)
Constructs a RawPDU from an iterator range.
Definition: rawpdu.h:96
const payload_type & payload() const
Const getter for the payload.
Definition: rawpdu.h:138
RawPDU * clone() const
Definition: rawpdu.h:200
Definition: rawpdu.h:64
RawPDU(payload_type &&data)
Creates an instance of RawPDU from a payload_type.
Definition: rawpdu.h:107
void payload(ForwardIterator start, ForwardIterator end)
Setter for the payload field.
Definition: rawpdu.h:130
payload_type & payload()
Non-const getter for the payload.
Definition: rawpdu.h:146
Base class for protocol data units.
Definition: pdu.h:108