[asterisk-commits] branch oej/test-this-branch r14180 - /team/oej/test-this-branch/cdr/cdr_radius.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Wed Mar 22 11:34:00 MST 2006


Author: oej
Date: Wed Mar 22 12:33:58 2006
New Revision: 14180

URL: http://svn.digium.com/view/asterisk?rev=14180&view=rev
Log:
Adding update from cdr_radius branch

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

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=14180&r1=14179&r2=14180&view=diff
==============================================================================
--- team/oej/test-this-branch/cdr/cdr_radius.c (original)
+++ team/oej/test-this-branch/cdr/cdr_radius.c Wed Mar 22 12:33:58 2006
@@ -37,8 +37,6 @@
 #include <time.h>
 #include <radiusclient-ng.h>
 
-#define RC_CONFIG_FILE "/usr/local/etc/radiusclient-ng/radiusclient.conf"
-
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Rev$")
@@ -51,20 +49,76 @@
 
 #define DATE_FORMAT "%Y-%m-%d %T"
 
+#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
+
+/* #define RADIUS_LOGUNIQUEID 1 */
+/* #define RADIUS_LOGUSERFIELD 1 */
+
+/*----------------------------------------------------
+  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 
+----------------------------------------------------------*/
+
+
 static char *desc = "RADIUS CDR Backend";
-
 static char *name = "radius";
+static char *config = "radius.conf";
+
+static char radiuscfg[AST_CONFIG_MAX_PATH];
 
 AST_MUTEX_DEFINE_STATIC(rc_lock);
 
 static rc_handle  *rh = NULL;
-
 
 static int build_radius_record(VALUE_PAIR **send, struct ast_cdr *cdr)
 {
 
 	int aux;
 	char buf[253];
+	struct tm tm;
+	char timestr[128];
+	time_t t;
 
 
 	aux = PW_STATUS_STOP;
@@ -73,64 +127,140 @@
 		return -1;
 	}
 
-
-/* 	/\* Account code *\/ */
-/* 	append_string(buf, cdr->accountcode, bufsize); */
+	/* 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)) {
+		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_CALLING_STATION_ID, &buf, strlen(buf), 0)) {
+	if (!rc_avpair_add(rh, send, PW_AST_SRC, &buf, strlen(buf), 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_CALLED_STATION_ID, &buf, strlen(buf), 0)) {
-		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
-		return -1;
-	}
-
-/* 	/\* Destination context *\/ */
-/* 	append_string(buf, cdr->dcontext, bufsize); */
-/* 	/\* Caller*ID *\/ */
-/* 	append_string(buf, cdr->clid, bufsize); */
-/* 	/\* Channel *\/ */
-/* 	append_string(buf, cdr->channel, bufsize); */
-/* 	/\* Destination Channel *\/ */
-/* 	append_string(buf, cdr->dstchannel, bufsize); */
-/* 	/\* Last Application *\/ */
-/* 	append_string(buf, cdr->lastapp, bufsize); */
-/* 	/\* Last Data *\/ */
-/* 	append_string(buf, cdr->lastdata, bufsize); */
-/* 	/\* Start Time *\/ */
-/* 	append_date(buf, cdr->start, bufsize); */
-/* 	/\* Answer Time *\/ */
-/* 	append_date(buf, cdr->answer, bufsize); */
-/* 	/\* End Time *\/ */
-/* 	append_date(buf, cdr->end, bufsize); */
+	if (!rc_avpair_add(rh, send, PW_AST_DST, &buf, strlen(buf), 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)) {
+		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)) {
+		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)) {
+		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)) {
+		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)) {
+		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)) {
+		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
+		return -1;
+	}
+
+	/* Start Time */
+	t = cdr->start.tv_sec;
+	localtime_r(&t, &tm);
+	strftime(timestr, 128, 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 */
+	t = cdr->answer.tv_sec;
+	localtime_r(&t, &tm);
+	strftime(timestr, 128, 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 */
+	t = cdr->end.tv_sec;
+	localtime_r(&t, &tm);
+	strftime(timestr, 128, 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_ACCT_SESSION_TIME, &cdr->duration, 0, 0)) {
-		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
-		return -1;
-	}
-
-/* 	/\* Billable seconds *\/ */
-/* 	append_int(buf, cdr->billsec, bufsize); */
-/* 	/\* Disposition *\/ */
-/* 	append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize); */
-/* 	/\* AMA Flags *\/ */
-/* 	append_string(buf, ast_cdr_flags2str(cdr->amaflags), bufsize); */
-
-/* #ifdef CSV_LOGUNIQUEID */
-/* 	/\* Unique ID *\/ */
-/* 	append_string(buf, cdr->uniqueid, bufsize); */
-/* #endif */
-/* #ifdef CSV_LOGUSERFIELD */
-/* 	/\* append the user field *\/ */
-/* 	append_string(buf, cdr->userfield,bufsize);	 */
-/* #endif */
+	if (!rc_avpair_add(rh, send, PW_AST_DURATION, &aux, 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)) {
+		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)) {
+		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;
+	}
+
+#ifdef RADIUS_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)) {
+		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
+		return -1;
+	}
+#endif
+#ifdef RADIUS_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)) {
+		ast_log(LOG_WARNING, "Failed to add VALUE PAIR. RADIUS CDR not recorded!\n");
+		return -1;
+	}
+#endif
 	return 1;
 }
 
@@ -172,13 +302,41 @@
 int load_module(void)
 {
 	int res;
+	struct ast_config *cfg;
+	struct ast_variable *var;
+	char *tmp;
+
+	cfg = ast_config_load(config);
+	if (!cfg) {
+		ast_log(LOG_WARNING,"Unable to load config for RADIUS CDR's: %s\n", config);
+		return 0;
+	}
+	
+	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(RC_CONFIG_FILE)) == NULL) {
-		ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", RC_CONFIG_FILE);
+	if ((rh = rc_read_config(radiuscfg)) == NULL) {
+		ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
 		return -1;
 	}
 



More information about the asterisk-commits mailing list