2016-10-12 06:38:26 +00:00
|
|
|
/*
|
|
|
|
Serval DNA server main loop - JNI entry point
|
|
|
|
Copyright (C) 2015 Serval Project Inc.
|
|
|
|
Copyright (C) 2016 Flinders University
|
|
|
|
|
|
|
|
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 <assert.h>
|
|
|
|
#include "jni_common.h"
|
|
|
|
#include "server.h"
|
Switch to feature-driven linking
This introduces a new way of linking Serval executables and dynamic
libraries from static libraries like libservald.a -- called
"feature-driven" linking.
The Makefile now links servald and serval-tests from libservald.a,
rather than from an explicit list of object (.o) files. Thanks to the
section-based method for registering functions such as HTTP handlers,
CLI commands and MDP handlers, these object files had become
"stand-alone" and hence were no longer included in the link because
there was no unresolved reference that required them to be linked in.
The new "feature.h" provides the DECLARE_FEATURE(name) macro that each
stand-alone source file uses to declare the named feature(s) it
provides. Each executable can call the USE_FEATURE(name) macro in any
of its explicitly-linked source files to cause the corresponding
object(s) to be included in the link, eg, servald_features.c.
The DEFINE_BINDING() macro has been extended so that every individual
MDP binding is given a feature name based on its port number macro, eg,
"mdp_binding_MDP_PORT_ECHO".
Some features have been factored into their own separate source files so
they can be omitted or included in a build independently of each other:
- the MDP bindings for MDP_PORT_DNALOOKUP, MDP_PORT_ECHO,
MDP_PORT_TRACE, MDP_PORT_KEYMAPREQUEST, MDP_PORT_RHIZOME_xxx,
MDP_PORT_PROBE, MDP_PORT_STUN, MDP_PORT_STUNREQ
- the CLI "log" and "echo" commands
- the CLI "rhizome direct" command
The JNI source files are only compiled if the <jni.h> header is present,
otherwise they are omitted from libservald.so.
2016-10-13 02:58:23 +00:00
|
|
|
#include "serval.h" // for mdp_loopback_port
|
|
|
|
#include "keyring.h"
|
2016-10-12 06:38:26 +00:00
|
|
|
#include "conf.h"
|
|
|
|
#include "instance.h"
|
2017-11-24 00:03:39 +00:00
|
|
|
#include "httpd.h"
|
2018-04-03 06:55:22 +00:00
|
|
|
#include "feature.h"
|
|
|
|
|
|
|
|
DEFINE_FEATURE(jni_server);
|
2016-10-12 06:38:26 +00:00
|
|
|
|
2016-11-07 03:46:51 +00:00
|
|
|
static JNIEnv *server_env=NULL;
|
|
|
|
static jclass IJniServer= NULL;
|
|
|
|
static jmethodID aboutToWait, wokeUp, started;
|
|
|
|
static jobject JniCallback;
|
2016-10-12 06:38:26 +00:00
|
|
|
|
|
|
|
static time_ms_t waiting(time_ms_t now, time_ms_t next_run, time_ms_t next_wakeup)
|
|
|
|
{
|
|
|
|
if (server_env && JniCallback){
|
|
|
|
jlong r = (*server_env)->CallLongMethod(server_env, JniCallback, aboutToWait, (jlong)now, (jlong)next_run, (jlong)next_wakeup);
|
|
|
|
// stop the server if there are any issues
|
|
|
|
if ((*server_env)->ExceptionCheck(server_env)){
|
|
|
|
INFO("Stopping server due to exception");
|
2017-09-12 05:54:10 +00:00
|
|
|
server_close();
|
2016-10-12 06:38:26 +00:00
|
|
|
return now;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return next_wakeup;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wokeup()
|
|
|
|
{
|
|
|
|
if (server_env && JniCallback){
|
|
|
|
(*server_env)->CallVoidMethod(server_env, JniCallback, wokeUp);
|
|
|
|
// stop the server if there are any issues
|
|
|
|
if ((*server_env)->ExceptionCheck(server_env)){
|
|
|
|
INFO("Stopping server due to exception");
|
2017-09-12 05:54:10 +00:00
|
|
|
server_close();
|
2016-10-12 06:38:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL Java_org_servalproject_servaldna_ServalDCommand_server(
|
|
|
|
JNIEnv *env, jobject UNUSED(this), jobject callback, jobject keyring_pin, jobjectArray entry_pins)
|
|
|
|
{
|
2016-10-20 02:49:59 +00:00
|
|
|
cf_init();
|
|
|
|
cf_reload_strict();
|
2016-10-12 06:38:26 +00:00
|
|
|
|
2016-10-20 02:49:59 +00:00
|
|
|
if (!IJniServer) {
|
2016-10-12 06:38:26 +00:00
|
|
|
IJniServer = (*env)->FindClass(env, "org/servalproject/servaldna/IJniServer");
|
|
|
|
if (IJniServer==NULL)
|
|
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Unable to locate class org.servalproject.servaldna.IJniServer");
|
|
|
|
// make sure the interface class cannot be garbage collected between invocations
|
|
|
|
IJniServer = (jclass)(*env)->NewGlobalRef(env, IJniServer);
|
|
|
|
if (IJniServer==NULL)
|
|
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Unable to create global ref to class org.servalproject.servaldna.IJniServer");
|
|
|
|
aboutToWait = (*env)->GetMethodID(env, IJniServer, "aboutToWait", "(JJJ)J");
|
|
|
|
if (aboutToWait==NULL)
|
|
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Unable to locate method aboutToWait");
|
|
|
|
wokeUp = (*env)->GetMethodID(env, IJniServer, "wokeUp", "()V");
|
|
|
|
if (wokeUp==NULL)
|
|
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Unable to locate method wokeUp");
|
|
|
|
started = (*env)->GetMethodID(env, IJniServer, "started", "(Ljava/lang/String;III)V");
|
|
|
|
if (started==NULL)
|
|
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Unable to locate method started");
|
|
|
|
}
|
|
|
|
|
|
|
|
int pid = server_pid();
|
|
|
|
if (pid < 0)
|
|
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Failed to read server pid");
|
2016-10-20 02:49:59 +00:00
|
|
|
if (pid > 0)
|
2016-10-12 06:38:26 +00:00
|
|
|
return jni_throw(env, "java/lang/IllegalStateException", "Server already running");
|
|
|
|
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
{
|
2016-11-11 00:12:49 +00:00
|
|
|
assert(keyring == NULL);
|
2016-10-12 06:38:26 +00:00
|
|
|
const char *cpin = keyring_pin?(*env)->GetStringUTFChars(env, keyring_pin, NULL):NULL;
|
|
|
|
if (cpin != NULL){
|
|
|
|
keyring = keyring_open_instance(cpin);
|
|
|
|
(*env)->ReleaseStringUTFChars(env, keyring_pin, cpin);
|
|
|
|
}else{
|
|
|
|
keyring = keyring_open_instance("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always open all PIN-less entries.
|
|
|
|
keyring_enter_pin(keyring, "");
|
|
|
|
if (entry_pins){
|
|
|
|
jsize len = (*env)->GetArrayLength(env, entry_pins);
|
|
|
|
jsize i;
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
const jstring pin = (jstring)(*env)->GetObjectArrayElement(env, entry_pins, i);
|
|
|
|
if ((*env)->ExceptionCheck(env))
|
|
|
|
goto end;
|
|
|
|
const char *cpin = (*env)->GetStringUTFChars(env, pin, NULL);
|
|
|
|
if (cpin != NULL){
|
|
|
|
keyring_enter_pin(keyring, cpin);
|
|
|
|
(*env)->ReleaseStringUTFChars(env, pin, cpin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server_env){
|
|
|
|
jni_throw(env, "java/lang/IllegalStateException", "Server java env variable already set");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
server_env = env;
|
|
|
|
JniCallback = (*env)->NewGlobalRef(env, callback);
|
|
|
|
|
|
|
|
ret = server_bind();
|
|
|
|
|
|
|
|
if (ret==-1){
|
|
|
|
jni_throw(env, "java/lang/IllegalStateException", "Failed to bind sockets");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
jstring str = (jstring)(*env)->NewStringUTF(env, instance_path());
|
|
|
|
(*env)->CallVoidMethod(env, callback, started, str, getpid(), mdp_loopback_port, httpd_server_port);
|
|
|
|
(*env)->DeleteLocalRef(env, str);
|
2020-03-10 12:54:20 +00:00
|
|
|
if ((*env)->ExceptionCheck(env)){
|
|
|
|
ret = WHY("Not starting server due to startup exception");
|
|
|
|
goto end;
|
|
|
|
}
|
2016-10-12 06:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
server_loop(waiting, wokeup);
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
|
|
|
server_env=NULL;
|
|
|
|
if (JniCallback){
|
|
|
|
(*env)->DeleteGlobalRef(env, JniCallback);
|
|
|
|
JniCallback = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyring)
|
|
|
|
keyring_free(keyring);
|
|
|
|
keyring = NULL;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|