A Discrete-Event Network Simulator
API
packet-socket-address.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 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 #include "packet-socket-address.h"
21 #include "ns3/net-device.h"
22 #include "ns3/log.h"
23 
24 namespace ns3 {
25 
26 NS_LOG_COMPONENT_DEFINE ("PacketSocketAddress");
27 
29 {
30  NS_LOG_FUNCTION (this);
31 }
32 void
34 {
35  NS_LOG_FUNCTION (this << protocol);
36  m_protocol = protocol;
37 }
38 void
40 {
41  NS_LOG_FUNCTION (this);
42  m_isSingleDevice = false;
43  m_device = 0;
44 }
45 void
47 {
48  NS_LOG_FUNCTION (this << index);
49  m_isSingleDevice = true;
50  m_device = index;
51 }
52 void
54 {
55  NS_LOG_FUNCTION (this << address);
57 }
58 
59 uint16_t
61 {
62  NS_LOG_FUNCTION (this);
63  return m_protocol;
64 }
65 bool
67 {
68  NS_LOG_FUNCTION (this);
69  return m_isSingleDevice;
70 }
71 uint32_t
73 {
74  NS_LOG_FUNCTION (this);
75  return m_device;
76 }
77 Address
79 {
80  NS_LOG_FUNCTION (this);
81  return m_address;
82 }
83 
84 PacketSocketAddress::operator Address () const
85 {
86  return ConvertTo ();
87 }
88 
89 Address
91 {
92  NS_LOG_FUNCTION (this);
94  uint8_t buffer[Address::MAX_SIZE];
95  buffer[0] = m_protocol & 0xff;
96  buffer[1] = (m_protocol >> 8) & 0xff;
97  buffer[2] = (m_device >> 24) & 0xff;
98  buffer[3] = (m_device >> 16) & 0xff;
99  buffer[4] = (m_device >> 8) & 0xff;
100  buffer[5] = (m_device >> 0) & 0xff;
101  buffer[6] = m_isSingleDevice ? 1 : 0;
102  uint32_t copied = m_address.CopyAllTo (buffer + 7, Address::MAX_SIZE - 7);
103  return Address (GetType (), buffer, 7 + copied);
104 }
107 {
110  uint8_t buffer[Address::MAX_SIZE];
111  address.CopyTo (buffer);
112  uint16_t protocol = buffer[0] | (buffer[1] << 8);
113  uint32_t device = 0;
114  device |= buffer[2];
115  device <<= 8;
116  device |= buffer[3];
117  device <<= 8;
118  device |= buffer[4];
119  device <<= 8;
120  device |= buffer[5];
121  bool isSingleDevice = (buffer[6] == 1);
122  Address physical;
123  physical.CopyAllFrom (buffer + 7, Address::MAX_SIZE - 7);
125  ad.SetProtocol (protocol);
126  if (isSingleDevice)
127  {
128  ad.SetSingleDevice (device);
129  }
130  else
131  {
132  ad.SetAllDevices ();
133  }
134  ad.SetPhysicalAddress (physical);
135  return ad;
136 }
137 bool
139 {
141  return address.IsMatchingType (GetType ());
142 }
143 uint8_t
145 {
147  static uint8_t type = Address::Register ();
148  return type;
149 }
150 
151 } // namespace ns3
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
NS_ASSERT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::PacketSocketAddress::GetProtocol
uint16_t GetProtocol(void) const
Get the protocol.
Definition: packet-socket-address.cc:60
ns3::Address::Register
static uint8_t Register(void)
Allocate a new type id for a new type of address.
Definition: address.cc:138
ns3::PacketSocketAddress
an address for a packet socket
Definition: packet-socket-address.h:39
ns3::PacketSocketAddress::SetAllDevices
void SetAllDevices(void)
Set the address to match all the outgoing NetDevice.
Definition: packet-socket-address.cc:39
ns3::PacketSocketAddress::IsMatchingType
static bool IsMatchingType(const Address &address)
Definition: packet-socket-address.cc:138
ns3::PacketSocketAddress::m_isSingleDevice
bool m_isSingleDevice
True if directed to a specific outgoing NetDevice.
Definition: packet-socket-address.h:125
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::PacketSocketAddress::PacketSocketAddress
PacketSocketAddress()
Definition: packet-socket-address.cc:28
ns3::PacketSocketAddress::m_address
Address m_address
Destination address.
Definition: packet-socket-address.h:127
first.address
address
Definition: first.py:44
ns3::PacketSocketAddress::m_device
uint32_t m_device
Outgoing NetDevice index.
Definition: packet-socket-address.h:126
ns3::PacketSocketAddress::GetSingleDevice
uint32_t GetSingleDevice(void) const
Get the device this address is bound to.
Definition: packet-socket-address.cc:72
ns3::PacketSocketAddress::GetPhysicalAddress
Address GetPhysicalAddress(void) const
Get the destination address.
Definition: packet-socket-address.cc:78
ns3::PacketSocketAddress::SetSingleDevice
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Definition: packet-socket-address.cc:46
ns3::PacketSocketAddress::ConvertTo
Address ConvertTo(void) const
Convert an instance of this class to a polymorphic Address instance.
Definition: packet-socket-address.cc:90
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log-macros-enabled.h:209
ns3::Address::CopyAllTo
uint32_t CopyAllTo(uint8_t *buffer, uint8_t len) const
Definition: address.cc:90
packet-socket-address.h
ns3::PacketSocketAddress::SetPhysicalAddress
void SetPhysicalAddress(const Address address)
Set the destination address.
Definition: packet-socket-address.cc:53
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::PacketSocketAddress::m_protocol
uint16_t m_protocol
Protocol.
Definition: packet-socket-address.h:124
ns3::PacketSocketAddress::SetProtocol
void SetProtocol(uint16_t protocol)
Set the protocol.
Definition: packet-socket-address.cc:33
ns3::PacketSocketAddress::IsSingleDevice
bool IsSingleDevice(void) const
Checks if the address is bound to a specified NetDevice.
Definition: packet-socket-address.cc:66
ns3::PacketSocketAddress::GetType
static uint8_t GetType(void)
Return the Type of address.
Definition: packet-socket-address.cc:144
ns3::Address::MAX_SIZE
@ MAX_SIZE
Definition: address.h:98
ns3::PacketSocketAddress::ConvertFrom
static PacketSocketAddress ConvertFrom(const Address &address)
Definition: packet-socket-address.cc:106
ns3::Address::CopyAllFrom
uint32_t CopyAllFrom(const uint8_t *buffer, uint8_t len)
Definition: address.cc:110