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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
|
/** @file
HTTP configuration support.
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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
http://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.
*/
#pragma once
#include "tscore/ink_inet.h"
#include "tscore/ink_resolver.h"
#include "ts/apidefs.h"
#include "ts/apidefs.h"
#include "tscore/ink_assert.h"
#include "tscore/IpMap.h"
#include "tscore/MemArena.h"
#include <algorithm>
#include <array>
/// Load default inbound IP addresses from the configuration file.
void RecHttpLoadIp(const char *name, ///< Name of value in configuration file.
IpAddr &ip4, ///< [out] IPv4 address.
IpAddr &ip6 ///< [out] Ipv6 address.
);
/// Load up an IpMap with IP addresses from the configuration file.
void RecHttpLoadIpMap(const char *name, ///< Name of value in configuration file.
IpMap &ipmap ///< [out] IpMap.
);
/** A set of session protocols.
This depends on using @c SessionProtocolNameRegistry to get the indices.
*/
class SessionProtocolSet
{
using self_type = SessionProtocolSet; ///< Self reference type.
/// Storage for the set - a bit vector.
uint32_t m_bits = 0;
public:
static constexpr int MAX = sizeof(m_bits) * CHAR_BIT;
/// Default constructor.
/// Constructs and empty set.
SessionProtocolSet() = default;
uint32_t
indexToMask(int idx) const
{
return 0 <= idx && idx < static_cast<int>(MAX) ? static_cast<uint32_t>(1) << idx : 0;
}
/// Mark the protocol at @a idx as present.
void
markIn(int idx)
{
m_bits |= this->indexToMask(idx);
}
/// Mark all the protocols in @a that as present in @a this.
void
markIn(self_type const &that)
{
m_bits |= that.m_bits;
}
/// Mark the protocol at a idx as not present.
void
markOut(int idx)
{
m_bits &= ~this->indexToMask(idx);
}
/// Mark the protocols in @a that as not in @a this.
void
markOut(self_type const &that)
{
m_bits &= ~(that.m_bits);
}
/// Test if a protocol is in the set.
bool
contains(int idx) const
{
return 0 != (m_bits & this->indexToMask(idx));
}
/// Test if all the protocols in @a that are in @a this protocol set.
bool
contains(self_type const &that) const
{
return that.m_bits == (that.m_bits & m_bits);
}
/// Mark all possible protocols.
void
markAllIn()
{
m_bits = ~static_cast<uint32_t>(0);
}
/// Clear all protocols.
void
markAllOut()
{
m_bits = 0;
}
/// Check for intersection.
bool
intersects(self_type const &that)
{
return 0 != (m_bits & that.m_bits);
}
/// Check for empty set.
bool
isEmpty() const
{
return m_bits == 0;
}
/// Equality (identical sets).
bool
operator==(self_type const &that) const
{
return m_bits == that.m_bits;
}
};
// Predefined sets of protocols, useful for configuration.
extern SessionProtocolSet HTTP_PROTOCOL_SET;
extern SessionProtocolSet HTTP2_PROTOCOL_SET;
extern SessionProtocolSet DEFAULT_NON_TLS_SESSION_PROTOCOL_SET;
extern SessionProtocolSet DEFAULT_TLS_SESSION_PROTOCOL_SET;
extern SessionProtocolSet DEFAULT_QUIC_SESSION_PROTOCOL_SET;
const char *RecNormalizeProtoTag(const char *tag);
/** Registered session protocol names.
We do this to avoid lots of string compares. By normalizing the string names we can just compare
their indices in this table.
@internal To simplify the implementation we limit the maximum number of strings to 32. That will
be sufficient for the foreseeable future. We can come back to this if it ever becomes a problem.
@internal Because we have so few strings we just use a linear search. If the size gets much
larger we should consider doing something more clever.
@internal This supports providing constant strings because those strings are exported to the
C API and this logic @b must return exactly those pointers.
*/
class SessionProtocolNameRegistry
{
public:
static int constexpr MAX = SessionProtocolSet::MAX; ///< Maximum # of registered names.
static int constexpr INVALID = -1; ///< Normalized invalid index value.
static std::string_view convert_openssl_alpn_wire_format(int index);
using TextView = ts::TextView;
/// Default constructor.
/// Creates empty registry with no names.
SessionProtocolNameRegistry() = default;
/** Get the index for @a name, registering it if needed.
The name is copied internally.
@return The index for the registered @a name.
*/
int toIndex(TextView name);
/** Get the index for @a name, registering it if needed.
The caller @b guarantees @a name is persistent and immutable.
@return The index for the registered @a name.
*/
int toIndexConst(TextView name);
/** Convert a @a name to an index.
@return The index for @a name or @c INVALID if it is not registered.
*/
int indexFor(TextView name) const;
/** Convert an @a index to the corresponding name.
@return A pointer to the name or @c nullptr if the index isn't registered.
*/
TextView nameFor(int index) const;
/** Convert an @a index to the corresponding name in OpenSSL ALPN wire format.
OpenSSL ALPN wire format (vector of non-empty, 8-bit length-prefixed, byte strings)
https://www.openssl.org/docs/man1.1.1/man3/SSL_CTX_set_alpn_protos.html
@return A pointer to the name or @c nullptr if the index isn't registered.
*/
static TextView wireNameFor(int index);
/// Mark protocols as present in @a sp_set based on the names in @a value.
/// The names can be separated by ;/|,: and space.
/// @internal This is separated out to make it easy to access from the plugin API
/// implementation.
void markIn(const char *value, SessionProtocolSet &sp_set);
protected:
int m_n = 0; ///< Index of first unused slot.
std::array<TextView, MAX> m_names;
ts::MemArena m_arena; ///< Storage for non-constant strings.
};
extern SessionProtocolNameRegistry globalSessionProtocolNameRegistry;
/** Description of an proxy port.
This consolidates the options needed for proxy ports, both data
and parsing. It provides a static global set of ports for
convenience although it can be used with an externally provided
set.
Options are described by a colon separated list of keywords
without spaces. The options are applied in left to right order. If
options do not conflict the order is irrelevant.
IPv6 addresses must be enclosed by brackets. Unfortunate but colon is
so overloaded there's no other option.
*/
struct HttpProxyPort {
private:
typedef HttpProxyPort self; ///< Self reference type.
public:
/// Explicitly supported collection of proxy ports.
typedef std::vector<self> Group;
/// Type of transport on the connection.
enum TransportType {
TRANSPORT_NONE = 0, ///< Unspecified / uninitialized
TRANSPORT_DEFAULT, ///< Default (normal HTTP).
TRANSPORT_COMPRESSED, ///< Compressed HTTP.
TRANSPORT_BLIND_TUNNEL, ///< Blind tunnel (no processing).
TRANSPORT_SSL, ///< SSL connection.
TRANSPORT_PLUGIN, /// < Protocol plugin connection
TRANSPORT_QUIC, ///< SSL connection.
};
int m_fd; ///< Pre-opened file descriptor if present.
TransportType m_type = TRANSPORT_DEFAULT; ///< Type of connection.
in_port_t m_port = 0; ///< Port on which to listen.
uint8_t m_family = AF_INET; ///< IP address family.
/// True if proxy protocol is required on incoming requests.
bool m_proxy_protocol = false;
/// True if inbound connects (from client) are transparent.
bool m_inbound_transparent_p = false;
/// True if outbound connections (to origin servers) are transparent.
bool m_outbound_transparent_p = false;
// True if transparent pass-through is enabled on this port.
bool m_transparent_passthrough = false;
/// True if MPTCP is enabled on this port.
bool m_mptcp = false;
/// Local address for inbound connections (listen address).
IpAddr m_inbound_ip;
/// Local address for outbound connections (to origin server).
IpAddr m_outbound_ip4;
/// Local address for outbound connections (to origin server).
IpAddr m_outbound_ip6;
/// Ordered preference for DNS resolution family ( @c FamilyPrefence )
/// A value of @c PREFER_NONE indicates that entry and subsequent ones
/// are invalid.
HostResPreferenceOrder m_host_res_preference;
/// Static preference list that is the default value.
static HostResPreferenceOrder const DEFAULT_HOST_RES_PREFERENCE;
/// Enabled session transports for this port.
SessionProtocolSet m_session_protocol_preference;
/// Default constructor.
HttpProxyPort();
/** Select the local outbound address object.
@return The IP address for @a family
*/
IpAddr &outboundIp(uint16_t family ///< IP address family.
);
/// Check for SSL port.
bool isSSL() const;
/// Check for QUIC port.
bool isQUIC() const;
/// Check for SSL port.
bool isPlugin() const;
/// Process options text.
/// @a opts should not contain any whitespace, only the option string.
/// This object's internal state is updated as specified by @a opts.
/// @return @c true if a port option was successfully processed, @c false otherwise.
bool processOptions(const char *opts ///< String containing the options.
);
/** Global instance.
This is provided because most of the work with this data is used as a singleton
and it's handy to encapsulate it here.
*/
static std::vector<self> &global();
/// Check for SSL ports.
/// @return @c true if any port in @a ports is an SSL port.
static bool hasSSL(Group const &ports ///< Ports to check.
);
/// Check for SSL ports.
/// @return @c true if any global port is an SSL port.
static bool hasSSL();
/// Check for QUIC ports.
/// @return @c true if any port in @a ports is an QUIC port.
static bool hasQUIC(Group const &ports ///< Ports to check.
);
/// Check for QUIC ports.
/// @return @c true if any global port is an QUIC port.
static bool hasQUIC();
/** Load all relevant configuration data.
This is hardwired to look up the appropriate values in the
configuration files. It clears @a ports and then loads all found
values in to it.
@return @c true if at least one valid port description was
found, @c false if none.
*/
static bool loadConfig(std::vector<self> &ports ///< Destination for found port data.
);
/** Load all relevant configuration data into the global ports.
@return @c true if at least one valid port description was
found, @c false if none.
*/
static bool loadConfig();
/** Load ports from a value string.
Load ports from single string with port descriptors. Ports
found are added to @a ports. @a value may safely be @c nullptr or empty.
@note This is used primarily internally but is available if needed.
@return @c true if a valid port was found, @c false if none.
*/
static bool loadValue(std::vector<self> &ports, ///< Destination for found port data.
const char *value ///< Source port data.
);
/** Load ports from a value string into the global ports.
Load ports from single string of port descriptors into the
global set of ports. @a value may safely be @c nullptr or empty.
@return @c true if a valid port was found, @c false if none.
*/
static bool loadValue(const char *value ///< Source port data.
);
/// Load default value if @a ports is empty.
/// @return @c true if the default was needed / loaded.
static bool loadDefaultIfEmpty(std::vector<self> &ports ///< Load target.
);
/// Load default value into the global set if it is empty.
/// @return @c true if the default was needed / loaded.
static bool loadDefaultIfEmpty();
/** Find an HTTP port in @a ports.
If @a family is specified then only ports for that family
are checked.
@return The port if found, @c nullptr if not.
*/
static const self *findHttp(Group const &ports, ///< Group to search.
uint16_t family = AF_UNSPEC ///< Desired address family.
);
/** Find an HTTP port in the global ports.
If @a family is specified then only ports for that family
are checked.
@return The port if found, @c nullptr if not.
*/
static const self *findHttp(uint16_t family = AF_UNSPEC);
/** Create text description to be used for inter-process access.
Prints the file descriptor and then any options.
@return The number of characters used for the description.
*/
int print(char *out, ///< Output string.
size_t n ///< Maximum output length.
);
static const char *const PORTS_CONFIG_NAME; ///< New unified port descriptor.
/// Default value if no other values can be found.
static const char *const DEFAULT_VALUE;
// Keywords (lower case versions, but compares should be case insensitive)
static const char *const OPT_FD_PREFIX; ///< Prefix for file descriptor value.
static const char *const OPT_OUTBOUND_IP_PREFIX; ///< Prefix for inbound IP address.
static const char *const OPT_INBOUND_IP_PREFIX; ///< Prefix for outbound IP address.
static const char *const OPT_IPV6; ///< IPv6.
static const char *const OPT_IPV4; ///< IPv4
static const char *const OPT_TRANSPARENT_INBOUND; ///< Inbound transparent.
static const char *const OPT_TRANSPARENT_OUTBOUND; ///< Outbound transparent.
static const char *const OPT_TRANSPARENT_FULL; ///< Full transparency.
static const char *const OPT_TRANSPARENT_PASSTHROUGH; ///< Pass-through non-HTTP.
static const char *const OPT_SSL; ///< SSL (experimental)
static const char *const OPT_QUIC; ///< QUIC (experimental)
static const char *const OPT_PROXY_PROTO; ///< Proxy Protocol
static const char *const OPT_PLUGIN; ///< Protocol Plugin handle (experimental)
static const char *const OPT_BLIND_TUNNEL; ///< Blind tunnel.
static const char *const OPT_COMPRESSED; ///< Compressed.
static const char *const OPT_HOST_RES_PREFIX; ///< Set DNS family preference.
static const char *const OPT_PROTO_PREFIX; ///< Transport layer protocols.
static const char *const OPT_MPTCP; ///< MPTCP.
static std::vector<self> &m_global; ///< Global ("default") data.
protected:
/// Process @a value for DNS resolution family preferences.
void processFamilyPreference(const char *value);
/// Process @a value for session protocol preferences.
void processSessionProtocolPreference(const char *value);
/** Check a prefix option and find the value.
@return The address of the start of the value, or @c nullptr if the prefix doesn't match.
*/
const char *checkPrefix(char const *src ///< Input text
,
const char *prefix ///< Keyword prefix
,
size_t prefix_len ///< Length of keyword prefix.
);
};
inline bool
HttpProxyPort::isSSL() const
{
return TRANSPORT_SSL == m_type;
}
inline bool
HttpProxyPort::isQUIC() const
{
return TRANSPORT_QUIC == m_type;
}
inline bool
HttpProxyPort::isPlugin() const
{
return TRANSPORT_PLUGIN == m_type;
}
inline IpAddr &
HttpProxyPort::outboundIp(uint16_t family)
{
static IpAddr invalid; // dummy to make compiler happy about return.
if (AF_INET == family)
return m_outbound_ip4;
else if (AF_INET6 == family)
return m_outbound_ip6;
ink_release_assert(!"Invalid family for outbound address on proxy port.");
return invalid; // never happens but compiler insists.
}
inline bool
HttpProxyPort::loadValue(const char *value)
{
return self::loadValue(m_global, value);
}
inline bool
HttpProxyPort::loadConfig()
{
return self::loadConfig(m_global);
}
inline bool
HttpProxyPort::loadDefaultIfEmpty()
{
return self::loadDefaultIfEmpty(m_global);
}
inline std::vector<HttpProxyPort> &
HttpProxyPort::global()
{
return m_global;
}
inline bool
HttpProxyPort::hasSSL()
{
return self::hasSSL(m_global);
}
inline bool
HttpProxyPort::hasQUIC()
{
return self::hasQUIC(m_global);
}
inline const HttpProxyPort *
HttpProxyPort::findHttp(uint16_t family)
{
return self::findHttp(m_global, family);
}
/** Session Protocol initialization.
This must be called before any proxy port parsing is done.
*/
extern void ts_session_protocol_well_known_name_indices_init();
|