jdk/src/windows/native/java/net/NetworkInterface.c
author ohair
Tue, 25 Mar 2008 14:38:56 -0700
changeset 20 41658053480c
parent 2 90ce3da70b43
child 910 1f53246fb014
permissions -rw-r--r--
6623832: Cleanup old j2se makefile targets Summary: Just removing unneeded makefile rules and 'control' logic. Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <winsock2.h>           /* needed for htonl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <iprtrmib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "java_net_NetworkInterface.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "NetworkInterface.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Windows implementation of the java.net.NetworkInterface native methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This module provides the implementations of getAll, getByName, getByIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and getByAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Interfaces and addresses are enumerated using the IP helper routines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * GetIfTable, GetIfAddrTable resp. These routines are available on Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * 98, NT SP+4, 2000, and XP. They are also available on Windows 95 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * IE is upgraded to 5.x.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Windows does not have any standard for device names so we are forced
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * to use our own convention which is based on the normal Unix naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * convention ("lo" for the loopback, eth0, eth1, .. for ethernet devices,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * tr0, tr1, .. for token ring, and so on). This convention gives us
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * consistency across multiple Windows editions and also consistency with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Solaris/Linux device names. Note that we always enumerate in index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * order and this ensures consistent device number across invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/* IP helper library routines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
int (PASCAL FAR *GetIpAddrTable_fn)();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
int (PASCAL FAR *GetIfTable_fn)();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
int (PASCAL FAR *GetFriendlyIfIndex_fn)();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
int (PASCAL FAR *GetAdaptersAddresses_fn)();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
int (PASCAL FAR *GetAdaptersInfo_fn)();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
int (PASCAL FAR *GetNumberOfInterfaces_fn)();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
/* Enumeration routines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
typedef int (*EnumerateNetInterfaces)(JNIEnv *, netif **);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
typedef int(*EnumerateNetAddresses)(JNIEnv *, netif *, netaddr **);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
static EnumerateNetInterfaces enumInterfaces_fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
static EnumerateNetAddresses enumAddresses_fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
/* Windows 9x routines are external (not needed on 64-bit) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#ifndef _WIN64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
extern int enumInterfaces_win9x(JNIEnv *, netif **);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
extern int enumAddresses_win9x(JNIEnv *, netif *, netaddr **);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
extern int init_win9x(void);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
extern int enumInterfaces_win(JNIEnv *env, netif **netifPP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
/* Windows 95/98/ME running */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
static jboolean isW9x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
/* Windows version supports */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
static jboolean os_supports_ipv6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
/* various JNI ids */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
jclass ni_class;            /* NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
jmethodID ni_ctor;          /* NetworkInterface() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
jfieldID ni_indexID;        /* NetworkInterface.index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
jfieldID ni_addrsID;        /* NetworkInterface.addrs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
jfieldID ni_bindsID;        /* NetworkInterface.bindings */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
jfieldID ni_nameID;         /* NetworkInterface.name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
jfieldID ni_displayNameID;  /* NetworkInterface.displayName */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
jfieldID ni_childsID;       /* NetworkInterface.childs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
jclass ni_iacls;            /* InetAddress */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
jfieldID ni_iaAddr;         /* InetAddress.address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
jclass ni_ia4cls;           /* Inet4Address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
jmethodID ni_ia4Ctor;       /* Inet4Address() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
jclass ni_ia6cls;           /* Inet6Address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
jmethodID ni_ia6ctrID;      /* Inet6Address() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
jfieldID ni_ia6ipaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
jfieldID ni_ia6ipaddressID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
jclass ni_ibcls;            /* InterfaceAddress */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
jmethodID ni_ibctrID;       /* InterfaceAddress() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
jfieldID ni_ibaddressID;        /* InterfaceAddress.address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
jfieldID ni_ibbroadcastID;      /* InterfaceAddress.broadcast */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
jfieldID ni_ibmaskID;           /* InterfaceAddress.maskLength */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * Support routines to free netif and netaddr lists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
void free_netif(netif *netifP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    netif *curr = netifP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (curr->name != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            free(curr->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (curr->displayName != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            free(curr->displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (curr->addrs != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            free_netaddr (curr->addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        netifP = netifP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        free(curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        curr = netifP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
void free_netaddr(netaddr *netaddrP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    netaddr *curr = netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        netaddrP = netaddrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        free(curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        curr = netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * Returns the interface structure from the table with the matching index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
MIB_IFROW *getIF(jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    MIB_IFTABLE *tableP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    MIB_IFROW *ifrowP, *ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    ULONG size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    DWORD i, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    jint ifindex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Ask the IP Helper library to enumerate the adapters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    size = sizeof(MIB_IFTABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    tableP = (MIB_IFTABLE *)malloc(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    count = (*GetIfTable_fn)(tableP, &size, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    if (count == ERROR_INSUFFICIENT_BUFFER || count == ERROR_BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        tableP = (MIB_IFTABLE *)realloc(tableP, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        count = (*GetIfTable_fn)(tableP, &size, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    if (count != NO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (tableP != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    if (tableP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
      ifrowP = tableP->table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
      for (i=0; i<tableP->dwNumEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * Warning the real index is obtained by GetFriendlyIfIndex()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        ifindex = (*GetFriendlyIfIndex_fn)(ifrowP->dwIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (ifindex == index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
          /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
           * Create a copy of the entry so that we can free the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
          ret = (MIB_IFROW *) malloc(sizeof(MIB_IFROW));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
          memcpy(ret, ifrowP, sizeof(MIB_IFROW));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        /* onto the next interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        ifrowP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
      free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * Enumerate network interfaces using IP Helper Library routine GetIfTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * We use GetIfTable rather than other IP helper routines because it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * available on 98 & NT SP4+.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * Returns the number of interfaces found or -1 if error. If no error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * occurs then netifPP be returned as list of netif structures or NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * if no interfaces are found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
int enumInterfaces_win(JNIEnv *env, netif **netifPP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    MIB_IFTABLE *tableP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    MIB_IFROW *ifrowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    ULONG size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    DWORD ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    netif *netifP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    DWORD i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    wchar_t wName[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    int lo=0, eth=0, tr=0, fddi=0, ppp=0, sl=0, net=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Ask the IP Helper library to enumerate the adapters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    size = sizeof(MIB_IFTABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    tableP = (MIB_IFTABLE *)malloc(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    ret = (*GetIfTable_fn)(tableP, &size, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    if (ret == ERROR_INSUFFICIENT_BUFFER || ret == ERROR_BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        tableP = (MIB_IFTABLE *)realloc(tableP, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        ret = (*GetIfTable_fn)(tableP, &size, TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    if (ret != NO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (tableP != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
#ifndef _WIN64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (isW9x && ret == ERROR_NOT_SUPPORTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
             * If ERROR_NOT_SUPPORTED is returned on Windows 98 it means that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
             * IE5.0 has been installed. In this case we revert to the Windows 95
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
             * approach and avoid using the IP Helper Library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
             * See: http://support.microsoft.com/support/kb/articles/q234/5/73.asp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            enumInterfaces_fn = enumInterfaces_win9x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            enumAddresses_fn = enumAddresses_win9x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            init_win9x();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return (*enumInterfaces_fn)(env, netifPP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        JNU_ThrowByName(env, "java/lang/Error",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                "IP Helper Library GetIfTable function failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Iterate through the list of adapters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    netifP = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    ifrowP = tableP->table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    for (i=0; i<tableP->dwNumEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        char dev_name[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        netif *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         * Generate a name for the device as Windows doesn't have any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         * real concept of a device name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        switch (ifrowP->dwType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            case MIB_IF_TYPE_ETHERNET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                sprintf(dev_name, "eth%d", eth++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            case MIB_IF_TYPE_TOKENRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                sprintf(dev_name, "tr%d", tr++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            case MIB_IF_TYPE_FDDI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                sprintf(dev_name, "fddi%d", fddi++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            case MIB_IF_TYPE_LOOPBACK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                /* There should only be only IPv4 loopback address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                if (lo > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                strcpy(dev_name, "lo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                lo++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            case MIB_IF_TYPE_PPP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                sprintf(dev_name, "ppp%d", ppp++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            case MIB_IF_TYPE_SLIP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                sprintf(dev_name, "sl%d", sl++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                sprintf(dev_name, "net%d", net++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * Allocate a netif structure and space for the name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         * display name (description in this case).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        curr = (netif *)calloc(1, sizeof(netif));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            curr->name = (char *)malloc(strlen(dev_name) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            curr->displayName = (char *)malloc(ifrowP->dwDescrLen + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (curr->name == NULL || curr->displayName == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                if (curr->name) free(curr->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if (curr->displayName) free(curr->displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                curr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (curr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            JNU_ThrowOutOfMemoryError(env, "heap allocation failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            free_netif(netifP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         * Populate the interface. Note that we need to convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * index into its "friendly" value as otherwise we will expose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         * 32-bit numbers as index values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        strcpy(curr->name, dev_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        strncpy(curr->displayName, ifrowP->bDescr, ifrowP->dwDescrLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        curr->displayName[ifrowP->dwDescrLen] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        curr->dwIndex = ifrowP->dwIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        curr->ifType = ifrowP->dwType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        curr->index = (*GetFriendlyIfIndex_fn)(ifrowP->dwIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * Put the interface at tail of list as GetIfTable(,,TRUE) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * returning the interfaces in index order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (netifP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            netifP = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            netif *tail = netifP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            while (tail->next != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                tail = tail->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            tail->next = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        /* onto the next interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        ifrowP++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Free the interface table and return the interface list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    if (tableP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    *netifPP = netifP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
 * Enumerate the IP addresses on an interface using the IP helper library
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
 * routine GetIfAddrTable and matching based on the index name. There are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
 * more efficient routines but we use GetIfAddrTable because it's avaliable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
 * on 98 and NT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 * Returns the count of addresses, or -1 if error. If no error occurs then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
 * netaddrPP will return a list of netaddr structures with the IP addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
int enumAddresses_win(JNIEnv *env, netif *netifP, netaddr **netaddrPP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    MIB_IPADDRTABLE *tableP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    ULONG size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    DWORD ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    DWORD i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    netaddr *netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    unsigned long mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Use GetIpAddrTable to enumerate the IP Addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    size = sizeof(MIB_IPADDRTABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    tableP = (MIB_IPADDRTABLE *)malloc(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    ret = (*GetIpAddrTable_fn)(&tableP, &size, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    if (ret == ERROR_INSUFFICIENT_BUFFER || ret == ERROR_BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        tableP = (MIB_IPADDRTABLE *)realloc(tableP, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        ret = (*GetIpAddrTable_fn)(tableP, &size, FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    if (ret != NO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (tableP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        JNU_ThrowByName(env, "java/lang/Error",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                "IP Helper Library GetIpAddrTable function failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Iterate through the table to find the addresses with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * matching dwIndex. Ignore 0.0.0.0 addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    netaddrP = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    while (i<tableP->dwNumEntries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (tableP->table[i].dwIndex == netifP->dwIndex &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            tableP->table[i].dwAddr != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            netaddr *curr = (netaddr *)malloc(sizeof(netaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (curr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                JNU_ThrowOutOfMemoryError(env, "heap allocation failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            curr->addr.him4.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            curr->addr.him4.sin_addr.s_addr = tableP->table[i].dwAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
             * Get netmask / broadcast address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            switch (netifP->ifType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            case MIB_IF_TYPE_ETHERNET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            case MIB_IF_TYPE_TOKENRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            case MIB_IF_TYPE_FDDI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            case MIB_IF_TYPE_LOOPBACK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
              /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
               * Contrary to what it seems to indicate, dwBCastAddr doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
               * contain the broadcast address but 0 or 1 depending on whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
               * the broadcast address should set the bits of the host part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
               * to 0 or 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
               * Yes, I know it's stupid, but what can I say, it's MSFTs API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
               */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
              curr->brdcast.him4.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
              if (tableP->table[i].dwBCastAddr == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                curr->brdcast.him4.sin_addr.s_addr = (tableP->table[i].dwAddr & tableP->table[i].dwMask) | (0xffffffff ^ tableP->table[i].dwMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
              else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                curr->brdcast.him4.sin_addr.s_addr = (tableP->table[i].dwAddr & tableP->table[i].dwMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
              mask = ntohl(tableP->table[i].dwMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
              curr->mask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
              while (mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                mask <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                curr->mask++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            case MIB_IF_TYPE_PPP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            case MIB_IF_TYPE_SLIP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
              /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
               * these don't have broadcast/subnet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
               */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
              curr->mask = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            curr->next = netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            netaddrP = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    *netaddrPP = netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    free(tableP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
 * Method:    init
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    OSVERSIONINFO ver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    HANDLE h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * First check if this is a Windows 9x machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    ver.dwOSVersionInfoSize = sizeof(ver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    GetVersionEx(&ver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    if (ver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && ver.dwMajorVersion == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        isW9x = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * Try to load the IP Helper Library and obtain the entry points we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * require. This will succeed on 98, NT SP4+, 2000 & XP. It will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * fail on Windows 95 (if IE hasn't been updated) and old versions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * of NT (IP helper library only appeared at SP4). If it fails on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * Windows 9x we will use the registry approach, otherwise if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * fails we throw an Error indicating that we have an incompatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * IP helper library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    h = LoadLibrary("iphlpapi.dll");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    if (h != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        GetIpAddrTable_fn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            (int (PASCAL FAR *)())GetProcAddress(h, "GetIpAddrTable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        GetIfTable_fn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            (int (PASCAL FAR *)())GetProcAddress(h, "GetIfTable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        GetFriendlyIfIndex_fn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            (int (PASCAL FAR *)())GetProcAddress(h, "GetFriendlyIfIndex");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        GetNumberOfInterfaces_fn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            (int (PASCAL FAR *)())GetProcAddress(h, "GetNumberOfInterfaces");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        GetAdaptersAddresses_fn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            (int (PASCAL FAR *)())GetProcAddress(h, "GetAdaptersAddresses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        GetAdaptersInfo_fn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            (int (PASCAL FAR *)())GetProcAddress(h, "GetAdaptersInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /* IPv6 is supported on Windows versions if the following APIs avail */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    os_supports_ipv6 = (GetAdaptersAddresses_fn != NULL) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                       (GetNumberOfInterfaces_fn != NULL) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                       (GetAdaptersInfo_fn != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    if (GetIpAddrTable_fn == NULL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        GetIfTable_fn == NULL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        GetFriendlyIfIndex_fn == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
#ifndef _WIN64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        if (isW9x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            /* Use Windows 9x registry approach which requires initialization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            enumInterfaces_fn = enumInterfaces_win9x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            enumAddresses_fn = enumAddresses_win9x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            init_win9x();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            JNU_ThrowByName(env, "java/lang/Error",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                "Incompatible IP helper library (iphlpapi.dll)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        enumInterfaces_fn = enumInterfaces_win;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        enumAddresses_fn = enumAddresses_win;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Get the various JNI ids that we require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    ni_class = (*env)->NewGlobalRef(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    ni_nameID = (*env)->GetFieldID(env, ni_class, "name", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    ni_displayNameID = (*env)->GetFieldID(env, ni_class, "displayName", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs", "[Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings", "[Ljava/net/InterfaceAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    ni_childsID = (*env)->GetFieldID(env, ni_class, "childs", "[Ljava/net/NetworkInterface;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    ni_ctor = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    ni_iacls = (*env)->FindClass(env, "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    ni_iaAddr = (*env)->GetFieldID(env, ni_iacls, "address", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    ni_ia4cls = (*env)->FindClass(env, "Ljava/net/Inet4Address;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    ni_ia4Ctor = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    ni_ibbroadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    ni_ibmaskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
 * Create a NetworkInterface object, populate the name and index, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
 * populate the InetAddress array based on the IP addresses for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
 * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
jobject createNetworkInterface(JNIEnv *env, netif *ifs, int netaddrCount, netaddr *netaddrP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    jobject netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    jobject name, displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    jobjectArray addrArr, bindsArr, childArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    netaddr *addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    jint addr_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    jint bind_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Create a NetworkInterface object and populate it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    netifObj = (*env)->NewObject(env, ni_class, ni_ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    name = (*env)->NewStringUTF(env, ifs->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    if (ifs->dNameIsUnicode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        displayName = (*env)->NewString(env, (PWCHAR)ifs->displayName, wcslen ((PWCHAR)ifs->displayName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        displayName = (*env)->NewStringUTF(env, ifs->displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    if (netifObj == NULL || name == NULL || displayName == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    (*env)->SetObjectField(env, netifObj, ni_nameID, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    (*env)->SetObjectField(env, netifObj, ni_displayNameID, displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * Get the IP addresses for this interface if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Note that 0 is a valid number of addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    if (netaddrCount < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        netaddrCount = (*enumAddresses_fn)(env, ifs, &netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    addrArr = (*env)->NewObjectArray(env, netaddrCount, ni_iacls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    if (addrArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    bindsArr = (*env)->NewObjectArray(env, netaddrCount, ni_ibcls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    if (bindsArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
      free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    addrs = netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    addr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    bind_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    while (addrs != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        jobject iaObj, ia2Obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        jobject ibObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (addrs->addr.him.sa_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (iaObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            /* default ctor will set family to AF_INET */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            (*env)->SetIntField(env, iaObj, ni_iaAddr, ntohl(addrs->addr.him4.sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (addrs->mask != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
              ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
              if (ibObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
              (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
              ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
              if (ia2Obj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
              (*env)->SetIntField(env, ia2Obj, ni_iaAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                                  ntohl(addrs->brdcast.him4.sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
              (*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
              (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
              (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        } else /* AF_INET6 */ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            int scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            if (iaObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                jbyteArray ipaddress = (*env)->NewByteArray(env, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                if (ipaddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    (jbyte *)&(addrs->addr.him6.sin6_addr.s6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                scope = addrs->addr.him6.sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                if (scope != 0) { /* zero is default value, no need to set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    (*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                if (ibObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                  free_netaddr(netaddrP);
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, ibObj, ni_ibaddressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        (*env)->SetObjectArrayElement(env, addrArr, addr_index, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        addrs = addrs->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        addr_index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    (*env)->SetObjectField(env, netifObj, ni_bindsID, bindsArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * Windows doesn't have virtual interfaces, so child array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * is always empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    childArr = (*env)->NewObjectArray(env, 0, ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    if (childArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    /* return the NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
 * Method:    getByName0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    (JNIEnv *env, jclass cls, jstring name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    const char *name_utf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    jobject netifObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    if (os_supports_ipv6 && ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        return Java_java_net_NetworkInterface_getByName0_XP (env, cls, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /* get the list of interfaces */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    if ((*enumInterfaces_fn)(env, &ifList) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    /* get the name as a C string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /* Search by name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if (strcmp(name_utf, curr->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    if (curr != NULL) {;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        netifObj = createNetworkInterface(env, curr, -1, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    /* release the UTF string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    (*env)->ReleaseStringUTFChars(env, name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
 * Class:     NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
 * Method:    getByIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
 * Signature: (I)LNetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
  (JNIEnv *env, jclass cls, jint index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    jobject netifObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    if (os_supports_ipv6 && ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        return Java_java_net_NetworkInterface_getByIndex_XP (env, cls, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    /* get the list of interfaces */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    if ((*enumInterfaces_fn)(env, &ifList) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    /* search by index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if (index == curr->index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    if (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        netifObj = createNetworkInterface(env, curr, -1, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
 * Method:    getByInetAddress0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    (JNIEnv *env, jclass cls, jobject iaObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    jint addr = (*env)->GetIntField(env, iaObj, ni_iaAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    jobject netifObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    if (os_supports_ipv6 && ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        return Java_java_net_NetworkInterface_getByInetAddress0_XP (env, cls, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    /* get the list of interfaces */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    if ((*enumInterfaces_fn)(env, &ifList) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * Enumerate the addresses on each interface until we find a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * matching address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        netaddr *addrList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        netaddr *addrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        /* enumerate the addresses on this interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        count = (*enumAddresses_fn)(env, curr, &addrList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        if (count < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        /* iterate through each address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        addrP = addrList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            if ((unsigned long)addr == ntohl(addrP->addr.him4.sin_addr.s_addr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            addrP = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
         * Address matched so create NetworkInterface for this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
         * and address list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        if (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            /* createNetworkInterface will free addrList */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            netifObj = createNetworkInterface(env, curr, count, addrList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        /* on next interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
 * Method:    getAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
 * Signature: ()[Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    (JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    jobjectArray netIFArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    jint arr_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    if (os_supports_ipv6 && ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        return Java_java_net_NetworkInterface_getAll_XP (env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * Get list of interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    count = (*enumInterfaces_fn)(env, &ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    if (count < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    /* allocate a NetworkInterface array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    netIFArr = (*env)->NewObjectArray(env, count, cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    if (netIFArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * Iterate through the interfaces, create a NetworkInterface instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * for each array element and populate the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    arr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        jobject netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        netifObj = createNetworkInterface(env, curr, -1, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (netifObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        /* put the NetworkInterface into the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    return netIFArr;
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
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
 * Method:    isUp0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
 * Signature: (Ljava/lang/String;)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
  jboolean ret = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
  if (os_supports_ipv6 && ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    return Java_java_net_NetworkInterface_isUp0_XP(env, cls, name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    MIB_IFROW *ifRowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    ifRowP = getIF(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    if (ifRowP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
      ret = ifRowP->dwAdminStatus == 1 && (ifRowP->dwOperStatus == MIB_IF_OPER_STATUS_OPERATIONAL || ifRowP->dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
      free(ifRowP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
 * Method:    isP2P0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0(JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
  MIB_IFROW *ifRowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
  jboolean ret = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
  if (os_supports_ipv6 && ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    return Java_java_net_NetworkInterface_isP2P0_XP(env, cls, name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    ifRowP = getIF(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    if (ifRowP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
      switch(ifRowP->dwType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
      case MIB_IF_TYPE_PPP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
      case MIB_IF_TYPE_SLIP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        ret = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
      free(ifRowP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
  return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
 * Method:    isLoopback0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
  MIB_IFROW *ifRowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
  jboolean ret = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
  if (os_supports_ipv6 && ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    return Java_java_net_NetworkInterface_isLoopback0_XP(env, cls, name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    ifRowP = getIF(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    if (ifRowP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
      if (ifRowP->dwType == MIB_IF_TYPE_LOOPBACK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        ret = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
      free(ifRowP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
 * Method:    supportsMulticast0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
  MIB_IFROW *ifRowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
  jboolean ret = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
  // Let's try to use the newer API (XP & 2003 only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
  if (GetAdaptersAddresses_fn != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    ret = Java_java_net_NetworkInterface_supportsMulticast0_XP(env, cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                                                               name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
  ifRowP = getIF(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
  if (ifRowP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    if (ifRowP->dwType == MIB_IF_TYPE_LOOPBACK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
      ret = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    free(ifRowP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
  return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
 * Method:    getMacAddr0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
 * Signature: ([bLjava/lang/String;I)[b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0(JNIEnv *env, jclass class, jbyteArray addrArray, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
  jbyteArray ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
  int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
  MIB_IFROW *ifRowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
  if (os_supports_ipv6 && ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    return Java_java_net_NetworkInterface_getMacAddr0_XP(env, class, 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
      switch(ifRowP->dwType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
      case MIB_IF_TYPE_ETHERNET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
      case MIB_IF_TYPE_TOKENRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
      case MIB_IF_TYPE_FDDI:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        len = ifRowP->dwPhysAddrLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        ret = (*env)->NewByteArray(env, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        if (!IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
          (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *) ifRowP->bPhysAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
      free(ifRowP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
 * Class:       java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
 * Method:      getMTU0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
 * Signature:   ([bLjava/lang/String;I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclass class, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
  jint ret = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
  MIB_IFROW *ifRowP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
  if (os_supports_ipv6 && ipv6_available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    return Java_java_net_NetworkInterface_getMTU0_XP(env, class, name, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    ifRowP = getIF(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    if (ifRowP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
      ret = ifRowP->dwMtu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
      free(ifRowP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
}