libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
rawpdu.h
1 /*
2  * Copyright (c) 2017, 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 <tins/pdu.h>
36 #include <tins/macros.h>
37 #include <tins/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 
106  RawPDU(const payload_type & data)
107  : payload_(data) { }
108 
109  #if TINS_IS_CXX11
110 
118  : payload_(move(data)) { }
119  #endif // TINS_IS_CXX11
120 
126  RawPDU(const std::string& data);
127 
132  void payload(const payload_type& pload);
133 
139  template<typename ForwardIterator>
140  void payload(ForwardIterator start, ForwardIterator end) {
141  payload_.assign(start, end);
142  }
143 
148  const payload_type& payload() const {
149  return payload_;
150  }
151 
157  return payload_;
158  }
159 
167  uint32_t header_size() const;
168 
174  uint32_t payload_size() const {
175  return static_cast<uint32_t>(payload_.size());
176  }
177 
188  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
189 
194  PDUType pdu_type() const {
195  return pdu_flag;
196  }
197 
202  template<typename T>
203  T to() const {
204  return T(&payload_[0], static_cast<uint32_t>(payload_.size()));
205  }
206 
210  RawPDU* clone() const {
211  return new RawPDU(*this);
212  }
213 private:
214  void write_serialization(uint8_t* buffer, uint32_t total_sz);
215 
216  payload_type payload_;
217 };
218 
219 } // Tins
220 
221 #endif // TINS_RAWPDU_H
RawPDU(const payload_type &data)
Creates an instance of RawPDU from a payload_type.
Definition: rawpdu.h:106
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
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:203
uint32_t payload_size() const
Returns the payload size.
Definition: rawpdu.h:174
PDUType pdu_type() const
Getter for the PDU's type.
Definition: rawpdu.h:194
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:148
RawPDU * clone() const
Definition: rawpdu.h:210
Definition: rawpdu.h:64
RawPDU(payload_type &&data)
Creates an instance of RawPDU from a payload_type.
Definition: rawpdu.h:117
void payload(ForwardIterator start, ForwardIterator end)
Setter for the payload field.
Definition: rawpdu.h:140
payload_type & payload()
Non-const getter for the payload.
Definition: rawpdu.h:156
Base class for protocol data units.
Definition: pdu.h:107