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::
82operator Address() const
83{
84 return ConvertTo();
85}
86
89{
90 NS_LOG_FUNCTION(this);
91 Address address;
92 uint8_t buffer[Address::MAX_SIZE];
93 buffer[0] = m_protocol & 0xff;
94 buffer[1] = (m_protocol >> 8) & 0xff;
95 buffer[2] = (m_device >> 24) & 0xff;
96 buffer[3] = (m_device >> 16) & 0xff;
97 buffer[4] = (m_device >> 8) & 0xff;
98 buffer[5] = (m_device >> 0) & 0xff;
99 buffer[6] = m_isSingleDevice ? 1 : 0;
100 uint32_t copied = m_address.CopyAllTo(buffer + 7, Address::MAX_SIZE - 7);
101 return Address(GetType(), buffer, 7 + copied);
102}
103
106{
107 NS_LOG_FUNCTION(address);
108 NS_ASSERT(IsMatchingType(address));
109 uint8_t buffer[Address::MAX_SIZE];
110 address.CopyTo(buffer);
111 uint16_t protocol = buffer[0] | (buffer[1] << 8);
112 uint32_t device = 0;
113 device |= buffer[2];
114 device <<= 8;
115 device |= buffer[3];
116 device <<= 8;
117 device |= buffer[4];
118 device <<= 8;
119 device |= buffer[5];
120 bool isSingleDevice = (buffer[6] == 1);
121 Address physical;
122 physical.CopyAllFrom(buffer + 7, Address::MAX_SIZE - 7);
124 ad.SetProtocol(protocol);
125 if (isSingleDevice)
126 {
127 ad.SetSingleDevice(device);
128 }
129 else
130 {
131 ad.SetAllDevices();
132 }
133 ad.SetPhysicalAddress(physical);
134 return ad;
135}
136
137bool
139{
140 NS_LOG_FUNCTION(address);
141 return address.IsMatchingType(GetType());
142}
143
144uint8_t
146{
148 static uint8_t type = Address::Register();
149 return type;
150}
151
152} // 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
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.