Commit e64b11b0 authored by unknown's avatar unknown
Browse files

ndb - bug#22195

  allow bind address for ndbd


ndb/include/mgmapi/mgmapi.h:
  Add support for setting local address in NdbMgmHandle
ndb/include/mgmcommon/ConfigRetriever.hpp:
  Add support for specifying local bindaddress to ConfigRetreiver
ndb/include/util/SocketClient.hpp:
  Add support for setting local address in NdbMgmHandle
ndb/src/common/mgmcommon/ConfigRetriever.cpp:
  Add support for setting local address in NdbMgmHandle
ndb/src/common/util/SocketClient.cpp:
  Add support for setting local address in NdbMgmHandle
ndb/src/kernel/vm/Configuration.cpp:
  Add support for specifying local bindaddress to ndbd
ndb/src/mgmapi/mgmapi.cpp:
  Add support for setting local address in NdbMgmHandle
parent 14bebaa2
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -231,6 +231,8 @@ extern "C" {
    NDB_MGM_SERVER_NOT_CONNECTED = 1010,
    /** Could not connect to socker */
    NDB_MGM_COULD_NOT_CONNECT_TO_SOCKET = 1011,
    /** Could not bind local address */
    NDB_MGM_BIND_ADDRESS = 1012,
    
    /* Alloc node id failures */
    /** Generic error, retry may succeed */
@@ -515,6 +517,15 @@ extern "C" {
  const char *ndb_mgm_get_connected_host(NdbMgmHandle handle);
  const char *ndb_mgm_get_connectstring(NdbMgmHandle handle, char *buf, int buf_sz);

  /**
   * Set local bindaddress
   * @param arg - Srting of form "host[:port]"
   * @note must be called before connect
   * @note Error on binding local address will not be reported until connect
   * @return 0 on success
   */
  int ndb_mgm_set_bindaddress(NdbMgmHandle, const char * arg);

  /**
   * Gets the connectstring used for a connection
   *
+2 −1
Original line number Diff line number Diff line
@@ -28,7 +28,8 @@
class ConfigRetriever {
public:
  ConfigRetriever(const char * _connect_string,
		  Uint32 version, Uint32 nodeType);
		  Uint32 version, Uint32 nodeType,
		  const char * _bind_address = 0);
  ~ConfigRetriever();

  int do_connect(int no_retries, int retry_delay_in_seconds, int verbose);
+2 −1
Original line number Diff line number Diff line
@@ -37,7 +37,8 @@ public:
  };
  unsigned short get_port() { return m_port; };
  char *get_server_name() { return m_server_name; };
  NDB_SOCKET_TYPE connect();
  int bind(const char* toaddress, unsigned short toport);
  NDB_SOCKET_TYPE connect(const char* toaddress = 0, unsigned short port = 0);
  bool close();
};

+11 −1
Original line number Diff line number Diff line
@@ -45,7 +45,8 @@
//****************************************************************************

ConfigRetriever::ConfigRetriever(const char * _connect_string,
				 Uint32 version, Uint32 node_type)
				 Uint32 version, Uint32 node_type,
				 const char * _bindaddress)
{
  DBUG_ENTER("ConfigRetriever::ConfigRetriever");

@@ -66,6 +67,15 @@ ConfigRetriever::ConfigRetriever(const char * _connect_string,
    setError(CR_ERROR, ndb_mgm_get_latest_error_desc(m_handle));
    DBUG_VOID_RETURN;
  }

  if (_bindaddress)
  {
    if (ndb_mgm_set_bindaddress(m_handle, _bindaddress))
    {
      setError(CR_ERROR, ndb_mgm_get_latest_error_desc(m_handle));
      DBUG_VOID_RETURN;
    }
  }
  resetError();
  DBUG_VOID_RETURN;
}
+64 −9
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ SocketClient::SocketClient(const char *server_name, unsigned short port, SocketA
{
  m_auth= sa;
  m_port= port;
  m_server_name= strdup(server_name);
  m_server_name= server_name ? strdup(server_name) : 0;
  m_sockfd= NDB_INVALID_SOCKET;
}

@@ -45,12 +45,15 @@ SocketClient::init()
  if (m_sockfd != NDB_INVALID_SOCKET)
    NDB_CLOSE_SOCKET(m_sockfd);

  if (m_server_name)
  {
    memset(&m_servaddr, 0, sizeof(m_servaddr));
    m_servaddr.sin_family = AF_INET;
    m_servaddr.sin_port = htons(m_port);
    // Convert ip address presentation format to numeric format
    if (Ndb_getInAddr(&m_servaddr.sin_addr, m_server_name))
      return false;
  }
  
  m_sockfd= socket(AF_INET, SOCK_STREAM, 0);
  if (m_sockfd == NDB_INVALID_SOCKET) {
@@ -62,8 +65,45 @@ SocketClient::init()
  return true;
}

int
SocketClient::bind(const char* bindaddress, unsigned short localport)
{
  if (m_sockfd == NDB_INVALID_SOCKET)
    return -1;

  struct sockaddr_in local;
  memset(&local, 0, sizeof(local));
  local.sin_family = AF_INET;
  local.sin_port = htons(localport);
  // Convert ip address presentation format to numeric format
  if (Ndb_getInAddr(&local.sin_addr, bindaddress))
  {
    return errno ? errno : EINVAL;
  }
  
  const int on = 1;
  if (setsockopt(m_sockfd, SOL_SOCKET, SO_REUSEADDR, 
		 (const char*)&on, sizeof(on)) == -1) {

    int ret = errno;
    NDB_CLOSE_SOCKET(m_sockfd);
    m_sockfd= NDB_INVALID_SOCKET;
    return errno;
  }
  
  if (::bind(m_sockfd, (struct sockaddr*)&local, sizeof(local)) == -1) 
  {
    int ret = errno;
    NDB_CLOSE_SOCKET(m_sockfd);
    m_sockfd= NDB_INVALID_SOCKET;
    return ret;
  }
  
  return 0;
}

NDB_SOCKET_TYPE
SocketClient::connect()
SocketClient::connect(const char *toaddress, unsigned short toport)
{
  if (m_sockfd == NDB_INVALID_SOCKET)
  {
@@ -74,6 +114,21 @@ SocketClient::connect()
      return NDB_INVALID_SOCKET;
    }
  }

  if (toaddress)
  {
    if (m_server_name)
      free(m_server_name);
    m_server_name = strdup(toaddress);
    m_port = toport;
    memset(&m_servaddr, 0, sizeof(m_servaddr));
    m_servaddr.sin_family = AF_INET;
    m_servaddr.sin_port = htons(toport);
    // Convert ip address presentation format to numeric format
    if (Ndb_getInAddr(&m_servaddr.sin_addr, m_server_name))
      return NDB_INVALID_SOCKET;
  }
  
  const int r = ::connect(m_sockfd, (struct sockaddr*) &m_servaddr, sizeof(m_servaddr));
  if (r == -1) {
    NDB_CLOSE_SOCKET(m_sockfd);
Loading