|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2026 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +// IP allow/deny access-control collaborator. Internal header; only |
| 22 | +// reachable when compiling libhttpserver translation units. NOT part of |
| 23 | +// the installed surface; consumers cannot reach it through the public |
| 24 | +// umbrella. |
| 25 | +#if !defined(HTTPSERVER_COMPILATION) |
| 26 | +#error "ip_access_control.hpp is internal; only reachable when compiling libhttpserver." |
| 27 | +#endif |
| 28 | + |
| 29 | +#ifndef SRC_HTTPSERVER_DETAIL_IP_ACCESS_CONTROL_HPP_ |
| 30 | +#define SRC_HTTPSERVER_DETAIL_IP_ACCESS_CONTROL_HPP_ |
| 31 | + |
| 32 | +#include <set> |
| 33 | +#include <shared_mutex> |
| 34 | +#include <string> |
| 35 | +#include <string_view> |
| 36 | + |
| 37 | +#include "httpserver/ip_representation.hpp" |
| 38 | + |
| 39 | +namespace httpserver { |
| 40 | +namespace detail { |
| 41 | + |
| 42 | +// Owns the deny/allow IP sets and their locks — the whole of the |
| 43 | +// webserver's IP access-control state. Extracted from webserver_impl so |
| 44 | +// that responsibility lives behind one boundary: policy_callback consults |
| 45 | +// it via classify(); webserver::{deny,allow,remove_denied,remove_allowed}_ip |
| 46 | +// mutate it. No other state cluster on webserver_impl shares these mutexes. |
| 47 | +class ip_access_control { |
| 48 | + public: |
| 49 | + // Add @p ip (an IP literal or wildcard pattern) to the deny / allow |
| 50 | + // list, preserving the wildcard-precedence invariant (see |
| 51 | + // insert_wildcard_aware). remove_* erase the exact entry previously |
| 52 | + // added. All four take the relevant list's mutex internally. |
| 53 | + void deny(std::string_view ip); |
| 54 | + void remove_denied(std::string_view ip); |
| 55 | + void allow(std::string_view ip); |
| 56 | + void remove_allowed(std::string_view ip); |
| 57 | + |
| 58 | + struct membership { |
| 59 | + bool denied = false; |
| 60 | + bool allowed = false; |
| 61 | + }; |
| 62 | + |
| 63 | + // Consistent deny/allow snapshot for @p peer. Both lists are read with |
| 64 | + // their shared locks held together, matching the pre-extraction |
| 65 | + // policy_callback semantics (no mutation can interleave between the two |
| 66 | + // reads). |
| 67 | + [[nodiscard]] membership classify( |
| 68 | + const http::ip_representation& peer) const; |
| 69 | + |
| 70 | + private: |
| 71 | + // Insert @p t_ip into @p list, preserving the invariant that a |
| 72 | + // less-specific (lower weight()) wildcard entry takes precedence over a |
| 73 | + // more-specific one. When the incoming entry is less specific than a |
| 74 | + // stored match, the stored entry is erased first so the wildcard wins; |
| 75 | + // when it is equal or more specific, std::set::insert is a no-op and the |
| 76 | + // existing entry is kept. The unconditional insert covers all three |
| 77 | + // cases: (1) no existing entry — insert; (2) equal/higher weight — no-op |
| 78 | + // insert; (3) lower weight — erase first, then insert. This works only |
| 79 | + // because ip_representation::operator< treats OVERLAPPING entries as |
| 80 | + // equivalent: octets masked out on either side are excluded from the |
| 81 | + // comparison, so a wildcard and any address it matches compare equal |
| 82 | + // under std::set's ordering. find(t_ip) may therefore return a DIFFERENT |
| 83 | + // stored entry — one the new entry subsumes or collides with — not just |
| 84 | + // an exact duplicate; that comparator invariant is what the |
| 85 | + // erase-then-insert relies on. Caller holds the list's mutex. |
| 86 | + static void insert_wildcard_aware(std::set<http::ip_representation>& list, |
| 87 | + const http::ip_representation& t_ip); |
| 88 | + |
| 89 | + mutable std::shared_mutex deny_list_mutex_; |
| 90 | + std::set<http::ip_representation> deny_list_; |
| 91 | + |
| 92 | + mutable std::shared_mutex allow_list_mutex_; |
| 93 | + std::set<http::ip_representation> allow_list_; |
| 94 | +}; |
| 95 | + |
| 96 | +} // namespace detail |
| 97 | +} // namespace httpserver |
| 98 | + |
| 99 | +#endif // SRC_HTTPSERVER_DETAIL_IP_ACCESS_CONTROL_HPP_ |
0 commit comments