-
Notifications
You must be signed in to change notification settings - Fork 39
/
android.c
578 lines (518 loc) · 21.8 KB
/
android.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include <jni.h>
#include <android/log.h>
#include "libevent/util-internal.h"
#include "bugsnag/bugsnag_ndk.h"
#include "network.h"
#include "thread.h"
#include "constants.h"
#include "log.h"
#include "newnode.h"
#include "d2d.h"
#include "lsd.h"
#include "inttypes.h"
#include "dns_prefetch.h"
static int pfd[2];
static JavaVM *g_jvm;
static jobject bugsnagClient;
static jobject newNode;
static network *g_n;
void* stdio_thread(void *useradata)
{
ssize_t readSize;
char buf[1024];
while ((readSize = read(pfd[0], buf, sizeof buf - 1)) > 0) {
if (buf[readSize - 1] == '\n') {
--readSize;
}
buf[readSize] = 0;
__android_log_write(ANDROID_LOG_VERBOSE, "newnode", buf);
}
return NULL;
}
void start_stdio_thread()
{
setvbuf(stdout, NULL, _IOLBF, 0);
setvbuf(stderr, NULL, _IOLBF, 0);
pipe(pfd);
dup2(pfd[1], 1);
dup2(pfd[1], 2);
pthread_t t;
if (pthread_create(&t, NULL, stdio_thread, NULL) == -1) {
return;
}
pthread_detach(t);
}
#define BUGSNAG_API_KEY "141ea25aa72c276c49d3a154b82f2b1f"
#define STR(A) #A
#define JSTR(s) (*env)->NewStringUTF(env, s)
#define IMPORT(pkg, class) jclass c ## class = (*env)->FindClass(env, STR(pkg) "/" STR(class));
#define IMPORT_INNER(pkg, class) jclass c ## class = (*env)->FindClass(env, STR(pkg) "$" STR(class));
#define CALL_VOID(class, obj, meth, sig, ...) \
jmethodID m ## meth = (*env)->GetMethodID(env, class, #meth, "(" STR(sig) ")V"); \
if (m ## meth) (*env)->CallVoidMethod(env, obj, m ## meth, __VA_ARGS__);
#define CATCH(code) if ((*env)->ExceptionOccurred(env)) { \
(*env)->ExceptionDescribe(env); \
(*env)->ExceptionClear(env); \
code; \
}
#define push_frame() (*env)->PushLocalFrame(env, 16)
#define pop_frame() (*env)->PopLocalFrame(env, NULL);
void jvm_frame(JNIEnv* env, void (^c)())
{
push_frame();
c();
pop_frame();
}
enum notify_type {
ALL = 1,
USER = 2,
APP = 3,
DEVICE = 4,
CONTEXT = 5,
RELEASE_STAGES = 6,
FILTERS = 7,
BREADCRUMB = 8,
META = 9
};
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_updateBugsnagDetails(JNIEnv* env, jobject thiz, int notifyType)
{
static bool bugsnag_setup = false;
if (!bugsnag_setup) {
bugsnag_setup = true;
setupBugsnag(env);
}
switch ((enum notify_type)notifyType) {
case USER: Java_com_bugsnag_android_ndk_BugsnagObserver_populateUserDetails(env, NULL); break;
case APP: Java_com_bugsnag_android_ndk_BugsnagObserver_populateAppDetails(env, NULL); break;
case DEVICE: Java_com_bugsnag_android_ndk_BugsnagObserver_populateDeviceDetails(env, NULL); break;
case CONTEXT: Java_com_bugsnag_android_ndk_BugsnagObserver_populateContextDetails(env, NULL); break;
case RELEASE_STAGES: Java_com_bugsnag_android_ndk_BugsnagObserver_populateReleaseStagesDetails(env, NULL); break;
case FILTERS: Java_com_bugsnag_android_ndk_BugsnagObserver_populateFilterDetails(env, NULL); break;
case BREADCRUMB: Java_com_bugsnag_android_ndk_BugsnagObserver_populateBreadcumbDetails(env, NULL); break;
case META: Java_com_bugsnag_android_ndk_BugsnagObserver_populateMetaDataDetails(env, NULL); break;
case ALL:
default:
Java_com_bugsnag_android_ndk_BugsnagObserver_populateErrorDetails(env, NULL); break;
}
}
void bugsnag_leave_breadcrumb_log(const char *buf)
{
bsg_breadcrumb *crumb = bugsnag_breadcrumb_init("newnode", BSG_CRUMB_LOG);
bugsnag_breadcrumb_add_metadata(crumb, "stdout", (char*)buf);
bugsnag_add_breadcrumb(crumb);
}
JNIEnv* get_env()
{
JNIEnv *env;
int stat = (*g_jvm)->GetEnv(g_jvm, (void **)&env, JNI_VERSION_1_6);
assert(stat == JNI_OK);
return env;
}
// Request that the platform's DNS asychronously library query IPv4
// and IPv6 addresses (as appropriate) for 'host'. The results are
// stored in evdns cache so that NN can connect directly to an address
// and hopefully also in the platform's DNS cache so that any "try first"
// attempts will be faster.
//
// This routine doesn't return a result. NN will use the result of
// the query if it arrives in time, or fallback to evdns.
void platform_dns_prefetch(network *n, const char *host)
{
debug("%s host:%s\n", __func__, host);
if (!newNode) {
return;
}
JNIEnv *env = get_env();
jvm_frame(env, ^() {
jclass cNewNode = (*env)->GetObjectClass(env, newNode);
CATCH(return);
CALL_VOID(cNewNode, newNode, dnsPrefetch, Ljava/lang/String;, JSTR(host));
CATCH(return);
});
}
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_storeDnsPrefetchResult(JNIEnv *env, jobject thiz, jstring host, jobjectArray addresses)
{
const char *cHost = (*env)->GetStringUTFChars(env, host, NULL);
int n_addresses = (*env)->GetArrayLength(env, addresses);
debug("%s host:%s n_addresses:%d\n", __func__, cHost, n_addresses);
evutil_addrinfo *rai = NULL;
for (int i = 0; i < n_addresses; ++i) {
jbyteArray jaddr = (jbyteArray)(*env)->GetObjectArrayElement(env, addresses, i);
jbyte* addr = (*env)->GetByteArrayElements(env, jaddr, NULL);
socklen_t addrlen = (*env)->GetArrayLength(env, jaddr);
switch (addrlen) {
case sizeof(in_addr_t): {
evutil_addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_STREAM, .ai_protocol = IPPROTO_TCP};
sockaddr_in sin = {.sin_family = AF_INET, .sin_addr.s_addr = *(in_addr_t*)addr};
evutil_addrinfo *ai = evutil_new_addrinfo_((sockaddr*)&sin, sizeof(sin), &hints);
rai = evutil_addrinfo_append_(rai, ai);
break;
}
case sizeof(in6_addr): {
evutil_addrinfo hints = {.ai_family = AF_INET6, .ai_socktype = SOCK_STREAM, .ai_protocol = IPPROTO_TCP};
sockaddr_in6 sin6 = {.sin6_family = AF_INET6};
memcpy(&sin6.sin6_addr, &addr, sizeof(sin6.sin6_addr));
evutil_addrinfo *ai = evutil_new_addrinfo_((sockaddr*)&sin6, sizeof(sin6), &hints);
rai = evutil_addrinfo_append_(rai, ai);
break;
}
default:
case 0:
debug("unsupported addrlen:%u\n", addrlen);
break;
}
(*env)->ReleaseByteArrayElements(env, jaddr, addr, JNI_ABORT);
}
char *temp_host = strdup(cHost);
(*env)->ReleaseStringUTFChars(env, host, cHost);
network_async(g_n, ^{
dns_prefetch_store_result(g_n, rai, temp_host, 0);
evutil_freeaddrinfo(rai);
free(temp_host);
});
}
void cancel_https_request(network *n, https_request_token token)
{
debug("%s request:%p)\n", __func__, token);
JNIEnv *env = get_env();
jvm_frame(env, ^{
jobject https_request = (*env)->NewLocalRef(env, token);
if (!https_request) {
return;
}
jclass cNewNode = (*env)->GetObjectClass(env, newNode);
CATCH(return);
CALL_VOID(cNewNode, newNode, httpCancel, Lcom/clostra/newnode/internal/NewNode$CallblockThread;, https_request);
CATCH(assert(false));
});
}
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_httpCallback(JNIEnv* env, jobject thiz, jlong callblock, jint https_error, jint http_status_code, jint flags, jbyteArray body)
{
extern char *https_strerror(https_result *result);
https_result result = {
.flags = flags,
.https_error = https_error,
.http_status = http_status_code,
.body_length = (*env)->GetArrayLength(env, body)
};
debug("g_https_cb callback length:%zu https_error:%d(%s) http_status_code:%d flags:0x%x\n",
result.body_length, https_error, https_strerror(&result),
http_status_code, flags);
if (result.body_length > 0 && http_status_code == 200) {
result.body = calloc(1, result.body_length + 1);
(*env)->GetByteArrayRegion(env, body, 0, result.body_length, (jbyte*)result.body);
}
https_complete_callback cb = (https_complete_callback)callblock;
network_async(g_n, ^{
if (cb) {
cb(http_status_code == 200, &result);
}
free(result.body);
Block_release(cb);
});
}
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_newnodeInit(JNIEnv* env, jobject thiz, jobject newNodeObj)
{
newNode = (*env)->NewGlobalRef(env, newNodeObj);
network_set_log_level(1);
char app_id[64] = {0};
FILE *cmdline = fopen("/proc/self/cmdline", "r");
if (cmdline) {
fread(app_id, sizeof(app_id), 1, cmdline);
__android_log_print(ANDROID_LOG_DEBUG, "newnode", "application id %s\n", app_id);
fclose(cmdline);
}
// XXX: TODO: use real app name
const char *app_name = app_id;
port_t newnode_port = 0;
g_n = newnode_init(app_name, app_id, &newnode_port, ^(const https_request *request, const char* url, https_complete_callback cb) {
network *n = g_n;
debug("g_https_cb(%s)\n", url);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow"
JNIEnv *env = get_env();
#pragma clang diagnostic pop
__block jobject https_request = NULL;
jvm_frame(env, ^{
jbyteArray request_body = (*env)->NewByteArray(env, request->body_size);
if (request->body && request->body_size > 0) {
(*env)->SetByteArrayRegion(env, request_body, 0, request->body_size, (const jbyte *)request->body);
}
jobjectArray request_header_names = NULL;
jobjectArray request_header_values = NULL;
if (request->headers) {
int nheaders;
for (nheaders = 0; request->headers[nheaders]; ++nheaders);
if (request->body_content_type) {
++nheaders;
}
request_header_names = (*env)->NewObjectArray(env, nheaders, (*env)->FindClass(env, "java/lang/String"), 0);
request_header_values = (*env)->NewObjectArray(env, nheaders, (*env)->FindClass(env, "java/lang/String"), 0);
for (int i = 0; request->headers[i]; ++i) {
char *colon = strchr(request->headers[i], ':');
if (!colon) {
continue;
}
auto_free char *name = strndup(request->headers[i], colon - request->headers[i]);
char *value = colon + 1;
// debug("%s setting header[%d] %s=%s\n", __func__, i, name, value);
(*env)->SetObjectArrayElement(env, request_header_names, i, JSTR(name));
(*env)->SetObjectArrayElement(env, request_header_values, i, JSTR(value));
}
if (request->body_content_type) {
(*env)->SetObjectArrayElement(env, request_header_names, nheaders - 1, JSTR("Content-Type"));
(*env)->SetObjectArrayElement(env, request_header_values, nheaders - 1, JSTR(request->body_content_type));
}
} else {
request_header_names = (*env)->NewObjectArray(env, 0, (*env)->FindClass(env, "java/lang/String"), 0);
request_header_values = (*env)->NewObjectArray(env, 0, (*env)->FindClass(env, "java/lang/String"), 0);
}
// debug("(jni call) http(url:%s, cbc:%p, flags:0x%08x, timeout:%d, bufsize:%lu)\n",
// url, cbc, request->flags, request->timeout_sec, (unsigned long) request->bufsize);
// debug(" flags=%s\n", expand_flags(request->flags));
jclass cNewNode = (*env)->GetObjectClass(env, newNode);
CATCH(return);
// javap -s -classpath ./build/intermediates/javac/debug/classes com.clostra.newnode.internal.NewNode
jmethodID mHttp = (*env)->GetMethodID(env, cNewNode, "http", "(Ljava/lang/String;JIIII[Ljava/lang/String;[Ljava/lang/String;[B)Lcom/clostra/newnode/internal/NewNode$CallblockThread;");
if (mHttp) {
https_complete_callback cbc = Block_copy(cb);
jobject t = (*env)->CallObjectMethod(env, newNode, mHttp,
JSTR(url),
(jlong)cbc, // callblock pointer (encoded as long)
(jint)request->flags,
(jint)(request->timeout_sec * 1000), // timeout_msec
(jint)request->bufsize, // bufsize
(jint)newnode_get_port(n),
request_header_names,
request_header_values,
request_body);
CATCH(assert(false));
https_request = (*env)->NewWeakGlobalRef(env, t);
}
});
return (https_request_token)https_request;
});
}
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_setCacheDir(JNIEnv* env, jobject thiz, jstring cacheDir)
{
const char* cCacheDir = (*env)->GetStringUTFChars(env, cacheDir, NULL);
chdir(cCacheDir);
(*env)->ReleaseStringUTFChars(env, cacheDir, cCacheDir);
}
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_registerProxy(JNIEnv* env, jobject thiz)
{
port_t newnode_port = newnode_get_port(g_n);
if (!newnode_port) {
return;
}
IMPORT(java/lang, System);
CATCH(return);
jmethodID mSetProp = (*env)->GetStaticMethodID(env, cSystem, "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
CATCH(return);
char port[6];
snprintf(port, sizeof(port), "%u", newnode_port);
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("socksProxyHost"), JSTR("127.0.0.1"));
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("socksProxyPort"), JSTR(port));
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("proxyHost"), JSTR("127.0.0.1"));
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("proxyPort"), JSTR(port));
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("http.proxyHost"), JSTR("127.0.0.1"));
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("http.proxyPort"), JSTR(port));
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("https.proxyHost"), JSTR("127.0.0.1"));
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("https.proxyPort"), JSTR(port));
char proxy[128];
snprintf(proxy, sizeof(proxy), "http://127.0.0.1:%s", port);
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("http_proxy"), JSTR(proxy));
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("https_proxy"), JSTR(proxy));
}
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_unregisterProxy(JNIEnv* env, jobject thiz)
{
IMPORT(java/lang, System);
CATCH(return);
jmethodID mClearProp = (*env)->GetStaticMethodID(env, cSystem, "clearProperty", "(Ljava/lang/String;)Ljava/lang/String;");
CATCH(return);
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("socksProxyHost"));
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("socksProxyPort"));
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("proxyHost"));
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("proxyPort"));
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("http.proxyHost"));
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("http.proxyPort"));
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("https.proxyHost"));
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("https.proxyPort"));
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("http_proxy"));
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("https_proxy"));
}
bool vpn_protect(int socket)
{
JNIEnv *env = get_env();
push_frame();
bool r = ^bool() {
if (!newNode) {
return false;
}
IMPORT(com/clostra/newnode/vpn, VpnService);
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionClear(env);
return true;
}
jmethodID mVpnProtect = (*env)->GetStaticMethodID(env, cVpnService, "vpnProtect", "(I)Z");
CATCH(return false);
jboolean success = (*env)->CallStaticBooleanMethod(env, cVpnService, mVpnProtect, socket);
CATCH(return false);
return (bool)success;
}();
pop_frame();
return r;
}
int __real_bind(int socket, const sockaddr *address, socklen_t length);
int __wrap_bind(int socket, const sockaddr *address, socklen_t length)
{
//debug("bind %d %s\n", socket, sockaddr_str(address));
if (!sockaddr_is_localhost(address, length) && !vpn_protect(socket)) {
debug("bind failed to protect\n");
errno = EADDRNOTAVAIL;
return -1;
}
return __real_bind(socket, address, length);
}
int __real_connect(int socket, const sockaddr *address, socklen_t length);
int __wrap_connect(int socket, const sockaddr *address, socklen_t length)
{
//debug("connect %d %s\n", socket, sockaddr_str(address));
sockaddr_storage ss;
socklen_t slen = sizeof(ss);
if (getsockname(socket, (sockaddr*)&ss, &slen) == -1 || slen == 0 || sockaddr_get_port((const sockaddr*)&ss) == 0) {
if (!vpn_protect(socket)) {
debug("connect failed to protect\n");
errno = EADDRNOTAVAIL;
return -1;
}
}
return __real_connect(socket, address, length);
}
ssize_t __real_sendto(int socket, const void *buffer, size_t length, int flags, const sockaddr *dest_addr, socklen_t dest_len);
ssize_t __wrap_sendto(int socket, const void *buffer, size_t length, int flags, const sockaddr *dest_addr, socklen_t dest_len)
{
//debug("sendto %d %s\n", socket, sockaddr_str(dest_addr));
sockaddr_storage ss;
socklen_t slen = sizeof(ss);
if (getsockname(socket, (sockaddr*)&ss, &slen) == -1 || slen == 0 || sockaddr_get_port((const sockaddr*)&ss) == 0) {
if (!vpn_protect(socket)) {
debug("sendto failed to protect\n");
errno = EADDRNOTAVAIL;
return -1;
}
}
return __real_sendto(socket, buffer, length, flags, dest_addr, dest_len);
}
sockaddr_in6 jbyteArray_to_addr(JNIEnv* env, jbyteArray je)
{
jbyte *buf = (*env)->GetByteArrayElements(env, je, NULL);
jsize len = (*env)->GetArrayLength(env, je);
endpoint e = {.port = 0};
memcpy(&e, buf, MIN((size_t)len, sizeof(e)));
(*env)->ReleaseByteArrayElements(env, je, buf, JNI_ABORT);
return endpoint_to_addr(&e);
}
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_addEndpoint(JNIEnv* env, jobject thiz, jbyteArray endpoint)
{
const sockaddr_in6 sin6 = jbyteArray_to_addr(env, endpoint);
network_async(g_n, ^{
if (g_n->sockaddr_cb) {
g_n->sockaddr_cb((const sockaddr *)&sin6, sizeof(sin6));
}
});
}
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_removeEndpoint(JNIEnv* env, jobject thiz, jbyteArray endpoint)
{
const sockaddr_in6 sin6 = jbyteArray_to_addr(env, endpoint);
network_async(g_n, ^{
// XXX: TODO: remove endpoint
});
}
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_packetReceived(JNIEnv* env, jobject thiz, jbyteArray array, jbyteArray endpoint)
{
jobject arrayref = (*env)->NewGlobalRef(env, array);
const sockaddr_in6 sin6 = jbyteArray_to_addr(env, endpoint);
network_async(g_n, ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow"
JNIEnv *env = get_env();
#pragma clang diagnostic pop
jbyte *buf = (*env)->GetByteArrayElements(env, arrayref, NULL);
jsize len = (*env)->GetArrayLength(env, arrayref);
d2d_received(g_n, (const uint8_t*)buf, len, (const sockaddr *)&sin6, sizeof(sin6));
(*env)->ReleaseByteArrayElements(env, arrayref, buf, JNI_ABORT);
(*env)->DeleteGlobalRef(env, arrayref);
// XXX: this should be called when the read buffer is drained
utp_issue_deferred_acks(g_n->utp);
});
}
ssize_t d2d_sendto(const uint8_t* buf, size_t len, const sockaddr_in6 *sin6)
{
JNIEnv *env = get_env();
push_frame();
ssize_t r = ^ssize_t() {
if (!IN6_IS_ADDR_UNIQUE_LOCAL(&sin6->sin6_addr)) {
return -1;
}
if (!newNode) {
return -1;
}
const endpoint e = addr_to_endpoint(sin6);
jbyteArray jendpoint = (*env)->NewByteArray(env, sizeof(endpoint));
CATCH(return -1);
(*env)->SetByteArrayRegion(env, jendpoint, 0, sizeof(endpoint), (const jbyte *)&e);
CATCH(return -1);
jbyteArray array = (*env)->NewByteArray(env, len);
CATCH(return -1);
(*env)->SetByteArrayRegion(env, array, 0, len, (const jbyte *)buf);
CATCH(return -1);
jclass cNewNode = (*env)->GetObjectClass(env, newNode);
CATCH(return -1);
CALL_VOID(cNewNode, newNode, sendPacket, [B[B, array, jendpoint);
CATCH(return -1);
return len;
}();
pop_frame();
return r;
}
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_ifChange(JNIEnv* env, jobject thiz)
{
network_async(g_n, ^{
network_ifchange(g_n);
});
}
void ui_display_stats(const char *type, uint64_t direct, uint64_t peers)
{
JNIEnv *env = get_env();
jvm_frame(env, ^{
jclass cNewNode = (*env)->GetObjectClass(env, newNode);
CATCH(return);
CALL_VOID(cNewNode, newNode, displayStats, Ljava/lang/String;JJ, JSTR(type), direct, peers);
CATCH(return);
});
}
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_setLogLevel(JNIEnv* env, jobject thiz, jint level)
{
network_set_log_level(level);
}
JNIEXPORT void JNICALL Java_com_clostra_newnode_internal_NewNode_newnodeRun(JNIEnv* env, jobject thiz)
{
newnode_run(g_n);
}
jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
g_jvm = vm;
JNIEnv *env;
if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
}
start_stdio_thread();
return JNI_VERSION_1_6;
}