forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork_functions.h
More file actions
197 lines (164 loc) · 6.92 KB
/
Copy pathnetwork_functions.h
File metadata and controls
197 lines (164 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Example extension library for introducing an OpaqueValue type.
//
// The address handling is simplified for the example, and IPv6 is
// unimplemented. Do not use this as-is.
#ifndef THIRD_PARTY_CEL_CPP_CODELAB_NETWORK_FUNCTIONS_H_
#define THIRD_PARTY_CEL_CPP_CODELAB_NETWORK_FUNCTIONS_H_
#include <cstdint>
#include <string>
#include <vector>
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "common/value.h"
#include "compiler/compiler.h"
#include "runtime/function_registry.h"
#include "runtime/runtime_options.h"
#include "runtime/type_registry.h"
#include "google/protobuf/arena.h"
namespace cel_codelab {
enum class IpVersion : uint8_t {
kUnset = 0,
kIPv4 = 4,
kIPv6 = 6, // unimplemented, but present for illustration.
};
// Represents a network address. To simplify the CEL type representation, this
// only supports IPv4.
//
// A the default value of 0v0 is special, and represents an invalid address,
// comparing unequal to anything except itself. For the purposes of ordering,
// compares less than any valid address.
//
// The example extension functions include a version that returns a zero value
// on error and a version that returns a CEL error.
//
// This class is stored inline in the OpaqueValue because it is compact and
// trivially copyable.
class NetworkAddressRep {
public:
// Creates a Value that wraps the given NetworkAddress. The representation is
// copied to the provided arena.
static cel::Value MakeValue(const NetworkAddressRep& rep);
// Unwraps a Value into a NetworkAddressRep. Returns nullptr if the value is
// not a NetworkAddress.
static absl::optional<NetworkAddressRep> Unwrap(const cel::Value& value);
// Parses a string representation of a network address. Returns nullopt if
// the string is not a valid network address.
//
// TODO(uncreated-issue/86): error handling simplified for example, real usage should
// provide some diagnostic for the parse failure.
static absl::optional<NetworkAddressRep> Parse(absl::string_view str);
// Zero value for an invalid address.
NetworkAddressRep() : addr_({0}), version_(IpVersion::kUnset) {}
NetworkAddressRep(const NetworkAddressRep& other) = default;
NetworkAddressRep(NetworkAddressRep&& other) = default;
NetworkAddressRep& operator=(const NetworkAddressRep& other) = default;
NetworkAddressRep& operator=(NetworkAddressRep&& other) = default;
IpVersion version() const { return version_; }
bool IsZeroValue() const { return version_ == IpVersion::kUnset; }
bool IsIPv4() const { return version_ == IpVersion::kIPv4; }
bool IsIPv6() const { return false; }
absl::optional<uint32_t> TryGetIPv4() const {
if (version_ == IpVersion::kIPv4) {
return addr_.v4;
}
return absl::nullopt;
}
absl::string_view TryGetIPv6() const { return absl::string_view(); }
std::string Format() const {
if (version_ == IpVersion::kUnset) {
return "null";
}
if (version_ == IpVersion::kIPv4) {
return absl::StrCat(
(addr_.v4 & 0xFF000000) >> 24, ".", (addr_.v4 & 0x00FF0000) >> 16,
".", (addr_.v4 & 0x0000FF00) >> 8, ".", (addr_.v4 & 0x000000FF));
}
return "v6 not yet implemented";
}
uint32_t GetIPv4() const { return addr_.v4; }
bool IsEqualTo(const NetworkAddressRep& other) const;
bool IsLessThan(const NetworkAddressRep& other) const;
private:
union {
uint32_t v0; // zero value
// Integer representation of an IPv4 address (system byte order)
uint32_t v4;
// TO_DO : add ipv6. this prevents storing the value inline due to size, so
// skipped here.
} addr_;
IpVersion version_;
};
// Represents a matcher for network addresses.
//
// Simple implementation that just stores a list of matching ranges.
//
// This is too big to store inline and has non-trivial copy and move behavior,
// so the inline representation is a pointer to an arena-allocated object.
class NetworkAddressMatcher {
public:
// Creates a Value that wraps the given NetworkAddress.
static cel::Value MakeValue(google::protobuf::Arena* arena, NetworkAddressMatcher rep);
// Unwraps a Value into a NetworkAddressMatcher. Returns nullptr if the value
// is not a NetworkAddressMatcher.
static const NetworkAddressMatcher* Unwrap(const cel::Value& value);
// Parses a string representation of a network address matcher. Returns
// nullopt if the string is not a valid network address matcher.
//
// TODO(uncreated-issue/86): supports a simple IPv4 range for illustration: e.g.
// 8.8.0.0-8.8.255.255
static absl::optional<NetworkAddressMatcher> Parse(absl::string_view str);
// Default value for an empty matcher. Matches nothing.
NetworkAddressMatcher() = default;
NetworkAddressMatcher(const NetworkAddressMatcher& other) = default;
NetworkAddressMatcher(NetworkAddressMatcher&& other) = default;
NetworkAddressMatcher& operator=(const NetworkAddressMatcher& other) =
default;
NetworkAddressMatcher& operator=(NetworkAddressMatcher&& other) = default;
bool IsEmpty() const { return ranges_v4_.empty(); }
bool IsEqualTo(const NetworkAddressMatcher& other) const;
bool Match(const NetworkAddressRep& addr) const;
private:
struct NetworkRangev4 {
uint32_t min_incl;
uint32_t max_incl;
};
// placeholder for illustration, not implemented.
struct NetworkRangev6 {
char min_incl[16];
char max_incl[16];
};
friend void swap(NetworkAddressMatcher& lhs, NetworkAddressMatcher& rhs) {
using std::swap;
swap(lhs.ranges_v4_, rhs.ranges_v4_);
}
// Sorted, non-overlapping ranges of matching IP addresses.
std::vector<NetworkRangev4> ranges_v4_;
};
// Returns a compiler library that adds the network functions to the type
// checker.
cel::CompilerLibrary NetworkFunctionsCompilerLibrary();
// Registers the network functions in a runtime for evaluation.
absl::Status RegisterNetworkFunctions(cel::FunctionRegistry& registry,
const cel::RuntimeOptions& options);
// Registers the network types in a runtime for evaluation. This is needed
// for resolving the type name to a runtime type `net.Address != type('foo')`.
absl::Status RegisterNetworkTypes(cel::TypeRegistry& registry,
const cel::RuntimeOptions& options);
} // namespace cel_codelab
#endif // THIRD_PARTY_CEL_CPP_CODELAB_NETWORK_FUNCTIONS_H_