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 
23 namespace ns3 {
24 
26 {
27 }
28 void
30 {
31  m_protocol = protocol;
32 }
33 void
35 {
36  m_isSingleDevice = false;
37  m_device = 0;
38 }
39 void
41 {
42  m_isSingleDevice = true;
43  m_device = index;
44 }
45 void
47 {
48  m_address = address;
49 }
50 
51 uint16_t
53 {
54  return m_protocol;
55 }
56 bool
58 {
59  return m_isSingleDevice;
60 }
61 uint32_t
63 {
64  return m_device;
65 }
66 Address
68 {
69  return m_address;
70 }
71 
72 PacketSocketAddress::operator Address () const
73 {
74  return ConvertTo ();
75 }
76 
77 Address
79 {
80  Address address;
81  uint8_t buffer[Address::MAX_SIZE];
82  buffer[0] = m_protocol & 0xff;
83  buffer[1] = (m_protocol >> 8) & 0xff;
84  buffer[2] = (m_device >> 24) & 0xff;
85  buffer[3] = (m_device >> 16) & 0xff;
86  buffer[4] = (m_device >> 8) & 0xff;
87  buffer[5] = (m_device >> 0) & 0xff;
88  buffer[6] = m_isSingleDevice ? 1 : 0;
89  uint32_t copied = m_address.CopyAllTo (buffer + 7, Address::MAX_SIZE - 7);
90  return Address (GetType (), buffer, 7 + copied);
91 }
94 {
95  NS_ASSERT (IsMatchingType (address));
96  uint8_t buffer[Address::MAX_SIZE];
97  address.CopyTo (buffer);
98  uint16_t protocol = buffer[0] | (buffer[1] << 8);
99  uint32_t device = 0;
100  device |= buffer[2];
101  device <<= 8;
102  device |= buffer[3];
103  device <<= 8;
104  device |= buffer[4];
105  device <<= 8;
106  device |= buffer[5];
107  bool isSingleDevice = (buffer[6] == 1) ? true : false;
108  Address physical;
109  physical.CopyAllFrom (buffer + 7, Address::MAX_SIZE - 7);
111  ad.SetProtocol (protocol);
112  if (isSingleDevice)
113  {
114  ad.SetSingleDevice (device);
115  }
116  else
117  {
118  ad.SetAllDevices ();
119  }
120  ad.SetPhysicalAddress (physical);
121  return ad;
122 }
123 bool
125 {
126  return address.IsMatchingType (GetType ());
127 }
128 uint8_t
130 {
131  static uint8_t type = Address::Register ();
132  return type;
133 }
134 
135 } // namespace ns3