view extensions/rt_debug/rt_debug.c @ 400:22f29007b931

Detect when extensions are loaded several times (not allowed)
author Sebastien Decugis <sdecugis@nict.go.jp>
date Tue, 02 Jun 2009 14:49:37 +0900
parents 8fdd47ce352a
children 860f41038ea2
line wrap: on
line source

/*********************************************************************************************************
* Software License Agreement (BSD License)                                                               *
* Author: Sebastien Decugis <sdecugis@nict.go.jp>							 *
*													 *
* Copyright (c) 2008, WIDE Project and NICT								 *
* All rights reserved.											 *
* 													 *
* Redistribution and use of this software in source and binary forms, with or without modification, are  *
* permitted provided that the following conditions are met:						 *
* 													 *
* * Redistributions of source code must retain the above 						 *
*   copyright notice, this list of conditions and the 							 *
*   following disclaimer.										 *
*    													 *
* * Redistributions in binary form must reproduce the above 						 *
*   copyright notice, this list of conditions and the 							 *
*   following disclaimer in the documentation and/or other						 *
*   materials provided with the distribution.								 *
* 													 *
* * Neither the name of the WIDE Project or NICT nor the 						 *
*   names of its contributors may be used to endorse or 						 *
*   promote products derived from this software without 						 *
*   specific prior written permission of WIDE Project and 						 *
*   NICT.												 *
* 													 *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
*********************************************************************************************************/

/* 
 * Debug-only extension for routing module; will simply install a callback for all kind of
 * routing callbacks and show the content of the message.
 */

#define IN_EXTENSION
#define DECLARE_API_POINTERS
#define DEFINE_DEBUG_MACRO	rt_debug
#include <waaad/waaad.h>

#include <stdio.h>
#include <errno.h>
#include <string.h>

#define DUMP_CMDFL_str	"%c%c%c%c"
#define DUMP_CMDFL_val(_val) (_val & CMD_FLAG_REQUEST)?'R':'-' , (_val & CMD_FLAG_PROXIABLE)?'P':'-' , (_val & CMD_FLAG_ERROR)?'E':'-' , (_val & CMD_FLAG_RETRANSMIT)?'T':'-'

static int rt_debug_verbosity = 1;

static rt_out_hdl_t * norm = NULL;
static rt_out_hdl_t * late = NULL;
static rt_fwd_hdl_t * req = NULL;
static rt_fwd_hdl_t * ans = NULL;
static rt_fwd_hdl_t * all = NULL;

/* Log some information about a message going through */
static void log_this_message(msg_t * msg, char * prefix)
{
	int ret = 0, parsed;
	msg_data_t * data = NULL;
	dict_object_t * dict = NULL;
	dict_cmd_data_t cmddata;
	
	memset(&cmddata, 0, sizeof(cmddata));
	
	ret = msg_data(msg, &data);
	if ( ret != 0) {
		log_debug("%s@%p Unable to retrieve data for message (%s)\n", prefix, msg, strerror(ret));
		return;
	}
	
	parsed = msg_parse_rules( msg, NULL);
	
	ret = msg_model ( msg, &dict );
	if ((ret == 0) && (dict != NULL))
		dict_getval ( dict, &cmddata );
	
	log_debug("%s@%p l:%u fl:" DUMP_CMDFL_str " cc:%u a:%u hbh:%x ete:%x n:'%s'\n",
			prefix,
			msg,
			data->msg_length,
			DUMP_CMDFL_val(data->msg_flags),
			data->msg_code,
			data->msg_appl,
			data->msg_hbhid,
			data->msg_eteid,
			cmddata.cmd_name ?: "(unresolved command)");
	
	return;
}

static void log_the_list(rt_dpl_t * list )
{
	rt_dpl_t * t = list;
	
	while (t != NULL) {
		int ret = 0;
		char * diamid = NULL;
		
		ret = peer_get( t->peer, PEER_DIAMETER_ID, (void *)&diamid );
		if (ret != 0)
			log_debug(" peer_list: @%p error getting id: %s\n", t, strerror(ret));
		else
			log_debug(" peer_list: @%p sc:%d id:'%s'\n", t, t->score, diamid);
		
		t = t->next;
	}
	
}

static int cb_out(void * data, msg_t * msg, rt_dpl_t * list )
{
	log_this_message(msg, data);
	log_the_list(list);
	return 0;
}

static int cb_fwd(void * data, msg_t * msg)
{
	log_this_message(msg, data);
	return 0;
}

static int entry(char * conffile)
{
	int ret = 0;
	
	ret = rt_out_register( cb_out, "(rt_debug)out,late: ", RT_OUT_LATE, &late );
	if (ret != 0) {
		log_debug("Unable to register callback out/late: %s", strerror(ret));
		return ret;
	}
	ret = rt_out_register( cb_out, "(rt_debug)out,normal: ", RT_OUT_NORMAL, &norm );
	if (ret != 0) {
		log_debug("Unable to register callback out/normal: %s", strerror(ret));
		return ret;
	}
	
	ret = rt_fwd_register( cb_fwd, "(rt_debug)fwd,ans: ", RT_FWD_ANS, &ans );
	if (ret != 0) {
		log_debug("Unable to register callback fwd/answers: %s", strerror(ret));
		return ret;
	}
	ret = rt_fwd_register( cb_fwd, "(rt_debug)fwd,all: ", RT_FWD_ALL, &all );
	if (ret != 0) {
		log_debug("Unable to register callback fwd/all: %s", strerror(ret));
		return ret;
	}
	ret = rt_fwd_register( cb_fwd, "(rt_debug)fwd,req: ", RT_FWD_REQ, &req );
	if (ret != 0) {
		log_debug("Unable to register callback fwd/requests: %s", strerror(ret));
		return ret;
	}
	
	TRACE_DEBUG(INFO, "Extension RT/Debug initialized");
	return ret;
}

void waaad_ext_fini(void)
{
	rt_out_unregister ( late );
	rt_out_unregister ( norm );
	rt_fwd_unregister ( ans );
	rt_fwd_unregister ( all );
	rt_fwd_unregister ( req );
}

EXTENSION_API_INIT(API_MODULE_MSG | API_MODULE_RT | API_MODULE_PEER | API_MODULE_DICTIONARY | API_MODULE_LOG, entry, "rt_debug", 1);
"Welcome to our mercurial repository"