A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
mac64-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 "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 #define ASCII_a (0x41)
33 #define ASCII_z (0x5a)
34 #define ASCII_A (0x61)
35 #define ASCII_Z (0x7a)
36 #define ASCII_COLON (0x3a)
37 #define ASCII_ZERO (0x30)
38 
39 static char
40 AsciiToLowCase (char c)
41 {
42  NS_LOG_FUNCTION (c);
43  if (c >= ASCII_a && c <= ASCII_z) {
44  return c;
45  } else if (c >= ASCII_A && c <= ASCII_Z) {
46  return c + (ASCII_a - ASCII_A);
47  } else {
48  return c;
49  }
50 }
51 
52 
53 Mac64Address::Mac64Address ()
54 {
55  NS_LOG_FUNCTION (this);
56  std::memset (m_address, 0, 8);
57 }
58 Mac64Address::Mac64Address (const char *str)
59 {
60  NS_LOG_FUNCTION (this << str);
61  int i = 0;
62  while (*str != 0 && i < 8)
63  {
64  uint8_t byte = 0;
65  while (*str != ASCII_COLON && *str != 0)
66  {
67  byte <<= 4;
68  char low = AsciiToLowCase (*str);
69  if (low >= ASCII_a)
70  {
71  byte |= low - ASCII_a + 10;
72  }
73  else
74  {
75  byte |= low - ASCII_ZERO;
76  }
77  str++;
78  }
79  m_address[i] = byte;
80  i++;
81  if (*str == 0)
82  {
83  break;
84  }
85  str++;
86  }
87  NS_ASSERT (i == 6);
88 }
89 void
90 Mac64Address::CopyFrom (const uint8_t buffer[8])
91 {
92  NS_LOG_FUNCTION (this << &buffer);
93  std::memcpy (m_address, buffer, 8);
94 }
95 void
96 Mac64Address::CopyTo (uint8_t buffer[8]) const
97 {
98  NS_LOG_FUNCTION (this << &buffer);
99  std::memcpy (buffer, m_address, 8);
100 }
101 
102 bool
104 {
105  NS_LOG_FUNCTION (&address);
106  return address.CheckCompatible (GetType (), 8);
107 }
108 Mac64Address::operator Address () const
109 {
110  return ConvertTo ();
111 }
114 {
115  NS_LOG_FUNCTION (address);
116  NS_ASSERT (address.CheckCompatible (GetType (), 8));
117  Mac64Address retval;
118  address.CopyTo (retval.m_address);
119  return retval;
120 }
121 
122 Address
124 {
125  NS_LOG_FUNCTION (this);
126  return Address (GetType (), m_address, 8);
127 }
128 
131 {
133  static uint64_t id = 0;
134  id++;
135  Mac64Address address;
136  address.m_address[0] = (id >> 56) & 0xff;
137  address.m_address[1] = (id >> 48) & 0xff;
138  address.m_address[2] = (id >> 40) & 0xff;
139  address.m_address[3] = (id >> 32) & 0xff;
140  address.m_address[4] = (id >> 24) & 0xff;
141  address.m_address[5] = (id >> 16) & 0xff;
142  address.m_address[6] = (id >> 8) & 0xff;
143  address.m_address[7] = (id >> 0) & 0xff;
144  return address;
145 }
146 uint8_t
147 Mac64Address::GetType (void)
148 {
150  static uint8_t type = Address::Register ();
151  return type;
152 }
153 
154 bool operator == (const Mac64Address &a, const Mac64Address &b)
155 {
156  uint8_t ada[8];
157  uint8_t adb[8];
158  a.CopyTo (ada);
159  b.CopyTo (adb);
160  return std::memcmp (ada, adb, 8) == 0;
161 }
162 bool operator != (const Mac64Address &a, const Mac64Address &b)
163 {
164  return !(a == b);
165 }
166 
167 std::ostream& operator<< (std::ostream& os, const Mac64Address & address)
168 {
169  uint8_t ad[8];
170  address.CopyTo (ad);
171 
172  os.setf (std::ios::hex, std::ios::basefield);
173  os.fill ('0');
174  for (uint8_t i=0; i < 7; i++)
175  {
176  os << std::setw (2) << (uint32_t)ad[i] << ":";
177  }
178  // Final byte not suffixed by ":"
179  os << std::setw (2) << (uint32_t)ad[7];
180  os.setf (std::ios::dec, std::ios::basefield);
181  os.fill (' ');
182  return os;
183 }
184 
185 
186 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
static bool IsMatchingType(const Address &address)
#define NS_ASSERT(condition)
Definition: assert.h:64
#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
an EUI-64 address
Definition: mac64-address.h:37
bool CheckCompatible(uint8_t type, uint8_t len) const
Definition: address.cc:122
void CopyTo(uint8_t buffer[8]) const
void CopyFrom(const uint8_t buffer[8])
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:43
Address ConvertTo(void) const
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Definition: address.cc:82
static Mac64Address ConvertFrom(const Address &address)
static uint8_t Register(void)
Definition: address.cc:137
static Mac64Address Allocate(void)