MySQL  8.0.22
Source Code Documentation
hostname_cache.h
Go to the documentation of this file.
1 /* Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef HOSTNAME_CACHE_INCLUDED
24 #define HOSTNAME_CACHE_INCLUDED
25 
26 #include "my_config.h"
27 
28 #include <sys/types.h>
29 #include <list>
30 #include <memory>
31 
32 #include "my_hostname.h"
33 #include "my_inttypes.h"
34 #include "mysql_com.h"
35 
36 #ifdef HAVE_NETINET_IN_H
37 #include <netinet/in.h>
38 #endif
39 
40 struct Host_errors {
41  public:
42  Host_errors();
43  ~Host_errors();
44 
45  void reset();
46  void aggregate(const Host_errors *errors);
47 
48  /** Number of connect errors. */
49  ulong m_connect;
50 
51  /** Number of host blocked errors. */
53  /** Number of transient errors from getnameinfo(). */
55  /** Number of permanent errors from getnameinfo(). */
57  /** Number of errors from is_hostname_valid(). */
58  ulong m_format;
59  /** Number of transient errors from getaddrinfo(). */
61  /** Number of permanent errors from getaddrinfo(). */
63  /** Number of errors from Forward-Confirmed reverse DNS checks. */
64  ulong m_FCrDNS;
65  /** Number of errors from host grants. */
66  ulong m_host_acl;
67  /** Number of errors from missing auth plugin. */
69  /** Number of errors from auth plugin. */
71  /** Number of errors from authentication plugins. */
72  ulong m_handshake;
73  /** Number of errors from proxy user. */
74  ulong m_proxy_user;
75  /** Number of errors from proxy user acl. */
77  /** Number of errors from authentication. */
79  /** Number of errors from ssl. */
80  ulong m_ssl;
81  /** Number of errors from max user connection. */
83  /** Number of errors from max user connection per hour. */
85  /** Number of errors from the default database. */
87  /** Number of errors from init_connect. */
89  /** Number of errors from the server itself. */
90  ulong m_local;
91 
92  bool has_error() const {
93  return (
94  (m_host_blocked != 0) || (m_nameinfo_transient != 0) ||
95  (m_nameinfo_permanent != 0) || (m_format != 0) ||
96  (m_addrinfo_transient != 0) || (m_addrinfo_permanent != 0) ||
97  (m_FCrDNS != 0) || (m_host_acl != 0) || (m_no_auth_plugin != 0) ||
98  (m_auth_plugin != 0) || (m_handshake != 0) || (m_proxy_user != 0) ||
99  (m_proxy_user_acl != 0) || (m_authentication != 0) || (m_ssl != 0) ||
100  (m_max_user_connection != 0) || (m_max_user_connection_per_hour != 0) ||
101  (m_default_database != 0) || (m_init_connect != 0) || (m_local != 0));
102  }
103 
105  /* Current (historical) behavior: */
106  m_connect = m_handshake;
107  }
108 
109  void clear_connect_errors() { m_connect = 0; }
110 };
111 
112 /** Size of IP address string in the hash cache. */
113 #define HOST_ENTRY_KEY_SIZE INET6_ADDRSTRLEN
114 
115 /**
116  An entry in the hostname hash table cache.
117 
118  Host name cache does two things:
119  - caches host names to save DNS look ups;
120  - counts errors from IP.
121 
122  Host name can be empty (that means DNS look up failed),
123  but errors still are counted.
124 */
125 class Host_entry {
126  public:
127  /**
128  Client IP address. This is the key used with the hash table.
129 
130  The client IP address is always expressed in IPv6, even when the
131  network IPv6 stack is not present.
132 
133  This IP address is never used to connect to a socket.
134  */
135  char ip_key[HOST_ENTRY_KEY_SIZE];
136 
137  /**
138  One of the host names for the IP address. May be a zero length string.
139  */
140  char m_hostname[HOSTNAME_LENGTH + 1];
141  /** Length in bytes of @c m_hostname. */
143  /** The hostname is validated and used for authorization. */
149  /** Error statistics. */
151 
153  if (m_first_error_seen == 0) m_first_error_seen = now;
154  m_last_error_seen = now;
155  }
156 };
157 
158 #define RC_OK 0
159 #define RC_BLOCKED_HOST 1
160 #define RC_LONG_HOSTNAME 2
161 int ip_to_hostname(struct sockaddr_storage *ip_storage, const char *ip_string,
162  char **hostname, uint *connect_errors);
163 
164 void inc_host_errors(const char *ip_string, Host_errors *errors);
165 void reset_host_connect_errors(const char *ip_string);
166 bool hostname_cache_init(uint size);
167 void hostname_cache_free();
168 void hostname_cache_refresh(void);
170 void hostname_cache_resize(uint size);
171 void hostname_cache_lock();
172 void hostname_cache_unlock();
173 std::list<std::unique_ptr<Host_entry>>::iterator hostname_cache_begin();
174 std::list<std::unique_ptr<Host_entry>>::iterator hostname_cache_end();
175 
176 #endif /* HOSTNAME_CACHE_INCLUDED */
ulong m_local
Number of errors from the server itself.
Definition: hostname_cache.h:90
ulong m_max_user_connection
Number of errors from max user connection.
Definition: hostname_cache.h:82
void hostname_cache_unlock()
Definition: hostname_cache.cc:223
unsigned long long int ulonglong
Definition: my_inttypes.h:55
ulong m_nameinfo_permanent
Number of permanent errors from getnameinfo().
Definition: hostname_cache.h:56
ulonglong m_first_error_seen
Definition: hostname_cache.h:147
uint hostname_cache_size()
Definition: hostname_cache.cc:178
Host_errors m_errors
Error statistics.
Definition: hostname_cache.h:150
Some integer typedefs for easier portability.
void aggregate(const Host_errors *errors)
Definition: hostname_cache.cc:139
ulong m_addrinfo_transient
Number of transient errors from getaddrinfo().
Definition: hostname_cache.h:60
void hostname_cache_free()
Definition: hostname_cache.cc:206
bool hostname_cache_init(uint size)
Definition: hostname_cache.cc:192
void clear_connect_errors()
Definition: hostname_cache.h:109
ulong m_proxy_user
Number of errors from proxy user.
Definition: hostname_cache.h:74
ulong m_host_acl
Number of errors from host grants.
Definition: hostname_cache.h:66
void hostname_cache_refresh(void)
Definition: hostname_cache.cc:170
Common definition used by mysys, performance schema and server & client.
ulong m_FCrDNS
Number of errors from Forward-Confirmed reverse DNS checks.
Definition: hostname_cache.h:64
bool has_error() const
Definition: hostname_cache.h:92
Common definition between mysql server & client.
ulong m_ssl
Number of errors from ssl.
Definition: hostname_cache.h:80
ulong m_format
Number of errors from is_hostname_valid().
Definition: hostname_cache.h:58
void hostname_cache_resize(uint size)
Definition: hostname_cache.cc:183
void sum_connect_errors()
Definition: hostname_cache.h:104
ulonglong m_last_seen
Definition: hostname_cache.h:146
ulong m_init_connect
Number of errors from init_connect.
Definition: hostname_cache.h:88
An entry in the hostname hash table cache.
Definition: hostname_cache.h:125
ulong m_max_user_connection_per_hour
Number of errors from max user connection per hour.
Definition: hostname_cache.h:84
unsigned int uint
Definition: uca-dump.cc:29
ulong m_default_database
Number of errors from the default database.
Definition: hostname_cache.h:86
static constexpr int HOSTNAME_LENGTH
Definition: my_hostname.h:42
void reset_host_connect_errors(const char *ip_string)
Definition: hostname_cache.cc:368
void reset()
Definition: hostname_cache.cc:115
ulong m_nameinfo_transient
Number of transient errors from getnameinfo().
Definition: hostname_cache.h:54
ulong m_host_blocked
Number of host blocked errors.
Definition: hostname_cache.h:52
bool m_host_validated
The hostname is validated and used for authorization.
Definition: hostname_cache.h:144
ulonglong m_first_seen
Definition: hostname_cache.h:145
ulong m_authentication
Number of errors from authentication.
Definition: hostname_cache.h:78
void set_error_timestamps(ulonglong now)
Definition: hostname_cache.h:152
Host_errors()
Definition: hostname_cache.cc:90
std::list< std::unique_ptr< Host_entry > >::iterator hostname_cache_begin()
Definition: hostname_cache.cc:228
void inc_host_errors(const char *ip_string, Host_errors *errors)
Definition: hostname_cache.cc:346
ulong m_connect
Number of connect errors.
Definition: hostname_cache.h:49
~Host_errors()
Definition: hostname_cache.cc:113
ulong m_handshake
Number of errors from authentication plugins.
Definition: hostname_cache.h:72
ulonglong m_last_error_seen
Definition: hostname_cache.h:148
void hostname_cache_lock()
Definition: hostname_cache.cc:218
ulong m_addrinfo_permanent
Number of permanent errors from getaddrinfo().
Definition: hostname_cache.h:62
ulong m_no_auth_plugin
Number of errors from missing auth plugin.
Definition: hostname_cache.h:68
#define HOST_ENTRY_KEY_SIZE
Size of IP address string in the hash cache.
Definition: hostname_cache.h:113
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:445
ulong m_proxy_user_acl
Number of errors from proxy user acl.
Definition: hostname_cache.h:76
Definition: hostname_cache.h:40
ulong m_auth_plugin
Number of errors from auth plugin.
Definition: hostname_cache.h:70
uint m_hostname_length
Length in bytes of m_hostname.
Definition: hostname_cache.h:142
std::list< std::unique_ptr< Host_entry > >::iterator hostname_cache_end()
Definition: hostname_cache.cc:233