jdk/src/windows/native/java/net/NetworkInterface.c
author michaelm
Wed, 13 Feb 2013 10:40:31 +0000
changeset 16870 f35b2bd19761
parent 14768 8800c12ef525
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: 13245
diff changeset
     2
 * Copyright (c) 2000, 2012, 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: 1247
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: 1247
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: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
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"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Windows implementation of the java.net.NetworkInterface native methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This module provides the implementations of getAll, getByName, getByIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and getByAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Interfaces and addresses are enumerated using the IP helper routines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * GetIfTable, GetIfAddrTable resp. These routines are available on Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * 98, NT SP+4, 2000, and XP. They are also available on Windows 95 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * IE is upgraded to 5.x.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Windows does not have any standard for device names so we are forced
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * to use our own convention which is based on the normal Unix naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * convention ("lo" for the loopback, eth0, eth1, .. for ethernet devices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * tr0, tr1, .. for token ring, and so on). This convention gives us
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * consistency across multiple Windows editions and also consistency with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Solaris/Linux device names. Note that we always enumerate in index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * order and this ensures consistent device number across invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/* various JNI ids */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
jclass ni_class;            /* NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
jmethodID ni_ctor;          /* NetworkInterface() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
jfieldID ni_indexID;        /* NetworkInterface.index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
jfieldID ni_addrsID;        /* NetworkInterface.addrs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
jfieldID ni_bindsID;        /* NetworkInterface.bindings */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
jfieldID ni_nameID;         /* NetworkInterface.name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
jfieldID ni_displayNameID;  /* NetworkInterface.displayName */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
jfieldID ni_childsID;       /* NetworkInterface.childs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
jclass ni_iacls;            /* InetAddress */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
jclass ni_ia4cls;           /* Inet4Address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
jmethodID ni_ia4Ctor;       /* Inet4Address() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
jclass ni_ia6cls;           /* Inet6Address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
jmethodID ni_ia6ctrID;      /* Inet6Address() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
jfieldID ni_ia6ipaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
jfieldID ni_ia6ipaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
jclass ni_ibcls;            /* InterfaceAddress */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
jmethodID ni_ibctrID;       /* InterfaceAddress() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
jfieldID ni_ibaddressID;        /* InterfaceAddress.address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
jfieldID ni_ibbroadcastID;      /* InterfaceAddress.broadcast */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
jfieldID ni_ibmaskID;           /* InterfaceAddress.maskLength */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * Support routines to free netif and netaddr lists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
void free_netif(netif *netifP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    netif *curr = netifP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (curr->name != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            free(curr->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (curr->displayName != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            free(curr->displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (curr->addrs != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            free_netaddr (curr->addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        netifP = netifP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        free(curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        curr = netifP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
void free_netaddr(netaddr *netaddrP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    netaddr *curr = netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        netaddrP = netaddrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        free(curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        curr = netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
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
 * Returns the interface structure from the table with the matching index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
MIB_IFROW *getIF(jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    MIB_IFTABLE *tableP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    MIB_IFROW *ifrowP, *ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    ULONG size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    DWORD i, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    jint ifindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Ask the IP Helper library to enumerate the adapters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    size = sizeof(MIB_IFTABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    tableP = (MIB_IFTABLE *)malloc(size);
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   126
    count = GetIfTable(tableP, &size, TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    if (count == ERROR_INSUFFICIENT_BUFFER || count == ERROR_BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        tableP = (MIB_IFTABLE *)realloc(tableP, size);
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   129
        count = GetIfTable(tableP, &size, TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    if (count != NO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (tableP != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    if (tableP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
      ifrowP = tableP->table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
      for (i=0; i<tableP->dwNumEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * Warning the real index is obtained by GetFriendlyIfIndex()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   144
        ifindex = GetFriendlyIfIndex(ifrowP->dwIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (ifindex == index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
          /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
           * Create a copy of the entry so that we can free the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
          ret = (MIB_IFROW *) malloc(sizeof(MIB_IFROW));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
          memcpy(ret, ifrowP, sizeof(MIB_IFROW));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        /* onto the next interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        ifrowP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
      free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    return ret;
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
 * Enumerate network interfaces using IP Helper Library routine GetIfTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * We use GetIfTable rather than other IP helper routines because it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * available on 98 & NT SP4+.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * Returns the number of interfaces found or -1 if error. If no error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * occurs then netifPP be returned as list of netif structures or NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * if no interfaces are found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   171
int enumInterfaces(JNIEnv *env, netif **netifPP)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    MIB_IFTABLE *tableP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    MIB_IFROW *ifrowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    ULONG size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    DWORD ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    netif *netifP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    DWORD i;
14768
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   180
    int lo=0, eth=0, tr=0, fddi=0, ppp=0, sl=0, wlan=0, net=0, wlen=0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Ask the IP Helper library to enumerate the adapters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    size = sizeof(MIB_IFTABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    tableP = (MIB_IFTABLE *)malloc(size);
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   187
    ret = GetIfTable(tableP, &size, TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    if (ret == ERROR_INSUFFICIENT_BUFFER || ret == ERROR_BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        tableP = (MIB_IFTABLE *)realloc(tableP, size);
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   190
        ret = GetIfTable(tableP, &size, TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    if (ret != NO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (tableP != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        JNU_ThrowByName(env, "java/lang/Error",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                "IP Helper Library GetIfTable function failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Iterate through the list of adapters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    netifP = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    ifrowP = tableP->table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    for (i=0; i<tableP->dwNumEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        char dev_name[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        netif *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * Generate a name for the device as Windows doesn't have any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         * real concept of a device name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        switch (ifrowP->dwType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            case MIB_IF_TYPE_ETHERNET:
12542
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   220
                _snprintf_s(dev_name, 8, _TRUNCATE, "eth%d", eth++);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            case MIB_IF_TYPE_TOKENRING:
12542
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   224
                _snprintf_s(dev_name, 8, _TRUNCATE, "tr%d", tr++);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            case MIB_IF_TYPE_FDDI:
12542
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   228
                _snprintf_s(dev_name, 8, _TRUNCATE, "fddi%d", fddi++);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            case MIB_IF_TYPE_LOOPBACK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                /* There should only be only IPv4 loopback address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                if (lo > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                }
12542
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   236
                strncpy_s(dev_name, 8, "lo", _TRUNCATE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                lo++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            case MIB_IF_TYPE_PPP:
12542
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   241
                _snprintf_s(dev_name, 8, _TRUNCATE, "ppp%d", ppp++);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            case MIB_IF_TYPE_SLIP:
12542
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   245
                _snprintf_s(dev_name, 8, _TRUNCATE, "sl%d", sl++);
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   246
                break;
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   247
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   248
            case IF_TYPE_IEEE80211:
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   249
                _snprintf_s(dev_name, 8, _TRUNCATE, "wlan%d", wlan++);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            default:
12542
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   253
                _snprintf_s(dev_name, 8, _TRUNCATE, "net%d", net++);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         * Allocate a netif structure and space for the name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
         * display name (description in this case).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        curr = (netif *)calloc(1, sizeof(netif));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (curr != NULL) {
14768
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   262
            wlen = MultiByteToWideChar(CP_OEMCP, 0, ifrowP->bDescr,
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   263
                       ifrowP->dwDescrLen, NULL, 0);
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   264
            if(wlen == 0) {
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   265
                // MultiByteToWideChar should not fail
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   266
                // But in rare case it fails, we allow 'char' to be displayed
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   267
                curr->displayName = (char *)malloc(ifrowP->dwDescrLen + 1);
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   268
            } else {
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   269
                curr->displayName = (wchar_t *)malloc(wlen*(sizeof(wchar_t))+1);
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   270
            }
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   271
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            curr->name = (char *)malloc(strlen(dev_name) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (curr->name == NULL || curr->displayName == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if (curr->name) free(curr->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (curr->displayName) free(curr->displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                curr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (curr == NULL) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12542
diff changeset
   281
            JNU_ThrowOutOfMemoryError(env, "Native heap allocation failure");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            free_netif(netifP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * Populate the interface. Note that we need to convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         * index into its "friendly" value as otherwise we will expose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         * 32-bit numbers as index values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        strcpy(curr->name, dev_name);
14768
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   293
        if (wlen == 0) {
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   294
            // display char type in case of MultiByteToWideChar failure
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   295
            strncpy(curr->displayName, ifrowP->bDescr, ifrowP->dwDescrLen);
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   296
            curr->displayName[ifrowP->dwDescrLen] = '\0';
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   297
        } else {
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   298
            // call MultiByteToWideChar again to fill curr->displayName
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   299
            // it should not fail, because we have called it once before
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   300
            if (MultiByteToWideChar(CP_OEMCP, 0, ifrowP->bDescr,
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   301
                   ifrowP->dwDescrLen, curr->displayName, wlen) == 0) {
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   302
                JNU_ThrowByName(env, "java/lang/Error",
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   303
                       "Cannot get multibyte char for interface display name");
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   304
                free_netif(netifP);
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   305
                free(tableP);
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   306
                free(curr->name);
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   307
                free(curr->displayName);
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   308
                free(curr);
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   309
                return -1;
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   310
            } else {
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   311
                curr->displayName[wlen*(sizeof(wchar_t))] = '\0';
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   312
                curr->dNameIsUnicode = TRUE;
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   313
            }
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   314
        }
8800c12ef525 6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents: 14342
diff changeset
   315
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        curr->dwIndex = ifrowP->dwIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        curr->ifType = ifrowP->dwType;
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   318
        curr->index = GetFriendlyIfIndex(ifrowP->dwIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * Put the interface at tail of list as GetIfTable(,,TRUE) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * returning the interfaces in index order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (netifP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            netifP = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            netif *tail = netifP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            while (tail->next != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                tail = tail->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            tail->next = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        /* onto the next interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        ifrowP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Free the interface table and return the interface list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    if (tableP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    *netifPP = netifP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
 * Enumerate the IP addresses on an interface using the IP helper library
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
 * routine GetIfAddrTable and matching based on the index name. There are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
 * more efficient routines but we use GetIfAddrTable because it's avaliable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
 * on 98 and NT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
 * Returns the count of addresses, or -1 if error. If no error occurs then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
 * netaddrPP will return a list of netaddr structures with the IP addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
int enumAddresses_win(JNIEnv *env, netif *netifP, netaddr **netaddrPP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    MIB_IPADDRTABLE *tableP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    ULONG size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    DWORD ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    DWORD i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    netaddr *netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    unsigned long mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Use GetIpAddrTable to enumerate the IP Addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    size = sizeof(MIB_IPADDRTABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    tableP = (MIB_IPADDRTABLE *)malloc(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   374
    ret = GetIpAddrTable(tableP, &size, FALSE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    if (ret == ERROR_INSUFFICIENT_BUFFER || ret == ERROR_BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        tableP = (MIB_IPADDRTABLE *)realloc(tableP, size);
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   377
        ret = GetIpAddrTable(tableP, &size, FALSE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    if (ret != NO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (tableP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        JNU_ThrowByName(env, "java/lang/Error",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                "IP Helper Library GetIpAddrTable function failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Iterate through the table to find the addresses with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * matching dwIndex. Ignore 0.0.0.0 addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    netaddrP = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    while (i<tableP->dwNumEntries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (tableP->table[i].dwIndex == netifP->dwIndex &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            tableP->table[i].dwAddr != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            netaddr *curr = (netaddr *)malloc(sizeof(netaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (curr == NULL) {
13245
7ab3ef5b9520 7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents: 12542
diff changeset
   402
                JNU_ThrowOutOfMemoryError(env, "Native heap allocation failure");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            curr->addr.him4.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            curr->addr.him4.sin_addr.s_addr = tableP->table[i].dwAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
             * Get netmask / broadcast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            switch (netifP->ifType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            case MIB_IF_TYPE_ETHERNET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            case MIB_IF_TYPE_TOKENRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            case MIB_IF_TYPE_FDDI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            case MIB_IF_TYPE_LOOPBACK:
12542
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   418
            case IF_TYPE_IEEE80211:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
              /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
               * Contrary to what it seems to indicate, dwBCastAddr doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
               * contain the broadcast address but 0 or 1 depending on whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
               * the broadcast address should set the bits of the host part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
               * to 0 or 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
               * Yes, I know it's stupid, but what can I say, it's MSFTs API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
               */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
              curr->brdcast.him4.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
              if (tableP->table[i].dwBCastAddr == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                curr->brdcast.him4.sin_addr.s_addr = (tableP->table[i].dwAddr & tableP->table[i].dwMask) | (0xffffffff ^ tableP->table[i].dwMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
              else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                curr->brdcast.him4.sin_addr.s_addr = (tableP->table[i].dwAddr & tableP->table[i].dwMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
              mask = ntohl(tableP->table[i].dwMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
              curr->mask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
              while (mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                mask <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                curr->mask++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            case MIB_IF_TYPE_PPP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            case MIB_IF_TYPE_SLIP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
              /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
               * these don't have broadcast/subnet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
               */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
              curr->mask = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            curr->next = netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            netaddrP = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    *netaddrPP = netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
 * Method:    init
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Get the various JNI ids that we require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    ni_class = (*env)->NewGlobalRef(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    ni_nameID = (*env)->GetFieldID(env, ni_class, "name", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    ni_displayNameID = (*env)->GetFieldID(env, ni_class, "displayName", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs", "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings", "[Ljava/net/InterfaceAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    ni_childsID = (*env)->GetFieldID(env, ni_class, "childs", "[Ljava/net/NetworkInterface;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    ni_ctor = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
1094
0cb4746beb0c 6734171: java.net.NetworkInterface reports XCheck:jni warnings
chegar
parents: 910
diff changeset
   480
    ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
1094
0cb4746beb0c 6734171: java.net.NetworkInterface reports XCheck:jni warnings
chegar
parents: 910
diff changeset
   483
    ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    ni_ia4Ctor = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    ni_ibbroadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    ni_ibmaskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
 * Create a NetworkInterface object, populate the name and index, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
 * populate the InetAddress array based on the IP addresses for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
 * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
 */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   506
jobject createNetworkInterface
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   507
    (JNIEnv *env, netif *ifs, int netaddrCount, netaddr *netaddrP)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    jobject netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    jobject name, displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    jobjectArray addrArr, bindsArr, childArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    netaddr *addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    jint addr_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    jint bind_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * Create a NetworkInterface object and populate it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    netifObj = (*env)->NewObject(env, ni_class, ni_ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    name = (*env)->NewStringUTF(env, ifs->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    if (ifs->dNameIsUnicode) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   522
        displayName = (*env)->NewString(env, (PWCHAR)ifs->displayName,
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   523
                                       (jsize)wcslen ((PWCHAR)ifs->displayName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        displayName = (*env)->NewStringUTF(env, ifs->displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    if (netifObj == NULL || name == NULL || displayName == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    (*env)->SetObjectField(env, netifObj, ni_nameID, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    (*env)->SetObjectField(env, netifObj, ni_displayNameID, displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Get the IP addresses for this interface if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Note that 0 is a valid number of addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    if (netaddrCount < 0) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   539
        netaddrCount = enumAddresses_win(env, ifs, &netaddrP);
10417
947e9a7bf244 7084560: Crash in net.dll
michaelm
parents: 9003
diff changeset
   540
        if (netaddrCount == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    addrArr = (*env)->NewObjectArray(env, netaddrCount, ni_iacls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    if (addrArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    bindsArr = (*env)->NewObjectArray(env, netaddrCount, ni_ibcls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    if (bindsArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
      free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    addrs = netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    addr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    bind_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    while (addrs != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        jobject iaObj, ia2Obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        jobject ibObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (addrs->addr.him.sa_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            if (iaObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            /* default ctor will set family to AF_INET */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14768
diff changeset
   569
            setInetAddress_addr(env, iaObj, ntohl(addrs->addr.him4.sin_addr.s_addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            if (addrs->mask != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
              ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
              if (ibObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
              (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
              ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
              if (ia2Obj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
              }
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14768
diff changeset
   582
              setInetAddress_addr(env, ia2Obj, ntohl(addrs->brdcast.him4.sin_addr.s_addr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
              (*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
              (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
              (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        } else /* AF_INET6 */ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            int scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            if (iaObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                jbyteArray ipaddress = (*env)->NewByteArray(env, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                if (ipaddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    (jbyte *)&(addrs->addr.him6.sin6_addr.s6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                scope = addrs->addr.him6.sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                if (scope != 0) { /* zero is default value, no need to set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    (*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                if (ibObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                  free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                  return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        (*env)->SetObjectArrayElement(env, addrArr, addr_index, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        addrs = addrs->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        addr_index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    (*env)->SetObjectField(env, netifObj, ni_bindsID, bindsArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Windows doesn't have virtual interfaces, so child array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * is always empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    childArr = (*env)->NewObjectArray(env, 0, ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    if (childArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /* return the NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
 * Method:    getByName0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    (JNIEnv *env, jclass cls, jstring name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    const char *name_utf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    jobject netifObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   650
    // Retained for now to support IPv4 only stack, java.net.preferIPv4Stack
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   651
    if (ipv6_available()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return Java_java_net_NetworkInterface_getByName0_XP (env, cls, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /* get the list of interfaces */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   656
    if (enumInterfaces(env, &ifList) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /* get the name as a C string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    /* Search by name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (strcmp(name_utf, curr->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    if (curr != NULL) {;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        netifObj = createNetworkInterface(env, curr, -1, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /* release the UTF string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    (*env)->ReleaseStringUTFChars(env, name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
 * Class:     NetworkInterface
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 1094
diff changeset
   688
 * Method:    getByIndex0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
 * Signature: (I)LNetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
 */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 1094
diff changeset
   691
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
  (JNIEnv *env, jclass cls, jint index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    jobject netifObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   697
    // Retained for now to support IPv4 only stack, java.net.preferIPv4Stack
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   698
    if (ipv6_available()) {
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 1094
diff changeset
   699
        return Java_java_net_NetworkInterface_getByIndex0_XP (env, cls, index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    /* get the list of interfaces */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   703
    if (enumInterfaces(env, &ifList) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    /* search by index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        if (index == curr->index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    if (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        netifObj = createNetworkInterface(env, curr, -1, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
 * Method:    getByInetAddress0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    (JNIEnv *env, jclass cls, jobject iaObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    netif *ifList, *curr;
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14768
diff changeset
   736
    jint addr = getInetAddress_addr(env, iaObj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    jobject netifObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   739
    // Retained for now to support IPv4 only stack, java.net.preferIPv4Stack
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   740
    if (ipv6_available()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        return Java_java_net_NetworkInterface_getByInetAddress0_XP (env, cls, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    /* get the list of interfaces */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   745
    if (enumInterfaces(env, &ifList) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * Enumerate the addresses on each interface until we find a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * matching address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        netaddr *addrList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        netaddr *addrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        /* enumerate the addresses on this interface */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   760
        count = enumAddresses_win(env, curr, &addrList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        if (count < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        /* iterate through each address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        addrP = addrList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            if ((unsigned long)addr == ntohl(addrP->addr.him4.sin_addr.s_addr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            addrP = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
         * Address matched so create NetworkInterface for this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
         * and address list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        if (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            /* createNetworkInterface will free addrList */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            netifObj = createNetworkInterface(env, curr, count, addrList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        /* on next interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
 * Method:    getAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
 * Signature: ()[Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    (JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    jobjectArray netIFArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    jint arr_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   809
    // Retained for now to support IPv4 only stack, java.net.preferIPv4Stack
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   810
    if (ipv6_available()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        return Java_java_net_NetworkInterface_getAll_XP (env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * Get list of interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   817
    count = enumInterfaces(env, &ifList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    if (count < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    /* allocate a NetworkInterface array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    netIFArr = (*env)->NewObjectArray(env, count, cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    if (netIFArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * Iterate through the interfaces, create a NetworkInterface instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * for each array element and populate the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    arr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        jobject netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        netifObj = createNetworkInterface(env, curr, -1, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        if (netifObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        /* put the NetworkInterface into the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    return netIFArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
 * Method:    isUp0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
 * Signature: (Ljava/lang/String;)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
  jboolean ret = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   863
  // Retained for now to support IPv4 only stack, java.net.preferIPv4Stack
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   864
  if (ipv6_available()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    return Java_java_net_NetworkInterface_isUp0_XP(env, cls, name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    MIB_IFROW *ifRowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    ifRowP = getIF(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    if (ifRowP != NULL) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   870
      ret = ifRowP->dwAdminStatus == 1 &&
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   871
            (ifRowP->dwOperStatus == MIB_IF_OPER_STATUS_OPERATIONAL ||
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   872
             ifRowP->dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
      free(ifRowP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
 * Method:    isP2P0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
 */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   884
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   885
    (JNIEnv *env, jclass cls, jstring name, jint index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
  MIB_IFROW *ifRowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
  jboolean ret = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   889
  // Retained for now to support IPv4 only stack, java.net.preferIPv4Stack
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   890
  if (ipv6_available()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    return Java_java_net_NetworkInterface_isP2P0_XP(env, cls, name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    ifRowP = getIF(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    if (ifRowP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
      switch(ifRowP->dwType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
      case MIB_IF_TYPE_PPP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
      case MIB_IF_TYPE_SLIP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        ret = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
      free(ifRowP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
  return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
 * Method:    isLoopback0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
  MIB_IFROW *ifRowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
  jboolean ret = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   917
  // Retained for now to support IPv4 only stack, java.net.preferIPv4Stack
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   918
  if (ipv6_available()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    return Java_java_net_NetworkInterface_isLoopback0_XP(env, cls, name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    ifRowP = getIF(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    if (ifRowP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
      if (ifRowP->dwType == MIB_IF_TYPE_LOOPBACK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        ret = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
      free(ifRowP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
 * Method:    supportsMulticast0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    (JNIEnv *env, jclass cls, jstring name, jint index) {
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   938
    return Java_java_net_NetworkInterface_supportsMulticast0_XP(env, cls,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                                                               name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
 * Method:    getMacAddr0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
 * Signature: ([bLjava/lang/String;I)[b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
 */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   947
JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   948
    (JNIEnv *env, jclass class, jbyteArray addrArray, jstring name, jint index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
  jbyteArray ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
  int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
  MIB_IFROW *ifRowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   953
  // Retained for now to support IPv4 only stack, java.net.preferIPv4Stack
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   954
  if (ipv6_available()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    return Java_java_net_NetworkInterface_getMacAddr0_XP(env, class, name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    ifRowP = getIF(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    if (ifRowP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
      switch(ifRowP->dwType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
      case MIB_IF_TYPE_ETHERNET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
      case MIB_IF_TYPE_TOKENRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
      case MIB_IF_TYPE_FDDI:
12542
31190502e9e3 7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents: 10417
diff changeset
   963
      case IF_TYPE_IEEE80211:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        len = ifRowP->dwPhysAddrLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        ret = (*env)->NewByteArray(env, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        if (!IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
          (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *) ifRowP->bPhysAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
      free(ifRowP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
 * Class:       java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
 * Method:      getMTU0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
 * Signature:   ([bLjava/lang/String;I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
 */
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   982
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   983
    (JNIEnv *env, jclass class, jstring name, jint index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
  jint ret = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
  MIB_IFROW *ifRowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
9003
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   987
  // Retained for now to support IPv4 only stack, java.net.preferIPv4Stack
e1b0f0099915 7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents: 5506
diff changeset
   988
  if (ipv6_available()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    return Java_java_net_NetworkInterface_getMTU0_XP(env, class, name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    ifRowP = getIF(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    if (ifRowP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
      ret = ifRowP->dwMtu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
      free(ifRowP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
}