jdk/src/windows/native/java/net/NetworkInterface_winXP.c
author michaelm
Wed, 13 Feb 2013 10:40:31 +0000
changeset 16870 f35b2bd19761
parent 14342 8435a30053c1
child 17193 8b561b7fb87a
permissions -rw-r--r--
8000724: Improve networking serialization Summary: delegate InetAddress fields to a holder object Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10417
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
    23
 * questions.
2
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 <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <winsock2.h>           /* needed for htonl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <iprtrmib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "java_net_NetworkInterface.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "NetworkInterface.h"
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
    36
#include "net_util.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Windows implementation of the java.net.NetworkInterface native methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This module provides the implementations of getAll, getByName, getByIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * and getByAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
extern int enumAddresses_win(JNIEnv *env, netif *netifP, netaddr **netaddrPP);
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
    45
int getAddrsFromAdapter(IP_ADAPTER_ADDRESSES *ptr, netaddr **netaddrPP);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
void printnif (netif *nif) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#ifdef _WIN64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        printf ("nif:0x%I64x name:%s\n", nif,nif->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        printf ("nif:0x%x name:%s\n", nif,nif->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (nif->dNameIsUnicode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            printf ("dName:%S index:%d ", nif->displayName,nif->index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            printf ("dName:%s index:%d ", nif->displayName,nif->index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        printf ("naddrs:%d\n", nif->naddrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
void printnifs (netif *netifPP, char *str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    netif *nif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    printf ("%s\n", str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    for (nif=netifPP; nif!=NULL; nif=nif->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        printnif (nif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    printf("-----------------\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
static int bufsize = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * return an array of IP_ADAPTER_ADDRESSES containing one element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * for each apdapter on the system. Returned in *adapters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Buffer is malloc'd and must be freed (unless error returned)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
static int getAdapters (JNIEnv *env, IP_ADAPTER_ADDRESSES **adapters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    DWORD ret, flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    IP_ADAPTER_ADDRESSES *adapterInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    ULONG len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    adapterInfo = (IP_ADAPTER_ADDRESSES *)malloc (bufsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    if (adapterInfo == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    len = bufsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    flags = GAA_FLAG_SKIP_DNS_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    flags |= GAA_FLAG_SKIP_MULTICAST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    flags |= GAA_FLAG_INCLUDE_PREFIX;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7668
diff changeset
    92
    ret = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    if (ret == ERROR_BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        adapterInfo = (IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (adapterInfo == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        bufsize = len;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7668
diff changeset
    99
        ret = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    if (ret != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        free (adapterInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        JNU_ThrowByName(env, "java/lang/Error",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                "IP Helper Library GetAdaptersAddresses function failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    *adapters = adapterInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    return ERROR_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * return an array of IP_ADAPTER_ADDRESSES containing one element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * for each apdapter on the system. Returned in *adapters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * Buffer is malloc'd and must be freed (unless error returned)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
IP_ADAPTER_ADDRESSES *getAdapter (JNIEnv *env,  jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    DWORD flags, val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    IP_ADAPTER_ADDRESSES *adapterInfo, *ptr, *ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    ULONG len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    adapterInfo = (IP_ADAPTER_ADDRESSES *)malloc (bufsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    if (adapterInfo == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        JNU_ThrowByName(env, "java/lang/OutOfMemoryError", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    len = bufsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    flags = GAA_FLAG_SKIP_DNS_SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    flags |= GAA_FLAG_SKIP_MULTICAST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    flags |= GAA_FLAG_INCLUDE_PREFIX;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7668
diff changeset
   129
    val = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    if (val == ERROR_BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        adapterInfo = (IP_ADAPTER_ADDRESSES *) realloc (adapterInfo, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (adapterInfo == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            JNU_ThrowByName(env, "java/lang/OutOfMemoryError", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        bufsize = len;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7668
diff changeset
   137
        val = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterInfo, &len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    if (val != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        free (adapterInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        JNU_ThrowByName(env, "java/lang/Error",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                "IP Helper Library GetAdaptersAddresses function failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    ptr = adapterInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    while (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
      // IPv4 interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
      if (ptr->Ipv6IfIndex == index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        ret = (IP_ADAPTER_ADDRESSES *) malloc(sizeof(IP_ADAPTER_ADDRESSES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        memcpy(ret, ptr, sizeof(IP_ADAPTER_ADDRESSES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
      ptr=ptr->Next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    free(adapterInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
static int ipinflen = 2048;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
int getAllInterfacesAndAddresses (JNIEnv *env, netif **netifPP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
{
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   165
    DWORD ret;
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   166
    IP_ADAPTER_ADDRESSES *ptr, *adapters=0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    ULONG len=ipinflen, count=0;
4811
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   168
    netif *nif=0, *dup_nif, *last=0, *loopif=0, *curr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    int tun=0, net=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    *netifPP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
   /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    * Get the IPv4 interfaces. This information is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    * as what previous JDK versions would return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7668
diff changeset
   178
    ret = enumInterfaces(env, netifPP);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    if (ret == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        count = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /* locate the loopback (and the last) interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    for (nif=*netifPP, last=nif; nif!=0; nif=nif->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (nif->ifType == MIB_IF_TYPE_LOOPBACK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            loopif = nif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        last = nif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
4811
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   193
    // Retrieve IPv4 addresses with the IP Helper API
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   194
    curr = *netifPP;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   195
    while (curr != NULL) {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   196
        netaddr *netaddrP;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   197
        ret = enumAddresses_win(env, curr, &netaddrP);
10417
947e9a7bf244 7084560: Crash in net.dll
michaelm
parents: 9003
diff changeset
   198
        if (ret == -1) {
4811
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   199
            return -1;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   200
        }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   201
        curr->addrs = netaddrP;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   202
        curr->naddrs += ret;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   203
        curr = curr->next;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   204
    }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   205
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    ret = getAdapters (env, &adapters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    if (ret != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /* Now get the IPv6 information. This includes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *  (a)  IPv6 information associated with interfaces already found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *  (b)  IPv6 information for IPv6 only interfaces (probably tunnels)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * For compatibility with previous releases we use the naming
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7668
diff changeset
   216
     * information gotten from enumInterfaces() for (a) entries
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * However, the index numbers are taken from the new API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * The procedure is to go through the list of adapters returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * by the new API looking for entries that correspond to IPv4 interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * already found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    ptr = adapters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    while (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        netif *nif0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (ptr->IfType == IF_TYPE_SOFTWARE_LOOPBACK && (loopif != NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            c = getAddrsFromAdapter(ptr, &loopif->addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            loopif->naddrs += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            int index = ptr->IfIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (index != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                /* This entry is associated with an IPv4 interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                for (nif=*netifPP; nif!=0; nif=nif->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    if (nif->index == index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        /* found the interface entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                         * set the index to the IPv6 index and add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                         * IPv6 addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        nif->index = ptr->Ipv6IfIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        c = getAddrsFromAdapter(ptr, &nif->addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        nif->naddrs += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                /* This entry is IPv6 only */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                char newname [128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                /* Windows allocates duplicate adapter entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                 * for tunnel interfaces when there are multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                 * physical adapters. Need to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                 * if this is a duplicate (ipv6Index is the same)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                dup_nif = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                for (nif0=*netifPP; nif0!=0; nif0=nif0->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    if (nif0->hasIpv6Address &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                ptr->Ipv6IfIndex == nif0->ipv6Index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        dup_nif = nif0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                if (dup_nif == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    /* new interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    nif = (netif *) calloc (1, sizeof(netif));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    if (nif == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    if (ptr->IfType == IF_TYPE_TUNNEL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        sprintf (newname, "tun%d", tun);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        tun ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        sprintf (newname, "net%d", net);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        net ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    nif->name = malloc (strlen(newname)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    nif->displayName = malloc (wcslen(ptr->FriendlyName)*2+2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    if (nif->name == 0 || nif->displayName == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    strcpy (nif->name, newname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    wcscpy ((PWCHAR)nif->displayName, ptr->FriendlyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    nif->dNameIsUnicode = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    nif->index = ptr->Ipv6IfIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    nif->ipv6Index = ptr->Ipv6IfIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    nif->hasIpv6Address = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    last->next = nif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    last = nif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    c = getAddrsFromAdapter(ptr, &nif->addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    nif->naddrs += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    /* add the addresses from this adapter to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                     * original (dup_nif)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    c = getAddrsFromAdapter(ptr, &dup_nif->addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    dup_nif->naddrs += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        ptr=ptr->Next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    free (adapters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
err:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    if (*netifPP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        free_netif (*netifPP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    if (adapters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        free (adapters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
/* If *netaddrPP is null, then the addresses are allocated and the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 * of the allocated chain is returned in *netaddrPP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 * If *netaddrPP is not null, then the addresses allocated here are appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 * to the existing chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 * Returns count of addresses or -1 on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
static int getAddrsFromAdapter(IP_ADAPTER_ADDRESSES *ptr, netaddr **netaddrPP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    LPSOCKADDR                   sock;
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   339
    int                          count = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    netaddr                     *curr, *start=0, *prev=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    PIP_ADAPTER_UNICAST_ADDRESS uni_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    PIP_ADAPTER_ANYCAST_ADDRESS any_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    PIP_ADAPTER_PREFIX prefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /* If chain passed in, find end */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    if (*netaddrPP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        for (start=*netaddrPP; start->next!=NULL; start=start->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        prev=start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    prefix = ptr->FirstPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /* Unicast */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    uni_addr = ptr->FirstUnicastAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    while (uni_addr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        /* address is only usable if dad state is preferred or deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (uni_addr->DadState == IpDadStateDeprecated ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                uni_addr->DadState == IpDadStatePreferred) {
4811
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   359
            sock = uni_addr->Address.lpSockaddr;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   360
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   361
            // IPv4 addresses already retrieved with enumAddresses_win
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   362
            if (sock->sa_family == AF_INET) {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   363
                uni_addr = uni_addr->Next;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   364
                continue;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   365
            }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   366
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            curr = (netaddr *)calloc (1, sizeof (netaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            if (curr == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            if (start == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                start = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (prev != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                prev->next = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            prev = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            SOCKETADDRESS_COPY (&curr->addr, sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (prefix != NULL) {
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   380
              curr->mask = (short)prefix->PrefixLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
              prefix = prefix->Next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        uni_addr = uni_addr->Next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /* Anycast */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    any_addr = ptr->FirstAnycastAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    while (any_addr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        curr = (netaddr *)calloc (1, sizeof (netaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (curr == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if (start == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            start = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (prev != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            prev->next = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        prev = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        sock = any_addr->Address.lpSockaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        SOCKETADDRESS_COPY (&curr->addr, sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        any_addr = any_addr->Next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    if (*netaddrPP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        *netaddrPP = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
 * Create a NetworkInterface object, populate the name and index, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
 * populate the InetAddress array based on the IP addresses for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
 * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
static jobject createNetworkInterfaceXP(JNIEnv *env, netif *ifs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    jobject netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    jobject name, displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    jobjectArray addrArr, bindsArr, childArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    netaddr *addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    jint addr_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    int netaddrCount=ifs->naddrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    netaddr *netaddrP=ifs->addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    jint bind_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Create a NetworkInterface object and populate it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    netifObj = (*env)->NewObject(env, ni_class, ni_ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    name = (*env)->NewStringUTF(env, ifs->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    if (ifs->dNameIsUnicode) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7668
diff changeset
   434
        displayName = (*env)->NewString(env, (PWCHAR)ifs->displayName,
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 7668
diff changeset
   435
                                        (jsize)wcslen ((PWCHAR)ifs->displayName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        displayName = (*env)->NewStringUTF(env, ifs->displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    if (netifObj == NULL || name == NULL || displayName == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    (*env)->SetObjectField(env, netifObj, ni_nameID, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    (*env)->SetObjectField(env, netifObj, ni_displayNameID, displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Get the IP addresses for this interface if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Note that 0 is a valid number of addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    if (netaddrCount < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        netaddrCount = enumAddresses_win(env, ifs, &netaddrP);
10417
947e9a7bf244 7084560: Crash in net.dll
michaelm
parents: 9003
diff changeset
   452
        if (netaddrCount == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    addrArr = (*env)->NewObjectArray(env, netaddrCount, ni_iacls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    if (addrArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    bindsArr = (*env)->NewObjectArray(env, netaddrCount, ni_ibcls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    if (bindsArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
      free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    addrs = netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    addr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    bind_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    while (addrs != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        jobject iaObj, ia2Obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        jobject ibObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (addrs->addr.him.sa_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            if (iaObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            /* default ctor will set family to AF_INET */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   481
            setInetAddress_addr(env, iaObj, ntohl(addrs->addr.him4.sin_addr.s_addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if (ibObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
              free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
              return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if (ia2Obj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
              free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
              return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14342
diff changeset
   494
            setInetAddress_addr(env, ia2Obj, ntohl(addrs->brdcast.him4.sin_addr.s_addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            (*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        } else /* AF_INET6 */ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            int scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            if (iaObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                jbyteArray ipaddress = (*env)->NewByteArray(env, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                if (ipaddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    (jbyte *)&(addrs->addr.him6.sin6_addr.s6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                scope = addrs->addr.him6.sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                if (scope != 0) { /* zero is default value, no need to set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    (*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                if (ibObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                  free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                  return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        (*env)->SetObjectArrayElement(env, addrArr, addr_index, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        addrs = addrs->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        addr_index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    (*env)->SetObjectField(env, netifObj, ni_bindsID, bindsArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Windows doesn't have virtual interfaces, so child array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * is always empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    childArr = (*env)->NewObjectArray(env, 0, ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    if (childArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /* return the NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    (JNIEnv *env, jclass cls, jstring name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    const char *name_utf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    jobject netifObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    if (getAllInterfacesAndAddresses (env, &ifList) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /* get the name as a C string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    /* Search by name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (strcmp(name_utf, curr->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    if (curr != NULL) {;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        netifObj = createNetworkInterfaceXP(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /* release the UTF string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    (*env)->ReleaseStringUTFChars(env, name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
 * Class:     NetworkInterface
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
   586
 * Method:    getByIndex0_XP
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
 * Signature: (I)LNetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
 */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
   589
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0_XP
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
  (JNIEnv *env, jclass cls, jint index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    jobject netifObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    if (getAllInterfacesAndAddresses (env, &ifList) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    /* search by index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        if (index == curr->index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    if (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        netifObj = createNetworkInterfaceXP(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
 * Method:    getByInetAddress0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    (JNIEnv *env, jclass cls, jobject iaObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    jobject netifObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    /* get the list of interfaces */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    if (getAllInterfacesAndAddresses (env, &ifList) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Enumerate the addresses on each interface until we find a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * matching address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        netaddr *addrList = curr->addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        netaddr *addrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        /* iterate through each address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        addrP = addrList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            if (NET_SockaddrEqualsInetAddress(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                                (struct sockaddr*)&addrP->addr, iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            addrP = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         * Address matched so create NetworkInterface for this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         * and address list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            netifObj = createNetworkInterfaceXP(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        /* on next interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
 * Method:    getAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
 * Signature: ()[Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    (JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    jobjectArray netIFArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    jint arr_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Get list of interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    count = getAllInterfacesAndAddresses (env, &ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    if (count < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    /* allocate a NetworkInterface array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    netIFArr = (*env)->NewObjectArray(env, count, cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    if (netIFArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Iterate through the interfaces, create a NetworkInterface instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * for each array element and populate the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    arr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        jobject netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        netifObj = createNetworkInterfaceXP(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (netifObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        /* put the NetworkInterface into the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    return netIFArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
 * Method:    supportsMulticast0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
      IP_ADAPTER_ADDRESSES *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
      jboolean val = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
      ptr = getAdapter(env, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
      if (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        val = ptr->Flags & IP_ADAPTER_NO_MULTICAST ? JNI_FALSE : JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
      return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
 * Method:    isUp0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
      IP_ADAPTER_ADDRESSES *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
      jboolean val = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
      ptr = getAdapter(env, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
      if (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        val = ptr->OperStatus == IfOperStatusUp ? JNI_TRUE : JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
      return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
 * Method:    getMacAddr0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
      IP_ADAPTER_ADDRESSES *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
      jbyteArray ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
      int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
      ptr = getAdapter(env, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
      if (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        len = ptr->PhysicalAddressLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        ret = (*env)->NewByteArray(env, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        if (!IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
          (*env)->SetByteArrayRegion(env, ret, 0, len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                                     (jbyte*) ptr->PhysicalAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
      return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
 * Class:       java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
 * Method:      getMTU0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
 * Signature:   ([bLjava/lang/String;I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
      IP_ADAPTER_ADDRESSES *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
      jint ret = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
      ptr = getAdapter(env, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
      if (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        ret = ptr->Mtu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
      return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
 * Method:    isLoopback0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
      IP_ADAPTER_ADDRESSES *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
      jboolean val = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
      ptr = getAdapter(env, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
      if (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        val = ptr->IfType == IF_TYPE_SOFTWARE_LOOPBACK ? JNI_TRUE : JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
      return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
 * Method:    isP2P0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
      IP_ADAPTER_ADDRESSES *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
      jboolean val = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
      ptr = getAdapter(env, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
      if (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        if (ptr->IfType == IF_TYPE_PPP || ptr->IfType == IF_TYPE_SLIP ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
           ptr->IfType == IF_TYPE_TUNNEL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
          val = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
      return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
}