A Discrete-Event Network Simulator
API
mac16-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  * Copyright (c) 2011 The Boeing Company
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 #include "mac16-address.h"
22 #include "ns3/address.h"
23 #include "ns3/assert.h"
24 #include "ns3/log.h"
25 #include <iomanip>
26 #include <iostream>
27 #include <cstring>
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("Mac16Address");
32 
34 
35 #define ASCII_a (0x41)
36 #define ASCII_z (0x5a)
37 #define ASCII_A (0x61)
38 #define ASCII_Z (0x7a)
39 #define ASCII_COLON (0x3a)
40 #define ASCII_ZERO (0x30)
41 
47 static char
49 {
50  if (c >= ASCII_a && c <= ASCII_z) {
51  return c;
52  } else if (c >= ASCII_A && c <= ASCII_Z) {
53  return c + (ASCII_a - ASCII_A);
54  } else {
55  return c;
56  }
57 }
58 
59 
61 {
62  NS_LOG_FUNCTION (this);
63  memset (m_address, 0, 2);
64 }
65 
66 Mac16Address::Mac16Address (const char *str)
67 {
68  NS_LOG_FUNCTION (this << str);
69  int i = 0;
70  while (*str != 0 && i < 2)
71  {
72  uint8_t byte = 0;
73  while (*str != ASCII_COLON && *str != 0)
74  {
75  byte <<= 4;
76  char low = AsciiToLowCase (*str);
77  if (low >= ASCII_a)
78  {
79  byte |= low - ASCII_a + 10;
80  }
81  else
82  {
83  byte |= low - ASCII_ZERO;
84  }
85  str++;
86  }
87  m_address[i] = byte;
88  i++;
89  if (*str == 0)
90  {
91  break;
92  }
93  str++;
94  }
95  NS_ASSERT (i == 2);
96 }
97 
98 void
99 Mac16Address::CopyFrom (const uint8_t buffer[2])
100 {
101  NS_LOG_FUNCTION (this << &buffer);
102  memcpy (m_address, buffer, 2);
103 }
104 void
105 Mac16Address::CopyTo (uint8_t buffer[2]) const
106 {
107  NS_LOG_FUNCTION (this << &buffer);
108  memcpy (buffer, m_address, 2);
109 }
110 
111 bool
113 {
115  return address.CheckCompatible (GetType (), 2);
116 }
117 
118 Mac16Address::operator Address () const
119 {
120  return ConvertTo ();
121 }
122 
125 {
127  NS_ASSERT (address.CheckCompatible (GetType (), 2));
128  Mac16Address retval;
129  address.CopyTo (retval.m_address);
130  return retval;
131 }
132 Address
134 {
135  NS_LOG_FUNCTION (this);
136  return Address (GetType (), m_address, 2);
137 }
138 
141 {
143  static uint64_t id = 0;
144  id++;
146  address.m_address[0] = (id >> 8) & 0xff;
147  address.m_address[1] = (id >> 0) & 0xff;
148  return address;
149 }
150 
151 uint8_t
153 {
155 
156  static uint8_t type = Address::Register ();
157  return type;
158 }
159 
162 {
164 
165  static Mac16Address broadcast = Mac16Address ("ff:ff");
166  return broadcast;
167 }
168 
171 {
173 
174  uint8_t ipv6AddrBuf[16];
175  address.GetBytes(ipv6AddrBuf);
176 
177  uint8_t addrBuf[2];
178 
179  addrBuf[0] = 0x80 | (ipv6AddrBuf[14] & 0x1F);
180  addrBuf[1] = ipv6AddrBuf[15];
181 
182  Mac16Address multicastAddr;
183  multicastAddr.CopyFrom (addrBuf);
184 
185  return multicastAddr;
186 }
187 
188 bool
190 {
191  NS_LOG_FUNCTION (this);
192  if (m_address[0] == 0xff && m_address[1] == 0xff)
193  {
194  return true;
195  }
196  return false;
197 }
198 
199 bool
201 {
202  NS_LOG_FUNCTION (this);
203  uint8_t val = m_address[0];
204  val >>= 5;
205  if (val == 0x4)
206  {
207  return true;
208  }
209  return false;
210 }
211 
212 std::ostream & operator<< (std::ostream& os, const Mac16Address & address)
213 {
214  uint8_t ad[2];
215  address.CopyTo (ad);
216 
217  os.setf (std::ios::hex, std::ios::basefield);
218  os.fill ('0');
219  for (uint8_t i = 0; i < 1; i++)
220  {
221  os << std::setw (2) << (uint32_t)ad[i] << ":";
222  }
223  // Final byte not suffixed by ":"
224  os << std::setw (2) << (uint32_t)ad[1];
225  os.setf (std::ios::dec, std::ios::basefield);
226  os.fill (' ');
227  return os;
228 }
229 
230 std::istream& operator>> (std::istream& is, Mac16Address & address)
231 {
232  std::string v;
233  is >> v;
234 
235  std::string::size_type col = 0;
236  for (uint8_t i = 0; i < 2; ++i)
237  {
238  std::string tmp;
239  std::string::size_type next;
240  next = v.find (":", col);
241  if (next == std::string::npos)
242  {
243  tmp = v.substr (col, v.size ()-col);
244  address.m_address[i] = strtoul (tmp.c_str(), 0, 16);
245  break;
246  }
247  else
248  {
249  tmp = v.substr (col, next-col);
250  address.m_address[i] = strtoul (tmp.c_str(), 0, 16);
251  col = next + 1;
252  }
253  }
254  return is;
255 }
256 
257 }
a polymophic address class
Definition: address.h:91
static uint8_t Register(void)
Allocate a new type id for a new type of address.
Definition: address.cc:138
Describes an IPv6 address.
Definition: ipv6-address.h:50
This class can contain 16 bit addresses.
Definition: mac16-address.h:42
static uint8_t GetType(void)
Return the Type of address.
bool IsBroadcast(void) const
Checks if the address is a broadcast address according to 802.15.4 scheme (i.e., 0xFFFF).
static Mac16Address GetMulticast(Ipv6Address address)
Returns the multicast address associated with an IPv6 address according to RFC 4944 Section 9.
static bool IsMatchingType(const Address &address)
static Mac16Address Allocate(void)
Allocate a new Mac16Address.
static Mac16Address ConvertFrom(const Address &address)
void CopyTo(uint8_t buffer[2]) const
Address ConvertTo(void) const
bool IsMulticast(void) const
Checks if the address is a multicast address according to RFC 4944 Section 9 (i.e....
void CopyFrom(const uint8_t buffer[2])
uint8_t m_address[2]
address value
static Mac16Address GetBroadcast(void)
#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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define ASCII_Z
#define ASCII_A
#define ASCII_z
#define ASCII_COLON
#define ASCII_ZERO
#define ASCII_a
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ATTRIBUTE_HELPER_CPP(Length)
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:162
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
static char AsciiToLowCase(char c)
Converts a char to lower case.
unsigned char byte
Definition: qkd-encryptor.h:64