-
Notifications
You must be signed in to change notification settings - Fork 80
/
jni_server.c
166 lines (142 loc) · 5.32 KB
/
jni_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
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"
#include "serval.h" // for mdp_loopback_port
#include "keyring.h"
#include "conf.h"
#include "instance.h"
#include "httpd.h"
#include "feature.h"
DEFINE_FEATURE(jni_server);
static JNIEnv *server_env=NULL;
static jclass IJniServer= NULL;
static jmethodID aboutToWait, wokeUp, started;
static jobject JniCallback;
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");
server_close();
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");
server_close();
}
}
}
JNIEXPORT jint JNICALL Java_org_servalproject_servaldna_ServalDCommand_server(
JNIEnv *env, jobject UNUSED(this), jobject callback, jobject keyring_pin, jobjectArray entry_pins)
{
cf_init();
cf_reload_strict();
if (!IJniServer) {
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");
if (pid > 0)
return jni_throw(env, "java/lang/IllegalStateException", "Server already running");
int ret = -1;
{
assert(keyring == NULL);
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);
if ((*env)->ExceptionCheck(env)){
ret = WHY("Not starting server due to startup exception");
goto end;
}
}
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;
}