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) ? true : false;
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
Address ConvertTo(void) const
Convert an instance of this class to a polymorphic Address instance.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
an address for a packet socket
#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
uint32_t CopyAllTo(uint8_t *buffer, uint8_t len) const
Definition: address.cc:90
static bool IsMatchingType(const Address &address)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
void SetAllDevices(void)
Set the address to match all the outgoing NetDevice.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
a polymophic address class
Definition: address.h:90
bool IsSingleDevice(void) const
Checks if the address is bound to a specified NetDevice.
uint16_t GetProtocol(void) const
Get the protocol.
uint32_t m_device
Outgoing NetDevice index.
uint32_t CopyAllFrom(const uint8_t *buffer, uint8_t len)
Definition: address.cc:110
static uint8_t GetType(void)
Return the Type of address.
uint32_t GetSingleDevice(void) const
Get the device this address is bound to.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetPhysicalAddress(const Address address)
Set the destination address.
address
Definition: first.py:44
Address m_address
Destination address.
void SetProtocol(uint16_t protocol)
Set the protocol.
static PacketSocketAddress ConvertFrom(const Address &address)
Address GetPhysicalAddress(void) const
Get the destination address.
bool m_isSingleDevice
True if directed to a specific outgoing NetDevice.
static uint8_t Register(void)
Allocate a new type id for a new type of address.
Definition: address.cc:138