A Discrete-Event Network Simulator
API
mac64-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 "mac64-address.h"
21 #include "ns3/address.h"
22 #include "ns3/assert.h"
23 #include "ns3/log.h"
24 #include <iomanip>
25 #include <iostream>
26 #include <cstring>
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("Mac64Address");
31 
33 
34 #define ASCII_a (0x41)
35 #define ASCII_z (0x5a)
36 #define ASCII_A (0x61)
37 #define ASCII_Z (0x7a)
38 #define ASCII_COLON (0x3a)
39 #define ASCII_ZERO (0x30)
40 
46 static char
48 {
49  NS_LOG_FUNCTION (c);
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  std::memset (m_address, 0, 8);
64 }
65 Mac64Address::Mac64Address (const char *str)
66 {
67  NS_LOG_FUNCTION (this << str);
68  int i = 0;
69  while (*str != 0 && i < 8)
70  {
71  uint8_t byte = 0;
72  while (*str != ASCII_COLON && *str != 0)
73  {
74  byte <<= 4;
75  char low = AsciiToLowCase (*str);
76  if (low >= ASCII_a)
77  {
78  byte |= low - ASCII_a + 10;
79  }
80  else
81  {
82  byte |= low - ASCII_ZERO;
83  }
84  str++;
85  }
86  m_address[i] = byte;
87  i++;
88  if (*str == 0)
89  {
90  break;
91  }
92  str++;
93  }
94  NS_ASSERT (i == 8);
95 }
96 void
97 Mac64Address::CopyFrom (const uint8_t buffer[8])
98 {
99  NS_LOG_FUNCTION (this << &buffer);
100  std::memcpy (m_address, buffer, 8);
101 }
102 void
103 Mac64Address::CopyTo (uint8_t buffer[8]) const
104 {
105  NS_LOG_FUNCTION (this << &buffer);
106  std::memcpy (buffer, m_address, 8);
107 }
108 
109 bool
111 {
113  return address.CheckCompatible (GetType (), 8);
114 }
115 Mac64Address::operator Address () const
116 {
117  return ConvertTo ();
118 }
121 {
123  NS_ASSERT (address.CheckCompatible (GetType (), 8));
124  Mac64Address retval;
125  address.CopyTo (retval.m_address);
126  return retval;
127 }
128 
129 Address
131 {
132  NS_LOG_FUNCTION (this);
133  return Address (GetType (), m_address, 8);
134 }
135 
138 {
140  static uint64_t id = 0;
141  id++;
143  address.m_address[0] = (id >> 56) & 0xff;
144  address.m_address[1] = (id >> 48) & 0xff;
145  address.m_address[2] = (id >> 40) & 0xff;
146  address.m_address[3] = (id >> 32) & 0xff;
147  address.m_address[4] = (id >> 24) & 0xff;
148  address.m_address[5] = (id >> 16) & 0xff;
149  address.m_address[6] = (id >> 8) & 0xff;
150  address.m_address[7] = (id >> 0) & 0xff;
151  return address;
152 }
153 uint8_t
155 {
157  static uint8_t type = Address::Register ();
158  return type;
159 }
160 
161 std::ostream& operator<< (std::ostream& os, const Mac64Address & address)
162 {
163  uint8_t ad[8];
164  address.CopyTo (ad);
165 
166  os.setf (std::ios::hex, std::ios::basefield);
167  os.fill ('0');
168  for (uint8_t i=0; i < 7; i++)
169  {
170  os << std::setw (2) << (uint32_t)ad[i] << ":";
171  }
172  // Final byte not suffixed by ":"
173  os << std::setw (2) << (uint32_t)ad[7];
174  os.setf (std::ios::dec, std::ios::basefield);
175  os.fill (' ');
176  return os;
177 }
178 
179 std::istream& operator>> (std::istream& is, Mac64Address & address)
180 {
181  std::string v;
182  is >> v;
183 
184  std::string::size_type col = 0;
185  for (uint8_t i = 0; i < 8; ++i)
186  {
187  std::string tmp;
188  std::string::size_type next;
189  next = v.find (":", col);
190  if (next == std::string::npos)
191  {
192  tmp = v.substr (col, v.size ()-col);
193  address.m_address[i] = strtoul (tmp.c_str(), 0, 16);
194  break;
195  }
196  else
197  {
198  tmp = v.substr (col, next-col);
199  address.m_address[i] = strtoul (tmp.c_str(), 0, 16);
200  col = next + 1;
201  }
202  }
203  return is;
204 }
205 
206 } // namespace ns3
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
#define ASCII_COLON
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define ASCII_ZERO
static bool IsMatchingType(const Address &address)
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type.
#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:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
a polymophic address class
Definition: address.h:90
an EUI-64 address
Definition: mac64-address.h:43
static char AsciiToLowCase(char c)
Converts a char to lower case.
void CopyTo(uint8_t buffer[8]) const
void CopyFrom(const uint8_t buffer[8])
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:37
#define ASCII_a
Address ConvertTo(void) const
static Mac64Address ConvertFrom(const Address &address)
uint8_t m_address[8]
address value
static uint8_t GetType(void)
Return the Type of address.
#define ASCII_A
static uint8_t Register(void)
Allocate a new type id for a new type of address.
Definition: address.cc:138
#define ASCII_z
static Mac64Address Allocate(void)
Allocate a new Mac64Address.
#define ASCII_Z