A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("InetSocketAddress");
26 
27 namespace ns3 {
28 
30  : m_ipv4 (ipv4),
31  m_port (port)
32 {
33  NS_LOG_FUNCTION (this << ipv4 << port);
34 }
36  : m_ipv4 (ipv4),
37  m_port (0)
38 {
39  NS_LOG_FUNCTION (this << ipv4);
40 }
41 InetSocketAddress::InetSocketAddress (const char *ipv4, uint16_t port)
42  : m_ipv4 (Ipv4Address (ipv4)),
43  m_port (port)
44 {
45  NS_LOG_FUNCTION (this << ipv4 << port);
46 }
48  : m_ipv4 (Ipv4Address (ipv4)),
49  m_port (0)
50 {
51  NS_LOG_FUNCTION (this << ipv4);
52 }
54  : m_ipv4 (Ipv4Address::GetAny ()),
55  m_port (port)
56 {
57  NS_LOG_FUNCTION (this << port);
58 }
59 uint16_t
61 {
62  NS_LOG_FUNCTION (this);
63  return m_port;
64 }
67 {
68  NS_LOG_FUNCTION (this);
69  return m_ipv4;
70 }
71 
72 void
74 {
75  NS_LOG_FUNCTION (this << port);
76  m_port = port;
77 }
78 void
80 {
81  NS_LOG_FUNCTION (this << address);
82  m_ipv4 = address;
83 }
84 
85 bool
87 {
88  NS_LOG_FUNCTION (&address);
89  return address.CheckCompatible (GetType (), 6);
90 }
91 
92 InetSocketAddress::operator Address () const
93 {
94  return ConvertTo ();
95 }
96 
97 Address
99 {
100  NS_LOG_FUNCTION (this);
101  uint8_t buf[6];
102  m_ipv4.Serialize (buf);
103  buf[4] = m_port & 0xff;
104  buf[5] = (m_port >> 8) & 0xff;
105  return Address (GetType (), buf, 6);
106 }
109 {
110  NS_LOG_FUNCTION (&address);
111  NS_ASSERT (address.CheckCompatible (GetType (), 6));
112  uint8_t buf[6];
113  address.CopyTo (buf);
115  uint16_t port = buf[4] | (buf[5] << 8);
116  return InetSocketAddress (ipv4, port);
117 }
118 uint8_t
120 {
122  static uint8_t type = Address::Register ();
123  return type;
124 }
125 
126 } // namespace ns3
static Ipv4Address Deserialize(const uint8_t buf[4])
InetSocketAddress(Ipv4Address ipv4, uint16_t port)
an Inet address class
Ipv4Address GetIpv4(void) const
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:86
bool CheckCompatible(uint8_t type, uint8_t len) const
Definition: address.cc:122
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
void SetPort(uint16_t port)
void SetIpv4(Ipv4Address address)
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition: address.cc:82
Address ConvertTo(void) const
Convert to an Address type.
uint16_t GetPort(void) const
tuple address
Definition: first.py:37
Ipv4Address m_ipv4
the IPv4 address
static uint8_t Register(void)
Allocate a new type id for a new type of address.
Definition: address.cc:138
static bool IsMatchingType(const Address &address)
static uint8_t GetType(void)
Get the underlying address type (automatically assigned).