jdk/src/windows/native/java/net/NetworkInterface_winXP.c
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 9003 e1b0f0099915
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4811
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <winsock2.h>           /* needed for htonl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <iprtrmib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "java_net_NetworkInterface.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "NetworkInterface.h"
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;
4811
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   175
    netif *nif=0, *dup_nif, *last=0, *loopif=0, *curr;
2
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
4811
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   200
    // Retrieve IPv4 addresses with the IP Helper API
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   201
    curr = *netifPP;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   202
    while (curr != NULL) {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   203
        netaddr *netaddrP;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   204
        ret = enumAddresses_win(env, curr, &netaddrP);
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   205
        if ((*env)->ExceptionOccurred(env)) {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   206
            free_netaddr(netaddrP);
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   207
            return -1;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   208
        }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   209
        curr->addrs = netaddrP;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   210
        curr->naddrs += ret;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   211
        curr = curr->next;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   212
    }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   213
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    ret = getAdapters (env, &adapters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    if (ret != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /* Now get the IPv6 information. This includes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *  (a)  IPv6 information associated with interfaces already found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *  (b)  IPv6 information for IPv6 only interfaces (probably tunnels)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * For compatibility with previous releases we use the naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * information gotten from enumInterfaces_win() for (a) entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * However, the index numbers are taken from the new API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * The procedure is to go through the list of adapters returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * by the new API looking for entries that correspond to IPv4 interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * already found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    ptr = adapters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    while (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        netif *nif0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (ptr->IfType == IF_TYPE_SOFTWARE_LOOPBACK && (loopif != NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            c = getAddrsFromAdapter(ptr, &loopif->addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            loopif->naddrs += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            int index = ptr->IfIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (index != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                /* This entry is associated with an IPv4 interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                for (nif=*netifPP; nif!=0; nif=nif->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    if (nif->index == index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        /* found the interface entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                         * set the index to the IPv6 index and add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                         * IPv6 addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        nif->index = ptr->Ipv6IfIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        c = getAddrsFromAdapter(ptr, &nif->addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        nif->naddrs += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                /* This entry is IPv6 only */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                char newname [128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                /* Windows allocates duplicate adapter entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                 * for tunnel interfaces when there are multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                 * physical adapters. Need to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                 * if this is a duplicate (ipv6Index is the same)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                dup_nif = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                for (nif0=*netifPP; nif0!=0; nif0=nif0->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    if (nif0->hasIpv6Address &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                ptr->Ipv6IfIndex == nif0->ipv6Index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        dup_nif = nif0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (dup_nif == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    /* new interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    nif = (netif *) calloc (1, sizeof(netif));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    if (nif == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    if (ptr->IfType == IF_TYPE_TUNNEL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        sprintf (newname, "tun%d", tun);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        tun ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        sprintf (newname, "net%d", net);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        net ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    nif->name = malloc (strlen(newname)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    nif->displayName = malloc (wcslen(ptr->FriendlyName)*2+2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    if (nif->name == 0 || nif->displayName == 0) {
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
                    strcpy (nif->name, newname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    wcscpy ((PWCHAR)nif->displayName, ptr->FriendlyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    nif->dNameIsUnicode = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    nif->index = ptr->Ipv6IfIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    nif->ipv6Index = ptr->Ipv6IfIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    nif->hasIpv6Address = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    last->next = nif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    last = nif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    c = getAddrsFromAdapter(ptr, &nif->addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    nif->naddrs += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    /* add the addresses from this adapter to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                     * original (dup_nif)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    c = getAddrsFromAdapter(ptr, &dup_nif->addrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        goto err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    dup_nif->naddrs += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        ptr=ptr->Next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    free (adapters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
err:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    if (*netifPP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        free_netif (*netifPP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    if (adapters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        free (adapters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
/* If *netaddrPP is null, then the addresses are allocated and the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
 * of the allocated chain is returned in *netaddrPP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
 * If *netaddrPP is not null, then the addresses allocated here are appended
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
 * to the existing chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
 * Returns count of addresses or -1 on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
static int getAddrsFromAdapter(IP_ADAPTER_ADDRESSES *ptr, netaddr **netaddrPP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    LPSOCKADDR                   sock;
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   347
    int                          count = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    netaddr                     *curr, *start=0, *prev=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    PIP_ADAPTER_UNICAST_ADDRESS uni_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    PIP_ADAPTER_ANYCAST_ADDRESS any_addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    PIP_ADAPTER_PREFIX prefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /* If chain passed in, find end */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    if (*netaddrPP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        for (start=*netaddrPP; start->next!=NULL; start=start->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        prev=start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    prefix = ptr->FirstPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /* Unicast */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    uni_addr = ptr->FirstUnicastAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    while (uni_addr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        /* address is only usable if dad state is preferred or deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (uni_addr->DadState == IpDadStateDeprecated ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                uni_addr->DadState == IpDadStatePreferred) {
4811
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   367
            sock = uni_addr->Address.lpSockaddr;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   368
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   369
            // IPv4 addresses already retrieved with enumAddresses_win
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   370
            if (sock->sa_family == AF_INET) {
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   371
                uni_addr = uni_addr->Next;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   372
                continue;
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   373
            }
448740c7f12d 6707289: InterfaceAddress.getNetworkPrefixLength() does not conform to Javadoc
chegar
parents: 1247
diff changeset
   374
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            curr = (netaddr *)calloc (1, sizeof (netaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if (curr == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (start == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                start = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            if (prev != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                prev->next = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            prev = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            SOCKETADDRESS_COPY (&curr->addr, sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (prefix != NULL) {
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   388
              curr->mask = (short)prefix->PrefixLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
              prefix = prefix->Next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        uni_addr = uni_addr->Next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /* Anycast */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    any_addr = ptr->FirstAnycastAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    while (any_addr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        curr = (netaddr *)calloc (1, sizeof (netaddr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (curr == 0) {
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
        if (start == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            start = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (prev != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            prev->next = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        prev = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        sock = any_addr->Address.lpSockaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        SOCKETADDRESS_COPY (&curr->addr, sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        any_addr = any_addr->Next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    if (*netaddrPP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        *netaddrPP = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
}
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, populate the name and index, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
 * populate the InetAddress array based on the IP addresses for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
 * interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
static jobject createNetworkInterfaceXP(JNIEnv *env, netif *ifs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    jobject netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    jobject name, displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    jobjectArray addrArr, bindsArr, childArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    netaddr *addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    jint addr_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    int netaddrCount=ifs->naddrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    netaddr *netaddrP=ifs->addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    jint bind_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Create a NetworkInterface object and populate it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    netifObj = (*env)->NewObject(env, ni_class, ni_ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    name = (*env)->NewStringUTF(env, ifs->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    if (ifs->dNameIsUnicode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        displayName = (*env)->NewString(env, (PWCHAR)ifs->displayName, wcslen ((PWCHAR)ifs->displayName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        displayName = (*env)->NewStringUTF(env, ifs->displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    if (netifObj == NULL || name == NULL || displayName == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    (*env)->SetObjectField(env, netifObj, ni_nameID, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    (*env)->SetObjectField(env, netifObj, ni_displayNameID, displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * Get the IP addresses for this interface if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Note that 0 is a valid number of addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    if (netaddrCount < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        netaddrCount = enumAddresses_win(env, ifs, &netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    addrArr = (*env)->NewObjectArray(env, netaddrCount, ni_iacls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    if (addrArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    bindsArr = (*env)->NewObjectArray(env, netaddrCount, ni_ibcls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    if (bindsArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
      free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    addrs = netaddrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    addr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    bind_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    while (addrs != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        jobject iaObj, ia2Obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        jobject ibObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (addrs->addr.him.sa_family == AF_INET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if (iaObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            /* default ctor will set family to AF_INET */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            (*env)->SetIntField(env, iaObj, ni_iaAddr, ntohl(addrs->addr.him4.sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (ibObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
              free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
              return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if (ia2Obj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
              free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
              return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            (*env)->SetIntField(env, ia2Obj, ni_iaAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                                ntohl(addrs->brdcast.him4.sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            (*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        } else /* AF_INET6 */ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            int scope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            if (iaObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                jbyteArray ipaddress = (*env)->NewByteArray(env, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                if (ipaddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    (jbyte *)&(addrs->addr.him6.sin6_addr.s6_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                scope = addrs->addr.him6.sin6_scope_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                if (scope != 0) { /* zero is default value, no need to set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    (*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                if (ibObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                  free_netaddr(netaddrP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                  return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        (*env)->SetObjectArrayElement(env, addrArr, addr_index, iaObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        addrs = addrs->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        addr_index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    (*env)->SetObjectField(env, netifObj, ni_bindsID, bindsArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Windows doesn't have virtual interfaces, so child array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * is always empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    childArr = (*env)->NewObjectArray(env, 0, ni_class, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    if (childArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
      return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /* return the NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    (JNIEnv *env, jclass cls, jstring name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    const char *name_utf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    jobject netifObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    if (getAllInterfacesAndAddresses (env, &ifList) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /* get the name as a C string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /* Search by name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (strcmp(name_utf, curr->name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    if (curr != NULL) {;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        netifObj = createNetworkInterfaceXP(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    /* release the UTF string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    (*env)->ReleaseStringUTFChars(env, name, name_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
 * Class:     NetworkInterface
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
   595
 * Method:    getByIndex0_XP
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
 * Signature: (I)LNetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
 */
1097
af4930f761df 6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents: 910
diff changeset
   598
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0_XP
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
  (JNIEnv *env, jclass cls, jint index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    jobject netifObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    if (getAllInterfacesAndAddresses (env, &ifList) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /* search by index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (index == curr->index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /* if found create a NetworkInterface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    if (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        netifObj = createNetworkInterfaceXP(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    return netifObj;
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
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
 * Method:    getByInetAddress0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    (JNIEnv *env, jclass cls, jobject iaObj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    jobject netifObj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /* get the list of interfaces */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    if (getAllInterfacesAndAddresses (env, &ifList) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Enumerate the addresses on each interface until we find a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * matching address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        netaddr *addrList = curr->addrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        netaddr *addrP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        /* iterate through each address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        addrP = addrList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        while (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            if (NET_SockaddrEqualsInetAddress(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                (struct sockaddr*)&addrP->addr, iaObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            addrP = addrP->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
         * Address matched so create NetworkInterface for this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
         * and address list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if (addrP != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            netifObj = createNetworkInterfaceXP(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        /* on next interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    return netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
 * Method:    getAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
 * Signature: ()[Ljava/net/NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    (JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    netif *ifList, *curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    jobjectArray netIFArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    jint arr_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * Get list of interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    count = getAllInterfacesAndAddresses (env, &ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    if (count < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    /* allocate a NetworkInterface array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    netIFArr = (*env)->NewObjectArray(env, count, cls, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    if (netIFArr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * Iterate through the interfaces, create a NetworkInterface instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * for each array element and populate the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    curr = ifList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    arr_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        jobject netifObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        netifObj = createNetworkInterfaceXP(env, curr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (netifObj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        /* put the NetworkInterface into the array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /* release the interface list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    free_netif(ifList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    return netIFArr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
 * Method:    supportsMulticast0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
      IP_ADAPTER_ADDRESSES *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
      jboolean val = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
      ptr = getAdapter(env, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
      if (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        val = ptr->Flags & IP_ADAPTER_NO_MULTICAST ? JNI_FALSE : JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
      return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
 * Method:    isUp0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
      IP_ADAPTER_ADDRESSES *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
      jboolean val = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
      ptr = getAdapter(env, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
      if (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        val = ptr->OperStatus == IfOperStatusUp ? JNI_TRUE : JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
      return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
 * Method:    getMacAddr0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
      IP_ADAPTER_ADDRESSES *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
      jbyteArray ret = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
      int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
      ptr = getAdapter(env, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
      if (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        len = ptr->PhysicalAddressLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        ret = (*env)->NewByteArray(env, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        if (!IS_NULL(ret)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
          (*env)->SetByteArrayRegion(env, ret, 0, len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                                     (jbyte*) ptr->PhysicalAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
      return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
 * Class:       java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
 * Method:      getMTU0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
 * Signature:   ([bLjava/lang/String;I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
      IP_ADAPTER_ADDRESSES *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
      jint ret = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
      ptr = getAdapter(env, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
      if (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        ret = ptr->Mtu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
      return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
 * Method:    isLoopback0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
      IP_ADAPTER_ADDRESSES *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
      jboolean val = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
      ptr = getAdapter(env, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
      if (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        val = ptr->IfType == IF_TYPE_SOFTWARE_LOOPBACK ? JNI_TRUE : JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
      return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
 * Class:     java_net_NetworkInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
 * Method:    isP2P0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
 * Signature: (Ljava/lang/String;I)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0_XP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    (JNIEnv *env, jclass cls, jstring name, jint index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
      IP_ADAPTER_ADDRESSES *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
      jboolean val = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
      ptr = getAdapter(env, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
      if (ptr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        if (ptr->IfType == IF_TYPE_PPP || ptr->IfType == IF_TYPE_SLIP ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
           ptr->IfType == IF_TYPE_TUNNEL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
          val = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        free(ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
      return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
}