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 
Ipv6InterfaceAddress()33 Ipv6InterfaceAddress::Ipv6InterfaceAddress ()
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 
Ipv6InterfaceAddress(Ipv6Address address)44 Ipv6InterfaceAddress::Ipv6InterfaceAddress (Ipv6Address address)
45 {
46   NS_LOG_FUNCTION (this << address);
47   m_prefix = Ipv6Prefix (64);
48   SetAddress (address);
49   SetState (TENTATIVE_OPTIMISTIC);
50   m_onLink = true;
51   m_nsDadUid = 0;
52 }
53 
Ipv6InterfaceAddress(Ipv6Address address,Ipv6Prefix prefix)54 Ipv6InterfaceAddress::Ipv6InterfaceAddress (Ipv6Address address, Ipv6Prefix prefix)
55 {
56   NS_LOG_FUNCTION (this << address << prefix);
57   m_prefix = prefix;
58   SetAddress (address);
59   SetState (TENTATIVE_OPTIMISTIC);
60   m_onLink = true;
61   m_nsDadUid = 0;
62 }
63 
Ipv6InterfaceAddress(Ipv6Address address,Ipv6Prefix prefix,bool onLink)64 Ipv6InterfaceAddress::Ipv6InterfaceAddress (Ipv6Address address, Ipv6Prefix prefix, bool onLink)
65 {
66   NS_LOG_FUNCTION (this << address << prefix << onLink);
67   m_prefix = prefix;
68   SetAddress (address);
69   SetState (TENTATIVE_OPTIMISTIC);
70   m_onLink = onLink;
71   m_nsDadUid = 0;
72 }
73 
Ipv6InterfaceAddress(const Ipv6InterfaceAddress & o)74 Ipv6InterfaceAddress::Ipv6InterfaceAddress (const Ipv6InterfaceAddress& o)
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 
~Ipv6InterfaceAddress()84 Ipv6InterfaceAddress::~Ipv6InterfaceAddress ()
85 {
86   NS_LOG_FUNCTION (this);
87 }
88 
GetAddress() const89 Ipv6Address Ipv6InterfaceAddress::GetAddress () const
90 {
91   NS_LOG_FUNCTION (this);
92   return m_address;
93 }
94 
SetAddress(Ipv6Address address)95 void Ipv6InterfaceAddress::SetAddress (Ipv6Address address)
96 {
97   NS_LOG_FUNCTION (this << address);
98   m_address = 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 
GetPrefix() const124 Ipv6Prefix Ipv6InterfaceAddress::GetPrefix () const
125 {
126   NS_LOG_FUNCTION (this);
127   return m_prefix;
128 }
129 
SetState(Ipv6InterfaceAddress::State_e state)130 void Ipv6InterfaceAddress::SetState (Ipv6InterfaceAddress::State_e state)
131 {
132   NS_LOG_FUNCTION (this << state);
133   m_state = state;
134 }
135 
GetState() const136 Ipv6InterfaceAddress::State_e Ipv6InterfaceAddress::GetState () const
137 {
138   NS_LOG_FUNCTION (this);
139   return m_state;
140 }
141 
SetScope(Ipv6InterfaceAddress::Scope_e scope)142 void Ipv6InterfaceAddress::SetScope (Ipv6InterfaceAddress::Scope_e scope)
143 {
144   NS_LOG_FUNCTION (this << scope);
145   m_scope = scope;
146 }
147 
GetScope() const148 Ipv6InterfaceAddress::Scope_e Ipv6InterfaceAddress::GetScope () const
149 {
150   NS_LOG_FUNCTION (this);
151   return m_scope;
152 }
153 
IsInSameSubnet(Ipv6Address b) const154 bool Ipv6InterfaceAddress::IsInSameSubnet (Ipv6Address b) const
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 
operator <<(std::ostream & os,const Ipv6InterfaceAddress & addr)177 std::ostream& operator<< (std::ostream& os, const Ipv6InterfaceAddress &addr)
178 {
179   os << "address: " << addr.GetAddress () << addr.GetPrefix () << "; scope: ";
180   switch (addr.GetScope ())
181   {
182     case Ipv6InterfaceAddress::HOST:
183       os << "HOST";
184       break;
185     case Ipv6InterfaceAddress::LINKLOCAL:
186       os << "LINK-LOCAL";
187       break;
188     case Ipv6InterfaceAddress::GLOBAL:
189       os << "GLOBAL";
190       break;
191     default:
192       os << "UNKNOWN";
193       break;
194   }
195   return os;
196 }
197 
GetNsDadUid() const198 uint32_t Ipv6InterfaceAddress::GetNsDadUid () const
199 {
200   NS_LOG_FUNCTION (this);
201   return m_nsDadUid;
202 }
203 
SetNsDadUid(uint32_t nsDadUid)204 void Ipv6InterfaceAddress::SetNsDadUid (uint32_t nsDadUid)
205 {
206   NS_LOG_FUNCTION (this << nsDadUid);
207   m_nsDadUid = nsDadUid;
208 }
209 
GetOnLink() const210 bool Ipv6InterfaceAddress::GetOnLink () const
211 {
212   NS_LOG_FUNCTION (this);
213   return m_onLink;
214 }
215 
SetOnLink(bool onLink)216 void Ipv6InterfaceAddress::SetOnLink (bool onLink)
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));
229   m_dadId = Simulator::Schedule (Seconds (1.), &Icmpv6L4Protocol::FunctionDadTimeout, interface, m_address);
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 
242