DESERT 3.5.1
Loading...
Searching...
No Matches
uwmulti-destination.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2018 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
38#include "uwmulti-destination.h"
42static class UwMultiDestinationClass : public TclClass
43{
44public:
49 : TclClass("Module/UW/MULTI_DESTINATION")
50 {
51 }
56 TclObject *
57 create(int, const char *const *)
58 {
59 return (new UwMultiDestination);
60 }
62
64
66 : Module()
67 , debug_(0)
68 , min_delay_(0)
69 , switch_mode_(UW_MANUAL_SWITCH)
70 , lower_id_active_(0)
71 , layer_list()
72 , default_lower_id(0)
73{
74 bind("debug_", &debug_);
75 bind("min_delay_", &min_delay_);
76 bind("switch_mode_", (int *) &switch_mode_);
77 bind("set_lower_id_active_", &lower_id_active_);
78}
79
80int
81UwMultiDestination::command(int argc, const char *const *argv)
82{
83 if (argc == 2) {
84 if (strcasecmp(argv[1], "setAutomaticSwitch") == 0) {
86 return TCL_OK;
87 } else if (strcasecmp(argv[1], "setManualSwitch") == 0) {
89 return TCL_OK;
90 }
91 } else if (argc == 3) {
92 if (strcasecmp(argv[1], "setManualLowerlId") == 0) {
93 lower_id_active_ = atoi(argv[2]);
94 return TCL_OK;
95 } else if (strcasecmp(argv[1], "setDefaultLowerId") == 0) {
96 default_lower_id = atoi(argv[2]);
97 return TCL_OK;
98 }
99 } else if (argc == 4) {
100
101 //parameters: IP_val, layer_id
102 if (strcasecmp(argv[1], "addLayer") == 0) {
103 if (atoi(argv[2]) < 0) {
104 std::cerr << "UwMultiDestination::command. "
105 <<"Error, negative IP address" << std::endl;
106 return TCL_ERROR;
107 } else {
108 IP_range range(atoi(argv[2]), atoi(argv[2]));
109 if (!addLayer(range, atoi(argv[3]))) {
110 std::cerr << "UwMultiDestination::command. "
111 <<"Error, overlapping IP ranges" << std::endl;
112 return TCL_ERROR;
113 }
114
115 return TCL_OK;
116 }
117
118 }
119
120 } else if (argc == 5) {
121 //parameters: IP_val_min, IP_val_max, layer_id
122 if (strcasecmp(argv[1], "addLayer") == 0) {
123 if (atoi(argv[2]) < 0 || atoi(argv[3]) < atoi(argv[2])) {
124 std::cerr << "UwMultiDestination::command. "
125 <<"Wrong IP address range" << std::endl;
126 return TCL_ERROR;
127 } else {
128 IP_range range(atoi(argv[2]),atoi(argv[3]));
129 if (!addLayer(range, atoi(argv[4]))) {
130 std::cerr << "UwMultiDestination::command. "
131 <<"Error, overlapping IP ranges" << std::endl;
132 return TCL_ERROR;
133 }
134 return TCL_OK;
135 }
136
137 }
138 }
139
140 return Module::command(argc, argv);
141} /* UwMultiDestination::command */
142
143bool
145{
146
147 if (layer_list.empty()) {
148 layer_IPrange item = std::make_pair(id,range);
149 layer_list.push_back(item);
150 return true;
151 } else {
152 if (checkNotOverlap(range)) {
153 layer_IPrange item = std::make_pair(id,range);
154 layer_list.push_back(item);
155 return true;
156 }
157 }
158 return false;
159}
160
161void
163{
164 hdr_cmn *ch = HDR_CMN(p);
165 if (ch->direction() == hdr_cmn::UP) {
166 sendUp(p, min_delay_);
167 } else {
168 // direction DOWN: packet is coming from upper layers
170 }
171}
172
173void
175{
176
178 sendDown(getDestinationLayer(p), p, min_delay_);
179 } else {
180 sendDown(lower_id_active_, p, min_delay_);
181 }
182}
183
184int
186{
187 hdr_uwip *ih = HDR_UWIP(p);
188
189
190 int dest_addr = ih->daddr();
191 auto it = layer_list.begin();
192 for (; it != layer_list.end(); it++) {
193 if (it->second.isInRange(dest_addr)) {
194 return it->first;
195 }
196 }
197 return default_lower_id;
198}
199
200bool
202{
203 auto it = layer_list.begin();
204 for (; it != layer_list.end(); it++) {
205 if (it->second.overlappingRange(range)) {
206 return false;
207 }
208 }
209 return true;
210}
Class that represents the binding with the tcl configuration script.
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
UwMultiDestinationClass()
Constructor of the class.
Class used to represents the UwMultiDestination layer of a node.
UwMultiDestination()
Constructor of UwMultiDestination class.
int default_lower_id
Maps a layer id into an IP_range.
std::list< layer_IPrange > layer_list
int lower_id_active_
Id of the current lower layer active.
@ UW_AUTOMATIC_SWITCH
State to switch-mode automatically.
@ UW_MANUAL_SWITCH
State to switch-mode manually.
virtual void recvFromUpperLayers(Packet *p)
Default lower id to use if dest adress is not found in the considered IP ranges.
static int const layer_not_exist
This constant is returned when a searched layer does not exist>
int debug_
Flag to activate debug verbosity.
virtual bool checkNotOverlap(IP_range range)
return true if there is not overlap between the new range and the previous rnage in the list
Mode switch_mode_
Current switch mode (either AUTOMATIC or MANUAL).
virtual int command(int, const char *const *)
TCL command interpreter.
virtual void recv(Packet *p)
recv method.
virtual bool addLayer(IP_range range, int id)
Add a layer in the layer map if the IP range is valide, i.e., if the range doesn't overlap with an ex...
virtual int getDestinationLayer(Packet *p)
Return the best layer to forward the packet when the system works in AUTOMATIC_MODE.
hdr_uwip describes UWIP packets.
Definition uwip-module.h:70
uint8_t & daddr()
Reference to the daddr_ variable.
#define HDR_UWIP(P)
Definition uwip-module.h:58
UwMultiDestinationClass class_multi_destination
Definition of UwMultiDestination class.
std::pair< int, IP_range > layer_IPrange