A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
inet-socket-address.cc
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
98 InetSocketAddress::ConvertTo (void) const
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 }
107 InetSocketAddress
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
119 InetSocketAddress::GetType (void)
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)
Ipv4Address GetIpv4(void) const
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
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
static InetSocketAddress ConvertFrom(const Address &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
Definition: address.cc:82
uint16_t GetPort(void) const
static uint8_t Register(void)
Definition: address.cc:137
static bool IsMatchingType(const Address &address)