A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
inet-socket-address.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
10
11#include "ns3/assert.h"
12#include "ns3/log.h"
13
14namespace ns3
15{
16
17NS_LOG_COMPONENT_DEFINE("InetSocketAddress");
18
20 : m_ipv4(ipv4),
21 m_port(port)
22{
23 NS_LOG_FUNCTION(this << ipv4 << port);
24}
25
27 : m_ipv4(ipv4),
28 m_port(0)
29{
30 NS_LOG_FUNCTION(this << ipv4);
31}
32
33InetSocketAddress::InetSocketAddress(const char* ipv4, uint16_t port)
34 : m_ipv4(Ipv4Address(ipv4)),
35 m_port(port)
36{
37 NS_LOG_FUNCTION(this << ipv4 << port);
38}
39
41 : m_ipv4(Ipv4Address(ipv4)),
42 m_port(0)
43{
44 NS_LOG_FUNCTION(this << ipv4);
45}
46
48 : m_ipv4(Ipv4Address::GetAny()),
49 m_port(port)
50{
51 NS_LOG_FUNCTION(this << port);
52}
53
54uint16_t
56{
57 NS_LOG_FUNCTION(this);
58 return m_port;
59}
60
63{
64 NS_LOG_FUNCTION(this);
65 return m_ipv4;
66}
67
68void
70{
71 NS_LOG_FUNCTION(this << port);
72 m_port = port;
73}
74
75void
77{
78 NS_LOG_FUNCTION(this << address);
79 m_ipv4 = address;
80}
81
82bool
84{
85 NS_LOG_FUNCTION(&address);
86 return address.CheckCompatible(GetType(), 6);
87}
88
89InetSocketAddress::operator Address() const
90{
91 return ConvertTo();
92}
93
96{
97 NS_LOG_FUNCTION(this);
98 uint8_t buf[6];
99 m_ipv4.Serialize(buf);
100 buf[4] = m_port & 0xff;
101 buf[5] = (m_port >> 8) & 0xff;
102 return Address(GetType(), buf, 6);
103}
104
107{
108 NS_LOG_FUNCTION(&address);
109 NS_ASSERT(address.CheckCompatible(GetType(), 6)); /* 4 (address) + 2 (port) */
110 uint8_t buf[6];
111 address.CopyTo(buf);
113 uint16_t port = buf[4] | (buf[5] << 8);
114 InetSocketAddress inet(ipv4, port);
115 return inet;
116}
117
118uint8_t
120{
122 static uint8_t type = Address::Register();
123 return type;
124}
125
126} // namespace ns3
a polymophic address class
Definition address.h:90
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition address.cc:135
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.
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:33
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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.