Author: ArcRiley
Date: 2009-01-07 21:33:49 -0500 (Wed, 07 Jan 2009)
New Revision: 1439

Modified:
   trunk/concordance/src/Core.c
   trunk/concordance/src/Core.h
Log:
splits element from namespace, starting work on sasl session 


Modified: trunk/concordance/src/Core.c
===================================================================
--- trunk/concordance/src/Core.c        2009-01-07 21:01:48 UTC (rev 1438)
+++ trunk/concordance/src/Core.c        2009-01-08 02:33:49 UTC (rev 1439)
@@ -168,6 +168,7 @@
     /* initialize gsasl context
 
        int              gsasl_init               (Gsasl** ctx)
+       Return value: GSASL_OK if successful, otherwise GSASL_MALLOC_ERROR.
     */
     if (gsasl_init(&self->saslCntx) != GSASL_OK) {
       PyErr_SetString(PyExc_MemoryError, "out of memory on gsasl_init");
@@ -351,7 +352,7 @@
       gint sent;
 
     /* make sure the session write buffer is empty first */
-    if (session->buff->len == 0) {
+    if (session->wbuff->len == 0) {
       /* send as much data as the channel will take
 
          GIOStatus      g_io_channel_write_chars (GIOChannel *channel,
@@ -370,7 +371,7 @@
                                                   const gchar *val,
                                                   gssize len);
         */
-        session->buff = g_string_append_len(session->buff, str+sent, len-sent);
+        session->wbuff = g_string_append_len(session->wbuff, str+sent, 
len-sent);
 
         /* add a watch to send more when the channel is ready for it
 
@@ -390,7 +391,7 @@
 
          see above for g_string_append_len prototype
       */
-      session->buff = g_string_append_len(session->buff, str, len);
+      session->wbuff = g_string_append_len(session->wbuff, str, len);
   }
 
   /*
@@ -466,7 +467,7 @@
 
          GString*       g_string_new             (const gchar *init);
       */
-      newSession->buff = g_string_new("");
+      newSession->wbuff = g_string_new("");
 
 
       /* initialize session state */
@@ -487,7 +488,7 @@
                                             (XML_Parser parser,
                                              XML_CharacterDataHandler handler);
       */
-      newSession->pars = XML_ParserCreateNS(NULL, 32); /* 32 == " " */
+      newSession->pars = XML_ParserCreateNS(NULL, 32); /* space is separator*/
       XML_SetUserData(newSession->pars, newSession);
       XML_SetElementHandler(newSession->pars, conCore_xmlStart, 
conCore_xmlEnd);
       XML_SetCharacterDataHandler(newSession->pars, conCore_xmlCharData);
@@ -594,8 +595,8 @@
                                                   gsize *bytes_written,
                                                   GError **error);
     */
-    g_io_channel_write_chars(channel, session->buff->str,
-                             session->buff->len, &sent, NULL);
+    g_io_channel_write_chars(channel, session->wbuff->str,
+                             session->wbuff->len, &sent, NULL);
 
     /* erase what we've just written
 
@@ -603,10 +604,10 @@
                                                   gssize pos,
                                                   gssize len);
     */
-    session->buff = g_string_erase(session->buff, 0, sent);
+    session->wbuff = g_string_erase(session->wbuff, 0, sent);
 
     /* keep this event alive only if there's more in the buffer to write */
-    return (session->buff->len != 0);
+    return (session->wbuff->len != 0);
   }
 
 
@@ -622,9 +623,18 @@
     cdef :                                                                \*/
       conSession*           session = (conSession*) s;
       conCoreObject*        self = session->core;
+      gchar**               element;
       GString*              buff;
       gint                  i;
 
+    /* split name by namespace and element
+
+    gchar**             g_strsplit               (const gchar *string,
+                                                  const gchar *delimiter,
+                                                  gint max_tokens);
+    */
+    element = g_strsplit(name, " ", 2);
+
     /* initialize response buffer */
     buff = g_string_new("");
 
@@ -632,7 +642,7 @@
     for (i = 0; i < session->depth; i++)
       printf("  ");
 
-    printf("<%s", name);
+    printf("<%s xmlns='%s'", element[1], element[0]);
     for (i = 0; atts[i]; i += 2)
       printf(" %s='%s'", atts[i], atts[i + 1]);
     printf(">\n");
@@ -641,7 +651,7 @@
       case 0 : {
         /* only stream element is valid at depth == 0 */
         if (strcmp(name, "http://etherx.jabber.org/streams stream") == 0) {
-          /* send stream:stream and stream:features
+          /* init <stream:stream>, <stream:features>, and <mechanisms>
 
              void       g_string_printf          (GString *string,
                                                   const gchar *format,
@@ -653,15 +663,65 @@
                           " xmlns:stream='http://etherx.jabber.org/streams'"
                           " from='%s' id='%s'>"
                           "<stream:features><mechanisms"
-                          " xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
-                          "<mechanism>DIGEST-MD5</mechanism>"
-                          "</mechanisms></stream:features>",
+                          " xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>",
                           "selket.apogean.org", "concordance-1");
+
+          /* add a <mechanism> element for each supported SASL mechanism
+
+             int        gsasl_server_support_p   (Gsasl* ctx,
+                                                  const char* name);
+             GString*   g_string_append          (GString *string,
+                                                  const gchar *val);
+          */
+          if (gsasl_server_support_p(self->saslCntx, "DIGEST-MD5"))
+            buff = g_string_append(buff, "<mechanism>DIGEST-MD5</mechanism>");
+          if (gsasl_server_support_p(self->saslCntx, "PLAIN"))
+            buff = g_string_append(buff, "<mechanism>PLAIN</mechanism>");
+
+          /* close <mechanisms> and <stream:features>, then send buffer */
+          buff = g_string_append(buff, "</mechanisms></stream:features>");
           conCore_sessionSend(session, buff->str, buff->len);
         }
         /* should disconnect here, something other than session was sent */
       }
       case 1 : {
+        if (strcmp(name, "urn:ietf:params:xml:ns:xmpp-sasl auth") == 0) {
+          gint mech = 0;
+          for (i = 0; atts[i]; i += 2)
+            if (strcmp(atts[i], "mechanism") == 0 &&
+                gsasl_server_support_p(self->saslCntx, atts[i+1]))
+              mech = i+1;
+
+          if (mech) {
+            /* initiate server session
+
+               int      gsasl_server_start       (Gsasl* ctx,
+                                                  const char* mech,
+                                                  Gsasl_session** sctx);
+            */
+            printf("------ init %s mechanism\n", atts[mech]);
+            gsasl_server_start(self->saslCntx, atts[mech], &session->sctx);
+            /* int      gsasl_nonce              (char* data,
+                                                  size_t datalen)
+            /* gchar*   g_base64_encode          (const guchar *data,
+                                                  gsize len);
+            */
+
+
+            g_string_printf(buff,
+                            "<challenge"
+                            " xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
+                            "%s</challenge>", "");
+          }
+          else {
+            g_string_append(buff,
+                            "<failure 
xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
+                            "<incorrect-encoding/></failure></stream:stream>");
+            /* need to close the connection now */
+          }
+          conCore_sessionSend(session, buff->str, buff->len);
+        }
+
       }
     }
 
@@ -674,6 +734,12 @@
                                                   gboolean free_segment);
     */
     g_string_free(buff, TRUE);
+
+    /* free the element string array
+
+       void             g_strfreev               (gchar **str_array);
+    */
+    g_strfreev(element);
   }
 
 
@@ -682,12 +748,29 @@
     cdef :                                                                \*/
       conSession*           session = (conSession*) s;
       conCoreObject*        self = session->core;
+      gchar**               element;
       gint                  i;
 
+    /* split name by namespace and element
+
+    gchar**             g_strsplit               (const gchar *string,
+                                                  const gchar *delimiter,
+                                                  gint max_tokens);
+    */
+    element = g_strsplit(name, " ", 2);
+
+    /* decrease XML depth */
     session->depth--;
+
     for (i = 0; i < session->depth; i++)
       printf("  ");
-    printf("</%s>\n", name);
+    printf("</%s>\n", element[1]);
+
+    /* free the element string array
+
+       void             g_strfreev               (gchar **str_array);
+    */
+    g_strfreev(element);    
   }
 
 

Modified: trunk/concordance/src/Core.h
===================================================================
--- trunk/concordance/src/Core.h        2009-01-07 21:01:48 UTC (rev 1438)
+++ trunk/concordance/src/Core.h        2009-01-08 02:33:49 UTC (rev 1439)
@@ -71,10 +71,11 @@
   PyObject*        self;               /* Session object for this or NULL */
   conCoreObject*   core;               /* Core this session belongs to */
   GIOChannel*      chan;               /* Glib IO channel for this session */
+  Gsasl_session*   sctx;               /* gsasl session context */
   XML_Parser       pars;               /* expat parser for this session */
   gchar            host[256];          /* verified hostname or NULL */
-  GString*         buff;               /* write buffer */
   gboolean         tls;                /* flag for whether tls is enabled */
+  GString*         wbuff;              /* write buffer */
   gint             state;              /* current session state */
   gint             depth;              /* parser element depth */
 } conSession;

_______________________________________________
PySoy-SVN mailing list
PySoy-SVN@pysoy.org
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to