comparison mcabber/mcabber/xmpp.c @ 1747:c4f67e2e675f

Randomize reconnection delay Conforming to rfc3920bis, 4.5: "The time that expires before an entity first seeks to reconnect MUST be randomized" (We should still increase the delay if the reconnection keeps failing.)
author Mikael Berthe <mikael@lilotux.net>
date Thu, 11 Mar 2010 22:27:36 +0100
parents b7574de9ae4b
children e4378fbab5d7
comparison
equal deleted inserted replaced
1746:b7574de9ae4b 1747:c4f67e2e675f
880 880
881 static void _try_to_reconnect(void) 881 static void _try_to_reconnect(void)
882 { 882 {
883 xmpp_disconnect(); 883 xmpp_disconnect();
884 if (AutoConnection) 884 if (AutoConnection)
885 g_timeout_add_seconds(RECONNECTION_TIMEOUT, xmpp_reconnect, NULL); 885 g_timeout_add_seconds(RECONNECTION_TIMEOUT + (random() % 90L),
886 xmpp_reconnect, NULL);
886 } 887 }
887 888
888 static void connection_open_cb(LmConnection *connection, gboolean success, 889 static void connection_open_cb(LmConnection *connection, gboolean success,
889 gpointer user_data) 890 gpointer user_data)
890 { 891 {
1705 scr_LogPrint(LPRINT_NORMAL|LPRINT_DEBUG, "Connecting to server: %s", 1706 scr_LogPrint(LPRINT_NORMAL|LPRINT_DEBUG, "Connecting to server: %s",
1706 servername ? servername : "..."); 1707 servername ? servername : "...");
1707 if (!resource) 1708 if (!resource)
1708 resource = resource_prefix; 1709 resource = resource_prefix;
1709 1710
1711 // Initialize pseudo-random seed
1712 srandom(time(NULL));
1713
1710 if (!settings_opt_get("disable_random_resource")) { 1714 if (!settings_opt_get("disable_random_resource")) {
1711 #if HAVE_ARC4RANDOM 1715 #if HAVE_ARC4RANDOM
1712 dynresource = g_strdup_printf("%s.%08x", resource, arc4random()); 1716 dynresource = g_strdup_printf("%s.%08x", resource, arc4random());
1713 #else 1717 #else
1714 unsigned int tab[2]; 1718 unsigned int tab[2];
1715 srandom(time(NULL));
1716 tab[0] = (unsigned int) (0xffff * (random() / (RAND_MAX + 1.0))); 1719 tab[0] = (unsigned int) (0xffff * (random() / (RAND_MAX + 1.0)));
1717 tab[1] = (unsigned int) (0xffff * (random() / (RAND_MAX + 1.0))); 1720 tab[1] = (unsigned int) (0xffff * (random() / (RAND_MAX + 1.0)));
1718 dynresource = g_strdup_printf("%s.%04x%04x", resource, tab[0], tab[1]); 1721 dynresource = g_strdup_printf("%s.%04x%04x", resource, tab[0], tab[1]);
1719 #endif 1722 #endif
1720 resource = dynresource; 1723 resource = dynresource;