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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
9
10#include "ns3/log.h"
11#include "ns3/net-device.h"
12
13namespace ns3
14{
15
16NS_LOG_COMPONENT_DEFINE("PacketSocketAddress");
17
22
23void
25{
26 NS_LOG_FUNCTION(this << protocol);
27 m_protocol = protocol;
28}
29
30void
37
38void
40{
41 NS_LOG_FUNCTION(this << index);
42 m_isSingleDevice = true;
43 m_device = index;
44}
45
46void
48{
49 NS_LOG_FUNCTION(this << address);
50 m_address = address;
51}
52
53uint16_t
55{
56 NS_LOG_FUNCTION(this);
57 return m_protocol;
58}
59
60bool
66
73
80
81PacketSocketAddress::operator Address() const
82{
83 return ConvertTo();
84}
85
88{
89 NS_LOG_FUNCTION(this);
90 Address address;
91 uint8_t buffer[Address::MAX_SIZE];
92 buffer[0] = m_protocol & 0xff;
93 buffer[1] = (m_protocol >> 8) & 0xff;
94 buffer[2] = (m_device >> 24) & 0xff;
95 buffer[3] = (m_device >> 16) & 0xff;
96 buffer[4] = (m_device >> 8) & 0xff;
97 buffer[5] = (m_device >> 0) & 0xff;
98 buffer[6] = m_isSingleDevice ? 1 : 0;
99 uint32_t copied = m_address.CopyAllTo(buffer + 7, Address::MAX_SIZE - 7);
100 return Address(GetType(), buffer, 7 + copied);
101}
102
105{
106 NS_LOG_FUNCTION(address);
107 NS_ASSERT(IsMatchingType(address));
108 uint8_t buffer[Address::MAX_SIZE];
109 address.CopyTo(buffer);
110 uint16_t protocol = buffer[0] | (buffer[1] << 8);
111 uint32_t device = 0;
112 device |= buffer[2];
113 device <<= 8;
114 device |= buffer[3];
115 device <<= 8;
116 device |= buffer[4];
117 device <<= 8;
118 device |= buffer[5];
119 bool isSingleDevice = (buffer[6] == 1);
120 Address physical;
121 physical.CopyAllFrom(buffer + 7, Address::MAX_SIZE - 7);
123 ad.SetProtocol(protocol);
124 if (isSingleDevice)
125 {
126 ad.SetSingleDevice(device);
127 }
128 else
129 {
130 ad.SetAllDevices();
131 }
132 ad.SetPhysicalAddress(physical);
133 return ad;
134}
135
136bool
138{
139 NS_LOG_FUNCTION(address);
140 return address.IsMatchingType(GetType());
141}
142
143uint8_t
145{
147 static uint8_t type = Address::Register();
148 return type;
149}
150
151} // namespace ns3
a polymophic address class
Definition address.h:90
static constexpr uint32_t MAX_SIZE
The maximum size of a byte buffer which can be stored in an Address instance.
Definition address.h:96
uint32_t CopyAllFrom(const uint8_t *buffer, uint8_t len)
Definition address.cc:105
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition address.cc:135
uint32_t CopyAllTo(uint8_t *buffer, uint8_t len) const
Definition address.cc:84
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: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.