[Freeswitch-svn] [commit] r5212 - freeswitch/branches/cparker/src/mod/event_handlers/mod_radius_cdr

Freeswitch SVN cparker at freeswitch.org
Thu May 24 11:21:46 EDT 2007


Author: cparker
Date: Thu May 24 11:21:45 2007
New Revision: 5212

Modified:
   freeswitch/branches/cparker/src/mod/event_handlers/mod_radius_cdr/mod_radius_cdr.c

Log:
Add Start/Stop Functionality and new initializer function

Modified: freeswitch/branches/cparker/src/mod/event_handlers/mod_radius_cdr/mod_radius_cdr.c
==============================================================================
--- freeswitch/branches/cparker/src/mod/event_handlers/mod_radius_cdr/mod_radius_cdr.c	(original)
+++ freeswitch/branches/cparker/src/mod/event_handlers/mod_radius_cdr/mod_radius_cdr.c	Thu May 24 11:21:45 2007
@@ -45,7 +45,84 @@
 static char *		my_retries; /* 3 */
 static char		my_servers[SERVER_MAX][255];
 
-static switch_status_t my_on_hangup(switch_core_session_t *session)
+static rc_handle *	my_radius_init(void)
+{
+	rc_handle 	*rad_config;
+
+	rad_config = rc_new();
+
+	if (rad_config == NULL) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Error initializing rc_handle!\n");
+		return NULL;
+	}
+
+	rad_config = rc_config_init(rad_config);
+
+	if (rad_config == NULL) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Error initializing radius config!\n");
+		rc_destroy(rad_config);
+		return NULL;
+	}	
+
+	/* Some hardcoded ( for now ) defaults needed to initialize radius */
+	if (rc_add_config(rad_config, "auth_order", "radius", "mod_radius_cdr.c", 0) != 0) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+					"[mod_radius_cdr] failed setting auth_order = radius\n");
+		rc_destroy(rad_config);
+		return NULL;
+	}
+
+	if (rc_add_config(rad_config, "seqfile", "/var/run/radius.seq", "mod_radius_cdr.c", 0) != 0) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+					"failed setting seqfile =  /var/run/radius.seq\n");
+		rc_destroy(rad_config);
+		return NULL;
+	}
+
+
+	/* Add the module configs to initialize rad_config */
+
+	for (int i = 0; i < SERVER_MAX && my_servers[i][0] != '\0'; i++) {
+		if (rc_add_config(rad_config, "acctserver", my_servers[i], cf, 0) != 0) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setting acctserver = %s failed\n", my_servers[i]);
+			rc_destroy(rad_config);
+			return NULL;
+		}
+	}
+
+	if (rc_add_config(rad_config, "dictionary", my_dictionary, cf, 0) != 0) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+					"failed setting dictionary = %s failed\n", my_dictionary);
+		rc_destroy(rad_config);
+		return NULL;
+	}
+
+	if (rc_add_config(rad_config, "radius_timeout", my_timeout, cf, 0) != 0) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+				"failed setting radius_timeout = %s failed\n", my_timeout);
+		rc_destroy(rad_config);
+		return NULL;
+	}
+
+	if (rc_add_config(rad_config, "radius_retries", my_retries, cf, 0) != 0) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+				"failed setting radius_retries = %s failed\n", my_retries);
+		rc_destroy(rad_config);
+	        return NULL;
+	}
+
+	/* Read the dictionary file(s) */
+	if (rc_read_dictionary(rad_config, rc_conf_str(rad_config, "dictionary")) != 0) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+				"failed reading dictionary file(s): %s\n", my_dictionary);
+		rc_destroy(rad_config);
+		return NULL;
+	}
+
+	return rad_config;
+}
+
+static switch_status_t my_on_ring(switch_core_session_t *session)
 {
 	switch_xml_t 	cdr;
 	switch_channel_t *channel = switch_core_session_get_channel(session);
@@ -53,87 +130,141 @@
 	int             retval = 0;
 	VALUE_PAIR      *send = NULL;
 	UINT4           client_port = 0;
-	UINT4           status_type = PW_STATUS_STOP;
+	UINT4           status_type = PW_STATUS_START;
 
 	char 		*uuid_str;
 
-	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Entering my_on_hangup\n");
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Entering my_on_ring\n");
 
-	rad_config = rc_new();
+	rad_config = my_radius_init();
 
 	if (rad_config == NULL) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Error initializing rc_handle!\n");
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Error initializing radius, Start packet not logged.\n");
 		return SWITCH_STATUS_TERM;
 	}
 
-	rad_config = rc_config_init(rad_config);
+	if (switch_ivr_generate_xml_cdr(session, &cdr) == SWITCH_STATUS_SUCCESS) {
+		uuid_str = switch_core_session_get_uuid(session);
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Error Generating Data!\n");
+		return SWITCH_STATUS_TERM;		
+	}
 
-	if (rad_config == NULL) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Error initializing radius config!\n");
+	/* Create the radius packet */
+
+	/* Set Status Type */
+	if (rc_avpair_add(rad_config, &send, PW_ACCT_STATUS_TYPE, &status_type, -1, 0) == NULL) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Failed setting Acct-Status-Type: Start\n");
 		rc_destroy(rad_config);
 		return SWITCH_STATUS_TERM;
-	}	
+	}			
 
-	if (switch_ivr_generate_xml_cdr(session, &cdr) == SWITCH_STATUS_SUCCESS) {
-		uuid_str = switch_core_session_get_uuid(session);
+	if (rc_avpair_add(rad_config, &send, PW_ACCT_SESSION_ID, uuid_str, -1, 0) == NULL) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Failed adding Acct-Session-ID: %s\n", uuid_str);
+		rc_destroy(rad_config);
+		return SWITCH_STATUS_TERM;
+	}			
+
+	/* Add VSAs */
+
+	if(channel) {
+		/*switch_call_cause_t	cause;*/
+		switch_caller_profile_t	*profile;
 
-		/* Some hardcoded ( for now ) defaults needed to initialize radius */
-		if (rc_add_config(rad_config, "auth_order", "radius", "internal radius config", 0) != 0) {
-	                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
-					"failed setting auth_order = radius failed\n");
+		/*
+		cause = switch_channel_get_cause(channel);
+		if (rc_avpair_add(rad_config, &send, PW_FS_HANGUPCAUSE, &cause, -1, PW_FS_PEC) == NULL) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "failed adding Freeswitch-Hangupcause: %d\n", cause);
 			rc_destroy(rad_config);
-	                return SWITCH_STATUS_TERM;
+			return SWITCH_STATUS_TERM;
 		}
+		*/
+		
+		profile = switch_channel_get_caller_profile(channel);
 
-		if (rc_add_config(rad_config, "seqfile", "/var/run/radius.seq", "internal radius config", 0) != 0) {
-	                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
-					"failed setting seqfile =  /var/run/radius.seq\n");
-			rc_destroy(rad_config);
-	                return SWITCH_STATUS_TERM;
+		if(profile) {
+			if(profile->caller_id_number) {
+				if (rc_avpair_add(rad_config, &send, PW_FS_SRC, profile->caller_id_number, -1, PW_FS_PEC) == NULL) {
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+							"failed adding Freeswitch-Src: %s\n", profile->caller_id_number);
+					rc_destroy(rad_config);
+					return SWITCH_STATUS_TERM;
+				}
+			} 
+			if(profile->caller_id_name) {
+				if (rc_avpair_add(rad_config, &send, PW_FS_CLID, profile->caller_id_name, -1, PW_FS_PEC) == NULL) {
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+							"failed adding Freeswitch-CLID: %s\n", profile->caller_id_name);
+					rc_destroy(rad_config);
+					return SWITCH_STATUS_TERM;
+				}
+			} 
+			if(profile->destination_number) {
+				if (rc_avpair_add(rad_config, &send, PW_FS_DST, profile->destination_number, -1, PW_FS_PEC) == NULL) {
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+							"failed adding Freeswitch-Dst: %s\n", profile->destination_number);
+					rc_destroy(rad_config);
+					return SWITCH_STATUS_TERM;
+				}
+			} 
+			if(profile->dialplan) {
+				if (rc_avpair_add(rad_config, &send, PW_FS_DIALPLAN, profile->dialplan, -1, PW_FS_PEC) == NULL) {
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+							"failed adding Freeswitch-Dialplan: %s\n", profile->dialplan);
+					rc_destroy(rad_config);
+					return SWITCH_STATUS_TERM;
+				}
+			} 
+			if(profile->source) {
+				if (rc_avpair_add(rad_config, &send, PW_FS_LASTAPP, profile->source, -1, PW_FS_PEC) == NULL) {
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
+							"failed adding Freeswitch-Lastapp: %s\n", profile->source);
+					rc_destroy(rad_config);
+					return SWITCH_STATUS_TERM;
+				}
+			} 
+		} else {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "profile == NULL\n");
 		}
+	}
 
+	if(rc_acct(rad_config, client_port, send) == OK_RC) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] RADIUS Accounting OK\n");
+		retval = SWITCH_STATUS_SUCCESS;
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] RADIUS Accounting Failed\n");
+		retval = SWITCH_STATUS_TERM;
+	}
+	rc_avpair_free(send);
 
-		/* Add the module configs to initialize rad_config */
+	return(retval);
+}
 
-		for (int i = 0; i < SERVER_MAX && my_servers[i][0] != '\0'; i++) {
-			if (rc_add_config(rad_config, "acctserver", my_servers[i], cf, 0) != 0) {
-				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setting acctserver = %s failed\n", my_servers[i]);
-				rc_destroy(rad_config);
-				return SWITCH_STATUS_TERM;
-			}
-		}
+static switch_status_t my_on_hangup(switch_core_session_t *session)
+{
+	switch_xml_t 	cdr;
+	switch_channel_t *channel = switch_core_session_get_channel(session);
+	rc_handle 	*rad_config;
+	int             retval = 0;
+	VALUE_PAIR      *send = NULL;
+	UINT4           client_port = 0;
+	UINT4           status_type = PW_STATUS_STOP;
 
-		if (rc_add_config(rad_config, "dictionary", my_dictionary, cf, 0) != 0) {
-	                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
-					"failed setting dictionary = %s failed\n", my_dictionary);
-			rc_destroy(rad_config);
-	                return SWITCH_STATUS_TERM;
-		}
+	char 		*uuid_str;
 
-		if (rc_add_config(rad_config, "radius_timeout", my_timeout, cf, 0) != 0) {
-	                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
-					"failed setting radius_timeout = %s failed\n", my_timeout);
-			rc_destroy(rad_config);
-	                return SWITCH_STATUS_TERM;
-		}
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Entering my_on_hangup\n");
 
-		if (rc_add_config(rad_config, "radius_retries", my_retries, cf, 0) != 0) {
-	                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
-					"failed setting radius_retries = %s failed\n", my_retries);
-			rc_destroy(rad_config);
-	                return SWITCH_STATUS_TERM;
-		}
+	rad_config = my_radius_init();
 
-		/* Read the dictionary file(s) */
-		if (rc_read_dictionary(rad_config, rc_conf_str(rad_config, "dictionary")) != 0) {
-	                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
-					"failed reading dictionary file(s): %s\n", my_dictionary);
-			rc_destroy(rad_config);
-	                return SWITCH_STATUS_TERM;
-		}
+	if (rad_config == NULL) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Error initializing radius, session not logged.\n");
+		return SWITCH_STATUS_TERM;
+	}
 
+	if (switch_ivr_generate_xml_cdr(session, &cdr) == SWITCH_STATUS_SUCCESS) {
+		uuid_str = switch_core_session_get_uuid(session);
 	} else {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Generating Data!\n");
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "[mod_radius_cdr] Error Generating Data!\n");
 		return SWITCH_STATUS_TERM;		
 	}
 
@@ -283,7 +414,7 @@
 
 static const switch_state_handler_table_t state_handlers = {
 	/*.on_init */ NULL,
-	/*.on_ring */ NULL,
+	/*.on_ring */ my_on_ring,
 	/*.on_execute */ NULL,
 	/*.on_hangup */ my_on_hangup,
 	/*.on_loopback */ NULL,



More information about the Freeswitch-svn mailing list