view waaad/conf.c @ 386:31c6b1e0294d

Improved default strings
author Sebastien Decugis <sdecugis@nict.go.jp>
date Fri, 29 May 2009 12:57:01 +0900
parents e86dba02630a
children
line wrap: on
line source

/*********************************************************************************************************
* Software License Agreement (BSD License)                                                               *
* Author: Sebastien Decugis <sdecugis@nict.go.jp>							 *
*													 *
* Copyright (c) 2009, 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.								 *
*********************************************************************************************************/

/* Conf facility.
 *
 * This file handles the global configuration of the daemon.
 *
 * See conf.h for more information on the functions 
 *
 */

/* NOTE:
 *
 * In case a "reload configuration" function has to be written, this file may need to be made re-entrant.
 * It will require work on every place in the code of the daemon that uses "g_conf" and "g_pconf".
 */

#include "waaad-internal.h"

#include <arpa/inet.h>
#include <netdb.h>
 
waaad_conf_t * g_conf=NULL;

static waaad_conf_t waaad_config;

/* The Yacc parser prototype */
int waaadparse (waaad_conf_t * pconfig);

/* Set the default values in a configuration structure */
static int conf_setdefaults(waaad_conf_t * conf)
{
	/* Clear the configuration */
	memset(conf, 0, sizeof(waaad_conf_t));
	
	/* log to console */
	conf->log_to = LOG_TO_CONSOLE;
	
	/* The default configuration file path (from configure) */
	conf->filepath = DEFAULT_CONF_FILE;
	
	/* The default port */
	conf->pub.local_port = 3868;
	
	/* The default number of streams */
	conf->pub.sctp_streams = 30;
	
	/* The default tctimer and twtimer values */
	conf->pub.tctimer = 30;
	conf->pub.twtimer = 30;
	
	/* Initialize the origin-state-id value; it may be overwritten by an extension if state recovery is implemented */
	conf->pub.origin_state_id = (uint32_t) time(NULL);
	
	return 0;
}

/* dump a list of SA */
static void conf_dump_addr(char * name, sSS * array)
{
	if (!array) {
		TRACE_DEBUG( INFO, "  %-11s : -", name);
	} else {
		TRACE_DEBUG( INFO, "  %-11s :", name);
		sSS_DUMP_ARRAY(INFO, "                ", array);
	}
}

/* Output the parsed configuration */
static void conf_dump(void)
{
	char buf4[INET_ADDRSTRLEN];
	char buf6[INET6_ADDRSTRLEN];
	char *str4 = "0.0.0.0";
	char *str6 = "::";
	if (g_pconf->local_addr_diamid[0].ss_family == AF_INET) {
		sSA4 * sin = (sSA4 *)&g_pconf->local_addr_diamid[0];
		if (inet_ntop(sin->sin_family, &sin->sin_addr, buf4, sizeof(buf4))) {
			str4 = buf4;
		}
	}
	if (g_pconf->local_addr_diamid[1].ss_family == AF_INET6) {
		sSA6 * sin6 = (sSA6 *)&g_pconf->local_addr_diamid[1];
		if (inet_ntop(sin6->sin6_family, &sin6->sin6_addr, buf6, sizeof(buf6))) {
			str6 = buf6;
		}
	}
	
	TRACE_DEBUG( INFO, "--- Parsed configuration : ---");
	TRACE_DEBUG( INFO, "  Diameter-Id : %s ( %s / %s )", g_pconf->diameter_identity, str4, str6);
	TRACE_DEBUG( INFO, "        Realm : %s", g_pconf->diameter_realm);
	TRACE_DEBUG( INFO, "  Local port  : %hd",g_pconf->local_port);
	TRACE_DEBUG( INFO, "  SCTP streams: %hd",g_pconf->sctp_streams);
	TRACE_DEBUG( INFO, "  Tc timer    : %d", g_pconf->tctimer);
	TRACE_DEBUG( INFO, "  Tw timer    : %d", g_pconf->twtimer);
	TRACE_DEBUG( INFO, "  Relaying    : %s", g_pconf->disable_relay ? "Disabled" : "Enabled");
	TRACE_DEBUG( INFO, "  TCP         : %s", g_pconf->disable_tcp   ? "Disabled" : "Enabled");
	TRACE_DEBUG( INFO, "  IP          : %s", g_pconf->disable_inet4 ? "Disabled" : "Enabled");
	TRACE_DEBUG( INFO, "  IPv6        : %s", g_pconf->disable_inet6 ? "Disabled" : "Enabled");
	   conf_dump_addr( "Primary",   g_pconf->local_addr_pri_sa );
	   conf_dump_addr( "Secondary", g_pconf->local_addr_sec_sa );
	TRACE_DEBUG( INFO, "------------------------------");
}

/* Initialize the configuration module */
int conf_init ( void )
{
	/* Set the global pointer */
	g_conf = &waaad_config;
	
	/* Now set the default values */
	return conf_setdefaults(g_conf);
}


/* Parse the configuration file (using the yacc parser) */
int conf_parse ( void )
{
	extern FILE * waaadin;
	int ret;
	
	CHECK_PARAMS( g_conf );

	TRACE_DEBUG (FULL, "Parsing configuration file: %s...", g_conf->filepath);
	
	waaadin = fopen(g_conf->filepath, "r");
	if (waaadin == NULL) {
		ret = errno;
		log_error("Unable to open configuration file %s for reading: %s\n", g_conf->filepath, strerror(ret));
		TRACE_DEBUG (INFO, "Error occurred, message logged -- configuration file.");
		return ret;
	}
	
	/* call yacc parser */
	CHECK_FCT(  waaadparse(g_conf)  );

	/* close the file */
	fclose(waaadin);
	
	/* resolve the diameter-id, store result in local_addr_diamid */
	{
		struct addrinfo *ai = NULL, *p, hints;
		int gotip4 = 0;
		int gotip6 = 0;
		
		memset(&hints, 0, sizeof(hints));
		hints.ai_flags = AI_ADDRCONFIG;

		ret = getaddrinfo(g_pconf->diameter_identity, NULL, &hints, &ai);
		if (ret != 0) {
			log_error("Error resolving Diameter-Id '%s': %s.\n", g_pconf->diameter_identity, gai_strerror(ret));
			return EINVAL;
		}
		for (p = ai; p != NULL; p = p->ai_next) {
			switch (p->ai_family) {
				case AF_INET:
					if (gotip4)
						break;
					memcpy(&g_pconf->local_addr_diamid[0], p->ai_addr, sizeof(sSA4));
					gotip4++;
					break;
					
				case AF_INET6:
					if (gotip6)
						break;
					memcpy(&g_pconf->local_addr_diamid[1], p->ai_addr, sizeof(sSA6));
					gotip6++;
					break;
			}
			if (gotip4 && gotip6)
				break;
		}

		freeaddrinfo(ai);
	}
	
	/* cache the length of the diameter id for the session module */
	g_conf->diamid_len = strlen(g_pconf->diameter_identity);
	
	/* Handle the realm part of the localhost */
	if (g_pconf->diameter_realm == NULL) {
		CHECK_MALLOC( g_pconf->diameter_realm = strdup( strchr(g_pconf->diameter_identity, '.') + 1 )  ); 
	}
	g_conf->realm_len = strlen(g_pconf->diameter_realm);
	
	/* Dump the config and exit */
	conf_dump();

	return 0;
}	

/* Terminate the configuration module. */
int conf_fini ( void )
{
	if (g_pconf->diameter_identity)
		free(g_pconf->diameter_identity);
	
	if (g_pconf->local_addr_pri_sa)
		free(g_pconf->local_addr_pri_sa);
	
	if (g_pconf->local_addr_sec_sa)
		free(g_pconf->local_addr_sec_sa);
	
	return 0;
}
"Welcome to our mercurial repository"