Refactored the lifetime_cfg_t struct to be simpler and more expressive. Initialization is now static.

This commit is contained in:
Tobias Brunner
2009-09-01 12:54:33 +02:00
parent abff49a7ff
commit e75f423753
12 changed files with 104 additions and 124 deletions
+7 -8
View File
@@ -99,7 +99,7 @@ struct private_child_cfg_t {
/**
* CHILD_SA lifetime config
*/
lifetime_cfg_t *lifetime;
lifetime_cfg_t lifetime;
/**
* enable IPComp
@@ -363,7 +363,7 @@ static u_int64_t apply_jitter(u_int64_t rekey, u_int64_t jitter)
jitter = (jitter == UINT64_MAX) ? jitter : jitter + 1;
return rekey - jitter * (random() / (RAND_MAX + 1.0));
}
#define APPLY_JITTER(l, f) l->rekey_##f = apply_jitter(l->rekey_##f, l->jitter_##f)
#define APPLY_JITTER(l) l.rekey = apply_jitter(l.rekey, l.jitter)
/**
* Implementation of child_cfg_t.get_lifetime.
@@ -371,10 +371,10 @@ static u_int64_t apply_jitter(u_int64_t rekey, u_int64_t jitter)
static lifetime_cfg_t *get_lifetime(private_child_cfg_t *this)
{
lifetime_cfg_t *lft = malloc_thing(lifetime_cfg_t);
memcpy(lft, this->lifetime, sizeof(lifetime_cfg_t));
APPLY_JITTER(lft, time);
APPLY_JITTER(lft, bytes);
APPLY_JITTER(lft, packets);
memcpy(lft, &this->lifetime, sizeof(lifetime_cfg_t));
APPLY_JITTER(lft->time);
APPLY_JITTER(lft->bytes);
APPLY_JITTER(lft->packets);
return lft;
}
@@ -480,7 +480,6 @@ static void destroy(private_child_cfg_t *this)
{
free(this->updown);
}
free(this->lifetime);
free(this->name);
free(this);
}
@@ -517,7 +516,6 @@ child_cfg_t *child_cfg_create(char *name, lifetime_cfg_t *lifetime,
this->public.destroy = (void (*) (child_cfg_t*))destroy;
this->name = strdup(name);
this->lifetime = lifetime;
this->updown = updown ? strdup(updown) : NULL;
this->hostaccess = hostaccess;
this->mode = mode;
@@ -530,6 +528,7 @@ child_cfg_t *child_cfg_create(char *name, lifetime_cfg_t *lifetime,
this->proposals = linked_list_create();
this->my_ts = linked_list_create();
this->other_ts = linked_list_create();
memcpy(&this->lifetime, lifetime, sizeof(lifetime_cfg_t));
return &this->public;
}
+10 -50
View File
@@ -72,56 +72,16 @@ extern enum_name_t *ipcomp_transform_names;
* Set any of these values to 0 to ignore.
*/
struct lifetime_cfg_t {
/** Time in seconds before the CHILD_SA gets invalid. */
u_int64_t life_time;
/** Number of bytes transmitted before the CHILD_SA gets invalid. */
u_int64_t life_bytes;
/** Number of packets transmitted before the CHILD_SA gets invalid. */
u_int64_t life_packets;
/** Time in seconds before the CHILD_SA gets rekeyed. */
u_int64_t rekey_time;
/** Number of bytes transmitted before the CHILD_SA gets rekeyed. */
u_int64_t rekey_bytes;
/** Number of packets transmitted before the CHILD_SA gets rekeyed. */
u_int64_t rekey_packets;
/** The range of a random value subtracted from rekey_time */
u_int64_t jitter_time;
/** The range of a random value subtracted from rekey_bytes */
u_int64_t jitter_bytes;
/** The range of a random value subtracted from rekey_packets */
u_int64_t jitter_packets;
struct {
/** Limit before the CHILD_SA gets invalid. */
u_int64_t life;
/** Limit before the CHILD_SA gets rekeyed. */
u_int64_t rekey;
/** The range of a random value subtracted from rekey. */
u_int64_t jitter;
} time, bytes, packets;
};
/**
* Helper macro to easily set all three values of a specified limit (time,
* bytes, packets).
*/
#define LIFETIME_CFG_SET(l, limit, life, rekey, jitter) do { \
(l)->life_##limit = (life); \
(l)->rekey_##limit = (rekey); \
(l)->jitter_##limit = (jitter); \
} while(0)
/**
* Create a new lifetime_cfg_t object.
*/
static inline lifetime_cfg_t* lifetime_cfg_create() {
lifetime_cfg_t *this = malloc_thing(lifetime_cfg_t);
memset(this, 0, sizeof(lifetime_cfg_t));
return this;
}
/**
* Special constructor for the (currently) most common case.
*/
static inline lifetime_cfg_t* lifetime_cfg_create_time(u_int64_t life,
u_int64_t rekey, u_int64_t jitter)
{
lifetime_cfg_t *this = lifetime_cfg_create();
LIFETIME_CFG_SET(this, time, life, rekey, jitter);
return this;
}
/**
* A child_cfg_t defines the config template for a CHILD_SA.
*
@@ -316,9 +276,9 @@ struct child_cfg_t {
*
* The "name" string gets cloned.
*
* The lifetime_cfg_t object gets adopted by this config.
* The lifetime_cfg_t object gets cloned.
* To prevent two peers to start rekeying at the same time, a jitter may be
* specified. Rekeying of an SA starts at (rekey_xxx - random(0, jitter_xxx)).
* specified. Rekeying of an SA starts at (x.rekey - random(0, x.jitter)).
*
* After a call to create, a reference is obtained (refcount = 1).
*