1 | /* |
2 | * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | * |
5 | * This code is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License version 2 only, as |
7 | * published by the Free Software Foundation. Oracle designates this |
8 | * particular file as subject to the "Classpath" exception as provided |
9 | * by Oracle in the LICENSE file that accompanied this code. |
10 | * |
11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | * version 2 for more details (a copy is included in the LICENSE file that |
15 | * accompanied this code). |
16 | * |
17 | * You should have received a copy of the GNU General Public License version |
18 | * 2 along with this work; if not, write to the Free Software Foundation, |
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 | * |
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 | * or visit www.oracle.com if you need additional information or have any |
23 | * questions. |
24 | */ |
25 | |
26 | #include "jni.h" |
27 | #include "jni_util.h" |
28 | |
29 | #include "proxy_util.h" |
30 | |
31 | jclass proxy_class; |
32 | jclass isaddr_class; |
33 | jclass ptype_class; |
34 | jmethodID isaddr_createUnresolvedID; |
35 | jmethodID proxy_ctrID; |
36 | jfieldID pr_no_proxyID; |
37 | jfieldID ptype_httpID; |
38 | jfieldID ptype_socksID; |
39 | |
40 | int initJavaClass(JNIEnv *env) { |
41 | jclass proxy_cls = NULL; |
42 | jclass ptype_cls = NULL; |
43 | jclass isaddr_cls = NULL; |
44 | |
45 | // Proxy initialization |
46 | proxy_cls = (*env)->FindClass(env,"java/net/Proxy" ); |
47 | CHECK_NULL_RETURN(proxy_cls, 0); |
48 | proxy_class = (*env)->NewGlobalRef(env, proxy_cls); |
49 | CHECK_NULL_RETURN(proxy_class, 0); |
50 | proxy_ctrID = (*env)->GetMethodID(env, proxy_class, "<init>" , |
51 | "(Ljava/net/Proxy$Type;Ljava/net/SocketAddress;)V" ); |
52 | CHECK_NULL_RETURN(proxy_ctrID, 0); |
53 | |
54 | // Proxy$Type initialization |
55 | ptype_cls = (*env)->FindClass(env,"java/net/Proxy$Type" ); |
56 | CHECK_NULL_RETURN(ptype_cls, 0); |
57 | ptype_class = (*env)->NewGlobalRef(env, ptype_cls); |
58 | CHECK_NULL_RETURN(ptype_class, 0); |
59 | ptype_httpID = (*env)->GetStaticFieldID(env, ptype_class, "HTTP" , |
60 | "Ljava/net/Proxy$Type;" ); |
61 | CHECK_NULL_RETURN(ptype_httpID, 0); |
62 | ptype_socksID = (*env)->GetStaticFieldID(env, ptype_class, "SOCKS" , |
63 | "Ljava/net/Proxy$Type;" ); |
64 | CHECK_NULL_RETURN(ptype_socksID, 0); |
65 | |
66 | // NO_PROXY |
67 | pr_no_proxyID = (*env)->GetStaticFieldID(env, proxy_class, "NO_PROXY" , |
68 | "Ljava/net/Proxy;" ); |
69 | CHECK_NULL_RETURN(pr_no_proxyID, 0); |
70 | |
71 | // InetSocketAddress initialization |
72 | isaddr_cls = (*env)->FindClass(env, "java/net/InetSocketAddress" ); |
73 | CHECK_NULL_RETURN(isaddr_cls, 0); |
74 | isaddr_class = (*env)->NewGlobalRef(env, isaddr_cls); |
75 | CHECK_NULL_RETURN(isaddr_class, 0); |
76 | isaddr_createUnresolvedID = (*env)->GetStaticMethodID(env, isaddr_class, |
77 | "createUnresolved" , |
78 | "(Ljava/lang/String;I)Ljava/net/InetSocketAddress;" ); |
79 | |
80 | return isaddr_createUnresolvedID != NULL ? 1 : 0; |
81 | } |
82 | |
83 | jobject createProxy(JNIEnv *env, jfieldID ptype_ID, const char* phost, unsigned short pport) { |
84 | jobject jProxy = NULL; |
85 | jobject type_proxy = NULL; |
86 | type_proxy = (*env)->GetStaticObjectField(env, ptype_class, ptype_ID); |
87 | if (type_proxy) { |
88 | jstring jhost = NULL; |
89 | jhost = (*env)->NewStringUTF(env, phost); |
90 | if (jhost) { |
91 | jobject isa = NULL; |
92 | isa = (*env)->CallStaticObjectMethod(env, isaddr_class, |
93 | isaddr_createUnresolvedID, jhost, pport); |
94 | if (isa) { |
95 | jProxy = (*env)->NewObject(env, proxy_class, proxy_ctrID, |
96 | type_proxy, isa); |
97 | } |
98 | } |
99 | } |
100 | return jProxy; |
101 | } |
102 | |