DESERT 3.6.0
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 "uwstaticrouting.h"
41#include <string>
42
46static class UwStaticRoutingModuleClass : public TclClass
47{
48public:
50 : TclClass("Module/UW/StaticRouting")
51 {
52 }
53
54 TclObject *
55 create(int, const char *const *)
56 {
57 return (new UwStaticRoutingModule);
58 }
60
62 : default_gateway(0)
63{
65}
66
67void
68UwStaticRoutingModule::addRoute(const uint8_t &dst, const uint8_t &next)
69{
70 if (dst == 0 || next == 0) {
71 std::cerr << "You are trying to insert an invalid entry in the routing "
72 "table with destination: "
73 << static_cast<uint32_t>(dst)
74 << " and next hop: " << static_cast<uint32_t>(next)
75 << std::endl;
76 exit(1);
77 }
78
79 std::map<uint8_t, uint8_t>::iterator it = routing_table.find(dst);
80
81 if (it != routing_table.end()) {
82 it->second = next;
83 } else {
85 routing_table.insert(std::pair<uint8_t, uint8_t>(dst, next));
86 } else {
87 std::cerr << "The routing table is full!" << std::endl;
88 }
89 }
90}
91
92int
93UwStaticRoutingModule::command(int argc, const char *const *argv)
94{
95 Tcl &tcl = Tcl::instance();
96 if (argc == 2) {
97 if (strcasecmp(argv[1], "numroutes") == 0) {
98 tcl.resultf("%d", routing_table.size());
99 return TCL_OK;
100 }
101 if (strcasecmp(argv[1], "clearroutes") == 0) {
102 clearRoutes();
103 return TCL_OK;
104 }
105 } else if (argc == 3) {
106 if (strcasecmp(argv[1], "defaultGateway") == 0) {
107 if (static_cast<uint8_t>(atoi(argv[2])) != 0) {
108 default_gateway = static_cast<uint8_t>(atoi(argv[2]));
109 return TCL_OK;
110 }
111
112 tcl.result("invalid address as default gateway. Exiting ...");
113 return TCL_ERROR;
114 }
115
116 } else if (argc == 4) {
117 if (strcasecmp(argv[1], "addroute") == 0) {
118 int dst = std::atoi(argv[2]);
119 int next = std::atoi(argv[3]);
120
121 if (dst <= 0 || dst > 255 || next <= 0 || next > 255) {
122 tcl.result("invalid destination or next hop address");
123 return TCL_ERROR;
124 }
125
126 addRoute(static_cast<uint8_t>(dst), static_cast<uint8_t>(next));
127 return TCL_OK;
128 }
129 }
130 return Module::command(argc, argv);
131}
132
133void
135{
136 hdr_cmn *ch = HDR_CMN(p);
137 hdr_uwip *uwiph = HDR_UWIP(p);
138
139 printOnLog(Logger::LogLevel::DEBUG,
140 "UWSTATICROUTING",
141 "recv(Packet *)::next hop " + to_string(ch->next_hop()) +
142 " destination ip = " + to_string((uint) uwiph->daddr()));
143
144 if (ch->direction() == hdr_cmn::UP) {
145 if (uwiph->daddr() == ch->next_hop() ||
146 uwiph->daddr() == UWIP_BROADCAST) {
147 sendUp(p);
148 return;
149 }
150
151 ch->direction() = hdr_cmn::DOWN;
152 ch->next_hop() = getNextHop(p);
153
154 if (ch->next_hop() == 0)
155 drop(p, 1, DROP_DEST_NO_ROUTE);
156 else
157 sendDown(p);
158 } else {
159 ch->next_hop() = getNextHop(p);
160
161 if (ch->next_hop() == 0)
162 drop(p, 1, DROP_DEST_NO_ROUTE);
163 else
164 sendDown(p);
165 }
166}
167
168uint8_t
170{
171 return getNextHop(HDR_UWIP(p)->daddr());
172}
173
174uint8_t
175UwStaticRoutingModule::getNextHop(const uint8_t &dst) const
176{
177 std::map<uint8_t, uint8_t>::const_iterator it = routing_table.find(dst);
178
179 if (it != routing_table.end()) {
180 return it->second;
181 } else {
182 if (default_gateway != 0)
183 return default_gateway;
184
185 return 0;
186 }
187}
Adds the module for UwStaticRoutingModuleClass in ns2.
TclObject * create(int, const char *const *)
UwStaticRoutingModule class implements basic routing functionalities.
virtual void recv(Packet *) override
Performs the reception of packets from upper and lower layers.
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 command(int, const char *const *) override
TCL command interpreter.
virtual uint8_t getNextHop(const Packet *) const
Returns the next hop address of a packet passed as input.
uint8_t default_gateway
Default gateway.
hdr_uwip describes UWIP packets.
Definition uwip-module.h:67
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:59
#define HDR_UWIP(P)
Definition uwip-module.h:57
UwStaticRoutingModuleClass class_uwstaticrouting_module
Provides basic routing functionalities.
static const uint16_t IP_ROUTING_MAX_ROUTES
Maximum number of entries in the routing table of a node.
#define DROP_DEST_NO_ROUTE
Reason for a drop in a UWVBR module.