A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("Mac64Address");
29 
30 namespace ns3 {
31 
32 ATTRIBUTE_HELPER_CPP (Mac64Address);
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 
41 static char
43 {
44  NS_LOG_FUNCTION (c);
45  if (c >= ASCII_a && c <= ASCII_z) {
46  return c;
47  } else if (c >= ASCII_A && c <= ASCII_Z) {
48  return c + (ASCII_a - ASCII_A);
49  } else {
50  return c;
51  }
52 }
53 
54 
56 {
57  NS_LOG_FUNCTION (this);
58  std::memset (m_address, 0, 8);
59 }
60 Mac64Address::Mac64Address (const char *str)
61 {
62  NS_LOG_FUNCTION (this << str);
63  int i = 0;
64  while (*str != 0 && i < 8)
65  {
66  uint8_t byte = 0;
67  while (*str != ASCII_COLON && *str != 0)
68  {
69  byte <<= 4;
70  char low = AsciiToLowCase (*str);
71  if (low >= ASCII_a)
72  {
73  byte |= low - ASCII_a + 10;
74  }
75  else
76  {
77  byte |= low - ASCII_ZERO;
78  }
79  str++;
80  }
81  m_address[i] = byte;
82  i++;
83  if (*str == 0)
84  {
85  break;
86  }
87  str++;
88  }
89  NS_ASSERT (i == 8);
90 }
91 void
92 Mac64Address::CopyFrom (const uint8_t buffer[8])
93 {
94  NS_LOG_FUNCTION (this << &buffer);
95  std::memcpy (m_address, buffer, 8);
96 }
97 void
98 Mac64Address::CopyTo (uint8_t buffer[8]) const
99 {
100  NS_LOG_FUNCTION (this << &buffer);
101  std::memcpy (buffer, m_address, 8);
102 }
103 
104 bool
106 {
107  NS_LOG_FUNCTION (&address);
108  return address.CheckCompatible (GetType (), 8);
109 }
110 Mac64Address::operator Address () const
111 {
112  return ConvertTo ();
113 }
116 {
117  NS_LOG_FUNCTION (address);
118  NS_ASSERT (address.CheckCompatible (GetType (), 8));
119  Mac64Address retval;
120  address.CopyTo (retval.m_address);
121  return retval;
122 }
123 
124 Address
126 {
127  NS_LOG_FUNCTION (this);
128  return Address (GetType (), m_address, 8);
129 }
130 
133 {
135  static uint64_t id = 0;
136  id++;
138  address.m_address[0] = (id >> 56) & 0xff;
139  address.m_address[1] = (id >> 48) & 0xff;
140  address.m_address[2] = (id >> 40) & 0xff;
141  address.m_address[3] = (id >> 32) & 0xff;
142  address.m_address[4] = (id >> 24) & 0xff;
143  address.m_address[5] = (id >> 16) & 0xff;
144  address.m_address[6] = (id >> 8) & 0xff;
145  address.m_address[7] = (id >> 0) & 0xff;
146  return address;
147 }
148 uint8_t
150 {
152  static uint8_t type = Address::Register ();
153  return type;
154 }
155 
156 std::ostream& operator<< (std::ostream& os, const Mac64Address & address)
157 {
158  uint8_t ad[8];
159  address.CopyTo (ad);
160 
161  os.setf (std::ios::hex, std::ios::basefield);
162  os.fill ('0');
163  for (uint8_t i=0; i < 7; i++)
164  {
165  os << std::setw (2) << (uint32_t)ad[i] << ":";
166  }
167  // Final byte not suffixed by ":"
168  os << std::setw (2) << (uint32_t)ad[7];
169  os.setf (std::ios::dec, std::ios::basefield);
170  os.fill (' ');
171  return os;
172 }
173 
174 static uint8_t
175 AsInt (std::string v)
176 {
177  NS_LOG_FUNCTION (v);
178  std::istringstream iss;
179  iss.str (v);
180  uint32_t retval;
181  iss >> std::hex >> retval >> std::dec;
182  return retval;
183 }
184 
185 std::istream& operator>> (std::istream& is, Mac64Address & address)
186 {
187  std::string v;
188  is >> v;
189 
190  std::string::size_type col = 0;
191  for (uint8_t i = 0; i < 8; ++i)
192  {
193  std::string tmp;
194  std::string::size_type next;
195  next = v.find (":", col);
196  if (next == std::string::npos)
197  {
198  tmp = v.substr (col, v.size ()-col);
199  address.m_address[i] = AsInt (tmp);
200  break;
201  }
202  else
203  {
204  tmp = v.substr (col, next-col);
205  address.m_address[i] = AsInt (tmp);
206  col = next + 1;
207  }
208  }
209  return is;
210 }
211 
212 } // namespace ns3
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:49
#define ASCII_COLON
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
#define ASCII_ZERO
static bool IsMatchingType(const Address &address)
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
static uint8_t AsInt(std::string v)
Definition: address.cc:252
NS_LOG_COMPONENT_DEFINE("Mac64Address")
a polymophic address class
Definition: address.h:86
an EUI-64 address
Definition: mac64-address.h:41
bool CheckCompatible(uint8_t type, uint8_t len) const
Definition: address.cc:122
static char AsciiToLowCase(char c)
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:43
#define ASCII_a
Macro to make help make class an ns-3 attribute.
Address ConvertTo(void) const
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Definition: address.cc:82
static Mac64Address ConvertFrom(const Address &address)
uint8_t m_address[8]
static uint8_t GetType(void)
#define ASCII_A
tuple address
Definition: first.py:37
ATTRIBUTE_HELPER_CPP(ObjectFactory)
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