1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "ipv4-routing-table-entry.h"
22 #include "ns3/assert.h"
23 #include "ns3/log.h"
24 
25 namespace ns3 {
26 
27 NS_LOG_COMPONENT_DEFINE ("Ipv4RoutingTableEntry");
28 
29 /*****************************************************
30  *     Network Ipv4RoutingTableEntry
31  *****************************************************/
32 
Ipv4RoutingTableEntry()33 Ipv4RoutingTableEntry::Ipv4RoutingTableEntry ()
34 {
35   NS_LOG_FUNCTION (this);
36 }
37 
Ipv4RoutingTableEntry(Ipv4RoutingTableEntry const & route)38 Ipv4RoutingTableEntry::Ipv4RoutingTableEntry (Ipv4RoutingTableEntry const &route)
39   : m_dest (route.m_dest),
40     m_destNetworkMask (route.m_destNetworkMask),
41     m_gateway (route.m_gateway),
42     m_interface (route.m_interface)
43 {
44   NS_LOG_FUNCTION (this << route);
45 }
46 
Ipv4RoutingTableEntry(Ipv4RoutingTableEntry const * route)47 Ipv4RoutingTableEntry::Ipv4RoutingTableEntry (Ipv4RoutingTableEntry const *route)
48   : m_dest (route->m_dest),
49     m_destNetworkMask (route->m_destNetworkMask),
50     m_gateway (route->m_gateway),
51     m_interface (route->m_interface)
52 {
53   NS_LOG_FUNCTION (this << route);
54 }
55 
Ipv4RoutingTableEntry(Ipv4Address dest,Ipv4Address gateway,uint32_t interface)56 Ipv4RoutingTableEntry::Ipv4RoutingTableEntry (Ipv4Address dest,
57                                               Ipv4Address gateway,
58                                               uint32_t interface)
59   : m_dest (dest),
60     m_destNetworkMask (Ipv4Mask::GetOnes ()),
61     m_gateway (gateway),
62     m_interface (interface)
63 {
64 }
Ipv4RoutingTableEntry(Ipv4Address dest,uint32_t interface)65 Ipv4RoutingTableEntry::Ipv4RoutingTableEntry (Ipv4Address dest,
66                                               uint32_t interface)
67   : m_dest (dest),
68     m_destNetworkMask (Ipv4Mask::GetOnes ()),
69     m_gateway (Ipv4Address::GetZero ()),
70     m_interface (interface)
71 {
72 }
Ipv4RoutingTableEntry(Ipv4Address network,Ipv4Mask networkMask,Ipv4Address gateway,uint32_t interface)73 Ipv4RoutingTableEntry::Ipv4RoutingTableEntry (Ipv4Address network,
74                                               Ipv4Mask networkMask,
75                                               Ipv4Address gateway,
76                                               uint32_t interface)
77   : m_dest (network),
78     m_destNetworkMask (networkMask),
79     m_gateway (gateway),
80     m_interface (interface)
81 {
82   NS_LOG_FUNCTION (this << network << networkMask << gateway << interface);
83 }
Ipv4RoutingTableEntry(Ipv4Address network,Ipv4Mask networkMask,uint32_t interface)84 Ipv4RoutingTableEntry::Ipv4RoutingTableEntry (Ipv4Address network,
85                                               Ipv4Mask networkMask,
86                                               uint32_t interface)
87   : m_dest (network),
88     m_destNetworkMask (networkMask),
89     m_gateway (Ipv4Address::GetZero ()),
90     m_interface (interface)
91 {
92   NS_LOG_FUNCTION (this << network << networkMask << interface);
93 }
94 
95 bool
IsHost(void) const96 Ipv4RoutingTableEntry::IsHost (void) const
97 {
98   NS_LOG_FUNCTION (this);
99   if (m_destNetworkMask == Ipv4Mask::GetOnes ())
100     {
101       return true;
102     }
103   else
104     {
105       return false;
106     }
107 }
108 Ipv4Address
GetDest(void) const109 Ipv4RoutingTableEntry::GetDest (void) const
110 {
111   NS_LOG_FUNCTION (this);
112   return m_dest;
113 }
114 bool
IsNetwork(void) const115 Ipv4RoutingTableEntry::IsNetwork (void) const
116 {
117   NS_LOG_FUNCTION (this);
118   return !IsHost ();
119 }
120 bool
IsDefault(void) const121 Ipv4RoutingTableEntry::IsDefault (void) const
122 {
123   NS_LOG_FUNCTION (this);
124   if (m_dest == Ipv4Address::GetZero ())
125     {
126       return true;
127     }
128   else
129     {
130       return false;
131     }
132 }
133 Ipv4Address
GetDestNetwork(void) const134 Ipv4RoutingTableEntry::GetDestNetwork (void) const
135 {
136   NS_LOG_FUNCTION (this);
137   return m_dest;
138 }
139 Ipv4Mask
GetDestNetworkMask(void) const140 Ipv4RoutingTableEntry::GetDestNetworkMask (void) const
141 {
142   NS_LOG_FUNCTION (this);
143   return m_destNetworkMask;
144 }
145 bool
IsGateway(void) const146 Ipv4RoutingTableEntry::IsGateway (void) const
147 {
148   NS_LOG_FUNCTION (this);
149   if (m_gateway == Ipv4Address::GetZero ())
150     {
151       return false;
152     }
153   else
154     {
155       return true;
156     }
157 }
158 Ipv4Address
GetGateway(void) const159 Ipv4RoutingTableEntry::GetGateway (void) const
160 {
161   NS_LOG_FUNCTION (this);
162   return m_gateway;
163 }
164 uint32_t
GetInterface(void) const165 Ipv4RoutingTableEntry::GetInterface (void) const
166 {
167   NS_LOG_FUNCTION (this);
168   return m_interface;
169 }
170 
171 Ipv4RoutingTableEntry
CreateHostRouteTo(Ipv4Address dest,Ipv4Address nextHop,uint32_t interface)172 Ipv4RoutingTableEntry::CreateHostRouteTo (Ipv4Address dest,
173                                           Ipv4Address nextHop,
174                                           uint32_t interface)
175 {
176   NS_LOG_FUNCTION (dest << nextHop << interface);
177   return Ipv4RoutingTableEntry (dest, nextHop, interface);
178 }
179 Ipv4RoutingTableEntry
CreateHostRouteTo(Ipv4Address dest,uint32_t interface)180 Ipv4RoutingTableEntry::CreateHostRouteTo (Ipv4Address dest,
181                                           uint32_t interface)
182 {
183   NS_LOG_FUNCTION (dest << interface);
184   return Ipv4RoutingTableEntry (dest, interface);
185 }
186 Ipv4RoutingTableEntry
CreateNetworkRouteTo(Ipv4Address network,Ipv4Mask networkMask,Ipv4Address nextHop,uint32_t interface)187 Ipv4RoutingTableEntry::CreateNetworkRouteTo (Ipv4Address network,
188                                              Ipv4Mask networkMask,
189                                              Ipv4Address nextHop,
190                                              uint32_t interface)
191 {
192   NS_LOG_FUNCTION (network << networkMask << nextHop << interface);
193   return Ipv4RoutingTableEntry (network, networkMask,
194                                 nextHop, interface);
195 }
196 Ipv4RoutingTableEntry
CreateNetworkRouteTo(Ipv4Address network,Ipv4Mask networkMask,uint32_t interface)197 Ipv4RoutingTableEntry::CreateNetworkRouteTo (Ipv4Address network,
198                                              Ipv4Mask networkMask,
199                                              uint32_t interface)
200 {
201   NS_LOG_FUNCTION (network << networkMask << interface);
202   return Ipv4RoutingTableEntry (network, networkMask,
203                                 interface);
204 }
205 Ipv4RoutingTableEntry
CreateDefaultRoute(Ipv4Address nextHop,uint32_t interface)206 Ipv4RoutingTableEntry::CreateDefaultRoute (Ipv4Address nextHop,
207                                            uint32_t interface)
208 {
209   NS_LOG_FUNCTION (nextHop << interface);
210   return Ipv4RoutingTableEntry (Ipv4Address::GetZero (), Ipv4Mask::GetZero (), nextHop, interface);
211 }
212 
213 
operator <<(std::ostream & os,Ipv4RoutingTableEntry const & route)214 std::ostream& operator<< (std::ostream& os, Ipv4RoutingTableEntry const& route)
215 {
216   if (route.IsDefault ())
217     {
218       NS_ASSERT (route.IsGateway ());
219       os << "default out=" << route.GetInterface () << ", next hop=" << route.GetGateway ();
220     }
221   else if (route.IsHost ())
222     {
223       if (route.IsGateway ())
224         {
225           os << "host="<< route.GetDest () <<
226           ", out=" << route.GetInterface () <<
227           ", next hop=" << route.GetGateway ();
228         }
229       else
230         {
231           os << "host="<< route.GetDest () <<
232           ", out=" << route.GetInterface ();
233         }
234     }
235   else if (route.IsNetwork ())
236     {
237       if (route.IsGateway ())
238         {
239           os << "network=" << route.GetDestNetwork () <<
240           ", mask=" << route.GetDestNetworkMask () <<
241           ",out=" << route.GetInterface () <<
242           ", next hop=" << route.GetGateway ();
243         }
244       else
245         {
246           os << "network=" << route.GetDestNetwork () <<
247           ", mask=" << route.GetDestNetworkMask () <<
248           ",out=" << route.GetInterface ();
249         }
250     }
251   else
252     {
253       NS_ASSERT (false);
254     }
255   return os;
256 }
257 
operator ==(const Ipv4RoutingTableEntry a,const Ipv4RoutingTableEntry b)258 bool operator== (const Ipv4RoutingTableEntry a, const Ipv4RoutingTableEntry b)
259 {
260   return (a.GetDest () == b.GetDest () &&
261           a.GetDestNetworkMask () == b.GetDestNetworkMask () &&
262           a.GetGateway () == b.GetGateway () &&
263           a.GetInterface () == b.GetInterface ());
264 }
265 
266 /*****************************************************
267  *     Ipv4MulticastRoutingTableEntry
268  *****************************************************/
269 
Ipv4MulticastRoutingTableEntry()270 Ipv4MulticastRoutingTableEntry::Ipv4MulticastRoutingTableEntry ()
271 {
272   NS_LOG_FUNCTION (this);
273 }
274 
Ipv4MulticastRoutingTableEntry(Ipv4MulticastRoutingTableEntry const & route)275 Ipv4MulticastRoutingTableEntry::Ipv4MulticastRoutingTableEntry (Ipv4MulticastRoutingTableEntry const &route)
276   :
277     m_origin (route.m_origin),
278     m_group (route.m_group),
279     m_inputInterface (route.m_inputInterface),
280     m_outputInterfaces (route.m_outputInterfaces)
281 {
282   NS_LOG_FUNCTION (this << route);
283 }
284 
Ipv4MulticastRoutingTableEntry(Ipv4MulticastRoutingTableEntry const * route)285 Ipv4MulticastRoutingTableEntry::Ipv4MulticastRoutingTableEntry (Ipv4MulticastRoutingTableEntry const *route)
286   :
287     m_origin (route->m_origin),
288     m_group (route->m_group),
289     m_inputInterface (route->m_inputInterface),
290     m_outputInterfaces (route->m_outputInterfaces)
291 {
292   NS_LOG_FUNCTION (this << route);
293 }
294 
Ipv4MulticastRoutingTableEntry(Ipv4Address origin,Ipv4Address group,uint32_t inputInterface,std::vector<uint32_t> outputInterfaces)295 Ipv4MulticastRoutingTableEntry::Ipv4MulticastRoutingTableEntry (
296   Ipv4Address origin,
297   Ipv4Address group,
298   uint32_t inputInterface,
299   std::vector<uint32_t> outputInterfaces)
300 {
301   NS_LOG_FUNCTION (this << origin << group << inputInterface << &outputInterfaces);
302   m_origin = origin;
303   m_group = group;
304   m_inputInterface = inputInterface;
305   m_outputInterfaces = outputInterfaces;
306 }
307 
308 Ipv4Address
GetOrigin(void) const309 Ipv4MulticastRoutingTableEntry::GetOrigin (void) const
310 {
311   NS_LOG_FUNCTION (this);
312   return m_origin;
313 }
314 
315 Ipv4Address
GetGroup(void) const316 Ipv4MulticastRoutingTableEntry::GetGroup (void) const
317 {
318   NS_LOG_FUNCTION (this);
319   return m_group;
320 }
321 
322 uint32_t
GetInputInterface(void) const323 Ipv4MulticastRoutingTableEntry::GetInputInterface (void) const
324 {
325   NS_LOG_FUNCTION (this);
326   return m_inputInterface;
327 }
328 
329 uint32_t
GetNOutputInterfaces(void) const330 Ipv4MulticastRoutingTableEntry::GetNOutputInterfaces (void) const
331 {
332   NS_LOG_FUNCTION (this);
333   return m_outputInterfaces.size ();
334 }
335 
336 uint32_t
GetOutputInterface(uint32_t n) const337 Ipv4MulticastRoutingTableEntry::GetOutputInterface (uint32_t n) const
338 {
339   NS_LOG_FUNCTION (this << n);
340   NS_ASSERT_MSG (n < m_outputInterfaces.size (),
341                  "Ipv4MulticastRoutingTableEntry::GetOutputInterface (): index out of bounds");
342 
343   return m_outputInterfaces[n];
344 }
345 
346 std::vector<uint32_t>
GetOutputInterfaces(void) const347 Ipv4MulticastRoutingTableEntry::GetOutputInterfaces (void) const
348 {
349   NS_LOG_FUNCTION (this);
350   return m_outputInterfaces;
351 }
352 
353 Ipv4MulticastRoutingTableEntry
CreateMulticastRoute(Ipv4Address origin,Ipv4Address group,uint32_t inputInterface,std::vector<uint32_t> outputInterfaces)354 Ipv4MulticastRoutingTableEntry::CreateMulticastRoute (Ipv4Address origin,
355                                                       Ipv4Address group,
356                                                       uint32_t inputInterface,
357                                                       std::vector<uint32_t> outputInterfaces)
358 {
359   NS_LOG_FUNCTION (origin << group << inputInterface << outputInterfaces);
360   return Ipv4MulticastRoutingTableEntry (origin, group, inputInterface, outputInterfaces);
361 }
362 
363 std::ostream&
operator <<(std::ostream & os,Ipv4MulticastRoutingTableEntry const & route)364 operator<< (std::ostream& os, Ipv4MulticastRoutingTableEntry const& route)
365 {
366   os << "origin=" << route.GetOrigin () <<
367   ", group=" << route.GetGroup () <<
368   ", input interface=" << route.GetInputInterface () <<
369   ", output interfaces=";
370 
371   for (uint32_t i = 0; i < route.GetNOutputInterfaces (); ++i)
372     {
373       os << route.GetOutputInterface (i) << " ";
374 
375     }
376 
377   return os;
378 }
379 
operator ==(const Ipv4MulticastRoutingTableEntry a,const Ipv4MulticastRoutingTableEntry b)380 bool operator== (const Ipv4MulticastRoutingTableEntry a, const Ipv4MulticastRoutingTableEntry b)
381 {
382   return (a.GetOrigin () == b.GetOrigin () &&
383           a.GetGroup () == b.GetGroup () &&
384           a.GetInputInterface () == b.GetInputInterface () &&
385           a.GetOutputInterfaces () == b.GetOutputInterfaces ());
386 }
387 
388 } // namespace ns3
389