Don't query the policy usetime if there was no traffic on the SA.

This helps in cases where a policy is assigned to more than one SA. That
is, SAs now should have different usetimes even if they use the same policy.
This commit is contained in:
Tobias Brunner
2009-08-06 15:14:54 +02:00
parent b3f8ea8346
commit dd83c6d490
+59 -31
View File
@@ -374,6 +374,58 @@ static enumerator_t* create_policy_enumerator(private_child_sa_t *this)
return &e->public; return &e->public;
} }
/**
* update the cached usebytes
* returns SUCCESS if the usebytes have changed, FAILED if not or no SPIs
* are available, and NOT_SUPPORTED if the kernel interface does not support
* quering the usebytes.
*/
static bool update_usebytes(private_child_sa_t *this, bool inbound)
{
status_t status = FAILED;
u_int64_t bytes;
if (inbound)
{
if (this->my_spi)
{
status = charon->kernel_interface->query_sa(
charon->kernel_interface,
this->other_addr, this->my_addr,
this->my_spi, this->protocol, &bytes);
if (status == SUCCESS)
{
if (bytes > this->my_usebytes)
{
this->my_usebytes = bytes;
return SUCCESS;
}
return FAILED;
}
}
}
else
{
if (this->other_spi)
{
status = charon->kernel_interface->query_sa(
charon->kernel_interface,
this->my_addr, this->other_addr,
this->other_spi, this->protocol, &bytes);
if (status == SUCCESS)
{
if (bytes > this->other_usebytes)
{
this->other_usebytes = bytes;
return SUCCESS;
}
return FAILED;
}
}
}
return status;
}
/** /**
* Implementation of child_sa_t.get_usetime * Implementation of child_sa_t.get_usetime
*/ */
@@ -383,6 +435,11 @@ static u_int32_t get_usetime(private_child_sa_t *this, bool inbound)
traffic_selector_t *my_ts, *other_ts; traffic_selector_t *my_ts, *other_ts;
u_int32_t last_use = 0; u_int32_t last_use = 0;
if (update_usebytes(this, inbound) == FAILED)
{ /* no SPI or no traffic since last update */
return inbound ? this->my_usetime : this->other_usetime;
}
enumerator = create_policy_enumerator(this); enumerator = create_policy_enumerator(this);
while (enumerator->enumerate(enumerator, &my_ts, &other_ts)) while (enumerator->enumerate(enumerator, &my_ts, &other_ts))
{ {
@@ -430,37 +487,8 @@ static u_int32_t get_usetime(private_child_sa_t *this, bool inbound)
*/ */
static u_int64_t get_usebytes(private_child_sa_t *this, bool inbound) static u_int64_t get_usebytes(private_child_sa_t *this, bool inbound)
{ {
status_t status; update_usebytes(this, inbound);
u_int64_t bytes; return inbound ? this->my_usebytes : this->other_usebytes;
if (inbound)
{
if (this->my_spi)
{
status = charon->kernel_interface->query_sa(charon->kernel_interface,
this->other_addr, this->my_addr,
this->my_spi, this->protocol, &bytes);
if (status == SUCCESS && bytes > this->my_usebytes)
{
this->my_usebytes = bytes;
}
}
return this->my_usebytes;
}
else
{
if (this->other_spi)
{
status = charon->kernel_interface->query_sa(charon->kernel_interface,
this->my_addr, this->other_addr,
this->other_spi, this->protocol, &bytes);
if (status == SUCCESS && bytes > this->other_usebytes)
{
this->other_usebytes = bytes;
}
}
return this->other_usebytes;
}
} }
/** /**