From: Martin Willi Date: Tue, 15 Sep 2009 14:19:39 +0000 (+0200) Subject: Passing 0 to segments->(de-)activate enables/disables all segments X-Git-Tag: 4.4.0~86 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c573b11c5529d3850a3f7f10afcf27055d182c2e;p=thirdparty%2Fstrongswan.git Passing 0 to segments->(de-)activate enables/disables all segments --- diff --git a/src/charon/plugins/ha_sync/ha_sync_segments.c b/src/charon/plugins/ha_sync/ha_sync_segments.c index f6f66bae9b..90897a3d6f 100644 --- a/src/charon/plugins/ha_sync/ha_sync_segments.c +++ b/src/charon/plugins/ha_sync/ha_sync_segments.c @@ -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); diff --git a/src/charon/plugins/ha_sync/ha_sync_segments.h b/src/charon/plugins/ha_sync/ha_sync_segments.h index 18df462d27..b88e3a4649 100644 --- a/src/charon/plugins/ha_sync/ha_sync_segments.h +++ b/src/charon/plugins/ha_sync/ha_sync_segments.h @@ -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);