[svn-commits] branch oej/test-this-branch r28480 - in /team/oej/test-this-branch: cdr/ conf...

svn-commits at lists.digium.com svn-commits at lists.digium.com
Thu May 18 23:40:11 MST 2006


Author: oej
Date: Fri May 19 01:40:10 2006
New Revision: 28480

URL: http://svn.digium.com/view/asterisk?rev=28480&view=rev
Log:
Updating cdr_radius to latest version. Thanks, Russell, for updating this code!

Modified:
    team/oej/test-this-branch/cdr/cdr_radius.c
    team/oej/test-this-branch/configs/cdr.conf.sample

Modified: team/oej/test-this-branch/cdr/cdr_radius.c
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/cdr/cdr_radius.c?rev=28480&r1=28479&r2=28480&view=diff
==============================================================================
--- team/oej/test-this-branch/cdr/cdr_radius.c (original)
+++ team/oej/test-this-branch/cdr/cdr_radius.c Fri May 19 01:40:10 2006
@@ -27,14 +27,17 @@
  * \ingroup cdr_drivers
  */
 
-#include <sys/types.h>
+/*** MODULEINFO
+	<depend>radiusclient</depend>
+ ***/
+
+#include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
-
-#include <stdlib.h>
 #include <unistd.h>
 #include <time.h>
+#include <sys/types.h>
 #include <radiusclient-ng.h>
 
 #include "asterisk.h"
@@ -47,231 +50,194 @@
 #include "asterisk/logger.h"
 #include "asterisk/utils.h"
 
-/* ISO 8601 standard format */
+/*! ISO 8601 standard format */
 #define DATE_FORMAT "%Y-%m-%d %T %z"
 
 #define VENDOR_CODE           22736
 
-#define PW_AST_ACCT_CODE      101
-#define PW_AST_SRC            102
-#define PW_AST_DST            103
-#define PW_AST_DST_CTX        104
-#define PW_AST_CLID           105
-#define PW_AST_CHAN           106
-#define PW_AST_DST_CHAN       107
-#define PW_AST_LAST_APP       108
-#define PW_AST_LAST_DATA      109
-#define PW_AST_START_TIME     110
-#define PW_AST_ANSWER_TIME    111
-#define PW_AST_END_TIME       112
-#define PW_AST_DURATION       113
-#define PW_AST_BILL_SEC       114
-#define PW_AST_DISPOSITION    115
-#define PW_AST_AMA_FLAGS      116
-#define PW_AST_UNIQUE_ID      117
-#define PW_AST_USER_FIELD     118
-
-/*----------------------------------------------------
-  The values are as follows (taken from cdr_cdsv.c):
-
-
-  "Asterisk-Acc-Code",          The account name of detail records, detail records are configured on a channel basis, IAX and SIP are determined by user *
-		        	Zap is determined by channel in zaptel.conf 
-  "Asterisk-Src",
-  "Asterisk-Dst",
-  "Asterisk-Dst-Ctx",           The destination context
-  "Asterisk-Clid",
-  "Asterisk-Chan",              The channel
-  "Asterisk-Dst-Chan",	        (if applicable)
-  "Asterisk-Last-App",	        Last application run on the channel 
-  "Asterisk-Last-Data", 	Argument to the last channel 
-  "Asterisk-Start-Time",        
-  "Asterisk-Answer-Time", 
-  "Asterisk-End-Time", 
-  "Asterisk-Duration",   	Duration is the whole length that the entire call lasted. ie. call rx'd to hangup  
-  			"end time" minus "start time" 
-  "Asterisk-Bill-Sec", 	        The duration that a call was up after other end answered which will be <= to duration  
-  			"end time" minus "answer time" 
-  "Asterisk-Disposition",    	ANSWERED, NO ANSWER, BUSY 
-  "Asterisk-AMA-Flags",       	DOCUMENTATION, BILL, IGNORE etc, specified on a per channel basis like accountcode. 
-  "Asterisk-Unique-ID",         Unique call identifier 
-  "Asterisk-User-Field"		User field set via SetCDRUserField 
-----------------------------------------------------------*/
-
+enum {
+	PW_AST_ACCT_CODE =    101,
+	PW_AST_SRC =          102,
+	PW_AST_DST =          103,
+	PW_AST_DST_CTX =      104,
+	PW_AST_CLID =         105,
+	PW_AST_CHAN =         106,
+	PW_AST_DST_CHAN =     107,
+	PW_AST_LAST_APP =     108,
+	PW_AST_LAST_DATA =    109,
+	PW_AST_START_TIME =   110,
+	PW_AST_ANSWER_TIME =  111,
+	PW_AST_END_TIME =     112,
+	PW_AST_DURATION =     113,
+	PW_AST_BILL_SEC =     114,
+	PW_AST_DISPOSITION =  115,
+	PW_AST_AMA_FLAGS =    116,
+	PW_AST_UNIQUE_ID =    117,
+	PW_AST_USER_FIELD =   118
+};
+
+enum {
+	/*! Log dates and times in UTC */
+	RADIUS_FLAG_USEGMTIME = (1 << 0),
+	/*! Log Unique ID */
+	RADIUS_FLAG_LOGUNIQUEID = (1 << 1),
+	/*! Log User Field */
+	RADIUS_FLAG_LOGUSERFIELD = (1 << 2)
+};
 
 static char *desc = "RADIUS CDR Backend";
 static char *name = "radius";
-static char *radius_config = "radius.conf";
 static char *cdr_config = "cdr.conf";
 
-static char radiuscfg[AST_CONFIG_MAX_PATH];
-
-AST_MUTEX_DEFINE_STATIC(rc_lock);
-
-static int global_usegmtime = -1; /*!< log dates and times in UTC */
-static int global_loguniqueid = -1; /*!< log unique id */
-static int global_loguserfield = -1; /*!< log user field */
+static char radiuscfg[AST_CONFIG_MAX_PATH] = "/etc/radiusclient-ng/radiusclient.conf";
+
+static struct ast_flags global_flags = { RADIUS_FLAG_USEGMTIME | RADIUS_FLAG_LOGUNIQUEID | RADIUS_FLAG_LOGUSERFIELD };
 
 static rc_handle *rh = NULL;
 
 static int build_radius_record(VALUE_PAIR **send, struct ast_cdr *cdr)
 {
-
-	int aux;
-	char buf[253];
+	int recordtype = PW_STATUS_STOP;
 	struct tm tm;
 	char timestr[128];
-
-	aux = PW_STATUS_STOP;
-	if (!rc_avpair_add(rh, send, PW_ACCT_STATUS_TYPE, &aux, 0, 0)) {
+	char *tmp;
+
+	if (!rc_avpair_add(rh, send, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
 	/* Account code */
-	ast_copy_string(buf, cdr->accountcode, sizeof(buf));
-	if (!rc_avpair_add(rh, send, PW_AST_ACCT_CODE, &buf, strlen(buf), VENDOR_CODE)) {
+	if (!rc_avpair_add(rh, send, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
  	/* Source */
-	ast_copy_string(buf, cdr->src, sizeof(buf));
-	if (!rc_avpair_add(rh, send, PW_AST_SRC, &buf, strlen(buf), VENDOR_CODE)) {
+	if (!rc_avpair_add(rh, send, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
  	/* Destination */
-	ast_copy_string(buf, cdr->dst, sizeof(buf));
-	if (!rc_avpair_add(rh, send, PW_AST_DST, &buf, strlen(buf), VENDOR_CODE)) {
+	if (!rc_avpair_add(rh, send, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
  	/* Destination context */
-	ast_copy_string(buf, cdr->dcontext, sizeof(buf));
-	if (!rc_avpair_add(rh, send, PW_AST_DST_CTX, &buf, strlen(buf), VENDOR_CODE)) {
+	if (!rc_avpair_add(rh, send, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
 	/* Caller ID */
-	ast_copy_string(buf, cdr->clid, sizeof(buf));
-	if (!rc_avpair_add(rh, send, PW_AST_CLID, &buf, strlen(buf), VENDOR_CODE)) {
+	if (!rc_avpair_add(rh, send, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
 	/* Channel */
-	ast_copy_string(buf, cdr->channel, sizeof(buf));
-	if (!rc_avpair_add(rh, send, PW_AST_CHAN, &buf, strlen(buf), VENDOR_CODE)) {
+	if (!rc_avpair_add(rh, send, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
 	/* Destination Channel */
-	ast_copy_string(buf, cdr->dstchannel, sizeof(buf));
-	if (!rc_avpair_add(rh, send, PW_AST_DST_CHAN, &buf, strlen(buf), VENDOR_CODE)) {
+	if (!rc_avpair_add(rh, send, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
 	/* Last Application */
-	ast_copy_string(buf, cdr->lastapp, sizeof(buf));
-	if (!rc_avpair_add(rh, send, PW_AST_LAST_APP, &buf, strlen(buf), VENDOR_CODE)) {
+	if (!rc_avpair_add(rh, send, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
 	/* Last Data */
-	ast_copy_string(buf, cdr->lastdata, sizeof(buf));
-	if (!rc_avpair_add(rh, send, PW_AST_LAST_DATA, &buf, strlen(buf), VENDOR_CODE)) {
+	if (!rc_avpair_add(rh, send, PW_AST_LAST_DATA, &cdr->lastdata, strlen(cdr->lastdata), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
 
 	/* Start Time */
-	if (global_usegmtime) 
+	if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME))
 		gmtime_r(&(cdr->start.tv_sec), &tm);
 	else
 		localtime_r(&(cdr->start.tv_sec), &tm);
-	strftime(timestr, 128, DATE_FORMAT, &tm);
+	strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
 	if (!rc_avpair_add(rh, send, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
 	/* Answer Time */
-	if (global_usegmtime) 
+	if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME))
 		gmtime_r(&(cdr->answer.tv_sec), &tm);
 	else
 		localtime_r(&(cdr->answer.tv_sec), &tm);
-	strftime(timestr, 128, DATE_FORMAT, &tm);
+	strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
 	if (!rc_avpair_add(rh, send, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
 	/* End Time */
-	if (global_usegmtime) 
+	if (ast_test_flag(&global_flags, RADIUS_FLAG_USEGMTIME))
 		gmtime_r(&(cdr->end.tv_sec), &tm);
 	else
 		localtime_r(&(cdr->end.tv_sec), &tm);
-	strftime(timestr, 128, DATE_FORMAT, &tm);
+	strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
 	if (!rc_avpair_add(rh, send, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
  	/* Duration */ 
-	aux = cdr->duration;
-	if (!rc_avpair_add(rh, send, PW_AST_DURATION, &aux, 0, VENDOR_CODE)) {
+	if (!rc_avpair_add(rh, send, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
 	/* Billable seconds */
-	aux = cdr->billsec;
-	if (!rc_avpair_add(rh, send, PW_AST_BILL_SEC, &aux, 0, VENDOR_CODE)) {
+	if (!rc_avpair_add(rh, send, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
 	/* Disposition */
-	ast_copy_string(buf, ast_cdr_disp2str(cdr->disposition), sizeof(buf));
-	if (!rc_avpair_add(rh, send, PW_AST_DISPOSITION, &buf, strlen(buf), VENDOR_CODE)) {
+	tmp = ast_cdr_disp2str(cdr->disposition);
+	if (!rc_avpair_add(rh, send, PW_AST_DISPOSITION, &tmp, strlen(tmp), VENDOR_CODE)) {
 		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 		return -1;
 	}
 
 	/* AMA Flags */
-	ast_copy_string(buf, ast_cdr_flags2str(cdr->amaflags), sizeof(buf));
-	if (!rc_avpair_add(rh, send, PW_AST_AMA_FLAGS, &buf, strlen(buf), VENDOR_CODE)) {
-		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
-		return -1;
-	}
-
-	if (global_loguniqueid) {
+	tmp = ast_cdr_flags2str(cdr->amaflags);
+	if (!rc_avpair_add(rh, send, PW_AST_AMA_FLAGS, &tmp, strlen(tmp), VENDOR_CODE)) {
+		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
+		return -1;
+	}
+
+	if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUNIQUEID)) {
 		/* Unique ID */
-		ast_copy_string(buf, cdr->uniqueid, sizeof(buf));
-		if (!rc_avpair_add(rh, send, PW_AST_UNIQUE_ID, &buf, strlen(buf), VENDOR_CODE)) {
+		if (!rc_avpair_add(rh, send, PW_AST_UNIQUE_ID, &cdr->uniqueid, strlen(cdr->uniqueid), VENDOR_CODE)) {
 			ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 			return -1;
 		}
 	}
 
-	if (global_loguserfield) {
+	if (ast_test_flag(&global_flags, RADIUS_FLAG_LOGUSERFIELD)) {
 		/* append the user field */
-		ast_copy_string(buf, cdr->userfield, sizeof(buf));
-		if (!rc_avpair_add(rh, send, PW_AST_USER_FIELD, &buf, strlen(buf), VENDOR_CODE)) {
+		if (!rc_avpair_add(rh, send, PW_AST_USER_FIELD, &cdr->userfield, strlen(cdr->userfield), VENDOR_CODE)) {
 			ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
 			return -1;
 		}
 	}
 
-	return 1;
+	return 0;
 }
 
 static int radius_log(struct ast_cdr *cdr)
@@ -279,113 +245,64 @@
 	int result = ERROR_RC;
 	VALUE_PAIR *send = NULL;
 
-	ast_mutex_lock(&rc_lock);	
-	
-	if (!build_radius_record(&send, cdr)) {
+	if (build_radius_record(&send, cdr)) {
 		ast_log(LOG_WARNING, "Unable to create RADIUS record. CDR not recorded!\n");
+		return result;
 	}
 	
 	result = rc_acct(rh, 0, send);
-	if (result == OK_RC) {
-		ast_log(LOG_NOTICE, "RADIUS CDR recorded.\n");
-	}
-	else {
-		ast_log(LOG_NOTICE, "Failed to record CDR.\n");
-	}
-
-	ast_mutex_unlock(&rc_lock);
+	if (result != OK_RC)
+		ast_log(LOG_ERROR, "Failed to record Radius CDR record!\n");
 
 	return result;
 }
 
-const char *description(void)
+static const char *description(void)
 {
 	return desc;
 }
 
-int unload_module(void)
+static int unload_module(void *mod)
 {
 	ast_cdr_unregister(name);
 	return 0;
 }
 
-int load_module(void)
-{
-	int res;
+static int load_module(void *mod)
+{
 	struct ast_config *cfg;
-	struct ast_variable *var;
 	char *tmp;
 
-	cfg = ast_config_load(radius_config);
-	if (!cfg) {
-		ast_log(LOG_WARNING,"Unable to load config for RADIUS CDR's: %s\n", radius_config);
-		return 0;
+	if ((cfg = ast_config_load(cdr_config))) {
+		ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "usegmtime")), RADIUS_FLAG_USEGMTIME);
+		ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguniqueid")), RADIUS_FLAG_LOGUNIQUEID);
+		ast_set2_flag(&global_flags, ast_true(ast_variable_retrieve(cfg, "radius", "loguserfield")), RADIUS_FLAG_LOGUSERFIELD);
+		if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg")))
+			ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
+		ast_config_destroy(cfg);
 	}
 	
-	var = ast_variable_browse(cfg, "global");
-	if (!var) {
-		/* nothing configured */
-		return 0;
-	}
-
-	tmp = ast_variable_retrieve(cfg, "global", "radiuscfg");
-	if (tmp) {
-		ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
-	}
-	else {
-		ast_log(LOG_ERROR,"Variable radiuscfg not set in config.\n");
-		return 0;
-	}
-
-	ast_config_destroy(cfg);
-
-	ast_log(LOG_DEBUG,"Got config file for radius client library: %s\n", radiuscfg);
-
 	/* start logging */
 	rc_openlog("asterisk");
 
 	/* read radiusclient-ng config file */
-	if ((rh = rc_read_config(radiuscfg)) == NULL) {
+	if (!(rh = rc_read_config(radiuscfg))) {
 		ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
 		return -1;
 	}
 
 	/* read radiusclient-ng dictionaries */
-	if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary")) != 0) {
+	if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
 		ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
 		return -1;
 	}
-
-	cfg = ast_config_load(cdr_config);
-	if (!cfg) {
-		ast_log(LOG_WARNING,"Unable to load config for RADIUS CDR's: %s\n", cdr_config);
-		return 0;
-	}
 	
-	global_usegmtime = ast_true(ast_variable_retrieve(cfg, "radius", "usegmtime"));
-	global_loguniqueid = ast_true(ast_variable_retrieve(cfg, "radius", "loguniqueid"));
-	global_loguserfield = ast_true(ast_variable_retrieve(cfg, "radius", "loguserfield"));
-		
-	ast_config_destroy(cfg);
-	
-	res = ast_cdr_register(name, desc, radius_log);
-	if (res) {
-		ast_log(LOG_ERROR, "Unable to register RADIUS CDR handling\n");
-	}
-	return res;
-}
-
-int reload(void)
-{
-	return 0;
-}
-
-int usecount(void)
-{
-	return 0;
-}
-
-const char *key()
+	return ast_cdr_register(name, desc, radius_log);
+}
+
+static const char *key(void)
 {
 	return ASTERISK_GPL_KEY;
 }
+
+STD_MOD(MOD_0, NULL, NULL, NULL);

Modified: team/oej/test-this-branch/configs/cdr.conf.sample
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/configs/cdr.conf.sample?rev=28480&r1=28479&r2=28480&view=diff
==============================================================================
--- team/oej/test-this-branch/configs/cdr.conf.sample (original)
+++ team/oej/test-this-branch/configs/cdr.conf.sample Fri May 19 01:40:10 2006
@@ -65,4 +65,7 @@
 ;usegmtime=yes ;log date/time in GMT
 ;loguniqueid=yes ;log uniqueid
 ;loguserfield=yes ;log user field
+; Set this to the location of the radiusclient-ng configuration file
+; The default is /etc/radiusclient-ng/radiusclient.conf
+;radiuscfg => /usr/local/etc/radiusclient-ng/radiusclient.conf
 



More information about the svn-commits mailing list