DESERT 3.6.0
Loading...
Searching...
No Matches
uwudp-module.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
40#include "uwudp-module.h"
41
42#include <iostream>
43#include <set>
44#include <sstream>
45#include <string>
46
47extern packet_t PT_UWUDP;
48
49int hdr_uwudp::offset_ = 0;
50
51static class UwUdpPktClass : public PacketHeaderClass
52{
53public:
55 : PacketHeaderClass("PacketHeader/UWUDP", sizeof(hdr_uwudp))
56 {
57 this->bind();
58 bind_offset(&hdr_uwudp::offset_);
59 }
61
62static class UwUdpClass : public TclClass
63{
64public:
66 : TclClass("Module/UW/UDP")
67 {
68 }
69
70 TclObject *
71 create(int, const char *const *)
72 {
73 return (new UwUdp);
74 }
76
78 : portcounter(0)
79 , drop_duplicated_packets_(0)
80 , debug_(0)
81{
82 bind("drop_duplicated_packets_", &drop_duplicated_packets_);
83 bind("debug_", &debug_);
84}
85
86int
87UwUdp::command(int argc, const char *const *argv)
88{
89 Tcl &tcl = Tcl::instance();
90 if (argc == 2) {
91 if (strcasecmp(argv[1], "getudpheadersize") == 0) {
92 tcl.resultf("%d", this->getUdpHeaderSize());
93 return TCL_OK;
94 } else if (strcasecmp(argv[1], "printidspkts") == 0) {
95 this->printIdsPkts();
96 return TCL_OK;
97 }
98 }
99 if (argc == 3) {
100 if (strcasecmp(argv[1], "assignPort") == 0) {
101 Module *m = dynamic_cast<Module *>(tcl.lookup(argv[2]));
102 if (!m)
103 return TCL_ERROR;
104 int port = assignPort(m);
105 tcl.resultf("%d", port);
106 return TCL_OK;
107 }
108 }
109 return Module::command(argc, argv);
110}
111
112void
113UwUdp::recv(Packet *p)
114{
115 printOnLog(Logger::LogLevel::ERROR,
116 "UWUDP",
117 "recv(Packet *)::packet sent without source module");
118 Packet::free(p);
119}
120
121void
122UwUdp::recv(Packet *p, int idSrc)
123{
124 hdr_cmn *ch = HDR_CMN(p);
125 hdr_uwudp *uwudp = HDR_UWUDP(p);
126 hdr_uwip *iph = HDR_UWIP(p);
127
128 if (!ch->error()) {
129 if (ch->direction() == hdr_cmn::UP) {
130
131 map<int, int>::const_iterator iter = id_map.find(uwudp->dport());
132
133 if (iter == id_map.end()) {
134 printOnLog(Logger::LogLevel::ERROR,
135 "UWUDP",
136 "recv(Packet *, int)::unknown port number, dport = " +
137 to_string(uwudp->dport()));
138
139 drop(p, 1, DROP_UNKNOWN_PORT_NUMBER);
140 return;
141 }
142
143 int module_id = iter->second;
144
145 printOnLog(Logger::LogLevel::DEBUG,
146 "UWUDP",
147 "recv(Packet *, int)::new packet with id " +
148 to_string(ch->uid()) + " from ip " +
149 to_string(static_cast<uint16_t>(iph->saddr())) +
150 " : " + to_string(iter->first));
151
152 if (drop_duplicated_packets_ == 1) {
153 map<uint16_t, map_packets_el>::iterator it =
154 map_packets.find(iter->first);
155
156 if (it == map_packets.end()) {
157 printOnLog(Logger::LogLevel::DEBUG,
158 "UWUDP",
159 "recv(Packet *, int)::packet to a new port");
160
161 std::set<int> tmp_set_;
162 tmp_set_.insert(ch->uid());
163 map_packets_el tmp_map_el_;
164 tmp_map_el_.insert(pair<uint8_t, std::set<int>>(
165 iph->saddr(), tmp_set_));
166 map_packets.insert(pair<uint16_t, map_packets_el>(
167 iter->first, tmp_map_el_));
168 } else {
169 printOnLog(Logger::LogLevel::DEBUG,
170 "UWUDP",
171 "recv(Packet *, int)::packet to a known port");
172
173 std::map<uint8_t, std::set<int>>::iterator it2 =
174 it->second.find(iph->saddr());
175 if (it2 == it->second.end()) {
176 printOnLog(Logger::LogLevel::DEBUG,
177 "UWUDP",
178 "recv(Packet *, int)::packet from a new "
179 "source");
180
181 std::set<int> tmp_set_;
182 tmp_set_.insert(ch->uid());
183 it->second.insert(pair<uint8_t, std::set<int>>(
184 iph->saddr(), tmp_set_));
185 } else {
186 printOnLog(Logger::LogLevel::DEBUG,
187 "UWUDP",
188 "recv(Packet *, int)::packet from a known "
189 "source");
190
191 if (it2->second.count(ch->uid()) < 1) {
192 printOnLog(Logger::LogLevel::DEBUG,
193 "UWUDP",
194 "recv(Packet *, int)::new packet received");
195
196 it2->second.insert(ch->uid());
197 } else { // Packet already received.
198 printOnLog(Logger::LogLevel::DEBUG,
199 "UWUDP",
200 "recv(Packet *, int)::duplicate packet "
201 "dropped");
202
204 return;
205 }
206 }
207 }
208 }
209
210 ch->size() -= sizeof(hdr_uwudp);
211 sendUp(module_id, p);
212 } else {
213 map<int, int>::const_iterator iter = port_map.find(idSrc);
214
215 if (iter == port_map.end()) {
216 printOnLog(Logger::LogLevel::ERROR,
217 "UWUDP",
218 "recv(Packet *, int)::no port assigned to id " +
219 to_string(idSrc) + ", dropping packet");
220
221 Packet::free(p);
222 return;
223 }
224
225 int sport = iter->second;
226 assert(sport > 0 && sport <= portcounter);
227 uwudp->sport() = sport;
228
229 ch->size() += sizeof(hdr_uwudp);
230 sendDown(p);
231 }
232 }
233}
234
235int
237{
238 int id = m->getId();
239
240 // Check that the provided module has not been given a port before
241 if (port_map.find(id) != port_map.end())
242 return TCL_ERROR;
243
244 int newport = ++portcounter;
245
246 port_map[id] = newport;
247 assert(id_map.find(newport) == id_map.end());
248 id_map[newport] = id;
249 assert(id_map.find(newport) != id_map.end());
250
251 std::stringstream msg;
252 msg << "assignPort(Module *)::"
253 << "id = " << id << " port = " << newport
254 << " portcounter = " << portcounter;
255 printOnLog(Logger::LogLevel::INFO, "UWUDP", msg.str());
256
257 return newport;
258}
TclObject * create(int, const char *const *)
UwUdp class is used to manage UWUDP packets, and flows to and from upper modules.
std::map< int, int > port_map
Map application module id to port number (id, port_number).
std::map< int, int > id_map
Map port number to application module id (port_number, id).
virtual int command(int, const char *const *) override
TCL command interpreter.
virtual void recv(Packet *) override
Performs the reception of packets from upper and lower layers.
virtual int assignPort(Module *)
Associates a module with a port.
uint16_t portcounter
Counter used to generate new port numbers.
UwUdp()
Constructor of UwUdp class.
static int getUdpHeaderSize()
Returns the size in byte of a hdr_uwudp packet header.
std::map< uint16_t, map_packets_el > map_packets
Map used to keep track of the packets received by each port.
int drop_duplicated_packets_
Flag to enable or disable the drop of duplicated packets.
int debug_
Flag to enable or disable debug.
void printIdsPkts() const
Prints the IDs of the packet's headers defined by UWUDP.
hdr_uwip describes UWIP packets.
Definition uwip-module.h:67
uint8_t & saddr()
Reference to the saddr_ variable.
Definition uwip-module.h:92
hdr_uwudp describes UWUDP packets.
static int offset_
Required by the PacketHeaderManager.
uint8_t & sport()
Reference to the sport_ variable.
uint8_t & dport()
Reference to the dport_ variable.
#define HDR_UWIP(P)
Definition uwip-module.h:57
UwUdpClass class_module_uwudp
UwUdpPktClass class_uwudp_pkt
Provides the UWUDP packets header description and the definition of the class UWUDP.
#define HDR_UWUDP(P)
std::map< uint8_t, std::set< int > > map_packets_el
#define DROP_RECEIVED_DUPLICATED_PACKET
Reason for a drop in a UWUDP module.
#define DROP_UNKNOWN_PORT_NUMBER
Reason for a drop in a UWUDP module.