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 1 commit
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
9 changes: 9 additions & 0 deletions include/mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ extern const char *SQLSTATE_UNKNOWN;
MYSQL_OPT_MAX_ALLOWED_PACKET,
MYSQL_OPT_NET_BUFFER_LENGTH,
MYSQL_OPT_TLS_VERSION,
MYSQL_OPT_USE_REDIRECTION,
shih-che marked this conversation as resolved.
Show resolved Hide resolved

/* MariaDB specific */
MYSQL_PROGRESS_CALLBACK=5999,
Expand Down Expand Up @@ -302,6 +303,13 @@ extern const char *SQLSTATE_UNKNOWN;
MYSQL_PROTOCOL_PIPE, MYSQL_PROTOCOL_MEMORY
};

typedef enum mysql_redirection_mode
shih-che marked this conversation as resolved.
Show resolved Hide resolved
{
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 All @@ -323,6 +331,7 @@ struct st_mysql_options {
char *bind_address;
my_bool secure_auth;
my_bool report_data_truncation;
enable_redirect redirection_mode;
shih-che marked this conversation as resolved.
Show resolved Hide resolved
/* function pointers for local infile support */
int (*local_infile_init)(void **, const char *, void *);
int (*local_infile_read)(void *, char *, unsigned int);
Expand Down
3 changes: 3 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 MYSQL_OPT_USE_REDIRECTION:
mysql->options.redirection_mode = *(enable_redirect*)arg1;
break;
case MARIADB_OPT_CONNECTION_HANDLER:
OPT_SET_EXTENDED_VALUE_STR(&mysql->options, connection_handler, (char *)arg1);
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)
116 changes: 116 additions & 0 deletions plugins/connection/redirection.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/************************************************************************************
Copyright (C) 2015-2018 MariaDB Corporation AB
shih-che marked this conversation as resolved.
Show resolved Hide resolved

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
Part of this code includes code from the PHP project which
is freely available from http://www.php.net
*************************************************************************************/
/* 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