DESERT 3.5.1
Loading...
Searching...
No Matches
packer.h
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
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
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
38#ifndef PACKER_H
39#define PACKER_H
40
41#include "hdr-uwal.h"
42
43#include <math.h>
44#include <packet.h>
45
46#include <bitset>
47#include <cstring>
48#include <sstream>
49#include <string>
50#include <vector>
51
56class packer : public TclObject
57{
58public:
63 packer(bool);
64
69 ~packer();
70
79 int command(int, const char *const *);
80
81 inline size_t
83 {
84 return payload_length;
85 }
86
87 inline size_t
89 {
90 return hdr_length;
91 }
92
93 size_t getPayloadBinLength();
94
95 size_t getHdrBinLength();
96
130 std::string packPayload(Packet *);
131
132 std::string packHdr(Packet *);
133
166 Packet *unpackPayload(Packet *);
167
168 Packet *unpackHdr(Packet *);
169
170 void printMap();
171
172 template <typename T>
173 static T
174 restoreSignedValue(T _header_field, const uint32_t &_num_compressed_bits)
175 {
176 if (_num_compressed_bits < sizeof(_header_field) * 8) {
177 bitset<sizeof(_header_field) * 8> my_bitset(_header_field);
178 if (my_bitset[_num_compressed_bits - 1] == 1) {
179 for (size_t i = _num_compressed_bits;
180 i < sizeof(_header_field) * 8;
181 ++i) {
182 my_bitset.set(i, 1);
183 }
184 }
185 return static_cast<T>(my_bitset.to_ulong());
186 } else {
187 return _header_field;
188 }
189 }
190
198 static std::string hexdump_nice(std::string);
199
200 static std::string hexdump_nice(const char *, size_t);
201
202 static std::string hexdump(std::string);
203
204 static std::string hex_bytes(float);
205 static std::string hex_bytes(double);
206 static std::string hex_bytes(const char &, const uint32_t &);
207 static std::string hex_bytes(const int8_t &, const uint32_t &);
208 static std::string hex_bytes(const int16_t &, const uint32_t &);
209 static std::string hex_bytes(const int32_t &, const uint32_t &);
210 static std::string hex_bytes(const int64_t &, const uint32_t &);
211 static std::string hex_bytes(const uint8_t &, const uint32_t &);
212 static std::string hex_bytes(const uint16_t &, const uint32_t &);
213 static std::string hex_bytes(const uint32_t &, const uint32_t &);
214 static std::string hex_bytes(const uint64_t &, const uint32_t &);
215
216 static std::string hexdump(const char *, size_t);
217
218 static std::string bindump(std::string);
219
220 static std::string bindump(const char *, size_t);
221
222protected:
223 std::vector<size_t>
227 int debug_;
229 virtual void init();
230
231 virtual size_t packMyHdr(Packet *, unsigned char *, size_t);
232
233 virtual size_t unpackMyHdr(unsigned char *, size_t, Packet *);
234
235 virtual void printMyHdrMap();
236
237 virtual void printMyHdrFields(Packet *);
238
239 virtual void printMyHdrField(Packet *, int);
240
241 size_t getMyHdrBinLength();
242
254 size_t get(unsigned char *buffer, size_t offset, void *val, size_t h);
255
267 size_t put(unsigned char *buffer, size_t offset, void *val, size_t h);
268
269private:
270 std::vector<packer *>
282 size_t hdr_length;
287
288 size_t SRC_ID_Bits;
290 size_t PKT_ID_Bits;
294 size_t M_BIT_Bits;
298};
299
300#endif
Class exploited by the Uwal module to map an NS-Miracle packet into a bit stream, and vice-versa.
Definition packer.h:57
std::vector< size_t > n_bits
Vector of elements containing the indication of the number of bits to consider for each header field.
Definition packer.h:224
size_t getPayloadBytesLength()
Definition packer.h:82
virtual size_t packMyHdr(Packet *, unsigned char *, size_t)
Definition packer.cpp:523
static std::string hexdump_nice(std::string)
Function used for log and debug purposes (in order to print binary strings, even if they contain spec...
Definition packer.cpp:697
virtual void printMyHdrMap()
Definition packer.cpp:582
virtual void printMyHdrFields(Packet *)
Definition packer.cpp:608
Packet * unpackHdr(Packet *)
Definition packer.cpp:412
void printMap()
Definition packer.cpp:506
int command(int, const char *const *)
Method to map tcl commands into c++ methods.
Definition packer.cpp:180
size_t PKT_ID_Bits
Bit length of the srcID_ field to be put in the header stream of bits.
Definition packer.h:290
size_t put(unsigned char *buffer, size_t offset, void *val, size_t h)
Method used to map in a certain number of bits, contained in a buffer of chars, a given variable.
Definition packer.cpp:685
std::vector< packer * > activePackers
Vector of elements containing the pointers to the active packers (i.e., the derived classed of packer...
Definition packer.h:271
bool printAllFields
Definition packer.h:286
size_t getHdrBytesLength()
Definition packer.h:88
size_t DUMMY_CONTENT_Bits
Bit length of the Mbit_ field to be put in the header stream of bits.
Definition packer.h:296
~packer()
Class destructor.
Definition packer.cpp:159
static std::string bindump(std::string)
Definition packer.cpp:1113
size_t M_BIT_Bits
Bit length of the frameID_ field to be put in the header stream of bits.
Definition packer.h:294
std::string packPayload(Packet *)
Method to map an NS-Miracle packet into a legal modem payload (i.e., a string of binary characters) w...
Definition packer.cpp:292
virtual void init()
Definition packer.cpp:164
size_t FRAME_OFFSET_Bits
Bit length of the pktID_ field to be put in the header stream of bits.
Definition packer.h:292
static std::string hex_bytes(float)
Definition packer.cpp:752
size_t get(unsigned char *buffer, size_t offset, void *val, size_t h)
Method used to retrieve a given variable from a certain number of bits contained in a buffer of chars...
Definition packer.cpp:673
size_t getMyHdrBinLength()
Definition packer.cpp:661
size_t getHdrBinLength()
Definition packer.cpp:228
size_t hdr_length
The minimum number of elements that a buffer of char must have in order to store the header fields of...
Definition packer.h:282
static T restoreSignedValue(T _header_field, const uint32_t &_num_compressed_bits)
Definition packer.h:174
static std::string hexdump(std::string)
Definition packer.cpp:727
int debug_
Flag to enable debug messages.
Definition packer.h:227
size_t payload_length
The minimum number of elements that a buffer of char must have in order to store the fields to be cod...
Definition packer.h:277
Packet * unpackPayload(Packet *)
Method to de-map a legal modem payload (i.e., a string of binary characters) into an NS-Miracle packe...
Definition packer.cpp:433
virtual void printMyHdrField(Packet *, int)
Definition packer.cpp:617
size_t getPayloadBinLength()
Definition packer.cpp:216
std::string packHdr(Packet *)
Definition packer.cpp:234
virtual size_t unpackMyHdr(unsigned char *, size_t, Packet *)
Definition packer.cpp:548
size_t SRC_ID_Bits
Definition packer.h:288
Header of the class providing the Uwal header's description.