132 const auto first = specific();
133 const auto state =
classify_mapping(first.result, first.client, first.internal_port, first.description, wanted);
134 if (!recorded || first.result != 0 || first.internal_port !=
"0" || first.client != wanted.
target || first.description != wanted.
description) {
135 return {state, first.enabled, first.lease};
137 if (first.enabled !=
"0" && first.enabled !=
"1") {
140 const auto parse_number = [](
const std::string &text) -> std::optional<std::uint32_t> {
141 std::uint32_t number = 0;
142 const auto parsed = std::from_chars(text.data(), text.data() + text.size(), number);
143 if (parsed.ec != std::errc {} || parsed.ptr != text.data() + text.size()) {
149 std::optional<mapping_reply_t> match;
150 std::size_t match_index = 0;
151 bool complete =
false;
153 for (std::size_t index = 0; index < 256; ++index) {
154 auto entry =
generic(index);
155 if (entry.result == 713) {
159 if (entry.result != 0) {
162 const auto external_port = parse_number(entry.external_port);
163 const auto internal_port = parse_number(entry.internal_port);
164 if (!external_port || *external_port == 0 || *external_port > 65535 || !internal_port || *internal_port == 0 || *internal_port > 65535 || entry.client.empty() || (entry.protocol !=
"TCP" && entry.protocol !=
"UDP") || (entry.enabled !=
"0" && entry.enabled !=
"1")) {
167 if (entry.external_port != wanted.
external_port || entry.protocol != wanted.
protocol || (!entry.remote_host.empty() && entry.remote_host !=
"0.0.0.0")) {
170 if (match || entry.client != wanted.
target || entry.internal_port != wanted.
internal_port || entry.enabled != first.enabled) {
173 match = std::move(entry);
176 if (!complete || !match) {
182 const auto last = specific();
183 if (last.result != 0 || last.client != first.client || last.internal_port != first.internal_port || last.description != first.description || last.enabled != first.enabled) {
188 const auto current =
generic(match_index);
189 if (current.result != 0 || current.external_port != wanted.
external_port || current.protocol != wanted.
protocol || (!current.remote_host.empty() && current.remote_host !=
"0.0.0.0") || current.client != wanted.
target || current.internal_port != wanted.
internal_port || current.enabled != first.enabled) {
192 auto duration = parse_number(first.lease);
196 for (
const auto *reply : {&std::as_const(*match), &last, ¤t}) {
197 const auto seconds = parse_number(reply->lease);
198 if (!seconds || ((*duration == 0) != (*seconds == 0))) {
201 duration = std::min(*duration, *seconds);
242 std::vector<owned_mapping_t> mappings;
243 std::istringstream input {text};
245 while (std::getline(input, line)) {
246 std::istringstream entry {line};
253 const auto found = std::find(mappings.begin(), mappings.end(), mapping);
254 if (event ==
'+' && found == mappings.end()) {
255 mappings.push_back(std::move(mapping));
256 }
else if (event ==
'-' && found != mappings.end()) {
257 mappings.erase(found);
mapping_lookup_t verify_mapping_lookup(const owned_mapping_t &wanted, bool recorded, Specific specific, Generic generic)
Verify an owned rule even when GetSpecificPortMappingEntry reports port zero.
mapping_state_e classify_mapping(int result, std::string_view client, std::string_view port, std::string_view description, const owned_mapping_t &wanted)
Classify a lookup without treating a destination alone as proof of ownership.