view extensions/sample/sample.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 316bb3f38d04
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.								 *
*********************************************************************************************************/

/* 
 * Sample extension to test extensions loading in waaad
 */

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

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

static int inside_extension(char * conffile)
{
	log_normal("Hello from inside the extension!\n");
	
	log_normal("I am extension " __FILE__ " running on host %s\n", g_pconf->diameter_identity);
	
	if (conffile) {
		log_debug("I should parse my configuration file there: %s\n", conffile);
	} else {
		log_debug("I received no configuration file to parse\n");
	}
	
	/* Use the dictionary for test */
	log_normal("Let's create that 'Example-AVP'...\n");
	{
		dict_object_t * origin_host_avp = NULL;
		dict_object_t * session_id_avp = NULL;
		dict_object_t * example_avp_avp = NULL;
		dict_rule_data_t rule_data = { NULL, RULE_REQUIRED, -1, -1, 1 };
		dict_avp_data_t example_avp_data = { 999999, 0, "Example-AVP", AVP_FLAG_VENDOR , 0, AVP_TYPE_GROUPED };
		int ret = 0;

		ret = dict_search ( DICT_AVP, AVP_BY_NAME, "Origin-Host", &origin_host_avp, ENOENT);
		if (ret != 0) {
			log_error("An error occured while searching the 'Origin-Host' AVP: %s\n", strerror(ret));
			/* We don't return an error since this is just a sample */
			return 0;
		}
		ret = dict_search ( DICT_AVP, AVP_BY_NAME, "Session-Id", &session_id_avp, ENOENT);
		if (ret != 0) {
			log_error("An error occured while searching the 'Session-Id' AVP: %s\n", strerror(ret));
			/* We don't return an error since this is just a sample */
			return 0;
		}
		
		ret = dict_new ( DICT_AVP, &example_avp_data , NULL, &example_avp_avp );
		if (ret != 0) {
			log_error("An error occured while creating the 'Example-AVP' AVP: %s\n", strerror(ret));
			/* We don't return an error since this is just a sample */
			return 0;
		}
		
		rule_data.rule_avp = origin_host_avp;
		rule_data.rule_min = 1;
		rule_data.rule_max = 1;
		ret = dict_new ( DICT_RULE, &rule_data, example_avp_avp, NULL );
		if (ret != 0) {
			log_error("An error occured while creating the rule on 'Example-AVP' AVP for Origin-Host: %s\n", strerror(ret));
			/* We don't return an error since this is just a sample */
			return 0;
		}
		
		rule_data.rule_avp = session_id_avp;
		rule_data.rule_min = 1;
		rule_data.rule_max = -1;
		ret = dict_new ( DICT_RULE, &rule_data, example_avp_avp, NULL );
		if (ret != 0) {
			log_error("An error occured while creating the rule on 'Example-AVP' AVP for Session-Id: %s\n", strerror(ret));
			/* We don't return an error since this is just a sample */
			return 0;
		}
	}
	log_normal("'Example-AVP' created without error\n");
	
	return 0;
}

EXTENSION_API_INIT(API_MODULE_ALL, inside_extension, "sample", 1);
"Welcome to our mercurial repository"