Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * Copyright (c) 2013-2017 Nest Labs, Inc.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : /**
20 : * @file
21 : * This file defines the concrete class
22 : * <tt>Inet::IPPrefix</tt>, which comprise two member fields:
23 : * a) a IP address and b) a length field. The CHIP Inet Layer
24 : * uses objects of this class to represent Internet protocol
25 : * address prefixes of both IPv4 and IPv6 address families.
26 : */
27 :
28 : #pragma once
29 :
30 : #include <inet/IPAddress.h>
31 :
32 : #define CHIP_INET_IPV6_DEFAULT_PREFIX_LEN (64)
33 : #define CHIP_INET_IPV6_MAX_PREFIX_LEN (128)
34 :
35 : namespace chip {
36 : namespace Inet {
37 :
38 : /**
39 : * @brief Internet protocol address prefix
40 : *
41 : * @details
42 : * Use objects of the \c IPPrefix class to represent Internet protocol address
43 : * prefixes of both IPv4 and IPv6 address families.
44 : */
45 : class IPPrefix
46 : {
47 : public:
48 : IPPrefix() = default;
49 4 : IPPrefix(const IPAddress & ipAddress, uint8_t length) : IPAddr(ipAddress), Length(length) {}
50 :
51 : /** An IPv6 or IPv4 address. */
52 : IPAddress IPAddr;
53 :
54 : /**
55 : * @brief Length of the prefix.
56 : *
57 : * @details
58 : * Note well: this field is public, and it is an invariant of this class
59 : * that <tt>Length <= 32</tt> where the type of \c IPAddr is
60 : * \c IPAddressType::kIPv4 and <tt>Length <= 128</tt> where the type of
61 : * \c IPAddr is \c IPAddressType::kIPv6.
62 : */
63 : uint8_t Length;
64 :
65 : /**
66 : * A distinguished object where the type of \c IPAddr is
67 : * \c IPAddressType::kAny and <tt>Length == 0</tt>.
68 : */
69 : static IPPrefix Zero;
70 :
71 : /**
72 : * @brief Compares the prefix with the distinguished \c Zero value.
73 : *
74 : * @details
75 : * Note well: a prefix is not equivalent to \c Zero if the type of
76 : * \c IPAddr is not \c IPAddressType::kAny.
77 : *
78 : * @return \c true if equivalent to \c Zero, else \c false.
79 : */
80 : bool IsZero() const;
81 :
82 : /**
83 : * @brief Compares the prefix with another for equivalence.
84 : *
85 : * @details
86 : * Note well: two prefixes are not equivalent unless the \c IPAddr fields
87 : * are completely equivalent, i.e. all 128 bits must be identical.
88 : *
89 : * @return \c true if equivalent, else \c false.
90 : */
91 : bool operator==(const IPPrefix & other) const;
92 :
93 : /**
94 : * @brief Compares the prefix with another for inequivalence.
95 : *
96 : * @details
97 : * Note well: two prefixes are not equivalent unless the \c IPAddr fields
98 : * are completely equivalent, i.e. all 128 bits must be identical.
99 : *
100 : * @return \c false if equivalent, else \c false.
101 : */
102 : bool operator!=(const IPPrefix & other) const;
103 :
104 : /**
105 : * @brief Test if an address matches the prefix.
106 : *
107 : * @param[in] addr the address to test.
108 : *
109 : * @return \c true if \c addr has the prefix, else \c false.
110 : */
111 : bool MatchAddress(const IPAddress & addr) const;
112 : };
113 :
114 : } // namespace Inet
115 : } // namespace chip
|