A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("PacketSocketAddress");
25 
26 namespace ns3 {
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 {
108  NS_LOG_FUNCTION (address);
109  NS_ASSERT (IsMatchingType (address));
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 {
140  NS_LOG_FUNCTION (address);
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
uint16_t GetProtocol(void) const
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
bool IsMatchingType(uint8_t type) const
Definition: address.cc:131
an address for a packet socket
#define NS_ASSERT(condition)
Definition: assert.h:64
static bool IsMatchingType(const Address &address)
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
uint32_t GetSingleDevice(void) const
void SetSingleDevice(uint32_t device)
a polymophic address class
Definition: address.h:86
uint32_t CopyAllTo(uint8_t *buffer, uint8_t len) const
Definition: address.cc:90
NS_LOG_COMPONENT_DEFINE("PacketSocketAddress")
uint32_t CopyAllFrom(const uint8_t *buffer, uint8_t len)
Definition: address.cc:110
void SetPhysicalAddress(const Address address)
Address GetPhysicalAddress(void) const
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Definition: address.cc:82
void SetProtocol(uint16_t protocol)
static PacketSocketAddress ConvertFrom(const Address &address)
Address ConvertTo(void) const
tuple address
Definition: first.py:37
static uint8_t Register(void)
Allocate a new type id for a new type of address.
Definition: address.cc:138