Skip to content

Commit ea395db

Browse files
committed
Merge branch 'refs/heads/wl13497-devapi-multihost'
# Conflicts: # common/settings.h # include/mysqlx/common_constants.h # xapi/tests/xapi-t.cc
2 parents e35adc4 + a2a216c commit ea395db

File tree

22 files changed

+1080
-151
lines changed

22 files changed

+1080
-151
lines changed

cdk/core/session.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ using std::unique_ptr;
4040
/*
4141
A class that creates a session from given data source.
4242
43-
Instances of this calss are callable objects which can be used as visitors
43+
Instances of this class are callable objects which can be used as visitors
4444
for ds::Multi_source implementing in this case the failover logic.
4545
*/
46+
4647
struct Session_builder
4748
{
4849
using TLS = cdk::connection::TLS;
@@ -398,7 +399,7 @@ Session::Session(ds::Multi_source &ds)
398399
throw_error(
399400
1 == sb.m_attempts ?
400401
"Could not connect to the given data source" :
401-
"Could not connect ot any of the given data sources"
402+
"Could not connect to any of the given data sources"
402403
);
403404
}
404405

cdk/foundation/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ target_link_libraries(cdk_foundation
9393
PRIVATE SSL::ssl
9494
)
9595

96+
IF(WIN32)
97+
target_link_libraries(cdk_foundation PRIVATE Dnsapi)
98+
ELSE()
99+
target_link_libraries(cdk_foundation PRIVATE resolv)
100+
ENDIF()
96101

97102
IF(WIN32)
98103
target_link_libraries(cdk_foundation PRIVATE ws2_32)

cdk/foundation/connection_tcpip.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <mysql/cdk/foundation/error.h>
3434

3535
#include "connection_tcpip_base.h"
36+
#include <list>
3637

3738
using namespace ::cdk::foundation;
3839

@@ -167,6 +168,28 @@ std::string get_local_hostname()
167168
}
168169

169170

171+
SRV_host::SRV_host(detail::Srv_host_detail &&detail)
172+
: prio(detail.prio)
173+
, weight(detail.weight)
174+
, port(detail.port)
175+
, name(std::move(detail.name))
176+
{}
177+
178+
179+
std::forward_list<SRV_host> srv_list(const std::string &host_name)
180+
{
181+
std::forward_list<SRV_host> list;
182+
std::forward_list<SRV_host>::const_iterator it = list.before_begin();
183+
184+
for(auto &el : detail::srv_list(host_name))
185+
{
186+
it = list.insert_after(it, std::move(el));
187+
}
188+
189+
return list;
190+
}
191+
192+
170193
TCPIP::TCPIP(const std::string& host,
171194
unsigned short port,
172195
const Options& opts)

cdk/foundation/socket_detail.cc

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,20 @@ PUSH_SYS_WARNINGS_CDK
4040
#include <sstream>
4141
#include <mutex>
4242
#include <thread>
43+
#include <forward_list>
44+
#include <map>
45+
#include <functional>
46+
#include <iostream>
4347

4448
#ifndef _WIN32
4549
#include <arpa/inet.h>
4650
#include <signal.h>
4751
#include <sys/un.h>
4852
#include <poll.h>
53+
#include <resolv.h>
54+
#include <arpa/nameser.h>
55+
#else
56+
#include <windns.h>
4957
#endif
5058
POP_SYS_WARNINGS_CDK
5159

@@ -1045,5 +1053,96 @@ std::string get_local_hostname()
10451053
return buf;
10461054
}
10471055

1056+
#ifdef _WIN32
1057+
std::forward_list<Srv_host_detail> srv_list(const std::string &hostname)
1058+
{
1059+
DNS_STATUS status; //Return value of DnsQuery_A() function.
1060+
PDNS_RECORD pDnsRecord =nullptr; //Pointer to DNS_RECORD structure.
1061+
1062+
using Srv_list = std::forward_list<Srv_host_detail>;
1063+
Srv_list srv;
1064+
Srv_list::const_iterator srv_it = srv.before_begin();
1065+
1066+
status = DnsQuery(hostname.c_str(), DNS_TYPE_SRV, DNS_QUERY_STANDARD, nullptr, &pDnsRecord, nullptr);
1067+
if (!status)
1068+
{
1069+
PDNS_RECORD pRecord = pDnsRecord;
1070+
while (pRecord)
1071+
{
1072+
if (pRecord->wType == DNS_TYPE_SRV)
1073+
{
1074+
srv_it = srv.emplace_after(srv_it,
1075+
Srv_host_detail
1076+
{
1077+
pRecord->Data.Srv.wPriority,
1078+
pRecord->Data.Srv.wWeight,
1079+
pRecord->Data.Srv.wPort,
1080+
pRecord->Data.Srv.pNameTarget
1081+
}
1082+
);
1083+
}
1084+
pRecord = pRecord->pNext;
1085+
}
1086+
1087+
DnsRecordListFree(pDnsRecord, DnsFreeRecordListDeep);
1088+
}
1089+
return srv;
1090+
}
1091+
#else
1092+
1093+
std::forward_list<Srv_host_detail> srv_list(const std::string &hostname)
1094+
{
1095+
struct __res_state state {};
1096+
res_ninit(&state);
1097+
1098+
using Srv_list = std::forward_list<Srv_host_detail>;
1099+
Srv_list srv;
1100+
Srv_list::const_iterator srv_it = srv.before_begin();
1101+
1102+
unsigned char query_buffer[NS_PACKETSZ];
1103+
1104+
1105+
//let get
1106+
int res = res_nsearch(&state, hostname.c_str(), ns_c_in, ns_t_srv, query_buffer, sizeof (query_buffer) );
1107+
1108+
if (res >= 0)
1109+
{
1110+
ns_msg msg;
1111+
char name_buffer[NS_MAXDNAME];
1112+
Srv_host_detail host_data;
1113+
ns_initparse(query_buffer, res, &msg);
1114+
1115+
1116+
auto process = [&msg, &name_buffer, &host_data, &srv, &srv_it](const ns_rr &rr) -> void
1117+
{
1118+
const unsigned char* srv_data = ns_rr_rdata(rr);
1119+
1120+
//Each NS_GET16 call moves srv_data to next value
1121+
NS_GET16(host_data.prio, srv_data);
1122+
NS_GET16(host_data.weight, srv_data);
1123+
NS_GET16(host_data.port, srv_data);
1124+
1125+
dn_expand(ns_msg_base(msg), ns_msg_end(msg),
1126+
srv_data, name_buffer, sizeof(name_buffer));
1127+
1128+
host_data.name = name_buffer;
1129+
1130+
srv_it = srv.emplace_after(
1131+
srv_it,
1132+
std::move(host_data));
1133+
};
1134+
1135+
for(int x= 0; x < ns_msg_count(msg, ns_s_an); x++)
1136+
{
1137+
ns_rr rr;
1138+
ns_parserr(&msg, ns_s_an, x, &rr);
1139+
process(rr);
1140+
}
1141+
}
1142+
res_nclose(&state);
1143+
1144+
return srv;
1145+
}
1146+
#endif
10481147

10491148
}}}} // cdk::foundation::connection::detail

cdk/foundation/socket_detail.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#ifndef CDK_FOUNDATION_SOCKET_DETAIL_H
3232
#define CDK_FOUNDATION_SOCKET_DETAIL_H
3333
#include <mysql/cdk/foundation/types.h>
34+
#include <forward_list>
35+
#include <string>
3436

3537
PUSH_SYS_WARNINGS_CDK
3638

@@ -447,6 +449,19 @@ size_t send_some(Socket socket, const byte *buffer, size_t buffer_size, bool wai
447449
*/
448450
std::string get_local_hostname();
449451

452+
/*
453+
Retrieve host SRV record (target:port) list for specified service and protocol
454+
*/
455+
struct Srv_host_detail
456+
{
457+
uint16_t prio;
458+
uint16_t weight;
459+
uint16_t port;
460+
string name;
461+
};
462+
463+
std::forward_list<Srv_host_detail> srv_list(const std::string &host_name);
464+
450465
}}}} // cdk::foundation::connection::detail
451466

452467

0 commit comments

Comments
 (0)