2013-09-16 09:53:25 +00:00
|
|
|
/* This file is part of Netsukuku
|
|
|
|
* (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
|
|
|
|
*
|
|
|
|
* This source code is free software; you can redistribute it and/or
|
2014-02-28 10:14:43 +00:00
|
|
|
* modify it under the terms of the GNU General Public License as published
|
2013-09-16 09:53:25 +00:00
|
|
|
* by the Free Software Foundation; either version 2 of the License,
|
|
|
|
* or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This source code is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* Please refer to the GNU Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Public License along with
|
|
|
|
* this source code; if not, write to:
|
|
|
|
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
* netsukuku.c:
|
|
|
|
* Where main() resides.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "libnetlink.h"
|
|
|
|
#include "ll_map.h"
|
|
|
|
#include "request.h"
|
|
|
|
#include "pkts.h"
|
|
|
|
#include "if.h"
|
|
|
|
#include "bmap.h"
|
|
|
|
#include "netsukuku.h"
|
|
|
|
#include "qspn.h"
|
|
|
|
#include "accept.h"
|
|
|
|
#include "daemon.h"
|
|
|
|
#include "crypto.h"
|
|
|
|
#include "andna_cache.h"
|
|
|
|
#include "andna.h"
|
|
|
|
#include "radar.h"
|
|
|
|
#include "hook.h"
|
|
|
|
#include "rehook.h"
|
2014-09-14 17:12:48 +00:00
|
|
|
#include "ntk-console-server.h"
|
2014-08-04 07:04:46 +00:00
|
|
|
#include <pthread.h>
|
2013-09-16 09:53:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
extern int errno;
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind, opterr, optopt;
|
|
|
|
|
|
|
|
int destroy_netsukuku_mutex;
|
|
|
|
int pid_saved;
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
int options_parsed = 0; /* How many times parse_options() has been called */
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
void
|
|
|
|
save_pid(void)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
FILE *fd;
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (!(fd = fopen(server_opt.pid_file, "w")))
|
2013-09-16 09:53:25 +00:00
|
|
|
error("Couldn't create pid file \"%s\": %s",
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.pid_file, strerror(errno));
|
2013-09-16 09:53:25 +00:00
|
|
|
fprintf(fd, "ntkd %ld\n", (long) getpid());
|
|
|
|
|
|
|
|
fclose(fd);
|
2014-09-17 12:41:24 +00:00
|
|
|
pid_saved = 1;
|
2013-09-16 09:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* is_ntkd_already_running
|
|
|
|
*
|
|
|
|
* Returns 1 if there's already a ntkd running
|
|
|
|
*/
|
2014-09-17 12:41:24 +00:00
|
|
|
int
|
|
|
|
is_ntkd_already_running(void)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
pid_t oldpid;
|
|
|
|
FILE *fd;
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (!(fd = fopen(server_opt.pid_file, "r"))) {
|
|
|
|
if (errno != ENOENT)
|
2014-02-28 10:14:43 +00:00
|
|
|
error("Cannot read pid file \"%s\": %s",
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.pid_file, strerror(errno));
|
2013-09-16 09:53:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fscanf(fd, "ntkd %d\n", &oldpid);
|
2014-09-17 12:41:24 +00:00
|
|
|
if (ferror(fd)) {
|
2014-02-28 10:14:43 +00:00
|
|
|
error("error reading pid file \"%s\": %s\n",
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.pid_file, strerror(errno));
|
2013-09-16 09:53:25 +00:00
|
|
|
fclose(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
return !kill(oldpid, 0);
|
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
int
|
|
|
|
ntk_load_maps(void)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
2014-09-17 12:41:24 +00:00
|
|
|
if (file_exist(server_opt.int_map_file) &&
|
|
|
|
(me.int_map = load_map(server_opt.int_map_file, &me.cur_node)))
|
2013-09-16 09:53:25 +00:00
|
|
|
debug(DBG_NORMAL, "Internal map loaded");
|
|
|
|
else
|
2014-09-17 12:41:24 +00:00
|
|
|
me.int_map = init_map(0);
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-02-28 10:14:43 +00:00
|
|
|
#if 0
|
2013-09-16 09:53:25 +00:00
|
|
|
/* Don't load the bnode map, it's useless */
|
2014-09-17 12:41:24 +00:00
|
|
|
if ((me.bnode_map = load_bmap(server_opt.bnode_map_file, me.ext_map,
|
|
|
|
FAMILY_LVLS, &me.bmap_nodes))) {
|
2013-09-16 09:53:25 +00:00
|
|
|
debug(DBG_NORMAL, "Bnode map loaded");
|
|
|
|
} else
|
|
|
|
#endif
|
2014-09-17 12:41:24 +00:00
|
|
|
bmap_levels_init(BMAP_LEVELS(FAMILY_LVLS), &me.bnode_map,
|
|
|
|
&me.bmap_nodes);
|
2014-02-28 10:14:43 +00:00
|
|
|
bmap_counter_init(BMAP_LEVELS(FAMILY_LVLS), &me.bmap_nodes_closed,
|
2014-09-17 12:41:24 +00:00
|
|
|
&me.bmap_nodes_opened);
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (file_exist(server_opt.ext_map_file) &&
|
|
|
|
(me.ext_map = load_extmap(server_opt.ext_map_file, &me.cur_quadg)))
|
2013-09-16 09:53:25 +00:00
|
|
|
debug(DBG_NORMAL, "External map loaded");
|
|
|
|
else
|
2014-09-17 12:41:24 +00:00
|
|
|
me.ext_map = init_extmap(FAMILY_LVLS, 0);
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
int
|
|
|
|
ntk_save_maps(void)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
debug(DBG_NORMAL, "Saving the internal map");
|
|
|
|
save_map(me.int_map, me.cur_node, server_opt.int_map_file);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
debug(DBG_NORMAL, "Saving the border nodes map");
|
2014-02-28 10:14:43 +00:00
|
|
|
save_bmap(me.bnode_map, me.bmap_nodes, me.ext_map, me.cur_quadg,
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.bnode_map_file);
|
2013-09-16 09:53:25 +00:00
|
|
|
#endif
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
debug(DBG_NORMAL, "Saving the external map");
|
2014-02-28 10:14:43 +00:00
|
|
|
save_extmap(me.ext_map, MAXGROUPNODE, &me.cur_quadg,
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.ext_map_file);
|
2013-09-16 09:53:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
int
|
|
|
|
ntk_free_maps(void)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
bmap_levels_free(me.bnode_map, me.bmap_nodes);
|
|
|
|
bmap_counter_free(me.bmap_nodes_closed, me.bmap_nodes_opened);
|
|
|
|
free_extmap(me.ext_map, FAMILY_LVLS, 0);
|
|
|
|
free_map(me.int_map, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
void
|
|
|
|
usage(void)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
printf("Usage:\n"
|
2014-09-17 12:41:24 +00:00
|
|
|
" ntkd [-hvaldrRD46] [-i net_interface] [-c conf_file] [-l logfile]\n\n"
|
|
|
|
" -4 ipv4\n"
|
|
|
|
" -6 ipv6\n"
|
|
|
|
" -i Specify the interface after this\n\n"
|
|
|
|
" -a Prevents running the ANDNA daemon\n"
|
|
|
|
" -R Prevents editting /etc/resolv.conf\n"
|
|
|
|
" -D Prevents running as daemon (Does not fork to the background)\n"
|
|
|
|
"\n"
|
|
|
|
" -r Runs in restricted mode\n"
|
|
|
|
" -I Share your internet connection with other nodes\n"
|
|
|
|
"\n"
|
|
|
|
" -c configuration file\n"
|
|
|
|
" -l Enables logging to file\n"
|
|
|
|
"\n"
|
|
|
|
" -d Debug (Add more ds to get more info)\n"
|
|
|
|
" -h Shows this help\n"
|
|
|
|
" -v Shows the version you are using\n"
|
|
|
|
" -k Kills the running instance of ntkd\n"
|
2014-11-06 23:44:02 +00:00
|
|
|
" -C Runs the console server for Ntk-Console to connect to\n"
|
|
|
|
" -e Excludes an interface from usage I.E all interfaces except this one\n"
|
|
|
|
" -n Experimental, Currently meaningless argument\n to implement ntk netsplit\n http://netsukuku.freaknet.org/docs/main_doc/ntk_rfc/Ntk_net_split\n");
|
2013-09-16 09:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fill_default_options: fills the default values in the server_opt struct
|
|
|
|
*/
|
2014-09-17 12:41:24 +00:00
|
|
|
void
|
|
|
|
fill_default_options(void)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
setzero(&server_opt, sizeof(server_opt));
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.family = AF_INET;
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.config_file = NTK_CONFIG_FILE;
|
|
|
|
server_opt.pid_file = NTK_PID_FILE;
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.int_map_file = INT_MAP_FILE;
|
|
|
|
server_opt.ext_map_file = EXT_MAP_FILE;
|
|
|
|
server_opt.bnode_map_file = BNODE_MAP_FILE;
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.andna_hnames_file = ANDNA_HNAMES_FILE;
|
|
|
|
server_opt.snsd_nodes_file = SNSD_NODES_FILE;
|
|
|
|
server_opt.andna_cache_file = ANDNA_CACHE_FILE;
|
|
|
|
server_opt.lclkey_file = LCLKEY_FILE;
|
|
|
|
server_opt.lcl_file = LCL_FILE;
|
|
|
|
server_opt.rhc_file = RHC_FILE;
|
|
|
|
server_opt.counter_c_file = COUNTER_C_FILE;
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.daemon = 1;
|
|
|
|
server_opt.dbg_lvl = 0;
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.disable_andna = 0;
|
|
|
|
server_opt.disable_resolvconf = 0;
|
|
|
|
server_opt.restricted = 0;
|
|
|
|
server_opt.restricted_class = 0;
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.use_shared_inet = 1;
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.ip_masq_script = IPMASQ_SCRIPT_FILE;
|
|
|
|
server_opt.tc_shaper_script = TCSHAPER_SCRIPT_FILE;
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.max_connections = MAX_CONNECTIONS;
|
|
|
|
server_opt.max_accepts_per_host = MAX_ACCEPTS;
|
|
|
|
server_opt.max_accepts_per_host_time = FREE_ACCEPT_TIME;
|
2013-09-16 09:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fill_loaded_cfg_options
|
|
|
|
*
|
|
|
|
* stores in server_opt the options loaded from the configuration file
|
|
|
|
*/
|
2014-09-17 12:41:24 +00:00
|
|
|
void
|
|
|
|
fill_loaded_cfg_options(void)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
char *value;
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
CONF_GET_STRN_VALUE(CONF_NTK_INT_MAP_FILE, &server_opt.int_map_file,
|
|
|
|
NAME_MAX - 1);
|
|
|
|
CONF_GET_STRN_VALUE(CONF_NTK_BNODE_MAP_FILE,
|
|
|
|
&server_opt.bnode_map_file, NAME_MAX - 1);
|
|
|
|
CONF_GET_STRN_VALUE(CONF_NTK_EXT_MAP_FILE, &server_opt.ext_map_file,
|
|
|
|
NAME_MAX - 1);
|
|
|
|
|
|
|
|
CONF_GET_STRN_VALUE(CONF_ANDNA_HNAMES_FILE,
|
|
|
|
&server_opt.andna_hnames_file, NAME_MAX - 1);
|
|
|
|
CONF_GET_STRN_VALUE(CONF_SNSD_NODES_FILE, &server_opt.snsd_nodes_file,
|
|
|
|
NAME_MAX - 1);
|
|
|
|
|
|
|
|
CONF_GET_STRN_VALUE(CONF_ANDNA_CACHE_FILE,
|
|
|
|
&server_opt.andna_cache_file, NAME_MAX - 1);
|
|
|
|
CONF_GET_STRN_VALUE(CONF_ANDNA_LCLKEY_FILE, &server_opt.lclkey_file,
|
|
|
|
NAME_MAX - 1);
|
|
|
|
CONF_GET_STRN_VALUE(CONF_ANDNA_LCL_FILE, &server_opt.lcl_file,
|
|
|
|
NAME_MAX - 1);
|
|
|
|
CONF_GET_STRN_VALUE(CONF_ANDNA_RHC_FILE, &server_opt.rhc_file,
|
|
|
|
NAME_MAX - 1);
|
|
|
|
CONF_GET_STRN_VALUE(CONF_ANDNA_COUNTER_C_FILE,
|
|
|
|
&server_opt.counter_c_file, NAME_MAX - 1);
|
|
|
|
|
|
|
|
CONF_GET_STRN_VALUE(CONF_NTK_PID_FILE, &server_opt.pid_file,
|
|
|
|
NAME_MAX - 1);
|
|
|
|
CONF_GET_INT_VALUE(CONF_NTK_MAX_CONNECTIONS,
|
|
|
|
server_opt.max_connections);
|
|
|
|
CONF_GET_INT_VALUE(CONF_NTK_MAX_ACCEPTS_PER_HOST,
|
|
|
|
server_opt.max_accepts_per_host);
|
|
|
|
CONF_GET_INT_VALUE(CONF_NTK_MAX_ACCEPTS_PER_HOST_TIME,
|
|
|
|
server_opt.max_accepts_per_host_time);
|
|
|
|
|
|
|
|
CONF_GET_INT_VALUE(CONF_DISABLE_ANDNA, server_opt.disable_andna);
|
|
|
|
CONF_GET_INT_VALUE(CONF_DISABLE_RESOLVCONF,
|
|
|
|
server_opt.disable_resolvconf);
|
|
|
|
CONF_GET_INT_VALUE(CONF_NTK_RESTRICTED_MODE, server_opt.restricted);
|
|
|
|
CONF_GET_INT_VALUE(CONF_NTK_RESTRICTED_CLASS,
|
|
|
|
server_opt.restricted_class);
|
|
|
|
|
|
|
|
CONF_GET_INT_VALUE(CONF_NTK_INTERNET_CONNECTION,
|
|
|
|
server_opt.inet_connection);
|
|
|
|
if ((value = CONF_GET_VALUE(CONF_NTK_INTERNET_GW))) {
|
|
|
|
if (str_to_inet_gw(value, &server_opt.inet_gw,
|
|
|
|
&server_opt.inet_gw_dev))
|
|
|
|
fatal
|
|
|
|
("Malformed `%s' option: \"%s\". Its syntax is \"IP:dev\"",
|
|
|
|
config_str[CONF_NTK_INTERNET_GW], value);
|
2013-09-16 09:53:25 +00:00
|
|
|
}
|
2014-09-17 12:41:24 +00:00
|
|
|
CONF_GET_INT_VALUE(CONF_NTK_INTERNET_UPLOAD, server_opt.my_upload_bw);
|
|
|
|
CONF_GET_INT_VALUE(CONF_NTK_INTERNET_DOWNLOAD,
|
|
|
|
server_opt.my_dnload_bw);
|
|
|
|
if (server_opt.my_upload_bw && server_opt.my_dnload_bw)
|
2013-09-16 09:53:25 +00:00
|
|
|
me.my_bandwidth =
|
2014-09-17 12:41:24 +00:00
|
|
|
bandwidth_in_8bit((server_opt.my_upload_bw +
|
|
|
|
server_opt.my_dnload_bw) / 2);
|
|
|
|
|
|
|
|
if ((value = CONF_GET_VALUE(CONF_NTK_INTERNET_PING_HOSTS))) {
|
|
|
|
server_opt.inet_hosts = parse_internet_hosts(value,
|
|
|
|
&server_opt.
|
|
|
|
inet_hosts_counter);
|
|
|
|
if (!server_opt.inet_hosts)
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("Malformed `%s' option: \"%s\". "
|
2014-09-17 12:41:24 +00:00
|
|
|
"Its syntax is host1:host2:...",
|
|
|
|
config_str[CONF_NTK_INTERNET_PING_HOSTS], value);
|
2013-09-16 09:53:25 +00:00
|
|
|
}
|
2014-09-17 12:41:24 +00:00
|
|
|
CONF_GET_INT_VALUE(CONF_SHARE_INTERNET, server_opt.share_internet);
|
|
|
|
CONF_GET_INT_VALUE(CONF_SHAPE_INTERNET, server_opt.shape_internet);
|
|
|
|
CONF_GET_INT_VALUE(CONF_USE_SHARED_INET, server_opt.use_shared_inet);
|
|
|
|
CONF_GET_STRN_VALUE(CONF_NTK_IP_MASQ_SCRIPT,
|
|
|
|
&server_opt.ip_masq_script, NAME_MAX - 1);
|
|
|
|
CONF_GET_STRN_VALUE(CONF_NTK_TC_SHAPER_SCRIPT,
|
|
|
|
&server_opt.tc_shaper_script, NAME_MAX - 1);
|
2013-09-16 09:53:25 +00:00
|
|
|
|
|
|
|
/* Clean the enviroment */
|
|
|
|
clear_config_env();
|
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
void
|
|
|
|
free_server_opt(void)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
int i;
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.config_file != NTK_CONFIG_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.config_file);
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.pid_file != NTK_PID_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.pid_file);
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.int_map_file != INT_MAP_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.int_map_file);
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.ext_map_file != EXT_MAP_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.ext_map_file);
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.bnode_map_file != BNODE_MAP_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.bnode_map_file);
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.andna_hnames_file != ANDNA_HNAMES_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.andna_hnames_file);
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.snsd_nodes_file != SNSD_NODES_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.snsd_nodes_file);
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.andna_cache_file != ANDNA_CACHE_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.andna_cache_file);
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.lclkey_file != LCLKEY_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.lclkey_file);
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.lcl_file != LCL_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.lcl_file);
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.rhc_file != RHC_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.rhc_file);
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.counter_c_file != COUNTER_C_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.counter_c_file);
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.ip_masq_script != IPMASQ_SCRIPT_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.ip_masq_script);
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.tc_shaper_script != TCSHAPER_SCRIPT_FILE)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.tc_shaper_script);
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.inet_gw_dev)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.inet_gw_dev);
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
for (i = 0; i < MAX_INTERFACES && server_opt.ifs[i]; i++)
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(server_opt.ifs[i]);
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:04:57 +00:00
|
|
|
/* Removes specified existing interface, Ntkd should populate the device list
|
|
|
|
* prior to this.
|
|
|
|
* returns 0 on success, And closes ntkd on error.
|
|
|
|
*/
|
I added a lot of stuff! I fully set up exclude_interface to ignore inactive interfaces, and optarg, Along with a small list of other interfaces. I did some bug fixing, And looked into how netsukuku sets it's interfaces by default, And added some stuff to deal with that too.
I, Also, Figured out that, The entire exclude_interface function is probably unnecessary, And can be fully replaced with just check_excluded, Which would be much simpler, Easier, And elegant. Along with the fact that, It will probably just work.
2014-05-27 03:21:17 +00:00
|
|
|
|
2014-10-27 18:04:57 +00:00
|
|
|
int
|
|
|
|
exclude_interface(void)
|
2014-09-17 12:41:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
2014-10-29 01:33:01 +00:00
|
|
|
|
|
|
|
printf("Number of Interfaces in Use: %d\n", me.cur_ifs_n);
|
|
|
|
|
|
|
|
for (i = 0; i < me.cur_ifs_n; i++)
|
|
|
|
printf("Interface names in Use: %s", me.cur_ifs[i].dev_name);
|
2014-09-17 12:41:24 +00:00
|
|
|
|
2014-10-27 18:04:57 +00:00
|
|
|
for (i = 0; i < me.cur_ifs_n; i++) {
|
2014-10-28 23:39:08 +00:00
|
|
|
if (strcmp(me.cur_ifs[i].dev_name, optarg) == 0) {
|
2014-09-17 12:41:24 +00:00
|
|
|
printf("Interface %s removed, And replaced with %s",
|
2014-10-28 23:39:08 +00:00
|
|
|
me.cur_ifs[i].dev_name, me.cur_ifs[me.cur_ifs_n].dev_name);
|
2014-10-27 18:04:57 +00:00
|
|
|
ifs_del(me.cur_ifs, &me.cur_ifs_n, i);
|
|
|
|
return 0;
|
2014-09-17 12:41:24 +00:00
|
|
|
}
|
2014-10-29 01:33:01 +00:00
|
|
|
else
|
|
|
|
fatal("Interface %s not found!", optarg);
|
2014-09-17 12:41:24 +00:00
|
|
|
}
|
2014-05-26 09:28:30 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 17:12:48 +00:00
|
|
|
void
|
2014-09-17 12:41:24 +00:00
|
|
|
ntk_thread_creatation(void)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
pthread_t console_recv_send_thread;
|
|
|
|
if (pthread_create
|
|
|
|
(&console_recv_send_thread, NULL, &console_recv_send, &x)) {
|
|
|
|
fprintf(stderr, "Error creating thread\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2014-08-04 07:04:46 +00:00
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
void
|
|
|
|
parse_options(int argc, char **argv)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
2014-09-17 12:41:24 +00:00
|
|
|
int c, saved_argc = argc;
|
|
|
|
optind = 0;
|
2013-09-16 09:53:25 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0;
|
|
|
|
static struct option long_options[] = {
|
2014-09-17 12:41:24 +00:00
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{"iface", 1, 0, 'i'},
|
|
|
|
{"ipv6", 0, 0, '6'},
|
|
|
|
{"ipv4", 0, 0, '4'},
|
|
|
|
|
|
|
|
{"conf", 1, 0, 'c'},
|
|
|
|
{"logfile", 1, 0, 'l'},
|
|
|
|
|
|
|
|
{"no_andna", 0, 0, 'a'},
|
|
|
|
{"no_daemon", 0, 0, 'D'},
|
|
|
|
{"no_resolv", 0, 0, 'R'},
|
|
|
|
|
|
|
|
{"restricted", 0, 0, 'r'},
|
|
|
|
{"share-inet", 0, 0, 'I'},
|
|
|
|
|
|
|
|
{"debug", 0, 0, 'd'},
|
|
|
|
{"version", 0, 0, 'v'},
|
|
|
|
{"kill", 0, 0, 'k'},
|
|
|
|
{"exclude", 1, 0, 'e'},
|
|
|
|
{"console", 0, 0, 'C'},
|
2014-11-06 23:44:02 +00:00
|
|
|
{"netsplit", 1, 0, 'n'},
|
2013-09-16 09:53:25 +00:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
c = getopt_long(argc, argv, "i:c:l:e:hvd64DRrIakC", long_options,
|
|
|
|
&option_index);
|
2013-09-16 09:53:25 +00:00
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
switch (c) {
|
2014-11-06 23:44:02 +00:00
|
|
|
case 'n':
|
|
|
|
|
|
|
|
break;
|
2014-09-17 12:41:24 +00:00
|
|
|
case 'C':
|
|
|
|
ntk_thread_creatation();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
printf("%s\n", VERSION_STR);
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
exclude_interface();
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
if (is_ntkd_already_running() == 1) {
|
|
|
|
char process_name[256] = { 0 };
|
|
|
|
pid_t pid;
|
|
|
|
printf("...Closing ntkd...\n");
|
|
|
|
FILE *fd = fopen(server_opt.pid_file, "r");
|
|
|
|
while (fscanf(fd, "%s %d", process_name, &pid) != EOF) {
|
|
|
|
if (strcmp(process_name, "ntkd") == 0) {
|
|
|
|
kill(pid, SIGINT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fd);
|
2014-02-28 10:14:43 +00:00
|
|
|
exit(0);
|
2014-09-17 12:41:24 +00:00
|
|
|
} else if (is_ntkd_already_running() == 0) {
|
|
|
|
printf("ntkd is not running\n ...Exiting...\n");
|
2013-09-16 09:53:25 +00:00
|
|
|
exit(0);
|
2014-09-17 12:41:24 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
server_opt.family = AF_INET;
|
|
|
|
break;
|
|
|
|
case '6':
|
2013-09-16 09:53:25 +00:00
|
|
|
#ifdef IPV6_DISABLED
|
2014-09-17 12:41:24 +00:00
|
|
|
fatal("The ipv6 is still not supported");
|
2013-09-16 09:53:25 +00:00
|
|
|
#endif
|
2014-09-17 12:41:24 +00:00
|
|
|
loginfo
|
|
|
|
("WARNING: The ipv6 support is still experimental and under "
|
|
|
|
"development, nothing is assured to work.");
|
|
|
|
server_opt.family = AF_INET6;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
server_opt.config_file = xstrndup(optarg, NAME_MAX - 1);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if (log_to_file(optarg) < 0)
|
|
|
|
fatal(0);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
server_opt.daemon = 0;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
server_opt.disable_andna = 1;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
server_opt.disable_resolvconf = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
server_opt.restricted = 1;
|
2013-09-16 09:53:25 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* This is a very dirty hack, but it handles
|
|
|
|
* the optional argument better than getopt.
|
|
|
|
*
|
|
|
|
* This is the problem:
|
|
|
|
* ntkd -abcrdefg
|
2014-02-28 10:14:43 +00:00
|
|
|
* If 'r' is an element that specifies an
|
2013-09-16 09:53:25 +00:00
|
|
|
* optional argument, then the "defg" string
|
|
|
|
* is taken as its arg, but this is not what
|
|
|
|
* we want because in this case '-r' is
|
|
|
|
* specified without its argument.
|
|
|
|
*
|
|
|
|
* So, what we do is checking if the argv
|
|
|
|
* next to the arg of 'r' begins with a '-'
|
|
|
|
* or not. If not, it is the option of '-r'
|
|
|
|
*
|
|
|
|
* The only thing that won't work is:
|
|
|
|
* ntkd -rOPTION
|
|
|
|
* it has to be specified in this way:
|
|
|
|
* ntkd -r OPTION
|
|
|
|
*/
|
2014-09-17 12:41:24 +00:00
|
|
|
if (argc > optind && argv[optind][0] != '-') {
|
|
|
|
server_opt.restricted_class = atoi(argv[optind]);
|
|
|
|
saved_argc--;
|
|
|
|
}
|
|
|
|
/**/ break;
|
|
|
|
case 'I':
|
|
|
|
server_opt.share_internet = 1;
|
|
|
|
if (!server_opt.restricted) {
|
|
|
|
loginfo("Share_internet=1. Assuming restricted=1");
|
|
|
|
server_opt.restricted = 1;
|
|
|
|
}
|
|
|
|
if (!server_opt.inet_connection) {
|
|
|
|
loginfo("Share_internet=1. Assuming inet_connection=1");
|
|
|
|
server_opt.inet_connection = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
server_opt.dbg_lvl++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
break;
|
2013-09-16 09:53:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < saved_argc && !options_parsed) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
options_parsed++;
|
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
void
|
|
|
|
check_conflicting_options(void)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
#define FATAL_NOT_SPECIFIED(str) fatal("You didn't specified the `%s' " \
|
|
|
|
"option in netsukuku.conf", \
|
|
|
|
(str)); \
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (!server_opt.pid_file)
|
2013-09-16 09:53:25 +00:00
|
|
|
FATAL_NOT_SPECIFIED("pid_file");
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (!server_opt.inet_hosts && server_opt.restricted)
|
2013-09-16 09:53:25 +00:00
|
|
|
FATAL_NOT_SPECIFIED("internet_ping_hosts");
|
|
|
|
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.restricted && server_opt.share_internet &&
|
|
|
|
!file_exist(server_opt.ip_masq_script))
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("ip_masquerade_script \"%s\" is inexistent",
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.ip_masq_script);
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.shape_internet &&
|
|
|
|
!file_exist(server_opt.tc_shaper_script))
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("tc_shaper_script \"%s\" is inexistent",
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.ip_masq_script);
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (!server_opt.restricted && server_opt.inet_connection)
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("inet_connection=1 but ntk_restricted_mode=0. If you "
|
2014-09-17 12:41:24 +00:00
|
|
|
"want to be compatible with the Internet, "
|
|
|
|
"set the restricted mode in the options");
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (!server_opt.restricted && (server_opt.share_internet))
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("You want to share your Internet connection,"
|
2014-09-17 12:41:24 +00:00
|
|
|
"but I am not running in restricted mode (-r), "
|
|
|
|
"'cause I'm not sure of what you want... " "I'm aborting.");
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.share_internet && me.my_bandwidth < MIN_CONN_BANDWIDTH)
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("You want to share your Internet connection but "
|
2014-09-17 12:41:24 +00:00
|
|
|
"your bandwidth is just TOO small. Do not share "
|
|
|
|
"it, or your connection will be saturated");
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (!server_opt.inet_connection && server_opt.share_internet) {
|
2013-09-16 09:53:25 +00:00
|
|
|
loginfo("You want to share your Internet connection,"
|
2014-09-17 12:41:24 +00:00
|
|
|
"but `internet_connection' is set to 0."
|
|
|
|
"We are assuming it is 1");
|
|
|
|
server_opt.inet_connection = 1;
|
2013-09-16 09:53:25 +00:00
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.shape_internet && !server_opt.inet_connection)
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("The Internet traffic shaping option is set, but "
|
2014-09-17 12:41:24 +00:00
|
|
|
"the `internet_connection' is set to 0, please check "
|
|
|
|
"your options.");
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
#ifdef IPV6_DISABLED
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.inet_gw.family == AF_INET6)
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("Ipv6 is not supported");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
void
|
|
|
|
init_netsukuku(char **argv)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
xsrand();
|
2014-09-17 12:41:24 +00:00
|
|
|
|
|
|
|
if (geteuid())
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("Need root privileges");
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
destroy_netsukuku_mutex = pid_saved = 0;
|
|
|
|
sigterm_timestamp = sighup_timestamp = sigalrm_timestamp = 0;
|
2013-09-16 09:53:25 +00:00
|
|
|
setzero(&me, sizeof(struct current_globals));
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (is_ntkd_already_running())
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("ntkd is already running. If it is not, remove \"%s\"",
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.pid_file);
|
2013-09-16 09:53:25 +00:00
|
|
|
else
|
|
|
|
save_pid();
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
my_family = server_opt.family;
|
|
|
|
restricted_mode = server_opt.restricted;
|
|
|
|
restricted_class =
|
|
|
|
server_opt.restricted_class ? RESTRICTED_172 : RESTRICTED_10;
|
2013-09-16 09:53:25 +00:00
|
|
|
|
|
|
|
/* Check if the DATA_DIR exists, if not create it */
|
2014-09-17 12:41:24 +00:00
|
|
|
if (check_and_create_dir(DATA_DIR))
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("Cannot access to the %s directory. Exiting.", DATA_DIR);
|
2014-02-28 10:14:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Device initialization
|
2013-09-16 09:53:25 +00:00
|
|
|
*/
|
2014-09-17 12:41:24 +00:00
|
|
|
if (if_init_all(server_opt.ifs, server_opt.ifs_n,
|
|
|
|
me.cur_ifs, &me.cur_ifs_n) < 0)
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("Cannot initialize any network interfaces");
|
|
|
|
|
2014-02-28 10:14:43 +00:00
|
|
|
/*
|
2013-09-16 09:53:25 +00:00
|
|
|
* ANDNA init
|
|
|
|
*/
|
2014-09-17 12:41:24 +00:00
|
|
|
if (!server_opt.disable_andna)
|
2013-09-16 09:53:25 +00:00
|
|
|
andna_init();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the Internet gateway stuff
|
|
|
|
*/
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.my_upload_bw && server_opt.my_dnload_bw)
|
|
|
|
me.my_bandwidth = bandwidth_in_8bit((server_opt.my_upload_bw +
|
|
|
|
server_opt.my_dnload_bw) / 2);
|
2013-09-16 09:53:25 +00:00
|
|
|
init_internet_gateway_search();
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
pkts_init(me.cur_ifs, me.cur_ifs_n, 0);
|
|
|
|
qspn_init(FAMILY_LVLS);
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
me.cur_erc = e_rnode_init(&me.cur_erc_counter);
|
2013-09-16 09:53:25 +00:00
|
|
|
|
|
|
|
/* Radar init */
|
|
|
|
rq_wait_idx_init(rq_wait_idx);
|
|
|
|
first_init_radar();
|
2014-09-17 12:41:24 +00:00
|
|
|
total_radars = 0;
|
2013-09-16 09:53:25 +00:00
|
|
|
|
|
|
|
ntk_load_maps();
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* TODO: activate and test it !! */
|
|
|
|
debug(DBG_NORMAL, "ACPT: Initializing the accept_tbl: \n"
|
2014-09-17 12:41:24 +00:00
|
|
|
" max_connections: %d,\n"
|
|
|
|
" max_accepts_per_host: %d,\n"
|
|
|
|
" max_accept_per_host_time: %d",
|
|
|
|
server_opt.max_connections,
|
|
|
|
server_opt.max_accepts_per_host,
|
|
|
|
server_opt.max_accepts_per_host_time);
|
2014-02-28 10:14:43 +00:00
|
|
|
init_accept_tbl(server_opt.max_connections,
|
2014-09-17 12:41:24 +00:00
|
|
|
server_opt.max_accepts_per_host,
|
|
|
|
server_opt.max_accepts_per_host_time);
|
2013-09-16 09:53:25 +00:00
|
|
|
#endif
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (restricted_mode)
|
2013-09-16 09:53:25 +00:00
|
|
|
loginfo("NetsukukuD is in restricted mode. "
|
2014-09-17 12:41:24 +00:00
|
|
|
"Restricted class: %s",
|
|
|
|
server_opt.
|
|
|
|
restricted_class ? RESTRICTED_172_STR : RESTRICTED_10_STR);
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
hook_init();
|
|
|
|
rehook_init();
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
me.uptime = time(0);
|
2013-09-16 09:53:25 +00:00
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
int
|
|
|
|
destroy_netsukuku(void)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
2014-09-17 12:41:24 +00:00
|
|
|
if (destroy_netsukuku_mutex)
|
2013-09-16 09:53:25 +00:00
|
|
|
return -1;
|
2014-09-17 12:41:24 +00:00
|
|
|
destroy_netsukuku_mutex = 1;
|
2013-09-16 09:53:25 +00:00
|
|
|
|
|
|
|
unlink(server_opt.pid_file);
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
ntk_save_maps();
|
|
|
|
ntk_free_maps();
|
2014-09-17 12:41:24 +00:00
|
|
|
if (!server_opt.disable_andna)
|
2013-09-16 09:53:25 +00:00
|
|
|
andna_close();
|
|
|
|
|
|
|
|
close_internet_gateway_search();
|
|
|
|
last_close_radar();
|
|
|
|
e_rnode_free(&me.cur_erc, &me.cur_erc_counter);
|
|
|
|
destroy_accept_tbl();
|
|
|
|
if_close_all();
|
|
|
|
qspn_free();
|
|
|
|
free_server_opt();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
void
|
|
|
|
sigterm_handler(int sig)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
time_t cur_t;
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (sigterm_timestamp == (cur_t = time(0)))
|
2013-09-16 09:53:25 +00:00
|
|
|
return;
|
2014-09-17 12:41:24 +00:00
|
|
|
sigterm_timestamp = time(0);
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (!destroy_netsukuku())
|
2013-09-16 09:53:25 +00:00
|
|
|
fatal("Termination signal caught. Dying, bye, bye");
|
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
void *
|
|
|
|
reload_hostname_thread(void *null)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
2014-02-28 10:14:43 +00:00
|
|
|
/*
|
2013-09-16 09:53:25 +00:00
|
|
|
* Reload the file where the hostnames to be registered are and
|
|
|
|
* register the new ones
|
|
|
|
*/
|
|
|
|
loginfo("Reloading the andna hostnames file");
|
|
|
|
load_hostnames(server_opt.andna_hnames_file, &andna_lcl, &lcl_counter);
|
|
|
|
load_snsd(server_opt.snsd_nodes_file, andna_lcl);
|
|
|
|
andna_update_hnames(1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
void
|
|
|
|
sighup_handler(int sig)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
pthread_t thread;
|
|
|
|
pthread_attr_t t_attr;
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
time_t cur_t;
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (sighup_timestamp == (cur_t = time(0)))
|
2013-09-16 09:53:25 +00:00
|
|
|
return;
|
2014-09-17 12:41:24 +00:00
|
|
|
sighup_timestamp = time(0);
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
pthread_attr_init(&t_attr);
|
|
|
|
pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
pthread_create(&thread, &t_attr, reload_hostname_thread, 0);
|
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
void *
|
|
|
|
rh_cache_flush_thread(void *null)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
2014-02-28 10:14:43 +00:00
|
|
|
/*
|
2013-09-16 09:53:25 +00:00
|
|
|
* Flush the resolved hostnames cache.
|
|
|
|
*/
|
|
|
|
loginfo("Flush the resolved hostnames cache");
|
|
|
|
rh_cache_flush();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
void
|
|
|
|
sigalrm_handler(int sig)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
pthread_t thread;
|
|
|
|
pthread_attr_t t_attr;
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
time_t cur_t;
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (sigalrm_timestamp == (cur_t = time(0)))
|
2013-09-16 09:53:25 +00:00
|
|
|
return;
|
2014-09-17 12:41:24 +00:00
|
|
|
sigalrm_timestamp = time(0);
|
2013-09-16 09:53:25 +00:00
|
|
|
|
|
|
|
pthread_attr_init(&t_attr);
|
|
|
|
pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
pthread_create(&thread, &t_attr, rh_cache_flush_thread, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The main flow shall never be stopped, and the sand of time will be
|
|
|
|
* revealed.
|
|
|
|
*/
|
2014-09-17 12:41:24 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
2013-09-16 09:53:25 +00:00
|
|
|
{
|
|
|
|
struct udp_daemon_argv ud_argv;
|
|
|
|
u_short *port;
|
|
|
|
pthread_t daemon_tcp_thread, daemon_udp_thread, andna_thread;
|
|
|
|
pthread_t ping_igw_thread;
|
|
|
|
pthread_attr_t t_attr;
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
log_init(argv[0], 0, 1);
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
/* Options loading... */
|
|
|
|
fill_default_options();
|
|
|
|
parse_options(argc, argv);
|
|
|
|
|
|
|
|
/* reinit the logs using the new `dbg_lvl' value */
|
|
|
|
log_init(argv[0], server_opt.dbg_lvl, 1);
|
|
|
|
log_to_file(0);
|
|
|
|
|
|
|
|
/* Load the option from the config file */
|
|
|
|
load_config_file(server_opt.config_file);
|
|
|
|
fill_loaded_cfg_options();
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
/* If a same option was specified in the config file and in the
|
|
|
|
* command line, give priority to the latter */
|
|
|
|
parse_options(argc, argv);
|
|
|
|
|
|
|
|
check_conflicting_options();
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
/* Initialize the whole netsukuku source code */
|
|
|
|
init_netsukuku(argv);
|
|
|
|
|
|
|
|
signal(SIGALRM, sigalrm_handler);
|
|
|
|
signal(SIGHUP, sighup_handler);
|
|
|
|
signal(SIGINT, sigterm_handler);
|
|
|
|
signal(SIGTERM, sigterm_handler);
|
|
|
|
signal(SIGQUIT, sigterm_handler);
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
/* Angelic foreground or Daemonic background ? */
|
2014-09-17 12:41:24 +00:00
|
|
|
if (server_opt.daemon) {
|
2013-09-16 09:53:25 +00:00
|
|
|
loginfo("Forking to background");
|
|
|
|
log_init(argv[0], server_opt.dbg_lvl, 0);
|
2014-09-17 12:41:24 +00:00
|
|
|
if (daemon(0, 0) == -1)
|
2013-09-16 09:53:25 +00:00
|
|
|
error("Impossible to daemonize: %s.", strerror(errno));
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_attr_init(&t_attr);
|
|
|
|
pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
setzero(&ud_argv, sizeof(struct udp_daemon_argv));
|
2014-09-17 12:41:24 +00:00
|
|
|
port = xmalloc(sizeof(u_short));
|
2013-09-16 09:53:25 +00:00
|
|
|
|
2014-02-28 10:14:43 +00:00
|
|
|
/*
|
2013-09-16 09:53:25 +00:00
|
|
|
* These are the daemons, the main threads that keeps NetsukukuD
|
2014-02-28 10:14:43 +00:00
|
|
|
* up & running.
|
2013-09-16 09:53:25 +00:00
|
|
|
*/
|
|
|
|
debug(DBG_NORMAL, "Activating all daemons");
|
|
|
|
|
|
|
|
pthread_mutex_init(&udp_daemon_lock, 0);
|
|
|
|
pthread_mutex_init(&tcp_daemon_lock, 0);
|
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
debug(DBG_SOFT, "Evoking the netsukuku udp radar daemon.");
|
|
|
|
ud_argv.port = ntk_udp_radar_port;
|
2013-09-16 09:53:25 +00:00
|
|
|
pthread_mutex_lock(&udp_daemon_lock);
|
2014-09-17 12:41:24 +00:00
|
|
|
pthread_create(&daemon_udp_thread, &t_attr, udp_daemon,
|
|
|
|
(void *) &ud_argv);
|
2013-09-16 09:53:25 +00:00
|
|
|
pthread_mutex_lock(&udp_daemon_lock);
|
|
|
|
pthread_mutex_unlock(&udp_daemon_lock);
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
debug(DBG_SOFT, "Evoking the netsukuku tcp daemon.");
|
|
|
|
*port = ntk_tcp_port;
|
2013-09-16 09:53:25 +00:00
|
|
|
pthread_mutex_lock(&tcp_daemon_lock);
|
2014-09-17 12:41:24 +00:00
|
|
|
pthread_create(&daemon_tcp_thread, &t_attr, tcp_daemon, (void *) port);
|
2013-09-16 09:53:25 +00:00
|
|
|
pthread_mutex_lock(&tcp_daemon_lock);
|
|
|
|
pthread_mutex_unlock(&tcp_daemon_lock);
|
|
|
|
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
/* Now we hook in Netsukuku */
|
|
|
|
netsukuku_hook(0, 0);
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
/*
|
2014-02-28 10:14:43 +00:00
|
|
|
* If not disabled, start the ANDNA daemon
|
2013-09-16 09:53:25 +00:00
|
|
|
*/
|
2014-09-17 12:41:24 +00:00
|
|
|
if (!server_opt.disable_andna)
|
2013-09-16 09:53:25 +00:00
|
|
|
pthread_create(&andna_thread, &t_attr, andna_main, 0);
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2013-09-16 09:53:25 +00:00
|
|
|
xfree(port);
|
2014-02-28 10:14:43 +00:00
|
|
|
|
2014-09-17 12:41:24 +00:00
|
|
|
if (restricted_mode && (server_opt.share_internet ||
|
|
|
|
server_opt.use_shared_inet)) {
|
2013-09-16 09:53:25 +00:00
|
|
|
debug(DBG_SOFT, "Evoking the Internet Gateway Pinger daemon");
|
2014-09-17 12:41:24 +00:00
|
|
|
pthread_create(&ping_igw_thread, &t_attr, igw_monitor_igws_t, 0);
|
2013-09-16 09:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We use this same process for the radar_daemon. */
|
2014-09-17 12:41:24 +00:00
|
|
|
debug(DBG_SOFT, "Evoking radar daemon.");
|
2013-09-16 09:53:25 +00:00
|
|
|
radar_daemon(0);
|
|
|
|
|
|
|
|
/* Not reached, hahaha */
|
|
|
|
loginfo("Cya m8");
|
|
|
|
pthread_attr_destroy(&t_attr);
|
|
|
|
destroy_netsukuku();
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|