DESERT 3.5.1
Loading...
Searching...
No Matches
packer-mac.cpp
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#include "packer-mac.h"
39
43static class PackerMACClass : public TclClass
44{
45public:
47 : TclClass("NS2/MAC/Packer")
48 {
49 }
50
51 TclObject *
52 create(int, const char *const *)
53 {
54 return (new packerMAC());
55 }
57
59 : packer(false)
60{
61
62 Ftype_Bits = 0;
63 SRC_Bits = sizeof(int);
64 DST_Bits = sizeof(int);
65 Htype_Bits = 0;
66 TXtime_Bits = 0;
67 SStime_Bits = 0;
68 Padding_Bits = 0;
69
70 bind("Ftype_Bits", (int *) &Ftype_Bits);
71 bind("SRC_Bits", (int *) &SRC_Bits);
72 bind("DST_Bits", (int *) &DST_Bits);
73 bind("Htype_Bits", (int *) &Htype_Bits);
74 bind("TXtime_Bits", (int *) &TXtime_Bits);
75 bind("SStime_Bits", (int *) &SStime_Bits);
76 bind("Padding_Bits", (int *) &Padding_Bits);
77
78 if (debug_)
79 cout << "Initialization (from constructor) of n_bits for the UWMAC "
80 "packer "
81 << endl;
82
83 n_bits.clear();
84
85 n_bits.push_back(Ftype_Bits);
86 n_bits.push_back(SRC_Bits);
87 n_bits.push_back(DST_Bits);
88 n_bits.push_back(Htype_Bits);
89 n_bits.push_back(TXtime_Bits);
90 n_bits.push_back(SStime_Bits);
91 n_bits.push_back(Padding_Bits);
92}
93
97
98void
100{
101
102 if (debug_)
103 cout << "Re-initialization of n_bits for the UWMAC packer " << endl;
104
105 n_bits.clear();
106
107 n_bits.push_back(Ftype_Bits);
108 n_bits.push_back(SRC_Bits);
109 n_bits.push_back(DST_Bits);
110 n_bits.push_back(Htype_Bits);
111 n_bits.push_back(TXtime_Bits);
112 n_bits.push_back(SStime_Bits);
113 n_bits.push_back(Padding_Bits);
114}
115
116size_t
117packerMAC::packMyHdr(Packet *p, unsigned char *buf, size_t offset)
118{
119
120 // Pointer to the MAC packet header
121 hdr_mac *hmac = HDR_MAC(p);
122
123 int field_idx = 0;
124
125 offset += put(buf, offset, &(hmac->ftype_), n_bits[field_idx++]);
126 offset += put(buf, offset, &(hmac->macSA_), n_bits[field_idx++]);
127 offset += put(buf, offset, &(hmac->macDA_), n_bits[field_idx++]);
128 offset += put(buf, offset, &(hmac->hdr_type_), n_bits[field_idx++]);
129 offset += put(buf, offset, &(hmac->txtime_), n_bits[field_idx++]);
130 offset += put(buf, offset, &(hmac->sstime_), n_bits[field_idx++]);
131 offset += put(buf, offset, &(hmac->padding_), n_bits[field_idx++]);
132
133 if (debug_) {
134 printf("\033[0;45;30m TX MAC packer hdr \033[0m \n");
136 }
137 return offset;
138}
139
140size_t
141packerMAC::unpackMyHdr(unsigned char *buf, size_t offset, Packet *p)
142{
143
144 // Pointer to the MAC packet header
145 hdr_mac *hmac = HDR_MAC(p);
146
147 int field_idx = 0;
148
149 memset(&(hmac->ftype_), 0, sizeof(hmac->ftype_));
150 offset += get(buf, offset, &(hmac->ftype_), n_bits[field_idx++]);
151
152 memset(&(hmac->macSA_), 0, sizeof(hmac->macSA_));
153 offset += get(buf, offset, &(hmac->macSA_), n_bits[field_idx++]);
154 hmac->macSA_ = restoreSignedValue(hmac->macSA_, n_bits[field_idx - 1]);
155
156 memset(&(hmac->macDA_), 0, sizeof(hmac->macDA_));
157 offset += get(buf, offset, &(hmac->macDA_), n_bits[field_idx++]);
158 hmac->macDA_ = restoreSignedValue(hmac->macDA_, n_bits[field_idx - 1]);
159
160 /* Old function to uncompresso signed value
161 if (n_bits[field_idx - 1] < sizeof(hmac->macDA()) * 8) { // Compression!
162 bitset<sizeof(hmac->macDA()) * 8 > my_bitset(hmac->macDA());
163 if (my_bitset[n_bits[field_idx - 1] - 1] == 1) {
164 for (int i = n_bits[field_idx - 1]; i < sizeof(hmac->macDA()) * 8;
165 ++i) {
166 my_bitset.set(i,1);
167 }
168 }
169 hmac->macDA() = static_cast<int>(my_bitset.to_ulong());
170 }
171 */
172
173 memset(&(hmac->hdr_type_), 0, sizeof(hmac->hdr_type_));
174 offset += get(buf, offset, &(hmac->hdr_type_), n_bits[field_idx++]);
175
176 memset(&(hmac->txtime_), 0, sizeof(hmac->txtime_));
177 offset += get(buf, offset, &(hmac->txtime_), n_bits[field_idx++]);
178
179 memset(&(hmac->sstime_), 0, sizeof(hmac->sstime_));
180 offset += get(buf, offset, &(hmac->sstime_), n_bits[field_idx++]);
181
182 memset(&(hmac->padding_), 0, sizeof(hmac->padding_));
183 offset += get(buf, offset, &(hmac->padding_), n_bits[field_idx++]);
184 hmac->padding_ = restoreSignedValue(hmac->padding_, n_bits[field_idx - 1]);
185
186 if (debug_) {
187 printf("\033[0;45;30m RX MAC packer hdr \033[0m \n");
189 }
190
191 return offset;
192}
193
194void
196{
197 std::cout << "\033[0;45;30m"
198 << " Packer Name "
199 << "\033[0m"
200 << " UWMAC" << std::endl;
201 std::cout << "\033[0;45;30m 1st field "
202 << "\033[0m"
203 << " ftype: " << Ftype_Bits << " bits" << std::endl;
204 std::cout << "\033[0;45;30m 2st field "
205 << "\033[0m"
206 << " macDA: " << SRC_Bits << " bits" << std::endl;
207 std::cout << "\033[0;45;30m 3st field "
208 << "\033[0m"
209 << " macDA: " << DST_Bits << " bits" << std::endl;
210 std::cout << "\033[0;45;30m 4st field "
211 << "\033[0m"
212 << " hdr_type: " << Htype_Bits << " bits" << std::endl;
213 std::cout << "\033[0;45;30m 5st field "
214 << "\033[0m"
215 << " txtime: " << TXtime_Bits << " bits" << std::endl;
216 std::cout << "\033[0;45;30m 6st field "
217 << "\033[0m"
218 << " sstime: " << SStime_Bits << " bits" << std::endl;
219 std::cout << "\033[0;45;30m 7st field "
220 << "\033[0m"
221 << " padding: " << Padding_Bits << " bits" << std::endl;
222 return;
223}
224
225void
227{
228
229 hdr_mac *hmac = HDR_MAC(p);
230
231 if (n_bits[0] != 0)
232 cout << "\033[0;45;30m ftype:\033[0m " << hmac->ftype_ << " "
233 << hex_bytes(hmac->ftype_, n_bits[0]) << std::endl;
234 if (n_bits[1] != 0)
235 cout << "\033[0;45;30m macSA:\033[0m " << hmac->macSA_ << " "
236 << hex_bytes(hmac->macSA_, n_bits[1]) << std::endl;
237 if (n_bits[2] != 0)
238 cout << "\033[0;45;30m macDA:\033[0m " << hmac->macDA_ << " "
239 << hex_bytes(hmac->macDA_, n_bits[2]) << std::endl;
240 if (n_bits[3] != 0)
241 cout << "\033[0;45;30m hdr_type:\033[0m " << hmac->hdr_type_ << " "
242 << hex_bytes(hmac->hdr_type_, n_bits[3]) << std::endl;
243 if (n_bits[4] != 0)
244 cout << "\033[0;45;30m txtime:\033[0m " << hmac->txtime_ << " "
245 << hex_bytes(hmac->txtime_) << std::endl;
246 if (n_bits[5] != 0)
247 cout << "\033[0;45;30m sstime:\033[0m " << hmac->sstime_ << " "
248 << hex_bytes(hmac->sstime_) << std::endl;
249 if (n_bits[6] != 0)
250 cout << "\033[0;45;30m padding:\033[0m " << hmac->padding_ << " "
251 << hex_bytes(hmac->padding_, n_bits[6]) << std::endl;
252 // cout << "\033[0;41;30m packerMAC::printMyHdrField WARNING \033[0m, Field
253 // index " << field << " does not exist or its printing is not implemented."
254 // << std::endl;
255}
Class to create the Otcl shadow object for an object of the class packer.
TclObject * create(int, const char *const *)
Class to map a ns2 mac header into a bit stream, and vice-versa.
Definition packer-mac.h:51
~packerMAC()
Class destructor.
size_t DST_Bits
Bit length of the macSA_ field to be put in the header stream of bits.
Definition packer-mac.h:103
size_t Htype_Bits
Bit length of the macDA_ field to be put in the header stream of bits.
Definition packer-mac.h:105
size_t TXtime_Bits
Bit length of the hdr_type_ field to be put in the header stream of bits.
Definition packer-mac.h:107
size_t Ftype_Bits
Definition packer-mac.h:99
size_t SStime_Bits
Bit length of the txtime_ field to be put in the header stream of bits.
Definition packer-mac.h:109
void printMyHdrMap()
Method used for debug purposes.
size_t packMyHdr(Packet *, unsigned char *, size_t)
Method to transform the headers of Uwpolling into a stream of bits.
void printMyHdrFields(Packet *)
Method used for debug purposes.
size_t SRC_Bits
Bit length of the ftype_ field to be put in the header stream of bits.
Definition packer-mac.h:101
size_t Padding_Bits
Bit length of the sstime_ field to be put in the header stream of bits.
Definition packer-mac.h:111
void init()
Init the Packer.
packerMAC()
Class constructor.
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...
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
static T restoreSignedValue(T _header_field, const uint32_t &_num_compressed_bits)
Definition packer.h:174
int debug_
Flag to enable debug messages.
Definition packer.h:227
PackerMACClass class_module_packerMAC
Header of the class responsible to map the ns2 packet of mac into a bit stream, and vice-versa.