DESERT 3.5.1
Loading...
Searching...
No Matches
packer-uwudp.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2013 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
36#include "packer-uwudp.h"
37
41static class PackerUWUDPClass : public TclClass {
42public:
43
44 PackerUWUDPClass() : TclClass("UW/UDP/Packer") {
45 }
46
47 TclObject* create(int, const char*const*) {
48 return (new packerUWUDP());
49 }
51
53 SPort_Bits = 8 * sizeof (u_int16_t);
54 DPort_Bits = 8 * sizeof (u_int16_t);
55
56 bind("SPort_Bits", (int*) &SPort_Bits);
57 bind("DPort_Bits", (int*) &DPort_Bits);
58
59 if (debug_)
60 cout << "Initialization (from constructor) of n_bits for the UWUDP packer " << endl;
61
62 n_bits.clear();
63
64 n_bits.push_back(SPort_Bits);
65 n_bits.push_back(DPort_Bits);
66}
67
71
73
74 if (debug_)
75 cout << "Re-initialization of n_bits for the UWUDP packer " << endl;
76
77 n_bits.clear();
78
79 n_bits.push_back(SPort_Bits);
80 n_bits.push_back(DPort_Bits);
81}
82
83size_t packerUWUDP::packMyHdr(Packet* p, unsigned char* buf, size_t offset) {
84
85 // Pointer to the UWUDP packet header
86 hdr_uwudp* hudp = HDR_UWUDP(p);
87
88 int field_idx = 0;
89
90 offset += put(buf, offset, &(hudp->sport_), n_bits[field_idx++]);
91 offset += put(buf, offset, &(hudp->dport_), n_bits[field_idx++]);
92
93 if (debug_) {
94 printf("\033[1;37;44m TX UDP packer hdr \033[0m \n");
96 }
97
98 return offset;
99}
100
101size_t packerUWUDP::unpackMyHdr(unsigned char* buf, size_t offset, Packet* p) {
102
103 // Pointer to the UWUDP packet header
104 hdr_uwudp* hudp = HDR_UWUDP(p);
105
106 int field_idx = 0;
107
108 memset(&(hudp->sport()), 0, sizeof ( hudp->sport_));
109 offset += get(buf, offset, &(hudp->sport_), n_bits[field_idx++]);
110
111 memset(&(hudp->dport()), 0, sizeof ( hudp->dport_));
112 offset += get(buf, offset, &(hudp->dport_), n_bits[field_idx++]);
113
114 if (debug_) {
115 printf("\033[1;37;44m RX UDP packer hdr \033[0m \n");
117 }
118
119 return offset;
120}
121
123 std::cout << "\033[1;37;44m" << " Packer Name " << "\033[0m" << " UWUDP" << std::endl;
124 std::cout << "\033[1;37;44m 1st field " << "\033[0m" << " sport: " << SPort_Bits << " bits" << std::endl;
125 std::cout << "\033[1;37;44m 2nd field " << "\033[0m" << " dport: " << DPort_Bits << " bits" << std::endl;
126}
127
129 hdr_uwudp* hudp = HDR_UWUDP(p);
130
131 if (n_bits[0] != 0) {
132 std::cout << "\033[1;37;44m sport:\033[0m " << static_cast<uint32_t> (hudp->sport()) << " " << hex_bytes(hudp->sport(), n_bits[0]) << std::endl;
133 }
134 if (n_bits[1] != 0) {
135 std::cout << "\033[1;37;44m dport:\033[0m " << static_cast<uint32_t> (hudp->dport()) << " " << hex_bytes(hudp->dport(), n_bits[1]) << std::endl;
136 }
137}
138
Class to create the Otcl shadow object for an object of the class packer.
TclObject * create(int, const char *const *)
Class exploited by the Uwal module to map an NS-Miracle packet into a bit stream, and vice-versa.
packerUWUDP()
Class constructor.
void init()
Init the Packer.
size_t unpackMyHdr(unsigned char *, size_t, Packet *)
Method responsible to take the informations from the received buffer and store it into the headers of...
size_t DPort_Bits
Bit length of the sport_ field to be put in the header stream of bits.
void printMyHdrMap()
Method used for debug purposes.
void printMyHdrFields(Packet *)
Method used for debug purposes.
size_t SPort_Bits
~packerUWUDP()
Class destructor.
size_t packMyHdr(Packet *, unsigned char *, size_t)
Method to transform the headers of Uwpolling into a stream of bits.
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 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
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
int debug_
Flag to enable debug messages.
Definition packer.h:227
PackerUWUDPClass class_module_packerUWUDP
hdr_uwudp describes UWUDP packets.
uint8_t dport_
Destination port number.
uint8_t & sport()
Reference to the sport_ variable.
uint8_t sport_
Source port number.
uint8_t & dport()
Reference to the dport_ variable.
#define HDR_UWUDP(P)