Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirection client support #137

Open
wants to merge 4 commits into
base: 3.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/ma_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ struct st_mysql_options_extension {
char *proxy_header;
size_t proxy_header_len;
int (*io_wait)(my_socket handle, my_bool is_read, int timeout);
enable_redirect redirection_mode;
};

typedef struct st_connection_handler
Expand Down
10 changes: 9 additions & 1 deletion include/mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ extern const char *SQLSTATE_UNKNOWN;
MARIADB_OPT_MULTI_STATEMENTS,
MARIADB_OPT_INTERACTIVE,
MARIADB_OPT_PROXY_HEADER,
MARIADB_OPT_IO_WAIT
MARIADB_OPT_IO_WAIT,
MARIADB_OPT_USE_REDIRECTION
};

enum mariadb_value {
Expand Down Expand Up @@ -302,6 +303,13 @@ extern const char *SQLSTATE_UNKNOWN;
MYSQL_PROTOCOL_PIPE, MYSQL_PROTOCOL_MEMORY
};

typedef enum mariadb_redirection_mode
{
REDIRECTION_OFF,
REDIRECTION_ON,
REDIRECTION_PREFERRED
} enable_redirect;

struct st_mysql_options {
unsigned int connect_timeout, read_timeout, write_timeout;
unsigned int port, protocol;
Expand Down
6 changes: 6 additions & 0 deletions libmariadb/mariadb_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -3053,6 +3053,9 @@ mysql_optionsv(MYSQL *mysql,enum mysql_option option, ...)
mysql->options.extension->connect_attrs_len= 0;
}
break;
case MARIADB_OPT_USE_REDIRECTION:
OPT_SET_EXTENDED_VALUE(&mysql->options, redirection_mode, *(enable_redirect*)arg1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, don't do that. sizeof(enum) is not portable, it depends even on compilation options. Use a stable portable type for passing arguments. For example, uint

break;
case MARIADB_OPT_CONNECTION_HANDLER:
OPT_SET_EXTENDED_VALUE_STR(&mysql->options, connection_handler, (char *)arg1);
break;
Expand Down Expand Up @@ -3416,6 +3419,9 @@ mysql_get_optionv(MYSQL *mysql, enum mysql_option option, void *arg, ...)
case MYSQL_OPT_BIND:
*((char **)arg)= mysql->options.bind_address;
break;
case MARIADB_OPT_USE_REDIRECTION:
*((enable_redirect *)arg) = mysql->options.extension ? mysql->options.extension->redirection_mode : REDIRECTION_OFF;
break;
case MARIADB_OPT_TLS_CIPHER_STRENGTH:
*((unsigned int *)arg) = mysql->options.extension ? mysql->options.extension->tls_cipher_strength : 0;
break;
Expand Down
8 changes: 8 additions & 0 deletions plugins/connection/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ REGISTER_PLUGIN(TARGET replication
CONFIGURATIONS STATIC DYNAMIC OFF
DEFAULT OFF
SOURCES ${CC_SOURCE_DIR}/plugins/connection/replication.c)

# Redirection
REGISTER_PLUGIN(TARGET redirection
TYPE MARIADB_CLIENT_PLUGIN_CONNECTION
CONFIGURATIONS STATIC DYNAMIC OFF
DEFAULT DYNAMIC
SOURCES ${CC_SOURCE_DIR}/plugins/connection/redirection.c
${CC_SOURCE_DIR}/plugins/connection/redirection_utility.c)
114 changes: 114 additions & 0 deletions plugins/connection/redirection.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/************************************************************************************
Copyright (C) 2020 MariaDB Corporation AB

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not see <http://www.gnu.org/licenses>
or write to the Free Software Foundation, Inc.,
51 Franklin St., Fifth Floor, Boston, MA 02110, USA
*************************************************************************************/
/* MariaDB Connection plugin for redirection. */
#include <ma_global.h>
#include <ma_sys.h>
#include <errmsg.h>
#include <mysql.h>
#include <mysql/client_plugin.h>
#include <string.h>
#include <ma_string.h>
#include <ma_common.h>
#include "redirection_utility.h"
#ifndef WIN32
#include <sys/time.h>
#endif
/* redirection function declaration */
int redirection_init(char* errbuf, size_t buf_size, int argc, va_list argv);
MYSQL* redirection_connect(
MYSQL* mysql,
const char* host,
const char* user,
const char* passwd,
const char* db,
unsigned int port,
const char* unix_socket,
unsigned long clientflag);
void redirection_close(MYSQL* mysql);
int redirection_set_connection(
MYSQL* mysql,
enum enum_server_command command,
const char* arg,
size_t length,
my_bool skipp_check,
void* opt_arg);

#ifndef PLUGIN_DYNAMIC
MARIADB_CONNECTION_PLUGIN redirection_client_plugin =
#else
MARIADB_CONNECTION_PLUGIN _mysql_client_plugin_declaration_ =
#endif
{
MARIADB_CLIENT_CONNECTION_PLUGIN,
MARIADB_CLIENT_CONNECTION_PLUGIN_INTERFACE_VERSION,
"redirection",
"Shihao Chen",
"MariaDB connection plugin for redirection",
{1, 0, 0},
"LGPL",
NULL,
redirection_init,
NULL,
NULL,
redirection_connect,
redirection_close,
NULL,
redirection_set_connection,
NULL,
NULL
};

int redirection_init(char* errbuf, size_t buf_size, int argc, va_list argv) {
return init_redirection_cache();
}

MYSQL* redirection_connect(MYSQL* mysql, const char* host, const char* user, const char* passwd,
const char* db, unsigned int port, const char* unix_socket, unsigned long clientflag)
{
struct st_mariadb_api* libmariadb_api = mysql->methods->api;
if (libmariadb_api == NULL)
{
return NULL;
}

if (check_redirect(mysql, host, user, passwd, db, port, unix_socket, clientflag)) {
return mysql;
}

return redirect(mysql, host, user, passwd, db, port, unix_socket, clientflag);
}

void redirection_close(MYSQL* mysql)
{
if (mysql != NULL)
{
mysql->extension->conn_hdlr->data = NULL;
}
}

int redirection_set_connection(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it for future use? It just returns 0, without setting any connection?!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't mean to use set_connection... But without defining a dummy set_connection the client will crash in mthd_my_send_cmd:

if (IS_CONNHDLR_ACTIVE(mysql))
{
    result= mysql->extension->conn_hdlr->plugin->set_connection(mysql, command, arg,     length, skipp_check, opt_arg);
    ...
}

Looks to me that a null check is missing here(as the check exists for other plugin members like reconnect and reset); or do I get it wrong and set_connection is a must-have member for a connection plugin? If so, what is it supposed to do?

MYSQL* mysql,
enum enum_server_command command,
const char* arg,
size_t length,
my_bool skipp_check,
void* opt_arg)
{
return 0;
}
Loading