jdk/src/share/native/java/net/net_util.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 482 9cb089744f91
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
int IPv6_supported() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
static int IPv6_available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
JNIEXPORT jint JNICALL ipv6_available()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    return IPv6_available ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
JNI_OnLoad(JavaVM *vm, void *reserved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    JNIEnv *env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    jclass iCls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    jmethodID mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    jstring s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    jint preferIPv4Stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_2) == JNI_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        if (JVM_InitializeSocketLibrary() < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                            "failed to initialize net library.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            return JNI_VERSION_1_2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    iCls = (*env)->FindClass(env, "java/lang/Boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    CHECK_NULL_RETURN(iCls, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    mid = (*env)->GetStaticMethodID(env, iCls, "getBoolean", "(Ljava/lang/String;)Z");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    CHECK_NULL_RETURN(mid, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    s = (*env)->NewStringUTF(env, "java.net.preferIPv4Stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    CHECK_NULL_RETURN(s, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    preferIPv4Stack = (*env)->CallStaticBooleanMethod(env, iCls, mid, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
       Since we have initialized and loaded the Socket library we will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
       check now to whether we have IPv6 on this platform and if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
       supporting socket APIs are available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    IPv6_available = IPv6_supported() & (!preferIPv4Stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    initLocalAddrTable ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    return JNI_VERSION_1_2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
static int initialized = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
void init(JNIEnv *env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Java_java_net_InetAddress_init(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        Java_java_net_Inet4Address_init(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        Java_java_net_Inet6Address_init(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        initialized = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
jobject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    jobject iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    init(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    if (him->sa_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        jbyteArray ipaddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
#ifdef WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        jbyte *caddr = (jbyte *)&(him6->sin6_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (NET_IsIPv4Mapped(caddr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            int address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            static jclass inet4Cls = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (inet4Cls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                inet4Cls = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                CHECK_NULL_RETURN(inet4Cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                (*env)->DeleteLocalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            CHECK_NULL_RETURN(iaObj, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            address = NET_IPv4MappedToIPv4(caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            (*env)->SetIntField(env, iaObj, ia_addressID, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            (*env)->SetIntField(env, iaObj, ia_familyID, IPv4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            static jclass inet6Cls = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (inet6Cls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                jclass c = (*env)->FindClass(env, "java/net/Inet6Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                inet6Cls = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                CHECK_NULL_RETURN(inet6Cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                (*env)->DeleteLocalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            iaObj = (*env)->NewObject(env, inet6Cls, ia6_ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            CHECK_NULL_RETURN(iaObj, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            ipaddress = (*env)->NewByteArray(env, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            CHECK_NULL_RETURN(ipaddress, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                       (jbyte *)&(him6->sin6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            (*env)->SetObjectField(env, iaObj, ia6_ipaddressID, ipaddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            (*env)->SetIntField(env, iaObj, ia_familyID, IPv6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            (*env)->SetIntField(env, iaObj, ia6_scopeidID, getScopeID(him));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        *port = ntohs(him6->sin6_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            struct sockaddr_in *him4 = (struct sockaddr_in *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            static jclass inet4Cls = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (inet4Cls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                jclass c = (*env)->FindClass(env, "java/net/Inet4Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                CHECK_NULL_RETURN(c, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                inet4Cls = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                CHECK_NULL_RETURN(inet4Cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                (*env)->DeleteLocalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            CHECK_NULL_RETURN(iaObj, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            (*env)->SetIntField(env, iaObj, ia_familyID, IPv4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            (*env)->SetIntField(env, iaObj, ia_addressID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                ntohl(him4->sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            *port = ntohs(him4->sin_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    return iaObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
NET_SockaddrEqualsInetAddress(JNIEnv *env, struct sockaddr *him, jobject iaObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    jint family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        AF_INET : AF_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
#ifdef AF_INET6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    if (him->sa_family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
#ifdef WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        jbyte *caddrNew = (jbyte *)&(him6->sin6_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (NET_IsIPv4Mapped(caddrNew)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            int addrNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            int addrCur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (family == AF_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            addrNew = NET_IPv4MappedToIPv4(caddrNew);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            addrCur = (*env)->GetIntField(env, iaObj, ia_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (addrNew == addrCur) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            jbyteArray ipaddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            jbyte caddrCur[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            int scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            ipaddress = (*env)->GetObjectField(env, iaObj, ia6_ipaddressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            scope = (*env)->GetIntField(env, iaObj, ia6_scopeidID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            (*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddrCur);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (NET_IsEqual(caddrNew, caddrCur) && cmpScopeID(scope, him)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
#endif /* AF_INET6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            struct sockaddr_in *him4 = (struct sockaddr_in *)him;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            int addrNew, addrCur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (family != AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            addrNew = ntohl(him4->sin_addr.s_addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            addrCur = (*env)->GetIntField(env, iaObj, ia_addressID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (addrNew == addrCur) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
unsigned short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
in_cksum(unsigned short *addr, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    int nleft = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    unsigned short *w = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    unsigned short answer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    while(nleft > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        sum += *w++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        nleft -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    if (nleft == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        *(unsigned char *) (&answer) = *(unsigned char *)w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        sum += answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    sum = (sum >> 16) + (sum & 0xffff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    sum += (sum >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    answer = ~sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    return (answer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
}