Updated pkcs1 plugin to the new builder API
[strongswan.git] / src / libfast / session.c
index 519187e..5fd5d98 100644 (file)
@@ -11,8 +11,6 @@
  * 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.
- *
- * $Id$
  */
 
 #define _GNU_SOURCE
@@ -24,7 +22,6 @@
 #include <stdio.h>
 
 #include <utils/linked_list.h>
-#include <utils/randomizer.h>
 
 typedef struct private_session_t private_session_t;
 
@@ -37,22 +34,22 @@ struct private_session_t {
         * public functions
         */
        session_t public;
-       
+
        /**
         * session ID
         */
        char *sid;
-       
+
        /**
         * list of controller instances controller_t
         */
        linked_list_t *controllers;
-       
+
        /**
         * list of filter instances filter_t
         */
        linked_list_t *filters;
-       
+
        /**
         * user defined session context
         */
@@ -82,33 +79,37 @@ static void create_sid(private_session_t *this, request_t *request)
 {
        char buf[16];
        chunk_t chunk = chunk_from_buf(buf);
-       randomizer_t *randomizer = randomizer_create();
-       
-       randomizer->get_pseudo_random_bytes(randomizer, sizeof(buf), buf);
-       this->sid = chunk_to_hex(chunk, FALSE);
-       request->add_cookie(request, "SID", this->sid);
-       randomizer->destroy(randomizer);
+       rng_t *rng;
+
+       rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
+       if (rng)
+       {
+               rng->get_bytes(rng, sizeof(buf), buf);
+               this->sid = chunk_to_hex(chunk, NULL, FALSE).ptr;
+               request->add_cookie(request, "SID", this->sid);
+               rng->destroy(rng);
+       }
 }
 
 /**
  * run all registered filters
  */
-static bool run_filter(private_session_t *this, request_t *request,
-                                          controller_t *controller)
+static bool run_filter(private_session_t *this, request_t *request, char *p0,
+                                          char *p1, char *p2, char *p3, char *p4, char *p5)
 {
-       iterator_t *iterator;
+       enumerator_t *enumerator;
        filter_t *filter;
-       
-       iterator = this->filters->create_iterator(this->filters, TRUE);
-       while (iterator->iterate(iterator, (void**)&filter))
+
+       enumerator = this->filters->create_enumerator(this->filters);
+       while (enumerator->enumerate(enumerator, &filter))
        {
-               if (!filter->run(filter, request, controller))
+               if (!filter->run(filter, request, p0, p1, p2, p3, p4, p5))
                {
-                       iterator->destroy(iterator);
+                       enumerator->destroy(enumerator);
                        return FALSE;
                }
        }
-       iterator->destroy(iterator);
+       enumerator->destroy(enumerator);
        return TRUE;
 }
 
@@ -118,44 +119,49 @@ static bool run_filter(private_session_t *this, request_t *request,
 static void process(private_session_t *this, request_t *request)
 {
        char *pos, *start, *param[6] = {NULL, NULL, NULL, NULL, NULL, NULL};
-       iterator_t *iterator;
+       enumerator_t *enumerator;
        bool handled = FALSE;
        controller_t *current;
        int i = 0;
-       
+
        if (this->sid == NULL)
        {
                create_sid(this, request);
        }
-       
+
        start = request->get_path(request);
        if (start)
        {
-               if (*start == '/') start++;
+               if (*start == '/')
+               {
+                       start++;
+               }
                while ((pos = strchr(start, '/')) != NULL && i < 5)
                {
-                       param[i++] = strndup(start, pos - start);
+                       param[i++] = strndupa(start, pos - start);
                        start = pos + 1;
                }
-               param[i] = strdup(start);
-               iterator = this->controllers->create_iterator(this->controllers, TRUE);
-               while (iterator->iterate(iterator, (void**)&current))
+               param[i] = strdupa(start);
+
+               if (run_filter(this, request, param[0], param[1], param[2], param[3],
+                                               param[4], param[5]))
                {
-                       if (streq(current->get_name(current), param[0]))
-                       {       
-                               if (run_filter(this, request, current))
+                       enumerator = this->controllers->create_enumerator(this->controllers);
+                       while (enumerator->enumerate(enumerator, &current))
+                       {
+                               if (streq(current->get_name(current), param[0]))
                                {
                                        current->handle(current, request, param[1], param[2],
                                                                        param[3], param[4], param[5]);
                                        handled = TRUE;
+                                       break;
                                }
-                               break;
                        }
+                       enumerator->destroy(enumerator);
                }
-               iterator->destroy(iterator);
-               for (i = 0; i < 6; i++)
+               else
                {
-                       free(param[i]);
+                       handled = TRUE;
                }
        }
        if (!handled)
@@ -183,7 +189,7 @@ static void destroy(private_session_t *this)
 {
        this->controllers->destroy_offset(this->controllers, offsetof(controller_t, destroy));
        this->filters->destroy_offset(this->filters, offsetof(filter_t, destroy));
-       if (this->context) this->context->destroy(this->context);
+       DESTROY_IF(this->context);
        free(this->sid);
        free(this);
 }
@@ -205,7 +211,7 @@ session_t *session_create(context_t *context)
        this->controllers = linked_list_create();
        this->filters = linked_list_create();
        this->context = context;
-       
+
        return &this->public;
 }