Allow binding of client to address
===== client/client_priv.h 1.51 vs edited =====
Index: telco/client/client_priv.h
===================================================================
--- telco.orig/client/client_priv.h 2007-10-09 11:19:14.199447817 +1000
+++ telco/client/client_priv.h 2007-10-09 11:19:16.875579072 +1000
@@ -78,6 +78,7 @@ enum options_client
OPT_SLAP_COMMIT,
OPT_SLAP_DETACH,
OPT_MYSQL_REPLACE_INTO, OPT_BASE64_OUTPUT, OPT_SERVER_ID,
+ OPT_BIND_ADDRESS,
OPT_FIX_TABLE_NAMES, OPT_FIX_DB_NAMES, OPT_SSL_VERIFY_SERVER_CERT,
OPT_DEBUG_INFO, OPT_DEBUG_CHECK, OPT_COLUMN_TYPES, OPT_ERROR_LOG_FILE,
OPT_WRITE_BINLOG, OPT_MAX_CLIENT_OPTION
Index: telco/client/mysql.cc
===================================================================
--- telco.orig/client/mysql.cc 2007-10-09 11:19:14.231449387 +1000
+++ telco/client/mysql.cc 2007-10-09 11:19:16.907580642 +1000
@@ -148,6 +148,7 @@ static ulong opt_max_allowed_packet, opt
static uint verbose=0,opt_silent=0,opt_mysql_port=0, opt_local_infile=0;
static uint my_end_arg;
static char * opt_mysql_unix_port=0;
+static char *opt_bind_addr = NULL;
static int connect_flag=CLIENT_INTERACTIVE;
static char *current_host,*current_db,*current_user=0,*opt_password=0,
*current_prompt=0, *delimiter_str= 0,
@@ -598,6 +599,9 @@ static struct my_option my_long_options[
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
{"batch", 'B',
"Don't use history file. Disable interactive behavior. (Enables --silent)", 0, 0, 0,
GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
+ {"bind-address", OPT_BIND_ADDRESS, "IP address to bind to.",
+ (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0, GET_STR,
+ REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"character-sets-dir", OPT_CHARSETS_DIR,
"Directory where character sets are.", (uchar**) &charsets_dir,
(uchar**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
@@ -3278,6 +3282,10 @@ sql_real_connect(char *host,char *databa
mysql_options(&mysql,MYSQL_OPT_CONNECT_TIMEOUT,
(char*) &timeout);
}
+ if (opt_bind_addr)
+ {
+ mysql_options(&mysql, MYSQL_OPT_BIND, opt_bind_addr);
+ }
if (opt_compress)
mysql_options(&mysql,MYSQL_OPT_COMPRESS,NullS);
if (opt_secure_auth)
Index: telco/client/mysqladmin.cc
===================================================================
--- telco.orig/client/mysqladmin.cc 2007-10-09 11:19:14.235449583 +1000
+++ telco/client/mysqladmin.cc 2007-10-09 11:19:16.935582015 +1000
@@ -44,6 +44,7 @@ static my_bool option_force=0,interrupte
static my_bool debug_info_flag= 0, debug_check_flag= 0;
static uint tcp_port = 0, option_wait = 0, option_silent=0, nr_iterations;
static uint opt_count_iterations= 0, my_end_arg;
+static char *opt_bind_addr = NULL;
static ulong opt_connect_timeout, opt_shutdown_timeout;
static char * unix_port=0;
#ifdef LATER_HAVE_NDBCLUSTER_DB
@@ -133,6 +134,9 @@ static struct my_option my_long_options[
{"autoclose", OPT_AUTO_CLOSE, "Auto close the screen on exit for Netware.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
#endif
+ {"bind-address", OPT_BIND_ADDRESS, "IP address to bind to.",
+ (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0, GET_STR,
+ REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"count", 'c',
"Number of iterations to make. This works with -i (--sleep) only.",
(uchar**) &nr_iterations, (uchar**) &nr_iterations, 0, GET_UINT,
@@ -341,6 +345,10 @@ int main(int argc,char *argv[])
VOID(signal(SIGINT,endprog)); /* Here if abort */
VOID(signal(SIGTERM,endprog)); /* Here if abort */
+ if (opt_bind_addr)
+ {
+ mysql_options(&mysql,MYSQL_OPT_BIND,opt_bind_addr);
+ }
if (opt_compress)
mysql_options(&mysql,MYSQL_OPT_COMPRESS,NullS);
if (opt_connect_timeout)
Index: telco/client/mysqlbinlog.cc
===================================================================
--- telco.orig/client/mysqlbinlog.cc 2007-10-09 11:19:14.255450564 +1000
+++ telco/client/mysqlbinlog.cc 2007-10-09 11:19:17.307600262 +1000
@@ -75,6 +75,7 @@ static uint my_end_arg;
static const char* sock= 0;
static const char* user = 0;
static char* pass = 0;
+static char *opt_bind_addr = NULL;
static char *charset= 0;
static ulonglong start_position, stop_position;
@@ -713,6 +714,9 @@ static struct my_option my_long_options[
"should not be applied on production systems.",
(uchar**) &opt_base64_output, (uchar**) &opt_base64_output, 0, GET_BOOL,
NO_ARG, 0, 0, 0, 0, 0, 0},
+ {"bind-address", OPT_BIND_ADDRESS, "IP address to bind to.",
+ (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0, GET_STR,
+ REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
/*
mysqlbinlog needs charsets knowledge, to be able to convert a charset
number found in binlog to a charset name (to be able to print things
@@ -1006,6 +1010,10 @@ static MYSQL* safe_connect()
if (opt_protocol)
mysql_options(local_mysql, MYSQL_OPT_PROTOCOL, (char*) &opt_protocol);
+ if (opt_bind_addr)
+ {
+ mysql_options(local_mysql,MYSQL_OPT_BIND,opt_bind_addr);
+ }
if (!mysql_real_connect(local_mysql, host, user, pass, 0, port, sock, 0))
{
char errmsg[256];
Index: telco/client/mysqlcheck.c
===================================================================
--- telco.orig/client/mysqlcheck.c 2007-10-09 11:19:14.263450956 +1000
+++ telco/client/mysqlcheck.c 2007-10-09 11:19:17.399604774 +1000
@@ -49,6 +49,7 @@ static char *shared_memory_base_name=0;
#endif
static uint opt_protocol=0;
static CHARSET_INFO *charset_info= &my_charset_latin1;
+static char *opt_bind_addr = NULL;
enum operations { DO_CHECK, DO_REPAIR, DO_ANALYZE, DO_OPTIMIZE, DO_UPGRADE };
@@ -72,6 +73,9 @@ static struct my_option my_long_options[
"If a checked table is corrupted, automatically fix it. Repairing will be done after
all tables have been checked, if corrupted ones were found.",
(uchar**) &opt_auto_repair, (uchar**) &opt_auto_repair, 0, GET_BOOL, NO_ARG,
0,
0, 0, 0, 0, 0},
+ {"bind-address", OPT_BIND_ADDRESS, "IP address to bind to.",
+ (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0, GET_STR,
+ REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"character-sets-dir", OPT_CHARSETS_DIR,
"Directory where character sets are.", (uchar**) &charsets_dir,
(uchar**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
@@ -719,6 +723,10 @@ static int dbConnect(char *host, char *u
#endif
if (opt_protocol)
mysql_options(&mysql_connection,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
+ if (opt_bind_addr)
+ {
+ mysql_options(&mysql_connection,MYSQL_OPT_BIND,opt_bind_addr);
+ }
#ifdef HAVE_SMEM
if (shared_memory_base_name)
mysql_options(&mysql_connection,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
Index: telco/client/mysqldump.c
===================================================================
--- telco.orig/client/mysqldump.c 2007-10-09 11:19:14.267451153 +1000
+++ telco/client/mysqldump.c 2007-10-09 11:19:17.455607521 +1000
@@ -117,6 +117,7 @@ static ulong opt_compatible_mode= 0;
#define MYSQL_OPT_MASTER_DATA_EFFECTIVE_SQL 1
#define MYSQL_OPT_MASTER_DATA_COMMENTED_SQL 2
static uint opt_mysql_port= 0, opt_master_data;
+static char *opt_bind_addr = NULL;
static uint my_end_arg;
static char * opt_mysql_unix_port=0;
static int first_error=0;
@@ -208,6 +209,9 @@ static struct my_option my_long_options[
{"autoclose", OPT_AUTO_CLOSE, "Auto close the screen on exit for Netware.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
#endif
+ {"bind-address", OPT_BIND_ADDRESS, "IP address to bind to.",
+ (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0, GET_STR,
+ REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"character-sets-dir", OPT_CHARSETS_DIR,
"Directory where character sets are.", (uchar**) &charsets_dir,
(uchar**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
@@ -1429,6 +1433,10 @@ static int connect_to_db(char *host, cha
#endif
if (opt_protocol)
mysql_options(&mysql_connection,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
+ if (opt_bind_addr)
+ {
+ mysql_options(&mysql_connection,MYSQL_OPT_BIND,opt_bind_addr);
+ }
#ifdef HAVE_SMEM
if (shared_memory_base_name)
mysql_options(&mysql_connection,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
Index: telco/client/mysqlimport.c
===================================================================
--- telco.orig/client/mysqlimport.c 2007-10-09 11:19:14.287452134 +1000
+++ telco/client/mysqlimport.c 2007-10-09 11:19:17.519610660 +1000
@@ -57,6 +57,7 @@ static char *opt_password=0, *current_us
*escaped=0, *opt_columns=0,
*default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME;
static uint opt_mysql_port= 0, opt_protocol= 0;
+static char *opt_bind_addr = NULL;
static char * opt_mysql_unix_port=0;
static longlong opt_ignore_lines= -1;
static CHARSET_INFO *charset_info= &my_charset_latin1;
@@ -72,6 +73,9 @@ static struct my_option my_long_options[
{"autoclose", OPT_AUTO_CLOSE, "Auto close the screen on exit for Netware.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
#endif
+ {"bind-address", OPT_BIND_ADDRESS, "IP address to bind to.",
+ (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0, GET_STR,
+ REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"character-sets-dir", OPT_CHARSETS_DIR,
"Directory where character sets are.", (uchar**) &charsets_dir,
(uchar**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
@@ -417,6 +421,8 @@ static MYSQL *db_connect(char *host, cha
#endif
if (opt_protocol)
mysql_options(mysql,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
+ if (opt_bind_addr)
+ mysql_options(mysql,MYSQL_OPT_BIND,opt_bind_addr);
#ifdef HAVE_SMEM
if (shared_memory_base_name)
mysql_options(mysql,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
Index: telco/client/mysqlshow.c
===================================================================
--- telco.orig/client/mysqlshow.c 2007-10-09 11:19:14.355455469 +1000
+++ telco/client/mysqlshow.c 2007-10-09 11:19:17.691619096 +1000
@@ -38,6 +38,7 @@ static char *default_charset= (char*) MY
static char *shared_memory_base_name=0;
#endif
static uint opt_protocol=0;
+static char *opt_bind_addr = NULL;
static void get_options(int *argc,char ***argv);
static uint opt_mysql_port=0;
@@ -115,6 +116,10 @@ int main(int argc, char **argv)
#endif
if (opt_protocol)
mysql_options(&mysql,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
+ if (opt_bind_addr)
+ {
+ mysql_options(&mysql,MYSQL_OPT_BIND,opt_bind_addr);
+ }
#ifdef HAVE_SMEM
if (shared_memory_base_name)
mysql_options(&mysql,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
@@ -163,6 +168,9 @@ static struct my_option my_long_options[
{"autoclose", OPT_AUTO_CLOSE, "Auto close the screen on exit for Netware.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
#endif
+ {"bind-address", OPT_BIND_ADDRESS, "IP address to bind to.",
+ (uchar**) &opt_bind_addr, (uchar**) &opt_bind_addr, 0, GET_STR,
+ REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"character-sets-dir", 'c', "Directory where character sets are.",
(uchar**) &charsets_dir, (uchar**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0,
0, 0, 0, 0, 0},
Index: telco/include/mysql.h
===================================================================
--- telco.orig/include/mysql.h 2007-10-09 11:19:14.363455861 +1000
+++ telco/include/mysql.h 2007-10-09 11:19:17.911629887 +1000
@@ -166,7 +166,7 @@ enum mysql_option
MYSQL_OPT_WRITE_TIMEOUT, MYSQL_OPT_USE_RESULT,
MYSQL_OPT_USE_REMOTE_CONNECTION, MYSQL_OPT_USE_EMBEDDED_CONNECTION,
MYSQL_OPT_GUESS_CONNECTION, MYSQL_SET_CLIENT_IP, MYSQL_SECURE_AUTH,
- MYSQL_REPORT_DATA_TRUNCATION, MYSQL_OPT_RECONNECT,
+ MYSQL_REPORT_DATA_TRUNCATION, MYSQL_OPT_RECONNECT, MYSQL_OPT_BIND,
MYSQL_OPT_SSL_VERIFY_SERVER_CERT
};
@@ -206,6 +206,8 @@ struct st_mysql_options {
#endif
enum mysql_option methods_to_use;
char *client_ip;
+ char *bind_name;
+
/* Refuse client connecting to server if it uses old (pre-4.1.1) protocol */
my_bool secure_auth;
/* 0 - never report, 1 - always report (default) */
Index: telco/sql-common/client.c
===================================================================
--- telco.orig/sql-common/client.c 2007-10-09 11:19:14.371456254 +1000
+++ telco/sql-common/client.c 2007-10-09 11:25:47.130719760 +1000
@@ -1992,6 +1992,50 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,cons
ER(net->last_errno),socket_errno);
goto error;
}
+ if (mysql->options.bind_name) {
+ /* TODOs for client bind:
+ - check error codes
+ - don't use socket for localhost if this option is given
+ */
+ in_addr_t bind_addr;
+ bind_addr = inet_addr(mysql->options.bind_name);
+ if (bind_addr == INADDR_NONE)
+ {
+ int tmp_errno;
+ struct hostent tmp_hostent,*hp;
+ char buff2[GETHOSTBYNAME_BUFF_SIZE];
+ hp = my_gethostbyname_r(mysql->options.bind_name, &tmp_hostent,
+ buff2, sizeof(buff2), &tmp_errno);
+ if (!hp)
+ {
+ my_gethostbyname_r_free();
+ net->last_errno=CR_UNKNOWN_HOST;
+ strmov(net->sqlstate, unknown_sqlstate);
+ my_snprintf(net->last_error, sizeof(net->last_error)-1,
+ ER(CR_UNKNOWN_HOST), mysql->options.bind_name, tmp_errno);
+ goto error;
+ }
+ bzero(&bind_addr,sizeof(bind_addr));
+ memcpy(&bind_addr, hp->h_addr,
+ min(sizeof(sock_addr.sin_addr), (size_t) hp->h_length));
+ my_gethostbyname_r_free();
+ }
+ if (bind_addr != INADDR_NONE) {
+ struct sockaddr_in IPaddr;
+ bzero((char*) &IPaddr, sizeof(IPaddr));
+ IPaddr.sin_family = AF_INET;
+ IPaddr.sin_addr.s_addr = bind_addr;
+ IPaddr.sin_port = 0;
+ if (bind(sock, &IPaddr, sizeof(IPaddr))) {
+ net->last_errno=CR_IPSOCK_ERROR;
+ strmov(net->sqlstate, unknown_sqlstate);
+ my_snprintf(net->last_error, sizeof(net->last_error)-1,
+ ER(CR_IPSOCK_ERROR), mysql->options.bind_name, errno);
+ goto error;
+ }
+ }
+ }
+
net->vio= vio_new(sock, VIO_TYPE_TCPIP, VIO_BUFFERED_READ);
bzero((char*) &sock_addr,sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
@@ -2560,6 +2604,7 @@ static void mysql_close_free_options(MYS
my_free(mysql->options.charset_dir,MYF(MY_ALLOW_ZERO_PTR));
my_free(mysql->options.charset_name,MYF(MY_ALLOW_ZERO_PTR));
my_free(mysql->options.client_ip,MYF(MY_ALLOW_ZERO_PTR));
+ my_free(mysql->options.bind_name,MYF(MY_ALLOW_ZERO_PTR));
if (mysql->options.init_commands)
{
DYNAMIC_ARRAY *init_commands= mysql->options.init_commands;
@@ -3040,6 +3085,10 @@ mysql_options(MYSQL *mysql,enum mysql_op
case MYSQL_OPT_RECONNECT:
mysql->reconnect= *(my_bool *) arg;
break;
+ case MYSQL_OPT_BIND:
+ my_free(mysql->options.bind_name, MYF(MY_ALLOW_ZERO_PTR));
+ mysql->options.bind_name= my_strdup(arg, MYF(MY_WME));
+ break;
case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
if (*(my_bool*) arg)
mysql->options.client_flag|= CLIENT_SSL_VERIFY_SERVER_CERT;
--
Stewart Smith