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 <iomanip>
24 #include <iostream>
25 #include <string.h>
26 
27 namespace ns3 {
28 
29 #define ASCII_a (0x41)
30 #define ASCII_z (0x5a)
31 #define ASCII_A (0x61)
32 #define ASCII_Z (0x7a)
33 #define ASCII_COLON (0x3a)
34 #define ASCII_ZERO (0x30)
35 
36 static char
38 {
39  if (c >= ASCII_a && c <= ASCII_z) {
40  return c;
41  } else if (c >= ASCII_A && c <= ASCII_Z) {
42  return c + (ASCII_a - ASCII_A);
43  } else {
44  return c;
45  }
46 }
47 
48 
50 {
51  memset (m_address, 0, 8);
52 }
53 Mac64Address::Mac64Address (const char *str)
54 {
55  int i = 0;
56  while (*str != 0 && i < 8)
57  {
58  uint8_t byte = 0;
59  while (*str != ASCII_COLON && *str != 0)
60  {
61  byte <<= 4;
62  char low = AsciiToLowCase (*str);
63  if (low >= ASCII_a)
64  {
65  byte |= low - ASCII_a + 10;
66  }
67  else
68  {
69  byte |= low - ASCII_ZERO;
70  }
71  str++;
72  }
73  m_address[i] = byte;
74  i++;
75  if (*str == 0)
76  {
77  break;
78  }
79  str++;
80  }
81  NS_ASSERT (i == 6);
82 }
83 void
84 Mac64Address::CopyFrom (const uint8_t buffer[8])
85 {
86  memcpy (m_address, buffer, 8);
87 }
88 void
89 Mac64Address::CopyTo (uint8_t buffer[8]) const
90 {
91  memcpy (buffer, m_address, 8);
92 }
93 
94 bool
96 {
97  return address.CheckCompatible (GetType (), 8);
98 }
99 Mac64Address::operator Address () const
100 {
101  return ConvertTo ();
102 }
105 {
106  NS_ASSERT (address.CheckCompatible (GetType (), 8));
107  Mac64Address retval;
108  address.CopyTo (retval.m_address);
109  return retval;
110 }
111 Address
113 {
114  return Address (GetType (), m_address, 8);
115 }
116 
119 {
120  static uint64_t id = 0;
121  id++;
122  Mac64Address address;
123  address.m_address[0] = (id >> 56) & 0xff;
124  address.m_address[1] = (id >> 48) & 0xff;
125  address.m_address[2] = (id >> 40) & 0xff;
126  address.m_address[3] = (id >> 32) & 0xff;
127  address.m_address[4] = (id >> 24) & 0xff;
128  address.m_address[5] = (id >> 16) & 0xff;
129  address.m_address[6] = (id >> 8) & 0xff;
130  address.m_address[7] = (id >> 0) & 0xff;
131  return address;
132 }
133 uint8_t
135 {
136  static uint8_t type = Address::Register ();
137  return type;
138 }
139 
140 bool operator == (const Mac64Address &a, const Mac64Address &b)
141 {
142  uint8_t ada[8];
143  uint8_t adb[8];
144  a.CopyTo (ada);
145  b.CopyTo (adb);
146  return memcmp (ada, adb, 8) == 0;
147 }
148 bool operator != (const Mac64Address &a, const Mac64Address &b)
149 {
150  return !(a == b);
151 }
152 
153 std::ostream& operator<< (std::ostream& os, const Mac64Address & address)
154 {
155  uint8_t ad[8];
156  address.CopyTo (ad);
157 
158  os.setf (std::ios::hex, std::ios::basefield);
159  os.fill ('0');
160  for (uint8_t i=0; i < 7; i++)
161  {
162  os << std::setw (2) << (uint32_t)ad[i] << ":";
163  }
164  // Final byte not suffixed by ":"
165  os << std::setw (2) << (uint32_t)ad[7];
166  os.setf (std::ios::dec, std::ios::basefield);
167  os.fill (' ');
168  return os;
169 }
170 
171 
172 } // namespace ns3