-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathhev-jni.c
135 lines (105 loc) · 2.97 KB
/
hev-jni.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
/*
============================================================================
Name : hev-jni.c
Author : hev <[email protected]>
Copyright : Copyright (c) 2019 - 2024 hev
Description : Jave Native Interface
============================================================================
*/
#ifdef ANDROID
#include <jni.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include "hev-main.h"
#include "hev-jni.h"
/* clang-format off */
#ifndef PKGNAME
#define PKGNAME hev/socks5
#endif
#ifndef CLSNAME
#define CLSNAME Socks5Service
#endif
/* clang-format on */
#define STR(s) STR_ARG (s)
#define STR_ARG(c) #c
#define N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
typedef struct _ThreadData ThreadData;
struct _ThreadData
{
char *path;
};
static JavaVM *java_vm;
static pthread_t work_thread;
static pthread_mutex_t mutex;
static pthread_key_t current_jni_env;
static void native_start_service (JNIEnv *env, jobject thiz,
jstring config_path);
static void native_stop_service (JNIEnv *env, jobject thiz);
static JNINativeMethod native_methods[] = {
{ "Socks5StartService", "(Ljava/lang/String;)V",
(void *)native_start_service },
{ "Socks5StopService", "()V", (void *)native_stop_service },
};
static void
detach_current_thread (void *env)
{
(*java_vm)->DetachCurrentThread (java_vm);
}
jint
JNI_OnLoad (JavaVM *vm, void *reserved)
{
JNIEnv *env = NULL;
jclass klass;
java_vm = vm;
if (JNI_OK != (*vm)->GetEnv (vm, (void **)&env, JNI_VERSION_1_4)) {
return 0;
}
klass = (*env)->FindClass (env, STR (PKGNAME) "/" STR (CLSNAME));
(*env)->RegisterNatives (env, klass, native_methods,
N_ELEMENTS (native_methods));
(*env)->DeleteLocalRef (env, klass);
pthread_key_create (¤t_jni_env, detach_current_thread);
pthread_mutex_init (&mutex, NULL);
return JNI_VERSION_1_4;
}
static void *
thread_handler (void *data)
{
ThreadData *tdata = data;
hev_socks5_server_main_from_file (tdata->path);
free (tdata->path);
free (tdata);
return NULL;
}
static void
native_start_service (JNIEnv *env, jobject thiz, jstring config_path)
{
const jbyte *bytes;
ThreadData *tdata;
pthread_mutex_lock (&mutex);
if (work_thread)
goto exit;
tdata = malloc (sizeof (ThreadData));
bytes = (const jbyte *)(*env)->GetStringUTFChars (env, config_path, NULL);
tdata->path = strdup ((const char *)bytes);
(*env)->ReleaseStringUTFChars (env, config_path, (const char *)bytes);
pthread_create (&work_thread, NULL, thread_handler, tdata);
exit:
pthread_mutex_unlock (&mutex);
}
static void
native_stop_service (JNIEnv *env, jobject thiz)
{
pthread_mutex_lock (&mutex);
if (!work_thread)
goto exit;
hev_socks5_server_quit ();
pthread_join (work_thread, NULL);
work_thread = 0;
exit:
pthread_mutex_unlock (&mutex);
}
#endif /* ANDROID */