A Discrete-Event Network Simulator
API
inet6-socket-address.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007-2008 Louis Pasteur University
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
18  */
19 
20 #include "inet6-socket-address.h"
21 
22 #include "ns3/assert.h"
23 #include "ns3/log.h"
24 
25 namespace ns3
26 {
27 
28 NS_LOG_COMPONENT_DEFINE("Inet6SocketAddress");
29 
31  : m_ipv6(ipv6),
32  m_port(port)
33 {
34  NS_LOG_FUNCTION(this << ipv6 << port);
35 }
36 
38  : m_ipv6(ipv6),
39  m_port(0)
40 {
41  NS_LOG_FUNCTION(this << ipv6);
42 }
43 
44 Inet6SocketAddress::Inet6SocketAddress(const char* ipv6, uint16_t port)
45  : m_ipv6(Ipv6Address(ipv6)),
46  m_port(port)
47 {
48  NS_LOG_FUNCTION(this << ipv6 << port);
49 }
50 
52  : m_ipv6(Ipv6Address(ipv6)),
53  m_port(0)
54 {
55  NS_LOG_FUNCTION(this << ipv6);
56 }
57 
59  : m_ipv6(Ipv6Address::GetAny()),
60  m_port(port)
61 {
62  NS_LOG_FUNCTION(this << port);
63 }
64 
65 uint16_t
67 {
68  NS_LOG_FUNCTION(this);
69  return m_port;
70 }
71 
72 void
74 {
75  NS_LOG_FUNCTION(this << port);
76  m_port = port;
77 }
78 
81 {
82  NS_LOG_FUNCTION(this);
83  return m_ipv6;
84 }
85 
86 void
88 {
89  NS_LOG_FUNCTION(this << ipv6);
90  m_ipv6 = ipv6;
91 }
92 
93 bool
95 {
96  NS_LOG_FUNCTION(&addr);
97  return addr.CheckCompatible(GetType(), 18); /* 16 (address) + 2 (port) */
98 }
99 
100 Inet6SocketAddress::operator Address() const
101 {
102  return ConvertTo();
103 }
104 
105 Address
107 {
108  NS_LOG_FUNCTION(this);
109  uint8_t buf[18];
110  m_ipv6.Serialize(buf);
111  buf[16] = m_port & 0xff;
112  buf[17] = (m_port >> 8) & 0xff;
113  return Address(GetType(), buf, 18);
114 }
115 
118 {
119  NS_LOG_FUNCTION(&addr);
120  NS_ASSERT(addr.CheckCompatible(GetType(), 18));
121  uint8_t buf[18];
122  addr.CopyTo(buf);
124  uint16_t port = buf[16] | (buf[17] << 8);
125  return Inet6SocketAddress(ipv6, port);
126 }
127 
128 uint8_t
130 {
132  static uint8_t type = Address::Register();
133  return type;
134 }
135 
136 } /* namespace ns3 */
a polymophic address class
Definition: address.h:101
bool CheckCompatible(uint8_t type, uint8_t len) const
Definition: address.cc:129
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition: address.cc:146
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition: address.cc:86
An Inet6 address class.
static uint8_t GetType()
Get the type.
void SetPort(uint16_t port)
Set the port.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
uint16_t GetPort() const
Get the port.
Inet6SocketAddress(Ipv6Address ipv6, uint16_t port)
Constructor.
void SetIpv6(Ipv6Address ipv6)
Set the IPv6 address.
static bool IsMatchingType(const Address &addr)
If the address match.
Ipv6Address GetIpv6() const
Get the IPv6 address.
Address ConvertTo() const
Convert to Address.
Ipv6Address m_ipv6
The IPv6 address.
Describes an IPv6 address.
Definition: ipv6-address.h:49
static Ipv6Address Deserialize(const uint8_t buf[16])
Deserialize this address.
void Serialize(uint8_t buf[16]) const
Serialize this address to a 16-byte buffer.
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.