soc.2012.statscollector: 347b6e5e: Add unique 16byte id for each installati...

sanket at soc.pidgin.im sanket at soc.pidgin.im
Wed May 30 14:17:58 EDT 2012


----------------------------------------------------------------------
Revision: 347b6e5e35974d211b9a7f3e09ef5c3e0bf66a70
Parent:   56b9ea714317aee75ea9e8920fa09ac1aada45b1
Author:   sanket at soc.pidgin.im
Date:     05/30/12 14:59:28
Branch:   im.pidgin.soc.2012.statscollector
URL: http://d.pidgin.im/viewmtn/revision/info/347b6e5e35974d211b9a7f3e09ef5c3e0bf66a70

Changelog: 

Add unique 16byte id for each installation; remove timeout handles on exit

In order to identify that same installations are sending information twice
I have kept a unique id (randomly generated) which each client can generate
and while sending stats this id is used. Ofcourse it is not even close to
secure, but on the way this unique id is exchanged we can develop a more
complex protocol.

Secondly, due to some purple_timeout_add_seconds here and there, even after
unloading there were cases of stats being sent continually. I have removed
these handles in plugin unload!

Changes against parent 56b9ea714317aee75ea9e8920fa09ac1aada45b1

  patched  libpurple/plugins/statscollector.c

-------------- next part --------------
============================================================
--- libpurple/plugins/statscollector.c	029288b9d8e7a425fe84bfcc72750489ee665736
+++ libpurple/plugins/statscollector.c	eada7d865da270380cd002f9c2a6211bda98f2a6
@@ -30,7 +30,7 @@
 #define RESEND_SEC 40
 
 /* Sending URL */
-#define SEND_URL "http://localhost:8000/collectstats/collect"
+#define SEND_URL "http://localhost:8000/collectstats/collect/"
 
 /* POSIX compliance is an issue that I have looked into some detail now
  * It seems like presence of unistd.h and _POSIX_VERSION being defined
@@ -50,7 +50,7 @@ GHashTable *stats_acc_ht, *stats_plugins
 
 xmlnode *root_stats, *cpuinfo_xml, *ui_info;
 GHashTable *stats_acc_ht, *stats_plugins_ht, *stats_uis_ht;
-int save_timer = 0;
+int save_timer = 0, send_handle = 0;
 
 /* Types of Operating Systems */
 enum OS_TYPES {WINDOWS, APPLE, UNIX};
@@ -69,6 +69,31 @@ get_time_sec(){
 
 }
 
+static char *
+rand_md5(){
+
+  char e[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
+  char *rand_str;
+  int i;
+  GTimeVal time;
+
+  g_get_current_time(&time);
+
+  /* Creates a random MD5 (16bytes) string */
+  rand_str = (char *)g_new0(char, 16*2);
+
+  /* Initialize the seed */
+  srand(time.tv_sec * time.tv_usec);
+
+  /* Randomly fill it */
+  for(i=0;i<32;i++){
+    rand_str[i] = e[rand()%16];
+  }
+
+  return rand_str;
+
+}
+
 static void
 save_xml(){
 
@@ -699,15 +724,21 @@ send_stats()
   /* Read from stats.xml and send it over the wire */
 
   PurpleUtilFetchUrlData *urldata;
-  gchar *host, *path, *request, *filename, *postdata, *url= SEND_URL;
+  gchar *host, *path, *request, *filename, *url= SEND_URL;
+  gchar *pd_xml, *pd_id, *postdata;
   GError *error;
   int port;
   gsize length;
 
   /* Load the xml as post-data */
   filename = g_build_filename(purple_user_dir(), "stats.xml", NULL);
-  g_file_get_contents(filename, &postdata, &length, &error);
+  g_file_get_contents(filename, &pd_xml, &length, &error);
 
+  /* Get the UID to send the data with */
+  pd_id = purple_prefs_get_string("/plugins/core/statscollector/send-key");
+
+  postdata = g_strdup_printf("xml=%s;id=%s", pd_xml, pd_id);
+
   purple_url_parse(url, &host, &port, &path, NULL, NULL);
 
   request = g_strdup_printf(\
@@ -748,7 +779,7 @@ schedule_send(){
   purple_debug_info("STATS SEND", "scheduling sending from %ld at %ld \
                     for %ld",last_saved, curr_epoch, time_to_send);
 
-  purple_timeout_add_seconds(time_to_send, send_stats, NULL);
+  send_handle = purple_timeout_add_seconds(time_to_send, send_stats, NULL);
 }
 
 
@@ -887,6 +918,16 @@ plugin_load(PurplePlugin *plugin) {
  return TRUE;
 }
 
+static gboolean
+plugin_unload(PurplePlugin *plugin){
+
+  /* Remove any timers which might be executing in a loop! */
+  if(send_handle) purple_timeout_remove(send_handle);
+
+  return TRUE;
+
+}
+
 static PurplePluginInfo info =
 {
 	PURPLE_PLUGIN_MAGIC,
@@ -909,7 +950,7 @@ static PurplePluginInfo info =
 	"Sanket Agarwal <sanket at sanketagarwal.com>",    /**< author */
 	PURPLE_WEBSITE,                                 /**< homepage */
 	plugin_load,                                    /**< load */
-	NULL,                                           /**< unload */
+	plugin_unload,                                  /**< unload */
 	NULL,                                           /**< destroy */
 	NULL,                                           /**< ui_info */
 	NULL,                                           /**< extra_info */
@@ -926,12 +967,15 @@ init_plugin(PurplePlugin *plugin)
 static void
 init_plugin(PurplePlugin *plugin)
 {
+
   /* Set up the preferences for the plugin */
   purple_prefs_add_none("/plugins/core/statscollector");
 
   /* Add public key information */
   purple_prefs_add_int("/plugins/core/statscollector/last-sent", 0);
 
+  /* A random 16 byte string for identifying unique machines */
+  purple_prefs_add_string("/plugins/core/statscollector/send-key", rand_md5());
 }
 
 PURPLE_INIT_PLUGIN(statscollector, init_plugin, info)


More information about the Commits mailing list