libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
address_helpers.h
1 /*
2  * Copyright (c) 2017, Matias Fontanini
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef TINS_ADDRESS_HELPERS_H
31 #define TINS_ADDRESS_HELPERS_H
32 
33 #include "../hw_address.h"
34 
38 namespace Tins {
39 
40 class IPv4Address;
41 class IPv6Address;
42 
43 namespace Internals {
44 
45 template<typename T>
46 bool increment_buffer(T& addr) {
47  typename T::iterator it = addr.end() - 1;
48  while (it >= addr.begin() && *it == 0xff) {
49  *it = 0;
50  --it;
51  }
52  // reached end
53  if (it < addr.begin()) {
54  return true;
55  }
56  (*it)++;
57  return false;
58 }
59 
60 template<typename T>
61 bool decrement_buffer(T& addr) {
62  typename T::iterator it = addr.end() - 1;
63  while (it >= addr.begin() && *it == 0) {
64  *it = 0xff;
65  --it;
66  }
67  // reached end
68  if (it < addr.begin()) {
69  return true;
70  }
71  (*it)--;
72  return false;
73 }
74 
75 bool increment(IPv4Address& addr);
76 bool increment(IPv6Address& addr);
77 bool decrement(IPv4Address& addr);
78 bool decrement(IPv6Address& addr);
79 template<size_t n>
80 bool increment(HWAddress<n>& addr) {
81  return increment_buffer(addr);
82 }
83 template<size_t n>
84 bool decrement(HWAddress<n>& addr) {
85  return decrement_buffer(addr);
86 }
87 
88 IPv4Address last_address_from_mask(IPv4Address addr, IPv4Address mask);
89 IPv6Address last_address_from_mask(IPv6Address addr, const IPv6Address& mask);
90 template<size_t n>
91 HWAddress<n> last_address_from_mask(HWAddress<n> addr, const HWAddress<n>& mask) {
92  typename HWAddress<n>::iterator addr_iter = addr.begin();
93  for (typename HWAddress<n>::const_iterator it = mask.begin(); it != mask.end(); ++it, ++addr_iter) {
94  *addr_iter = *addr_iter | ~*it;
95  }
96  return addr;
97 }
98 
99 } // Internals
100 } // Tins
101 
106 #endif // TINS_ADDRESS_HELPERS_H