2012-04-20 07:32:29 +00:00
|
|
|
/*
|
2012-02-16 14:05:28 +00:00
|
|
|
Serval Distributed Numbering Architecture (DNA)
|
2012-04-20 07:32:29 +00:00
|
|
|
Copyright (C) 2010-2012 Paul Gardner-Stephen
|
2012-02-16 14:05:28 +00:00
|
|
|
|
|
|
|
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-04-10 08:42:17 +00:00
|
|
|
#define _GNU_SOURCE // For asprintf()
|
2012-03-04 22:57:31 +00:00
|
|
|
#include <sys/stat.h>
|
2012-04-02 08:12:40 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
2012-04-10 08:42:17 +00:00
|
|
|
#include <time.h>
|
2012-04-13 08:17:20 +00:00
|
|
|
#include <unistd.h>
|
2012-04-23 02:35:00 +00:00
|
|
|
#include <stdarg.h>
|
2012-04-19 08:33:04 +00:00
|
|
|
#include <stdlib.h>
|
2012-04-10 08:42:17 +00:00
|
|
|
#include <stdio.h>
|
2012-04-20 07:51:03 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2012-04-20 07:32:29 +00:00
|
|
|
#include <jni.h>
|
2012-04-20 07:51:03 +00:00
|
|
|
#endif
|
2012-02-23 02:15:42 +00:00
|
|
|
#include "serval.h"
|
2012-04-02 08:12:40 +00:00
|
|
|
#include "rhizome.h"
|
2012-02-16 14:05:28 +00:00
|
|
|
|
2012-03-29 03:37:07 +00:00
|
|
|
static int servalNodeRunning(int *pid)
|
2012-02-23 01:25:39 +00:00
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *instancepath = serval_instancepath();
|
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-03-29 03:37:07 +00:00
|
|
|
char filename[1024];
|
|
|
|
if (FORM_SERVAL_INSTANCE_PATH(filename, "serval.pid")) {
|
|
|
|
FILE *f=fopen(filename,"r");
|
|
|
|
if (f) {
|
|
|
|
char line[1024];
|
|
|
|
line[0]=0; fgets(line,1024,f);
|
|
|
|
*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.*/
|
|
|
|
running=1; // assume pid means is running for now
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
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-04-23 07:42:10 +00:00
|
|
|
/* Data structures for accumulating output of a single JNI call.
|
|
|
|
*/
|
|
|
|
|
2012-04-24 08:33:52 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
|
|
|
|
2012-04-30 08:15:24 +00:00
|
|
|
#define OUTV_BUFFER_ALLOCSIZE (8192)
|
2012-04-23 07:42:10 +00:00
|
|
|
|
2012-04-24 07:36:48 +00:00
|
|
|
JNIEnv *jni_env = NULL;
|
|
|
|
int jni_exception = 0;
|
2012-04-23 07:42:10 +00:00
|
|
|
|
2012-04-30 08:15:24 +00:00
|
|
|
jobject outv_list = NULL;
|
|
|
|
jmethodID listAddMethodId = NULL;
|
2012-04-23 07:42:10 +00:00
|
|
|
|
2012-04-24 07:36:48 +00:00
|
|
|
char *outv_buffer = NULL;
|
2012-04-23 07:42:10 +00:00
|
|
|
char *outv_current = NULL;
|
|
|
|
char *outv_limit = NULL;
|
|
|
|
|
2012-04-24 07:36:48 +00:00
|
|
|
static int outv_growbuf(size_t needed)
|
2012-04-23 07:42:10 +00:00
|
|
|
{
|
2012-04-24 07:36:48 +00:00
|
|
|
size_t newsize = (outv_limit - outv_current < needed) ? (outv_limit - outv_buffer) + needed : 0;
|
|
|
|
if (newsize) {
|
2012-04-30 08:15:24 +00:00
|
|
|
// Round up to nearest multiple of OUTV_BUFFER_ALLOCSIZE.
|
|
|
|
newsize = newsize + OUTV_BUFFER_ALLOCSIZE - ((newsize - 1) % OUTV_BUFFER_ALLOCSIZE + 1);
|
2012-04-24 07:36:48 +00:00
|
|
|
size_t length = outv_current - outv_buffer;
|
|
|
|
outv_buffer = realloc(outv_buffer, newsize);
|
|
|
|
if (outv_buffer == NULL)
|
|
|
|
return WHYF("Out of memory allocating %lu bytes", (unsigned long) newsize);
|
|
|
|
outv_current = outv_buffer + length;
|
|
|
|
outv_limit = outv_buffer + newsize;
|
2012-04-23 07:42:10 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-24 07:36:48 +00:00
|
|
|
static int outv_end_field()
|
2012-04-23 07:42:10 +00:00
|
|
|
{
|
2012-04-24 07:36:48 +00:00
|
|
|
outv_growbuf(1);
|
|
|
|
*outv_current++ = '\0';
|
2012-04-30 08:15:24 +00:00
|
|
|
jstring str = (jstring)(*jni_env)->NewStringUTF(jni_env, outv_buffer);
|
2012-04-24 07:36:48 +00:00
|
|
|
outv_current = outv_buffer;
|
2012-04-30 08:15:24 +00:00
|
|
|
if (str == NULL) {
|
2012-04-24 07:36:48 +00:00
|
|
|
jni_exception = 1;
|
|
|
|
return WHY("Exception thrown from NewStringUTF()");
|
|
|
|
}
|
2012-04-30 08:15:24 +00:00
|
|
|
(*jni_env)->CallBooleanMethod(jni_env, outv_list, listAddMethodId, str);
|
|
|
|
if ((*jni_env)->ExceptionOccurred(jni_env)) {
|
|
|
|
jni_exception = 1;
|
|
|
|
return WHY("Exception thrown from CallBooleanMethod()");
|
|
|
|
}
|
2012-04-23 07:42:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-04-20 07:32:29 +00:00
|
|
|
|
|
|
|
/* JNI entry point to command line. See org.servalproject.servald.ServalD class for the Java side.
|
2012-04-30 08:15:24 +00:00
|
|
|
JNI method descriptor: "(Ljava/util/List;[Ljava/lang/String;)I"
|
2012-04-20 07:32:29 +00:00
|
|
|
*/
|
2012-04-30 08:15:24 +00:00
|
|
|
JNIEXPORT jint JNICALL Java_org_servalproject_servald_ServalD_rawCommand(JNIEnv *env, jobject this, jobject outv, jobjectArray args)
|
2012-04-20 07:32:29 +00:00
|
|
|
{
|
|
|
|
jclass stringClass = NULL;
|
2012-04-30 08:15:24 +00:00
|
|
|
jclass listClass = NULL;
|
2012-04-24 07:36:48 +00:00
|
|
|
jint status = 0;
|
2012-04-23 07:42:10 +00:00
|
|
|
// Enforce non re-entrancy.
|
2012-04-24 07:36:48 +00:00
|
|
|
if (jni_env) {
|
2012-04-23 07:42:10 +00:00
|
|
|
jclass exceptionClass = NULL;
|
2012-04-30 08:15:24 +00:00
|
|
|
if ((exceptionClass = (*env)->FindClass(env, "java/lang/IllegalStateException")) == NULL)
|
|
|
|
return -1; // exception
|
2012-04-23 07:42:10 +00:00
|
|
|
(*env)->ThrowNew(env, exceptionClass, "re-entrancy not supported");
|
2012-04-30 08:15:24 +00:00
|
|
|
return -1;
|
2012-04-23 07:42:10 +00:00
|
|
|
}
|
2012-04-24 07:36:48 +00:00
|
|
|
// Get some handles to some classes and methods that we use later on.
|
2012-04-20 07:32:29 +00:00
|
|
|
if ((stringClass = (*env)->FindClass(env, "java/lang/String")) == NULL)
|
2012-04-30 08:15:24 +00:00
|
|
|
return -1; // exception
|
|
|
|
if ((listClass = (*env)->FindClass(env, "java/util/List")) == NULL)
|
|
|
|
return -1; // exception
|
|
|
|
if ((listAddMethodId = (*env)->GetMethodID(env, listClass, "add", "(Ljava/lang/Object;)Z")) == NULL)
|
|
|
|
return -1; // exception
|
2012-04-24 07:36:48 +00:00
|
|
|
// Construct argv, argc from this method's arguments.
|
2012-04-20 07:32:29 +00:00
|
|
|
jsize len = (*env)->GetArrayLength(env, args);
|
2012-04-23 07:42:10 +00:00
|
|
|
const char **argv = malloc(sizeof(char*) * (len + 1));
|
|
|
|
if (argv == NULL) {
|
|
|
|
jclass exceptionClass = NULL;
|
|
|
|
if ((exceptionClass = (*env)->FindClass(env, "java/lang/OutOfMemoryError")) == NULL)
|
2012-04-30 08:15:24 +00:00
|
|
|
return -1; // exception
|
2012-04-23 07:42:10 +00:00
|
|
|
(*env)->ThrowNew(env, exceptionClass, "malloc returned NULL");
|
2012-04-30 08:15:24 +00:00
|
|
|
return -1;
|
2012-04-23 07:42:10 +00:00
|
|
|
}
|
2012-04-20 07:32:29 +00:00
|
|
|
jsize i;
|
2012-04-24 07:36:48 +00:00
|
|
|
for (i = 0; i <= len; ++i)
|
|
|
|
argv[i] = NULL;
|
|
|
|
int argc = len;
|
|
|
|
// From now on, in case of an exception we have to free some resources before
|
|
|
|
// returning.
|
|
|
|
jni_exception = 0;
|
|
|
|
for (i = 0; !jni_exception && i != len; ++i) {
|
2012-04-20 07:32:29 +00:00
|
|
|
const jstring arg = (jstring)(*env)->GetObjectArrayElement(env, args, i);
|
2012-04-24 07:36:48 +00:00
|
|
|
if (arg == NULL)
|
|
|
|
jni_exception = 1;
|
|
|
|
else {
|
|
|
|
const char *str = (*env)->GetStringUTFChars(env, arg, NULL);
|
|
|
|
if (str == NULL)
|
|
|
|
jni_exception = 1;
|
|
|
|
else
|
|
|
|
argv[i] = str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!jni_exception) {
|
|
|
|
// Set up the output buffer.
|
2012-04-30 08:15:24 +00:00
|
|
|
outv_list = outv;
|
2012-04-24 07:36:48 +00:00
|
|
|
outv_current = outv_buffer;
|
|
|
|
// Execute the command.
|
|
|
|
jni_env = env;
|
|
|
|
status = parseCommandLine(argc, argv);
|
|
|
|
jni_env = NULL;
|
|
|
|
}
|
|
|
|
// Release argv Java string buffers.
|
|
|
|
for (i = 0; i != len; ++i) {
|
|
|
|
if (argv[i]) {
|
|
|
|
const jstring arg = (jstring)(*env)->GetObjectArrayElement(env, args, i);
|
|
|
|
(*env)->ReleaseStringUTFChars(env, arg, argv[i]);
|
|
|
|
}
|
2012-04-23 07:42:10 +00:00
|
|
|
}
|
|
|
|
free(argv);
|
2012-04-24 07:36:48 +00:00
|
|
|
// Deal with Java exceptions: NewStringUTF out of memory in outv_end_field().
|
|
|
|
if (jni_exception || (outv_current != outv_buffer && outv_end_field() == -1))
|
2012-04-30 08:15:24 +00:00
|
|
|
return -1;
|
|
|
|
return status;
|
2012-04-20 07:32:29 +00:00
|
|
|
}
|
2012-04-23 02:35:00 +00:00
|
|
|
|
|
|
|
#endif /* HAVE_JNI_H */
|
2012-04-20 07:32:29 +00:00
|
|
|
|
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. */
|
2012-04-23 07:42:10 +00:00
|
|
|
int parseCommandLine(int argc, const char *const *args)
|
2012-02-16 14:05:28 +00:00
|
|
|
{
|
2012-03-29 07:21:59 +00:00
|
|
|
int i;
|
2012-02-16 14:05:28 +00:00
|
|
|
int ambiguous=0;
|
|
|
|
int cli_call=-1;
|
|
|
|
for(i=0;command_line_options[i].function;i++)
|
|
|
|
{
|
2012-03-29 07:21:59 +00:00
|
|
|
int j;
|
|
|
|
const char *word = NULL;
|
2012-04-19 08:33:04 +00:00
|
|
|
int optional = 0;
|
|
|
|
int mandatory = 0;
|
|
|
|
for (j = 0; (word = command_line_options[i].words[j]); ++j) {
|
|
|
|
int wordlen = strlen(word);
|
2012-04-23 08:55:26 +00:00
|
|
|
if (optional < 0) {
|
|
|
|
fprintf(stderr,"Internal error: command_line_options[%d].word[%d]=\"%s\" not allowed after \"...\"\n", i, j, word);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!( (wordlen > 2 && word[0] == '<' && word[wordlen-1] == '>')
|
|
|
|
|| (wordlen > 4 && word[0] == '[' && word[1] == '<' && word[wordlen-2] == '>' && word[wordlen-1] == ']')
|
|
|
|
|| (wordlen > 0)
|
2012-04-19 08:33:04 +00:00
|
|
|
)) {
|
|
|
|
fprintf(stderr,"Internal error: command_line_options[%d].word[%d]=\"%s\" is malformed\n", i, j, word);
|
2012-02-16 14:05:28 +00:00
|
|
|
break;
|
2012-04-19 08:33:04 +00:00
|
|
|
} else if (word[0] == '<') {
|
|
|
|
++mandatory;
|
|
|
|
if (optional) {
|
|
|
|
fprintf(stderr,"Internal error: command_line_options[%d].word[%d]=\"%s\" should be optional\n", i, j, word);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (word[0] == '[') {
|
|
|
|
++optional;
|
2012-04-23 08:55:26 +00:00
|
|
|
} else if (wordlen == 3 && word[0] == '.' && word[1] == '.' && word[2] == '.') {
|
|
|
|
optional = -1;
|
2012-04-19 08:33:04 +00:00
|
|
|
} else {
|
|
|
|
++mandatory;
|
|
|
|
if (j < argc && strcasecmp(word, args[j])) // literal words don't match
|
|
|
|
break;
|
2012-02-16 14:05:28 +00:00
|
|
|
}
|
2012-03-29 07:21:59 +00:00
|
|
|
}
|
2012-04-23 08:55:26 +00:00
|
|
|
if (!word && argc >= mandatory && (optional < 0 || argc <= mandatory + optional)) {
|
2012-04-19 08:33:04 +00:00
|
|
|
/* A match! We got through the command definition with no internal errors and all literal
|
|
|
|
args matched and we have a proper number of args. If we have multiple matches, then note
|
|
|
|
that the call is ambiguous. */
|
2012-02-16 14:05:28 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 08:33:04 +00:00
|
|
|
|
2012-02-16 14:05:28 +00:00
|
|
|
/* Don't process ambiguous calls */
|
|
|
|
if (ambiguous) return -1;
|
|
|
|
/* Complain if we found no matching calls */
|
2012-04-26 07:29:55 +00:00
|
|
|
if (cli_call<0) {
|
|
|
|
fprintf(stderr,"Unknown command line call:\n ");
|
|
|
|
int j; for(j=0;j<argc;j++) fprintf(stderr," %s",args[j]);
|
|
|
|
return cli_usage();
|
|
|
|
}
|
2012-02-16 14:05:28 +00:00
|
|
|
|
|
|
|
/* Otherwise, make call */
|
2012-04-12 09:00:52 +00:00
|
|
|
setVerbosity(confValueGet("debug",""));
|
2012-04-23 07:42:10 +00:00
|
|
|
return command_line_options[cli_call].function(argc, args, &command_line_options[cli_call]);
|
2012-02-16 14:05:28 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int cli_arg(int argc, const char *const *argv, command_line_option *o, char *argname, const char **dst, int (*validator)(const char *arg), char *defaultvalue)
|
2012-04-19 08:33:04 +00:00
|
|
|
{
|
|
|
|
int arglen = strlen(argname);
|
|
|
|
int i;
|
|
|
|
const char *word;
|
|
|
|
for(i = 0; (word = o->words[i]); ++i) {
|
|
|
|
int wordlen = strlen(word);
|
|
|
|
/* No need to check that the "<...>" and "[<...>]" are all intact in the command_line_option,
|
|
|
|
because that was already checked in parseCommandLine(). */
|
|
|
|
if (i < argc
|
|
|
|
&&( (wordlen == arglen + 2 && word[0] == '<' && !strncasecmp(&word[1], argname, arglen))
|
|
|
|
|| (wordlen == arglen + 4 && word[0] == '[' && !strncasecmp(&word[2], argname, arglen)))
|
|
|
|
) {
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *value = argv[i];
|
2012-04-19 08:33:04 +00:00
|
|
|
if (validator && !(*validator)(value)) {
|
|
|
|
fprintf(stderr, "Invalid argument %d '%s': \"%s\"\n", i, argname, value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*dst = value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* No matching valid 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 option 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. */
|
|
|
|
*dst = defaultvalue;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
/* Write a single character to output. If in a JNI call, then this appends the character to the
|
|
|
|
current output field. Returns the character written cast to an unsigned char then to int, or EOF
|
|
|
|
on error.
|
|
|
|
*/
|
|
|
|
int cli_putchar(char c)
|
|
|
|
{
|
2012-04-24 08:33:52 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2012-04-24 07:36:48 +00:00
|
|
|
if (jni_env) {
|
2012-04-23 07:42:10 +00:00
|
|
|
if (outv_current == outv_limit && outv_growbuf(1) == -1)
|
|
|
|
return EOF;
|
|
|
|
*outv_current++ = c;
|
|
|
|
return (unsigned char) c;
|
|
|
|
}
|
|
|
|
else
|
2012-04-24 08:33:52 +00:00
|
|
|
#endif
|
2012-04-23 07:42:10 +00:00
|
|
|
return putchar(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a null-terminated string to output. If in a JNI call, then this appends the string to the
|
|
|
|
current output field. The terminating null is not included. Returns a non-negative integer on
|
|
|
|
success, EOF on error.
|
|
|
|
*/
|
|
|
|
int cli_puts(const char *str)
|
|
|
|
{
|
2012-04-24 08:33:52 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2012-04-24 07:36:48 +00:00
|
|
|
if (jni_env) {
|
2012-04-23 07:42:10 +00:00
|
|
|
size_t len = strlen(str);
|
|
|
|
size_t avail = outv_limit - outv_current;
|
|
|
|
if (avail < len) {
|
|
|
|
strncpy(outv_current, str, avail);
|
|
|
|
outv_current = outv_limit;
|
|
|
|
if (outv_growbuf(len) == -1)
|
|
|
|
return EOF;
|
|
|
|
len -= avail;
|
|
|
|
str += avail;
|
|
|
|
}
|
|
|
|
strncpy(outv_current, str, len);
|
|
|
|
outv_current += len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
2012-04-24 08:33:52 +00:00
|
|
|
#endif
|
2012-04-23 07:42:10 +00:00
|
|
|
return fputs(str, stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a formatted string to output. If in a JNI call, then this appends the string to the
|
|
|
|
current output field, excluding the terminating null. Returns the number of bytes
|
|
|
|
written/appended, or -1 on error.
|
|
|
|
*/
|
2012-04-23 02:35:00 +00:00
|
|
|
int cli_printf(const char *fmt, ...)
|
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
int ret = 0;
|
2012-04-23 02:35:00 +00:00
|
|
|
va_list ap,ap2;
|
|
|
|
va_start(ap,fmt);
|
|
|
|
va_copy(ap2,ap);
|
2012-04-24 08:33:52 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2012-04-24 07:36:48 +00:00
|
|
|
if (jni_env) {
|
2012-04-23 07:42:10 +00:00
|
|
|
size_t avail = outv_limit - outv_current;
|
|
|
|
int count = vsnprintf(outv_current, avail, fmt, ap2);
|
|
|
|
if (count >= avail) {
|
|
|
|
if (outv_growbuf(count) == -1)
|
|
|
|
return -1;
|
|
|
|
vsprintf(outv_current, fmt, ap2);
|
|
|
|
}
|
|
|
|
outv_current += count;
|
|
|
|
ret = count;
|
|
|
|
} else
|
2012-04-24 08:33:52 +00:00
|
|
|
#endif
|
2012-04-23 07:42:10 +00:00
|
|
|
ret = vfprintf(stdout, fmt, ap2);
|
2012-04-23 02:35:00 +00:00
|
|
|
va_end(ap);
|
2012-04-23 07:42:10 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delimit the current output field. This closes the current field, so that the next cli_ output
|
|
|
|
function will start appending to a new field. Returns 0 on success, -1 on error. If not in a
|
|
|
|
JNI call, then this simply writes a newline to standard output (or the value of the
|
|
|
|
SERVALD_OUTPUT_DELIMITER env var if set).
|
|
|
|
*/
|
2012-04-24 08:20:27 +00:00
|
|
|
int cli_delim(const char *opt)
|
2012-04-23 07:42:10 +00:00
|
|
|
{
|
2012-04-24 08:33:52 +00:00
|
|
|
#ifdef HAVE_JNI_H
|
2012-04-24 07:36:48 +00:00
|
|
|
if (jni_env) {
|
2012-04-30 08:15:24 +00:00
|
|
|
return outv_end_field();
|
2012-04-24 08:33:52 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *delim = getenv("SERVALD_OUTPUT_DELIMITER");
|
|
|
|
if (delim == NULL)
|
2012-04-24 08:20:27 +00:00
|
|
|
delim = opt ? opt : "\n";
|
2012-04-23 07:42:10 +00:00
|
|
|
fputs(delim, stdout);
|
|
|
|
}
|
2012-04-23 02:35:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-23 08:55:26 +00:00
|
|
|
int app_echo(int argc, const char *const *argv, struct command_line_option *o)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
cli_puts(argv[i]);
|
2012-04-24 08:20:27 +00:00
|
|
|
cli_delim(NULL);
|
2012-04-23 08:55:26 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_dna_lookup(int argc, const char *const *argv, struct command_line_option *o)
|
2012-02-16 14:05:28 +00:00
|
|
|
{
|
2012-04-25 05:54:21 +00:00
|
|
|
int i;
|
2012-03-29 07:21:59 +00:00
|
|
|
/* Create the instance directory if it does not yet exist */
|
|
|
|
if (create_serval_instance_dir() == -1)
|
|
|
|
return -1;
|
2012-04-24 20:46:31 +00:00
|
|
|
|
2012-04-25 05:54:21 +00:00
|
|
|
const char *did;
|
|
|
|
if (cli_arg(argc, argv, o, "did", &did, NULL, "*") == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Bind to MDP socket and await confirmation */
|
|
|
|
unsigned char srcsid[SID_SIZE];
|
|
|
|
int port=32768+(random()&32767);
|
|
|
|
if (overlay_mdp_getmyaddr(0,srcsid)) return WHY("Could not get local address");
|
2012-04-25 10:44:56 +00:00
|
|
|
printf("binding to %s:%d\n",
|
|
|
|
overlay_render_sid(srcsid),port);
|
2012-04-25 05:54:21 +00:00
|
|
|
if (overlay_mdp_bind(srcsid,port)) return WHY("Could not bind to MDP socket");
|
|
|
|
WHY("bound port");
|
|
|
|
|
2012-04-24 20:46:31 +00:00
|
|
|
/* use MDP to send the lookup request to MDP_PORT_DNALOOKUP, and wait for
|
|
|
|
replies. */
|
2012-04-25 05:54:21 +00:00
|
|
|
overlay_mdp_frame mdp;
|
|
|
|
bzero(&mdp,sizeof(mdp));
|
2012-04-24 20:46:31 +00:00
|
|
|
|
2012-04-25 06:25:04 +00:00
|
|
|
WHY("polling network");
|
|
|
|
|
2012-04-25 05:54:21 +00:00
|
|
|
/* Now repeatedly send resolution request and collect results until we reach
|
|
|
|
timeout. */
|
|
|
|
unsigned long long timeout=overlay_gettime_ms()+3000;
|
|
|
|
unsigned long long last_tx=0;
|
|
|
|
while(timeout>overlay_gettime_ms())
|
|
|
|
{
|
|
|
|
unsigned long long now=overlay_gettime_ms();
|
|
|
|
if ((last_tx+125)<now)
|
|
|
|
{
|
2012-04-25 10:44:56 +00:00
|
|
|
mdp.packetTypeAndFlags=MDP_TX|MDP_NOCRYPT;
|
|
|
|
|
|
|
|
/* set source address to a local address, and pick a random port */
|
|
|
|
mdp.out.src.port=port;
|
|
|
|
bcopy(&srcsid[0],&mdp.out.src.sid[0],SID_SIZE);
|
|
|
|
|
|
|
|
/* Send to broadcast address and DNA lookup port */
|
|
|
|
for(i=0;i<SID_SIZE;i++) mdp.out.dst.sid[i]=0xff;
|
|
|
|
mdp.out.dst.port=MDP_PORT_DNALOOKUP;
|
|
|
|
|
|
|
|
/* put DID into packet */
|
|
|
|
bcopy(did,&mdp.out.payload[0],strlen(did)+1);
|
|
|
|
mdp.out.payload_length=strlen(did)+1;
|
|
|
|
|
2012-04-25 05:54:21 +00:00
|
|
|
overlay_mdp_send(&mdp,0,0);
|
|
|
|
last_tx=now;
|
|
|
|
}
|
|
|
|
long long short_timeout=125;
|
|
|
|
while(short_timeout>0) {
|
|
|
|
if (overlay_mdp_client_poll(short_timeout))
|
|
|
|
{
|
|
|
|
overlay_mdp_frame rx;
|
|
|
|
int ttl;
|
|
|
|
while (overlay_mdp_recv(&rx,&ttl)==0)
|
|
|
|
{
|
|
|
|
if (rx.packetTypeAndFlags==MDP_ERROR)
|
|
|
|
{
|
|
|
|
fprintf(stderr," Error message: %s\n",mdp.error.message);
|
|
|
|
}
|
2012-04-25 11:03:25 +00:00
|
|
|
else if ((rx.packetTypeAndFlags&MDP_TYPE_MASK)==MDP_TX)
|
2012-04-25 05:54:21 +00:00
|
|
|
fprintf(stderr,"%s:%s\n",
|
2012-04-25 11:03:25 +00:00
|
|
|
overlay_render_sid(&rx.in.src.sid[0]),
|
|
|
|
&rx.in.payload[0]);
|
|
|
|
else WHYF("packettype=0x%x",rx.packetTypeAndFlags);
|
2012-04-25 05:54:21 +00:00
|
|
|
if (servalShutdown) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (servalShutdown) break;
|
|
|
|
short_timeout=125-(overlay_gettime_ms()-now);
|
|
|
|
}
|
|
|
|
if (servalShutdown) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-02-16 14:36:08 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
char filename[1024];
|
2012-03-29 03:37:07 +00:00
|
|
|
if (!FORM_SERVAL_INSTANCE_PATH(filename, "serval.conf")) {
|
|
|
|
fprintf(stderr, "Using default value of %s: %s\n", var, defaultValue);
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
FILE *f = fopen(filename,"r");
|
|
|
|
if (!f) {
|
|
|
|
fprintf(stderr, "Cannot open serval.conf. Using default value of %s: %s\n", var, defaultValue);
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2012-03-04 22:57:31 +00:00
|
|
|
|
|
|
|
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-04-19 08:33:04 +00:00
|
|
|
int cli_absolute_path(const char *arg)
|
|
|
|
{
|
|
|
|
return arg[0] == '/' && arg[1] != '\0';
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_server_start(int argc, const char *const *argv, struct command_line_option *o)
|
2012-02-16 14:36:08 +00:00
|
|
|
{
|
2012-03-04 22:57:31 +00:00
|
|
|
/* Process optional arguments */
|
2012-04-25 21:44:01 +00:00
|
|
|
int foregroundP= (argc >= 2 && !strcasecmp(argv[1], "foreground"));
|
2012-04-19 08:33:04 +00:00
|
|
|
if (cli_arg(argc, argv, o, "instance path", &thisinstancepath, cli_absolute_path, NULL) == -1)
|
|
|
|
return -1;
|
2012-03-16 22:32:37 +00:00
|
|
|
|
2012-03-29 04:33:17 +00:00
|
|
|
/* Create the instance directory if it does not yet exist */
|
|
|
|
if (create_serval_instance_dir() == -1)
|
|
|
|
return -1;
|
|
|
|
|
2012-03-19 03:56:14 +00:00
|
|
|
/* Now that we know our instance path, we can ask for the default set of
|
|
|
|
network interfaces that we will take interest in. */
|
|
|
|
overlay_interface_args(confValueGet("interfaces",""));
|
|
|
|
if (strlen(confValueGet("interfaces",""))<1) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: Noone has told me which network interfaces to listen on.\n"
|
|
|
|
" You should probably put something in the interfaces setting.\n");
|
|
|
|
}
|
|
|
|
|
2012-03-04 22:57:31 +00:00
|
|
|
int pid=-1;
|
2012-03-29 03:37:07 +00:00
|
|
|
int running = servalNodeRunning(&pid);
|
2012-03-04 22:57:31 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
2012-03-29 07:21:59 +00:00
|
|
|
rhizome_datastore_path = serval_instancepath();
|
2012-03-04 22:57:31 +00:00
|
|
|
rhizome_opendb();
|
|
|
|
|
|
|
|
overlayMode=1;
|
2012-04-12 23:55:03 +00:00
|
|
|
return server(NULL,foregroundP);
|
2012-02-16 14:36:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_server_stop(int argc, const char *const *argv, struct command_line_option *o)
|
2012-02-16 14:36:08 +00:00
|
|
|
{
|
2012-04-19 08:33:04 +00:00
|
|
|
if (cli_arg(argc, argv, o, "instance path", &thisinstancepath, cli_absolute_path, NULL) == -1)
|
|
|
|
return -1;
|
2012-03-04 22:57:31 +00:00
|
|
|
|
2012-04-30 06:04:41 +00:00
|
|
|
int tries=0;
|
2012-03-04 22:57:31 +00:00
|
|
|
|
2012-04-30 06:04:41 +00:00
|
|
|
for(tries=0;tries<3;tries++)
|
|
|
|
{
|
|
|
|
int pid=-1;
|
2012-03-29 03:37:07 +00:00
|
|
|
int running = servalNodeRunning(&pid);
|
2012-04-30 06:04:41 +00:00
|
|
|
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];
|
|
|
|
FILE *f;
|
|
|
|
if (!(FORM_SERVAL_INSTANCE_PATH(stopfile, "doshutdown") && (f = fopen(stopfile, "w")))) {
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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)+2;
|
|
|
|
while(timeout>time(0)) {
|
|
|
|
pid=-1;
|
|
|
|
int running = servalNodeRunning(&pid);
|
|
|
|
if (running<1) {
|
|
|
|
fprintf(stderr,"Serval process appears to have stopped.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return WHY("Serval process for that instance does not appear to be running.");
|
2012-03-04 22:57:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 06:04:41 +00:00
|
|
|
return WHY("Tried to stop servald without success");
|
2012-02-16 14:36:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_server_status(int argc, const char *const *argv, struct command_line_option *o)
|
2012-02-16 14:36:08 +00:00
|
|
|
{
|
2012-04-19 08:33:04 +00:00
|
|
|
if (cli_arg(argc, argv, o, "instance path", &thisinstancepath, cli_absolute_path, NULL) == -1)
|
|
|
|
return -1;
|
2012-02-16 14:36:08 +00:00
|
|
|
|
2012-03-29 03:37:07 +00:00
|
|
|
/* Display configuration information */
|
2012-02-23 01:25:39 +00:00
|
|
|
char filename[1024];
|
|
|
|
FILE *f;
|
2012-03-29 03:37:07 +00:00
|
|
|
if (FORM_SERVAL_INSTANCE_PATH(filename, "serval.conf") && (f = fopen(filename, "r"))) {
|
|
|
|
char line[1024];
|
2012-02-23 01:25:39 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-03-29 03:37:07 +00:00
|
|
|
|
2012-02-23 01:25:39 +00:00
|
|
|
/* Display running status of daemon from serval.pid file */
|
|
|
|
int pid=-1;
|
2012-03-29 03:37:07 +00:00
|
|
|
int running = servalNodeRunning(&pid);
|
2012-03-04 22:57:31 +00:00
|
|
|
if (running<0) return -1;
|
2012-02-23 01:25:39 +00:00
|
|
|
|
2012-03-29 03:37:07 +00:00
|
|
|
printf("For Serval Mesh instance %s:\n", serval_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-04-23 07:42:10 +00:00
|
|
|
int app_mdp_ping(int argc, const char *const *argv, struct command_line_option *o)
|
2012-03-18 22:57:06 +00:00
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *sid;
|
2012-04-19 08:33:04 +00:00
|
|
|
if (cli_arg(argc, argv, o, "SID|broadcast", &sid, validateSid, "broadcast") == -1)
|
|
|
|
return -1;
|
2012-03-18 22:57:06 +00:00
|
|
|
|
2012-03-19 05:36:34 +00:00
|
|
|
overlay_mdp_frame mdp;
|
2012-04-25 05:54:21 +00:00
|
|
|
|
2012-03-19 05:36:34 +00:00
|
|
|
/* Bind to MDP socket and await confirmation */
|
2012-03-27 09:15:52 +00:00
|
|
|
unsigned char srcsid[SID_SIZE];
|
2012-04-25 05:54:21 +00:00
|
|
|
int port=32768+(random()&32767);
|
|
|
|
if (overlay_mdp_getmyaddr(0,srcsid)) return WHY("Could not get local address");
|
|
|
|
if (overlay_mdp_bind(srcsid,port)) return WHY("Could not bind to MDP socket");
|
2012-03-18 22:57:06 +00:00
|
|
|
|
2012-03-20 16:57:47 +00:00
|
|
|
/* First sequence number in the echo frames */
|
|
|
|
unsigned int firstSeq=random();
|
|
|
|
unsigned int sequence_number=firstSeq;
|
|
|
|
|
2012-03-27 08:55:38 +00:00
|
|
|
/* Get SID that we want to ping.
|
2012-03-28 02:56:14 +00:00
|
|
|
XXX - allow lookup of SID prefixes and telephone numbers
|
|
|
|
(that would require MDP lookup of phone numbers, which doesn't yet occur) */
|
2012-03-20 17:25:13 +00:00
|
|
|
int i;
|
2012-04-13 17:07:50 +00:00
|
|
|
int broadcast=0;
|
2012-03-20 17:25:13 +00:00
|
|
|
unsigned char ping_sid[SID_SIZE];
|
|
|
|
if (strcasecmp(sid,"broadcast")) {
|
|
|
|
stowSid(ping_sid,0,sid);
|
|
|
|
} else {
|
|
|
|
for(i=0;i<SID_SIZE;i++) ping_sid[i]=0xff;
|
2012-04-13 17:07:50 +00:00
|
|
|
broadcast=1;
|
2012-03-20 17:25:13 +00:00
|
|
|
}
|
|
|
|
|
2012-03-28 02:56:14 +00:00
|
|
|
/* XXX Eventually we should try to resolve SID to phone number and vice versa */
|
|
|
|
printf("MDP PING %s (%s): 12 data bytes\n",
|
|
|
|
overlay_render_sid(ping_sid),
|
|
|
|
overlay_render_sid(ping_sid));
|
|
|
|
|
2012-03-29 10:00:17 +00:00
|
|
|
long long rx_mintime=-1;
|
|
|
|
long long rx_maxtime=-1;
|
|
|
|
long long rx_count=0,tx_count=0;
|
|
|
|
long long rx_ms=0;
|
|
|
|
long long rx_times[1024];
|
|
|
|
|
2012-04-15 21:36:22 +00:00
|
|
|
if (broadcast)
|
|
|
|
fprintf(stderr,"WARNING: broadcast ping packets will not be encryped.\n");
|
2012-03-20 16:57:47 +00:00
|
|
|
while(1) {
|
|
|
|
/* Now send the ping packets */
|
2012-04-13 17:07:50 +00:00
|
|
|
mdp.packetTypeAndFlags=MDP_TX;
|
2012-04-15 21:36:22 +00:00
|
|
|
if (broadcast) mdp.packetTypeAndFlags|=MDP_NOCRYPT;
|
2012-03-27 09:15:52 +00:00
|
|
|
mdp.out.src.port=port;
|
|
|
|
bcopy(srcsid,mdp.out.src.sid,SID_SIZE);
|
2012-04-18 18:51:45 +00:00
|
|
|
bcopy(ping_sid,&mdp.out.dst.sid[0],SID_SIZE);
|
2012-03-20 16:57:47 +00:00
|
|
|
/* Set port to well known echo port (from /etc/services) */
|
|
|
|
mdp.out.dst.port=7;
|
2012-03-28 02:46:06 +00:00
|
|
|
mdp.out.payload_length=4+8;
|
2012-03-20 16:57:47 +00:00
|
|
|
int *seq=(int *)&mdp.out.payload;
|
|
|
|
*seq=sequence_number;
|
2012-03-28 02:46:06 +00:00
|
|
|
long long *txtime=(long long *)&mdp.out.payload[4];
|
|
|
|
*txtime=overlay_gettime_ms();
|
2012-03-20 16:57:47 +00:00
|
|
|
|
2012-03-28 02:40:55 +00:00
|
|
|
int res=overlay_mdp_send(&mdp,0,0);
|
2012-03-20 16:57:47 +00:00
|
|
|
if (res) {
|
|
|
|
fprintf(stderr,"ERROR: Could not dispatch PING frame #%d (error %d)\n",
|
|
|
|
sequence_number-firstSeq,res);
|
|
|
|
if (mdp.packetTypeAndFlags==MDP_ERROR)
|
|
|
|
fprintf(stderr," Error message: %s\n",mdp.error.message);
|
2012-03-29 10:00:17 +00:00
|
|
|
} else tx_count++;
|
2012-03-20 16:57:47 +00:00
|
|
|
|
|
|
|
/* Now look for replies until one second has passed, and print any replies
|
|
|
|
with appropriate information as required */
|
|
|
|
long long now=overlay_gettime_ms();
|
|
|
|
long long timeout=now+1000;
|
|
|
|
|
|
|
|
while(now<timeout) {
|
|
|
|
long long timeout_ms=timeout-overlay_gettime_ms();
|
2012-04-25 05:54:21 +00:00
|
|
|
int result = overlay_mdp_client_poll(timeout_ms);
|
2012-03-20 16:57:47 +00:00
|
|
|
|
|
|
|
if (result>0) {
|
2012-03-20 17:25:13 +00:00
|
|
|
int ttl=-1;
|
|
|
|
while (overlay_mdp_recv(&mdp,&ttl)==0) {
|
|
|
|
switch(mdp.packetTypeAndFlags&MDP_TYPE_MASK) {
|
|
|
|
case MDP_ERROR:
|
|
|
|
fprintf(stderr,"mdpping: overlay_mdp_recv: %s (code %d)\n",
|
|
|
|
mdp.error.message,mdp.error.error);
|
|
|
|
break;
|
2012-04-25 11:03:25 +00:00
|
|
|
case MDP_TX:
|
2012-03-28 02:46:06 +00:00
|
|
|
{
|
|
|
|
int *rxseq=(int *)&mdp.in.payload;
|
|
|
|
long long *txtime=(long long *)&mdp.in.payload[4];
|
2012-03-29 10:00:17 +00:00
|
|
|
long long delay=overlay_gettime_ms()-*txtime;
|
2012-04-14 13:48:18 +00:00
|
|
|
printf("%s: seq=%d time=%lld ms%s%s\n",
|
2012-04-18 18:51:45 +00:00
|
|
|
overlay_render_sid(mdp.in.src.sid),(*rxseq)-firstSeq+1,delay,
|
2012-04-14 13:48:18 +00:00
|
|
|
mdp.packetTypeAndFlags&MDP_NOCRYPT?"":" ENCRYPTED",
|
|
|
|
mdp.packetTypeAndFlags&MDP_NOSIGN?"":" SIGNED");
|
2012-04-18 18:51:45 +00:00
|
|
|
#warning put duplicate pong detection here so that stats work properly
|
2012-03-29 10:00:17 +00:00
|
|
|
rx_count++;
|
|
|
|
rx_ms+=delay;
|
|
|
|
if (rx_mintime>delay||rx_mintime==-1) rx_mintime=delay;
|
|
|
|
if (delay>rx_maxtime) rx_maxtime=delay;
|
|
|
|
rx_times[rx_count%1024]=delay;
|
2012-03-28 02:46:06 +00:00
|
|
|
}
|
2012-03-20 17:25:13 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-04-18 18:51:45 +00:00
|
|
|
fprintf(stderr,"mdpping: overlay_mdp_recv: Unexpected MDP frame type"
|
|
|
|
" 0x%x\n",mdp.packetTypeAndFlags);
|
2012-03-20 17:25:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-20 16:57:47 +00:00
|
|
|
}
|
|
|
|
now=overlay_gettime_ms();
|
|
|
|
if (servalShutdown) {
|
2012-03-29 10:00:17 +00:00
|
|
|
|
|
|
|
float rx_stddev=0;
|
|
|
|
float rx_mean=rx_ms*1.0/rx_count;
|
|
|
|
int samples=rx_count;
|
|
|
|
if (samples>1024) samples=1024;
|
|
|
|
int i;
|
|
|
|
for(i=0;i<samples;i++)
|
|
|
|
rx_stddev+=(rx_mean-rx_times[i])*(rx_mean-rx_times[i]);
|
|
|
|
rx_stddev/=samples;
|
|
|
|
rx_stddev=sqrtf(rx_stddev);
|
|
|
|
|
2012-03-20 16:57:47 +00:00
|
|
|
/* XXX Report final statistics before going */
|
2012-03-29 10:00:17 +00:00
|
|
|
fprintf(stderr,"--- %s ping statistics ---\n",overlay_render_sid(ping_sid));
|
|
|
|
fprintf(stderr,"%lld packets transmitted, %lld packets received, %3.1f%% packet loss\n",
|
|
|
|
tx_count,rx_count,tx_count?(tx_count-rx_count)*100.0/tx_count:0);
|
|
|
|
fprintf(stderr,"round-trip min/avg/max/stddev%s = %lld/%.3f/%lld/%.3f ms\n",
|
|
|
|
(samples<rx_count)?" (stddev calculated from last 1024 samples)":"",
|
|
|
|
rx_mintime,rx_mean,rx_maxtime,rx_stddev);
|
|
|
|
|
2012-03-20 16:57:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2012-03-28 02:40:55 +00:00
|
|
|
sequence_number++;
|
2012-03-20 16:57:47 +00:00
|
|
|
timeout=now+1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-03-18 22:57:06 +00:00
|
|
|
}
|
|
|
|
|
2012-03-29 04:51:30 +00:00
|
|
|
static int set_variable(const char *var, const char *val)
|
2012-03-19 03:56:14 +00:00
|
|
|
{
|
|
|
|
char conffile[1024];
|
2012-03-29 03:37:07 +00:00
|
|
|
FILE *in;
|
|
|
|
if (!FORM_SERVAL_INSTANCE_PATH(conffile, "serval.conf") ||
|
|
|
|
!((in = fopen(conffile, "r")) || (in = fopen(conffile, "w")))
|
|
|
|
) {
|
2012-03-29 04:51:30 +00:00
|
|
|
if (var)
|
|
|
|
return WHY("could not read configuration file.");
|
|
|
|
return -1;
|
2012-03-29 03:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char tempfile[1024];
|
|
|
|
FILE *out;
|
|
|
|
if (!FORM_SERVAL_INSTANCE_PATH(tempfile, "serval.conf.temp") ||
|
|
|
|
!(out = fopen(tempfile, "w"))
|
|
|
|
) {
|
2012-03-19 03:56:14 +00:00
|
|
|
fclose(in);
|
|
|
|
return WHY("could not write temporary file.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read and write lines of config file, replacing the variable in question
|
|
|
|
if required. If the variable didn't already exist, then write it out at
|
|
|
|
the end. */
|
2012-03-29 03:37:07 +00:00
|
|
|
char line[1024];
|
2012-03-19 03:56:14 +00:00
|
|
|
int found=0;
|
|
|
|
int varlen=strlen(var);
|
|
|
|
line[0]=0; fgets(line,1024,in);
|
|
|
|
while(line[0]) {
|
2012-03-29 04:51:30 +00:00
|
|
|
if (!strncasecmp(var, line, varlen) && line[varlen] == '=') {
|
2012-04-12 08:10:00 +00:00
|
|
|
if (!found && val)
|
2012-03-29 04:51:30 +00:00
|
|
|
fprintf(out, "%s=%s\n", var, val);
|
2012-04-12 08:10:00 +00:00
|
|
|
found = 1;
|
2012-03-29 04:51:30 +00:00
|
|
|
} else
|
2012-03-19 03:56:14 +00:00
|
|
|
fprintf(out,"%s",line);
|
|
|
|
line[0]=0; fgets(line,1024,in);
|
|
|
|
}
|
2012-03-29 04:51:30 +00:00
|
|
|
if (!found && val)
|
|
|
|
fprintf(out, "%s=%s\n", var, val);
|
2012-03-19 03:56:14 +00:00
|
|
|
fclose(in); fclose(out);
|
2012-03-29 04:51:30 +00:00
|
|
|
|
2012-03-19 03:56:14 +00:00
|
|
|
if (rename(tempfile,conffile)) {
|
2012-03-29 03:37:07 +00:00
|
|
|
return WHYF("Failed to rename \"%s\" to \"%s\".", tempfile, conffile);
|
2012-03-19 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-19 08:33:04 +00:00
|
|
|
int cli_configvarname(const char *arg)
|
|
|
|
{
|
|
|
|
if (arg[0] == '\0')
|
|
|
|
return 0;
|
|
|
|
const char *s;
|
|
|
|
for (s = arg; *s; ++s)
|
|
|
|
if (!(isalnum(*s) || *s == '_'))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_config_set(int argc, const char *const *argv, struct command_line_option *o)
|
2012-03-29 04:51:30 +00:00
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *var, *val;
|
2012-04-19 08:33:04 +00:00
|
|
|
if ( cli_arg(argc, argv, o, "variable", &var, cli_configvarname, NULL)
|
|
|
|
|| cli_arg(argc, argv, o, "value", &val, NULL, ""))
|
|
|
|
return -1;
|
2012-03-29 04:51:30 +00:00
|
|
|
if (create_serval_instance_dir() == -1)
|
|
|
|
return -1;
|
|
|
|
return set_variable(var, val);
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_config_del(int argc, const char *const *argv, struct command_line_option *o)
|
2012-03-29 04:51:30 +00:00
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *var;
|
2012-04-19 08:33:04 +00:00
|
|
|
if (cli_arg(argc, argv, o, "variable", &var, cli_configvarname, NULL))
|
|
|
|
return -1;
|
2012-04-12 08:10:00 +00:00
|
|
|
if (create_serval_instance_dir() == -1)
|
|
|
|
return -1;
|
2012-03-29 04:51:30 +00:00
|
|
|
return set_variable(var, NULL);
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_config_get(int argc, const char *const *argv, struct command_line_option *o)
|
2012-03-29 04:51:30 +00:00
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *var;
|
|
|
|
if (cli_arg(argc, argv, o, "variable", &var, cli_configvarname, NULL) == -1)
|
2012-04-19 08:33:04 +00:00
|
|
|
return -1;
|
2012-04-12 08:10:00 +00:00
|
|
|
if (create_serval_instance_dir() == -1)
|
|
|
|
return -1;
|
2012-04-19 08:33:04 +00:00
|
|
|
char conffile[1024];
|
|
|
|
FILE *in;
|
2012-03-29 04:51:30 +00:00
|
|
|
if (!FORM_SERVAL_INSTANCE_PATH(conffile, "serval.conf") ||
|
|
|
|
!((in = fopen(conffile, "r")) || (in = fopen(conffile, "w")))
|
|
|
|
) {
|
|
|
|
return WHY("could not read configuration file.");
|
|
|
|
}
|
|
|
|
/* Read lines of config file. */
|
|
|
|
char line[1024];
|
2012-04-23 07:42:10 +00:00
|
|
|
int varlen = var ? strlen(var) : 0;
|
2012-03-29 04:51:30 +00:00
|
|
|
line[0]=0; fgets(line,1024,in);
|
|
|
|
while(line[0]) {
|
2012-03-29 07:21:59 +00:00
|
|
|
if (varlen == 0) {
|
|
|
|
fputs(line, stdout);
|
|
|
|
}
|
|
|
|
else if (!strncasecmp(var, line, varlen) && line[varlen] == '=') {
|
2012-03-29 04:51:30 +00:00
|
|
|
fputs(line, stdout);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
line[0]=0;
|
|
|
|
fgets(line,1024,in);
|
|
|
|
}
|
|
|
|
fclose(in);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_rhizome_add_file(int argc, const char *const *argv, struct command_line_option *o)
|
2012-03-29 07:21:59 +00:00
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *filepath, *manifestpath;
|
2012-04-19 08:33:04 +00:00
|
|
|
cli_arg(argc, argv, o, "filepath", &filepath, NULL, "");
|
|
|
|
cli_arg(argc, argv, o, "manifestpath", &manifestpath, NULL, "");
|
2012-04-02 08:12:40 +00:00
|
|
|
/* Ensure the Rhizome database exists and is open */
|
|
|
|
if (create_serval_instance_dir() == -1)
|
|
|
|
return -1;
|
|
|
|
rhizome_datastore_path = serval_instancepath();
|
|
|
|
rhizome_opendb();
|
2012-04-10 08:42:17 +00:00
|
|
|
/* Create a new manifest that will represent the file. If a manifest file was supplied, then read
|
|
|
|
* it, otherwise create a blank manifest. */
|
|
|
|
rhizome_manifest *m = NULL;
|
2012-04-13 08:17:20 +00:00
|
|
|
int manifest_file_supplied = 0;
|
|
|
|
if (manifestpath[0] && access(manifestpath, R_OK) == 0) {
|
2012-04-12 09:00:52 +00:00
|
|
|
m = rhizome_read_manifest_file(manifestpath, 0, 0); // no verify
|
2012-04-13 08:17:20 +00:00
|
|
|
if (!m)
|
|
|
|
return WHY("Manifest file could not be loaded -- not added to rhizome");
|
|
|
|
manifest_file_supplied = 1;
|
2012-04-10 08:42:17 +00:00
|
|
|
} else {
|
|
|
|
m = rhizome_new_manifest();
|
2012-04-13 08:17:20 +00:00
|
|
|
if (!m)
|
|
|
|
return WHY("Manifest struct could not be allocated -- not added to rhizome");
|
2012-04-10 08:42:17 +00:00
|
|
|
}
|
2012-04-13 08:17:20 +00:00
|
|
|
/* Fill in a few missing manifest fields, to make it easier to use when adding new files:
|
|
|
|
- the payload file's basename for "name"
|
|
|
|
- current time for "date"
|
|
|
|
*/
|
2012-04-10 08:42:17 +00:00
|
|
|
if (rhizome_manifest_get(m, "name", NULL, 0) == NULL) {
|
|
|
|
const char *name = strrchr(filepath, '/');
|
|
|
|
name = name ? name + 1 : filepath;
|
|
|
|
rhizome_manifest_set(m, "name", name);
|
|
|
|
}
|
|
|
|
if (rhizome_manifest_get(m, "date", NULL, 0) == NULL) {
|
|
|
|
rhizome_manifest_set_ll(m, "date", overlay_gettime_ms());
|
|
|
|
}
|
2012-04-02 08:12:40 +00:00
|
|
|
/* Add the manifest and its associated file to the Rhizome database, generating an "id" in the
|
|
|
|
* process */
|
2012-04-13 08:17:20 +00:00
|
|
|
rhizome_manifest *mout = NULL;
|
|
|
|
int ret = rhizome_add_manifest(m, &mout, filepath,
|
2012-04-02 08:12:40 +00:00
|
|
|
NULL, // no groups - XXX should allow them
|
|
|
|
255, // ttl - XXX should read from somewhere
|
2012-04-13 08:17:20 +00:00
|
|
|
manifest_file_supplied, // int verifyP
|
2012-04-02 08:12:40 +00:00
|
|
|
1, // int checkFileP
|
|
|
|
1 // int signP
|
|
|
|
);
|
2012-04-16 02:16:58 +00:00
|
|
|
if (ret == -1)
|
2012-04-02 08:12:40 +00:00
|
|
|
return WHY("Manifest not added to Rhizome database");
|
2012-04-16 02:16:58 +00:00
|
|
|
/* If successfully added, overwrite the manifest file so that the Java component that is
|
|
|
|
invoking this command can read it to obtain feedback on the result. */
|
|
|
|
if (manifestpath[0] && rhizome_write_manifest_file(mout, manifestpath) == -1)
|
|
|
|
ret = WHY("Could not overwrite manifest file.");
|
2012-04-02 08:12:40 +00:00
|
|
|
rhizome_manifest_free(m);
|
2012-04-13 08:17:20 +00:00
|
|
|
if (mout != m)
|
|
|
|
rhizome_manifest_free(mout);
|
2012-04-02 08:12:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-19 08:33:04 +00:00
|
|
|
int cli_uint(const char *arg)
|
|
|
|
{
|
|
|
|
register const char *s = arg;
|
|
|
|
while (isdigit(*s++))
|
|
|
|
;
|
|
|
|
return s != arg && *s == '\0';
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_rhizome_list(int argc, const char *const *argv, struct command_line_option *o)
|
2012-04-02 08:12:40 +00:00
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *offset, *limit;
|
2012-04-19 08:33:04 +00:00
|
|
|
cli_arg(argc, argv, o, "offset", &offset, cli_uint, "0");
|
|
|
|
cli_arg(argc, argv, o, "limit", &limit, cli_uint, "0");
|
2012-04-02 08:12:40 +00:00
|
|
|
/* Create the instance directory if it does not yet exist */
|
|
|
|
if (create_serval_instance_dir() == -1)
|
|
|
|
return -1;
|
|
|
|
rhizome_datastore_path = serval_instancepath();
|
|
|
|
rhizome_opendb();
|
2012-04-19 08:33:04 +00:00
|
|
|
return rhizome_list_manifests(atoi(offset), atoi(limit));
|
2012-03-29 07:21:59 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_keyring_create(int argc, const char *const *argv, struct command_line_option *o)
|
2012-04-12 04:30:51 +00:00
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *pin;
|
2012-04-19 08:33:04 +00:00
|
|
|
cli_arg(argc, argv, o, "pin,pin ...", &pin, NULL, "");
|
2012-04-12 13:45:21 +00:00
|
|
|
keyring_file *k=keyring_open_with_pins(pin);
|
|
|
|
if (!k) fprintf(stderr,"keyring create:Failed to create/open keyring file\n");
|
2012-04-12 04:30:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_keyring_list(int argc, const char *const *argv, struct command_line_option *o)
|
2012-04-12 04:30:51 +00:00
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *pin;
|
2012-04-19 08:33:04 +00:00
|
|
|
cli_arg(argc, argv, o, "pin,pin ...", &pin, NULL, "");
|
2012-04-12 13:45:21 +00:00
|
|
|
keyring_file *k=keyring_open_with_pins(pin);
|
2012-04-12 04:30:51 +00:00
|
|
|
|
|
|
|
int cn=0;
|
|
|
|
int in=0;
|
|
|
|
|
|
|
|
for(cn=0;cn<k->context_count;cn++)
|
|
|
|
for(in=0;in<k->contexts[cn]->identity_count;in++)
|
|
|
|
{
|
|
|
|
int kpn;
|
|
|
|
keypair *kp;
|
2012-04-12 13:45:21 +00:00
|
|
|
unsigned char *sid=NULL,*did=NULL;
|
2012-04-12 04:30:51 +00:00
|
|
|
for(kpn=0;kpn<k->contexts[cn]->identities[in]->keypair_count;kpn++)
|
|
|
|
{
|
|
|
|
kp=k->contexts[cn]->identities[in]->keypairs[kpn];
|
2012-04-12 13:45:21 +00:00
|
|
|
if (kp->type==KEYTYPE_CRYPTOBOX) sid=kp->public_key;
|
|
|
|
if (kp->type==KEYTYPE_DID) did=kp->private_key;
|
|
|
|
}
|
|
|
|
if (sid||did) {
|
2012-04-12 04:30:51 +00:00
|
|
|
int i;
|
2012-04-26 06:24:06 +00:00
|
|
|
if (sid) for(i=0;i<SID_SIZE;i++) cli_printf("%02x",sid[i]);
|
|
|
|
cli_delim(":");
|
|
|
|
if (did) cli_puts((char*)did);
|
|
|
|
cli_delim("\n");
|
2012-04-12 13:45:21 +00:00
|
|
|
}
|
2012-04-12 04:30:51 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_keyring_add(int argc, const char *const *argv, struct command_line_option *o)
|
2012-04-12 04:30:51 +00:00
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *pin;
|
2012-04-19 08:33:04 +00:00
|
|
|
cli_arg(argc, argv, o, "pin", &pin, NULL, "");
|
2012-04-12 04:30:51 +00:00
|
|
|
|
2012-04-12 13:45:21 +00:00
|
|
|
keyring_file *k=keyring_open_with_pins("");
|
|
|
|
if (!k) { fprintf(stderr,"keyring add:Failed to create/open keyring file\n");
|
|
|
|
return -1; }
|
2012-04-12 04:30:51 +00:00
|
|
|
|
2012-04-13 17:14:41 +00:00
|
|
|
if (keyring_create_identity(k,k->contexts[0],(char *)pin)==NULL)
|
2012-04-12 04:30:51 +00:00
|
|
|
{
|
2012-04-13 17:14:41 +00:00
|
|
|
fprintf(stderr,"Could not create new identity (keyring_create_identity() failed)\n");
|
2012-04-12 04:30:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (keyring_commit(k)) {
|
2012-04-13 17:14:41 +00:00
|
|
|
fprintf(stderr,"Could not write new identity (keyring_commit() failed)\n");
|
2012-04-12 04:30:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
keyring_free(k);
|
|
|
|
return 0;
|
2012-04-12 13:45:21 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 07:42:10 +00:00
|
|
|
int app_keyring_set_did(int argc, const char *const *argv, struct command_line_option *o)
|
2012-04-12 13:45:21 +00:00
|
|
|
{
|
2012-04-23 07:42:10 +00:00
|
|
|
const char *sid, *did, *pin;
|
2012-04-19 08:33:04 +00:00
|
|
|
cli_arg(argc, argv, o, "sid", &sid, NULL, "");
|
|
|
|
cli_arg(argc, argv, o, "did", &did, NULL, "");
|
|
|
|
cli_arg(argc, argv, o, "pin", &pin, NULL, "");
|
2012-04-12 13:45:21 +00:00
|
|
|
|
|
|
|
if (strlen(did)>31) return WHY("DID too long (31 digits max)");
|
2012-04-12 04:30:51 +00:00
|
|
|
|
2012-04-27 04:47:41 +00:00
|
|
|
keyring=keyring_open_with_pins((char *)pin);
|
|
|
|
if (!keyring) return WHY("Could not open keyring file");
|
2012-04-12 13:45:21 +00:00
|
|
|
|
|
|
|
unsigned char packedSid[SID_SIZE];
|
|
|
|
stowSid(packedSid,0,(char *)sid);
|
|
|
|
|
|
|
|
int cn=0,in=0,kp=0;
|
2012-04-27 04:47:41 +00:00
|
|
|
int r=keyring_find_sid(keyring,&cn,&in,&kp,packedSid);
|
2012-04-12 13:45:21 +00:00
|
|
|
if (!r) return WHY("No matching SID");
|
2012-04-27 04:47:41 +00:00
|
|
|
if (keyring_set_did(keyring->contexts[cn]->identities[in],(char *)did))
|
2012-04-12 13:45:21 +00:00
|
|
|
return WHY("Could not set DID");
|
2012-04-27 04:47:41 +00:00
|
|
|
if (keyring_commit(keyring))
|
2012-04-12 13:45:21 +00:00
|
|
|
return WHY("Could not write updated keyring record");
|
|
|
|
|
|
|
|
return 0;
|
2012-04-12 04:30:51 +00:00
|
|
|
}
|
|
|
|
|
2012-04-25 20:18:06 +00:00
|
|
|
int app_id_self(int argc, const char *const *argv, struct command_line_option *o)
|
|
|
|
{
|
|
|
|
/* List my own identities */
|
|
|
|
overlay_mdp_frame a;
|
2012-04-25 20:24:54 +00:00
|
|
|
int result;
|
2012-04-26 20:28:31 +00:00
|
|
|
int count=0;
|
2012-04-25 20:18:06 +00:00
|
|
|
|
|
|
|
a.packetTypeAndFlags=MDP_GETADDRS;
|
2012-04-25 20:36:04 +00:00
|
|
|
if (!strcasecmp(argv[1],"self"))
|
|
|
|
a.addrlist.selfP=1; /* get own identities, not those of peers */
|
|
|
|
else
|
|
|
|
a.addrlist.selfP=0; /* get peer list */
|
2012-04-25 20:18:06 +00:00
|
|
|
a.addrlist.first_sid=-1;
|
|
|
|
a.addrlist.last_sid=0x7fffffff;
|
|
|
|
a.addrlist.frame_sid_count=MDP_MAX_SID_REQUEST;
|
2012-04-25 20:24:54 +00:00
|
|
|
|
|
|
|
while(a.addrlist.frame_sid_count==MDP_MAX_SID_REQUEST) {
|
|
|
|
result=overlay_mdp_send(&a,MDP_AWAITREPLY,5000);
|
|
|
|
if (result) {
|
|
|
|
if (a.packetTypeAndFlags==MDP_ERROR)
|
|
|
|
{
|
|
|
|
WHYF(" MDP Server error #%d: '%s'",
|
|
|
|
a.error.error,a.error.message);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
WHYF("Could not get list of local MDP addresses");
|
|
|
|
return WHY("Failed to get local address list");
|
|
|
|
}
|
|
|
|
if ((a.packetTypeAndFlags&MDP_TYPE_MASK)!=MDP_ADDRLIST)
|
|
|
|
return WHY("MDP Server returned something other than an address list");
|
|
|
|
int i;
|
|
|
|
for(i=0;i<a.addrlist.frame_sid_count;i++) {
|
2012-04-26 20:28:31 +00:00
|
|
|
count++;
|
2012-04-26 06:17:05 +00:00
|
|
|
cli_printf("%s",overlay_render_sid(a.addrlist.sids[i])); cli_delim("\n");
|
2012-04-25 20:24:54 +00:00
|
|
|
}
|
|
|
|
/* get ready to ask for next block of SIDs */
|
|
|
|
a.packetTypeAndFlags=MDP_GETADDRS;
|
|
|
|
a.addrlist.first_sid=a.addrlist.last_sid+1;
|
2012-04-25 20:18:06 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-25 21:44:01 +00:00
|
|
|
int app_node_info(int argc, const char *const *argv, struct command_line_option *o)
|
|
|
|
{
|
|
|
|
const char *sid;
|
|
|
|
cli_arg(argc, argv, o, "sid", &sid, NULL, "");
|
|
|
|
|
|
|
|
overlay_mdp_frame mdp;
|
|
|
|
bzero(&mdp,sizeof(mdp));
|
2012-04-30 05:42:31 +00:00
|
|
|
int resolveDid=0;
|
2012-04-25 21:44:01 +00:00
|
|
|
|
|
|
|
mdp.packetTypeAndFlags=MDP_NODEINFO;
|
|
|
|
if (argc>3) mdp.nodeinfo.resolve_did=1;
|
2012-04-30 05:42:31 +00:00
|
|
|
resolveDid=mdp.nodeinfo.resolve_did;
|
2012-04-25 21:44:01 +00:00
|
|
|
|
2012-04-26 06:17:05 +00:00
|
|
|
/* get SID or SID prefix
|
|
|
|
XXX - Doesn't correctly handle odd-lengthed SID prefixes (ignores last digit).
|
|
|
|
The matching code in overlay_route.c also has a similar problem with the last
|
|
|
|
digit of an odd-length prefix being ignored. */
|
2012-04-25 21:44:01 +00:00
|
|
|
int i;
|
|
|
|
mdp.nodeinfo.sid_prefix_length=0;
|
2012-04-26 06:17:05 +00:00
|
|
|
for(i = 0; (i != SID_SIZE)&&sid[i<<1]&&sid[(i<<1)+1]; i++) {
|
|
|
|
mdp.nodeinfo.sid[mdp.nodeinfo.sid_prefix_length] = hexvalue(sid[i<<1]) << 4;
|
|
|
|
mdp.nodeinfo.sid[mdp.nodeinfo.sid_prefix_length++] |= hexvalue(sid[(i<<1)+1]);
|
2012-04-25 21:44:01 +00:00
|
|
|
}
|
2012-04-26 06:17:05 +00:00
|
|
|
mdp.nodeinfo.sid_prefix_length*=2;
|
2012-04-25 21:44:01 +00:00
|
|
|
|
|
|
|
int result=overlay_mdp_send(&mdp,MDP_AWAITREPLY,5000);
|
|
|
|
if (result) {
|
|
|
|
if (mdp.packetTypeAndFlags==MDP_ERROR)
|
|
|
|
{
|
|
|
|
return WHYF(" MDP Server error #%d: '%s'",mdp.error.error,mdp.error.message);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return WHYF("Could not get information about node.");
|
|
|
|
}
|
|
|
|
|
2012-04-30 05:42:31 +00:00
|
|
|
if (resolveDid&&(!mdp.nodeinfo.resolve_did)) {
|
|
|
|
/* Asked for DID resolution, but did not get it, so do a DNA lookup
|
|
|
|
here. We do this on the client side, so that we don't block the
|
|
|
|
single-threaded server. */
|
|
|
|
overlay_mdp_frame m2;
|
|
|
|
bzero(&m2,sizeof(m2));
|
|
|
|
int port=32768+(random()&0xffff);
|
|
|
|
unsigned char srcsid[SID_SIZE];
|
|
|
|
if (overlay_mdp_getmyaddr(0,srcsid)) port=0;
|
|
|
|
if (overlay_mdp_bind(srcsid,port)) port=0;
|
|
|
|
|
|
|
|
if (port) {
|
|
|
|
int i;
|
|
|
|
for(i=0;i<(3000/125);i++) {
|
|
|
|
m2.packetTypeAndFlags=MDP_TX;
|
|
|
|
m2.out.src.port=port;
|
|
|
|
bcopy(&srcsid[0],&m2.out.src.sid[0],SID_SIZE);
|
|
|
|
bcopy(&mdp.nodeinfo.sid[0],&m2.out.dst.sid[0],SID_SIZE);
|
|
|
|
m2.out.dst.port=MDP_PORT_DNALOOKUP;
|
|
|
|
/* search for any DID */
|
|
|
|
m2.out.payload[0]=0;
|
|
|
|
m2.out.payload_length=1;
|
|
|
|
|
|
|
|
if (!overlay_mdp_send(&m2,MDP_AWAITREPLY,125))
|
|
|
|
{
|
|
|
|
int bytes=m2.in.payload_length;
|
|
|
|
if ((bytes+1)<sizeof(mdp.nodeinfo.did))
|
|
|
|
{
|
|
|
|
bcopy(&m2.in.payload[0],&mdp.nodeinfo.did[0],bytes);
|
|
|
|
mdp.nodeinfo.did[bytes]=0;
|
|
|
|
mdp.nodeinfo.resolve_did=1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-26 22:57:55 +00:00
|
|
|
cli_printf("record"); cli_delim(":");
|
|
|
|
cli_printf("%d",mdp.nodeinfo.index); cli_delim(":");
|
|
|
|
cli_printf("%d",mdp.nodeinfo.count); cli_delim(":");
|
|
|
|
cli_printf("%s",mdp.nodeinfo.foundP?"found":"noresult"); cli_delim(":");
|
|
|
|
cli_printf("%s",overlay_render_sid(mdp.nodeinfo.sid)); cli_delim(":");
|
|
|
|
cli_printf("%s",mdp.nodeinfo.resolve_did?mdp.nodeinfo.did:"did-not-resolved");
|
|
|
|
cli_delim(":");
|
|
|
|
cli_printf("%s",mdp.nodeinfo.localP?"self":"peer"); cli_delim(":");
|
|
|
|
cli_printf("%s",mdp.nodeinfo.neighbourP?"direct":"indirect");
|
|
|
|
cli_delim(":");
|
|
|
|
cli_printf("%d",mdp.nodeinfo.score); cli_delim(":");
|
|
|
|
cli_printf("%d",mdp.nodeinfo.interface_number);
|
2012-04-26 06:17:05 +00:00
|
|
|
cli_delim("\n");
|
2012-04-25 21:44:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:05:28 +00:00
|
|
|
/* NULL marks ends of command structure.
|
|
|
|
"<anystring>" marks an arg that can take any value.
|
2012-03-29 07:21:59 +00:00
|
|
|
"[<anystring>]" marks an optional arg that can take any value.
|
|
|
|
All args following the first optional arg are optional, whether marked or not.
|
2012-02-16 14:05:28 +00:00
|
|
|
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.
|
|
|
|
|
2012-04-25 20:18:06 +00:00
|
|
|
The CLIFLAG_STANDALONE means that they cannot be used with a running servald
|
|
|
|
instance, but act as an instance. In other words, don't call these from the
|
|
|
|
serval frontend, e.g, Java application on Android. There are various reasons,
|
|
|
|
such as some will try to fork() and exec() (bad for a Java thread to do), while
|
|
|
|
others manipulate files that the running instance may be using.
|
|
|
|
|
2012-02-16 14:05:28 +00:00
|
|
|
Keep this list alphabetically sorted for user convenience.
|
|
|
|
*/
|
|
|
|
command_line_option command_line_options[]={
|
2012-04-25 20:18:06 +00:00
|
|
|
{app_dna_lookup,{"dna","lookup","<did>",NULL},0,
|
2012-03-29 07:21:59 +00:00
|
|
|
"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."},
|
2012-04-23 08:55:26 +00:00
|
|
|
{app_echo,{"echo","...",NULL},CLIFLAG_STANDALONE,
|
2012-04-25 05:54:21 +00:00
|
|
|
"Output the supplied string."},
|
2012-04-25 21:44:01 +00:00
|
|
|
{app_server_start,{"start",NULL},CLIFLAG_STANDALONE,
|
2012-03-29 07:21:59 +00:00
|
|
|
"Start Serval Mesh node process with instance path taken from SERVALINSTANCE_PATH environment variable."},
|
2012-04-25 21:44:01 +00:00
|
|
|
{app_server_start,{"start","in","<instance path>",NULL},CLIFLAG_STANDALONE,
|
2012-03-29 07:21:59 +00:00
|
|
|
"Start Serval Mesh node process with given instance path."},
|
2012-04-25 21:44:01 +00:00
|
|
|
{app_server_start,{"start","foreground",NULL},CLIFLAG_STANDALONE,
|
2012-03-04 22:57:31 +00:00
|
|
|
"Start Serval Mesh node process without detatching from foreground."},
|
2012-04-25 21:44:01 +00:00
|
|
|
{app_server_start,{"start","foreground","in","<instance path>",NULL},CLIFLAG_STANDALONE,
|
2012-03-29 07:21:59 +00:00
|
|
|
"Start Serval Mesh node process with given instance path, without detatching from foreground."},
|
2012-04-25 21:44:01 +00:00
|
|
|
{app_server_stop,{"stop",NULL},0,
|
2012-03-29 07:21:59 +00:00
|
|
|
"Stop a running Serval Mesh node process with instance path taken from SERVALINSTANCE_PATH environment variable."},
|
2012-04-25 21:44:01 +00:00
|
|
|
{app_server_stop,{"stop","in","<instance path>",NULL},0,
|
2012-03-29 07:21:59 +00:00
|
|
|
"Stop a running Serval Mesh node process with given instance path."},
|
2012-04-25 21:44:01 +00:00
|
|
|
{app_server_status,{"status",NULL},0,
|
2012-02-23 01:25:39 +00:00
|
|
|
"Display information about any running Serval Mesh node."},
|
2012-03-29 04:51:30 +00:00
|
|
|
{app_mdp_ping,{"mdp","ping","<SID|broadcast>",NULL},CLIFLAG_STANDALONE,
|
2012-03-18 22:57:06 +00:00
|
|
|
"Attempts to ping specified node via Mesh Datagram Protocol (MDP)."},
|
2012-04-20 07:32:29 +00:00
|
|
|
{app_config_set,{"config","set","<variable>","<value>",NULL},CLIFLAG_STANDALONE,
|
2012-03-29 04:51:30 +00:00
|
|
|
"Set specified configuration variable."},
|
2012-04-20 07:32:29 +00:00
|
|
|
{app_config_del,{"config","del","<variable>",NULL},CLIFLAG_STANDALONE,
|
2012-03-19 03:56:14 +00:00
|
|
|
"Set specified configuration variable."},
|
2012-04-20 07:32:29 +00:00
|
|
|
{app_config_get,{"config","get","[<variable>]",NULL},CLIFLAG_STANDALONE,
|
2012-03-29 04:51:30 +00:00
|
|
|
"Get specified configuration variable."},
|
2012-04-20 07:32:29 +00:00
|
|
|
{app_rhizome_add_file,{"rhizome","add","file","<filepath>","[<manifestpath>]",NULL},CLIFLAG_STANDALONE,
|
2012-04-02 08:12:40 +00:00
|
|
|
"Add a file to Rhizome and optionally write its manifest to the given path"},
|
2012-04-20 07:32:29 +00:00
|
|
|
{app_rhizome_list,{"rhizome","list","[<offset>]","[<limit>]",NULL},CLIFLAG_STANDALONE,
|
2012-04-02 08:12:40 +00:00
|
|
|
"List all manifests and files in Rhizome"},
|
2012-04-12 04:30:51 +00:00
|
|
|
{app_keyring_create,{"keyring","create",NULL},0,
|
|
|
|
"Create a new keyring file."},
|
2012-04-25 20:18:06 +00:00
|
|
|
{app_keyring_list,{"keyring","list","[<pin,pin ...>]",NULL},CLIFLAG_STANDALONE,
|
2012-04-12 04:30:51 +00:00
|
|
|
"List identites in specified key ring that can be accessed using the specified PINs"},
|
2012-04-12 13:45:21 +00:00
|
|
|
{app_keyring_add,{"keyring","add","[<pin>]",NULL},CLIFLAG_STANDALONE,
|
2012-04-12 04:30:51 +00:00
|
|
|
"Create a new identity in the keyring protected by the provided PIN"},
|
2012-04-12 13:45:21 +00:00
|
|
|
{app_keyring_set_did,{"set","did","<sid>","<did>","[<pin>]",NULL},CLIFLAG_STANDALONE,
|
|
|
|
"Set the DID for the specified SID. Optionally supply PIN to unlock the SID record in the keyring."},
|
2012-04-20 06:11:13 +00:00
|
|
|
{app_vomp_status,{"vomp","status",NULL},0,
|
|
|
|
"Display status of any VoMP calls"},
|
2012-04-22 09:17:24 +00:00
|
|
|
{app_vomp_monitor,{"vomp","monitor",NULL},0,
|
|
|
|
"Monitor state and audio-flow of VoMP calls"},
|
2012-04-22 03:21:58 +00:00
|
|
|
{app_vomp_pickup,{"vomp","pickup","<call>",NULL},0,
|
|
|
|
"Accept specified call (use vomp status to get list of calls)"},
|
|
|
|
{app_vomp_hangup,{"vomp","hangup","<call>",NULL},0,
|
|
|
|
"End specified call (use vomp status to get list of calls)"},
|
2012-04-24 01:34:29 +00:00
|
|
|
{app_vomp_dtmf,{"vomp","dtmf","<call>","<digits>",NULL},0,
|
|
|
|
"Send DTMF digits over specified call"},
|
2012-04-22 03:21:58 +00:00
|
|
|
{app_vomp_dial,{"vomp","dial","<sid>","<did>","[<callerid>]",NULL},0,
|
2012-04-25 20:18:06 +00:00
|
|
|
"Attempt to dial the specified sid and did."},
|
|
|
|
{app_id_self,{"id","self",NULL},0,
|
|
|
|
"Return my own identity(s) as SIDs"},
|
2012-04-25 20:36:04 +00:00
|
|
|
{app_id_self,{"id","peers",NULL},0,
|
|
|
|
"Return identity of known peers as SIDs"},
|
2012-04-27 04:47:41 +00:00
|
|
|
{app_node_info,{"node","info","<sid>","[getdid]",NULL},0,
|
2012-04-25 21:44:01 +00:00
|
|
|
"Return information about SID, and optionally ask for DID resolution via network"},
|
2012-04-23 11:28:28 +00:00
|
|
|
#ifdef HAVE_VOIPTEST
|
|
|
|
{app_pa_phone,{"phone",NULL},0,
|
|
|
|
"Run phone test application"},
|
|
|
|
#endif
|
2012-02-16 14:05:28 +00:00
|
|
|
{NULL,{NULL}}
|
|
|
|
};
|