Coverage Report

Created: 2026-04-24 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openweave-core/src/inet/IPPrefix.cpp
Line
Count
Source
1
/*
2
 *
3
 *    Copyright (c) 2013-2017 Nest Labs, Inc.
4
 *    All rights reserved.
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 implements the concrete class
22
 *      <tt>nl::Inet::IPPrefix</tt>, which comprise two member fields:
23
 *      a) a IP address and b) a length field. The Nest 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
29
#include <InetLayer/IPPrefix.h>
30
#include <Weave/Core/WeaveEncoding.h>
31
32
namespace nl {
33
namespace Inet {
34
35
IPPrefix IPPrefix::Zero;
36
37
bool IPPrefix::IsZero() const
38
0
{
39
0
    return  IPAddr.Addr[0] == 0 &&
40
0
            IPAddr.Addr[1] == 0 &&
41
0
            IPAddr.Addr[2] == 0 &&
42
0
            IPAddr.Addr[3] == 0 &&
43
0
            Length == 0;
44
0
}
45
46
bool IPPrefix::operator==(const IPPrefix& other) const
47
0
{
48
0
    return IPAddr == other.IPAddr && Length == other.Length;
49
0
}
50
51
bool IPPrefix::operator!=(const IPPrefix& other) const
52
0
{
53
0
    return IPAddr != other.IPAddr || Length != other.Length;
54
0
}
55
56
bool IPPrefix::MatchAddress(const IPAddress& addr) const
57
0
{
58
0
    uint8_t l = (Length <= 128) ? Length : 128;
59
0
    int i;
60
61
0
    for (i = 0; l >= 32; i++, l -= 32)
62
0
        if (IPAddr.Addr[i] != addr.Addr[i])
63
0
            return false;
64
65
0
    if (l == 0)
66
0
        return true;
67
68
0
    uint32_t mask = nl::Weave::Encoding::BigEndian::HostSwap32(0xFFFFFFFF << (32 - l));
69
0
    return (IPAddr.Addr[i] & mask) == (addr.Addr[i] & mask);
70
0
}
71
72
} // namespace Inet
73
} // namespace nl