1/*
2Copyright (c) 2015-2015 Cisco Systems, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef _MAC_ADDRESS_H_
18#define _MAC_ADDRESS_H_
19
20#include "CPktCmn.h"
21
22#ifndef ETHER_ADDR_LEN
23#define ETHER_ADDR_LEN  6 /**< Length of Ethernet address. */
24#endif
25
26class MacAddress
27{
28public:
29
30    MacAddress()
31    {
32        set(0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
33    };
34
35    MacAddress(uint8_t a0,
36               uint8_t a1,
37               uint8_t a2,
38               uint8_t a3,
39               uint8_t a4,
40               uint8_t a5)
41    {
42        set(a0,
43            a1,
44            a2,
45            a3,
46            a4,
47            a5);
48    };
49
50    MacAddress(const uint8_t macAddr[ETHER_ADDR_LEN])
51    {
52        set(macAddr[0],
53            macAddr[1],
54            macAddr[2],
55            macAddr[3],
56            macAddr[4],
57            macAddr[5]	);
58    };
59
60    void    set(uint8_t a0,
61                uint8_t a1,
62                uint8_t a2,
63                uint8_t a3,
64                uint8_t a4,
65                uint8_t a5)
66    {
67        data[0]=a0;
68        data[1]=a1;
69        data[2]=a2;
70        data[3]=a3;
71        data[4]=a4;
72        data[5]=a5;
73    };
74
75    void set(uint8_t *argPtr) {
76        memcpy( data, argPtr, sizeof(data) );
77    }
78
79    void set(uint8_t *argPtr,uint8_t val) {
80        memcpy( data, argPtr, sizeof(data) );
81        data[5]=val;
82    }
83
84    void set(uint64_t val) {
85        for (int i = 0; i < 6; i++) {
86            data[i] = ( val >> ((5 - i) * 8) ) & 0xFF;
87        }
88    }
89
90    bool isDefaultAddress() const
91    {
92        static MacAddress defaultMac;
93        return (*this == defaultMac);
94    }
95
96    bool isInvalidAddress() const
97    {
98        static MacAddress allZeros(0,0,0,0,0,0);
99        static MacAddress defaultMac;
100        return (*this == allZeros || *this == defaultMac);
101    }
102    void setIdentifierAsBogusAddr(uint32_t identifier)
103    {
104        *(uint32_t*)data = identifier;
105    }
106
107    uint32_t getIdentifierFromBogusAddr()
108    {
109        return *(uint32_t*)data;
110    }
111
112    MacAddress& operator+=(const int& val) {
113        uint64_t tmp = 0;
114
115        for (int i = 0; i < 6; i++) {
116            tmp <<= 8;
117            tmp |= data[i];
118        }
119
120        tmp += val;
121
122        for (int i = 0; i < 6; i++) {
123            data[i] = ( tmp >> ((5 - i) * 8) ) & 0xFF;
124        }
125
126        return *this;
127    }
128
129    bool operator == (const MacAddress& rhs) const
130    {
131        for(int i = 0; i < ETHER_ADDR_LEN; i++)
132        {
133            if(data[i] != rhs.data[i])
134                return false;
135        }
136
137        return true;
138    }
139
140    MacAddress& operator = (const uint8_t *rhs) {
141        memcpy(data, rhs, ETHER_ADDR_LEN);
142        return (*this);
143    }
144
145    uint8_t*	GetBuffer()
146    {
147        return data;
148    }
149
150    const uint8_t*	GetConstBuffer() const
151    {
152        return data;
153    }
154    void dump(FILE *fd) const;
155
156    void copyToArray(uint8_t *arrayToFill) const
157    {
158        ((uint32_t*)arrayToFill)[0] = ((uint32_t*)data)[0];//Copy first 32bit
159        ((uint16_t*)arrayToFill)[2] = ((uint16_t*)data)[2];//Copy last 16bit
160    }
161
162public:
163    uint8_t data[ETHER_ADDR_LEN];
164};
165
166#endif //_MAC_ADDRESS_H_
167