DESERT 3.5.1
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
42extern packet_t PT_UWIP;
43
45
46uint8_t UWIPModule::lastIP = 0;
47
51static class UwIpPktClass : public PacketHeaderClass
52{
53public:
55 : PacketHeaderClass("PacketHeader/UWIP", sizeof(hdr_uwip))
56 {
57 this->bind();
58 bind_offset(&hdr_uwip::offset_);
59 }
61
65static class UWIPModuleClass : public TclClass
66{
67public:
69 : TclClass("Module/UW/IP")
70 {
71 }
72
73 TclObject *
74 create(int, const char *const *)
75 {
76 return (new UWIPModule());
77 }
79
81 : ipAddr_(0)
82 , debug_(0)
83 , addr_type_inet(true)
84{
85 bind("debug_", &debug_);
86 ipAddr_ = ++lastIP;
87}
88
92
93int
94UWIPModule::command(int argc, const char *const *argv)
95{
96 Tcl &tcl = Tcl::instance();
97 if (argc == 2) {
98 if (strcasecmp(argv[1], "addr") == 0) {
99 tcl.resultf("%d", ipAddr_);
100 return TCL_OK;
101 } else if (strcasecmp(argv[1], "setaddrinet") == 0) {
102 addr_type_inet = true;
103 return TCL_OK;
104 } else if (strcasecmp(argv[1], "setaddrilink") == 0) {
105 addr_type_inet = false;
106 return TCL_OK;
107 } else if (strcasecmp(argv[1], "addr-string") == 0) {
108 tcl.resultf("%d.%d.%d.%d",
109 (ipAddr_ & 0xff000000) >> 24,
110 (ipAddr_ & 0x00ff0000) >> 16,
111 (ipAddr_ & 0x0000ff00) >> 8,
112 (ipAddr_ & 0x000000ff));
113 return TCL_OK;
114 } else if (strcasecmp(argv[1], "getipheadersize") == 0) {
115 tcl.resultf("%d", this->getIpHeaderSize());
116 return TCL_OK;
117 } else if (strcasecmp(argv[1], "printidspkts") == 0) {
118 this->printIdsPkts();
119 return TCL_OK;
120 }
121 } else if (argc == 3) {
122 if (strcasecmp(argv[1], "addr") == 0) {
123 ipAddr_ = static_cast<uint8_t>(atoi(argv[2]));
124 if (ipAddr_ == 0) {
125 fprintf(stderr, "0 is not a valid IP address");
126 return TCL_ERROR;
127 }
128 return TCL_OK;
129 }
130 }
131 return Module::command(argc, argv);
132}
133
134void
136{
137 hdr_cmn *ch = HDR_CMN(p);
138 hdr_uwip *iph = HDR_UWIP(p);
139
140 if (ch->direction() == hdr_cmn::UP) {
141 if (iph->saddr() == ipAddr_) {
142 drop(p, 1, ORIGINATED_BY_ME);
143 return;
144 } else if ((ch->next_hop() & 0x000000ff) == (ipAddr_ & 0x000000ff) ||
145 (ch->next_hop() & 0x000000ff) ==
146 (UWIP_BROADCAST & 0x000000ff) ||
147 (iph->daddr() & 0x000000ff) == (ipAddr_ & 0x000000ff) ||
148 (iph->daddr() & 0x000000ff) ==
149 (UWIP_BROADCAST & 0x000000ff)) { // Right destination.
150 ch->size() -= sizeof(hdr_uwip);
151 sendUp(p);
152 return;
153 } else {
154 drop(p, 1, NOT_FOR_ME_REASON);
155 return;
156 }
157 } else if (ch->direction() == hdr_cmn::DOWN) { // Direction DOWN
158 if (iph->daddr() == ipAddr_ ||
159 ch->next_hop() == ipAddr_) { // The node is sending a packet to
160 // itself, drop it.
161 drop(p, 1, INVALID_DESTINATION_ADDR);
162 return;
163 }
164 if (addr_type_inet) {
165 ch->addr_type() = NS_AF_INET;
166 } else {
167 ch->addr_type() = NS_AF_ILINK;
168 }
169
170 ch->size() += sizeof(hdr_uwip);
171
172 if (iph->saddr() == 0) {
173 iph->saddr() = ipAddr_;
174 }
175
176 if (ch->prev_hop_ == 0) {
177 ch->prev_hop_ = ipAddr_;
178 }
179
180 if (iph->daddr() == 0) {
181 drop(p, 1, DESTINATION_ADDR_UNSET);
182 return;
183 }
184
185 if (ch->next_hop() == 0 && iph->daddr() != 0) {
186 if (debug_)
187 cerr << "Node:" << this->printIP(ipAddr_)
188 << ":Warning:Packet sent with next_hop equals to 0"
189 << endl;
190 ch->next_hop() = iph->daddr();
191 }
192
193 sendDown(p);
194 } else {
195 Packet::free(p);
196 return;
197 }
198}
199
200int
202{
203 if (m->type() == UWIP_CLMSG_SEND_ADDR) {
204 UWIPClMsgSendAddr *c = new UWIPClMsgSendAddr(UNICAST, m->getSource());
205 c->setAddr(ipAddr_);
206 sendAsyncClMsg(c);
207
208 ((UWIPClMsgSendAddr *) m)->setAddr(ipAddr_);
209 return 0;
210 }
211 return Module::recvSyncClMsg(m);
212}
213
214const string
215UWIPModule::printIP(const nsaddr_t &ip_)
216{
217 stringstream out;
218 out << ((ip_ & 0xff000000) >> 24);
219 out << ".";
220 out << ((ip_ & 0x00ff0000) >> 16);
221 out << ".";
222 out << ((ip_ & 0x0000ff00) >> 8);
223 out << ".";
224 out << ((ip_ & 0x000000ff));
225 return out.str();
226}
227
228const string
229UWIPModule::printIP(const uint8_t &ip_)
230{
231 std::stringstream out;
232 out << ((ip_ & 0xff000000) >> 24);
233 out << ".";
234 out << ((ip_ & 0x00ff0000) >> 16);
235 out << ".";
236 out << ((ip_ & 0x0000ff00) >> 8);
237 out << ".";
238 out << ((ip_ & 0x000000ff));
239 return out.str();
240}
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 ~UWIPModule()
Destructor of UWIPModule class.
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:70
uint8_t & daddr()
Reference to the daddr_ variable.
uint8_t & saddr()
Reference to the saddr_ variable.
Definition uwip-module.h:95
static int offset_
Required by the PacketHeaderManager.
Definition uwip-module.h:74
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:62
#define ORIGINATED_BY_ME
Reason for a drop in a UWIP module.
Definition uwip-module.h:53
#define NOT_FOR_ME_REASON
Reason for a drop in a UWIP module.
Definition uwip-module.h:49
#define INVALID_DESTINATION_ADDR
Reason for a drop in a UWIP module.
Definition uwip-module.h:55
#define DESTINATION_ADDR_UNSET
Reason for a drop in a UWIP module.
Definition uwip-module.h:51
#define HDR_UWIP(P)
Definition uwip-module.h:58