Skip to content

Commit dc4fde6

Browse files
committed
Bug#99511: UNABLE TO CONNECT USING UNIX SOCKETS
1 parent ef85f51 commit dc4fde6

File tree

6 files changed

+295
-187
lines changed

6 files changed

+295
-187
lines changed

driver/mysql_connection.cpp

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,8 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
576576
intern->is_valid = true;
577577

578578
MySQL_Uri uri;
579+
MySQL_Uri::Host_data host;
580+
579581

580582
sql::SQLString userName;
581583
sql::SQLString password;
@@ -654,7 +656,10 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
654656
throw sql::InvalidArgumentException("Wrong type passed for port expected int");
655657
}
656658
if (p_i) {
657-
uri.setPort(static_cast<unsigned int>(*p_i));
659+
for(auto &h : uri)
660+
{
661+
h.setPort(*p_i);
662+
}
658663
} else {
659664
throw sql::InvalidArgumentException("No long long value passed for port");
660665
}
@@ -665,7 +670,8 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
665670
throw sql::InvalidArgumentException("Wrong type passed for socket expected sql::SQLString");
666671
}
667672
if (p_s) {
668-
uri.setSocket(*p_s);
673+
host.setSocket(*p_s);
674+
uri.setHost(host);
669675
} else {
670676
throw sql::InvalidArgumentException("No string value passed for socket");
671677
}
@@ -676,7 +682,8 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
676682
throw sql::InvalidArgumentException("Wrong type passed for pipe expected sql::SQLString");
677683
}
678684
if (p_s) {
679-
uri.setPipe(*p_s);
685+
host.setPipe(*p_s);
686+
uri.setHost(host);
680687
} else {
681688
throw sql::InvalidArgumentException("No string value passed for pipe");
682689
}
@@ -868,7 +875,8 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
868875
defaultCharset = *p_s;
869876
} else if (!it->first.compare(OPT_NAMED_PIPE)) {
870877
/* Not sure it is really needed */
871-
uri.setProtocol(NativeAPI::PROTOCOL_PIPE);
878+
host.setProtocol(NativeAPI::PROTOCOL_PIPE);
879+
uri.setHost(host);
872880
} else if (!it->first.compare(OPT_CAN_HANDLE_EXPIRED_PASSWORDS)) {
873881
try {
874882
p_b = (it->second).get<bool>();
@@ -987,18 +995,21 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
987995

988996
#undef PROCESS_CONNSTR_OPTION
989997

990-
// Throwing in case of wrong protocol
998+
for(auto h : uri)
999+
{
1000+
1001+
// Throwing in case of wrong protocol
9911002
#ifdef _WIN32
992-
if (uri.Protocol() == NativeAPI::PROTOCOL_SOCKET) {
993-
throw sql::InvalidArgumentException("Invalid for this platform protocol requested(MYSQL_PROTOCOL_SOCKET)");
994-
}
1003+
if (h.Protocol() == NativeAPI::PROTOCOL_SOCKET) {
1004+
throw sql::InvalidArgumentException("Invalid for this platform protocol requested(MYSQL_PROTOCOL_SOCKET)");
1005+
}
9951006
#else
996-
if (uri.Protocol() == NativeAPI::PROTOCOL_PIPE) {
997-
throw sql::InvalidArgumentException("Invalid for this platform protocol requested(MYSQL_PROTOCOL_PIPE)");
998-
}
1007+
if (h.Protocol() == NativeAPI::PROTOCOL_PIPE) {
1008+
throw sql::InvalidArgumentException("Invalid for this platform protocol requested(MYSQL_PROTOCOL_PIPE)");
1009+
}
9991010
#endif
10001011

1001-
1012+
}
10021013

10031014
#if MYCPPCONN_STATIC_MYSQL_VERSION_ID < 80000
10041015
try {
@@ -1100,34 +1111,49 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
11001111

11011112
if(opt_dns_srv)
11021113
{
1103-
if(uri.Protocol() != NativeAPI::PROTOCOL_TCP)
1114+
if(uri.size() > 1)
1115+
{
1116+
throw sql::InvalidArgumentException("Using more than one host with DNS SRV lookup is not allowed.");
1117+
}
1118+
1119+
if(uri.size() ==0)
1120+
{
1121+
throw sql::InvalidArgumentException("No hostname specified for DNS SRV lookup.");
1122+
}
1123+
1124+
host = *uri.begin();
1125+
1126+
if(host.Protocol() != NativeAPI::PROTOCOL_TCP)
11041127
{
11051128
throw sql::InvalidArgumentException("Using Unix domain sockets with DNS SRV lookup is not allowed.");
11061129
}
11071130

1108-
if(uri.hasPort())
1131+
if(host.hasPort())
11091132
{
11101133
throw sql::InvalidArgumentException("Specifying a port number with DNS SRV lookup is not allowed.");
11111134
}
11121135

1113-
host_list = srv_list(uri.Host(),total_weight);
1136+
host_list = srv_list(host.Host(),total_weight);
11141137

11151138
if(host_list.empty())
11161139
{
11171140
std::stringstream err;
1118-
err << "Unable to locate any hosts for " << uri.Host();
1141+
err << "Unable to locate any hosts for " << host.Host();
11191142
throw sql::InvalidArgumentException(err.str());
11201143
}
11211144

1122-
1123-
11241145
}
11251146
else
11261147
{
11271148
for(auto host : uri)
11281149
{
11291150
host_list.insert(std::make_pair(Prio({0, 0}), host));
11301151
}
1152+
if(host_list.empty())
1153+
{
1154+
//Adding default host
1155+
host_list.insert(std::make_pair(Prio({0, 0}), Host_data()));
1156+
}
11311157
}
11321158

11331159
//Connect loop
@@ -1142,7 +1168,6 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
11421168
std::vector<uint16_t> weights;
11431169

11441170

1145-
11461171
for(auto it = same_range.first; it != same_range.second; ++it)
11471172
{
11481173
//if weight is not used, we should put all weights = 1 so that
@@ -1167,14 +1192,14 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
11671192
std::advance(weight_el, pos);
11681193
}
11691194

1170-
proxy->use_protocol(uri.Protocol());
1195+
proxy->use_protocol((*el)->Protocol());
11711196

11721197
try {
1173-
connect((*el)->name, userName,
1198+
connect((*el)->Host(), userName,
11741199
password,
11751200
uri.Schema() /* schema */,
1176-
(*el)->port,
1177-
uri.SocketOrPipe());
1201+
(*el)->Port(),
1202+
(*el)->SocketOrPipe());
11781203
connected = true;
11791204
break;
11801205
}
@@ -1213,12 +1238,21 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
12131238
{
12141239
std::stringstream err;
12151240
if(opt_dns_srv)
1216-
err << "Unable to connect to any of the hosts of " << uri.Host() << " SRV";
1241+
err << "Unable to connect to any of the hosts of " << host.Host() << " SRV";
12171242
else if (uri.size() >1) {
12181243
err << "Unable to connect to any of the hosts";
12191244
}
12201245
else {
1221-
err << "Unable to connect to " << uri.Host() << ":" << uri.Port();
1246+
switch(host.Protocol())
1247+
{
1248+
case NativeAPI::PROTOCOL_SOCKET:
1249+
case NativeAPI::PROTOCOL_PIPE:
1250+
err << "Unable to connect to " << host.SocketOrPipe() ;
1251+
break;
1252+
default:
1253+
err << "Unable to connect to " << host.Host() << ":" << host.Port();
1254+
break;
1255+
}
12221256
}
12231257
proxy.reset();
12241258
throw sql::InvalidArgumentException(err.str());

0 commit comments

Comments
 (0)