Add a function to check a file transfert request.
authorNicolas Cornu <nicolas.cornu@ensi-bourges.fr>
Sun, 13 Jun 2010 02:25:51 +0200
changeset 20 72e53665328e
parent 19 60a10ab26723
child 21 147f131382dd
Add a function to check a file transfert request.
jingle-filetransfert/filetransfert.c
jingle-filetransfert/filetransfert.h
jingle/check.c
jingle/jingle.h
jingle/register.h
--- a/jingle-filetransfert/filetransfert.c	Fri Jun 11 01:56:34 2010 +0200
+++ b/jingle-filetransfert/filetransfert.c	Sun Jun 13 02:25:51 2010 +0200
@@ -24,6 +24,7 @@
 #include <glib.h>
 
 #include <mcabber/modules.h>
+#include <mcabber/utils.h>
 #include <mcabber/xmpp_helper.h>
 
 #include <jingle/jingle.h>
@@ -33,7 +34,7 @@
 #include "filetransfert.h"
 
 
-void jingle_ft_check();
+gconstpointer jingle_ft_check(JingleContent *cn, GError **err, gpointer *data);
 static void jingle_ft_init(void);
 static void jingle_ft_uninit(void);
 
@@ -54,9 +55,57 @@
 };
 
 
-void jingle_ft_check(JingleContentNode *cn, GError **err, gpointer *data)
+gconstpointer jingle_ft_check(JingleContent *cn, GError **err, gpointer *data)
 {
-  return;
+  JingleFT *ft = NULL;
+  LmMessageNode *node;
+  const gchar *datestr, *sizestr;
+
+  node = lm_message_node_get_child(cn->description, "offer");
+  if (!node) {
+    g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_MISSING,
+                "the offer element is missing");
+    return NULL;
+  }
+
+  node = lm_message_node_get_child(cn->description, "file");
+  if (!node) {
+    g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_MISSING,
+                "the file element is missing");
+    return NULL;
+  }
+
+  if (g_strcmp0(lm_message_node_get_attribute(node, "xmlns"), NS_SI_FT)) {
+    g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_MISSING,
+                "the file transfert offer has an invalid/unsupported namespace");
+    return NULL;
+  }
+
+  ft = g_new0(JingleFT, 1);
+  datestr  = lm_message_node_get_attribute(node, "date");
+  ft->hash = lm_message_node_get_attribute(node, "hash");
+  ft->name = lm_message_node_get_attribute(node, "name");
+  sizestr  = lm_message_node_get_attribute(node, "size");
+  
+  if (!ft->name || !sizestr) {
+    g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_MISSING,
+                "an attribute of the file element is missing");
+    g_free(ft);
+    return NULL;
+  }
+  
+  ft->date = from_iso8601(datestr, 1);
+  ft->size = g_ascii_strtoll(sizestr, NULL, 10);
+
+  // the size attribute is a xs:integer an therefore can be negative.
+  if (ft->size < 0) {
+    g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_BADVALUE,
+                "the offered file has a negative size");
+    g_free(ft);
+    return NULL;
+  }
+
+  return (gconstpointer) ft;
 }
 
 static void jingle_ft_init(void)
--- a/jingle-filetransfert/filetransfert.h	Fri Jun 11 01:56:34 2010 +0200
+++ b/jingle-filetransfert/filetransfert.h	Sun Jun 13 02:25:51 2010 +0200
@@ -3,5 +3,21 @@
 
 #define NS_JINGLE_APP_FT      "urn:xmpp:jingle:apps:file-transfer:1"
 #define NS_JINGLE_APP_FT_INFO "urn:xmpp:jingle:apps:file-transfer:info:1"
+#define NS_SI_FT              "http://jabber.org/protocol/si/profile/file-transfer"
+
+typedef struct {
+  /* the last modification of the file, optional */
+  time_t date;
+
+  /* MD5 hash of the file, optional */
+  const gchar *hash;
+
+  /* the name of the file that the sender wishes to send */
+  const gchar *name;
+
+  /* the size, in bytes, of the data to be sent */
+  gint64 size;
+
+} JingleFT;
 
 #endif
--- a/jingle/check.c	Fri Jun 11 01:56:34 2010 +0200
+++ b/jingle/check.c	Sun Jun 13 02:25:51 2010 +0200
@@ -26,7 +26,7 @@
 #include <jingle/jingle.h>
 
 
-static JingleContentNode *check_content(LmMessageNode *node, GError **err);
+static JingleContent *check_content(LmMessageNode *node, GError **err);
 gint index_in_array(const gchar *str, const gchar **array);
 
 
@@ -90,9 +90,9 @@
   return TRUE;
 }
 
-static JingleContentNode *check_content(LmMessageNode *node, GError **err)
+static JingleContent *check_content(LmMessageNode *node, GError **err)
 {
-  JingleContentNode *cn = g_new0(JingleContentNode, 1);
+  JingleContent *cn = g_new0(JingleContent, 1);
   const gchar *creatorstr, *sendersstr;
   gint tmp, tmp2;
 
@@ -139,7 +139,7 @@
 gboolean check_contents(JingleNode *jn, GError **err)
 {
   LmMessageNode *child = NULL;
-  JingleContentNode *cn;
+  JingleContent *cn;
 
   for (child = jn->node->children; child; child = child->next) {
     if (!g_strcmp0(child->name, "content")) {
--- a/jingle/jingle.h	Fri Jun 11 01:56:34 2010 +0200
+++ b/jingle/jingle.h	Sun Jun 13 02:25:51 2010 +0200
@@ -63,7 +63,7 @@
   /* Random session identifier generated by the initator. */
   const gchar *sid;
 
-  /* Doubly-linked list of JingleContentNode. */
+  /* Doubly-linked list of JingleContent. */
   GSList *content;
 
 } JingleNode;
@@ -98,7 +98,7 @@
    * method */
   LmMessageNode *transport;
 
-} JingleContentNode;
+} JingleContent;
 
 struct JingleActionList {
   const gchar  *name;
--- a/jingle/register.h	Fri Jun 11 01:56:34 2010 +0200
+++ b/jingle/register.h	Sun Jun 13 02:25:51 2010 +0200
@@ -8,13 +8,13 @@
 #define NS_JINGLE_TRANSPORT_PREFIX "urn:xmpp:jingle:transport:"
 
 
-typedef void (*JingleAppCheck) (JingleContentNode *cn, GError **err, gpointer *data);
-typedef void (*JingleAppHandle) (JingleNode *jn, JingleContentNode *cn, gpointer *data);
-typedef void (*JingleTransportCheck) (JingleContentNode *cn, GError **err, gpointer *data);
-typedef void (*JingleTransportHandle) (JingleNode *jn, JingleContentNode *cn, gpointer *data);
+typedef gconstpointer (*JingleAppCheck) (JingleContent *cn, GError **err, gpointer *data);
+typedef void (*JingleAppHandle) (JingleNode *jn, JingleContent *cn, gpointer *data);
+typedef gconstpointer (*JingleTransportCheck) (JingleContent *cn, GError **err, gpointer *data);
+typedef void (*JingleTransportHandle) (JingleNode *jn, JingleContent *cn, gpointer *data);
 
 typedef struct {
-  /* check if the description of a JingleContentNode is correct */
+  /* check if the description of a JingleContent is correct */
   JingleAppCheck  check;
 
   /* */
@@ -23,7 +23,7 @@
 } JingleAppFuncs;
 
 typedef struct {
-  /* check if the transport of a JingleContentNode is correct */
+  /* check if the transport of a JingleContent is correct */
   JingleAppCheck  check;
 
   /* */