author | michaelm |
Wed, 13 Feb 2013 10:40:31 +0000 | |
changeset 16870 | f35b2bd19761 |
parent 14768 | 8800c12ef525 |
child 17193 | 8b561b7fb87a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13245
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 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 |
/* various JNI ids */ |
|
57 |
||
58 |
jclass ni_class; /* NetworkInterface */ |
|
59 |
||
60 |
jmethodID ni_ctor; /* NetworkInterface() */ |
|
61 |
||
62 |
jfieldID ni_indexID; /* NetworkInterface.index */ |
|
63 |
jfieldID ni_addrsID; /* NetworkInterface.addrs */ |
|
64 |
jfieldID ni_bindsID; /* NetworkInterface.bindings */ |
|
65 |
jfieldID ni_nameID; /* NetworkInterface.name */ |
|
66 |
jfieldID ni_displayNameID; /* NetworkInterface.displayName */ |
|
67 |
jfieldID ni_childsID; /* NetworkInterface.childs */ |
|
68 |
jclass ni_iacls; /* InetAddress */ |
|
69 |
||
70 |
jclass ni_ia4cls; /* Inet4Address */ |
|
71 |
jmethodID ni_ia4Ctor; /* Inet4Address() */ |
|
72 |
||
73 |
jclass ni_ia6cls; /* Inet6Address */ |
|
74 |
jmethodID ni_ia6ctrID; /* Inet6Address() */ |
|
75 |
jfieldID ni_ia6ipaddressID; |
|
76 |
jfieldID ni_ia6ipaddressID; |
|
77 |
||
78 |
jclass ni_ibcls; /* InterfaceAddress */ |
|
79 |
jmethodID ni_ibctrID; /* InterfaceAddress() */ |
|
80 |
jfieldID ni_ibaddressID; /* InterfaceAddress.address */ |
|
81 |
jfieldID ni_ibbroadcastID; /* InterfaceAddress.broadcast */ |
|
82 |
jfieldID ni_ibmaskID; /* InterfaceAddress.maskLength */ |
|
83 |
||
84 |
/* |
|
85 |
* Support routines to free netif and netaddr lists |
|
86 |
*/ |
|
87 |
void free_netif(netif *netifP) { |
|
88 |
netif *curr = netifP; |
|
89 |
while (curr != NULL) { |
|
90 |
if (curr->name != NULL) |
|
91 |
free(curr->name); |
|
92 |
if (curr->displayName != NULL) |
|
93 |
free(curr->displayName); |
|
94 |
if (curr->addrs != NULL) |
|
95 |
free_netaddr (curr->addrs); |
|
96 |
netifP = netifP->next; |
|
97 |
free(curr); |
|
98 |
curr = netifP; |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
void free_netaddr(netaddr *netaddrP) { |
|
103 |
netaddr *curr = netaddrP; |
|
104 |
while (curr != NULL) { |
|
105 |
netaddrP = netaddrP->next; |
|
106 |
free(curr); |
|
107 |
curr = netaddrP; |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
/* |
|
112 |
* Returns the interface structure from the table with the matching index. |
|
113 |
*/ |
|
114 |
MIB_IFROW *getIF(jint index) { |
|
115 |
MIB_IFTABLE *tableP; |
|
116 |
MIB_IFROW *ifrowP, *ret = NULL; |
|
117 |
ULONG size; |
|
118 |
DWORD i, count; |
|
119 |
jint ifindex; |
|
120 |
||
121 |
/* |
|
122 |
* Ask the IP Helper library to enumerate the adapters |
|
123 |
*/ |
|
124 |
size = sizeof(MIB_IFTABLE); |
|
125 |
tableP = (MIB_IFTABLE *)malloc(size); |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
126 |
count = GetIfTable(tableP, &size, TRUE); |
2 | 127 |
if (count == ERROR_INSUFFICIENT_BUFFER || count == ERROR_BUFFER_OVERFLOW) { |
128 |
tableP = (MIB_IFTABLE *)realloc(tableP, size); |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
129 |
count = GetIfTable(tableP, &size, TRUE); |
2 | 130 |
} |
131 |
||
132 |
if (count != NO_ERROR) { |
|
133 |
if (tableP != NULL) |
|
134 |
free(tableP); |
|
135 |
return NULL; |
|
136 |
} |
|
137 |
||
138 |
if (tableP != NULL) { |
|
139 |
ifrowP = tableP->table; |
|
140 |
for (i=0; i<tableP->dwNumEntries; i++) { |
|
141 |
/* |
|
142 |
* Warning the real index is obtained by GetFriendlyIfIndex() |
|
143 |
*/ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
144 |
ifindex = GetFriendlyIfIndex(ifrowP->dwIndex); |
2 | 145 |
if (ifindex == index) { |
146 |
/* |
|
147 |
* Create a copy of the entry so that we can free the table. |
|
148 |
*/ |
|
149 |
ret = (MIB_IFROW *) malloc(sizeof(MIB_IFROW)); |
|
150 |
memcpy(ret, ifrowP, sizeof(MIB_IFROW)); |
|
151 |
break; |
|
152 |
} |
|
153 |
||
154 |
/* onto the next interface */ |
|
155 |
ifrowP++; |
|
156 |
} |
|
157 |
free(tableP); |
|
158 |
} |
|
159 |
return ret; |
|
160 |
} |
|
161 |
||
162 |
/* |
|
163 |
* Enumerate network interfaces using IP Helper Library routine GetIfTable. |
|
164 |
* We use GetIfTable rather than other IP helper routines because it's |
|
165 |
* available on 98 & NT SP4+. |
|
166 |
* |
|
167 |
* Returns the number of interfaces found or -1 if error. If no error |
|
168 |
* occurs then netifPP be returned as list of netif structures or NULL |
|
169 |
* if no interfaces are found. |
|
170 |
*/ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
171 |
int enumInterfaces(JNIEnv *env, netif **netifPP) |
2 | 172 |
{ |
173 |
MIB_IFTABLE *tableP; |
|
174 |
MIB_IFROW *ifrowP; |
|
175 |
ULONG size; |
|
176 |
DWORD ret; |
|
177 |
int count; |
|
178 |
netif *netifP; |
|
179 |
DWORD i; |
|
14768
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
180 |
int lo=0, eth=0, tr=0, fddi=0, ppp=0, sl=0, wlan=0, net=0, wlen=0; |
2 | 181 |
|
182 |
/* |
|
183 |
* Ask the IP Helper library to enumerate the adapters |
|
184 |
*/ |
|
185 |
size = sizeof(MIB_IFTABLE); |
|
186 |
tableP = (MIB_IFTABLE *)malloc(size); |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
187 |
ret = GetIfTable(tableP, &size, TRUE); |
2 | 188 |
if (ret == ERROR_INSUFFICIENT_BUFFER || ret == ERROR_BUFFER_OVERFLOW) { |
189 |
tableP = (MIB_IFTABLE *)realloc(tableP, size); |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
190 |
ret = GetIfTable(tableP, &size, TRUE); |
2 | 191 |
} |
192 |
||
193 |
if (ret != NO_ERROR) { |
|
194 |
if (tableP != NULL) |
|
195 |
free(tableP); |
|
196 |
||
197 |
JNU_ThrowByName(env, "java/lang/Error", |
|
198 |
"IP Helper Library GetIfTable function failed"); |
|
199 |
||
200 |
return -1; |
|
201 |
} |
|
202 |
||
203 |
/* |
|
204 |
* Iterate through the list of adapters |
|
205 |
*/ |
|
206 |
count = 0; |
|
207 |
netifP = NULL; |
|
208 |
||
209 |
ifrowP = tableP->table; |
|
210 |
for (i=0; i<tableP->dwNumEntries; i++) { |
|
211 |
char dev_name[8]; |
|
212 |
netif *curr; |
|
213 |
||
214 |
/* |
|
215 |
* Generate a name for the device as Windows doesn't have any |
|
216 |
* real concept of a device name. |
|
217 |
*/ |
|
218 |
switch (ifrowP->dwType) { |
|
219 |
case MIB_IF_TYPE_ETHERNET: |
|
12542
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
220 |
_snprintf_s(dev_name, 8, _TRUNCATE, "eth%d", eth++); |
2 | 221 |
break; |
222 |
||
223 |
case MIB_IF_TYPE_TOKENRING: |
|
12542
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
224 |
_snprintf_s(dev_name, 8, _TRUNCATE, "tr%d", tr++); |
2 | 225 |
break; |
226 |
||
227 |
case MIB_IF_TYPE_FDDI: |
|
12542
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
228 |
_snprintf_s(dev_name, 8, _TRUNCATE, "fddi%d", fddi++); |
2 | 229 |
break; |
230 |
||
231 |
case MIB_IF_TYPE_LOOPBACK: |
|
232 |
/* There should only be only IPv4 loopback address */ |
|
233 |
if (lo > 0) { |
|
234 |
continue; |
|
235 |
} |
|
12542
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
236 |
strncpy_s(dev_name, 8, "lo", _TRUNCATE); |
2 | 237 |
lo++; |
238 |
break; |
|
239 |
||
240 |
case MIB_IF_TYPE_PPP: |
|
12542
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
241 |
_snprintf_s(dev_name, 8, _TRUNCATE, "ppp%d", ppp++); |
2 | 242 |
break; |
243 |
||
244 |
case MIB_IF_TYPE_SLIP: |
|
12542
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
245 |
_snprintf_s(dev_name, 8, _TRUNCATE, "sl%d", sl++); |
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
246 |
break; |
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
247 |
|
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
248 |
case IF_TYPE_IEEE80211: |
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
249 |
_snprintf_s(dev_name, 8, _TRUNCATE, "wlan%d", wlan++); |
2 | 250 |
break; |
251 |
||
252 |
default: |
|
12542
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
253 |
_snprintf_s(dev_name, 8, _TRUNCATE, "net%d", net++); |
2 | 254 |
} |
255 |
||
256 |
/* |
|
257 |
* Allocate a netif structure and space for the name and |
|
258 |
* display name (description in this case). |
|
259 |
*/ |
|
260 |
curr = (netif *)calloc(1, sizeof(netif)); |
|
261 |
if (curr != NULL) { |
|
14768
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
262 |
wlen = MultiByteToWideChar(CP_OEMCP, 0, ifrowP->bDescr, |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
263 |
ifrowP->dwDescrLen, NULL, 0); |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
264 |
if(wlen == 0) { |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
265 |
// MultiByteToWideChar should not fail |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
266 |
// But in rare case it fails, we allow 'char' to be displayed |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
267 |
curr->displayName = (char *)malloc(ifrowP->dwDescrLen + 1); |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
268 |
} else { |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
269 |
curr->displayName = (wchar_t *)malloc(wlen*(sizeof(wchar_t))+1); |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
270 |
} |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
271 |
|
2 | 272 |
curr->name = (char *)malloc(strlen(dev_name) + 1); |
273 |
||
274 |
if (curr->name == NULL || curr->displayName == NULL) { |
|
275 |
if (curr->name) free(curr->name); |
|
276 |
if (curr->displayName) free(curr->displayName); |
|
277 |
curr = NULL; |
|
278 |
} |
|
279 |
} |
|
280 |
if (curr == NULL) { |
|
13245
7ab3ef5b9520
7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents:
12542
diff
changeset
|
281 |
JNU_ThrowOutOfMemoryError(env, "Native heap allocation failure"); |
2 | 282 |
free_netif(netifP); |
283 |
free(tableP); |
|
284 |
return -1; |
|
285 |
} |
|
286 |
||
287 |
/* |
|
288 |
* Populate the interface. Note that we need to convert the |
|
289 |
* index into its "friendly" value as otherwise we will expose |
|
290 |
* 32-bit numbers as index values. |
|
291 |
*/ |
|
292 |
strcpy(curr->name, dev_name); |
|
14768
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
293 |
if (wlen == 0) { |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
294 |
// display char type in case of MultiByteToWideChar failure |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
295 |
strncpy(curr->displayName, ifrowP->bDescr, ifrowP->dwDescrLen); |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
296 |
curr->displayName[ifrowP->dwDescrLen] = '\0'; |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
297 |
} else { |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
298 |
// call MultiByteToWideChar again to fill curr->displayName |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
299 |
// it should not fail, because we have called it once before |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
300 |
if (MultiByteToWideChar(CP_OEMCP, 0, ifrowP->bDescr, |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
301 |
ifrowP->dwDescrLen, curr->displayName, wlen) == 0) { |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
302 |
JNU_ThrowByName(env, "java/lang/Error", |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
303 |
"Cannot get multibyte char for interface display name"); |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
304 |
free_netif(netifP); |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
305 |
free(tableP); |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
306 |
free(curr->name); |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
307 |
free(curr->displayName); |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
308 |
free(curr); |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
309 |
return -1; |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
310 |
} else { |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
311 |
curr->displayName[wlen*(sizeof(wchar_t))] = '\0'; |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
312 |
curr->dNameIsUnicode = TRUE; |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
313 |
} |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
314 |
} |
8800c12ef525
6512101: Incorrect encoding in NetworkInterface.getDisplayName()
dingxmin
parents:
14342
diff
changeset
|
315 |
|
2 | 316 |
curr->dwIndex = ifrowP->dwIndex; |
317 |
curr->ifType = ifrowP->dwType; |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
318 |
curr->index = GetFriendlyIfIndex(ifrowP->dwIndex); |
2 | 319 |
|
320 |
/* |
|
321 |
* Put the interface at tail of list as GetIfTable(,,TRUE) is |
|
322 |
* returning the interfaces in index order. |
|
323 |
*/ |
|
324 |
count++; |
|
325 |
if (netifP == NULL) { |
|
326 |
netifP = curr; |
|
327 |
} else { |
|
328 |
netif *tail = netifP; |
|
329 |
while (tail->next != NULL) { |
|
330 |
tail = tail->next; |
|
331 |
} |
|
332 |
tail->next = curr; |
|
333 |
} |
|
334 |
||
335 |
/* onto the next interface */ |
|
336 |
ifrowP++; |
|
337 |
} |
|
338 |
||
339 |
/* |
|
340 |
* Free the interface table and return the interface list |
|
341 |
*/ |
|
342 |
if (tableP) { |
|
343 |
free(tableP); |
|
344 |
} |
|
345 |
*netifPP = netifP; |
|
346 |
return count; |
|
347 |
} |
|
348 |
||
349 |
/* |
|
350 |
* Enumerate the IP addresses on an interface using the IP helper library |
|
351 |
* routine GetIfAddrTable and matching based on the index name. There are |
|
352 |
* more efficient routines but we use GetIfAddrTable because it's avaliable |
|
353 |
* on 98 and NT. |
|
354 |
* |
|
355 |
* Returns the count of addresses, or -1 if error. If no error occurs then |
|
356 |
* netaddrPP will return a list of netaddr structures with the IP addresses. |
|
357 |
*/ |
|
358 |
int enumAddresses_win(JNIEnv *env, netif *netifP, netaddr **netaddrPP) |
|
359 |
{ |
|
360 |
MIB_IPADDRTABLE *tableP; |
|
361 |
ULONG size; |
|
362 |
DWORD ret; |
|
363 |
DWORD i; |
|
364 |
netaddr *netaddrP; |
|
365 |
int count = 0; |
|
366 |
unsigned long mask; |
|
367 |
||
368 |
/* |
|
369 |
* Use GetIpAddrTable to enumerate the IP Addresses |
|
370 |
*/ |
|
371 |
size = sizeof(MIB_IPADDRTABLE); |
|
372 |
tableP = (MIB_IPADDRTABLE *)malloc(size); |
|
373 |
||
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
374 |
ret = GetIpAddrTable(tableP, &size, FALSE); |
2 | 375 |
if (ret == ERROR_INSUFFICIENT_BUFFER || ret == ERROR_BUFFER_OVERFLOW) { |
376 |
tableP = (MIB_IPADDRTABLE *)realloc(tableP, size); |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
377 |
ret = GetIpAddrTable(tableP, &size, FALSE); |
2 | 378 |
} |
379 |
if (ret != NO_ERROR) { |
|
380 |
if (tableP) { |
|
381 |
free(tableP); |
|
382 |
} |
|
383 |
JNU_ThrowByName(env, "java/lang/Error", |
|
384 |
"IP Helper Library GetIpAddrTable function failed"); |
|
385 |
return -1; |
|
386 |
} |
|
387 |
||
388 |
/* |
|
389 |
* Iterate through the table to find the addresses with the |
|
390 |
* matching dwIndex. Ignore 0.0.0.0 addresses. |
|
391 |
*/ |
|
392 |
count = 0; |
|
393 |
netaddrP = NULL; |
|
394 |
||
395 |
i = 0; |
|
396 |
while (i<tableP->dwNumEntries) { |
|
397 |
if (tableP->table[i].dwIndex == netifP->dwIndex && |
|
398 |
tableP->table[i].dwAddr != 0) { |
|
399 |
||
400 |
netaddr *curr = (netaddr *)malloc(sizeof(netaddr)); |
|
401 |
if (curr == NULL) { |
|
13245
7ab3ef5b9520
7181353: Update error message to distinguish native OOM and java OOM in net
zhouyx
parents:
12542
diff
changeset
|
402 |
JNU_ThrowOutOfMemoryError(env, "Native heap allocation failure"); |
2 | 403 |
free_netaddr(netaddrP); |
404 |
free(tableP); |
|
405 |
return -1; |
|
406 |
} |
|
407 |
||
408 |
curr->addr.him4.sin_family = AF_INET; |
|
409 |
curr->addr.him4.sin_addr.s_addr = tableP->table[i].dwAddr; |
|
410 |
/* |
|
411 |
* Get netmask / broadcast address |
|
412 |
*/ |
|
413 |
switch (netifP->ifType) { |
|
414 |
case MIB_IF_TYPE_ETHERNET: |
|
415 |
case MIB_IF_TYPE_TOKENRING: |
|
416 |
case MIB_IF_TYPE_FDDI: |
|
417 |
case MIB_IF_TYPE_LOOPBACK: |
|
12542
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
418 |
case IF_TYPE_IEEE80211: |
2 | 419 |
/** |
420 |
* Contrary to what it seems to indicate, dwBCastAddr doesn't |
|
421 |
* contain the broadcast address but 0 or 1 depending on whether |
|
422 |
* the broadcast address should set the bits of the host part |
|
423 |
* to 0 or 1. |
|
424 |
* Yes, I know it's stupid, but what can I say, it's MSFTs API. |
|
425 |
*/ |
|
426 |
curr->brdcast.him4.sin_family = AF_INET; |
|
427 |
if (tableP->table[i].dwBCastAddr == 1) |
|
428 |
curr->brdcast.him4.sin_addr.s_addr = (tableP->table[i].dwAddr & tableP->table[i].dwMask) | (0xffffffff ^ tableP->table[i].dwMask); |
|
429 |
else |
|
430 |
curr->brdcast.him4.sin_addr.s_addr = (tableP->table[i].dwAddr & tableP->table[i].dwMask); |
|
431 |
mask = ntohl(tableP->table[i].dwMask); |
|
432 |
curr->mask = 0; |
|
433 |
while (mask) { |
|
434 |
mask <<= 1; |
|
435 |
curr->mask++; |
|
436 |
} |
|
437 |
break; |
|
438 |
case MIB_IF_TYPE_PPP: |
|
439 |
case MIB_IF_TYPE_SLIP: |
|
440 |
default: |
|
441 |
/** |
|
442 |
* these don't have broadcast/subnet |
|
443 |
*/ |
|
444 |
curr->mask = -1; |
|
445 |
break; |
|
446 |
} |
|
447 |
||
448 |
curr->next = netaddrP; |
|
449 |
netaddrP = curr; |
|
450 |
count++; |
|
451 |
} |
|
452 |
i++; |
|
453 |
} |
|
454 |
||
455 |
*netaddrPP = netaddrP; |
|
456 |
free(tableP); |
|
457 |
return count; |
|
458 |
} |
|
459 |
||
460 |
/* |
|
461 |
* Class: java_net_NetworkInterface |
|
462 |
* Method: init |
|
463 |
* Signature: ()V |
|
464 |
*/ |
|
465 |
JNIEXPORT void JNICALL |
|
466 |
Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls) |
|
467 |
{ |
|
468 |
/* |
|
469 |
* Get the various JNI ids that we require |
|
470 |
*/ |
|
471 |
ni_class = (*env)->NewGlobalRef(env, cls); |
|
472 |
ni_nameID = (*env)->GetFieldID(env, ni_class, "name", "Ljava/lang/String;"); |
|
473 |
ni_displayNameID = (*env)->GetFieldID(env, ni_class, "displayName", "Ljava/lang/String;"); |
|
474 |
ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I"); |
|
475 |
ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs", "[Ljava/net/InetAddress;"); |
|
476 |
ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings", "[Ljava/net/InterfaceAddress;"); |
|
477 |
ni_childsID = (*env)->GetFieldID(env, ni_class, "childs", "[Ljava/net/NetworkInterface;"); |
|
478 |
ni_ctor = (*env)->GetMethodID(env, ni_class, "<init>", "()V"); |
|
479 |
||
1094
0cb4746beb0c
6734171: java.net.NetworkInterface reports XCheck:jni warnings
chegar
parents:
910
diff
changeset
|
480 |
ni_iacls = (*env)->FindClass(env, "java/net/InetAddress"); |
2 | 481 |
ni_iacls = (*env)->NewGlobalRef(env, ni_iacls); |
482 |
||
1094
0cb4746beb0c
6734171: java.net.NetworkInterface reports XCheck:jni warnings
chegar
parents:
910
diff
changeset
|
483 |
ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address"); |
2 | 484 |
ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls); |
485 |
ni_ia4Ctor = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V"); |
|
486 |
||
487 |
ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address"); |
|
488 |
ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls); |
|
489 |
ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V"); |
|
490 |
ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B"); |
|
491 |
||
492 |
ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress"); |
|
493 |
ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls); |
|
494 |
ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V"); |
|
495 |
ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;"); |
|
496 |
ni_ibbroadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;"); |
|
497 |
ni_ibmaskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S"); |
|
498 |
||
499 |
} |
|
500 |
||
501 |
/* |
|
502 |
* Create a NetworkInterface object, populate the name and index, and |
|
503 |
* populate the InetAddress array based on the IP addresses for this |
|
504 |
* interface. |
|
505 |
*/ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
506 |
jobject createNetworkInterface |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
507 |
(JNIEnv *env, netif *ifs, int netaddrCount, netaddr *netaddrP) |
2 | 508 |
{ |
509 |
jobject netifObj; |
|
510 |
jobject name, displayName; |
|
511 |
jobjectArray addrArr, bindsArr, childArr; |
|
512 |
netaddr *addrs; |
|
513 |
jint addr_index; |
|
514 |
jint bind_index; |
|
515 |
||
516 |
/* |
|
517 |
* Create a NetworkInterface object and populate it |
|
518 |
*/ |
|
519 |
netifObj = (*env)->NewObject(env, ni_class, ni_ctor); |
|
520 |
name = (*env)->NewStringUTF(env, ifs->name); |
|
521 |
if (ifs->dNameIsUnicode) { |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
522 |
displayName = (*env)->NewString(env, (PWCHAR)ifs->displayName, |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
523 |
(jsize)wcslen ((PWCHAR)ifs->displayName)); |
2 | 524 |
} else { |
525 |
displayName = (*env)->NewStringUTF(env, ifs->displayName); |
|
526 |
} |
|
527 |
if (netifObj == NULL || name == NULL || displayName == NULL) { |
|
528 |
return NULL; |
|
529 |
} |
|
530 |
(*env)->SetObjectField(env, netifObj, ni_nameID, name); |
|
531 |
(*env)->SetObjectField(env, netifObj, ni_displayNameID, displayName); |
|
532 |
(*env)->SetIntField(env, netifObj, ni_indexID, ifs->index); |
|
533 |
||
534 |
/* |
|
535 |
* Get the IP addresses for this interface if necessary |
|
536 |
* Note that 0 is a valid number of addresses. |
|
537 |
*/ |
|
538 |
if (netaddrCount < 0) { |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
539 |
netaddrCount = enumAddresses_win(env, ifs, &netaddrP); |
10417 | 540 |
if (netaddrCount == -1) { |
2 | 541 |
return NULL; |
542 |
} |
|
543 |
} |
|
544 |
addrArr = (*env)->NewObjectArray(env, netaddrCount, ni_iacls, NULL); |
|
545 |
if (addrArr == NULL) { |
|
546 |
free_netaddr(netaddrP); |
|
547 |
return NULL; |
|
548 |
} |
|
549 |
||
550 |
bindsArr = (*env)->NewObjectArray(env, netaddrCount, ni_ibcls, NULL); |
|
551 |
if (bindsArr == NULL) { |
|
552 |
free_netaddr(netaddrP); |
|
553 |
return NULL; |
|
554 |
} |
|
555 |
addrs = netaddrP; |
|
556 |
addr_index = 0; |
|
557 |
bind_index = 0; |
|
558 |
while (addrs != NULL) { |
|
559 |
jobject iaObj, ia2Obj; |
|
560 |
jobject ibObj = NULL; |
|
561 |
if (addrs->addr.him.sa_family == AF_INET) { |
|
562 |
iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor); |
|
563 |
if (iaObj == NULL) { |
|
564 |
free_netaddr(netaddrP); |
|
565 |
return NULL; |
|
566 |
} |
|
567 |
/* default ctor will set family to AF_INET */ |
|
568 |
||
16870 | 569 |
setInetAddress_addr(env, iaObj, ntohl(addrs->addr.him4.sin_addr.s_addr)); |
2 | 570 |
if (addrs->mask != -1) { |
571 |
ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID); |
|
572 |
if (ibObj == NULL) { |
|
573 |
free_netaddr(netaddrP); |
|
574 |
return NULL; |
|
575 |
} |
|
576 |
(*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj); |
|
577 |
ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor); |
|
578 |
if (ia2Obj == NULL) { |
|
579 |
free_netaddr(netaddrP); |
|
580 |
return NULL; |
|
581 |
} |
|
16870 | 582 |
setInetAddress_addr(env, ia2Obj, ntohl(addrs->brdcast.him4.sin_addr.s_addr)); |
2 | 583 |
(*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj); |
584 |
(*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask); |
|
585 |
(*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj); |
|
586 |
} |
|
587 |
} else /* AF_INET6 */ { |
|
588 |
int scope; |
|
589 |
iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID); |
|
590 |
if (iaObj) { |
|
591 |
jbyteArray ipaddress = (*env)->NewByteArray(env, 16); |
|
592 |
if (ipaddress == NULL) { |
|
593 |
return NULL; |
|
594 |
} |
|
595 |
(*env)->SetByteArrayRegion(env, ipaddress, 0, 16, |
|
596 |
(jbyte *)&(addrs->addr.him6.sin6_addr.s6_addr)); |
|
597 |
scope = addrs->addr.him6.sin6_scope_id; |
|
598 |
if (scope != 0) { /* zero is default value, no need to set */ |
|
599 |
(*env)->SetIntField(env, iaObj, ia6_scopeidID, scope); |
|
600 |
(*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE); |
|
601 |
(*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj); |
|
602 |
} |
|
603 |
(*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress); |
|
604 |
ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID); |
|
605 |
if (ibObj == NULL) { |
|
606 |
free_netaddr(netaddrP); |
|
607 |
return NULL; |
|
608 |
} |
|
609 |
(*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj); |
|
610 |
(*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask); |
|
611 |
(*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj); |
|
612 |
} |
|
613 |
} |
|
614 |
(*env)->SetObjectArrayElement(env, addrArr, addr_index, iaObj); |
|
615 |
addrs = addrs->next; |
|
616 |
addr_index++; |
|
617 |
} |
|
618 |
(*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr); |
|
619 |
(*env)->SetObjectField(env, netifObj, ni_bindsID, bindsArr); |
|
620 |
||
621 |
free_netaddr(netaddrP); |
|
622 |
||
623 |
/* |
|
624 |
* Windows doesn't have virtual interfaces, so child array |
|
625 |
* is always empty. |
|
626 |
*/ |
|
627 |
childArr = (*env)->NewObjectArray(env, 0, ni_class, NULL); |
|
628 |
if (childArr == NULL) { |
|
629 |
return NULL; |
|
630 |
} |
|
631 |
(*env)->SetObjectField(env, netifObj, ni_childsID, childArr); |
|
632 |
||
633 |
/* return the NetworkInterface */ |
|
634 |
return netifObj; |
|
635 |
} |
|
636 |
||
637 |
/* |
|
638 |
* Class: java_net_NetworkInterface |
|
639 |
* Method: getByName0 |
|
640 |
* Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface; |
|
641 |
*/ |
|
642 |
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0 |
|
643 |
(JNIEnv *env, jclass cls, jstring name) |
|
644 |
{ |
|
645 |
netif *ifList, *curr; |
|
646 |
jboolean isCopy; |
|
647 |
const char *name_utf; |
|
648 |
jobject netifObj = NULL; |
|
649 |
||
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
650 |
// Retained for now to support IPv4 only stack, java.net.preferIPv4Stack |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
651 |
if (ipv6_available()) { |
2 | 652 |
return Java_java_net_NetworkInterface_getByName0_XP (env, cls, name); |
653 |
} |
|
654 |
||
655 |
/* get the list of interfaces */ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
656 |
if (enumInterfaces(env, &ifList) < 0) { |
2 | 657 |
return NULL; |
658 |
} |
|
659 |
||
660 |
/* get the name as a C string */ |
|
661 |
name_utf = (*env)->GetStringUTFChars(env, name, &isCopy); |
|
662 |
||
663 |
/* Search by name */ |
|
664 |
curr = ifList; |
|
665 |
while (curr != NULL) { |
|
666 |
if (strcmp(name_utf, curr->name) == 0) { |
|
667 |
break; |
|
668 |
} |
|
669 |
curr = curr->next; |
|
670 |
} |
|
671 |
||
672 |
/* if found create a NetworkInterface */ |
|
673 |
if (curr != NULL) {; |
|
674 |
netifObj = createNetworkInterface(env, curr, -1, NULL); |
|
675 |
} |
|
676 |
||
677 |
/* release the UTF string */ |
|
678 |
(*env)->ReleaseStringUTFChars(env, name, name_utf); |
|
679 |
||
680 |
/* release the interface list */ |
|
681 |
free_netif(ifList); |
|
682 |
||
683 |
return netifObj; |
|
684 |
} |
|
685 |
||
686 |
/* |
|
687 |
* Class: NetworkInterface |
|
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
1094
diff
changeset
|
688 |
* Method: getByIndex0 |
2 | 689 |
* Signature: (I)LNetworkInterface; |
690 |
*/ |
|
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
1094
diff
changeset
|
691 |
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0 |
2 | 692 |
(JNIEnv *env, jclass cls, jint index) |
693 |
{ |
|
694 |
netif *ifList, *curr; |
|
695 |
jobject netifObj = NULL; |
|
696 |
||
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
697 |
// Retained for now to support IPv4 only stack, java.net.preferIPv4Stack |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
698 |
if (ipv6_available()) { |
1097
af4930f761df
6717876: Make java.net.NetworkInterface.getIndex() public
jccollet
parents:
1094
diff
changeset
|
699 |
return Java_java_net_NetworkInterface_getByIndex0_XP (env, cls, index); |
2 | 700 |
} |
701 |
||
702 |
/* get the list of interfaces */ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
703 |
if (enumInterfaces(env, &ifList) < 0) { |
2 | 704 |
return NULL; |
705 |
} |
|
706 |
||
707 |
/* search by index */ |
|
708 |
curr = ifList; |
|
709 |
while (curr != NULL) { |
|
710 |
if (index == curr->index) { |
|
711 |
break; |
|
712 |
} |
|
713 |
curr = curr->next; |
|
714 |
} |
|
715 |
||
716 |
/* if found create a NetworkInterface */ |
|
717 |
if (curr != NULL) { |
|
718 |
netifObj = createNetworkInterface(env, curr, -1, NULL); |
|
719 |
} |
|
720 |
||
721 |
/* release the interface list */ |
|
722 |
free_netif(ifList); |
|
723 |
||
724 |
return netifObj; |
|
725 |
} |
|
726 |
||
727 |
/* |
|
728 |
* Class: java_net_NetworkInterface |
|
729 |
* Method: getByInetAddress0 |
|
730 |
* Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface; |
|
731 |
*/ |
|
732 |
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0 |
|
733 |
(JNIEnv *env, jclass cls, jobject iaObj) |
|
734 |
{ |
|
735 |
netif *ifList, *curr; |
|
16870 | 736 |
jint addr = getInetAddress_addr(env, iaObj); |
2 | 737 |
jobject netifObj = NULL; |
738 |
||
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
739 |
// Retained for now to support IPv4 only stack, java.net.preferIPv4Stack |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
740 |
if (ipv6_available()) { |
2 | 741 |
return Java_java_net_NetworkInterface_getByInetAddress0_XP (env, cls, iaObj); |
742 |
} |
|
743 |
||
744 |
/* get the list of interfaces */ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
745 |
if (enumInterfaces(env, &ifList) < 0) { |
2 | 746 |
return NULL; |
747 |
} |
|
748 |
||
749 |
/* |
|
750 |
* Enumerate the addresses on each interface until we find a |
|
751 |
* matching address. |
|
752 |
*/ |
|
753 |
curr = ifList; |
|
754 |
while (curr != NULL) { |
|
755 |
int count; |
|
756 |
netaddr *addrList; |
|
757 |
netaddr *addrP; |
|
758 |
||
759 |
/* enumerate the addresses on this interface */ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
760 |
count = enumAddresses_win(env, curr, &addrList); |
2 | 761 |
if (count < 0) { |
762 |
free_netif(ifList); |
|
763 |
return NULL; |
|
764 |
} |
|
765 |
||
766 |
/* iterate through each address */ |
|
767 |
addrP = addrList; |
|
768 |
||
769 |
while (addrP != NULL) { |
|
770 |
if ((unsigned long)addr == ntohl(addrP->addr.him4.sin_addr.s_addr)) { |
|
771 |
break; |
|
772 |
} |
|
773 |
addrP = addrP->next; |
|
774 |
} |
|
775 |
||
776 |
/* |
|
777 |
* Address matched so create NetworkInterface for this interface |
|
778 |
* and address list. |
|
779 |
*/ |
|
780 |
if (addrP != NULL) { |
|
781 |
/* createNetworkInterface will free addrList */ |
|
782 |
netifObj = createNetworkInterface(env, curr, count, addrList); |
|
783 |
break; |
|
784 |
} |
|
785 |
||
786 |
/* on next interface */ |
|
787 |
curr = curr->next; |
|
788 |
} |
|
789 |
||
790 |
/* release the interface list */ |
|
791 |
free_netif(ifList); |
|
792 |
||
793 |
return netifObj; |
|
794 |
} |
|
795 |
||
796 |
/* |
|
797 |
* Class: java_net_NetworkInterface |
|
798 |
* Method: getAll |
|
799 |
* Signature: ()[Ljava/net/NetworkInterface; |
|
800 |
*/ |
|
801 |
JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll |
|
802 |
(JNIEnv *env, jclass cls) |
|
803 |
{ |
|
804 |
int count; |
|
805 |
netif *ifList, *curr; |
|
806 |
jobjectArray netIFArr; |
|
807 |
jint arr_index; |
|
808 |
||
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
809 |
// Retained for now to support IPv4 only stack, java.net.preferIPv4Stack |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
810 |
if (ipv6_available()) { |
2 | 811 |
return Java_java_net_NetworkInterface_getAll_XP (env, cls); |
812 |
} |
|
813 |
||
814 |
/* |
|
815 |
* Get list of interfaces |
|
816 |
*/ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
817 |
count = enumInterfaces(env, &ifList); |
2 | 818 |
if (count < 0) { |
819 |
return NULL; |
|
820 |
} |
|
821 |
||
822 |
/* allocate a NetworkInterface array */ |
|
823 |
netIFArr = (*env)->NewObjectArray(env, count, cls, NULL); |
|
824 |
if (netIFArr == NULL) { |
|
825 |
return NULL; |
|
826 |
} |
|
827 |
||
828 |
/* |
|
829 |
* Iterate through the interfaces, create a NetworkInterface instance |
|
830 |
* for each array element and populate the object. |
|
831 |
*/ |
|
832 |
curr = ifList; |
|
833 |
arr_index = 0; |
|
834 |
while (curr != NULL) { |
|
835 |
jobject netifObj; |
|
836 |
||
837 |
netifObj = createNetworkInterface(env, curr, -1, NULL); |
|
838 |
if (netifObj == NULL) { |
|
839 |
return NULL; |
|
840 |
} |
|
841 |
||
842 |
/* put the NetworkInterface into the array */ |
|
843 |
(*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj); |
|
844 |
||
845 |
curr = curr->next; |
|
846 |
} |
|
847 |
||
848 |
/* release the interface list */ |
|
849 |
free_netif(ifList); |
|
850 |
||
851 |
return netIFArr; |
|
852 |
} |
|
853 |
||
854 |
/* |
|
855 |
* Class: java_net_NetworkInterface |
|
856 |
* Method: isUp0 |
|
857 |
* Signature: (Ljava/lang/String;)Z |
|
858 |
*/ |
|
859 |
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0 |
|
860 |
(JNIEnv *env, jclass cls, jstring name, jint index) { |
|
861 |
jboolean ret = JNI_FALSE; |
|
862 |
||
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
863 |
// Retained for now to support IPv4 only stack, java.net.preferIPv4Stack |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
864 |
if (ipv6_available()) { |
2 | 865 |
return Java_java_net_NetworkInterface_isUp0_XP(env, cls, name, index); |
866 |
} else { |
|
867 |
MIB_IFROW *ifRowP; |
|
868 |
ifRowP = getIF(index); |
|
869 |
if (ifRowP != NULL) { |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
870 |
ret = ifRowP->dwAdminStatus == 1 && |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
871 |
(ifRowP->dwOperStatus == MIB_IF_OPER_STATUS_OPERATIONAL || |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
872 |
ifRowP->dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED); |
2 | 873 |
free(ifRowP); |
874 |
} |
|
875 |
} |
|
876 |
return ret; |
|
877 |
} |
|
878 |
||
879 |
/* |
|
880 |
* Class: java_net_NetworkInterface |
|
881 |
* Method: isP2P0 |
|
882 |
* Signature: (Ljava/lang/String;I)Z |
|
883 |
*/ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
884 |
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0 |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
885 |
(JNIEnv *env, jclass cls, jstring name, jint index) { |
2 | 886 |
MIB_IFROW *ifRowP; |
887 |
jboolean ret = JNI_FALSE; |
|
888 |
||
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
889 |
// Retained for now to support IPv4 only stack, java.net.preferIPv4Stack |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
890 |
if (ipv6_available()) { |
2 | 891 |
return Java_java_net_NetworkInterface_isP2P0_XP(env, cls, name, index); |
892 |
} else { |
|
893 |
ifRowP = getIF(index); |
|
894 |
if (ifRowP != NULL) { |
|
895 |
switch(ifRowP->dwType) { |
|
896 |
case MIB_IF_TYPE_PPP: |
|
897 |
case MIB_IF_TYPE_SLIP: |
|
898 |
ret = JNI_TRUE; |
|
899 |
break; |
|
900 |
} |
|
901 |
free(ifRowP); |
|
902 |
} |
|
903 |
} |
|
904 |
return ret; |
|
905 |
} |
|
906 |
||
907 |
/* |
|
908 |
* Class: java_net_NetworkInterface |
|
909 |
* Method: isLoopback0 |
|
910 |
* Signature: (Ljava/lang/String;I)Z |
|
911 |
*/ |
|
912 |
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0 |
|
913 |
(JNIEnv *env, jclass cls, jstring name, jint index) { |
|
914 |
MIB_IFROW *ifRowP; |
|
915 |
jboolean ret = JNI_FALSE; |
|
916 |
||
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
917 |
// Retained for now to support IPv4 only stack, java.net.preferIPv4Stack |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
918 |
if (ipv6_available()) { |
2 | 919 |
return Java_java_net_NetworkInterface_isLoopback0_XP(env, cls, name, index); |
920 |
} else { |
|
921 |
ifRowP = getIF(index); |
|
922 |
if (ifRowP != NULL) { |
|
923 |
if (ifRowP->dwType == MIB_IF_TYPE_LOOPBACK) |
|
924 |
ret = JNI_TRUE; |
|
925 |
free(ifRowP); |
|
926 |
} |
|
927 |
return ret; |
|
928 |
} |
|
929 |
} |
|
930 |
||
931 |
/* |
|
932 |
* Class: java_net_NetworkInterface |
|
933 |
* Method: supportsMulticast0 |
|
934 |
* Signature: (Ljava/lang/String;I)Z |
|
935 |
*/ |
|
936 |
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0 |
|
937 |
(JNIEnv *env, jclass cls, jstring name, jint index) { |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
938 |
return Java_java_net_NetworkInterface_supportsMulticast0_XP(env, cls, |
2 | 939 |
name, index); |
940 |
} |
|
941 |
||
942 |
/* |
|
943 |
* Class: java_net_NetworkInterface |
|
944 |
* Method: getMacAddr0 |
|
945 |
* Signature: ([bLjava/lang/String;I)[b |
|
946 |
*/ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
947 |
JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0 |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
948 |
(JNIEnv *env, jclass class, jbyteArray addrArray, jstring name, jint index) { |
2 | 949 |
jbyteArray ret = NULL; |
950 |
int len; |
|
951 |
MIB_IFROW *ifRowP; |
|
952 |
||
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
953 |
// Retained for now to support IPv4 only stack, java.net.preferIPv4Stack |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
954 |
if (ipv6_available()) { |
2 | 955 |
return Java_java_net_NetworkInterface_getMacAddr0_XP(env, class, name, index); |
956 |
} else { |
|
957 |
ifRowP = getIF(index); |
|
958 |
if (ifRowP != NULL) { |
|
959 |
switch(ifRowP->dwType) { |
|
960 |
case MIB_IF_TYPE_ETHERNET: |
|
961 |
case MIB_IF_TYPE_TOKENRING: |
|
962 |
case MIB_IF_TYPE_FDDI: |
|
12542
31190502e9e3
7158636: InterfaceAddress.getBroadcast() returns invalid broadcast address on WLAN
khazra
parents:
10417
diff
changeset
|
963 |
case IF_TYPE_IEEE80211: |
2 | 964 |
len = ifRowP->dwPhysAddrLen; |
965 |
ret = (*env)->NewByteArray(env, len); |
|
966 |
if (!IS_NULL(ret)) { |
|
967 |
(*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *) ifRowP->bPhysAddr); |
|
968 |
} |
|
969 |
break; |
|
970 |
} |
|
971 |
free(ifRowP); |
|
972 |
} |
|
973 |
return ret; |
|
974 |
} |
|
975 |
} |
|
976 |
||
977 |
/* |
|
978 |
* Class: java_net_NetworkInterface |
|
979 |
* Method: getMTU0 |
|
980 |
* Signature: ([bLjava/lang/String;I)I |
|
981 |
*/ |
|
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
982 |
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0 |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
983 |
(JNIEnv *env, jclass class, jstring name, jint index) { |
2 | 984 |
jint ret = -1; |
985 |
MIB_IFROW *ifRowP; |
|
986 |
||
9003
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
987 |
// Retained for now to support IPv4 only stack, java.net.preferIPv4Stack |
e1b0f0099915
7030256: Cleanup/Remove code supporting old Windows versions (98, NT, 2000, etc)
chegar
parents:
5506
diff
changeset
|
988 |
if (ipv6_available()) { |
2 | 989 |
return Java_java_net_NetworkInterface_getMTU0_XP(env, class, name, index); |
990 |
} else { |
|
991 |
ifRowP = getIF(index); |
|
992 |
if (ifRowP != NULL) { |
|
993 |
ret = ifRowP->dwMtu; |
|
994 |
free(ifRowP); |
|
995 |
} |
|
996 |
return ret; |
|
997 |
} |
|
998 |
} |