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