A Discrete-Event Network Simulator
API
inet-socket-address.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 
20 #include "inet-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("InetSocketAddress");
29 
31  : m_ipv4(ipv4),
32  m_port(port),
33  m_tos(0)
34 {
35  NS_LOG_FUNCTION(this << ipv4 << port);
36 }
37 
39  : m_ipv4(ipv4),
40  m_port(0),
41  m_tos(0)
42 {
43  NS_LOG_FUNCTION(this << ipv4);
44 }
45 
47  : m_ipv4(Ipv4Address(ipv4)),
48  m_port(port),
49  m_tos(0)
50 {
51  NS_LOG_FUNCTION(this << ipv4 << port);
52 }
53 
55  : m_ipv4(Ipv4Address(ipv4)),
56  m_port(0),
57  m_tos(0)
58 {
59  NS_LOG_FUNCTION(this << ipv4);
60 }
61 
63  : m_ipv4(Ipv4Address::GetAny()),
64  m_port(port),
65  m_tos(0)
66 {
67  NS_LOG_FUNCTION(this << port);
68 }
69 
70 uint16_t
72 {
73  NS_LOG_FUNCTION(this);
74  return m_port;
75 }
76 
79 {
80  NS_LOG_FUNCTION(this);
81  return m_ipv4;
82 }
83 
84 uint8_t
86 {
87  NS_LOG_FUNCTION(this);
88  return m_tos;
89 }
90 
91 void
93 {
94  NS_LOG_FUNCTION(this << port);
95  m_port = port;
96 }
97 
98 void
100 {
101  NS_LOG_FUNCTION(this << address);
102  m_ipv4 = address;
103 }
104 
105 void
107 {
108  NS_LOG_FUNCTION(this << tos);
109  m_tos = tos;
110 }
111 
112 bool
114 {
116  return address.CheckCompatible(GetType(), 7);
117 }
118 
119 InetSocketAddress::operator Address() const
120 {
121  return ConvertTo();
122 }
123 
124 Address
126 {
127  NS_LOG_FUNCTION(this);
128  uint8_t buf[7];
129  m_ipv4.Serialize(buf);
130  buf[4] = m_port & 0xff;
131  buf[5] = (m_port >> 8) & 0xff;
132  buf[6] = m_tos;
133  return Address(GetType(), buf, 7);
134 }
135 
138 {
140  NS_ASSERT(address.CheckCompatible(GetType(), 7));
141  uint8_t buf[7];
142  address.CopyTo(buf);
144  uint16_t port = buf[4] | (buf[5] << 8);
145  uint8_t tos = buf[6];
146  InetSocketAddress inet(ipv4, port);
147  inet.SetTos(tos);
148  return inet;
149 }
150 
151 uint8_t
153 {
155  static uint8_t type = Address::Register();
156  return type;
157 }
158 
159 } // namespace ns3
a polymophic address class
Definition: address.h:101
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition: address.cc:146
an Inet address class
void SetPort(uint16_t port)
static uint8_t GetType()
Get the underlying address type (automatically assigned).
Address ConvertTo() const
Convert to an Address type.
void SetIpv4(Ipv4Address address)
static bool IsMatchingType(const Address &address)
InetSocketAddress(Ipv4Address ipv4, uint16_t port)
Ipv4Address m_ipv4
the IPv4 address
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.
static Ipv4Address Deserialize(const uint8_t buf[4])
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 ",...
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.