jpayne@69
|
1
|
jpayne@69
|
2 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
jpayne@69
|
3 // Licensed under the MIT License:
|
jpayne@69
|
4 //
|
jpayne@69
|
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
jpayne@69
|
6 // of this software and associated documentation files (the "Software"), to deal
|
jpayne@69
|
7 // in the Software without restriction, including without limitation the rights
|
jpayne@69
|
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
jpayne@69
|
9 // copies of the Software, and to permit persons to whom the Software is
|
jpayne@69
|
10 // furnished to do so, subject to the following conditions:
|
jpayne@69
|
11 //
|
jpayne@69
|
12 // The above copyright notice and this permission notice shall be included in
|
jpayne@69
|
13 // all copies or substantial portions of the Software.
|
jpayne@69
|
14 //
|
jpayne@69
|
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
jpayne@69
|
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
jpayne@69
|
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
jpayne@69
|
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
jpayne@69
|
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
jpayne@69
|
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
jpayne@69
|
21 // THE SOFTWARE.
|
jpayne@69
|
22
|
jpayne@69
|
23 #pragma once
|
jpayne@69
|
24
|
jpayne@69
|
25 #include "common.h"
|
jpayne@69
|
26 #include <cstdint>
|
jpayne@69
|
27
|
jpayne@69
|
28 KJ_BEGIN_HEADER
|
jpayne@69
|
29
|
jpayne@69
|
30 struct sockaddr;
|
jpayne@69
|
31
|
jpayne@69
|
32 namespace kj {
|
jpayne@69
|
33
|
jpayne@69
|
34 class CidrRange {
|
jpayne@69
|
35 public:
|
jpayne@69
|
36 CidrRange(StringPtr pattern);
|
jpayne@69
|
37
|
jpayne@69
|
38 static CidrRange inet4(ArrayPtr<const byte> bits, uint bitCount);
|
jpayne@69
|
39 static CidrRange inet6(ArrayPtr<const uint16_t> prefix, ArrayPtr<const uint16_t> suffix,
|
jpayne@69
|
40 uint bitCount);
|
jpayne@69
|
41 // Zeros are inserted between `prefix` and `suffix` to extend the address to 128 bits.
|
jpayne@69
|
42
|
jpayne@69
|
43 uint getSpecificity() const { return bitCount; }
|
jpayne@69
|
44
|
jpayne@69
|
45 bool matches(const struct sockaddr* addr) const;
|
jpayne@69
|
46 bool matchesFamily(int family) const;
|
jpayne@69
|
47
|
jpayne@69
|
48 String toString() const;
|
jpayne@69
|
49
|
jpayne@69
|
50 private:
|
jpayne@69
|
51 int family;
|
jpayne@69
|
52 byte bits[16];
|
jpayne@69
|
53 uint bitCount; // how many bits in `bits` need to match
|
jpayne@69
|
54
|
jpayne@69
|
55 CidrRange(int family, ArrayPtr<const byte> bits, uint bitCount);
|
jpayne@69
|
56
|
jpayne@69
|
57 void zeroIrrelevantBits();
|
jpayne@69
|
58 };
|
jpayne@69
|
59
|
jpayne@69
|
60 } // namespace kj
|
jpayne@69
|
61
|
jpayne@69
|
62 KJ_END_HEADER
|