Mercurial > hg > freeDiameter
diff freeDiameter/config.c @ 8:3e143f047f78
Backup for the week-end
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Fri, 18 Sep 2009 18:54:07 +0900 |
parents | |
children | c5c99c73c2bf |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/freeDiameter/config.c Fri Sep 18 18:54:07 2009 +0900 @@ -0,0 +1,189 @@ +/********************************************************************************************************* +* 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. * +*********************************************************************************************************/ + +#include "fD.h" + +/* Configuration management */ + +/* Initialize the fd_g_config structure to default values */ +int fd_conf_init() +{ + TRACE_ENTRY(); + + fd_g_config->eyec = EYEC_CONFIG; + fd_g_config->conf_file = DEFAULT_CONF_FILE; + + fd_g_config->loc_port = 3868; + fd_g_config->loc_port_tls = 3869; + fd_g_config->loc_sctp_str = 30; + fd_list_init(&fd_g_config->loc_endpoints, NULL); + + #ifdef DISABLE_SCTP + fd_g_config->flags.no_sctp = 1; + #endif /* DISABLE_SCTP */ + + fd_g_config->timer_tc = 30; + fd_g_config->timer_tw = 30; + + fd_g_config->or_state_id = (uint32_t) time(NULL); + + CHECK_FCT( fd_dict_init(&fd_g_config->g_dict) ); + CHECK_FCT( fd_fifo_new(&fd_g_config->g_fifo_main) ); + + return 0; +} + +void fd_conf_dump() +{ + if (!TRACE_BOOL(INFO)) + return; + + fd_log_debug("-- Configuration :\n"); + fd_log_debug(" Debug trace level ...... : %+d\n", fd_g_debug_lvl); + fd_log_debug(" Configuration file ..... : %s\n", fd_g_config->conf_file); + fd_log_debug(" Diameter Identity ...... : %s (l:%Zi)\n", fd_g_config->diam_id, fd_g_config->diam_id_len); + fd_log_debug(" Diameter Realm ......... : %s (l:%Zi)\n", fd_g_config->diam_realm, fd_g_config->diam_realm_len); + fd_log_debug(" Local port ............. : %hu\n", fd_g_config->loc_port); + fd_log_debug(" Local secure port ...... : %hu\n", fd_g_config->loc_port_tls); + fd_log_debug(" Number of SCTP streams . : %hu\n", fd_g_config->loc_sctp_str); + if (FD_IS_LIST_EMPTY(&fd_g_config->loc_endpoints)) { + fd_log_debug(" Local endpoints ........ : Default (use all available)\n"); + } else { + struct fd_list * li = fd_g_config->loc_endpoints.next; + fd_log_debug(" Local endpoints ........ : "); + while (li != &fd_g_config->loc_endpoints) { + struct fd_endpoint * ep = (struct fd_endpoint *)li; + if (li != fd_g_config->loc_endpoints.next) fd_log_debug(" "); + sSA_DUMP_NODE( &ep->ss, NI_NUMERICHOST ); + fd_log_debug("\n"); + li = li->next; + } + } + fd_log_debug(" Flags : - IP ........... : %s\n", fd_g_config->flags.no_ip4 ? "DISABLED" : "Enabled"); + fd_log_debug(" - IPv6 ......... : %s\n", fd_g_config->flags.no_ip6 ? "DISABLED" : "Enabled"); + fd_log_debug(" - Relay app .... : %s\n", fd_g_config->flags.no_fwd ? "DISABLED" : "Enabled"); + fd_log_debug(" - TCP .......... : %s\n", fd_g_config->flags.no_tcp ? "DISABLED" : "Enabled"); + #ifdef DISABLE_SCTP + fd_log_debug(" - SCTP ......... : DISABLED (at compilation)\n"); + #else /* DISABLE_SCTP */ + fd_log_debug(" - SCTP ......... : %s\n", fd_g_config->flags.no_sctp ? "DISABLED" : "Enabled"); + #endif /* DISABLE_SCTP */ + fd_log_debug(" - Pref. proto .. : %s\n", fd_g_config->flags.pr_tcp ? "TCP" : "SCTP"); + fd_log_debug(" - TLS method ... : %s\n", fd_g_config->flags.tls_alg ? "INBAND" : "Separate port"); + fd_log_debug(" Tc Timer ............... : %u\n", fd_g_config->timer_tc); + fd_log_debug(" Tw Timer ............... : %u\n", fd_g_config->timer_tw); + fd_log_debug(" Origin-State-Id ........ : %u\n", fd_g_config->or_state_id); +} + +/* Parse the configuration file (using the yacc parser) */ +int fd_conf_parse() +{ + extern FILE * fddin; + + TRACE_DEBUG (FULL, "Parsing configuration file: %s", fd_g_config->conf_file); + + fddin = fopen(fd_g_config->conf_file, "r"); + if (fddin == NULL) { + int ret = errno; + fprintf(stderr, "Unable to open configuration file %s for reading: %s\n", fd_g_config->conf_file, strerror(ret)); + return ret; + } + + /* call yacc parser */ + CHECK_FCT( fddparse(fd_g_config) ); + + /* close the file */ + fclose(fddin); + + /* Resolve hostname if not provided */ + if (fd_g_config->diam_id == NULL) { +#ifndef HOST_NAME_MAX +#define HOST_NAME_MAX 1024 +#endif /* HOST_NAME_MAX */ + char buf[HOST_NAME_MAX + 1]; + struct addrinfo hints, *info; + int ret; + + /* local host name */ + CHECK_SYS(gethostname(buf, sizeof(buf))); + + /* get FQDN */ + memset(&hints, 0, sizeof hints); + hints.ai_flags = AI_CANONNAME; + + ret = getaddrinfo(buf, NULL, &hints, &info); + if (ret != 0) { + fprintf(stderr, "Error resolving local FQDN :\n" + " '%s' : %s\n" + "Please provide LocalIdentity in configuration file.\n", + buf, gai_strerror(ret)); + return EINVAL; + } + CHECK_MALLOC( fd_g_config->diam_id = strdup(info->ai_canonname) ); + freeaddrinfo(info); + } + + /* cache the length of the diameter id for the session module */ + fd_g_config->diam_id_len = strlen(fd_g_config->diam_id); + + /* Handle the realm part */ + if (fd_g_config->diam_realm == NULL) { + char * start = NULL; + + /* Check the diameter identity is a fqdn */ + start = strchr(fd_g_config->diam_id, '.'); + if ((start == NULL) || (start[1] == '\0')) { + fprintf(stderr, "Unable to extract realm from the LocalIdentity '%s'.\n" + "Please fix your LocalIdentity setting or provide LocalRealm.\n", + fd_g_config->diam_id); + return EINVAL; + } + + CHECK_MALLOC( fd_g_config->diam_realm = strdup( start + 1 ) ); + } + fd_g_config->diam_realm_len = strlen(fd_g_config->diam_realm); + + /* Validate some flags */ + if (fd_g_config->flags.no_ip4 && fd_g_config->flags.no_ip6) { + fprintf(stderr, "IP and IPv6 cannot be disabled at the same time.\n"); + return EINVAL; + } + if (fd_g_config->flags.no_tcp && fd_g_config->flags.no_sctp) { + fprintf(stderr, "TCP and SCTP cannot be disabled at the same time.\n"); + return EINVAL; + } + + return 0; +}