The Pedigree Project  0.1
MacAddress.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/kernel/network/MacAddress.h"
21 #include "pedigree/kernel/utilities/StaticString.h"
22 #include "pedigree/kernel/utilities/utility.h"
23 
24 MacAddress::MacAddress() : m_Mac(), m_Valid(false)
25 {
26  ByteSet(m_Mac, 0, 6);
27 }
28 
29 MacAddress::MacAddress(const MacAddress &other) : MacAddress()
30 {
31  m_Valid = other.valid();
32  if (other.valid())
33  {
34  setMac(other.getMac());
35  }
36 }
37 
38 void MacAddress::setMac(uint8_t byte, size_t element)
39 {
40  uint16_t word = byte;
41  if (element % 2)
42  {
43  word <<= 8;
44  }
45 
46  element /= 2;
47 
48  if (element < 3)
49  {
50  m_Mac[element] |= word;
51  m_Valid =
52  true; // it has at least a byte, which makes it partially valid
53  }
54 }
55 
56 void MacAddress::setMac(uint8_t element)
57 {
58  ByteSet(m_Mac, element, 6);
59  m_Valid = true;
60 }
61 
62 void MacAddress::setMac(const uint16_t *data, bool bSwap)
63 {
64  if (bSwap)
65  {
66  size_t i;
67  for (i = 0; i < 3; i++)
68  m_Mac[i] = BIG_TO_HOST16(data[i]);
69  }
70  else
71  MemoryCopy(m_Mac, data, 6);
72 
73  m_Valid = true;
74 };
75 
76 uint8_t MacAddress::getMac(size_t element) const
77 {
78  size_t realElement = element / 2;
79  if (realElement >= 3)
80  return 0;
81 
82  uint16_t word = m_Mac[realElement];
83  if (element % 2)
84  return word >> 8;
85  else
86  return word & 0xFF;
87 };
88 
89 const uint16_t *MacAddress::getMac() const
90 {
91  return m_Mac;
92 };
93 
94 uint8_t MacAddress::operator[](size_t offset) const
95 {
96  if (m_Valid)
97  return getMac(offset);
98  else
99  return 0;
100 };
101 
102 MacAddress &MacAddress::operator=(const MacAddress &a)
103 {
104  if (a.valid())
105  setMac(a.getMac());
106  return *this;
107 }
108 
109 MacAddress &MacAddress::operator=(const uint16_t *a)
110 {
111  setMac(a);
112  return *this;
113 }
114 
115 String MacAddress::toString()
116 {
117  NormalStaticString str;
118  for (int i = 0; i < 6; i++)
119  {
120  str.append(getMac(i), 16, 2, '0');
121  if (i < 5)
122  str += ":";
123  }
124  return String(static_cast<const char *>(str));
125 }
Definition: String.h:49