2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/ClientInfo.h>
|
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Core/Defines.h>
|
|
|
|
#include <Common/getFQDNOrHostName.h>
|
|
|
|
#include <Common/ClickHouseRevision.h>
|
2018-03-23 16:05:14 +00:00
|
|
|
#include <port/unistd.h>
|
2016-10-24 21:40:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-10-24 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-16 20:46:02 +00:00
|
|
|
void ClientInfo::write(WriteBuffer & out, const UInt64 server_protocol_revision) const
|
2016-10-24 21:40:39 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (server_protocol_revision < DBMS_MIN_REVISION_WITH_CLIENT_INFO)
|
|
|
|
throw Exception("Logical error: method ClientInfo::write is called for unsupported server revision", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
writeBinary(UInt8(query_kind), out);
|
|
|
|
if (empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
writeBinary(initial_user, out);
|
|
|
|
writeBinary(initial_query_id, out);
|
|
|
|
writeBinary(initial_address.toString(), out);
|
|
|
|
|
|
|
|
writeBinary(UInt8(interface), out);
|
|
|
|
|
|
|
|
if (interface == Interface::TCP)
|
|
|
|
{
|
|
|
|
writeBinary(os_user, out);
|
|
|
|
writeBinary(client_hostname, out);
|
|
|
|
writeBinary(client_name, out);
|
|
|
|
writeVarUInt(client_version_major, out);
|
|
|
|
writeVarUInt(client_version_minor, out);
|
|
|
|
writeVarUInt(client_revision, out);
|
|
|
|
}
|
|
|
|
else if (interface == Interface::HTTP)
|
|
|
|
{
|
|
|
|
writeBinary(UInt8(http_method), out);
|
|
|
|
writeBinary(http_user_agent, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server_protocol_revision >= DBMS_MIN_REVISION_WITH_QUOTA_KEY_IN_CLIENT_INFO)
|
|
|
|
writeBinary(quota_key, out);
|
2016-10-24 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-16 20:46:02 +00:00
|
|
|
void ClientInfo::read(ReadBuffer & in, const UInt64 client_protocol_revision)
|
2016-10-24 21:40:39 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (client_protocol_revision < DBMS_MIN_REVISION_WITH_CLIENT_INFO)
|
|
|
|
throw Exception("Logical error: method ClientInfo::read is called for unsupported client revision", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
UInt8 read_query_kind = 0;
|
|
|
|
readBinary(read_query_kind, in);
|
|
|
|
query_kind = QueryKind(read_query_kind);
|
|
|
|
if (empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
readBinary(initial_user, in);
|
|
|
|
readBinary(initial_query_id, in);
|
|
|
|
|
|
|
|
String initial_address_string;
|
|
|
|
readBinary(initial_address_string, in);
|
|
|
|
initial_address = Poco::Net::SocketAddress(initial_address_string);
|
|
|
|
|
|
|
|
UInt8 read_interface = 0;
|
|
|
|
readBinary(read_interface, in);
|
|
|
|
interface = Interface(read_interface);
|
|
|
|
|
|
|
|
if (interface == Interface::TCP)
|
|
|
|
{
|
|
|
|
readBinary(os_user, in);
|
|
|
|
readBinary(client_hostname, in);
|
|
|
|
readBinary(client_name, in);
|
|
|
|
readVarUInt(client_version_major, in);
|
|
|
|
readVarUInt(client_version_minor, in);
|
|
|
|
readVarUInt(client_revision, in);
|
|
|
|
}
|
|
|
|
else if (interface == Interface::HTTP)
|
|
|
|
{
|
|
|
|
UInt8 read_http_method = 0;
|
|
|
|
readBinary(read_http_method, in);
|
|
|
|
http_method = HTTPMethod(read_http_method);
|
|
|
|
|
|
|
|
readBinary(http_user_agent, in);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client_protocol_revision >= DBMS_MIN_REVISION_WITH_QUOTA_KEY_IN_CLIENT_INFO)
|
|
|
|
readBinary(quota_key, in);
|
2016-10-24 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ClientInfo::fillOSUserHostNameAndVersionInfo()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
os_user.resize(256, '\0');
|
|
|
|
if (0 == getlogin_r(&os_user[0], os_user.size() - 1))
|
|
|
|
os_user.resize(strlen(os_user.data()));
|
|
|
|
else
|
|
|
|
os_user.clear(); /// Don't mind if we cannot determine user login.
|
2016-10-24 21:40:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
client_hostname = getFQDNOrHostName();
|
2016-10-24 21:40:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
client_version_major = DBMS_VERSION_MAJOR;
|
|
|
|
client_version_minor = DBMS_VERSION_MINOR;
|
|
|
|
client_revision = ClickHouseRevision::get();
|
2016-10-24 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|