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