Move make_names() and related variables to its own source file.

This commit is contained in:
Guus Sliepen 2013-01-17 16:39:02 +01:00
parent a9eba276a4
commit f5bb64b36a
23 changed files with 155 additions and 151 deletions

View file

@ -9,7 +9,7 @@ tincd_SOURCES = \
buffer.c conf.c connection.c control.c edge.c graph.c logger.c meta.c net.c net_packet.c net_setup.c \ buffer.c conf.c connection.c control.c edge.c graph.c logger.c meta.c net.c net_packet.c net_setup.c \
net_socket.c netutl.c node.c process.c protocol.c protocol_auth.c protocol_edge.c protocol_misc.c \ net_socket.c netutl.c node.c process.c protocol.c protocol_auth.c protocol_edge.c protocol_misc.c \
protocol_key.c protocol_subnet.c route.c sptps.c subnet.c subnet_parse.c event.c tincd.c \ protocol_key.c protocol_subnet.c route.c sptps.c subnet.c subnet_parse.c event.c tincd.c \
dummy_device.c raw_socket_device.c multicast_device.c dummy_device.c raw_socket_device.c multicast_device.c names.c
if UML if UML
tincd_SOURCES += uml_device.c tincd_SOURCES += uml_device.c
@ -24,7 +24,7 @@ nodist_tincd_SOURCES = \
tincctl_SOURCES = \ tincctl_SOURCES = \
utils.c getopt.c getopt1.c dropin.c \ utils.c getopt.c getopt1.c dropin.c \
info.c list.c subnet_parse.c tincctl.c top.c info.c list.c subnet_parse.c tincctl.c top.c names.c
nodist_tincctl_SOURCES = \ nodist_tincctl_SOURCES = \
ecdsagen.c rsagen.c ecdsagen.c rsagen.c

View file

@ -24,6 +24,7 @@
#include "conf.h" #include "conf.h"
#include "device.h" #include "device.h"
#include "logger.h" #include "logger.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "route.h" #include "route.h"
#include "utils.h" #include "utils.h"

View file

@ -28,6 +28,7 @@
#include "conf.h" #include "conf.h"
#include "list.h" #include "list.h"
#include "logger.h" #include "logger.h"
#include "names.h"
#include "netutl.h" /* for str2address */ #include "netutl.h" /* for str2address */
#include "protocol.h" #include "protocol.h"
#include "utils.h" /* for cp */ #include "utils.h" /* for cp */
@ -37,11 +38,8 @@ splay_tree_t *config_tree;
int pinginterval = 0; /* seconds between pings */ int pinginterval = 0; /* seconds between pings */
int pingtimeout = 0; /* seconds to wait for response */ int pingtimeout = 0; /* seconds to wait for response */
char *confbase = NULL; /* directory in which all config files are */
char *netname = NULL; /* name of the vpn network */
list_t *cmdline_conf = NULL; /* global/host configuration values given at the command line */ list_t *cmdline_conf = NULL; /* global/host configuration values given at the command line */
static int config_compare(const config_t *a, const config_t *b) { static int config_compare(const config_t *a, const config_t *b) {
int result; int result;

View file

@ -39,8 +39,6 @@ extern int pinginterval;
extern int pingtimeout; extern int pingtimeout;
extern int maxtimeout; extern int maxtimeout;
extern bool bypass_security; extern bool bypass_security;
extern char *confbase;
extern char *netname;
extern list_t *cmdline_conf; extern list_t *cmdline_conf;
extern void init_configuration(splay_tree_t **); extern void init_configuration(splay_tree_t **);

View file

@ -25,6 +25,7 @@
#include "graph.h" #include "graph.h"
#include "logger.h" #include "logger.h"
#include "meta.h" #include "meta.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "netutl.h" #include "netutl.h"
#include "protocol.h" #include "protocol.h"
@ -33,7 +34,6 @@
#include "xalloc.h" #include "xalloc.h"
char controlcookie[65]; char controlcookie[65];
extern char *pidfilename;
static bool control_return(connection_t *c, int type, int error) { static bool control_return(connection_t *c, int type, int error) {
return send_request(c, "%d %d %d", CONTROL, type, error); return send_request(c, "%d %d %d", CONTROL, type, error);

View file

@ -26,6 +26,7 @@
#include "conf.h" #include "conf.h"
#include "device.h" #include "device.h"
#include "logger.h" #include "logger.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "route.h" #include "route.h"
#include "utils.h" #include "utils.h"

View file

@ -51,6 +51,7 @@
#include "graph.h" #include "graph.h"
#include "list.h" #include "list.h"
#include "logger.h" #include "logger.h"
#include "names.h"
#include "netutl.h" #include "netutl.h"
#include "node.h" #include "node.h"
#include "process.h" #include "process.h"

View file

@ -26,6 +26,7 @@
#include "conf.h" #include "conf.h"
#include "device.h" #include "device.h"
#include "logger.h" #include "logger.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "route.h" #include "route.h"
#include "utils.h" #include "utils.h"

View file

@ -22,6 +22,7 @@
#include "conf.h" #include "conf.h"
#include "meta.h" #include "meta.h"
#include "names.h"
#include "logger.h" #include "logger.h"
#include "connection.h" #include "connection.h"
#include "control_common.h" #include "control_common.h"
@ -30,7 +31,6 @@
debug_t debug_level = DEBUG_NOTHING; debug_t debug_level = DEBUG_NOTHING;
static logmode_t logmode = LOGMODE_STDERR; static logmode_t logmode = LOGMODE_STDERR;
static pid_t logpid; static pid_t logpid;
extern char *logfilename;
static FILE *logfile = NULL; static FILE *logfile = NULL;
#ifdef HAVE_MINGW #ifdef HAVE_MINGW
static HANDLE loghandle = NULL; static HANDLE loghandle = NULL;

View file

@ -26,6 +26,7 @@
#include "conf.h" #include "conf.h"
#include "device.h" #include "device.h"
#include "logger.h" #include "logger.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "route.h" #include "route.h"
#include "utils.h" #include "utils.h"

94
src/names.c Normal file
View file

@ -0,0 +1,94 @@
/*
names.c -- generate commonly used (file)names
Copyright (C) 1998-2005 Ivo Timmermans
2000-2013 Guus Sliepen <guus@tinc-vpn.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "system.h"
#include "logger.h"
#include "xalloc.h"
char *netname = NULL;
char *confdir = NULL; /* base configuration directory */
char *confbase = NULL; /* base configuration directory for this instance of tinc */
char *identname = NULL; /* program name for syslog */
char *logfilename = NULL; /* log file location */
char *pidfilename = NULL;
char *program_name = NULL;
/*
Set all files and paths according to netname
*/
void make_names(void) {
#ifdef HAVE_MINGW
HKEY key;
char installdir[1024] = "";
long len = sizeof installdir;
#endif
if(netname)
xasprintf(&identname, "tinc.%s", netname);
else
identname = xstrdup("tinc");
#ifdef HAVE_MINGW
if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
confdir = xstrdup(installdir);
if(!logfilename)
xasprintf(&logfilename, "%s" SLASH "log" SLASH "%s.log", installdir, identname);
if(!confbase) {
if(netname)
xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
else
xasprintf(&confbase, "%s", installdir);
}
if(!pidfilename)
xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
}
RegCloseKey(key);
}
#endif
if(!confdir)
confdir = xstrdup(CONFDIR);
if(!logfilename)
xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname);
if(!pidfilename)
xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
if(netname) {
if(!confbase)
xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
else
logger(DEBUG_ALWAYS, LOG_INFO, "Both netname and configuration directory given, using the latter...");
} else {
if(!confbase)
xasprintf(&confbase, CONFDIR SLASH "tinc");
}
}
void free_names(void) {
free(identname);
free(netname);
free(pidfilename);
free(logfilename);
free(confbase);
free(confdir);
}

35
src/names.h Normal file
View file

@ -0,0 +1,35 @@
/*
names.h -- header for names.c
Copyright (C) 1998-2005 Ivo Timmermans
2000-2013 Guus Sliepen <guus@tinc-vpn.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef __TINC_NAMES_H__
#define __TINC_NAMES_H__
extern char *confdir;
extern char *confbase;
extern char *netname;
extern char *identname;
extern char *logfilename;
extern char *pidfilename;
extern char *program_name;
extern void make_names(void);
extern void free_names(void);
#endif /* __TINC_NAMES_H__ */

View file

@ -29,6 +29,7 @@
#include "graph.h" #include "graph.h"
#include "logger.h" #include "logger.h"
#include "meta.h" #include "meta.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "netutl.h" #include "netutl.h"
#include "process.h" #include "process.h"

View file

@ -31,6 +31,7 @@
#include "ecdsa.h" #include "ecdsa.h"
#include "graph.h" #include "graph.h"
#include "logger.h" #include "logger.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "netutl.h" #include "netutl.h"
#include "process.h" #include "process.h"

View file

@ -27,6 +27,7 @@
#include "list.h" #include "list.h"
#include "logger.h" #include "logger.h"
#include "meta.h" #include "meta.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "netutl.h" #include "netutl.h"
#include "protocol.h" #include "protocol.h"

View file

@ -27,6 +27,7 @@
#include "edge.h" #include "edge.h"
#include "event.h" #include "event.h"
#include "logger.h" #include "logger.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "node.h" #include "node.h"
#include "process.h" #include "process.h"
@ -38,17 +39,12 @@
bool do_detach = true; bool do_detach = true;
bool sigalrm = false; bool sigalrm = false;
extern char *identname;
extern char **g_argv; extern char **g_argv;
extern bool use_logfile; extern bool use_logfile;
/* Some functions the less gifted operating systems might lack... */ /* Some functions the less gifted operating systems might lack... */
#ifdef HAVE_MINGW #ifdef HAVE_MINGW
extern char *identname;
extern char *program_name;
extern char **g_argv;
static SC_HANDLE manager = NULL; static SC_HANDLE manager = NULL;
static SC_HANDLE service = NULL; static SC_HANDLE service = NULL;
static SERVICE_STATUS status = {0}; static SERVICE_STATUS status = {0};

View file

@ -28,6 +28,7 @@
#include "conf.h" #include "conf.h"
#include "device.h" #include "device.h"
#include "logger.h" #include "logger.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "utils.h" #include "utils.h"
#include "xalloc.h" #include "xalloc.h"

View file

@ -25,6 +25,7 @@
#include "device.h" #include "device.h"
#include "hash.h" #include "hash.h"
#include "logger.h" #include "logger.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "netutl.h" #include "netutl.h"
#include "node.h" #include "node.h"

View file

@ -31,6 +31,7 @@
#include "control_common.h" #include "control_common.h"
#include "ecdsagen.h" #include "ecdsagen.h"
#include "info.h" #include "info.h"
#include "names.h"
#include "rsagen.h" #include "rsagen.h"
#include "utils.h" #include "utils.h"
#include "tincctl.h" #include "tincctl.h"
@ -40,10 +41,6 @@
#define mkdir(a, b) mkdir(a) #define mkdir(a, b) mkdir(a)
#endif #endif
/* The name this program was run with. */
static char *program_name = NULL;
static char **orig_argv; static char **orig_argv;
static int orig_argc; static int orig_argc;
@ -54,12 +51,7 @@ static bool show_help = false;
static bool show_version = false; static bool show_version = false;
static char *name = NULL; static char *name = NULL;
static char *identname = NULL; /* program name for syslog */
static char *pidfilename = NULL; /* pid file location */
static char *confdir = NULL;
static char controlcookie[1025]; static char controlcookie[1025];
char *netname = NULL;
char *confbase = NULL;
static char *tinc_conf = NULL; static char *tinc_conf = NULL;
static char *hosts_dir = NULL; static char *hosts_dir = NULL;
struct timeval now; struct timeval now;
@ -107,10 +99,9 @@ static void version(void) {
} }
static void usage(bool status) { static void usage(bool status) {
if(status) if(status) {
fprintf(stderr, "Try `%s --help\' for more information.\n", fprintf(stderr, "Try `%s --help\' for more information.\n", program_name);
program_name); } else {
else {
printf("Usage: %s [options] command\n\n", program_name); printf("Usage: %s [options] command\n\n", program_name);
printf("Valid options are:\n" printf("Valid options are:\n"
" -c, --config=DIR Read configuration options from DIR.\n" " -c, --config=DIR Read configuration options from DIR.\n"
@ -455,62 +446,6 @@ static bool rsa_keygen(int bits, bool ask) {
return true; return true;
} }
/*
Set all files and paths according to netname
*/
static void make_names(void) {
#ifdef HAVE_MINGW
HKEY key;
char installdir[1024] = "";
long len = sizeof installdir;
#endif
if(netname)
xasprintf(&identname, "tinc.%s", netname);
else
identname = xstrdup("tinc");
#ifdef HAVE_MINGW
if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
if(!confbase) {
if(netname)
xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
else
xasprintf(&confbase, "%s", installdir);
}
}
if(!pidfilename)
xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
RegCloseKey(key);
}
if(!*installdir) {
#endif
confdir = xstrdup(CONFDIR);
if(!pidfilename)
xasprintf(&pidfilename, "%s" SLASH "run" SLASH "%s.pid", LOCALSTATEDIR, identname);
if(netname) {
if(!confbase)
xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
else
fprintf(stderr, "Both netname and configuration directory given, using the latter...\n");
} else {
if(!confbase)
xasprintf(&confbase, CONFDIR SLASH "tinc");
}
#ifdef HAVE_MINGW
} else
confdir = xstrdup(installdir);
#endif
xasprintf(&tinc_conf, "%s" SLASH "tinc.conf", confbase);
xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase);
}
static char buffer[4096]; static char buffer[4096];
static size_t blen = 0; static size_t blen = 0;
@ -2285,6 +2220,8 @@ int main(int argc, char *argv[]) {
return 1; return 1;
make_names(); make_names();
xasprintf(&tinc_conf, "%s" SLASH "tinc.conf", confbase);
xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase);
if(show_version) { if(show_version) {
version(); version();

View file

@ -56,6 +56,7 @@
#include "crypto.h" #include "crypto.h"
#include "device.h" #include "device.h"
#include "logger.h" #include "logger.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "netutl.h" #include "netutl.h"
#include "process.h" #include "process.h"
@ -63,9 +64,6 @@
#include "utils.h" #include "utils.h"
#include "xalloc.h" #include "xalloc.h"
/* The name this program was run with. */
char *program_name = NULL;
/* If nonzero, display usage information and exit. */ /* If nonzero, display usage information and exit. */
static bool show_help = false; static bool show_help = false;
@ -87,9 +85,6 @@ static const char *switchuser = NULL;
/* If nonzero, write log entries to a separate file. */ /* If nonzero, write log entries to a separate file. */
bool use_logfile = false; bool use_logfile = false;
char *identname = NULL; /* program name for syslog */
char *logfilename = NULL; /* log file location */
char *pidfilename = NULL;
char **g_argv; /* a copy of the cmdline arguments */ char **g_argv; /* a copy of the cmdline arguments */
static int status = 1; static int status = 1;
@ -256,66 +251,6 @@ static bool parse_options(int argc, char **argv) {
return true; return true;
} }
/*
Set all files and paths according to netname
*/
static void make_names(void) {
#ifdef HAVE_MINGW
HKEY key;
char installdir[1024] = "";
long len = sizeof installdir;
#endif
if(netname)
xasprintf(&identname, "tinc.%s", netname);
else
identname = xstrdup("tinc");
#ifdef HAVE_MINGW
if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
if(!logfilename)
xasprintf(&logfilename, "%s" SLASH "log" SLASH "%s.log", identname);
if(!confbase) {
if(netname)
xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
else
xasprintf(&confbase, "%s", installdir);
}
if(!pidfilename)
xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
}
RegCloseKey(key);
if(*installdir)
return;
}
#endif
if(!logfilename)
xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname);
if(!pidfilename)
xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
if(netname) {
if(!confbase)
xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
else
logger(DEBUG_ALWAYS, LOG_INFO, "Both netname and configuration directory given, using the latter...");
} else {
if(!confbase)
xasprintf(&confbase, CONFDIR SLASH "tinc");
}
}
static void free_names(void) {
if (identname) free(identname);
if (netname) free(netname);
if (pidfilename) free(pidfilename);
if (logfilename) free(logfilename);
if (confbase) free(confbase);
}
static bool drop_privs(void) { static bool drop_privs(void) {
#ifdef HAVE_MINGW #ifdef HAVE_MINGW
return false; return false;

View file

@ -25,6 +25,7 @@
#include "control_common.h" #include "control_common.h"
#include "list.h" #include "list.h"
#include "names.h"
#include "tincctl.h" #include "tincctl.h"
#include "top.h" #include "top.h"
#include "xalloc.h" #include "xalloc.h"

View file

@ -24,6 +24,7 @@
#include "conf.h" #include "conf.h"
#include "device.h" #include "device.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "logger.h" #include "logger.h"
#include "utils.h" #include "utils.h"
@ -37,7 +38,6 @@ static int write_fd = -1;
static int state = 0; static int state = 0;
static char *device_info; static char *device_info;
extern char *identname;
extern volatile bool running; extern volatile bool running;
static uint64_t device_total_in = 0; static uint64_t device_total_in = 0;

View file

@ -23,6 +23,7 @@
#include "conf.h" #include "conf.h"
#include "device.h" #include "device.h"
#include "names.h"
#include "net.h" #include "net.h"
#include "logger.h" #include "logger.h"
#include "utils.h" #include "utils.h"
@ -35,7 +36,6 @@ static int port = 0;
static char *group = NULL; static char *group = NULL;
static char *device_info; static char *device_info;
extern char *identname;
extern volatile bool running; extern volatile bool running;
static uint64_t device_total_in = 0; static uint64_t device_total_in = 0;