DESERT 3.5.1
Loading...
Searching...
No Matches
packer-uwip.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-uwip.h"
37
41static class PackerUWIPClass : public TclClass {
42public:
43
44 PackerUWIPClass() : TclClass("UW/IP/Packer") {
45 }
46
47 TclObject* create(int, const char*const*) {
48 return (new packerUWIP());
49 }
51
52//packerUWIP::packerUWIP() : packer(false), isRacunBroadcast(0) {
54 SAddr_Bits = 8 * sizeof (uint8_t);
55 DAddr_Bits = 8 * sizeof (uint8_t);
56
57 bind("SAddr_Bits", (int*) &SAddr_Bits);
58 bind("DAddr_Bits", (int*) &DAddr_Bits);
59 //bind("isRacunBroadcast_", &isRacunBroadcast);
60
61 if (debug_)
62 cout << "Initialization (from constructor) of n_bits for the UWIP packer " << endl;
63
64 n_bits.clear();
65
66 n_bits.push_back(SAddr_Bits);
67 n_bits.push_back(DAddr_Bits);
68}
69
73
75
76 if (debug_)
77 cout << "Re-initialization of n_bits for the UWIP packer " << endl;
78
79 n_bits.clear();
80
81 n_bits.push_back(SAddr_Bits);
82 n_bits.push_back(DAddr_Bits);
83}
84
85size_t packerUWIP::packMyHdr(Packet* p, unsigned char* buf, size_t offset) {
86
87 // Pointer to the UWIP packet header
88 hdr_uwip* hip = HDR_UWIP(p);
89
90 int field_idx = 0;
91
92 offset += put(buf, offset, &(hip->saddr_), n_bits[field_idx++]);
93 offset += put(buf, offset, &(hip->daddr_), n_bits[field_idx++]);
94
95 if (debug_) {
96 printf("\033[0;42;30m TX IP packer hdr \033[0m \n");
98 }
99
100 return offset;
101}
102
103size_t packerUWIP::unpackMyHdr(unsigned char* buf, size_t offset, Packet* p) {
104
105 // Pointer to the UWIP packet header
106 hdr_uwip* hip = HDR_UWIP(p);
107
108 int field_idx = 0;
109
110 memset(&(hip->saddr()), 0, sizeof ( hip->saddr_));
111 offset += get(buf, offset, &(hip->saddr_), n_bits[field_idx++]);
112
113 memset(&(hip->daddr()), 0, sizeof ( hip->daddr_));
114 offset += get(buf, offset, &(hip->daddr_), n_bits[field_idx++]);
115
116// if (isRacunBroadcast && (hip->daddr_ == RACUN_BROADCAST)){
117// hip->daddr_ = UWIP_BROADCAST;
118// }
119
120 if (debug_) {
121 printf("\033[0;42;30m RX IP packer hdr \033[0m \n");
123 }
124
125 return offset;
126}
127
129 std::cout << "\033[0;42;30m" << " Packer Name " << "\033[0m" << " UWIP" << std::endl;
130 std::cout << "\033[0;42;30m 1st field " << "\033[0m" << " saddr: " << SAddr_Bits << " bits" << std::endl;
131 std::cout << "\033[0;42;30m 2nd field " << "\033[0m" << " daddr: " << DAddr_Bits << " bits" << std::endl;
132 return;
133}
134
136 hdr_uwip* hip = HDR_UWIP(p);
137
138 if (n_bits[0] != 0) {
139 std::cout << "\033[0;42;30m saddr:\033[0m " << static_cast<uint32_t> (hip->saddr()) << " " << hex_bytes(hip->saddr(), n_bits[0]) << std::endl;
140 }
141 if (n_bits[1] != 0) {
142 std::cout << "\033[0;42;30m daddr:\033[0m " << static_cast<uint32_t> (hip->daddr()) << " " << hex_bytes(hip->daddr(), n_bits[1]) << std::endl;
143 }
144}
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.
Definition packer-uwip.h:49
packerUWIP()
Class constructor.
size_t packMyHdr(Packet *, unsigned char *, size_t)
Method to transform the headers of Uwpolling into a stream of bits.
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...
void init()
Init the Packer.
size_t SAddr_Bits
Definition packer-uwip.h:97
~packerUWIP()
Class destructor.
void printMyHdrFields(Packet *)
Method used for debug purposes.
size_t DAddr_Bits
Bit length of the saddr_ field to be put in the header stream of bits.
Definition packer-uwip.h:98
void printMyHdrMap()
Method used for debug purposes.
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
PackerUWIPClass class_module_packerUWIP
hdr_uwip describes UWIP packets.
Definition uwip-module.h:70
uint8_t & daddr()
Reference to the daddr_ variable.
uint8_t daddr_
IP of the destination.
Definition uwip-module.h:72
uint8_t & saddr()
Reference to the saddr_ variable.
Definition uwip-module.h:95
uint8_t saddr_
IP of the source.
Definition uwip-module.h:71
#define HDR_UWIP(P)
Definition uwip-module.h:58