A Discrete-Event Network Simulator
API
ipv6-interface-address.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007-2009 Strasbourg University
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: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  */
20 
21 #include <iostream>
22 
23 #include "ns3/log.h"
24 #include "ns3/assert.h"
25 
26 #include "ipv6-interface-address.h"
27 
28 namespace ns3
29 {
30 
31 NS_LOG_COMPONENT_DEFINE ("Ipv6InterfaceAddress");
32 
34  : m_address (Ipv6Address ()),
35  m_prefix (Ipv6Prefix ()),
36  m_state (TENTATIVE_OPTIMISTIC),
37  m_scope (HOST),
38  m_onLink (true),
39  m_nsDadUid (0)
40 {
41  NS_LOG_FUNCTION (this);
42 }
43 
45 {
46  NS_LOG_FUNCTION (this << address);
47  m_prefix = Ipv6Prefix (64);
50  m_onLink = true;
51  m_nsDadUid = 0;
52 }
53 
55 {
56  NS_LOG_FUNCTION (this << address << prefix);
57  m_prefix = prefix;
60  m_onLink = true;
61  m_nsDadUid = 0;
62 }
63 
65 {
66  NS_LOG_FUNCTION (this << address << prefix << onLink);
67  m_prefix = prefix;
70  m_onLink = onLink;
71  m_nsDadUid = 0;
72 }
73 
75  : m_address (o.m_address),
76  m_prefix (o.m_prefix),
77  m_state (o.m_state),
78  m_scope (o.m_scope),
79  m_onLink (o.m_onLink),
80  m_nsDadUid (o.m_nsDadUid)
81 {
82 }
83 
85 {
86  NS_LOG_FUNCTION (this);
87 }
88 
90 {
91  NS_LOG_FUNCTION (this);
92  return m_address;
93 }
94 
96 {
97  NS_LOG_FUNCTION (this << address);
99 
100  if (address.IsLocalhost ())
101  {
102  m_scope = HOST;
103  /* localhost address is always /128 prefix */
104  m_prefix = Ipv6Prefix (128);
105  }
106  else if (address.IsLinkLocal ())
107  {
108  m_scope = LINKLOCAL;
109  /* link-local address is always /64 prefix */
110  m_prefix = Ipv6Prefix (64);
111  }
112  else if (address.IsLinkLocalMulticast ())
113  {
114  m_scope = LINKLOCAL;
115  /* link-local multicast address is always /16 prefix */
116  m_prefix = Ipv6Prefix (16);
117  }
118  else
119  {
120  m_scope = GLOBAL;
121  }
122 }
123 
125 {
126  NS_LOG_FUNCTION (this);
127  return m_prefix;
128 }
129 
131 {
132  NS_LOG_FUNCTION (this << state);
133  m_state = state;
134 }
135 
137 {
138  NS_LOG_FUNCTION (this);
139  return m_state;
140 }
141 
143 {
144  NS_LOG_FUNCTION (this << scope);
145  m_scope = scope;
146 }
147 
149 {
150  NS_LOG_FUNCTION (this);
151  return m_scope;
152 }
153 
155 {
156  NS_LOG_FUNCTION (this);
157 
158  Ipv6Address aAddr = m_address;
159  aAddr = aAddr.CombinePrefix (m_prefix);
160  Ipv6Address bAddr = b;
161  bAddr = bAddr.CombinePrefix (m_prefix);
162 
163  if (aAddr == bAddr)
164  {
165  return true;
166  }
167 
168  if ((bAddr.IsLinkLocalMulticast () && aAddr.IsLinkLocal ()) ||
169  (aAddr.IsLinkLocalMulticast () && bAddr.IsLinkLocal ()))
170  {
171  return true;
172  }
173 
174  return false;
175 }
176 
177 std::ostream& operator<< (std::ostream& os, const Ipv6InterfaceAddress &addr)
178 {
179  os << "address: " << addr.GetAddress () << addr.GetPrefix () << "; scope: ";
180  switch (addr.GetScope ())
181  {
183  os << "HOST";
184  break;
186  os << "LINK-LOCAL";
187  break;
189  os << "GLOBAL";
190  break;
191  default:
192  os << "UNKNOWN";
193  break;
194  }
195  return os;
196 }
197 
199 {
200  NS_LOG_FUNCTION (this);
201  return m_nsDadUid;
202 }
203 
204 void Ipv6InterfaceAddress::SetNsDadUid (uint32_t nsDadUid)
205 {
206  NS_LOG_FUNCTION (this << nsDadUid);
207  m_nsDadUid = nsDadUid;
208 }
209 
211 {
212  NS_LOG_FUNCTION (this);
213  return m_onLink;
214 }
215 
217 {
218  NS_LOG_FUNCTION (this << onLink);
219  m_onLink = onLink;
220 }
221 
222 #if 0
223 void Ipv6InterfaceAddress::StartDadTimer (Ptr<Ipv6Interface> interface)
224 {
225  NS_LOG_FUNCTION (this << interface);
226  m_dadTimer.SetFunction (&Icmpv6L4Protocol::FunctionDadTimeout);
227  m_dadTimer.SetArguments (interface, m_address);
228  m_dadTimer.Schedule (Seconds (1));
230 }
231 
232 void Ipv6InterfaceAddress::StopDadTimer ()
233 {
234  NS_LOG_FUNCTION (this);
235  m_dadTimer.Cancel ();
236  Simulator::Cancel (m_dadId);
237 }
238 #endif
239 
240 } /* namespace ns3 */
241 
void FunctionDadTimeout(Ipv6Interface *interface, Ipv6Address addr)
Function called when DAD timeout.
Describes an IPv6 address.
Definition: ipv6-address.h:50
bool IsLinkLocal() const
If the IPv6 address is a link-local address (fe80::/64).
Ipv6Address CombinePrefix(Ipv6Prefix const &prefix) const
Combine this address with a prefix.
bool IsLinkLocalMulticast() const
If the IPv6 address is link-local multicast (ff02::/16).
IPv6 address associated with an interface.
void SetScope(Ipv6InterfaceAddress::Scope_e scope)
Set the scope.
Ipv6Address m_address
The IPv6 address.
uint32_t m_nsDadUid
Last DAD probe packet UID.
void SetAddress(Ipv6Address address)
Set IPv6 address (and scope).
Ipv6Address GetAddress() const
Get the IPv6 address.
Ipv6InterfaceAddress()
Default constructor.
uint32_t GetNsDadUid() const
Get the latest DAD probe packet UID.
void SetState(Ipv6InterfaceAddress::State_e state)
Set the state.
Ipv6InterfaceAddress::Scope_e GetScope() const
Get address scope.
void SetNsDadUid(uint32_t uid)
Set the latest DAD probe packet UID.
Ipv6Prefix GetPrefix() const
Get the IPv6 prefix.
bool GetOnLink() const
Get the on-link property.
void SetOnLink(bool onLink)
Get the on-link property.
Ipv6InterfaceAddress::State_e GetState() const
Get the address state.
bool m_onLink
The address belongs to an on-link network.
State_e
State of an address associated with an interface.
@ TENTATIVE_OPTIMISTIC
Address is tentative but we are optimistic so we can send packet even if DAD is not yet finished.
bool IsInSameSubnet(Ipv6Address b) const
Checks if the address is in the same subnet.
Scope_e m_scope
Scope of the address.
State_e m_state
State of the address.
Ipv6Prefix m_prefix
The IPv6 prefix.
@ LINKLOCAL
Link-local address (fe80::/64)
@ GLOBAL
Global address (2000::/3)
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition: simulator.cc:268
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139