A Discrete-Event Network Simulator
API
inet-socket-address.cc
Go to the documentation of this file.
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 "inet-socket-address.h"
22 #include "ns3/assert.h"
23 #include "ns3/log.h"
24 
25 namespace ns3 {
26 
27 NS_LOG_COMPONENT_DEFINE ("InetSocketAddress");
28 
30  : m_ipv4 (ipv4),
31  m_port (port),
32  m_tos (0)
33 {
34  NS_LOG_FUNCTION (this << ipv4 << port);
35 }
37  : m_ipv4 (ipv4),
38  m_port (0),
39  m_tos (0)
40 {
41  NS_LOG_FUNCTION (this << ipv4);
42 }
43 InetSocketAddress::InetSocketAddress (const char *ipv4, uint16_t port)
44  : m_ipv4 (Ipv4Address (ipv4)),
45  m_port (port),
46  m_tos (0)
47 {
48  NS_LOG_FUNCTION (this << ipv4 << port);
49 }
51  : m_ipv4 (Ipv4Address (ipv4)),
52  m_port (0),
53  m_tos (0)
54 {
55  NS_LOG_FUNCTION (this << ipv4);
56 }
58  : m_ipv4 (Ipv4Address::GetAny ()),
59  m_port (port),
60  m_tos (0)
61 {
62  NS_LOG_FUNCTION (this << port);
63 }
64 uint16_t
66 {
67  NS_LOG_FUNCTION (this);
68  return m_port;
69 }
72 {
73  NS_LOG_FUNCTION (this);
74  return m_ipv4;
75 }
76 uint8_t
78 {
79  NS_LOG_FUNCTION (this);
80  return m_tos;
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION (this << port);
87  m_port = port;
88 }
89 void
91 {
92  NS_LOG_FUNCTION (this << address);
93  m_ipv4 = address;
94 }
95 void
97 {
98  NS_LOG_FUNCTION (this << tos);
99  m_tos = tos;
100 }
101 
102 bool
104 {
106  return address.CheckCompatible (GetType (), 7);
107 }
108 
109 InetSocketAddress::operator Address () const
110 {
111  return ConvertTo ();
112 }
113 
114 Address
116 {
117  NS_LOG_FUNCTION (this);
118  uint8_t buf[7];
119  m_ipv4.Serialize (buf);
120  buf[4] = m_port & 0xff;
121  buf[5] = (m_port >> 8) & 0xff;
122  buf[6] = m_tos;
123  return Address (GetType (), buf, 7);
124 }
127 {
129  NS_ASSERT (address.CheckCompatible (GetType (), 7));
130  uint8_t buf[7];
131  address.CopyTo (buf);
133  uint16_t port = buf[4] | (buf[5] << 8);
134  uint8_t tos = buf[6];
135  InetSocketAddress inet (ipv4, port);
136  inet.SetTos (tos);
137  return inet;
138 }
139 uint8_t
141 {
143  static uint8_t type = Address::Register ();
144  return type;
145 }
146 
147 } // namespace ns3
a polymophic address class
Definition: address.h:91
static uint8_t Register(void)
Allocate a new type id for a new type of address.
Definition: address.cc:138
an Inet address class
void SetPort(uint16_t port)
uint8_t GetTos(void) const
uint16_t GetPort(void) const
void SetIpv4(Ipv4Address address)
Ipv4Address GetIpv4(void) const
static bool IsMatchingType(const Address &address)
InetSocketAddress(Ipv4Address ipv4, uint16_t port)
Ipv4Address m_ipv4
the IPv4 address
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
static uint8_t GetType(void)
Get the underlying address type (automatically assigned).
Address ConvertTo(void) const
Convert to an Address type.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
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:45
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.