/soc/2012/sanket/statscollector-2.x.y: 87c961cbc230: Add unique ...
Sanket Agarwal
sanket at soc.pidgin.im
Tue Jul 10 00:36:25 EDT 2012
Changeset: 87c961cbc23049b02be46b0119fb26734d55084f
Author: Sanket Agarwal <sanket at soc.pidgin.im>
Date: 2012-05-30 18:59 +0000
Branch: soc.2012.statscollector
URL: http://hg.pidgin.im/soc/2012/sanket/statscollector-2.x.y/rev/87c961cbc230
Description:
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!
diffstat:
libpurple/plugins/statscollector.c | 56 +++++++++++++++++++++++++++++++++----
1 files changed, 50 insertions(+), 6 deletions(-)
diffs (127 lines):
diff --git a/libpurple/plugins/statscollector.c b/libpurple/plugins/statscollector.c
--- a/libpurple/plugins/statscollector.c
+++ b/libpurple/plugins/statscollector.c
@@ -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 @@
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 @@
}
+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,14 +724,20 @@
/* 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);
@@ -748,7 +779,7 @@
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 @@
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 @@
"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 @@
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