A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
packet-socket-address.cc
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 
28 PacketSocketAddress::PacketSocketAddress ()
29 {
30  NS_LOG_FUNCTION (this);
31 }
32 void
33 PacketSocketAddress::SetProtocol (uint16_t protocol)
34 {
35  NS_LOG_FUNCTION (this << protocol);
36  m_protocol = protocol;
37 }
38 void
39 PacketSocketAddress::SetAllDevices (void)
40 {
41  NS_LOG_FUNCTION (this);
42  m_isSingleDevice = false;
43  m_device = 0;
44 }
45 void
46 PacketSocketAddress::SetSingleDevice (uint32_t index)
47 {
48  NS_LOG_FUNCTION (this << index);
49  m_isSingleDevice = true;
50  m_device = index;
51 }
52 void
53 PacketSocketAddress::SetPhysicalAddress (const Address address)
54 {
55  NS_LOG_FUNCTION (this << address);
56  m_address = address;
57 }
58 
59 uint16_t
60 PacketSocketAddress::GetProtocol (void) const
61 {
62  NS_LOG_FUNCTION (this);
63  return m_protocol;
64 }
65 bool
66 PacketSocketAddress::IsSingleDevice (void) const
67 {
68  NS_LOG_FUNCTION (this);
69  return m_isSingleDevice;
70 }
71 uint32_t
72 PacketSocketAddress::GetSingleDevice (void) const
73 {
74  NS_LOG_FUNCTION (this);
75  return m_device;
76 }
77 Address
78 PacketSocketAddress::GetPhysicalAddress (void) const
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
90 PacketSocketAddress::ConvertTo (void) const
91 {
92  NS_LOG_FUNCTION (this);
93  Address address;
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 }
105 PacketSocketAddress
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
144 PacketSocketAddress::GetType (void)
145 {
147  static uint8_t type = Address::Register ();
148  return type;
149 }
150 
151 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
bool IsMatchingType(uint8_t type) const
Definition: address.cc:130
an address for a packet socket
#define NS_ASSERT(condition)
Definition: assert.h:64
static bool IsMatchingType(const Address &address)
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
a polymophic address class
Definition: address.h:86
uint32_t CopyAllTo(uint8_t *buffer, uint8_t len) const
Definition: address.cc:90
uint32_t CopyAllFrom(const uint8_t *buffer, uint8_t len)
Definition: address.cc:110
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Definition: address.cc:82
static PacketSocketAddress ConvertFrom(const Address &address)
static uint8_t Register(void)
Definition: address.cc:137