Author: tjansen Date: Tue Oct 27 16:10:15 2009 New Revision: 1333 Log: Cosmetics: Moved cd_{,de}init() immediately before main(). This saves 5 forward declarations. Modified: trunk/pisacd/cdmain.c Modified: trunk/pisacd/cdmain.c ============================================================================== --- trunk/pisacd/cdmain.c Tue Oct 27 16:08:44 2009 (r1332) +++ trunk/pisacd/cdmain.c Tue Oct 27 16:10:15 2009 (r1333) @@ -35,8 +35,6 @@ #ifdef CONFIG_PISA_PERFORMANCE #include "pisaperf.h" -static void cd_perf_init(void); -static void cd_perf_destroy(void); #endif /* CONFIG_PISA_PERFORMANCE */ #define OPTS "f:i:p:q:bdvh" @@ -58,11 +56,8 @@ /** * Prototype declarations of functions. */ -static void cd_get_cmdargs(int argc, char **argv); static void cd_print_usage(char **argv); static void cd_print_version(void); -static void cd_reload_confs(int quitcode); -static void cd_quit(int quitcode); /** * A connection is removed permanently from the list. Clean up all associated @@ -80,123 +75,6 @@ } /** - * Initialize the basic settings before starting the main loop. - */ -static void cd_init(int argc, char *argv[]) -{ - /* Set signal handler for each signal */ - signal(SIGTERM, cd_quit); - signal(SIGINT, cd_quit); - signal(SIGQUIT, cd_quit); - signal(SIGILL, cd_quit); - signal(SIGHUP, cd_reload_confs); - signal(SIGPIPE, SIG_IGN); - signal(SIGBUS, cd_quit); - - /* Check and create lockfile */ - pisa_create_lock_file(PISACD_LOCK_FILE, 0); - - /* Set default values in context and config */ - cdctx_init(&cd_ctx); - cdconf_init(&cd_cfg); - - /* Receive and parse command line arguments. - * Note that command line args must be obtained before calling any - * cdconf_* operations except for cdconf_init. */ - cd_get_cmdargs(argc, argv); - - if (pisa_make_hipd_run() < 0) { - /* TODO: is there any other ways than stopping here? */ - PISA_ERROR("hipd cannot be executed. stop.\n"); - exit(EXIT_FAILURE); - } - -#ifdef CONFIG_PISA_PERFORMANCE - cd_perf_init(); -#endif - - /* set the default or command line specified debug level */ - cdconf_set_debuglevel(&cd_cfg); - - /* daemonize pisacd when running background mode */ - if (cd_ctx.is_bgrun) - pisa_daemonize(); - - cd_ctx.natlist = pisa_nat_init(); - - /* Setup configuration file. - * This is needed before any cdconf_read_*() operations. - */ - cdconf_setup_conffile(&cd_cfg); - - /* Read several configuration from pisacd.conf. */ - cdconf_read_basic_confs(&cd_cfg); - - cdconf_set_debuglevel(&cd_cfg); - - /* Make default sockets */ - cd_ctx.tunnel = pisa_tunnel_open_tundev(cd_ctx.ifname_tunnel, IFNAMSIZ); - cd_ctx.fd_pisaconf = pisa_conf_open_server_socket(PISA_CONF_PORT_CD); - - /* open peer sockets targeting the pisa server daemon, - * through the tunnel, using HIT as the target IPv6 address. */ - cd_ctx.tunc = pisa_tunnel_open_socket(cd_cfg.port_control); - cd_ctx.tund = pisa_tunnel_open_socket(cd_cfg.port_data); - - /* set handlers for tunnel control packet types */ - pisa_ctrlhandler_set(&cd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_REGISTER_ACK, pisa_recv_register_ack); - pisa_ctrlhandler_set(&cd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_DEREGISTER_ACK, pisa_recv_deregister_ack); - pisa_ctrlhandler_set(&cd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_HEARTBEAT_ACK, pisa_recv_heartbeat_ack); - - /* set handlers for config requests */ - memset(&conf_handle_func_set, 0, sizeof(conf_handle_func_set)); - conf_handle_func_set.debuglevel = pisa_conf_cb_debuglevel; - conf_handle_func_set.debugmask = pisa_conf_cb_debugmask;; - conf_handle_func_set.serveradd = pisa_cdconf_cb_server_add; - conf_handle_func_set.serviceadd = pisa_cdconf_cb_service_add; - -/* TODO: Read a config file, long term goal is to replace libconfig. - pisa_conf_read_file("foo", pisa_cdconf_parse); -*/ - - /* initialize the connection list */ - cd_ctx.conlist = pisa_conmgr_init(pisacd_cleanup_after_removed_connection); - - pisa_tunnel_configure_main(cd_ctx.ifname_tunnel, &cd_cfg.local_ipv4, &cd_cfg.local_netmask, MTU_TUN); - - /* Initialize the scheduler */ - pisa_sched_init(&cd_ctx.scheduler); -} - -static void cd_deinit(void) -{ - PISA_INFO("\nShutting down...\n"); - - pisa_conmgr_cleanup(&cd_ctx.conlist, cd_ctx.natlist); - - pisa_tunnel_remove_firewall_rules(cd_ctx.ifname_tunnel); - pisa_nat_destroy(cd_ctx.natlist); - cdctx_destroy(&cd_ctx); - cdconf_destroy(&cd_cfg); - pisa_arp_cleanup(); - pisa_sched_cleanup(&cd_ctx.scheduler); - - close(cd_ctx.fd_pisaconf); - close(cd_ctx.tunc); - close(cd_ctx.tund); - close(cd_ctx.tunnel); - - pisa_ctrlhandler_cleanup(&cd_ctx.ctrlhandlers); - - /* Remove lockfile */ - pisa_remove_lock_file(PISACD_LOCK_FILE); - -#ifdef CONFIG_PISA_PERFORMANCE - cd_perf_destroy(); -#endif -} - -/** * The scheduler signaled that something must be handled. Read the byte from * the pipe and run the scheduled task. */ @@ -463,6 +341,123 @@ #endif /* CONFIG_PISA_PERFORMANCE */ /** + * Initialize the basic settings before starting the main loop. + */ +static void cd_init(int argc, char *argv[]) +{ + /* Set signal handler for each signal */ + signal(SIGTERM, cd_quit); + signal(SIGINT, cd_quit); + signal(SIGQUIT, cd_quit); + signal(SIGILL, cd_quit); + signal(SIGHUP, cd_reload_confs); + signal(SIGPIPE, SIG_IGN); + signal(SIGBUS, cd_quit); + + /* Check and create lockfile */ + pisa_create_lock_file(PISACD_LOCK_FILE, 0); + + /* Set default values in context and config */ + cdctx_init(&cd_ctx); + cdconf_init(&cd_cfg); + + /* Receive and parse command line arguments. + * Note that command line args must be obtained before calling any + * cdconf_* operations except for cdconf_init. */ + cd_get_cmdargs(argc, argv); + + if (pisa_make_hipd_run() < 0) { + /* TODO: is there any other ways than stopping here? */ + PISA_ERROR("hipd cannot be executed. stop.\n"); + exit(EXIT_FAILURE); + } + +#ifdef CONFIG_PISA_PERFORMANCE + cd_perf_init(); +#endif + + /* set the default or command line specified debug level */ + cdconf_set_debuglevel(&cd_cfg); + + /* daemonize pisacd when running background mode */ + if (cd_ctx.is_bgrun) + pisa_daemonize(); + + cd_ctx.natlist = pisa_nat_init(); + + /* Setup configuration file. + * This is needed before any cdconf_read_*() operations. + */ + cdconf_setup_conffile(&cd_cfg); + + /* Read several configuration from pisacd.conf. */ + cdconf_read_basic_confs(&cd_cfg); + + cdconf_set_debuglevel(&cd_cfg); + + /* Make default sockets */ + cd_ctx.tunnel = pisa_tunnel_open_tundev(cd_ctx.ifname_tunnel, IFNAMSIZ); + cd_ctx.fd_pisaconf = pisa_conf_open_server_socket(PISA_CONF_PORT_CD); + + /* open peer sockets targeting the pisa server daemon, + * through the tunnel, using HIT as the target IPv6 address. */ + cd_ctx.tunc = pisa_tunnel_open_socket(cd_cfg.port_control); + cd_ctx.tund = pisa_tunnel_open_socket(cd_cfg.port_data); + + /* set handlers for tunnel control packet types */ + pisa_ctrlhandler_set(&cd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_REGISTER_ACK, pisa_recv_register_ack); + pisa_ctrlhandler_set(&cd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_DEREGISTER_ACK, pisa_recv_deregister_ack); + pisa_ctrlhandler_set(&cd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_HEARTBEAT_ACK, pisa_recv_heartbeat_ack); + + /* set handlers for config requests */ + memset(&conf_handle_func_set, 0, sizeof(conf_handle_func_set)); + conf_handle_func_set.debuglevel = pisa_conf_cb_debuglevel; + conf_handle_func_set.debugmask = pisa_conf_cb_debugmask;; + conf_handle_func_set.serveradd = pisa_cdconf_cb_server_add; + conf_handle_func_set.serviceadd = pisa_cdconf_cb_service_add; + +/* TODO: Read a config file, long term goal is to replace libconfig. + pisa_conf_read_file("foo", pisa_cdconf_parse); +*/ + + /* initialize the connection list */ + cd_ctx.conlist = pisa_conmgr_init(pisacd_cleanup_after_removed_connection); + + pisa_tunnel_configure_main(cd_ctx.ifname_tunnel, &cd_cfg.local_ipv4, &cd_cfg.local_netmask, MTU_TUN); + + /* Initialize the scheduler */ + pisa_sched_init(&cd_ctx.scheduler); +} + +static void cd_deinit(void) +{ + PISA_INFO("\nShutting down...\n"); + + pisa_conmgr_cleanup(&cd_ctx.conlist, cd_ctx.natlist); + + pisa_tunnel_remove_firewall_rules(cd_ctx.ifname_tunnel); + pisa_nat_destroy(cd_ctx.natlist); + cdctx_destroy(&cd_ctx); + cdconf_destroy(&cd_cfg); + pisa_arp_cleanup(); + pisa_sched_cleanup(&cd_ctx.scheduler); + + close(cd_ctx.fd_pisaconf); + close(cd_ctx.tunc); + close(cd_ctx.tund); + close(cd_ctx.tunnel); + + pisa_ctrlhandler_cleanup(&cd_ctx.ctrlhandlers); + + /* Remove lockfile */ + pisa_remove_lock_file(PISACD_LOCK_FILE); + +#ifdef CONFIG_PISA_PERFORMANCE + cd_perf_destroy(); +#endif +} + +/** * The main function of this client daemon. * * @param argc number of command arguments