net_util.c 10.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
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
 */

#include "jni.h"
#include "jvm.h"
#include "jni_util.h"
#include "net_util.h"

int IPv6_supported() ;

static int IPv6_available;

JNIEXPORT jint JNICALL ipv6_available()
{
    return IPv6_available ;
}

JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *vm, void *reserved)
{
    JNIEnv *env;
    jclass iCls;
    jmethodID mid;
    jstring s;
    jint preferIPv4Stack;

    if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_2) == JNI_OK) {
        if (JVM_InitializeSocketLibrary() < 0) {
            JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError",
                            "failed to initialize net library.");
            return JNI_VERSION_1_2;
        }
    }
    iCls = (*env)->FindClass(env, "java/lang/Boolean");
    CHECK_NULL_RETURN(iCls, JNI_VERSION_1_2);
    mid = (*env)->GetStaticMethodID(env, iCls, "getBoolean", "(Ljava/lang/String;)Z");
    CHECK_NULL_RETURN(mid, JNI_VERSION_1_2);
    s = (*env)->NewStringUTF(env, "java.net.preferIPv4Stack");
    CHECK_NULL_RETURN(s, JNI_VERSION_1_2);
    preferIPv4Stack = (*env)->CallStaticBooleanMethod(env, iCls, mid, s);

    /*
       Since we have initialized and loaded the Socket library we will
       check now to whether we have IPv6 on this platform and if the
       supporting socket APIs are available
    */
    IPv6_available = IPv6_supported() & (!preferIPv4Stack);
70
    platformInit();
71 72
    parseExclusiveBindProperty(env);

D
duke 已提交
73 74 75 76 77
    return JNI_VERSION_1_2;
}

static int initialized = 0;

78
JNIEXPORT void JNICALL initInetAddressIDs(JNIEnv *env) {
D
duke 已提交
79 80
    if (!initialized) {
        Java_java_net_InetAddress_init(env, 0);
81
        JNU_CHECK_EXCEPTION(env);
D
duke 已提交
82
        Java_java_net_Inet4Address_init(env, 0);
83
        JNU_CHECK_EXCEPTION(env);
D
duke 已提交
84
        Java_java_net_Inet6Address_init(env, 0);
85
        JNU_CHECK_EXCEPTION(env);
D
duke 已提交
86 87 88 89
        initialized = 1;
    }
}

90 91 92 93 94 95 96 97 98 99
/* The address, and family fields used to be in InetAddress
 * but are now in an implementation object. So, there is an extra
 * level of indirection to access them now.
 */

extern jclass iac_class;
extern jfieldID ia_holderID;
extern jfieldID iac_addressID;
extern jfieldID iac_familyID;

M
michaelm 已提交
100 101 102 103 104 105
/**
 * set_ methods return JNI_TRUE on success JNI_FALSE on error
 * get_ methods that return +ve int return -1 on error
 * get_ methods that return objects return NULL on error.
 */
jobject getInet6Address_scopeifname(JNIEnv *env, jobject iaObj) {
106
    jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
M
michaelm 已提交
107 108 109 110 111
    CHECK_NULL_RETURN(holder, NULL);
    return (*env)->GetObjectField(env, holder, ia6_scopeifnameID);
}

int setInet6Address_scopeifname(JNIEnv *env, jobject iaObj, jobject scopeifname) {
112
    jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
M
michaelm 已提交
113 114 115 116 117 118
    CHECK_NULL_RETURN(holder, JNI_FALSE);
    (*env)->SetObjectField(env, holder, ia6_scopeifnameID, scopeifname);
    return JNI_TRUE;
}

int getInet6Address_scopeid_set(JNIEnv *env, jobject iaObj) {
119
    jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
M
michaelm 已提交
120 121 122 123 124
    CHECK_NULL_RETURN(holder, -1);
    return (*env)->GetBooleanField(env, holder, ia6_scopeidsetID);
}

int getInet6Address_scopeid(JNIEnv *env, jobject iaObj) {
125
    jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
M
michaelm 已提交
126 127 128 129 130
    CHECK_NULL_RETURN(holder, -1);
    return (*env)->GetIntField(env, holder, ia6_scopeidID);
}

int setInet6Address_scopeid(JNIEnv *env, jobject iaObj, int scopeid) {
131
    jobject holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
M
michaelm 已提交
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
    CHECK_NULL_RETURN(holder, JNI_FALSE);
    (*env)->SetIntField(env, holder, ia6_scopeidID, scopeid);
    if (scopeid > 0) {
            (*env)->SetBooleanField(env, holder, ia6_scopeidsetID, JNI_TRUE);
    }
    return JNI_TRUE;
}


int getInet6Address_ipaddress(JNIEnv *env, jobject iaObj, char *dest) {
    jobject holder, addr;
    jbyteArray barr;

    holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
    CHECK_NULL_RETURN(holder, JNI_FALSE);
    addr =  (*env)->GetObjectField(env, holder, ia6_ipaddressID);
    CHECK_NULL_RETURN(addr, JNI_FALSE);
    (*env)->GetByteArrayRegion(env, addr, 0, 16, (jbyte *)dest);
    return JNI_TRUE;
}

int setInet6Address_ipaddress(JNIEnv *env, jobject iaObj, char *address) {
    jobject holder;
    jbyteArray addr;

    holder = (*env)->GetObjectField(env, iaObj, ia6_holder6ID);
    CHECK_NULL_RETURN(holder, JNI_FALSE);
    addr =  (jbyteArray)(*env)->GetObjectField(env, holder, ia6_ipaddressID);
    if (addr == NULL) {
        addr = (*env)->NewByteArray(env, 16);
        CHECK_NULL_RETURN(addr, JNI_FALSE);
        (*env)->SetObjectField(env, holder, ia6_ipaddressID, addr);
    }
    (*env)->SetByteArrayRegion(env, addr, 0, 16, (jbyte *)address);
    return JNI_TRUE;
}

169
void setInetAddress_addr(JNIEnv *env, jobject iaObj, int address) {
170
    jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
171 172 173 174
    (*env)->SetIntField(env, holder, iac_addressID, address);
}

void setInetAddress_family(JNIEnv *env, jobject iaObj, int family) {
175
    jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
176 177 178 179
    (*env)->SetIntField(env, holder, iac_familyID, family);
}

void setInetAddress_hostName(JNIEnv *env, jobject iaObj, jobject host) {
180
    jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
181
    (*env)->SetObjectField(env, holder, iac_hostNameID, host);
182
    (*env)->SetObjectField(env, holder, iac_origHostNameID, host);
183 184 185
}

int getInetAddress_addr(JNIEnv *env, jobject iaObj) {
186
    jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
187 188 189 190
    return (*env)->GetIntField(env, holder, iac_addressID);
}

int getInetAddress_family(JNIEnv *env, jobject iaObj) {
191
    jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
192 193 194 195
    return (*env)->GetIntField(env, holder, iac_familyID);
}

jobject getInetAddress_hostName(JNIEnv *env, jobject iaObj) {
196
    jobject holder = (*env)->GetObjectField(env, iaObj, ia_holderID);
197 198 199
    return (*env)->GetObjectField(env, holder, iac_hostNameID);
}

200
JNIEXPORT jobject JNICALL
D
duke 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213
NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) {
    jobject iaObj;
#ifdef AF_INET6
    if (him->sa_family == AF_INET6) {
        jbyteArray ipaddress;
#ifdef WIN32
        struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
#else
        struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
#endif
        jbyte *caddr = (jbyte *)&(him6->sin6_addr);
        if (NET_IsIPv4Mapped(caddr)) {
            int address;
214
            iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
D
duke 已提交
215 216
            CHECK_NULL_RETURN(iaObj, NULL);
            address = NET_IPv4MappedToIPv4(caddr);
217 218
            setInetAddress_addr(env, iaObj, address);
            setInetAddress_family(env, iaObj, IPv4);
D
duke 已提交
219
        } else {
220
            jint scope;
M
michaelm 已提交
221
            int ret;
222
            iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
D
duke 已提交
223
            CHECK_NULL_RETURN(iaObj, NULL);
M
michaelm 已提交
224 225
            ret = setInet6Address_ipaddress(env, iaObj, (char *)&(him6->sin6_addr));
            CHECK_NULL_RETURN(ret, NULL);
226
            setInetAddress_family(env, iaObj, IPv6);
227
            scope = getScopeID(him);
M
michaelm 已提交
228
            setInet6Address_scopeid(env, iaObj, scope);
D
duke 已提交
229 230 231 232 233 234
        }
        *port = ntohs(him6->sin6_port);
    } else
#endif /* AF_INET6 */
        {
            struct sockaddr_in *him4 = (struct sockaddr_in *)him;
235
            iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
D
duke 已提交
236
            CHECK_NULL_RETURN(iaObj, NULL);
237 238
            setInetAddress_family(env, iaObj, IPv4);
            setInetAddress_addr(env, iaObj, ntohl(him4->sin_addr.s_addr));
D
duke 已提交
239 240 241 242 243
            *port = ntohs(him4->sin_port);
        }
    return iaObj;
}

244
JNIEXPORT jint JNICALL
D
duke 已提交
245 246
NET_SockaddrEqualsInetAddress(JNIEnv *env, struct sockaddr *him, jobject iaObj)
{
C
chegar 已提交
247
    jint family = AF_INET;
D
duke 已提交
248 249

#ifdef AF_INET6
250
    family = getInetAddress_family(env, iaObj) == IPv4? AF_INET : AF_INET6;
D
duke 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264
    if (him->sa_family == AF_INET6) {
#ifdef WIN32
        struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
#else
        struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
#endif
        jbyte *caddrNew = (jbyte *)&(him6->sin6_addr);
        if (NET_IsIPv4Mapped(caddrNew)) {
            int addrNew;
            int addrCur;
            if (family == AF_INET6) {
                return JNI_FALSE;
            }
            addrNew = NET_IPv4MappedToIPv4(caddrNew);
265
            addrCur = getInetAddress_addr(env, iaObj);
D
duke 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278
            if (addrNew == addrCur) {
                return JNI_TRUE;
            } else {
                return JNI_FALSE;
            }
        } else {
            jbyteArray ipaddress;
            jbyte caddrCur[16];
            int scope;

            if (family == AF_INET) {
                return JNI_FALSE;
            }
M
michaelm 已提交
279 280
            scope = getInet6Address_scopeid(env, iaObj);
            getInet6Address_ipaddress(env, iaObj, (char *)caddrCur);
D
duke 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
            if (NET_IsEqual(caddrNew, caddrCur) && cmpScopeID(scope, him)) {
                return JNI_TRUE;
            } else {
                return JNI_FALSE;
            }
        }
    } else
#endif /* AF_INET6 */
        {
            struct sockaddr_in *him4 = (struct sockaddr_in *)him;
            int addrNew, addrCur;
            if (family != AF_INET) {
                return JNI_FALSE;
            }
            addrNew = ntohl(him4->sin_addr.s_addr);
296
            addrCur = getInetAddress_addr(env, iaObj);
D
duke 已提交
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
            if (addrNew == addrCur) {
                return JNI_TRUE;
            } else {
                return JNI_FALSE;
            }
        }
}

unsigned short
in_cksum(unsigned short *addr, int len) {
    int nleft = len;
    int sum = 0;
    unsigned short *w = addr;
    unsigned short answer = 0;
    while(nleft > 1) {
        sum += *w++;
        nleft -= 2;
    }

    if (nleft == 1) {
        *(unsigned char *) (&answer) = *(unsigned char *)w;
        sum += answer;
    }

    sum = (sum >> 16) + (sum & 0xffff);
    sum += (sum >> 16);
    answer = ~sum;
    return (answer);
}