Passing 0 to segments->(de-)activate enables/disables all segments
authorMartin Willi <martin@strongswan.org>
Tue, 15 Sep 2009 14:19:39 +0000 (16:19 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 7 Apr 2010 11:55:14 +0000 (13:55 +0200)
src/charon/plugins/ha_sync/ha_sync_segments.c
src/charon/plugins/ha_sync/ha_sync_segments.h

index f6f66ba..90897a3 100644 (file)
@@ -104,59 +104,78 @@ static void log_segments(private_ha_sync_segments_t *this, bool activated,
 }
 
 /**
- * Implementation of ha_sync_segments_t.activate
+ * Get the bit of the segment in the bitmask
  */
-static void activate(private_ha_sync_segments_t *this, u_int segment)
+static inline u_int16_t bit_of(u_int segment)
+{
+       return 0x01 << (segment - 1);
+}
+
+/**
+ * Enable/Disable an an IKE_SA.
+ */
+static void enable_disable(private_ha_sync_segments_t *this, u_int segment,
+                                                  ike_sa_state_t old, ike_sa_state_t new, bool enable)
 {
        ike_sa_t *ike_sa;
        enumerator_t *enumerator;
-       u_int16_t mask = 0x01 << (segment - 1);
+       u_int i, limit;
 
-       if (segment > 0 && segment <= this->segment_count && !(this->active & mask))
+       if (segment == 0 || segment <= this->segment_count)
        {
-               this->active |= mask;
-
+               if (segment)
+               {       /* loop once for single segment ... */
+                       limit = segment + 1;
+               }
+               else
+               {       /* or segment_count times for all segments */
+                       limit = this->segment_count;
+               }
+               for (i = segment; i < limit; i++)
+               {
+                       if (enable)
+                       {
+                               this->active |= bit_of(i);
+                       }
+                       else
+                       {
+                               this->active &= ~bit_of(i);
+                       }
+               }
                enumerator = charon->ike_sa_manager->create_enumerator(charon->ike_sa_manager);
                while (enumerator->enumerate(enumerator, &ike_sa))
                {
-                       if (ike_sa->get_state(ike_sa) == IKE_PASSIVE &&
-                               in_segment(this, ike_sa->get_other_host(ike_sa), segment))
+                       if (ike_sa->get_state(ike_sa) == old)
                        {
-                               ike_sa->set_state(ike_sa, IKE_ESTABLISHED);
+                               for (i = segment; i < limit; i++)
+                               {
+                                       if (in_segment(this, ike_sa->get_other_host(ike_sa), i))
+                                       {
+                                               ike_sa->set_state(ike_sa, new);
+                                       }
+                               }
                        }
                }
                enumerator->destroy(enumerator);
 
-               log_segments(this, TRUE, segment);
+               log_segments(this, enable, segment);
        }
 }
 
 /**
+ * Implementation of ha_sync_segments_t.activate
+ */
+static void activate(private_ha_sync_segments_t *this, u_int segment)
+{
+       return enable_disable(this, segment, IKE_PASSIVE, IKE_ESTABLISHED, TRUE);
+}
+
+/**
  * Implementation of ha_sync_segments_t.deactivate
  */
 static void deactivate(private_ha_sync_segments_t *this, u_int segment)
 {
-       ike_sa_t *ike_sa;
-       enumerator_t *enumerator;
-       u_int16_t mask = 0x01 << (segment - 1);
-
-       if (segment > 0 && segment <= this->segment_count && (this->active & mask))
-       {
-               this->active &= ~mask;
-
-               enumerator = charon->ike_sa_manager->create_enumerator(charon->ike_sa_manager);
-               while (enumerator->enumerate(enumerator, &ike_sa))
-               {
-                       if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED &&
-                               in_segment(this, ike_sa->get_other_host(ike_sa), segment))
-                       {
-                               ike_sa->set_state(ike_sa, IKE_PASSIVE);
-                       }
-               }
-               enumerator->destroy(enumerator);
-
-               log_segments(this, FALSE, segment);
-       }
+       return enable_disable(this, segment, IKE_ESTABLISHED, IKE_PASSIVE, FALSE);
 }
 
 /**
@@ -192,8 +211,7 @@ static void resync(private_ha_sync_segments_t *this, u_int segment)
        enumerator_t *enumerator;
        linked_list_t *list;
        ike_sa_id_t *id;
-       u_int16_t mask = 0x01 << (segment - 1);
-
+       u_int16_t mask = bit_of(segment);
 
        if (segment > 0 && segment <= this->segment_count && (this->active & mask))
        {
@@ -277,7 +295,7 @@ ha_sync_segments_t *ha_sync_segments_create()
                segment = atoi(str);
                if (segment > 0 && segment < MAX_SEGMENTS)
                {
-                       this->active |= 0x01 << (segment - 1);
+                       this->active |= bit_of(segment);
                }
        }
        enumerator->destroy(enumerator);
index 18df462..b88e3a4 100644 (file)
@@ -37,14 +37,14 @@ struct ha_sync_segments_t {
         * This involves moving all SAs to the daemons IKE_SA manager and handle
         * them actively now.
         *
-        * @param segment       numerical segments to takeover
+        * @param segment       numerical segment to takeover, 0 for all
         */
        void (*activate)(ha_sync_segments_t *this, u_int segment);
 
        /**
         * Deactivate a set of IKE_SAs identified by a segments.
         *
-        * @param segment       numerical segments to takeover
+        * @param segment       numerical segment to takeover, 0 for all
         */
        void (*deactivate)(ha_sync_segments_t *this, u_int segment);