Refactored construction of RADIUS accounting messages
authorMartin Willi <martin@revosec.ch>
Fri, 24 Feb 2012 10:12:18 +0000 (11:12 +0100)
committerMartin Willi <martin@revosec.ch>
Fri, 24 Feb 2012 10:12:18 +0000 (11:12 +0100)
src/libcharon/plugins/eap_radius/eap_radius_accounting.c

index 1f32a56..d2e2cb3 100644 (file)
@@ -140,13 +140,30 @@ static bool send_message(private_eap_radius_accounting_t *this,
 }
 
 /**
+ * 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);