2012-02-16 14:05:28 +00:00
|
|
|
/*
|
|
|
|
Serval Distributed Numbering Architecture (DNA)
|
|
|
|
Copyright (C) 2010 Paul Gardner-Stephen
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2012-03-04 22:57:31 +00:00
|
|
|
#include <sys/stat.h>
|
2012-02-23 02:15:42 +00:00
|
|
|
#include "serval.h"
|
2012-02-16 14:05:28 +00:00
|
|
|
|
|
|
|
typedef struct command_line_option {
|
|
|
|
int (*function)(int argc,char **argv,struct command_line_option *o);
|
|
|
|
char *words[32]; // 32 words should be plenty!
|
|
|
|
unsigned long long flags;
|
|
|
|
#define CLIFLAG_NONOVERLAY (1<<0) /* Uses a legacy IPv4 DNA call instead of overlay mnetwork */
|
|
|
|
#define CLIFLAG_STANDALONE (1<<1) /* Cannot be issued to a running instance */
|
|
|
|
char *description; // describe this invocation
|
|
|
|
} command_line_option;
|
|
|
|
|
|
|
|
extern command_line_option command_line_options[];
|
|
|
|
|
2012-02-23 01:25:39 +00:00
|
|
|
int servalNodeRunning(int *pid,char *instancepath)
|
|
|
|
{
|
|
|
|
char filename[1024];
|
|
|
|
char line[1024];
|
|
|
|
if (!instancepath) instancepath=DEFAULT_INSTANCE_PATH;
|
|
|
|
|
2012-03-04 22:57:31 +00:00
|
|
|
struct stat st;
|
|
|
|
int r=stat(instancepath,&st);
|
|
|
|
if (r) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Instance path '%s' non existant or not accessable.\n"
|
|
|
|
" Operating system says: %s (errno=%d)\n",
|
|
|
|
instancepath,strerror(errno),errno);
|
|
|
|
fprintf(stderr,
|
|
|
|
" (Set SERVALINSTANCE_PATH to specify an alternate location.)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((st.st_mode&S_IFMT)!=S_IFDIR) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Instance path must be a valid directory.\n"
|
|
|
|
" '%s' is not a directory.\n",instancepath);
|
|
|
|
*pid=-1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-23 01:25:39 +00:00
|
|
|
int running=0;
|
2012-02-23 02:08:19 +00:00
|
|
|
snprintf(filename,1023,"%s/serval.pid",instancepath); filename[1023]=0;
|
2012-02-23 01:25:39 +00:00
|
|
|
FILE *f=fopen(filename,"r");
|
|
|
|
if (f) {
|
2012-03-04 22:57:31 +00:00
|
|
|
line[0]=0; fgets(line,1024,f);
|
2012-02-23 01:25:39 +00:00
|
|
|
*pid = strtoll(line,NULL,10);
|
|
|
|
running=*pid;
|
|
|
|
if (running) {
|
|
|
|
/* Check that process is really running.
|
|
|
|
Some systems don't have /proc (including mac),
|
|
|
|
so we need to find out some otherway.*/
|
2012-03-04 22:57:31 +00:00
|
|
|
running=1; // assume pid means is running for now
|
2012-02-23 01:25:39 +00:00
|
|
|
}
|
|
|
|
fclose(f);
|
2012-02-23 02:08:19 +00:00
|
|
|
}
|
2012-02-23 01:25:39 +00:00
|
|
|
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:05:28 +00:00
|
|
|
int cli_usage() {
|
|
|
|
fprintf(stderr,"\nServal Mesh version <version>.\n");
|
|
|
|
fprintf(stderr,"Usage:\n");
|
|
|
|
int i,j;
|
|
|
|
for(i=0;command_line_options[i].function;i++)
|
|
|
|
{
|
|
|
|
for(j=0;command_line_options[i].words[j];j++)
|
|
|
|
fprintf(stderr," %s",command_line_options[i].words[j]);
|
|
|
|
fprintf(stderr,"\n %s\n",command_line_options[i].description);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:36:08 +00:00
|
|
|
char *cli_arg(int argc,char **argv,command_line_option *o,
|
|
|
|
char *argname,char *defaultvalue)
|
|
|
|
{
|
|
|
|
int arglen=strlen(argname)+2;
|
|
|
|
int i;
|
|
|
|
for(i=0;o->words[i];i++)
|
|
|
|
if ((strlen(o->words[i])==arglen)
|
|
|
|
&&(o->words[i][0]=='<')
|
|
|
|
&&(o->words[i][arglen-1]=='>')
|
|
|
|
&&(!strncasecmp(&o->words[i][1],argname,arglen-2)))
|
|
|
|
{
|
|
|
|
/* Found the arg, so return the corresponding argument */
|
|
|
|
return argv[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No matching argument was found, so return default value.
|
|
|
|
It might seem that this should never happen, but it can because more than
|
|
|
|
one version of a command line optiom may exist, one with a given argument
|
|
|
|
and another without, and allowing a default value means we can have a single
|
|
|
|
function handle both in a fairly simple manner. */
|
|
|
|
return defaultvalue;
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:05:28 +00:00
|
|
|
/* args[] excludes command name (unless hardlinks are used to use first words
|
|
|
|
of command sequences as alternate names of the command. */
|
|
|
|
int parseCommandLine(int argc, char **args)
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
int ambiguous=0;
|
|
|
|
int cli_call=-1;
|
2012-02-23 02:08:19 +00:00
|
|
|
|
2012-02-16 14:05:28 +00:00
|
|
|
for(i=0;command_line_options[i].function;i++)
|
|
|
|
{
|
|
|
|
for(j=0;(j<argc)&&command_line_options[i].words[j];j++)
|
|
|
|
if ((command_line_options[i].words[j][0]!='<')&&
|
|
|
|
strcasecmp(command_line_options[i].words[j],args[j])) {
|
|
|
|
/* Words don't match, and word is not a place-holder for an argument,
|
|
|
|
so it isn't this command line call. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((j==argc)&&(!command_line_options[i].words[j])) {
|
|
|
|
/* We used up all words in args and command line call sequence, so we have
|
|
|
|
a match. If we have multiple matches, then note that the call is
|
|
|
|
ambiguous. */
|
|
|
|
if (cli_call>=0) ambiguous++;
|
|
|
|
if (ambiguous==1) {
|
|
|
|
fprintf(stderr,"Ambiguous command line call:\n ");
|
|
|
|
for(j=0;j<argc;j++) fprintf(stderr," %s",args[j]);
|
|
|
|
fprintf(stderr,"\nMatches the following known command line calls:\n");
|
|
|
|
}
|
|
|
|
if (ambiguous) {
|
|
|
|
fprintf(stderr," ");
|
|
|
|
for(j=0;j<argc;j++) fprintf(stderr," %s",command_line_options[i].words[j]);
|
|
|
|
fprintf(stderr,"\n");
|
|
|
|
}
|
|
|
|
cli_call=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't process ambiguous calls */
|
|
|
|
if (ambiguous) return -1;
|
|
|
|
/* Complain if we found no matching calls */
|
|
|
|
if (cli_call<0) return cli_usage();
|
|
|
|
|
|
|
|
/* Otherwise, make call */
|
2012-02-23 02:08:19 +00:00
|
|
|
return command_line_options[cli_call].function(argc,args,
|
|
|
|
&command_line_options[cli_call]);
|
2012-02-16 14:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int app_dna_lookup(int argc,char **argv,struct command_line_option *o)
|
|
|
|
{
|
2012-02-16 14:36:08 +00:00
|
|
|
return WHY("Not implemented");
|
|
|
|
}
|
|
|
|
|
2012-03-04 22:57:31 +00:00
|
|
|
int confValueRotor=0;
|
|
|
|
char confValue[4][128];
|
|
|
|
char *confValueGet(char *var,char *defaultValue)
|
|
|
|
{
|
|
|
|
if (!var) return defaultValue;
|
|
|
|
int varLen=strlen(var);
|
|
|
|
|
2012-03-16 22:32:37 +00:00
|
|
|
char *instancepath=serval_instancepath();
|
2012-03-04 22:57:31 +00:00
|
|
|
|
|
|
|
char filename[1024];
|
|
|
|
snprintf(filename,1024,"%s/serval.conf",instancepath); filename[1023]=0;
|
|
|
|
FILE *f=fopen(filename,"r");
|
|
|
|
if (!f) return defaultValue;
|
|
|
|
|
|
|
|
char line[1024];
|
|
|
|
line[0]=0; fgets(line,1024,f);
|
|
|
|
while(line[0]) {
|
|
|
|
if (!strncasecmp(line,var,varLen)) {
|
|
|
|
if (line[varLen]=='=') {
|
|
|
|
fclose(f);
|
|
|
|
if (strlen(&line[varLen+1])>127) return defaultValue;
|
|
|
|
/* The rotor is used to pick which of four buffers to return in.
|
|
|
|
This allows the use of up to four calls to confValueGet() in
|
|
|
|
a single string formatting exercise, without unexpected side
|
|
|
|
effect. */
|
|
|
|
confValueRotor++; confValueRotor&=3;
|
|
|
|
strcpy(&confValue[confValueRotor][0],&line[varLen+1]);
|
|
|
|
return &confValue[confValueRotor][0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
line[0]=0; fgets(line,1024,f);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:36:08 +00:00
|
|
|
int app_server_start(int argc,char **argv,struct command_line_option *o)
|
|
|
|
{
|
2012-03-04 22:57:31 +00:00
|
|
|
int foregroundP=0;
|
|
|
|
char *instancepath=getenv("SERVALINSTANCE_PATH");
|
|
|
|
if (!instancepath) instancepath=DEFAULT_INSTANCE_PATH;
|
|
|
|
|
|
|
|
/* Process optional arguments */
|
|
|
|
if ((argc>=3)&&(!strcasecmp(argv[2],"foreground"))) foregroundP=1;
|
|
|
|
if ((argc>=4)&&(!strcasecmp(argv[2],"in"))) instancepath=argv[3];
|
|
|
|
|
2012-03-16 22:32:37 +00:00
|
|
|
/* Record instance path for easy access by whole process */
|
|
|
|
thisinstancepath=strdup(instancepath);
|
|
|
|
|
2012-03-04 22:57:31 +00:00
|
|
|
int pid=-1;
|
|
|
|
int running = servalNodeRunning(&pid,instancepath);
|
|
|
|
if (running<0) return -1;
|
|
|
|
if (running>0) {
|
|
|
|
fprintf(stderr,"ERROR: Serval process already running (pid=%d)\n",pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Start the Serval process.
|
|
|
|
All server settings will be read by the server process from the
|
|
|
|
instance directory when it starts up.
|
|
|
|
We can just become the server process ourselves --- no need to fork.
|
|
|
|
|
|
|
|
*/
|
|
|
|
rhizome_datastore_path=strdup(instancepath);
|
|
|
|
rhizome_opendb();
|
|
|
|
char temp[1024];temp[1023]=0;
|
|
|
|
snprintf(temp,1024,"%s/hlr.dat",instancepath);
|
|
|
|
if (temp[1023]) {
|
|
|
|
exit(WHY("Instance path directory name too long."));
|
|
|
|
}
|
|
|
|
char *hlr_file=strdup(temp);
|
|
|
|
hlr_size=atof(confValueGet("hlr_size","1"))*1048576;
|
|
|
|
if (hlr_size<0) {
|
|
|
|
fprintf(stderr,"HLR Size must be >0MB\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
overlayMode=1;
|
|
|
|
return server(hlr_file,hlr_size,foregroundP);
|
2012-02-16 14:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int app_server_stop(int argc,char **argv,struct command_line_option *o)
|
|
|
|
{
|
2012-03-04 22:57:31 +00:00
|
|
|
char *instancepath=getenv("SERVALINSTANCE_PATH");
|
|
|
|
if (!instancepath) instancepath=DEFAULT_INSTANCE_PATH;
|
|
|
|
|
|
|
|
int pid=-1;
|
|
|
|
int running = servalNodeRunning(&pid,instancepath);
|
|
|
|
if (running>0) {
|
|
|
|
/* Is running, so we can try to kill it.
|
|
|
|
This is a little complicated by the fact that we catch most signals
|
|
|
|
so that unexpected aborts just restart.
|
|
|
|
What we can do is put some code in the signal handler that does abort
|
|
|
|
the process if a certain file exists, perhaps instance_path/doshutdown,
|
|
|
|
and removes the file.
|
|
|
|
*/
|
|
|
|
if (pid<0) {
|
|
|
|
WHY("Could not determine process id of Serval process. Stale instance perhaps?");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char stopfile[1024];
|
|
|
|
snprintf(stopfile,1024,"%s/doshutdown",instancepath);
|
|
|
|
FILE *f=fopen(stopfile,"w");
|
|
|
|
if (!f) {
|
|
|
|
WHY("Could not create shutdown file");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
int result=kill(pid,SIGHUP);
|
|
|
|
if (!result) {
|
|
|
|
fprintf(stderr,"Stop request sent to Serval process.\n");
|
|
|
|
} else {
|
|
|
|
WHY("Could not send SIGHUP to Serval process.");
|
|
|
|
switch (errno) {
|
|
|
|
case EINVAL: WHY("This is embarassing, but the operating system says I don't know how to send a signal."); break;
|
|
|
|
case EPERM: WHY("I don't have permission to stop the Serval process. You could try using sudo, or run the stop command as the appropriate user."); break;
|
2012-03-04 23:05:12 +00:00
|
|
|
case ESRCH: WHY("The process id I have recorded doesn't seem to exist anymore. Did someone kill the process without telling me?");
|
|
|
|
/* Clean up any lingering mess */
|
|
|
|
servalShutdownCleanly();
|
|
|
|
break;
|
2012-03-04 22:57:31 +00:00
|
|
|
default:
|
|
|
|
perror("This is reason given by the operating system");
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allow a few seconds for the process to die, and keep an eye on things
|
|
|
|
while this is happening. */
|
|
|
|
time_t timeout=time(0)+5;
|
|
|
|
while(timeout>time(0)) {
|
|
|
|
pid=-1;
|
|
|
|
int running = servalNodeRunning(&pid,instancepath);
|
|
|
|
if (running<1) {
|
|
|
|
fprintf(stderr,"Serval process appears to have stopped.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return WHY("I tried to stop it, but it seems that the Serval process is still running.");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return WHY("Serval process for that instance does not appear to be running.");
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:36:08 +00:00
|
|
|
return WHY("Not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
int app_server_status(int argc,char **argv,struct command_line_option *o)
|
|
|
|
{
|
|
|
|
char *instancepath
|
|
|
|
=cli_arg(argc,argv,o,"instance path",getenv("SERVALINSTANCE_PATH"));
|
|
|
|
if (!instancepath) instancepath=DEFAULT_INSTANCE_PATH;
|
|
|
|
|
2012-02-23 01:25:39 +00:00
|
|
|
char filename[1024];
|
|
|
|
char line[1024];
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
/* Display configuration information */
|
|
|
|
snprintf(filename,1023,"%s/serval.conf",instancepath); filename[1023]=0;
|
|
|
|
f=fopen(filename,"r");
|
|
|
|
if (f) {
|
|
|
|
line[0]=0; fgets(line,1024,f);
|
|
|
|
printf("\nServal Mesh configuration:\n");
|
|
|
|
while(line[0]) {
|
|
|
|
printf(" %s",line);
|
|
|
|
line[0]=0; fgets(line,1024,f);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
/* Display running status of daemon from serval.pid file */
|
|
|
|
int pid=-1;
|
|
|
|
int running = servalNodeRunning(&pid,instancepath);
|
2012-03-04 22:57:31 +00:00
|
|
|
if (running<0) return -1;
|
2012-02-23 01:25:39 +00:00
|
|
|
|
2012-02-23 02:08:19 +00:00
|
|
|
printf("For Serval Mesh instance %s:\n",instancepath);
|
2012-02-23 01:25:39 +00:00
|
|
|
if (running)
|
2012-02-23 02:08:19 +00:00
|
|
|
printf(" Serval mesh process is running (pid=%d)\n",pid);
|
|
|
|
else
|
|
|
|
printf(" Serval Mesh process not running\n");
|
|
|
|
|
2012-02-23 01:25:39 +00:00
|
|
|
return 0;
|
2012-02-16 14:05:28 +00:00
|
|
|
}
|
|
|
|
|
2012-03-18 22:57:06 +00:00
|
|
|
int app_mdp_ping(int argc,char **argv,struct command_line_option *o)
|
|
|
|
{
|
|
|
|
char *sid=cli_arg(argc,argv,o,"SID|broadcast","broadcast");
|
|
|
|
char *instancepath=serval_instancepath();
|
|
|
|
|
|
|
|
/* Open socket to MDP */
|
|
|
|
|
|
|
|
int sock;
|
|
|
|
struct sockaddr_un name;
|
|
|
|
char mdp_socket_name[101];
|
|
|
|
mdp_socket_name[100]=0;
|
|
|
|
snprintf(mdp_socket_name,100,"%s/mdp.socket",instancepath);
|
|
|
|
if (mdp_socket_name[100]) {
|
|
|
|
return WHY("instance path is too long (unix domain named sockets have a short maximum path length)");
|
|
|
|
}
|
|
|
|
|
|
|
|
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
|
|
|
|
if (sock < 0) {
|
|
|
|
perror("opening datagram socket");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* Construct name of socket to send to. */
|
|
|
|
name.sun_family = AF_UNIX;
|
|
|
|
strcpy(name.sun_path, mdp_socket_name);
|
|
|
|
close(sock);
|
|
|
|
|
|
|
|
return WHY("Not implmented");
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:05:28 +00:00
|
|
|
/* NULL marks ends of command structure.
|
|
|
|
"<anystring>" marks an arg that can take any value.
|
|
|
|
Only exactly matching prototypes will be used.
|
|
|
|
Together with the description, this makes it easy for us to auto-generate the
|
|
|
|
list of valid command line formats for display to the user if they try an
|
|
|
|
invalid one. It also means we can do away with getopt() etc.
|
|
|
|
|
|
|
|
Keep this list alphabetically sorted for user convenience.
|
|
|
|
*/
|
|
|
|
command_line_option command_line_options[]={
|
|
|
|
{app_dna_lookup,{"dna","lookup","<did>",NULL},CLIFLAG_NONOVERLAY,"Lookup the SIP/MDP address of the supplied telephone number (DID)."},
|
2012-02-23 01:25:39 +00:00
|
|
|
{cli_usage,{"help",NULL},0,
|
|
|
|
"Display command usage."},
|
|
|
|
{app_server_start,{"node","start"},CLIFLAG_STANDALONE,
|
|
|
|
"Start Serval Mesh node process. Instance path is read from SERVALINSTANCE_PATH environment variable."},
|
2012-03-04 22:57:31 +00:00
|
|
|
{app_server_start,{"node","start","foreground"},CLIFLAG_STANDALONE,
|
|
|
|
"Start Serval Mesh node process without detatching from foreground."},
|
2012-02-23 01:25:39 +00:00
|
|
|
{app_server_start,{"node","start","in","<instance path>"},CLIFLAG_STANDALONE,
|
|
|
|
"Start Serval Mesh node process. Instance path is as specified."},
|
|
|
|
{app_server_stop,{"node","stop"},0,
|
|
|
|
"Ask running Serval Mesh node process to stop. Instance path is read from SERVALINSTANCE_PATH environment variable."},
|
|
|
|
{app_server_stop,{"node","stop","in","<instance path>"},0,
|
|
|
|
"Ask running Serval Mesh node process to stop. Instance path as specified."},
|
|
|
|
{app_server_status,{"node","status"},0,
|
|
|
|
"Display information about any running Serval Mesh node."},
|
2012-03-18 22:57:06 +00:00
|
|
|
{app_mdp_ping,{"mdp","ping","<SID|broadcast>"},CLIFLAG_STANDALONE,
|
|
|
|
"Attempts to ping specified node via Mesh Datagram Protocol (MDP)."},
|
2012-02-16 14:05:28 +00:00
|
|
|
{NULL,{NULL}}
|
|
|
|
};
|