Accouting ON/OFF

Gunter Burchardt gbur at informatik.uni-rostock.de
Thu Jan 29 05:23:33 EST 2004


Hello

When using accouting it is a style to inform the accouting server that
from this NAS accouting information will be send and that from now no
accouting information will be send. That enables the accouting server
to close unclosed accouting session in database.

I create a patch which allows hostapd to sends an
accounting-request with type accounting-on on startup and  with type
accouting-off at shutdown.

The patch is for the cvs version (0.2.0)

regards
gunter


-------------- next part --------------
diff -urN hostap-0.2.0/hostapd/accounting.c hostap-0.2.0.patched/hostapd/accounting.c
--- hostap-0.2.0/hostapd/accounting.c	2004-01-23 13:47:01.000000000 +0100
+++ hostap-0.2.0.patched/hostapd/accounting.c	2004-01-23 13:25:32.000000000 +0100
@@ -45,15 +45,21 @@
 		return NULL;
 	}
 
-	radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(sta));
-
-	snprintf(buf, sizeof(buf), "%08X-%08X",
-		 hapd->radius->acct_session_id_hi, sta->acct_session_id_lo);
-	if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
-				 buf, strlen(buf))) {
-		printf("Could not add Acct-Session-Id\n");
-		goto fail;
-	}
+  if(sta != NULL)
+  {
+   	radius_msg_make_authenticator(msg, (u8 *) sta, sizeof(sta));
+
+  	snprintf(buf, sizeof(buf), "%08X-%08X",
+  		 hapd->radius->acct_session_id_hi, sta->acct_session_id_lo);
+  	if (!radius_msg_add_attr(msg, RADIUS_ATTR_ACCT_SESSION_ID,
+  				 buf, strlen(buf))) {
+  		printf("Could not add Acct-Session-Id\n");
+  		goto fail;
+  	}
+  } else
+  {
+    radius_msg_make_authenticator(msg, (u8 *) hapd, sizeof(hapd));
+  }
 
 	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_STATUS_TYPE,
 				       status_type)) {
@@ -69,19 +75,22 @@
 		goto fail;
 	}
 
-	val = sta->identity;
-	len = sta->identity_len;
-	if (!val) {
-		snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
-			 MAC2STR(sta->addr));
-		val = buf;
-		len = strlen(val);
-	}
-
-	if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val, len)) {
-		printf("Could not add User-Name\n");
-		goto fail;
-	}
+  if(sta != NULL)
+  {
+  	val = sta->identity;
+  	len = sta->identity_len;
+  	if (!val) {
+  		snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT,
+  			 MAC2STR(sta->addr));
+  		val = buf;
+  		len = strlen(val);
+  	}
+
+  	if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, val, len)) {
+  		printf("Could not add User-Name\n");
+  		goto fail;
+  	}
+  }
 
 	if (!radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER, hapd->conf->NAS_Identifier, strlen(hapd->conf->NAS_Identifier))) {
 		printf("Could not add NAS-Identifier\n");
@@ -94,10 +103,13 @@
 		goto fail;
 	}
 
-	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
-		printf("Could not add NAS-Port\n");
-		goto fail;
-	}
+  if(sta != NULL)
+  {
+  	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT, sta->aid)) {
+	  	printf("Could not add NAS-Port\n");
+	    goto fail;
+	  }
+  }
 
 	snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
 		 MAC2STR(hapd->own_addr), hapd->conf->ssid);
@@ -107,26 +119,29 @@
 		goto fail;
 	}
 
-	snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
-		 MAC2STR(sta->addr));
-	if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
-				 buf, strlen(buf))) {
-		printf("Could not add Calling-Station-Id\n");
-		goto fail;
-	}
-
-	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
-				       RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
-		printf("Could not add NAS-Port-Type\n");
-		goto fail;
-	}
-
-	snprintf(buf, sizeof(buf), "CONNECT 11Mbps 802.11b");
-	if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
-				 buf, strlen(buf))) {
-		printf("Could not add Connect-Info\n");
-		goto fail;
-	}
+  if(sta != NULL)
+  {
+  	snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
+  		 MAC2STR(sta->addr));
+  	if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
+  				 buf, strlen(buf))) {
+  		printf("Could not add Calling-Station-Id\n");
+  		goto fail;
+  	}
+
+  	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
+  				       RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
+  		printf("Could not add NAS-Port-Type\n");
+  		goto fail;
+  	}
+
+  	snprintf(buf, sizeof(buf), "CONNECT 11Mbps 802.11b");
+  	if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
+  				 buf, strlen(buf))) {
+  		printf("Could not add Connect-Info\n");
+  		goto fail;
+  	}
+  }
 
 	return msg;
 
@@ -287,10 +296,11 @@
 	return RADIUS_RX_PROCESSED;
 }
 
-
 int accounting_init(hostapd *hapd)
 {
-	/* Acct-Session-Id should be unique over reboots. If reliable clock is
+	struct radius_msg *msg;
+
+  /* Acct-Session-Id should be unique over reboots. If reliable clock is
 	 * not available, this could be replaced with reboot counter, etc. */
 	hapd->radius->acct_session_id_hi = time(NULL);
 
@@ -298,10 +308,55 @@
 				   NULL))
 		return -1;
 
+  /*
+   * Send radius-server a message that accounting will start
+   * Radius server have the chance to close old accounting sessions
+   */
+	msg = accounting_msg(hapd, NULL, RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON);
+  if (msg)
+  {
+  	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
+  				       RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT))
+    {
+  		printf("Could not add Acct-Terminate-Cause\n");
+		  goto fail;
+    }
+		radius_client_send(hapd, msg, RADIUS_ACCT);
+  }
+
 	return 0;
+
+ fail:
+	radius_msg_free(msg);
+	free(msg);
+  return 0;
+  
 }
 
 
 void accounting_deinit(hostapd *hapd)
 {
+	struct radius_msg *msg;
+
+  /*
+   * Send radius-server a message that accounting will end
+   * Radius server have the chance to close old accounting sessions
+   */
+  msg = accounting_msg(hapd, NULL, RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF);
+  if (msg)
+  {
+  	if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_ACCT_TERMINATE_CAUSE,
+  				       RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT))
+    {
+  		printf("Could not add Acct-Terminate-Cause\n");
+		  goto fail;
+    }
+		radius_client_send(hapd, msg, RADIUS_ACCT);
+  }
+  return;
+
+ fail:
+	radius_msg_free(msg);
+	free(msg);
+    
 }


More information about the HostAP mailing list