Host.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #include "squid.h"
10 #include "anyp/Host.h"
11 
12 #include <iostream>
13 
14 std::optional<AnyP::Host>
16 {
17  // any preparsed IP value is acceptable
18  debugs(23, 7, ip);
19  return Host(ip);
20 }
21 
23 std::optional<AnyP::Host>
25 {
26  if (rawName.isEmpty()) {
27  debugs(23, 3, "rejecting empty name");
28  return std::nullopt;
29  }
30 
31  // Reject bytes incompatible with rfc1035NamePack() and ::matchDomainName()
32  // implementations (at least). Such bytes can come from percent-encoded HTTP
33  // URIs or length-based X.509 fields, for example. Higher-level parsers must
34  // reject or convert domain name encodings like UTF-16, but this low-level
35  // check works as an additional (albeit unreliable) layer of defense against
36  // those (unsupported by Squid DNS code) encodings.
37  if (rawName.find('\0') != SBuf::npos) {
38  debugs(83, 3, "rejecting ASCII NUL character in " << rawName);
39  return std::nullopt;
40  }
41 
42  // TODO: Consider rejecting names with isspace(3) bytes.
43 
44  debugs(23, 7, rawName);
45  return Host(rawName);
46 }
47 
48 std::optional<AnyP::Host>
50 {
51  if (rawName.find('*') != SBuf::npos) {
52  debugs(23, 3, "rejecting wildcard in " << rawName);
53  return std::nullopt;
54  }
55  return ParseDomainName(rawName);
56 }
57 
58 std::optional<AnyP::Host>
60 {
61  const static SBuf wildcardLabel("*.");
62  if (rawName.startsWith(wildcardLabel)) {
63  if (rawName.find('*', 2) != SBuf::npos) {
64  debugs(23, 3, "rejecting excessive wildcards in " << rawName);
65  return std::nullopt;
66  }
67  // else: fall through to final checks
68  } else {
69  if (rawName.find('*', 0) != SBuf::npos) {
70  // this case includes "*" and "example.*" input
71  debugs(23, 3, "rejecting unsupported wildcard in " << rawName);
72  return std::nullopt;
73  }
74  // else: fall through to final checks
75  }
76  return ParseDomainName(rawName);
77 }
78 
79 std::ostream &
80 AnyP::operator <<(std::ostream &os, const Host &host)
81 {
82  if (const auto ip = host.ip()) {
83  char buf[MAX_IPSTRLEN];
84  (void)ip->toStr(buf, sizeof(buf)); // no brackets
85  os << buf;
86  } else {
87  // If Host object creators start applying Uri::Decode() to reg-names,
88  // then we must start applying Uri::Encode() here, but only to names
89  // that require it. See "The reg-name syntax allows percent-encoded
90  // octets" paragraph in RFC 3986.
91  const auto domainName = host.domainName();
92  Assure(domainName);
93  os << *domainName;
94  }
95  return os;
96 }
97 
98 std::ostream &
99 AnyP::operator <<(std::ostream &os, const Bracketed &hostWrapper)
100 {
101  bool addBrackets = false;
102  if (const auto ip = hostWrapper.host.ip())
103  addBrackets = ip->isIPv6();
104 
105  if (addBrackets)
106  os << '[';
107  os << hostWrapper.host;
108  if (addBrackets)
109  os << ']';
110 
111  return os;
112 }
113 
size_type find(char c, size_type startPos=0) const
Definition: SBuf.cc:584
bool isEmpty() const
Definition: SBuf.h:435
static std::optional< Host > ParseSimpleDomainName(const SBuf &)
Definition: Host.cc:49
std::ostream & operator<<(std::ostream &, const Host &)
Definition: Host.cc:80
Definition: SBuf.h:93
bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive=caseSensitive) const
Definition: SBuf.cc:442
const Host & host
Definition: Host.h:67
#define MAX_IPSTRLEN
Length of buffer that needs to be allocated to old a null-terminated IP-string.
Definition: forward.h:25
static std::optional< Host > ParseDomainName(const SBuf &)
common parts of FromSimpleDomain() and FromWildDomain()
Definition: Host.cc:24
static std::optional< Host > ParseWildDomainName(const SBuf &)
Definition: Host.cc:59
static std::optional< Host > ParseIp(const Ip::Address &)
converts an already parsed IP address to a Host object
Definition: Host.cc:15
either a domain name (as defined in DNS RFC 1034) or an IP address
Definition: Host.h:24
auto domainName() const
stored domain name (if any)
Definition: Host.h:48
#define Assure(condition)
Definition: Assure.h:35
static const size_type npos
Definition: SBuf.h:100
auto ip() const
Definition: Host.h:45
Host(const Storage &raw)
Definition: Host.h:56
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors