diff --git a/include/proxysql_utils.h b/include/proxysql_utils.h index 05e271c55..df2a467be 100644 --- a/include/proxysql_utils.h +++ b/include/proxysql_utils.h @@ -332,4 +332,14 @@ static inline void set_thread_name(const char(&name)[LEN], const bool en = true) #endif } +/** + * @brief Gets the client address stored in 'client_addr' member as + * an string if available. If member 'client_addr' is NULL, returns an + * empty string. + * + * @return Either an string holding the string representation of internal + * member 'client_addr', or empty string if this member is NULL. + */ +std::string get_client_addr(struct sockaddr* client_addr); + #endif diff --git a/lib/MySQL_Session.cpp b/lib/MySQL_Session.cpp index e64513056..a19040d92 100644 --- a/lib/MySQL_Session.cpp +++ b/lib/MySQL_Session.cpp @@ -7784,15 +7784,24 @@ void MySQL_Session::RequestEnd(MySQL_Data_Stream *myds,const unsigned int myerrn if (client_myds->unexp_com_pings) { client_myds->setDSS_STATE_QUERY_SENT_NET(); - while (client_myds->unexp_com_pings) { - proxy_warning("Sending OK packet for unexpected COM_PING packet\n"); + if (client_myds->unexp_com_pings) { + const string cli_addr { get_client_addr(this->client_myds->client_addr) }; - client_myds->pkt_sid += 1; - uint16_t st = NumActiveTransactions() ? SERVER_STATUS_IN_TRANS : 0; - if (autocommit) { st |= SERVER_STATUS_AUTOCOMMIT; } + while (client_myds->unexp_com_pings) { + proxy_warning( + "Sending OK packet for unexpected COM_PING packet client_addr=\"%s\"\n", + cli_addr.c_str() + ); + + client_myds->pkt_sid += 1; + uint16_t st = NumActiveTransactions() ? SERVER_STATUS_IN_TRANS : 0; + if (autocommit) { st |= SERVER_STATUS_AUTOCOMMIT; } - client_myds->myprot.generate_pkt_OK(true, NULL, NULL, client_myds->pkt_sid, 0, 0, st, 0, NULL); - client_myds->unexp_com_pings--; + client_myds->myprot.generate_pkt_OK( + true, NULL, NULL, client_myds->pkt_sid, 0, 0, st, 0, NULL + ); + client_myds->unexp_com_pings--; + } } client_myds->DSS = STATE_SLEEP; diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 5ca0dfd42..f8191ba9e 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -7,6 +7,7 @@ using json = nlohmann::json; #include #include +#include "proxysql_utils.h" #include "MySQL_HostGroups_Manager.h" #include "prometheus_helpers.h" #define MYSQL_THREAD_IMPLEMENTATION @@ -2584,43 +2585,6 @@ void MySQL_Threads_Handler::stop_listeners() { free_tokenizer( &tok ); } -/** - * @brief Gets the client address stored in 'client_addr' member as - * an string if available. If member 'client_addr' is NULL, returns an - * empty string. - * - * @return Either an string holding the string representation of internal - * member 'client_addr', or empty string if this member is NULL. - */ -static std::string get_client_addr(struct sockaddr* client_addr) { - char buf[INET6_ADDRSTRLEN]; - std::string str_client_addr {}; - - if (client_addr == NULL) { - return str_client_addr; - } - - switch (client_addr->sa_family) { - case AF_INET: { - struct sockaddr_in *ipv4 = (struct sockaddr_in *)client_addr; - inet_ntop(client_addr->sa_family, &ipv4->sin_addr, buf, INET_ADDRSTRLEN); - str_client_addr = std::string { buf }; - break; - } - case AF_INET6: { - struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)client_addr; - inet_ntop(client_addr->sa_family, &ipv6->sin6_addr, buf, INET6_ADDRSTRLEN); - str_client_addr = std::string { buf }; - break; - } - default: - str_client_addr = std::string { "localhost" }; - break; - } - - return str_client_addr; -} - MySQL_Client_Host_Cache_Entry MySQL_Threads_Handler::find_client_host_cache(struct sockaddr* client_sockaddr) { MySQL_Client_Host_Cache_Entry entry { 0, 0 }; // Client_sockaddr **shouldn't** ever by 'NULL', no matter the diff --git a/lib/PgSQL_Thread.cpp b/lib/PgSQL_Thread.cpp index 402c09204..d8f8cdfbf 100644 --- a/lib/PgSQL_Thread.cpp +++ b/lib/PgSQL_Thread.cpp @@ -7,6 +7,7 @@ using json = nlohmann::json; #include #include +#include "proxysql_utils.h" #include "PgSQL_HostGroups_Manager.h" #include "prometheus_helpers.h" #define PGSQL_THREAD_IMPLEMENTATION @@ -2354,43 +2355,6 @@ void PgSQL_Threads_Handler::stop_listeners() { free_tokenizer(&tok); } -/** - * @brief Gets the client address stored in 'client_addr' member as - * an string if available. If member 'client_addr' is NULL, returns an - * empty string. - * - * @return Either an string holding the string representation of internal - * member 'client_addr', or empty string if this member is NULL. - */ -static std::string get_client_addr(struct sockaddr* client_addr) { - char buf[INET6_ADDRSTRLEN]; - std::string str_client_addr{}; - - if (client_addr == NULL) { - return str_client_addr; - } - - switch (client_addr->sa_family) { - case AF_INET: { - struct sockaddr_in* ipv4 = (struct sockaddr_in*)client_addr; - inet_ntop(client_addr->sa_family, &ipv4->sin_addr, buf, INET_ADDRSTRLEN); - str_client_addr = std::string{ buf }; - break; - } - case AF_INET6: { - struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)client_addr; - inet_ntop(client_addr->sa_family, &ipv6->sin6_addr, buf, INET6_ADDRSTRLEN); - str_client_addr = std::string{ buf }; - break; - } - default: - str_client_addr = std::string{ "localhost" }; - break; - } - - return str_client_addr; -} - PgSQL_Client_Host_Cache_Entry PgSQL_Threads_Handler::find_client_host_cache(struct sockaddr* client_sockaddr) { PgSQL_Client_Host_Cache_Entry entry{ 0, 0 }; // Client_sockaddr **shouldn't** ever by 'NULL', no matter the diff --git a/lib/mysql_data_stream.cpp b/lib/mysql_data_stream.cpp index bb28d9194..ea3df59e7 100644 --- a/lib/mysql_data_stream.cpp +++ b/lib/mysql_data_stream.cpp @@ -490,7 +490,11 @@ void MySQL_Data_Stream::check_data_flow() { const uint8_t c = *(static_cast(PSarrayIN->pdata[0].ptr) + sizeof(mysql_hdr)); if (c == _MYSQL_COM_PING && this->sess->status != WAITING_CLIENT_DATA) { - proxy_warning("Handling unexpected COM_PING packet\n"); + const string cli_addr { get_client_addr(this->client_addr) }; + proxy_warning( + "Handling unexpected COM_PING packet client_addr=\"%s\" sess_status=%d myds_status=%d\n", + cli_addr.c_str(), this->sess->status, this->DSS + ); // Queue the COM_PING for later handling at MySQL_Session level this->unexp_com_pings += 1; diff --git a/lib/proxysql_utils.cpp b/lib/proxysql_utils.cpp index 7920c24ef..062998414 100644 --- a/lib/proxysql_utils.cpp +++ b/lib/proxysql_utils.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -550,3 +551,40 @@ const nlohmann::json* get_nested_elem(const nlohmann::json& j, const vectorsa_family) { + case AF_INET: { + struct sockaddr_in *ipv4 = (struct sockaddr_in *)client_addr; + inet_ntop(client_addr->sa_family, &ipv4->sin_addr, buf, INET_ADDRSTRLEN); + str_client_addr = std::string { buf }; + break; + } + case AF_INET6: { + struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)client_addr; + inet_ntop(client_addr->sa_family, &ipv6->sin6_addr, buf, INET6_ADDRSTRLEN); + str_client_addr = std::string { buf }; + break; + } + default: + str_client_addr = std::string { "localhost" }; + break; + } + + return str_client_addr; +}