diff --git a/sql-common/client.cc b/sql-common/client.cc index 5b85ae168011f65af9f2527ecaae5c7a99c9072f..d83ce35c46dd4b9c6a2ad2198b0c4c78b51fd500 100644 --- a/sql-common/client.cc +++ b/sql-common/client.cc @@ -5724,6 +5724,41 @@ static mysql_state_machine_status authsm_finish_auth(mysql_async_auth *ctx) { return ctx->res ? STATE_MACHINE_FAILED : STATE_MACHINE_DONE; } +#ifdef HAVE_ZSQL_CONNECTION_ATTRIBUTE_ADD_IP_PORT +static bool get_org_ip_port(MYSQL *mysql, std::string *org_ip, std::string *org_port) { + + if (mysql->net.vio == nullptr) return true; + + char ip_str[INET6_ADDRSTRLEN]; + int port; + + union { + struct sockaddr_in in; + struct sockaddr_in6 in6; + } addr; + + socklen_t addrlen = sizeof(addr); + + if (getsockname(vio_fd(mysql->net.vio), (struct sockaddr*)&addr, &addrlen) != 0) return true; + struct sockaddr *saddr = (struct sockaddr *)&addr; + + if (saddr->sa_family == AF_INET) { + inet_ntop(AF_INET, &addr.in.sin_addr, ip_str, sizeof(ip_str)); + port = ntohs(addr.in.sin_port); + } else if (saddr->sa_family == AF_INET6) { + inet_ntop(AF_INET6, &addr.in6.sin6_addr, ip_str, sizeof(ip_str)); + port = ntohs(addr.in6.sin6_port); + } else { + return true; + } + + *org_ip = ip_str; + *org_port = std::to_string(port); + + return false; +} +#endif // HAVE_ZSQL_CONNECTION_ATTRIBUTE_ADD_IP_PORT + /** Start multi factor authentication */ static mysql_state_machine_status authsm_init_multi_auth( mysql_async_auth *ctx) { @@ -6467,6 +6502,9 @@ static mysql_state_machine_status csm_complete_connect( DBUG_TRACE; MYSQL *mysql = ctx->mysql; NET *net = &mysql->net; +#ifdef HAVE_ZSQL_CONNECTION_ATTRIBUTE_ADD_IP_PORT + std::string org_ip, org_port; +#endif // HAVE_ZSQL_CONNECTION_ATTRIBUTE_ADD_IP_PORT DBUG_PRINT("info", ("net->vio: %p", net->vio)); if (!net->vio) { DBUG_PRINT("error", ("Unknow protocol %d ", mysql->options.protocol)); @@ -6481,6 +6519,15 @@ static mysql_state_machine_status csm_complete_connect( return STATE_MACHINE_FAILED; } vio_keepalive(net->vio, true); +#ifdef HAVE_ZSQL_CONNECTION_ATTRIBUTE_ADD_IP_PORT + mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "_orgclient"); + if (get_org_ip_port(mysql, &org_ip, &org_port)) { + org_ip = "error"; + org_port = "error"; + } + mysql_options4(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "_orgclient", + (org_ip + ":" + org_port).c_str()); +#endif // HAVE_ZSQL_CONNECTION_ATTRIBUTE_ADD_IP_PORT /* If user set read_timeout, let it override the default */ if (mysql->options.read_timeout)