DESERT 3.5.1
Loading...
Searching...
No Matches
uwstaticrouting.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 <stdlib.h>
41
42#include "uwstaticrouting.h"
43
47static class UwStaticRoutingModuleClass : public TclClass
48{
49public:
51 : TclClass("Module/UW/StaticRouting")
52 {
53 }
54
55 TclObject *
56 create(int, const char *const *)
57 {
58 return (new UwStaticRoutingModule);
59 }
61
63 : default_gateway(0)
64{
66}
67
71
72int
74{
75 return Module::recvSyncClMsg(m);
76}
77
78void
83
84void
85UwStaticRoutingModule::addRoute(const uint8_t &dst, const uint8_t &next)
86{
87 if (dst == 0 || next == 0) {
88 std::cerr << "You are trying to insert an invalid entry in the routing "
89 "table with destination: "
90 << static_cast<uint32_t>(dst)
91 << " and next hop: " << static_cast<uint32_t>(next)
92 << std::endl;
93 exit(EXIT_FAILURE);
94 }
95 std::map<uint8_t, uint8_t>::iterator it = routing_table.find(dst);
96 if (it != routing_table.end()) {
97 it->second = next;
98 return;
99 } else {
100 if (routing_table.size() < IP_ROUTING_MAX_ROUTES) {
101 routing_table.insert(std::pair<uint8_t, uint8_t>(dst, next));
102 return;
103 } else {
104 std::cerr << "The routing table is full!" << std::endl;
105 return;
106 }
107 }
108}
109
110int
111UwStaticRoutingModule::command(int argc, const char *const *argv)
112{
113 Tcl &tcl = Tcl::instance();
114 if (argc == 2) {
115 if (strcasecmp(argv[1], "numroutes") == 0) {
116 tcl.resultf("%d", routing_table.size());
117 return TCL_OK;
118 }
119 if (strcasecmp(argv[1], "clearroutes") == 0) {
120 clearRoutes();
121 return TCL_OK;
122 }
123 } else if (argc == 3) {
124 if (strcasecmp(argv[1], "defaultGateway") == 0) {
125 if (static_cast<uint8_t>(atoi(argv[2])) != 0) {
126 default_gateway = static_cast<uint8_t>(atoi(argv[2]));
127 } else {
128 std::cerr << "You are trying to set an invalid address as "
129 "default gateway. Exiting ..."
130 << std::endl;
131 exit(EXIT_FAILURE);
132 }
133 return TCL_OK;
134 }
135
136 } else if (argc == 4) {
137 if (strcasecmp(argv[1], "addroute") == 0) {
138 addRoute(static_cast<uint8_t>(atoi(argv[2])),
139 static_cast<uint8_t>(atoi(argv[3])));
140 return TCL_OK;
141 }
142 }
143 return Module::command(argc, argv);
144}
145
146void
148{
149 hdr_cmn *ch = HDR_CMN(p);
150 hdr_uwip *uwiph = HDR_UWIP(p);
151
152 if (debug_) {
153 std::cout << NOW << "::UWSTATICROUTING::RECV"
154 << "::NEXT_HOP::" << ch->next_hop()
155 << "::DESTINATION_IP::" << (uint)uwiph->daddr()
156 << std::endl;
157 }
158
159 if (ch->direction() == hdr_cmn::UP) {
160
161 if (uwiph->daddr() == ch->next_hop() ||
162 uwiph->daddr() ==
163 UWIP_BROADCAST) { /* Packet is arrived at its
164 destination */
165 sendUp(p);
166 return;
167 }
168 /* Forward Packet */
169 ch->direction() = hdr_cmn::DOWN;
170 ch->next_hop() = getNextHop(p);
171 if (ch->next_hop() == 0) {
172 drop(p, 1, DROP_DEST_NO_ROUTE);
173 } else {
174 sendDown(p);
175 }
176 } else { /* direction DOWN */
177 ch->next_hop() = getNextHop(p);
178 if (ch->next_hop() == 0) {
179 drop(p, 1, DROP_DEST_NO_ROUTE);
180 } else {
181 sendDown(p);
182 }
183 }
184}
185
186uint8_t
188{
189 hdr_uwip *uwiph = HDR_UWIP(p);
190 return getNextHop(uwiph->daddr());
191}
192
193uint8_t
194UwStaticRoutingModule::getNextHop(const uint8_t &dst) const
195{
196 std::map<uint8_t, uint8_t>::const_iterator it = routing_table.find(dst);
197 if (it != routing_table.end()) {
198 return it->second;
199 } else {
200 if (default_gateway != 0) {
201 return default_gateway;
202 } else {
203 return 0;
204 }
205 }
206}
int debug_
Flag to enable or disable dirrefent levels of debug.
Adds the module for UwStaticRoutingModuleClass in ns2.
TclObject * create(int, const char *const *)
UwStaticRoutingModule class implements basic routing functionalities.
virtual void clearRoutes()
Removes all the routing information.
virtual void addRoute(const uint8_t &, const uint8_t &)
Adds a new entry in the routing table.
std::map< uint8_t, uint8_t > routing_table
Routing table: destination - next hop.
UwStaticRoutingModule()
Constructor of UwStaticRoutingModule class.
virtual int recvSyncClMsg(ClMessage *)
Cross-Layer messages synchronous interpreter.
virtual uint8_t getNextHop(const Packet *) const
Returns the next hop address of a packet passed as input.
virtual void recv(Packet *)
Performs the reception of packets from upper and lower layers.
virtual ~UwStaticRoutingModule()
Destructor of UwStaticRoutingModule class.
uint8_t default_gateway
Default gateway.
virtual int command(int, const char *const *)
TCL command interpreter.
hdr_uwip describes UWIP packets.
Definition uwip-module.h:70
uint8_t & daddr()
Reference to the daddr_ variable.
static const uint8_t UWIP_BROADCAST
Variable used to represent a broadcast UWIP.
Definition uwip-module.h:62
#define HDR_UWIP(P)
Definition uwip-module.h:58
UwStaticRoutingModuleClass class_uwstaticrouting_module
Provides basic routing functionalities.
#define DROP_DEST_NO_ROUTE
Reason for a drop in a UWVBR module.