MCPcopy Create free account
hub / github.com/cel-expr/cel-cpp / ParseAddressImpl

Function ParseAddressImpl

codelab/network_functions.cc:84–120  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

82}
83
84absl::StatusOr<IpVersion> ParseAddressImpl(absl::string_view str,
85 uint32_t* ipv4_out,
86 absl::Span<char> ipv6_out) {
87 if (str.size() < 2 || str.size() > 39) {
88 return absl::InvalidArgumentError("unsupported address format (length)");
89 }
90 if (absl::StrContains(str, ":")) {
91 if (ipv6_out.size() < 16) {
92 return absl::InternalError("invalid outbuffer in parse call");
93 }
94 return absl::InvalidArgumentError("unsupported address format (ipv6)");
95 }
96 uint32_t ipv4 = 0;
97 int octet = 0;
98 for (auto part : absl::StrSplit(str, '.')) {
99 if (octet >= 4) {
100 return absl::InvalidArgumentError(
101 "unsupported address format (invalid ipv4)");
102 }
103 int octet_val;
104 if (!absl::SimpleAtoi(part, &octet_val) || octet_val > 255 ||
105 octet_val < 0) {
106 return absl::InvalidArgumentError(
107 "unsupported address format (invalid ipv4)");
108 }
109 ipv4 <<= 8;
110 ipv4 |= (uint32_t)octet_val;
111
112 octet++;
113 }
114 if (octet != 4) {
115 return absl::InvalidArgumentError(
116 "unsupported address format (invalid ipv4)");
117 }
118 *ipv4_out = ipv4;
119 return IpVersion::kIPv4;
120}
121
122absl::Status ConfigureNetworkFunctions(cel::TypeCheckerBuilder& builder) {
123 // Type identifiers

Callers 1

ParseMethod · 0.85

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected