MySQL 9.0.0
Source Code Documentation
hostname_cache.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 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 HOSTNAME_CACHE_INCLUDED
25#define HOSTNAME_CACHE_INCLUDED
26
27#include "my_config.h"
28
29#include <sys/types.h>
30#include <list>
31#include <memory>
32
33#include "my_hostname.h"
34#include "my_inttypes.h"
35#include "mysql_com.h"
36
37#ifdef HAVE_NETINET_IN_H
38#include <netinet/in.h>
39#endif
40
42 public:
45
46 void reset();
47 void aggregate(const Host_errors *errors);
48
49 /** Number of connect errors. */
50 ulong m_connect;
51
52 /** Number of host blocked errors. */
54 /** Number of transient errors from getnameinfo(). */
56 /** Number of permanent errors from getnameinfo(). */
58 /** Number of errors from is_hostname_valid(). */
59 ulong m_format;
60 /** Number of transient errors from getaddrinfo(). */
62 /** Number of permanent errors from getaddrinfo(). */
64 /** Number of errors from Forward-Confirmed reverse DNS checks. */
65 ulong m_FCrDNS;
66 /** Number of errors from host grants. */
68 /** Number of errors from missing auth plugin. */
70 /** Number of errors from auth plugin. */
72 /** Number of errors from authentication plugins. */
74 /** Number of errors from proxy user. */
76 /** Number of errors from proxy user acl. */
78 /** Number of errors from authentication. */
80 /** Number of errors from ssl. */
81 ulong m_ssl;
82 /** Number of errors from max user connection. */
84 /** Number of errors from max user connection per hour. */
86 /** Number of errors from the default database. */
88 /** Number of errors from init_connect. */
90 /** Number of errors from the server itself. */
91 ulong m_local;
92
93 bool has_error() const {
94 return (
95 (m_host_blocked != 0) || (m_nameinfo_transient != 0) ||
96 (m_nameinfo_permanent != 0) || (m_format != 0) ||
98 (m_FCrDNS != 0) || (m_host_acl != 0) || (m_no_auth_plugin != 0) ||
99 (m_auth_plugin != 0) || (m_handshake != 0) || (m_proxy_user != 0) ||
100 (m_proxy_user_acl != 0) || (m_authentication != 0) || (m_ssl != 0) ||
102 (m_default_database != 0) || (m_init_connect != 0) || (m_local != 0));
103 }
104
106 /* Current (historical) behavior: */
108 }
109
111};
112
113/** Size of IP address string in the hash cache. */
114#define HOST_ENTRY_KEY_SIZE INET6_ADDRSTRLEN
115
116/**
117 An entry in the hostname hash table cache.
118
119 Host name cache does two things:
120 - caches host names to save DNS look ups;
121 - counts errors from IP.
122
123 Host name can be empty (that means DNS look up failed),
124 but errors still are counted.
125*/
127 public:
128 /**
129 Client IP address. This is the key used with the hash table.
130
131 The client IP address is always expressed in IPv6, even when the
132 network IPv6 stack is not present.
133
134 This IP address is never used to connect to a socket.
135 */
137
138 /**
139 One of the host names for the IP address. May be a zero length string.
140 */
142 /** Length in bytes of @c m_hostname. */
144 /** The hostname is validated and used for authorization. */
150 /** Error statistics. */
152
155 m_last_error_seen = now;
156 }
157};
158
159#define RC_OK 0
160#define RC_BLOCKED_HOST 1
161#define RC_LONG_HOSTNAME 2
162int ip_to_hostname(struct sockaddr_storage *ip_storage, const char *ip_string,
163 char **hostname, uint *connect_errors);
164
165void inc_host_errors(const char *ip_string, Host_errors *errors);
166void reset_host_connect_errors(const char *ip_string);
167bool hostname_cache_init(uint size);
169void hostname_cache_refresh(void);
171void hostname_cache_resize(uint size);
174std::list<std::unique_ptr<Host_entry>>::iterator hostname_cache_begin();
175std::list<std::unique_ptr<Host_entry>>::iterator hostname_cache_end();
176
177#endif /* HOSTNAME_CACHE_INCLUDED */
An entry in the hostname hash table cache.
Definition: hostname_cache.h:126
bool m_host_validated
The hostname is validated and used for authorization.
Definition: hostname_cache.h:145
uint m_hostname_length
Length in bytes of m_hostname.
Definition: hostname_cache.h:143
ulonglong m_last_error_seen
Definition: hostname_cache.h:149
Host_errors m_errors
Error statistics.
Definition: hostname_cache.h:151
ulonglong m_first_error_seen
Definition: hostname_cache.h:148
char ip_key[HOST_ENTRY_KEY_SIZE]
Client IP address.
Definition: hostname_cache.h:136
ulonglong m_first_seen
Definition: hostname_cache.h:146
char m_hostname[HOSTNAME_LENGTH+1]
One of the host names for the IP address.
Definition: hostname_cache.h:141
ulonglong m_last_seen
Definition: hostname_cache.h:147
void set_error_timestamps(ulonglong now)
Definition: hostname_cache.h:153
int ip_to_hostname(struct sockaddr_storage *ip_storage, const char *ip_string, char **hostname, uint *connect_errors)
Resolve IP-address to host name.
Definition: hostname_cache.cc:444
std::list< std::unique_ptr< Host_entry > >::iterator hostname_cache_begin()
Definition: hostname_cache.cc:229
void reset_host_connect_errors(const char *ip_string)
Definition: hostname_cache.cc:367
void inc_host_errors(const char *ip_string, Host_errors *errors)
Definition: hostname_cache.cc:345
void hostname_cache_free()
Definition: hostname_cache.cc:207
bool hostname_cache_init(uint size)
Definition: hostname_cache.cc:193
#define HOST_ENTRY_KEY_SIZE
Size of IP address string in the hash cache.
Definition: hostname_cache.h:114
std::list< std::unique_ptr< Host_entry > >::iterator hostname_cache_end()
Definition: hostname_cache.cc:234
void hostname_cache_refresh(void)
Definition: hostname_cache.cc:171
uint hostname_cache_size()
Definition: hostname_cache.cc:179
void hostname_cache_resize(uint size)
Definition: hostname_cache.cc:184
void hostname_cache_unlock()
Definition: hostname_cache.cc:224
void hostname_cache_lock()
Definition: hostname_cache.cc:219
Common definition used by mysys, performance schema and server & client.
static constexpr int HOSTNAME_LENGTH
Definition: my_hostname.h:43
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
Common definition between mysql server & client.
size_t size(const char *const c)
Definition: base64.h:46
Definition: hostname_cache.h:41
ulong m_host_blocked
Number of host blocked errors.
Definition: hostname_cache.h:53
ulong m_authentication
Number of errors from authentication.
Definition: hostname_cache.h:79
void clear_connect_errors()
Definition: hostname_cache.h:110
ulong m_handshake
Number of errors from authentication plugins.
Definition: hostname_cache.h:73
ulong m_max_user_connection_per_hour
Number of errors from max user connection per hour.
Definition: hostname_cache.h:85
bool has_error() const
Definition: hostname_cache.h:93
ulong m_default_database
Number of errors from the default database.
Definition: hostname_cache.h:87
void reset()
Definition: hostname_cache.cc:116
void sum_connect_errors()
Definition: hostname_cache.h:105
ulong m_nameinfo_transient
Number of transient errors from getnameinfo().
Definition: hostname_cache.h:55
void aggregate(const Host_errors *errors)
Definition: hostname_cache.cc:140
ulong m_init_connect
Number of errors from init_connect.
Definition: hostname_cache.h:89
ulong m_connect
Number of connect errors.
Definition: hostname_cache.h:50
ulong m_nameinfo_permanent
Number of permanent errors from getnameinfo().
Definition: hostname_cache.h:57
ulong m_FCrDNS
Number of errors from Forward-Confirmed reverse DNS checks.
Definition: hostname_cache.h:65
Host_errors()
Definition: hostname_cache.cc:91
ulong m_max_user_connection
Number of errors from max user connection.
Definition: hostname_cache.h:83
ulong m_no_auth_plugin
Number of errors from missing auth plugin.
Definition: hostname_cache.h:69
ulong m_proxy_user
Number of errors from proxy user.
Definition: hostname_cache.h:75
ulong m_proxy_user_acl
Number of errors from proxy user acl.
Definition: hostname_cache.h:77
ulong m_host_acl
Number of errors from host grants.
Definition: hostname_cache.h:67
ulong m_local
Number of errors from the server itself.
Definition: hostname_cache.h:91
ulong m_addrinfo_permanent
Number of permanent errors from getaddrinfo().
Definition: hostname_cache.h:63
ulong m_addrinfo_transient
Number of transient errors from getaddrinfo().
Definition: hostname_cache.h:61
ulong m_ssl
Number of errors from ssl.
Definition: hostname_cache.h:81
ulong m_format
Number of errors from is_hostname_valid().
Definition: hostname_cache.h:59
ulong m_auth_plugin
Number of errors from auth plugin.
Definition: hostname_cache.h:71