Author: tjansen Date: Tue Oct 27 16:46:55 2009 New Revision: 1347 Log: Cosmetics: Moved sd_{,de}init before main(). Saves all forward declarations. Modified: trunk/pisasd/sdmain.c Modified: trunk/pisasd/sdmain.c ============================================================================== --- trunk/pisasd/sdmain.c Tue Oct 27 16:42:49 2009 (r1346) +++ trunk/pisasd/sdmain.c Tue Oct 27 16:46:55 2009 (r1347) @@ -71,18 +71,6 @@ extern pisa_conf_handle_func_set conf_handle_func_set; /** - * Prototype declarations of functions. - */ -static void sd_get_cmdargs(int argc, char **argv); -static void sd_reload_confs(pisa_conf_packet *p); -static void sd_quit(int quitcode); - -static void sd_perf_init(void); -static void sd_perf_destroy(void); - -extern void sdctx_init(); /* in sdctx.c */ - -/** * A connection is removed permanently from the list. Clean up all associated * state. * @@ -95,146 +83,6 @@ } /** - * Initialize the basic settings before starting the main loop. - */ -static void sd_init(int argc, char *argv[]) -{ - struct in_addr netmask; - - /* Set signal handler for each signal */ - signal(SIGTERM, sd_quit); - signal(SIGINT, sd_quit); - signal(SIGQUIT, sd_quit); - signal(SIGILL, sd_quit); - signal(SIGPIPE, SIG_IGN); - signal(SIGBUS, sd_quit); - - /* Check and create lockfile */ - pisa_create_lock_file(PISASD_LOCK_FILE, 0); - - /* Set default values in context and config */ - sdctx_init(&sd_ctx); - sdconf_init(&sd_cfg); - - /* Receive and parse command line arguments. - * Note that command line args must be obtained before calling any - * sdconf_* operations except for sdconf_init. */ - sd_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); - } - - sd_perf_init(); - - /* set the default or command line specified debug level */ - sdconf_set_debuglevel(&sd_cfg); - - /* daemonize pisasd when running background mode */ - if (sd_ctx.is_bgrun) - pisa_daemonize(); - - sd_ctx.natlist = pisa_nat_init(); - - /* Setup configuration file. - * This is needed before any sdconf_read_*() operations. - */ - sdconf_setup_conffile(&sd_cfg); -// Use this line to use seperate authorized_hosts.cfg config file for allowed_hosts: -// sdconf_setup_authorized_hosts_conffile(); -// Or this line to use the pisasd.cfg as before: - pisa_cfg_authorized_hosts_setup_file(sd_cfg.conffile); - - /* Read several configuration from pisasd.conf. */ - sdconf_read_basic_confs(&sd_cfg); - - sdconf_set_debuglevel(&sd_cfg); - - /* Make default sockets */ - sd_ctx.tunc = pisa_tunnel_open_socket(sd_cfg.port_control); - sd_ctx.tund = pisa_tunnel_open_socket(sd_cfg.port_data); - sd_ctx.fd_pisaconf = pisa_conf_open_server_socket(PISA_CONF_PORT_SD); - - /* Enable NAT if this process is a relay server */ - if (sd_cfg.is_relay == 1) - pisa_sdnat_start(); - - /* Enable IPv4 forwarding */ - pisa_forwarding_start(); - - /* Initialize Logfile */ -#ifdef CONFIG_PISA_LOGGING - if(!init_log(LOG_FILE)) - PISA_ERROR("Error opening logfile: " LOG_FILE "\n"); -#endif - - /* set handlers for tunnel control packet types */ - pisa_ctrlhandler_set(&sd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_DATA, pisa_recv_data); - pisa_ctrlhandler_set(&sd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_REGISTER, pisa_recv_register); - pisa_ctrlhandler_set(&sd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_HEARTBEAT, pisa_recv_heartbeat); - pisa_ctrlhandler_set(&sd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_DEREGISTER, pisa_recv_deregister); - - /* set handlers for config requests */ - memset(&conf_handle_func_set, 0, sizeof(conf_handle_func_set)); - conf_handle_func_set.reload_conf = sd_reload_confs; - conf_handle_func_set.debuglevel = pisa_conf_cb_debuglevel; - conf_handle_func_set.debugmask = pisa_conf_cb_debugmask; - - sd_ctx.conlist = pisa_conmgr_init(pisasd_cleanup_after_removed_connection); - - /* create the tunnel device and assign an IP address */ - inet_pton(AF_INET, "255.255.255.0", &netmask); - sd_ctx.tunnel = pisa_tunnel_open_tundev(sd_ctx.fd_pisa_tunnel_name, IFNAMSIZ); - pisa_tunnel_configure_main(sd_ctx.fd_pisa_tunnel_name, &sd_cfg.ipaddr, &netmask, MTU_TUN); - - /* Initialize the scheduler */ - pisa_sched_init(&sd_ctx.scheduler); -} - -/** - * Destroy the basic settings after finishing the main loop. - */ -static void sd_deinit(void) -{ - PISA_INFO("\nShutting down...\n"); - - pisa_tunnel_remove_firewall_rules(sd_ctx.fd_pisa_tunnel_name); - - pisa_cfg_authorized_hosts_cleanup(); - pisa_ctrlhandler_cleanup(&sd_ctx.ctrlhandlers); - pisa_conmgr_cleanup(&sd_ctx.conlist, sd_ctx.natlist); - pisa_nat_destroy(sd_ctx.natlist); - - if (sd_cfg.is_relay == 1) - pisa_sdnat_stop(); - - sdctx_destroy(&sd_ctx); - sdconf_destroy(&sd_cfg); - pisa_arp_cleanup(); - pisa_sched_cleanup(&sd_ctx.scheduler); - - /* finish all the remaining jobs */ - close(sd_ctx.tunc); - close(sd_ctx.tund); - close(sd_ctx.tunnel); - close(sd_ctx.fd_pisaconf); - - /* Disable IPv4 forwarding */ - pisa_forwarding_stop(); - - /* Remove lockfile */ - pisa_remove_lock_file(PISASD_LOCK_FILE); - -#ifdef CONFIG_PISA_LOGGING - close_log(); -#endif - - sd_perf_destroy(); -} - -/** * The scheduler signaled that something must be handled. Read the byte from * the pipe and run the scheduled task. */ @@ -506,6 +354,146 @@ } /** + * Initialize the basic settings before starting the main loop. + */ +static void sd_init(int argc, char *argv[]) +{ + struct in_addr netmask; + + /* Set signal handler for each signal */ + signal(SIGTERM, sd_quit); + signal(SIGINT, sd_quit); + signal(SIGQUIT, sd_quit); + signal(SIGILL, sd_quit); + signal(SIGPIPE, SIG_IGN); + signal(SIGBUS, sd_quit); + + /* Check and create lockfile */ + pisa_create_lock_file(PISASD_LOCK_FILE, 0); + + /* Set default values in context and config */ + sdctx_init(&sd_ctx); + sdconf_init(&sd_cfg); + + /* Receive and parse command line arguments. + * Note that command line args must be obtained before calling any + * sdconf_* operations except for sdconf_init. */ + sd_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); + } + + sd_perf_init(); + + /* set the default or command line specified debug level */ + sdconf_set_debuglevel(&sd_cfg); + + /* daemonize pisasd when running background mode */ + if (sd_ctx.is_bgrun) + pisa_daemonize(); + + sd_ctx.natlist = pisa_nat_init(); + + /* Setup configuration file. + * This is needed before any sdconf_read_*() operations. + */ + sdconf_setup_conffile(&sd_cfg); +// Use this line to use seperate authorized_hosts.cfg config file for allowed_hosts: +// sdconf_setup_authorized_hosts_conffile(); +// Or this line to use the pisasd.cfg as before: + pisa_cfg_authorized_hosts_setup_file(sd_cfg.conffile); + + /* Read several configuration from pisasd.conf. */ + sdconf_read_basic_confs(&sd_cfg); + + sdconf_set_debuglevel(&sd_cfg); + + /* Make default sockets */ + sd_ctx.tunc = pisa_tunnel_open_socket(sd_cfg.port_control); + sd_ctx.tund = pisa_tunnel_open_socket(sd_cfg.port_data); + sd_ctx.fd_pisaconf = pisa_conf_open_server_socket(PISA_CONF_PORT_SD); + + /* Enable NAT if this process is a relay server */ + if (sd_cfg.is_relay == 1) + pisa_sdnat_start(); + + /* Enable IPv4 forwarding */ + pisa_forwarding_start(); + + /* Initialize Logfile */ +#ifdef CONFIG_PISA_LOGGING + if(!init_log(LOG_FILE)) + PISA_ERROR("Error opening logfile: " LOG_FILE "\n"); +#endif + + /* set handlers for tunnel control packet types */ + pisa_ctrlhandler_set(&sd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_DATA, pisa_recv_data); + pisa_ctrlhandler_set(&sd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_REGISTER, pisa_recv_register); + pisa_ctrlhandler_set(&sd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_HEARTBEAT, pisa_recv_heartbeat); + pisa_ctrlhandler_set(&sd_ctx.ctrlhandlers, PISA_PKTTYPE_TUN_DEREGISTER, pisa_recv_deregister); + + /* set handlers for config requests */ + memset(&conf_handle_func_set, 0, sizeof(conf_handle_func_set)); + conf_handle_func_set.reload_conf = sd_reload_confs; + conf_handle_func_set.debuglevel = pisa_conf_cb_debuglevel; + conf_handle_func_set.debugmask = pisa_conf_cb_debugmask; + + sd_ctx.conlist = pisa_conmgr_init(pisasd_cleanup_after_removed_connection); + + /* create the tunnel device and assign an IP address */ + inet_pton(AF_INET, "255.255.255.0", &netmask); + sd_ctx.tunnel = pisa_tunnel_open_tundev(sd_ctx.fd_pisa_tunnel_name, IFNAMSIZ); + pisa_tunnel_configure_main(sd_ctx.fd_pisa_tunnel_name, &sd_cfg.ipaddr, &netmask, MTU_TUN); + + /* Initialize the scheduler */ + pisa_sched_init(&sd_ctx.scheduler); +} + +/** + * Destroy the basic settings after finishing the main loop. + */ +static void sd_deinit(void) +{ + PISA_INFO("\nShutting down...\n"); + + pisa_tunnel_remove_firewall_rules(sd_ctx.fd_pisa_tunnel_name); + + pisa_cfg_authorized_hosts_cleanup(); + pisa_ctrlhandler_cleanup(&sd_ctx.ctrlhandlers); + pisa_conmgr_cleanup(&sd_ctx.conlist, sd_ctx.natlist); + pisa_nat_destroy(sd_ctx.natlist); + + if (sd_cfg.is_relay == 1) + pisa_sdnat_stop(); + + sdctx_destroy(&sd_ctx); + sdconf_destroy(&sd_cfg); + pisa_arp_cleanup(); + pisa_sched_cleanup(&sd_ctx.scheduler); + + /* finish all the remaining jobs */ + close(sd_ctx.tunc); + close(sd_ctx.tund); + close(sd_ctx.tunnel); + close(sd_ctx.fd_pisaconf); + + /* Disable IPv4 forwarding */ + pisa_forwarding_stop(); + + /* Remove lockfile */ + pisa_remove_lock_file(PISASD_LOCK_FILE); + +#ifdef CONFIG_PISA_LOGGING + close_log(); +#endif + + sd_perf_destroy(); +} + +/** * The main function of this server daemon. * * @param argc number of command arguments