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