Skip to content

Commit 02b1449

Browse files
committed
WL#15130 Socket-level TLS patch mysql#9: TransporterRegistry::connect_server()
Change-Id: I1bcb50507deccf7ebd17678c333cf90b7822f9cf
1 parent 307e094 commit 02b1449

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

storage/ndb/include/transporter/TransporterRegistry.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <NdbMutex.h>
5050

5151
#include "portlib/NdbTick.h"
52+
#include "util/NdbSocket.h"
5253

5354
#ifndef _WIN32
5455
/*
@@ -243,16 +244,22 @@ class TransporterRegistry
243244
244245
@returns false on failure and true on success
245246
*/
246-
bool connect_server(ndb_socket_t sockfd,
247+
bool connect_server(NdbSocket & sockfd,
247248
BaseString& msg,
248249
bool& close_with_reset,
249250
bool& log_failure);
250251

252+
bool connect_server(ndb_socket_t sockfd, BaseString & msg,
253+
bool & close_with_reset, bool & log_failure) {
254+
NdbSocket sock(sockfd, NdbSocket::From::Existing);
255+
return connect_server(sock, msg, close_with_reset, log_failure);
256+
}
257+
251258
bool connect_client(NdbMgmHandle *h);
252259

253260
/**
254-
* Given a SocketClient, creates a NdbMgmHandle, turns it into a transporter
255-
* and returns the socket.
261+
* Given a hostname and port, creates a NdbMgmHandle, turns it into
262+
* a transporter, and returns the socket.
256263
*/
257264
ndb_socket_t connect_ndb_mgmd(const char* server_name,
258265
unsigned short server_port);

storage/ndb/include/util/SocketAuthenticator.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,10 @@ class SocketAuthenticator
3232
public:
3333
SocketAuthenticator() {}
3434
virtual ~SocketAuthenticator() {}
35-
bool client_authenticate(ndb_socket_t);
36-
bool server_authenticate(ndb_socket_t);
3735
virtual bool client_authenticate(NdbSocket &) = 0;
3836
virtual bool server_authenticate(NdbSocket &) = 0;
3937
};
4038

41-
inline bool SocketAuthenticator::client_authenticate(ndb_socket_t fd) {
42-
NdbSocket socket(fd, NdbSocket::From::Existing);
43-
return client_authenticate(socket);
44-
}
45-
46-
inline bool SocketAuthenticator::server_authenticate(ndb_socket_t fd) {
47-
NdbSocket socket(fd, NdbSocket::From::Existing);
48-
return server_authenticate(socket);
49-
}
50-
5139

5240
class SocketAuthSimple : public SocketAuthenticator
5341
{

storage/ndb/src/common/transporter/TransporterRegistry.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#include "NdbSpin.h"
4747
#include "InputStream.hpp"
4848
#include "OutputStream.hpp"
49-
#include "socket_io.h"
5049
#include "portlib/NdbTCP.h"
5150

5251
#include <mgmapi/mgmapi.h>
@@ -115,25 +114,32 @@ TransporterRegistry::get_bytes_received(NodeId node_id) const
115114

116115
SocketServer::Session * TransporterService::newSession(ndb_socket_t sockfd)
117116
{
117+
/* The connection is currently running over a plain network socket.
118+
If m_auth is a TlsAuthenticator, it might get upgraded to a TLS socket
119+
in server_authenticate().
120+
*/
121+
NdbSocket secureSocket;
122+
secureSocket.init_from_new(sockfd);
123+
118124
DBUG_ENTER("SocketServer::Session * TransporterService::newSession");
119125
DEBUG_FPRINTF((stderr, "New session created\n"));
120-
if (m_auth && !m_auth->server_authenticate(sockfd))
126+
if (m_auth && !m_auth->server_authenticate(secureSocket))
121127
{
122128
DEBUG_FPRINTF((stderr, "Failed to authenticate new session\n"));
123-
ndb_socket_close_with_reset(sockfd, true); // Close with reset
129+
secureSocket.close_with_reset(true); // Close with reset
124130
DBUG_RETURN(0);
125131
}
126132

127133
BaseString msg;
128134
bool close_with_reset = true;
129135
bool log_failure = false;
130-
if (!m_transporter_registry->connect_server(sockfd,
136+
if (!m_transporter_registry->connect_server(secureSocket,
131137
msg,
132138
close_with_reset,
133139
log_failure))
134140
{
135141
DEBUG_FPRINTF((stderr, "New session failed in connect_server\n"));
136-
ndb_socket_close_with_reset(sockfd, close_with_reset);
142+
secureSocket.close_with_reset(close_with_reset);
137143
if (log_failure)
138144
{
139145
g_eventLogger->warning("TR : %s", msg.c_str());
@@ -469,7 +475,7 @@ TransporterRegistry::init(TransporterReceiveHandle& recvhandle)
469475
}
470476

471477
bool
472-
TransporterRegistry::connect_server(ndb_socket_t sockfd,
478+
TransporterRegistry::connect_server(NdbSocket & socket,
473479
BaseString & msg,
474480
bool& close_with_reset,
475481
bool& log_failure)
@@ -480,7 +486,7 @@ TransporterRegistry::connect_server(ndb_socket_t sockfd,
480486

481487
// Read "hello" that consists of node id and other info
482488
// from client
483-
SocketInputStream s_input(sockfd);
489+
SecureSocketInputStream s_input(socket);
484490
char buf[256]; // <int> <int> <int> <int> <..expansion..>
485491
if (s_input.gets(buf, sizeof(buf)) == nullptr) {
486492
/* Could be spurious connection, need not log */
@@ -741,7 +747,7 @@ TransporterRegistry::connect_server(ndb_socket_t sockfd,
741747
*/
742748

743749
// Avoid TIME_WAIT on server by requesting client to close connection
744-
SocketOutputStream s_output(sockfd);
750+
SecureSocketOutputStream s_output(socket);
745751
if (s_output.println("BYE") < 0)
746752
{
747753
// Failed to request client close
@@ -751,8 +757,7 @@ TransporterRegistry::connect_server(ndb_socket_t sockfd,
751757

752758
// Wait for to close connection by reading EOF(i.e read returns 0)
753759
const int read_eof_timeout = 1000; // Fairly short timeout
754-
if (read_socket(sockfd, read_eof_timeout,
755-
buf, sizeof(buf)) == 0)
760+
if (socket.read(read_eof_timeout, buf, sizeof(buf)) == 0)
756761
{
757762
// Client gracefully closed connection, turn off close_with_reset
758763
close_with_reset = false;
@@ -764,7 +769,7 @@ TransporterRegistry::connect_server(ndb_socket_t sockfd,
764769
}
765770

766771
// Send reply to client
767-
SocketOutputStream s_output(sockfd);
772+
SecureSocketOutputStream s_output(socket);
768773
if (s_output.println("%d %d", t->getLocalNodeId(), t->m_type) < 0)
769774
{
770775
/* Strange, log it */
@@ -778,7 +783,7 @@ TransporterRegistry::connect_server(ndb_socket_t sockfd,
778783
// Setup transporter (transporter responsible for closing sockfd)
779784
DEBUG_FPRINTF((stderr, "connect_server for trp_id %u\n",
780785
t->getTransporterIndex()));
781-
DBUG_RETURN(t->connect_server(sockfd, msg));
786+
DBUG_RETURN(t->connect_server(socket.ndb_socket(), msg)); // WL#15130 fix me soon
782787
}
783788

784789
void

0 commit comments

Comments
 (0)