DESERT 3.6.0
Loading...
Searching...
No Matches
uwip-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
39#include "uwip-module.h"
40#include "uwip-clmsg.h"
41#include <packet.h>
42#include <sstream>
43
44extern packet_t PT_UWIP;
45
47
48uint8_t UWIPModule::lastIP = 0;
49
53static class UwIpPktClass : public PacketHeaderClass
54{
55public:
57 : PacketHeaderClass("PacketHeader/UWIP", sizeof(hdr_uwip))
58 {
59 this->bind();
60 bind_offset(&hdr_uwip::offset_);
61 }
63
67static class UWIPModuleClass : public TclClass
68{
69public:
71 : TclClass("Module/UW/IP")
72 {
73 }
74
75 TclObject *
76 create(int, const char *const *)
77 {
78 return (new UWIPModule());
79 }
81
83 : ipAddr_(0)
84 , debug_(0)
85 , addr_type_inet(true)
86{
87 bind("debug_", &debug_);
88 ipAddr_ = ++lastIP;
89}
90
91int
92UWIPModule::command(int argc, const char *const *argv)
93{
94 Tcl &tcl = Tcl::instance();
95 if (argc == 2) {
96 if (strcasecmp(argv[1], "addr") == 0) {
97 tcl.resultf("%d", ipAddr_);
98 return TCL_OK;
99 } else if (strcasecmp(argv[1], "setaddrinet") == 0) {
100 addr_type_inet = true;
101 return TCL_OK;
102 } else if (strcasecmp(argv[1], "setaddrilink") == 0) {
103 addr_type_inet = false;
104 return TCL_OK;
105 } else if (strcasecmp(argv[1], "addr-string") == 0) {
106 tcl.resultf("%s", printIP(ipAddr_).c_str());
107 return TCL_OK;
108 } else if (strcasecmp(argv[1], "getipheadersize") == 0) {
109 tcl.resultf("%d", this->getIpHeaderSize());
110 return TCL_OK;
111 } else if (strcasecmp(argv[1], "printidspkts") == 0) {
112 this->printIdsPkts();
113 return TCL_OK;
114 }
115 } else if (argc == 3) {
116 if (strcasecmp(argv[1], "addr") == 0) {
117 int addr = std::atoi(argv[2]);
118 if (addr <= 0 || addr > 255) {
119 tcl.resultf("invalid IP address: %d", addr);
120 return TCL_ERROR;
121 }
122
123 ipAddr_ = static_cast<uint8_t>(addr);
124 return TCL_OK;
125 }
126 }
127 return Module::command(argc, argv);
128}
129
130void
132{
133 hdr_cmn *ch = HDR_CMN(p);
134 hdr_uwip *iph = HDR_UWIP(p);
135
136 if (ch->direction() == hdr_cmn::UP) {
137 if (iph->saddr() == ipAddr_) {
138 drop(p, 1, ORIGINATED_BY_ME);
139 return;
140 } else if ((ch->next_hop() & 0x000000ff) == (ipAddr_ & 0x000000ff) ||
141 (ch->next_hop() & 0x000000ff) ==
142 (UWIP_BROADCAST & 0x000000ff) ||
143 (iph->daddr() & 0x000000ff) == (ipAddr_ & 0x000000ff) ||
144 (iph->daddr() & 0x000000ff) == (UWIP_BROADCAST & 0x000000ff)) {
145 ch->size() -= sizeof(hdr_uwip);
146 sendUp(p);
147 return;
148 } else {
149 drop(p, 1, NOT_FOR_ME_REASON);
150 return;
151 }
152 } else if (ch->direction() == hdr_cmn::DOWN) {
153 if (iph->daddr() == ipAddr_ || ch->next_hop() == ipAddr_) {
154 drop(p, 1, INVALID_DESTINATION_ADDR);
155 return;
156 }
157
158 ch->addr_type() = addr_type_inet ? NS_AF_INET : NS_AF_ILINK;
159 ch->size() += sizeof(hdr_uwip);
160
161 if (iph->saddr() == 0)
162 iph->saddr() = ipAddr_;
163
164 if (ch->prev_hop_ == 0)
165 ch->prev_hop_ = ipAddr_;
166
167 if (iph->daddr() == 0) {
168 drop(p, 1, DESTINATION_ADDR_UNSET);
169 return;
170 }
171
172 if (ch->next_hop() == 0 && iph->daddr() != 0) {
173 printOnLog(Logger::LogLevel::ERROR,
174 "UWIP",
175 "recv(Packet *)::packet sent with next_hop equals to 0");
176
177 ch->next_hop() = iph->daddr();
178 }
179
180 sendDown(p);
181 } else {
182 Packet::free(p);
183 return;
184 }
185}
186
187int
189{
190 if (m->type() == UWIP_CLMSG_SEND_ADDR) {
191 UWIPClMsgSendAddr *c = new UWIPClMsgSendAddr(UNICAST, m->getSource());
192 c->setAddr(ipAddr_);
193 sendAsyncClMsg(c);
194
195 ((UWIPClMsgSendAddr *) m)->setAddr(ipAddr_);
196 return 0;
197 }
198 return Module::recvSyncClMsg(m);
199}
200
201const string
202UWIPModule::printIP(const nsaddr_t &ip_)
203{
204 std::stringstream out;
205 out << ((ip_ & 0xff000000) >> 24);
206 out << ".";
207 out << ((ip_ & 0x00ff0000) >> 16);
208 out << ".";
209 out << ((ip_ & 0x0000ff00) >> 8);
210 out << ".";
211 out << ((ip_ & 0x000000ff));
212 return out.str();
213}
214
215const string
216UWIPModule::printIP(const uint8_t &ip_)
217{
218 std::stringstream out;
219 out << ((ip_ & 0xff000000) >> 24);
220 out << ".";
221 out << ((ip_ & 0x00ff0000) >> 16);
222 out << ".";
223 out << ((ip_ & 0x0000ff00) >> 8);
224 out << ".";
225 out << ((ip_ & 0x000000ff));
226 return out.str();
227}
Class used to answer to UWIPClMsgReqAddr cross layer messages.
Definition uwip-clmsg.h:68
void setAddr(nsaddr_t addr)
Adds the module for UWIPModuleClass in ns2.
TclObject * create(int, const char *const *)
UWIPModule class is used to define the Internet Protocol (IP) layer of a node.
bool addr_type_inet
true if the addressing type is INET, false if it is ILINK.
int debug_
Flag to enable or disable dirrefent levels of debug.
static const string printIP(const nsaddr_t &)
Returns a string with an IP in the classic form "x.x.x.x" converting an ns2 nsaddr_t address.
virtual void recv(Packet *p)
Performs the reception of packets from upper and lower layers.
static uint8_t lastIP
Used to set a default IP address.
static int getIpHeaderSize()
Returns the size in byte of a hdr_sun_data packet header.
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
UWIPModule()
Constructor of UWIPModule class.
virtual int recvSyncClMsg(ClMessage *m)
Cross-Layer messages synchronous interpreter.
void printIdsPkts() const
Prints the IDs of the packet's headers defined by UWIP.
uint8_t ipAddr_
IP address of the node.
Adds the header for hdr_uwip packets in ns2.
hdr_uwip describes UWIP packets.
Definition uwip-module.h:67
uint8_t & daddr()
Reference to the daddr_ variable.
uint8_t & saddr()
Reference to the saddr_ variable.
Definition uwip-module.h:92
static int offset_
Required by the PacketHeaderManager.
Definition uwip-module.h:71
Cross layer messages definition for the UWIP Module.
UwIpPktClass class_uwip_pkt
UWIPModuleClass class_uwipmodule
Provides the UWIP packets header description. Definition of the class that define the network layer.
static const uint8_t UWIP_BROADCAST
Variable used to represent a broadcast UWIP.
Definition uwip-module.h:59
#define ORIGINATED_BY_ME
Reason for a drop in a UWIP module.
Definition uwip-module.h:52
#define NOT_FOR_ME_REASON
Reason for a drop in a UWIP module.
Definition uwip-module.h:48
#define INVALID_DESTINATION_ADDR
Reason for a drop in a UWIP module.
Definition uwip-module.h:54
#define DESTINATION_ADDR_UNSET
Reason for a drop in a UWIP module.
Definition uwip-module.h:50
#define HDR_UWIP(P)
Definition uwip-module.h:57