Skip to content

Commit 207d0bb

Browse files
adds tls sni support to mysql2
1 parent 441b104 commit 207d0bb

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

ext/mysql2/client.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,26 +504,34 @@ static int opt_connect_attr_add_i(VALUE key, VALUE value, VALUE arg)
504504
}
505505
#endif
506506

507-
static VALUE rb_mysql_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE port, VALUE database, VALUE socket, VALUE flags, VALUE conn_attrs) {
507+
static VALUE rb_mysql_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE port, VALUE database, VALUE socket, VALUE flags, VALUE conn_attrs, VALUE tls_sni_name) {
508508
struct nogvl_connect_args args;
509509
time_t start_time, end_time, elapsed_time, connect_timeout;
510+
const char *sni_hostname;
510511
VALUE rv;
511512
GET_CLIENT(self);
512513

513-
args.host = NIL_P(host) ? NULL : StringValueCStr(host);
514-
args.unix_socket = NIL_P(socket) ? NULL : StringValueCStr(socket);
515-
args.port = NIL_P(port) ? 0 : NUM2INT(port);
516-
args.user = NIL_P(user) ? NULL : StringValueCStr(user);
517-
args.passwd = NIL_P(pass) ? NULL : StringValueCStr(pass);
518-
args.db = NIL_P(database) ? NULL : StringValueCStr(database);
519-
args.mysql = wrapper->client;
520-
args.client_flag = NUM2ULONG(flags);
514+
args.host = NIL_P(host) ? NULL : StringValueCStr(host);
515+
args.unix_socket = NIL_P(socket) ? NULL : StringValueCStr(socket);
516+
args.port = NIL_P(port) ? 0 : NUM2INT(port);
517+
args.user = NIL_P(user) ? NULL : StringValueCStr(user);
518+
args.passwd = NIL_P(pass) ? NULL : StringValueCStr(pass);
519+
args.db = NIL_P(database) ? NULL : StringValueCStr(database);
520+
args.mysql = wrapper->client;
521+
args.client_flag = NUM2ULONG(flags);
522+
523+
sni_hostname = NIL_P(tls_sni_name) ? NULL : StringValueCStr(tls_sni_name);
521524

522525
#ifdef CLIENT_CONNECT_ATTRS
523526
mysql_options(wrapper->client, MYSQL_OPT_CONNECT_ATTR_RESET, 0);
524527
rb_hash_foreach(conn_attrs, opt_connect_attr_add_i, (VALUE)wrapper);
525528
#endif
526529

530+
if(sni_hostname != NULL) {
531+
/* Set the TLS SNI name if provided */
532+
mysql_options(wrapper->client, MYSQL_OPT_TLS_SNI_SERVERNAME, sni_hostname);
533+
}
534+
527535
if (wrapper->connect_timeout)
528536
time(&start_time);
529537
rv = (VALUE) rb_thread_call_without_gvl(nogvl_connect, &args, RUBY_UBF_IO, 0);
@@ -1619,7 +1627,7 @@ void init_mysql2_client() {
16191627
rb_define_private_method(cMysql2Client, "ssl_mode=", rb_set_ssl_mode_option, 1);
16201628
rb_define_private_method(cMysql2Client, "enable_cleartext_plugin=", set_enable_cleartext_plugin, 1);
16211629
rb_define_private_method(cMysql2Client, "initialize_ext", initialize_ext, 0);
1622-
rb_define_private_method(cMysql2Client, "connect", rb_mysql_connect, 8);
1630+
rb_define_private_method(cMysql2Client, "connect", rb_mysql_connect, 9);
16231631
rb_define_private_method(cMysql2Client, "_query", rb_mysql_query, 2);
16241632

16251633
sym_id = ID2SYM(rb_intern("id"));

lib/mysql2/client.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,25 @@ def initialize(opts = {})
7373

7474
check_and_clean_query_options
7575

76-
user = opts[:username] || opts[:user]
77-
pass = opts[:password] || opts[:pass]
78-
host = opts[:host] || opts[:hostname]
79-
port = opts[:port]
80-
database = opts[:database] || opts[:dbname] || opts[:db]
81-
socket = opts[:socket] || opts[:sock]
76+
user = opts[:username] || opts[:user]
77+
pass = opts[:password] || opts[:pass]
78+
host = opts[:host] || opts[:hostname]
79+
tls_sni_name = opts[:tls_sni_name]
80+
port = opts[:port]
81+
database = opts[:database] || opts[:dbname] || opts[:db]
82+
socket = opts[:socket] || opts[:sock]
8283

8384
# Correct the data types before passing these values down to the C level
8485
user = user.to_s unless user.nil?
8586
pass = pass.to_s unless pass.nil?
8687
host = host.to_s unless host.nil?
8788
port = port.to_i unless port.nil?
8889
database = database.to_s unless database.nil?
90+
tls_sni_name = tls_sni_name.to_s unless tls_sni_name.nil?
8991
socket = socket.to_s unless socket.nil?
9092
conn_attrs = parse_connect_attrs(opts[:connect_attrs])
9193

92-
connect user, pass, host, port, database, socket, flags, conn_attrs
94+
connect user, pass, host, port, database, socket, flags, conn_attrs, tls_sni_name
9395
end
9496

9597
def parse_ssl_mode(mode)

0 commit comments

Comments
 (0)