MySQL 9.1.0
Source Code Documentation
gcs_xcom_networking.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef GCS_XCOM_NETWORKING_H
25#define GCS_XCOM_NETWORKING_H
26
27#include <map>
28#include <string>
29#include <vector>
30
33
34#ifdef _WIN32
35typedef u_short sa_family_t;
36#endif
37
38/**
39 * @brief Interface to decouple XCom sock_probe implementation to allow
40 * unit testing.
41 */
43 public:
45 virtual ~Gcs_sock_probe_interface() = default;
46
47 virtual int init_sock_probe(sock_probe *s) = 0;
48 virtual int number_of_interfaces(sock_probe *s) = 0;
50 struct sockaddr **out) = 0;
52 struct sockaddr **out) = 0;
53 virtual char *get_if_name(sock_probe *s, int count) = 0;
54 virtual bool_t is_if_running(sock_probe *s, int count) = 0;
55 virtual void close_sock_probe(sock_probe *s) = 0;
56
59 default; // move constructor must be explicit because copy is
61 default;
63 default; // move assignment must be explicit because move is
64};
65
66/**
67 * @brief Implementation of @class Gcs_sock_probe_interface
68 */
70 public:
72 ~Gcs_sock_probe_interface_impl() override = default;
73
74 int init_sock_probe(sock_probe *s) override;
75 int number_of_interfaces(sock_probe *s) override;
77 struct sockaddr **out) override;
79 struct sockaddr **out) override;
80 char *get_if_name(sock_probe *s, int count) override;
81 bool_t is_if_running(sock_probe *s, int count) override;
82 void close_sock_probe(sock_probe *s) override;
83
86 default; // move constructor must be explicit because copy is
88 const Gcs_sock_probe_interface_impl &) = default;
90 default; // move assignment must be explicit because move is
91};
92
93/**
94 This function gets all network addresses on this host and their
95 subnet masks as a string.
96
97 @param[out] sock_probe Socket probe interface
98 @param[out] out maps IP addresses to subnetmasks
99 @param filter_out_inactive If set to true, only active interfaces will be
100 added to out
101 @return false on success, true otherwise.
102 */
104 std::map<std::string, int> &out,
105 bool filter_out_inactive = false);
106
107/**
108 This function gets all private network addresses and their
109 subnet masks as a string. IPv4 only. (SOCK_STREAM only)
110 @param[out] out maps IP addresses to subnetmasks
111 @param filter_out_inactive If set to true, only active interfaces will be added
112 to out
113 @return false on success, true otherwise.
114 */
115bool get_local_private_addresses(std::map<std::string, int> &out,
116 bool filter_out_inactive = false);
117
118/**
119 This function translates hostnames to all possible IP addresses.
120
121 @param[in] name The hostname to translate.
122 @param[out] ips The IP addresses after translation.
123
124 @return false on success, true otherwise.
125 */
127 std::string name, std::vector<std::pair<sa_family_t, std::string>> &ips);
128
129/**
130 This function translates hostname to all possible IP addresses.
131
132 @param[in] name The hostname to translate.
133 @param[out] ip The IP addresses after translation.
134
135 @return false on success, true otherwise.
136 */
137bool resolve_ip_addr_from_hostname(std::string name,
138 std::vector<std::string> &ip);
139
140/**
141 Converts an address in string format (X.X.X.X/XX) into network octet format
142
143 @param[in] addr IP address in X.X.X.X format
144 @param[in] mask Network mask associated with the address
145 @param[out] out_pair IP address and netmask, in binary format (network byte
146 order)
147
148 @return false on success, true otherwise.
149 */
150bool get_address_for_allowlist(std::string addr, std::string mask,
151 std::pair<std::vector<unsigned char>,
152 std::vector<unsigned char>> &out_pair);
153
154/**
155 @class Gcs_ip_allowlist_entry
156 @brief Base abstract class for the allowlist entries.
157
158 This is the base class for the Allowlist entries. Any derived class must
159 implement its two abstract methods:
160 - init_value();
161 - get_value();
162 */
164 public:
165 /**
166 Constructor
167
168 @param[in] addr IP address or hostname of this entry
169 @param[in] mask Network mask of this entry.
170 */
171 Gcs_ip_allowlist_entry(std::string addr, std::string mask);
172
173 virtual ~Gcs_ip_allowlist_entry() = default;
174
175 /**
176 Entry initialization.
177
178 If one needs to initialize internal values, it should be done in this
179 method.
180
181 @return false on success, true otherwise
182 */
183 virtual bool init_value() = 0;
184
185 /**
186 Virtual Method that implements value retrieval for this entry.
187
188 The returned value must be a list of std::pairs that contains both the
189 address and the mask in network octet value. This is in list format because
190 in the case of allowlist names, we can have multiple value for the same
191 entry
192
193 @return an std::vector of std::pair with ip and mask in network octet form
194 */
195 virtual std::vector<
196 std::pair<std::vector<unsigned char>, std::vector<unsigned char>>>
197 *get_value() = 0;
198
199 /** Getters */
200 std::string get_addr() const { return m_addr; }
201 std::string get_mask() const { return m_mask; }
202
203 private:
204 std::string m_addr;
205 std::string m_mask;
206};
207
210 const Gcs_ip_allowlist_entry *rhs) const {
211 // Check if addresses are different in content
212 if (lhs->get_addr() != rhs->get_addr()) { // Then compare only the
213 // addresses
214 return lhs->get_addr() < rhs->get_addr();
215 } else { // If addresses are equal, then compare the masks to untie.
216 return lhs->get_mask() < rhs->get_mask();
217 }
218 }
219};
220
221/**
222 @class Gcs_ip_allowlist_entry_ip
223 @brief Implementation of Gcs_ip_allowlist_entry to use with
224 raw IP addresses in format X.X.X.X/XX
225 */
227 public:
228 Gcs_ip_allowlist_entry_ip(std::string addr, std::string mask);
229
230 public:
231 bool init_value() override;
232 std::vector<std::pair<std::vector<unsigned char>, std::vector<unsigned char>>>
233 *get_value() override;
234
235 private:
236 std::pair<std::vector<unsigned char>, std::vector<unsigned char>> m_value;
237};
238
239/**
240 @class Gcs_ip_allowlist_entry_hostname
241 @brief Implementation of Gcs_ip_allowlist_entry to use with
242 hostnames
243 */
245 public:
246 Gcs_ip_allowlist_entry_hostname(std::string addr, std::string mask);
247 Gcs_ip_allowlist_entry_hostname(std::string addr);
248
249 public:
250 bool init_value() override;
251 std::vector<std::pair<std::vector<unsigned char>, std::vector<unsigned char>>>
252 *get_value() override;
253};
254
256 public:
257 static const std::string DEFAULT_ALLOWLIST;
258
259 private:
261 private:
262 /**
263 * @brief When true, it is locked. When false it is not.
264 */
265 std::atomic_flag &m_guard;
266
267 public:
268 Atomic_lock_guard(std::atomic_flag &guard) : m_guard(guard) {
269 // keep trying until it is unlocked, then lock
270 while (m_guard.test_and_set()) {
271 std::this_thread::yield();
272 }
273 }
274
276 };
277
278 private:
279 /*
280 The IP allowlist. It is a list of tuples Hexadecimal IP number
281 and subnet mask also in Hexadecimal. E.g.: 192.168.1.2/24 or 127.0.0.1/32.
282
283 This is for optimization purposes, so that we don't calculate the
284 values each time we want to check.
285 */
286 std::set<Gcs_ip_allowlist_entry *, Gcs_ip_allowlist_entry_pointer_comparator>
288
289 /**
290 This is the list that originally submitted to be parsed and to configure
291 the allowlist.
292 */
293 std::string m_original_list;
294
295 public:
297 m_atomic_guard.clear();
298 }
299 virtual ~Gcs_ip_allowlist();
300
301 /**
302 This member function shall be used to configure the allowlist.
303
304 @param the_list The list with IP addresses. This list is a comma separated
305 list formatted only with IP addresses and/or in the form of
306 a subnet range, e.g., IP/netbits.
307 @return true if the configuration failed, false otherwise.
308 */
309 bool configure(const std::string &the_list);
310
311 /**
312 This member function shall be used to validate the list that is used as
313 input to the configure member function.
314
315 @param the_list The list with IP addresses. This list is a comma separated
316 list formatted only with IP addresses and/or in the form of
317 a subnet range, e.g., IP/netbits.
318
319 @return true if the configuration failed, false otherwise.
320 */
321 bool is_valid(const std::string &the_list);
322
323 /**
324 This member function SHALL return true if the given IP is to be blocked,
325 false otherwise.
326
327 @param ip_addr a string representation of an IPv4 address.
328 @param xcom_config the latest XCom configuration.
329
330 @return true if the ip should be blocked, false otherwise.
331 */
332 bool shall_block(const std::string &ip_addr,
333 site_def const *xcom_config = nullptr);
334
335 /**
336 This member function SHALL return true if the IP of the given file
337 descriptor is to be blocked, false otherwise.
338
339 @param fd the file descriptor of the accepted socket to check.
340 @param xcom_config the latest XCom configuration.
341
342 @return true if the ip should be blocked, false otherwise.
343 */
344 bool shall_block(int fd, site_def const *xcom_config = nullptr);
345
346 /**
347 This member function gets the textual representation of the list as
348 provided to the configure member function.
349 */
350 const std::string get_configured_ip_allowlist() {
351 // lock the list
353 return m_original_list;
354 }
355
356 /**
357 A string representation of the internal list of IP addresses. Can have
358 more addresses than those submitted through the configure member
359 function, since there are addresses that are implicitly added when
360 configuring the list.
361 */
362 std::string to_string() const;
363
364 private:
365 bool do_check_block(struct sockaddr_storage *sa,
366 site_def const *xcom_config) const;
368 std::vector<unsigned char> const &incoming_octets) const;
369 bool do_check_block_xcom(std::vector<unsigned char> const &incoming_octets,
370 site_def const *xcom_config) const;
371 bool add_address(std::string addr, std::string mask);
372
373 /**
374 @brief Clears the contents of this Allowlist object.
375
376 It deletes all entries and clears the internal set.
377 */
378 void clear();
379
380 /**
381 * @brief An atomic lock to guard the ip allowlist.
382 */
383 std::atomic_flag m_atomic_guard;
384
385 private:
388};
389
390#endif /* GCS_XCOM_NETWORKING_H */
Definition: gcs_xcom_networking.h:260
Atomic_lock_guard(std::atomic_flag &guard)
Definition: gcs_xcom_networking.h:268
std::atomic_flag & m_guard
When true, it is locked.
Definition: gcs_xcom_networking.h:265
~Atomic_lock_guard()
Definition: gcs_xcom_networking.h:275
Implementation of Gcs_ip_allowlist_entry to use with hostnames.
Definition: gcs_xcom_networking.h:244
Gcs_ip_allowlist_entry_hostname(std::string addr, std::string mask)
Definition: gcs_xcom_networking.cc:512
std::vector< std::pair< std::vector< unsigned char >, std::vector< unsigned char > > > * get_value() override
Virtual Method that implements value retrieval for this entry.
Definition: gcs_xcom_networking.cc:523
bool init_value() override
Entry initialization.
Definition: gcs_xcom_networking.cc:520
Implementation of Gcs_ip_allowlist_entry to use with raw IP addresses in format X....
Definition: gcs_xcom_networking.h:226
bool init_value() override
Entry initialization.
Definition: gcs_xcom_networking.cc:499
Gcs_ip_allowlist_entry_ip(std::string addr, std::string mask)
Definition: gcs_xcom_networking.cc:495
std::vector< std::pair< std::vector< unsigned char >, std::vector< unsigned char > > > * get_value() override
Virtual Method that implements value retrieval for this entry.
Definition: gcs_xcom_networking.cc:506
std::pair< std::vector< unsigned char >, std::vector< unsigned char > > m_value
Definition: gcs_xcom_networking.h:236
Base abstract class for the allowlist entries.
Definition: gcs_xcom_networking.h:163
virtual ~Gcs_ip_allowlist_entry()=default
std::string m_mask
Definition: gcs_xcom_networking.h:205
std::string m_addr
Definition: gcs_xcom_networking.h:204
virtual bool init_value()=0
Entry initialization.
std::string get_mask() const
Definition: gcs_xcom_networking.h:201
virtual std::vector< std::pair< std::vector< unsigned char >, std::vector< unsigned char > > > * get_value()=0
Virtual Method that implements value retrieval for this entry.
std::string get_addr() const
Getters.
Definition: gcs_xcom_networking.h:200
Gcs_ip_allowlist_entry(std::string addr, std::string mask)
Constructor.
Definition: gcs_xcom_networking.cc:491
Definition: gcs_xcom_networking.h:255
bool do_check_block_xcom(std::vector< unsigned char > const &incoming_octets, site_def const *xcom_config) const
Definition: gcs_xcom_networking.cc:832
std::set< Gcs_ip_allowlist_entry *, Gcs_ip_allowlist_entry_pointer_comparator > m_ip_allowlist
Definition: gcs_xcom_networking.h:287
std::string to_string() const
A string representation of the internal list of IP addresses.
Definition: gcs_xcom_networking.cc:573
Gcs_ip_allowlist(Gcs_ip_allowlist const &)
const std::string get_configured_ip_allowlist()
This member function gets the textual representation of the list as provided to the configure member ...
Definition: gcs_xcom_networking.h:350
bool do_check_block(struct sockaddr_storage *sa, site_def const *xcom_config) const
Definition: gcs_xcom_networking.cc:900
Gcs_ip_allowlist & operator=(Gcs_ip_allowlist const &)
Gcs_ip_allowlist()
Definition: gcs_xcom_networking.h:296
bool shall_block(const std::string &ip_addr, site_def const *xcom_config=nullptr)
This member function SHALL return true if the given IP is to be blocked, false otherwise.
Definition: gcs_xcom_networking.cc:986
static const std::string DEFAULT_ALLOWLIST
Definition: gcs_xcom_networking.h:257
bool do_check_block_allowlist(std::vector< unsigned char > const &incoming_octets) const
Definition: gcs_xcom_networking.cc:795
virtual ~Gcs_ip_allowlist()
Definition: gcs_xcom_networking.cc:770
void clear()
Clears the contents of this Allowlist object.
Definition: gcs_xcom_networking.cc:761
bool configure(const std::string &the_list)
This member function shall be used to configure the allowlist.
Definition: gcs_xcom_networking.cc:641
std::string m_original_list
This is the list that originally submitted to be parsed and to configure the allowlist.
Definition: gcs_xcom_networking.h:293
bool add_address(std::string addr, std::string mask)
Definition: gcs_xcom_networking.cc:772
std::atomic_flag m_atomic_guard
An atomic lock to guard the ip allowlist.
Definition: gcs_xcom_networking.h:383
bool is_valid(const std::string &the_list)
This member function shall be used to validate the list that is used as input to the configure member...
Definition: gcs_xcom_networking.cc:587
Definition: gcs_xcom_networking.h:69
Gcs_sock_probe_interface_impl()
Definition: gcs_xcom_networking.h:71
Gcs_sock_probe_interface_impl & operator=(Gcs_sock_probe_interface_impl &&)=default
void close_sock_probe(sock_probe *s) override
Definition: gcs_xcom_networking.cc:474
Gcs_sock_probe_interface_impl(Gcs_sock_probe_interface_impl &)=default
void get_sockaddr_netmask(sock_probe *s, int count, struct sockaddr **out) override
Definition: gcs_xcom_networking.cc:466
void get_sockaddr_address(sock_probe *s, int count, struct sockaddr **out) override
Definition: gcs_xcom_networking.cc:461
~Gcs_sock_probe_interface_impl() override=default
int number_of_interfaces(sock_probe *s) override
Definition: gcs_xcom_networking.cc:457
Gcs_sock_probe_interface_impl & operator=(const Gcs_sock_probe_interface_impl &)=default
char * get_if_name(sock_probe *s, int count) override
Definition: gcs_xcom_networking.cc:470
Gcs_sock_probe_interface_impl(Gcs_sock_probe_interface_impl &&)=default
bool_t is_if_running(sock_probe *s, int count) override
Definition: gcs_xcom_networking.cc:478
int init_sock_probe(sock_probe *s) override
Definition: gcs_xcom_networking.cc:453
Interface to decouple XCom sock_probe implementation to allow unit testing.
Definition: gcs_xcom_networking.h:42
Gcs_sock_probe_interface(Gcs_sock_probe_interface &)=default
virtual int init_sock_probe(sock_probe *s)=0
virtual char * get_if_name(sock_probe *s, int count)=0
Gcs_sock_probe_interface & operator=(const Gcs_sock_probe_interface &)=default
Gcs_sock_probe_interface & operator=(Gcs_sock_probe_interface &&)=default
virtual int number_of_interfaces(sock_probe *s)=0
virtual void close_sock_probe(sock_probe *s)=0
Gcs_sock_probe_interface(Gcs_sock_probe_interface &&)=default
virtual ~Gcs_sock_probe_interface()=default
virtual void get_sockaddr_address(sock_probe *s, int count, struct sockaddr **out)=0
virtual bool_t is_if_running(sock_probe *s, int count)=0
Gcs_sock_probe_interface()=default
virtual void get_sockaddr_netmask(sock_probe *s, int count, struct sockaddr **out)=0
bool resolve_ip_addr_from_hostname(std::string name, std::vector< std::string > &ip)
This function translates hostname to all possible IP addresses.
Definition: gcs_xcom_networking.cc:258
bool get_local_addresses(Gcs_sock_probe_interface &sock_probe, std::map< std::string, int > &out, bool filter_out_inactive=false)
This function gets all network addresses on this host and their subnet masks as a string.
Definition: gcs_xcom_networking.cc:93
bool get_local_private_addresses(std::map< std::string, int > &out, bool filter_out_inactive=false)
This function gets all private network addresses and their subnet masks as a string.
Definition: gcs_xcom_networking.cc:209
bool get_address_for_allowlist(std::string addr, std::string mask, std::pair< std::vector< unsigned char >, std::vector< unsigned char > > &out_pair)
Converts an address in string format (X.X.X.X/XX) into network octet format.
Definition: gcs_xcom_networking.cc:708
bool resolve_all_ip_addr_from_hostname(std::string name, std::vector< std::pair< sa_family_t, std::string > > &ips)
This function translates hostnames to all possible IP addresses.
Definition: gcs_xcom_networking.cc:300
static mi_bit_type mask[]
Definition: mi_packrec.cc:141
static int count
Definition: myisam_ftdump.cc:45
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2876
struct sockaddr sockaddr
Definition: sock_probe_win32.h:63
case opt name
Definition: sslopt-case.h:29
Definition: gcs_xcom_networking.h:208
bool operator()(const Gcs_ip_allowlist_entry *lhs, const Gcs_ip_allowlist_entry *rhs) const
Definition: gcs_xcom_networking.h:209
Definition: site_struct.h:43
Definition: sock_probe_ix.h:54
__u_short u_short
Definition: types.h:72
int bool_t
Definition: types.h:35