Some sessions management code.
authorNicolas Cornu <nicolas.cornu@ensi-bourges.fr>
Wed, 16 Jun 2010 20:16:45 +0200
changeset 21 147f131382dd
parent 20 72e53665328e
child 22 a089f2f49e9f
Some sessions management code. Fix a spelling mistake (transfer not transfert)
CMakeLists.txt
jingle-filetransfer/CMakeLists.txt
jingle-filetransfer/filetransfer.c
jingle-filetransfer/filetransfer.h
jingle-filetransfert/CMakeLists.txt
jingle-filetransfert/filetransfert.c
jingle-filetransfert/filetransfert.h
jingle/CMakeLists.txt
jingle/check.c
jingle/jingle.h
jingle/sessions.c
jingle/sessions.h
--- a/CMakeLists.txt	Sun Jun 13 02:25:51 2010 +0200
+++ b/CMakeLists.txt	Wed Jun 16 20:16:45 2010 +0200
@@ -38,7 +38,7 @@
 
 ## Target definitions
 add_subdirectory(jingle)
-add_subdirectory(jingle-filetransfert)
+add_subdirectory(jingle-filetransfer)
 
 ## Packaging information
 set(CPACK_PACKAGE_NAME mcabber-jingle)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jingle-filetransfer/CMakeLists.txt	Wed Jun 16 20:16:45 2010 +0200
@@ -0,0 +1,4 @@
+add_library(jingle-filetransfer MODULE filetransfer.c)
+set_target_properties(jingle-filetransfer PROPERTIES COMPILE_FLAGS "-Wall")
+include_directories(${CMAKE_SOURCE_DIR})
+install(TARGETS jingle-filetransfer DESTINATION lib/mcabber)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jingle-filetransfer/filetransfer.c	Wed Jun 16 20:16:45 2010 +0200
@@ -0,0 +1,121 @@
+/*
+ * filetransfer.c
+ *
+ * Copyrigth (C) 2010 Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include <mcabber/modules.h>
+#include <mcabber/utils.h>
+#include <mcabber/xmpp_helper.h>
+
+#include <jingle/jingle.h>
+#include <jingle/check.h>
+#include <jingle/register.h>
+
+#include "filetransfer.h"
+
+
+gconstpointer jingle_ft_check(JingleContent *cn, GError **err, gpointer *data);
+static void jingle_ft_init(void);
+static void jingle_ft_uninit(void);
+
+
+const gchar *deps[] = { "jingle", NULL };
+
+JingleAppFuncs funcs = {jingle_ft_check, NULL};
+
+module_info_t info_jingle_filetransfer = {
+  .branch          = MCABBER_BRANCH,
+  .api             = MCABBER_API_VERSION,
+  .version         = PROJECT_VERSION,
+  .description     = "Jingle File Transfer (XEP-0234)\n",
+  .requires        = deps,
+  .init            = jingle_ft_init,
+  .uninit          = jingle_ft_uninit,
+  .next            = NULL,
+};
+
+
+gconstpointer jingle_ft_check(JingleContent *cn, GError **err, gpointer *data)
+{
+  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 transfer 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)
+{
+  jingle_register_app(NS_JINGLE_APP_FT, &funcs, NULL);
+  xmpp_add_feature(NS_JINGLE_APP_FT);
+}
+
+static void jingle_ft_uninit(void)
+{
+  xmpp_del_feature(NS_JINGLE_APP_FT);
+  jingle_unregister_app(NS_JINGLE_APP_FT);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jingle-filetransfer/filetransfer.h	Wed Jun 16 20:16:45 2010 +0200
@@ -0,0 +1,23 @@
+#ifndef __JINGLEFT_H__
+#define __JINGLEFT_H__ 1
+
+#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-filetransfert/CMakeLists.txt	Sun Jun 13 02:25:51 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-add_library(jingle-filetransfert MODULE filetransfert.c)
-set_target_properties(jingle-filetransfert PROPERTIES COMPILE_FLAGS "-Wall")
-include_directories(${CMAKE_SOURCE_DIR})
-install(TARGETS jingle-filetransfert DESTINATION lib/mcabber)
--- a/jingle-filetransfert/filetransfert.c	Sun Jun 13 02:25:51 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,121 +0,0 @@
-/*
- * filetransfert.c
- *
- * Copyrigth (C) 2010 Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or (at
- * your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
- */
-
-#include "config.h"
-
-#include <glib.h>
-
-#include <mcabber/modules.h>
-#include <mcabber/utils.h>
-#include <mcabber/xmpp_helper.h>
-
-#include <jingle/jingle.h>
-#include <jingle/check.h>
-#include <jingle/register.h>
-
-#include "filetransfert.h"
-
-
-gconstpointer jingle_ft_check(JingleContent *cn, GError **err, gpointer *data);
-static void jingle_ft_init(void);
-static void jingle_ft_uninit(void);
-
-
-const gchar *deps[] = { "jingle", NULL };
-
-JingleAppFuncs funcs = {jingle_ft_check, NULL};
-
-module_info_t info_jingle_filetransfert = {
-  .branch          = MCABBER_BRANCH,
-  .api             = MCABBER_API_VERSION,
-  .version         = PROJECT_VERSION,
-  .description     = "Jingle File Transfert (XEP-0234)\n",
-  .requires        = deps,
-  .init            = jingle_ft_init,
-  .uninit          = jingle_ft_uninit,
-  .next            = NULL,
-};
-
-
-gconstpointer jingle_ft_check(JingleContent *cn, GError **err, gpointer *data)
-{
-  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)
-{
-  jingle_register_app(NS_JINGLE_APP_FT, &funcs, NULL);
-  xmpp_add_feature(NS_JINGLE_APP_FT);
-}
-
-static void jingle_ft_uninit(void)
-{
-  xmpp_del_feature(NS_JINGLE_APP_FT);
-  jingle_unregister_app(NS_JINGLE_APP_FT);
-}
--- a/jingle-filetransfert/filetransfert.h	Sun Jun 13 02:25:51 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-#ifndef __JINGLEFT_H__
-#define __JINGLEFT_H__ 1
-
-#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/CMakeLists.txt	Sun Jun 13 02:25:51 2010 +0200
+++ b/jingle/CMakeLists.txt	Wed Jun 16 20:16:45 2010 +0200
@@ -1,5 +1,5 @@
-add_library(jingle MODULE jingle.c check.c action-handlers.c register.c)
-set_target_properties(jingle PROPERTIES COMPILE_FLAGS "-Wall")
+add_library(jingle MODULE jingle.c check.c action-handlers.c register.c sessions.c)
+set_target_properties(jingle PROPERTIES COMPILE_FLAGS "")
 include_directories(${LM_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR})
 target_link_libraries(jingle ${LM_LIBRARIES})
 install(TARGETS jingle DESTINATION lib/mcabber)
--- a/jingle/check.c	Sun Jun 13 02:25:51 2010 +0200
+++ b/jingle/check.c	Wed Jun 16 20:16:45 2010 +0200
@@ -20,8 +20,11 @@
  */
 
 #include <glib.h>
+
 #include <loudmouth/loudmouth.h>
 
+#include <mcabber/utils.h>
+
 #include <jingle/check.h>
 #include <jingle/jingle.h>
 
@@ -66,6 +69,12 @@
     return FALSE;
   }
 
+  if (!check_jid_syntax(jn->initiator)) {
+    g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_BADVALUE,
+                "the initiator attribute in invalid (not a jid)");
+    return FALSE;
+  }
+
   jn->action = jingle_action_from_str(actionstr);
   if (jn->action == JINGLE_UNKNOWN_ACTION) {
     g_set_error(err, JINGLE_CHECK_ERROR, JINGLE_CHECK_ERROR_BADVALUE,
--- a/jingle/jingle.h	Sun Jun 13 02:25:51 2010 +0200
+++ b/jingle/jingle.h	Wed Jun 16 20:16:45 2010 +0200
@@ -63,12 +63,15 @@
   /* Random session identifier generated by the initator. */
   const gchar *sid;
 
-  /* Doubly-linked list of JingleContent. */
+  /* Linked list of JingleContent. */
   GSList *content;
 
 } JingleNode;
 
 typedef struct {
+  /* pointer to the <content> element */
+  LmMessageNode *node;
+
   /* which party originally generated the content type.
    * the defined values are "initiator" and "responder"
    * (where the default is "initiator"). required. */
@@ -91,12 +94,12 @@
   /* each content element (must) contain one description
    * child element that specifies a desired application.
    * the content of this node is app specific. */
-  LmMessageNode *description;
+  gconstpointer *description;
 
   /* each content element (must) contain one transport
    * child element that specifies a potential transport
    * method */
-  LmMessageNode *transport;
+  gconstpointer *transport;
 
 } JingleContent;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jingle/sessions.c	Wed Jun 16 20:16:45 2010 +0200
@@ -0,0 +1,89 @@
+/*
+ * sessions.c
+ *
+ * Copyrigth (C) 2010 Nicolas Cornu <nicolas.cornu@ensi-bourges.fr>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#include <glib.h>
+
+
+#include <jingle/jingle.h>
+#include <jingle/sessions.h>
+
+
+static GSList *sessions;
+
+
+/**
+ * Create a new session and insert it in the linked list.
+ */
+JingleSession *session_new(JingleNode *jn)
+{
+  JingleSession *js = g_new0(JingleSession, 1);
+  js->sid = g_strdup(jn->sid);
+  js->initiator = g_strdup(jn->initiator);
+  js->from = lm_message_node_get_attribute(lm_message_get_node(jn->message),
+                                           "from");
+  if (!js->from) {
+    return NULL;
+  }
+  js->from = g_strdup(js->from);
+  
+
+  sessions = g_slist_append(sessions, js);
+}
+
+JingleSession *session_find(const gchar *sid, const gchar *from)
+{
+  GSList *el;
+  JingleSession *js;
+  for (el = sessions; el; el = el->next) {
+    js = (JingleSession*) el->data;
+    if (g_strcmp0(js->sid, sid) && g_strcmp0(js->from, from)) {
+      return js;
+	}
+  }
+  return NULL;
+}
+
+/**
+ * Remove a session from the linked list and free it.
+ */
+void session_delete(JingleSession *sess)
+{
+  session_remove(sess);
+  session_free(sess);
+}
+
+/**
+ * Remove a session from the linked list.
+ */
+void session_remove(JingleSession *sess)
+{
+  sessions = g_slist_remove(sessions, sess);
+}
+
+/**
+ * Free a session.
+ */
+void session_free(JingleSession *sess)
+{
+  g_free(sess->sid);
+  g_free(sess->from);
+  g_free(sess);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jingle/sessions.h	Wed Jun 16 20:16:45 2010 +0200
@@ -0,0 +1,27 @@
+#ifndef __JINGLE_SESSIONS_H__
+#define __JINGLE_SESSIONS_H__ 1
+
+#include <glib.h>
+
+
+typedef enum {
+  JINGLE_SESSION_
+} JingleStatus;
+
+typedef struct {
+  JingleStatus  status;
+  const gchar *sid;
+  const gchar *from;
+  GSList *content;
+} JingleSession;
+
+
+JingleSession *session_new(JingleNode *jn,
+                           gconstpointer *app,
+                           gconstpointer *transport);
+JingleSession *session_find();
+void session_delete(JingleSession *sess);
+void session_remove(JingleSession *sess);
+void session_free(JingleSession *sess);
+
+#endif