Refactored construction of RADIUS accounting messages

This commit is contained in:
Martin Willi 2012-02-24 11:12:18 +01:00
parent d93f204ca5
commit d15ae70c8c
1 changed files with 21 additions and 23 deletions

View File

@ -139,14 +139,31 @@ static bool send_message(private_eap_radius_accounting_t *this,
return ack;
}
/**
* Add common IKE_SA parameters to RADIUS account message
*/
static void add_ike_sa_parameters(radius_message_t *message, ike_sa_t *ike_sa)
{
host_t *vip;
char buf[64];
snprintf(buf, sizeof(buf), "%Y", ike_sa->get_other_eap_id(ike_sa));
message->add(message, RAT_USER_NAME, chunk_create(buf, strlen(buf)));
snprintf(buf, sizeof(buf), "%#H", ike_sa->get_other_host(ike_sa));
message->add(message, RAT_CALLING_STATION_ID, chunk_create(buf, strlen(buf)));
vip = ike_sa->get_virtual_ip(ike_sa, FALSE);
if (vip)
{
message->add(message, RAT_FRAMED_IP_ADDRESS, vip->get_address(vip));
}
}
/**
* Send an accounting start message
*/
static void send_start(private_eap_radius_accounting_t *this, ike_sa_t *ike_sa)
{
char buf[64];
radius_message_t *message;
host_t *vip;
entry_t *entry;
u_int32_t id, value;
@ -161,15 +178,7 @@ static void send_start(private_eap_radius_accounting_t *this, ike_sa_t *ike_sa)
message->add(message, RAT_ACCT_STATUS_TYPE, chunk_from_thing(value));
message->add(message, RAT_ACCT_SESSION_ID,
chunk_create(entry->sid, strlen(entry->sid)));
snprintf(buf, sizeof(buf), "%Y", ike_sa->get_other_eap_id(ike_sa));
message->add(message, RAT_USER_NAME, chunk_create(buf, strlen(buf)));
snprintf(buf, sizeof(buf), "%#H", ike_sa->get_other_host(ike_sa));
message->add(message, RAT_CALLING_STATION_ID, chunk_create(buf, strlen(buf)));
vip = ike_sa->get_virtual_ip(ike_sa, FALSE);
if (vip)
{
message->add(message, RAT_FRAMED_IP_ADDRESS, vip->get_address(vip));
}
add_ike_sa_parameters(message, ike_sa);
if (send_message(this, message))
{
this->mutex->lock(this->mutex);
@ -188,8 +197,6 @@ static void send_stop(private_eap_radius_accounting_t *this, ike_sa_t *ike_sa)
radius_message_t *message;
entry_t *entry;
u_int32_t id, value;
host_t *vip;
char buf[64];
id = ike_sa->get_unique_id(ike_sa);
this->mutex->lock(this->mutex);
@ -202,16 +209,7 @@ static void send_stop(private_eap_radius_accounting_t *this, ike_sa_t *ike_sa)
message->add(message, RAT_ACCT_STATUS_TYPE, chunk_from_thing(value));
message->add(message, RAT_ACCT_SESSION_ID,
chunk_create(entry->sid, strlen(entry->sid)));
snprintf(buf, sizeof(buf), "%Y", ike_sa->get_other_eap_id(ike_sa));
message->add(message, RAT_USER_NAME, chunk_create(buf, strlen(buf)));
snprintf(buf, sizeof(buf), "%#H", ike_sa->get_other_host(ike_sa));
message->add(message, RAT_CALLING_STATION_ID,
chunk_create(buf, strlen(buf)));
vip = ike_sa->get_virtual_ip(ike_sa, FALSE);
if (vip)
{
message->add(message, RAT_FRAMED_IP_ADDRESS, vip->get_address(vip));
}
add_ike_sa_parameters(message, ike_sa);
value = htonl(entry->sent);
message->add(message, RAT_ACCT_OUTPUT_OCTETS, chunk_from_thing(value));
value = htonl(entry->sent >> 32);