Path: blob/master/src/java.base/share/native/libnet/proxy_util.c
41149 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni.h"26#include "jni_util.h"2728#include "proxy_util.h"2930jclass proxy_class;31jclass isaddr_class;32jclass ptype_class;33jmethodID isaddr_createUnresolvedID;34jmethodID proxy_ctrID;35jfieldID pr_no_proxyID;36jfieldID ptype_httpID;37jfieldID ptype_socksID;3839int initJavaClass(JNIEnv *env) {40jclass proxy_cls = NULL;41jclass ptype_cls = NULL;42jclass isaddr_cls = NULL;4344// Proxy initialization45proxy_cls = (*env)->FindClass(env,"java/net/Proxy");46CHECK_NULL_RETURN(proxy_cls, 0);47proxy_class = (*env)->NewGlobalRef(env, proxy_cls);48CHECK_NULL_RETURN(proxy_class, 0);49proxy_ctrID = (*env)->GetMethodID(env, proxy_class, "<init>",50"(Ljava/net/Proxy$Type;Ljava/net/SocketAddress;)V");51CHECK_NULL_RETURN(proxy_ctrID, 0);5253// Proxy$Type initialization54ptype_cls = (*env)->FindClass(env,"java/net/Proxy$Type");55CHECK_NULL_RETURN(ptype_cls, 0);56ptype_class = (*env)->NewGlobalRef(env, ptype_cls);57CHECK_NULL_RETURN(ptype_class, 0);58ptype_httpID = (*env)->GetStaticFieldID(env, ptype_class, "HTTP",59"Ljava/net/Proxy$Type;");60CHECK_NULL_RETURN(ptype_httpID, 0);61ptype_socksID = (*env)->GetStaticFieldID(env, ptype_class, "SOCKS",62"Ljava/net/Proxy$Type;");63CHECK_NULL_RETURN(ptype_socksID, 0);6465// NO_PROXY66pr_no_proxyID = (*env)->GetStaticFieldID(env, proxy_class, "NO_PROXY",67"Ljava/net/Proxy;");68CHECK_NULL_RETURN(pr_no_proxyID, 0);6970// InetSocketAddress initialization71isaddr_cls = (*env)->FindClass(env, "java/net/InetSocketAddress");72CHECK_NULL_RETURN(isaddr_cls, 0);73isaddr_class = (*env)->NewGlobalRef(env, isaddr_cls);74CHECK_NULL_RETURN(isaddr_class, 0);75isaddr_createUnresolvedID = (*env)->GetStaticMethodID(env, isaddr_class,76"createUnresolved",77"(Ljava/lang/String;I)Ljava/net/InetSocketAddress;");7879return isaddr_createUnresolvedID != NULL ? 1 : 0;80}8182jobject createProxy(JNIEnv *env, jfieldID ptype_ID, const char* phost, unsigned short pport) {83jobject jProxy = NULL;84jobject type_proxy = NULL;85type_proxy = (*env)->GetStaticObjectField(env, ptype_class, ptype_ID);86if (type_proxy) {87jstring jhost = NULL;88jhost = (*env)->NewStringUTF(env, phost);89if (jhost) {90jobject isa = NULL;91isa = (*env)->CallStaticObjectMethod(env, isaddr_class,92isaddr_createUnresolvedID, jhost, pport);93if (isa) {94jProxy = (*env)->NewObject(env, proxy_class, proxy_ctrID,95type_proxy, isa);96}97}98}99return jProxy;100}101102103