1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 /*****************************************************************************
25  *
26  * P_SplitDNSProcessor.h - Interface to DNS server selection
27  *
28  *
29  ****************************************************************************/
30 
31 #pragma once
32 
33 #include "ProxyConfig.h"
34 
35 #include "tscore/HostLookup.h"
36 
37 /* ---------------------------
38    forward declarations ...
39    --------------------------- */
40 void ink_split_dns_init(ts::ModuleVersion version);
41 
42 struct RequestData;
43 
44 struct matcher_line;
45 
46 class SplitDNSRecord;
47 struct SplitDNSResult;
48 
49 struct DNSServer;
50 
51 enum DNSResultType {
52   DNS_SRVR_UNDEFINED = 0,
53   DNS_SRVR_SPECIFIED,
54   DNS_SRVR_FAIL,
55 };
56 
57 typedef ControlMatcher<SplitDNSRecord, SplitDNSResult> DNS_table;
58 
59 /* --------------------------------------------------------------
60    **                struct SplitDNSResult
61    -------------------------------------------------------------- */
62 struct SplitDNSResult {
63   SplitDNSResult();
64 
65   /* ------------
66      public
67      ------------ */
68   DNSResultType r = DNS_SRVR_UNDEFINED;
69 
70   int m_line_number = 0;
71 
72   SplitDNSRecord *m_rec = nullptr;
73 };
74 
75 /* --------------------------------------------------------------
76    **                struct SplitDNS
77    -------------------------------------------------------------- */
78 struct SplitDNS : public ConfigInfo {
79   SplitDNS();
80   ~SplitDNS() override;
81 
82   void *getDNSRecord(const char *hostname);
83   void findServer(RequestData *rdata, SplitDNSResult *result);
84 
85   DNS_table *m_DNSSrvrTable = nullptr;
86 
87   int32_t m_SplitDNSlEnable = 0;
88 
89   /* ----------------------------
90      required by the alleged fast
91      path
92      ---------------------------- */
93   bool m_bEnableFastPath               = false;
94   HostLookup::LeafArray *m_pxLeafArray = nullptr;
95   int m_numEle                         = 0;
96 };
97 
98 /* --------------------------------------------------------------
99    SplitDNSConfig::isSplitDNSEnabled()
100    -------------------------------------------------------------- */
101 TS_INLINE bool
isSplitDNSEnabled()102 SplitDNSConfig::isSplitDNSEnabled()
103 {
104   return (gsplit_dns_enabled ? true : false);
105 }
106 
107 //
108 // End API to outside world
109 //
110 
111 /* --------------------------------------------------------------
112    **                class DNSRequestData
113 
114    A record for an single server
115    -------------------------------------------------------------- */
116 class DNSRequestData : public RequestData
117 {
118 public:
119   DNSRequestData();
120 
121   char *get_string() override;
122 
123   const char *get_host() override;
124 
125   sockaddr const *get_ip() override;        // unused required virtual method.
126   sockaddr const *get_client_ip() override; // unused required virtual method.
127 
128   const char *m_pHost = nullptr;
129 };
130 
131 /* --------------------------------------------------------------
132    DNSRequestData::get_string()
133    -------------------------------------------------------------- */
134 TS_INLINE
DNSRequestData()135 DNSRequestData::DNSRequestData() {}
136 
137 /* --------------------------------------------------------------
138    DNSRequestData::get_string()
139    -------------------------------------------------------------- */
140 TS_INLINE char *
get_string()141 DNSRequestData::get_string()
142 {
143   return ats_strdup((char *)m_pHost);
144 }
145 
146 /* --------------------------------------------------------------
147    DNSRequestData::get_host()
148    -------------------------------------------------------------- */
149 TS_INLINE const char *
get_host()150 DNSRequestData::get_host()
151 {
152   return m_pHost;
153 }
154 
155 /* --------------------------------------------------------------
156    DNSRequestData::get_ip()
157    -------------------------------------------------------------- */
158 TS_INLINE sockaddr const *
get_ip()159 DNSRequestData::get_ip()
160 {
161   return nullptr;
162 }
163 
164 /* --------------------------------------------------------------
165    DNSRequestData::get_client_ip()
166    -------------------------------------------------------------- */
167 TS_INLINE sockaddr const *
get_client_ip()168 DNSRequestData::get_client_ip()
169 {
170   return nullptr;
171 }
172 
173 /* --------------------------------------------------------------
174    *                 class SplitDNSRecord
175 
176    A record for a configuration line in the splitdns.config file
177    -------------------------------------------------------------- */
178 class SplitDNSRecord : public ControlBase
179 {
180 public:
181   SplitDNSRecord();
182   ~SplitDNSRecord();
183 
184   Result Init(matcher_line *line_info);
185 
186   const char *ProcessDNSHosts(char *val);
187   const char *ProcessDomainSrchList(char *val);
188   const char *ProcessDefDomain(char *val);
189 
190   void UpdateMatch(SplitDNSResult *result, RequestData *rdata);
191   void Print() const;
192 
193   DNSServer m_servers;
194   int m_dnsSrvr_cnt      = 0;
195   int m_domain_srch_list = 0;
196 };
197 
198 /* --------------------------------------------------------------
199    SplitDNSRecord::SplitDNSRecord()
200    -------------------------------------------------------------- */
201 TS_INLINE
SplitDNSRecord()202 SplitDNSRecord::SplitDNSRecord() {}
203 
204 /* --------------------------------------------------------------
205    SplitDNSRecord::~SplitDNSRecord()
206    -------------------------------------------------------------- */
~SplitDNSRecord()207 TS_INLINE SplitDNSRecord::~SplitDNSRecord() {}
208