A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
packet-socket-address.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 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 */
20
21#include "ns3/log.h"
22#include "ns3/net-device.h"
23
24namespace ns3
25{
26
27NS_LOG_COMPONENT_DEFINE("PacketSocketAddress");
28
30{
31 NS_LOG_FUNCTION(this);
32}
33
34void
36{
37 NS_LOG_FUNCTION(this << protocol);
38 m_protocol = protocol;
39}
40
41void
43{
44 NS_LOG_FUNCTION(this);
45 m_isSingleDevice = false;
46 m_device = 0;
47}
48
49void
51{
52 NS_LOG_FUNCTION(this << index);
53 m_isSingleDevice = true;
54 m_device = index;
55}
56
57void
59{
60 NS_LOG_FUNCTION(this << address);
61 m_address = address;
62}
63
64uint16_t
66{
67 NS_LOG_FUNCTION(this);
68 return m_protocol;
69}
70
71bool
73{
74 NS_LOG_FUNCTION(this);
75 return m_isSingleDevice;
76}
77
80{
81 NS_LOG_FUNCTION(this);
82 return m_device;
83}
84
87{
88 NS_LOG_FUNCTION(this);
89 return m_address;
90}
91
92PacketSocketAddress::operator Address() const
93{
94 return ConvertTo();
95}
96
99{
100 NS_LOG_FUNCTION(this);
101 Address address;
102 uint8_t buffer[Address::MAX_SIZE];
103 buffer[0] = m_protocol & 0xff;
104 buffer[1] = (m_protocol >> 8) & 0xff;
105 buffer[2] = (m_device >> 24) & 0xff;
106 buffer[3] = (m_device >> 16) & 0xff;
107 buffer[4] = (m_device >> 8) & 0xff;
108 buffer[5] = (m_device >> 0) & 0xff;
109 buffer[6] = m_isSingleDevice ? 1 : 0;
110 uint32_t copied = m_address.CopyAllTo(buffer + 7, Address::MAX_SIZE - 7);
111 return Address(GetType(), buffer, 7 + copied);
112}
113
116{
117 NS_LOG_FUNCTION(address);
118 NS_ASSERT(IsMatchingType(address));
119 uint8_t buffer[Address::MAX_SIZE];
120 address.CopyTo(buffer);
121 uint16_t protocol = buffer[0] | (buffer[1] << 8);
122 uint32_t device = 0;
123 device |= buffer[2];
124 device <<= 8;
125 device |= buffer[3];
126 device <<= 8;
127 device |= buffer[4];
128 device <<= 8;
129 device |= buffer[5];
130 bool isSingleDevice = (buffer[6] == 1);
131 Address physical;
132 physical.CopyAllFrom(buffer + 7, Address::MAX_SIZE - 7);
134 ad.SetProtocol(protocol);
135 if (isSingleDevice)
136 {
137 ad.SetSingleDevice(device);
138 }
139 else
140 {
141 ad.SetAllDevices();
142 }
143 ad.SetPhysicalAddress(physical);
144 return ad;
145}
146
147bool
149{
150 NS_LOG_FUNCTION(address);
151 return address.IsMatchingType(GetType());
152}
153
154uint8_t
156{
158 static uint8_t type = Address::Register();
159 return type;
160}
161
162} // namespace ns3
a polymophic address class
Definition: address.h:101
static constexpr uint32_t MAX_SIZE
The maximum size of a byte buffer which can be stored in an Address instance.
Definition: address.h:107
uint32_t CopyAllFrom(const uint8_t *buffer, uint8_t len)
Definition: address.cc:116
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition: address.cc:146
uint32_t CopyAllTo(uint8_t *buffer, uint8_t len) const
Definition: address.cc:95
an address for a packet socket
Address ConvertTo() const
Convert an instance of this class to a polymorphic Address instance.
uint32_t GetSingleDevice() const
Get the device this address is bound to.
bool IsSingleDevice() const
Checks if the address is bound to a specified NetDevice.
Address GetPhysicalAddress() const
Get the destination address.
Address m_address
Destination address.
uint32_t m_device
Outgoing NetDevice index.
void SetProtocol(uint16_t protocol)
Set the protocol.
static bool IsMatchingType(const Address &address)
void SetPhysicalAddress(const Address address)
Set the destination address.
bool m_isSingleDevice
True if directed to a specific outgoing NetDevice.
static PacketSocketAddress ConvertFrom(const Address &address)
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
static uint8_t GetType()
Return the Type of address.
void SetAllDevices()
Set the address to match all the outgoing NetDevice.
uint16_t GetProtocol() const
Get the protocol.
#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 ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.