diff --git a/telegram-base.c b/telegram-base.c index 0e5ee46..d9b158d 100644 --- a/telegram-base.c +++ b/telegram-base.c @@ -112,8 +112,14 @@ void write_files_schedule (struct tgl_state *TLS) { } } +struct write_dc_extra { + int auth_file; + int flags; +}; + void write_dc (struct tgl_dc *DC, void *extra) { - int auth_file_fd = *(int *)extra; + struct write_dc_extra *ex = extra; + int auth_file_fd = ex->auth_file; if (!DC) { int x = 0; assert (write (auth_file_fd, &x, 4) == 4); @@ -125,15 +131,16 @@ void write_dc (struct tgl_dc *DC, void *extra) { assert (DC->flags & TGLDCF_LOGGED_IN); - assert (write (auth_file_fd, &DC->options[0]->port, 4) == 4); - int l = strlen (DC->options[0]->ip); + assert (write (auth_file_fd, &DC->options[ex->flags]->port, 4) == 4); + int l = strlen (DC->options[ex->flags]->ip); assert (write (auth_file_fd, &l, 4) == 4); - assert (write (auth_file_fd, DC->options[0]->ip, l) == l); + assert (write (auth_file_fd, DC->options[ex->flags]->ip, l) == l); assert (write (auth_file_fd, &DC->auth_key_id, 8) == 8); assert (write (auth_file_fd, DC->auth_key, 256) == 256); } void write_auth_file (struct tgl_state *TLS) { + struct write_dc_extra extra; char *name = 0; name = g_strdup_printf("%s/%s", TLS->base_path, "auth"); int auth_file_fd = open (name, O_CREAT | O_RDWR | O_BINARY, 0600); @@ -144,7 +151,10 @@ void write_auth_file (struct tgl_state *TLS) { assert (write (auth_file_fd, &TLS->max_dc_num, 4) == 4); assert (write (auth_file_fd, &TLS->dc_working_num, 4) == 4); - tgl_dc_iterator_ex (TLS, write_dc, &auth_file_fd); + extra.auth_file = auth_file_fd; + extra.flags = TLS->ipv6_enabled ? 1 : 0; + + tgl_dc_iterator_ex (TLS, write_dc, &extra); assert (write (auth_file_fd, &TLS->our_id, 4) == 4); close (auth_file_fd); @@ -166,7 +176,7 @@ void read_dc (struct tgl_state *TLS, int auth_file_fd, int id, unsigned ver) { assert (read (auth_file_fd, &auth_key_id, 8) == 8); assert (read (auth_file_fd, auth_key, 256) == 256); - bl_do_dc_option (TLS, 0, id, "DC", 2, ip, l, port); + bl_do_dc_option (TLS, TLS->ipv6_enabled ? 1: 0, id, "DC", 2, ip, l, port); bl_do_set_auth_key (TLS, id, auth_key); bl_do_dc_signed (TLS, id); debug ("read dc: id=%d", id); @@ -190,11 +200,19 @@ void empty_auth_file (struct tgl_state *TLS) { bl_do_dc_option (TLS, 0, 3, "", 0, TG_SERVER_TEST_3, strlen (TG_SERVER_TEST_3), 443); bl_do_set_working_dc (TLS, TG_SERVER_TEST_DEFAULT); } else { - bl_do_dc_option (TLS, 0, 1, "", 0, TG_SERVER_1, strlen (TG_SERVER_1), 443); - bl_do_dc_option (TLS, 0, 2, "", 0, TG_SERVER_2, strlen (TG_SERVER_2), 443); - bl_do_dc_option (TLS, 0, 3, "", 0, TG_SERVER_3, strlen (TG_SERVER_3), 443); - bl_do_dc_option (TLS, 0, 4, "", 0, TG_SERVER_4, strlen (TG_SERVER_4), 443); - bl_do_dc_option (TLS, 0, 5, "", 0, TG_SERVER_5, strlen (TG_SERVER_5), 443); + if (TLS->ipv6_enabled) { + bl_do_dc_option (TLS, 1, 1, "", 0, TG_SERVER_IPV6_1, strlen (TG_SERVER_IPV6_1), 443); + bl_do_dc_option (TLS, 1, 2, "", 0, TG_SERVER_IPV6_2, strlen (TG_SERVER_IPV6_2), 443); + bl_do_dc_option (TLS, 1, 3, "", 0, TG_SERVER_IPV6_3, strlen (TG_SERVER_IPV6_3), 443); + bl_do_dc_option (TLS, 1, 4, "", 0, TG_SERVER_IPV6_4, strlen (TG_SERVER_IPV6_4), 443); + bl_do_dc_option (TLS, 1, 5, "", 0, TG_SERVER_IPV6_5, strlen (TG_SERVER_IPV6_5), 443); + } else { + bl_do_dc_option (TLS, 0, 1, "", 0, TG_SERVER_1, strlen (TG_SERVER_1), 443); + bl_do_dc_option (TLS, 0, 2, "", 0, TG_SERVER_2, strlen (TG_SERVER_2), 443); + bl_do_dc_option (TLS, 0, 3, "", 0, TG_SERVER_3, strlen (TG_SERVER_3), 443); + bl_do_dc_option (TLS, 0, 4, "", 0, TG_SERVER_4, strlen (TG_SERVER_4), 443); + bl_do_dc_option (TLS, 0, 5, "", 0, TG_SERVER_5, strlen (TG_SERVER_5), 443); + } bl_do_set_working_dc (TLS, TG_SERVER_DEFAULT); } } diff --git a/telegram-purple.c b/telegram-purple.c index 6a74dcb..61d0058 100644 --- a/telegram-purple.c +++ b/telegram-purple.c @@ -567,6 +567,9 @@ static void tgprpl_login (PurpleAccount * acct) { return; } + if (purple_account_get_bool (acct, TGP_KEY_USE_IPV6, FALSE)) + tgl_enable_ipv6(TLS); + if (! tgp_startswith (purple_account_get_username (acct), "+")) { // TRANSLATORS: Please fill in your own prefix! char *cause = g_strdup_printf (_("Unable to sign on as %s: phone number lacks country prefix." @@ -876,6 +879,11 @@ static void tgprpl_init (PurplePlugin *plugin) { TGP_KEY_SEND_READ_NOTIFICATIONS, TGP_DEFAULT_SEND_READ_NOTIFICATIONS); prpl_info.protocol_options = g_list_append (prpl_info.protocol_options, opt); + // IPv6 + opt = purple_account_option_bool_new (_("Use IPv6 for connecting"), + TGP_KEY_USE_IPV6, TGP_DEFAULT_USE_IPV6); + prpl_info.protocol_options = g_list_append (prpl_info.protocol_options, opt); + _telegram_protocol = plugin; debug ("tgprpl_init finished: This is " PACKAGE_VERSION "+g" GIT_COMMIT " on libtgl " TGL_VERSION); } diff --git a/telegram-purple.h b/telegram-purple.h index 480ce48..d554862 100644 --- a/telegram-purple.h +++ b/telegram-purple.h @@ -96,6 +96,9 @@ #define TGP_DEFAULT_CHANNEL_MEMBERS 500 #define TGP_KEY_CHANNEL_MEMBERS "channel-member-count" +#define TGP_DEFAULT_USE_IPV6 FALSE +#define TGP_KEY_USE_IPV6 "ipv6" + #define TGP_KEY_RESET_AUTH "reset-authorization" #define TGP_CHANNEL_HISTORY_LIMIT 500