• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #pragma once
21 
22 #include <functional>
23 
24 #include <linux/if_ether.h>
25 #include <stdint.h>
26 #include <string.h>
27 
28 struct MacAddress {
29     uint8_t addr[ETH_ALEN];
isBroadcastMacAddress30     bool isBroadcast() const {
31         return memcmp(addr, "\xFF\xFF\xFF\xFF\xFF\xFF", ETH_ALEN) == 0;
32     }
33 } __attribute__((__packed__));
34 
35 template<class T>
hash_combine(size_t & seed,const T & value)36 inline void hash_combine(size_t& seed, const T& value) {
37     std::hash<T> hasher;
38     seed ^= hasher(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
39 }
40 
41 namespace std {
42 template<> struct hash<MacAddress> {
43     size_t operator()(const MacAddress& addr) const {
44         size_t seed = 0;
45         // Treat the first 4 bytes as an uint32_t to save some computation
46         hash_combine(seed, *reinterpret_cast<const uint32_t*>(addr.addr));
47         // And the remaining 2 bytes as an uint16_t
48         hash_combine(seed, *reinterpret_cast<const uint16_t*>(addr.addr + 4));
49         return seed;
50     }
51 };
52 }
53 
54