• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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  */
16 
17 #pragma once
18 
19 #include <linux/if_ether.h>
20 #include <stdint.h>
21 
22 #include <functional>
23 
24 // A lease consists of both the interface index and the MAC address. This
25 // way the server can run on many different interfaces that have the same
26 // client MAC address without giving out the same IP address. The reason
27 // this is useful is because we might have several virtual interfaces, one
28 // for each access point, that all have the same endpoint on the other side.
29 // This endpoint would then have the same MAC address and always get the
30 // same address. But for routing purposes it's useful to give it different
31 // addresses depending on the server side interface. That way the routing
32 // table can be set up so that packets are forwarded to the correct access
33 // point interface based on IP address.
34 struct Lease {
LeaseLease35     Lease(unsigned int interfaceIndex, const uint8_t* macAddress) {
36         InterfaceIndex = interfaceIndex;
37         memcpy(MacAddress, macAddress, sizeof(MacAddress));
38     }
39     unsigned int InterfaceIndex;
40     uint8_t MacAddress[ETH_ALEN];
41 };
42 
43 template<class T>
hash_combine(size_t & seed,const T & value)44 inline void hash_combine(size_t& seed, const T& value) {
45     std::hash<T> hasher;
46     seed ^= hasher(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
47 }
48 
49 namespace std {
50 template<> struct hash<Lease> {
51     size_t operator()(const Lease& lease) const {
52         size_t seed = 0;
53         hash_combine(seed, lease.InterfaceIndex);
54         // Treat the first 4 bytes as an uint32_t to save some computation
55         hash_combine(seed, *reinterpret_cast<const uint32_t*>(lease.MacAddress));
56         // And the remaining 2 bytes as an uint16_t
57         hash_combine(seed,
58                      *reinterpret_cast<const uint16_t*>(lease.MacAddress + 4));
59         return seed;
60     }
61 };
62 }
63 
64 inline bool operator==(const Lease& left, const Lease& right) {
65     return left.InterfaceIndex == right.InterfaceIndex &&
66         memcmp(left.MacAddress, right.MacAddress, sizeof(left.MacAddress)) == 0;
67 }
68