blob: f60e03fa24b013d0587705cc35b70889d0aa7589 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2006 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 <errno.h>
27#include <strings.h>
28#include <netinet/in.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <arpa/inet.h>
34#include <net/if.h>
35#include <net/if_arp.h>
36#ifdef __solaris__
37#include <sys/dlpi.h>
38#include <fcntl.h>
39#include <stropts.h>
40#endif
41#ifdef __linux__
42#include <sys/ioctl.h>
43#include <bits/ioctls.h>
44#include <linux/sockios.h>
45#include <sys/utsname.h>
46#include <stdio.h>
47#else
48#include <sys/sockio.h>
49#endif
50
51#ifdef __linux__
52#define ifr_index ifr_ifindex
53#define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
54#endif
55
56#include "jvm.h"
57#include "jni_util.h"
58#include "net_util.h"
59
60typedef struct _netaddr {
61 struct sockaddr *addr;
62 struct sockaddr *brdcast;
63 short mask;
64 int family; /* to make searches simple */
65 struct _netaddr *next;
66} netaddr;
67
68typedef struct _netif {
69 char *name;
70 int index;
71 char virtual;
72 netaddr *addr;
73 struct _netif *childs;
74 struct _netif *next;
75} netif;
76
77/************************************************************************
78 * NetworkInterface
79 */
80
81#include "java_net_NetworkInterface.h"
82
83/************************************************************************
84 * NetworkInterface
85 */
86jclass ni_class;
87jfieldID ni_nameID;
88jfieldID ni_indexID;
89jfieldID ni_descID;
90jfieldID ni_addrsID;
91jfieldID ni_bindsID;
92jfieldID ni_virutalID;
93jfieldID ni_childsID;
94jfieldID ni_parentID;
95jmethodID ni_ctrID;
96
97static jclass ni_iacls;
98static jclass ni_ia4cls;
99static jclass ni_ia6cls;
100static jclass ni_ibcls;
101static jmethodID ni_ia4ctrID;
102static jmethodID ni_ia6ctrID;
103static jmethodID ni_ibctrID;
104static jfieldID ni_iaaddressID;
105static jfieldID ni_iafamilyID;
106static jfieldID ni_ia6ipaddressID;
107static jfieldID ni_ibaddressID;
108static jfieldID ni_ib4broadcastID;
109static jfieldID ni_ib4maskID;
110
111static jobject createNetworkInterface(JNIEnv *env, netif *ifs);
112
113static netif *enumInterfaces(JNIEnv *env);
114static netif *enumIPv4Interfaces(JNIEnv *env, netif *ifs);
115#ifdef AF_INET6
116static netif *enumIPv6Interfaces(JNIEnv *env, netif *ifs);
117#endif
118
119static netif *addif(JNIEnv *env, netif *ifs, char *if_name, int index,
120 int family, struct sockaddr *new_addrP, int new_addrlen,
121 short prefix);
122static void freeif(netif *ifs);
123static struct sockaddr *getBroadcast(JNIEnv *env, const char *ifname);
124static short getSubnet(JNIEnv *env, const char *ifname);
125
126/*
127 * Class: java_net_NetworkInterface
128 * Method: init
129 * Signature: ()V
130 */
131JNIEXPORT void JNICALL
132Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls) {
133 ni_class = (*env)->FindClass(env,"java/net/NetworkInterface");
134 ni_class = (*env)->NewGlobalRef(env, ni_class);
135 ni_nameID = (*env)->GetFieldID(env, ni_class,"name", "Ljava/lang/String;");
136 ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
137 ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs", "[Ljava/net/InetAddress;");
138 ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings", "[Ljava/net/InterfaceAddress;");
139 ni_descID = (*env)->GetFieldID(env, ni_class, "displayName", "Ljava/lang/String;");
140 ni_virutalID = (*env)->GetFieldID(env, ni_class, "virtual", "Z");
141 ni_childsID = (*env)->GetFieldID(env, ni_class, "childs", "[Ljava/net/NetworkInterface;");
142 ni_parentID = (*env)->GetFieldID(env, ni_class, "parent", "Ljava/net/NetworkInterface;");
143 ni_ctrID = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
144
145 ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
146 ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
147 ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
148 ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
149 ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
150 ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
151 ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
152 ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
153 ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
154 ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
155 ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
156 ni_iaaddressID = (*env)->GetFieldID(env, ni_iacls, "address", "I");
157 ni_iafamilyID = (*env)->GetFieldID(env, ni_iacls, "family", "I");
158 ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
159 ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;");
160 ni_ib4broadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
161 ni_ib4maskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
162}
163
164
165/*
166 * Class: java_net_NetworkInterface
167 * Method: getByName0
168 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
169 */
170JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
171 (JNIEnv *env, jclass cls, jstring name) {
172
173 netif *ifs, *curr;
174 jboolean isCopy;
175 const char* name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
176 jobject obj = NULL;
177
178 ifs = enumInterfaces(env);
179 if (ifs == NULL) {
180 return NULL;
181 }
182
183 /*
184 * Search the list of interface based on name
185 */
186 curr = ifs;
187 while (curr != NULL) {
188 if (strcmp(name_utf, curr->name) == 0) {
189 break;
190 }
191 curr = curr->next;
192 }
193
194 /* if found create a NetworkInterface */
195 if (curr != NULL) {;
196 obj = createNetworkInterface(env, curr);
197 }
198
199 /* release the UTF string and interface list */
200 (*env)->ReleaseStringUTFChars(env, name, name_utf);
201 freeif(ifs);
202
203 return obj;
204}
205
206
207/*
208 * Class: java_net_NetworkInterface
209 * Method: getByIndex
210 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
211 */
212JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex
213 (JNIEnv *env, jclass cls, jint index) {
214
215 netif *ifs, *curr;
216 jobject obj = NULL;
217
218 if (index <= 0) {
219 return NULL;
220 }
221
222 ifs = enumInterfaces(env);
223 if (ifs == NULL) {
224 return NULL;
225 }
226
227 /*
228 * Search the list of interface based on index
229 */
230 curr = ifs;
231 while (curr != NULL) {
232 if (index == curr->index) {
233 break;
234 }
235 curr = curr->next;
236 }
237
238 /* if found create a NetworkInterface */
239 if (curr != NULL) {;
240 obj = createNetworkInterface(env, curr);
241 }
242
243 freeif(ifs);
244 return obj;
245}
246
247/*
248 * Class: java_net_NetworkInterface
249 * Method: getByInetAddress0
250 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
251 */
252JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
253 (JNIEnv *env, jclass cls, jobject iaObj) {
254
255 netif *ifs, *curr;
256 int family = (*env)->GetIntField(env, iaObj, ni_iafamilyID) == IPv4?
257 AF_INET : AF_INET6;
258 jobject obj = NULL;
259 jboolean match = JNI_FALSE;
260
261 ifs = enumInterfaces(env);
262 if (ifs == NULL) {
263 return NULL;
264 }
265
266 curr = ifs;
267 while (curr != NULL) {
268 netaddr *addrP = curr->addr;
269
270 /*
271 * Iterate through each address on the interface
272 */
273 while (addrP != NULL) {
274
275 if (family == addrP->family) {
276 if (family == AF_INET) {
277 int address1 = htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr);
278 int address2 = (*env)->GetIntField(env, iaObj, ni_iaaddressID);
279
280 if (address1 == address2) {
281 match = JNI_TRUE;
282 break;
283 }
284 }
285
286#ifdef AF_INET6
287 if (family == AF_INET6) {
288 jbyte *bytes = (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr);
289 jbyteArray ipaddress = (*env)->GetObjectField(env, iaObj, ni_ia6ipaddressID);
290 jbyte caddr[16];
291 int i;
292
293 (*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddr);
294 i = 0;
295 while (i < 16) {
296 if (caddr[i] != bytes[i]) {
297 break;
298 }
299 i++;
300 }
301 if (i >= 16) {
302 match = JNI_TRUE;
303 break;
304 }
305 }
306#endif
307
308 }
309
310 if (match) {
311 break;
312 }
313 addrP = addrP->next;
314 }
315
316 if (match) {
317 break;
318 }
319 curr = curr->next;
320 }
321
322 /* if found create a NetworkInterface */
323 if (match) {;
324 obj = createNetworkInterface(env, curr);
325 }
326
327 freeif(ifs);
328 return obj;
329}
330
331
332/*
333 * Class: java_net_NetworkInterface
334 * Method: getAll
335 * Signature: ()[Ljava/net/NetworkInterface;
336 */
337JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll
338 (JNIEnv *env, jclass cls) {
339
340 netif *ifs, *curr;
341 jobjectArray netIFArr;
342 jint arr_index, ifCount;
343
344 ifs = enumInterfaces(env);
345 if (ifs == NULL) {
346 return NULL;
347 }
348
349 /* count the interface */
350 ifCount = 0;
351 curr = ifs;
352 while (curr != NULL) {
353 ifCount++;
354 curr = curr->next;
355 }
356
357 /* allocate a NetworkInterface array */
358 netIFArr = (*env)->NewObjectArray(env, ifCount, cls, NULL);
359 if (netIFArr == NULL) {
360 freeif(ifs);
361 return NULL;
362 }
363
364 /*
365 * Iterate through the interfaces, create a NetworkInterface instance
366 * for each array element and populate the object.
367 */
368 curr = ifs;
369 arr_index = 0;
370 while (curr != NULL) {
371 jobject netifObj;
372
373 netifObj = createNetworkInterface(env, curr);
374 if (netifObj == NULL) {
375 freeif(ifs);
376 return NULL;
377 }
378
379 /* put the NetworkInterface into the array */
380 (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
381
382 curr = curr->next;
383 }
384
385 freeif(ifs);
386 return netIFArr;
387}
388
389/*
390 * Create a NetworkInterface object, populate the name and index, and
391 * populate the InetAddress array based on the IP addresses for this
392 * interface.
393 */
394jobject createNetworkInterface(JNIEnv *env, netif *ifs)
395{
396 jobject netifObj;
397 jobject name;
398 jobjectArray addrArr;
399 jobjectArray bindArr;
400 jobjectArray childArr;
401 netaddr *addrs;
402 jint addr_index, addr_count, bind_index;
403 jint child_count, child_index;
404 netaddr *addrP;
405 netif *childP;
406 jobject tmp;
407
408 /*
409 * Create a NetworkInterface object and populate it
410 */
411 netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
412 name = (*env)->NewStringUTF(env, ifs->name);
413 if (netifObj == NULL || name == NULL) {
414 return NULL;
415 }
416 (*env)->SetObjectField(env, netifObj, ni_nameID, name);
417 (*env)->SetObjectField(env, netifObj, ni_descID, name);
418 (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
419 (*env)->SetBooleanField(env, netifObj, ni_virutalID, ifs->virtual ? JNI_TRUE : JNI_FALSE);
420
421 /*
422 * Count the number of address on this interface
423 */
424 addr_count = 0;
425 addrP = ifs->addr;
426 while (addrP != NULL) {
427 addr_count++;
428 addrP = addrP->next;
429 }
430
431 /*
432 * Create the array of InetAddresses
433 */
434 addrArr = (*env)->NewObjectArray(env, addr_count, ni_iacls, NULL);
435 if (addrArr == NULL) {
436 return NULL;
437 }
438
439 bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
440 if (bindArr == NULL) {
441 return NULL;
442 }
443 addrP = ifs->addr;
444 addr_index = 0;
445 bind_index = 0;
446 while (addrP != NULL) {
447 jobject iaObj = NULL;
448 jobject ibObj = NULL;
449
450 if (addrP->family == AF_INET) {
451 iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
452 if (iaObj) {
453 (*env)->SetIntField(env, iaObj, ni_iaaddressID,
454 htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
455 }
456 ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
457 if (ibObj) {
458 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
459 if (addrP->brdcast) {
460 jobject ia2Obj = NULL;
461 ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
462 if (ia2Obj) {
463 (*env)->SetIntField(env, ia2Obj, ni_iaaddressID,
464 htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
465 (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
466 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
467 }
468 }
469 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
470 }
471 }
472
473#ifdef AF_INET6
474 if (addrP->family == AF_INET6) {
475 int scope=0;
476 iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
477 if (iaObj) {
478 jbyteArray ipaddress = (*env)->NewByteArray(env, 16);
479 if (ipaddress == NULL) {
480 return NULL;
481 }
482 (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
483 (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
484#ifdef __linux__
485 if (!kernelIsV22()) {
486 scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
487 }
488#else
489 scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
490#endif
491 if (scope != 0) { /* zero is default value, no need to set */
492 (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
493 (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
494 (*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj);
495 }
496 (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
497 }
498 ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
499 if (ibObj) {
500 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
501 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
502 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
503 }
504 }
505#endif
506
507 if (iaObj == NULL) {
508 return NULL;
509 }
510
511 (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
512 addrP = addrP->next;
513 }
514
515 /*
516 * See if there is any virtual interface attached to this one.
517 */
518 child_count = 0;
519 childP = ifs->childs;
520 while (childP) {
521 child_count++;
522 childP = childP->next;
523 }
524
525 childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
526 if (childArr == NULL) {
527 return NULL;
528 }
529
530 /*
531 * Create the NetworkInterface instances for the sub-interfaces as
532 * well.
533 */
534 child_index = 0;
535 childP = ifs->childs;
536 while(childP) {
537 tmp = createNetworkInterface(env, childP);
538 if (tmp == NULL) {
539 return NULL;
540 }
541 (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
542 (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
543 childP = childP->next;
544 }
545 (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
546 (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
547 (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
548
549 /* return the NetworkInterface */
550 return netifObj;
551}
552
553/*
554 * Enumerates all interfaces
555 */
556static netif *enumInterfaces(JNIEnv *env) {
557 netif *ifs;
558
559 /*
560 * Enumerate IPv4 addresses
561 */
562 ifs = enumIPv4Interfaces(env, NULL);
563 if (ifs == NULL) {
564 if ((*env)->ExceptionOccurred(env)) {
565 return NULL;
566 }
567 }
568
569 /*
570 * If IPv6 is available then enumerate IPv6 addresses.
571 */
572#ifdef AF_INET6
573 if (ipv6_available()) {
574 ifs = enumIPv6Interfaces(env, ifs);
575
576 if ((*env)->ExceptionOccurred(env)) {
577 freeif(ifs);
578 return NULL;
579 }
580 }
581#endif
582
583 return ifs;
584}
585
586
587/*
588 * Enumerates and returns all IPv4 interfaces
589 */
590static netif *enumIPv4Interfaces(JNIEnv *env, netif *ifs) {
591 int sock;
592 struct ifconf ifc;
593 struct ifreq *ifreqP;
594 char *buf;
595 int numifs;
596 unsigned i;
597 unsigned bufsize;
598
599 sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
600 if (sock < 0) {
601 /*
602 * If EPROTONOSUPPORT is returned it means we don't have
603 * IPv4 support so don't throw an exception.
604 */
605 if (errno != EPROTONOSUPPORT) {
606 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
607 "Socket creation failed");
608 }
609 return ifs;
610 }
611
612#ifdef __linux__
613 /* need to do a dummy SIOCGIFCONF to determine the buffer size.
614 * SIOCGIFCOUNT doesn't work
615 */
616 ifc.ifc_buf = NULL;
617 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
618 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
619 "ioctl SIOCGIFCONF failed");
620 close(sock);
621 return ifs;
622 }
623 bufsize = ifc.ifc_len;
624#else
625 if (ioctl(sock, SIOCGIFNUM, (char *)&numifs) < 0) {
626 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
627 "ioctl SIOCGIFNUM failed");
628 close(sock);
629 return ifs;
630 }
631 bufsize = numifs * sizeof (struct ifreq);
632#endif /* __linux__ */
633
634 buf = (char *)malloc(bufsize);
635 if (!buf) {
636 JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
637 (void) close(sock);
638 return ifs;
639 }
640 ifc.ifc_len = bufsize;
641 ifc.ifc_buf = buf;
642 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
643 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
644 "ioctl SIOCGIFCONF failed");
645 (void) close(sock);
646 (void) free(buf);
647 return ifs;
648 }
649
650 /*
651 * Iterate through each interface
652 */
653 ifreqP = ifc.ifc_req;
654 for (i=0; i<ifc.ifc_len/sizeof (struct ifreq); i++, ifreqP++) {
655 int index;
656 struct ifreq if2;
657
658 memset((char *)&if2, 0, sizeof(if2));
659 strcpy(if2.ifr_name, ifreqP->ifr_name);
660
661 /*
662 * Try to get the interface index
663 * (Not supported on Solaris 2.6 or 7)
664 */
665 if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) >= 0) {
666 index = if2.ifr_index;
667 } else {
668 index = -1;
669 }
670
671 /*
672 * Add to the list
673 */
674 ifs = addif(env, ifs, ifreqP->ifr_name, index, AF_INET,
675 (struct sockaddr *)&(ifreqP->ifr_addr),
676 sizeof(struct sockaddr_in), 0);
677
678 /*
679 * If an exception occurred then free the list
680 */
681 if ((*env)->ExceptionOccurred(env)) {
682 close(sock);
683 free(buf);
684 freeif(ifs);
685 return NULL;
686 }
687 }
688
689 /*
690 * Free socket and buffer
691 */
692 close(sock);
693 free(buf);
694 return ifs;
695}
696
697
698#if defined(__solaris__) && defined(AF_INET6)
699/*
700 * Enumerates and returns all IPv6 interfaces on Solaris
701 */
702static netif *enumIPv6Interfaces(JNIEnv *env, netif *ifs) {
703 int sock;
704 struct lifconf ifc;
705 struct lifreq *ifr;
706 int n;
707 char *buf;
708 struct lifnum numifs;
709 unsigned bufsize;
710
711 sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0);
712 if (sock < 0) {
713 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
714 "Failed to create IPv6 socket");
715 return ifs;
716 }
717
718 /*
719 * Get the interface count
720 */
721 numifs.lifn_family = AF_UNSPEC;
722 numifs.lifn_flags = 0;
723 if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
724 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
725 "ioctl SIOCGLIFNUM failed");
726 close(sock);
727 return ifs;
728 }
729
730 /*
731 * Enumerate the interface configurations
732 */
733 bufsize = numifs.lifn_count * sizeof (struct lifreq);
734 buf = (char *)malloc(bufsize);
735 if (!buf) {
736 JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
737 (void) close(sock);
738 return ifs;
739 }
740 ifc.lifc_family = AF_UNSPEC;
741 ifc.lifc_flags = 0;
742 ifc.lifc_len = bufsize;
743 ifc.lifc_buf = buf;
744 if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
745 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
746 "ioctl SIOCGLIFCONF failed");
747 close(sock);
748 free(buf);
749 return ifs;
750 }
751
752 /*
753 * Iterate through each interface
754 */
755 ifr = ifc.lifc_req;
756 for (n=0; n<numifs.lifn_count; n++, ifr++) {
757 int index = -1;
758 struct lifreq if2;
759
760 /*
761 * Ignore non-IPv6 addresses
762 */
763 if (ifr->lifr_addr.ss_family != AF_INET6) {
764 continue;
765 }
766
767 /*
768 * Get the index
769 */
770 memset((char *)&if2, 0, sizeof(if2));
771 strcpy(if2.lifr_name, ifr->lifr_name);
772 if (ioctl(sock, SIOCGLIFINDEX, (char *)&if2) >= 0) {
773 struct sockaddr_in6 *s6= (struct sockaddr_in6 *)&(ifr->lifr_addr);
774 index = if2.lifr_index;
775 s6->sin6_scope_id = index;
776 }
777
778 /* add to the list */
779 ifs = addif(env, ifs, ifr->lifr_name, index, AF_INET6,
780 (struct sockaddr *)&(ifr->lifr_addr),
781 sizeof(struct sockaddr_in6), (short) ifr->lifr_addrlen);
782
783 /*
784 * If an exception occurred we return
785 */
786 if ((*env)->ExceptionOccurred(env)) {
787 close(sock);
788 free(buf);
789 return ifs;
790 }
791
792 }
793
794 close(sock);
795 free(buf);
796 return ifs;
797
798}
799#endif
800
801
802#if defined(__linux__) && defined(AF_INET6)
803/*
804 * Enumerates and returns all IPv6 interfaces on Linux
805 */
806static netif *enumIPv6Interfaces(JNIEnv *env, netif *ifs) {
807 FILE *f;
808 char addr6[40], devname[20];
809 char addr6p[8][5];
810 int plen, scope, dad_status, if_idx;
811 uint8_t ipv6addr[16];
812
813 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
814 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
815 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
816 addr6p[4], addr6p[5], addr6p[6], addr6p[7],
817 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
818 struct netif *ifs_ptr = NULL;
819 struct netif *last_ptr = NULL;
820 struct sockaddr_in6 addr;
821
822 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
823 addr6p[0], addr6p[1], addr6p[2], addr6p[3],
824 addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
825 inet_pton(AF_INET6, addr6, ipv6addr);
826
827 memset(&addr, 0, sizeof(struct sockaddr_in6));
828 memcpy((void*)addr.sin6_addr.s6_addr, (const void*)ipv6addr, 16);
829 addr.sin6_scope_id = if_idx;
830
831 ifs = addif(env, ifs, devname, if_idx, AF_INET6,
832 (struct sockaddr *)&addr,
833 sizeof(struct sockaddr_in6), plen);
834
835 /*
836 * If an exception occurred then return the list as is.
837 */
838 if ((*env)->ExceptionOccurred(env)) {
839 fclose(f);
840 return ifs;
841 }
842 }
843 fclose(f);
844 }
845 return ifs;
846}
847#endif
848
849
850/*
851 * Free an interface list (including any attached addresses)
852 */
853void freeif(netif *ifs) {
854 netif *currif = ifs;
855 netif *child = NULL;
856
857 while (currif != NULL) {
858 netaddr *addrP = currif->addr;
859 while (addrP != NULL) {
860 netaddr *next = addrP->next;
861 if (addrP->addr != NULL)
862 free(addrP->addr);
863 if (addrP->brdcast != NULL)
864 free(addrP->brdcast);
865 free(addrP);
866 addrP = next;
867 }
868
869 free(currif->name);
870
871 /*
872 * Don't forget to free the sub-interfaces.
873 */
874 if (currif->childs != NULL) {
875 freeif(currif->childs);
876 }
877
878 ifs = currif->next;
879 free(currif);
880 currif = ifs;
881 }
882}
883
884/*
885 * Add an interface to the list. If known interface just link
886 * a new netaddr onto the list. If new interface create new
887 * netif structure.
888 */
889netif *addif(JNIEnv *env, netif *ifs, char *if_name, int index, int family,
890 struct sockaddr *new_addrP, int new_addrlen, short prefix) {
891 netif *currif = ifs, *parent;
892 netaddr *addrP;
893#ifdef LIFNAMSIZ
894 char name[LIFNAMSIZ];
895 char vname[LIFNAMSIZ];
896#else
897 char name[IFNAMSIZ];
898 char vname[IFNAMSIZ];
899#endif
900 char *unit;
901 int isVirtual = 0;
902
903 /*
904 * If the interface name is a logical interface then we
905 * remove the unit number so that we have the physical
906 * interface (eg: hme0:1 -> hme0). NetworkInterface
907 * currently doesn't have any concept of physical vs.
908 * logical interfaces.
909 */
910 strcpy(name, if_name);
911
912 /*
913 * Create and populate the netaddr node. If allocation fails
914 * return an un-updated list.
915 */
916 addrP = (netaddr *)malloc(sizeof(netaddr));
917 if (addrP) {
918 addrP->addr = (struct sockaddr *)malloc(new_addrlen);
919 if (addrP->addr == NULL) {
920 free(addrP);
921 addrP = NULL;
922 }
923 }
924 if (addrP == NULL) {
925 JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
926 return ifs; /* return untouched list */
927 }
928 memcpy(addrP->addr, new_addrP, new_addrlen);
929 addrP->family = family;
930
931 addrP->brdcast = NULL;
932 addrP->mask = prefix;
933 if (family == AF_INET) {
934 /*
935 * Deal with brodcast addr & subnet mask
936 */
937 addrP->brdcast = getBroadcast(env, name);
938 if (addrP->brdcast) {
939 addrP->mask = getSubnet(env, name);
940 }
941 }
942
943 vname[0] = 0;
944 unit = strchr(name, ':');
945 if (unit != NULL) {
946 /**
947 * This is a virtual interface. If we are able to access the parent
948 * we need to create a new entry if it doesn't exist yet *and* update
949 * the 'parent' interface with the new records.
950 */
951 struct ifreq if2;
952 int sock;
953 int len;
954
955 sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
956 if (sock < 0) {
957 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
958 "Socket creation failed");
959 return ifs; /* return untouched list */
960 }
961
962 len = unit - name;
963 if (len > 0) {
964 // temporarily use vname to hold the parent name of the interface
965 // instead of creating another buffer.
966 memcpy(&vname, name, len);
967 vname[len] = '\0';
968
969 memset((char *) &if2, 0, sizeof(if2));
970 strcpy(if2.ifr_name, vname);
971
972 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) >= 0) {
973 // Got access to parent, so create it if necessary.
974 strcpy(vname, name);
975 *unit = '\0';
976 }
977 else {
978 // failed to access parent interface do not create parent.
979 // We are a virtual interface with no parent.
980 isVirtual = 1;
981
982 vname[0] = 0;
983 }
984 }
985 close(sock);
986 }
987
988 /*
989 * Check if this is a "new" interface. Use the interface
990 * name for matching because index isn't supported on
991 * Solaris 2.6 & 7.
992 */
993 while (currif != NULL) {
994 if (strcmp(name, currif->name) == 0) {
995 break;
996 }
997 currif = currif->next;
998 }
999
1000 /*
1001 * If "new" then create an netif structure and
1002 * insert it onto the list.
1003 */
1004 if (currif == NULL) {
1005 currif = (netif *)malloc(sizeof(netif));
1006 if (currif) {
1007 currif->name = strdup(name);
1008 if (currif->name == NULL) {
1009 free(currif);
1010 currif = NULL;
1011 }
1012 }
1013 if (currif == NULL) {
1014 JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
1015 return ifs;
1016 }
1017 currif->index = index;
1018 currif->addr = NULL;
1019 currif->childs = NULL;
1020 currif->virtual = isVirtual;
1021 currif->next = ifs;
1022 ifs = currif;
1023 }
1024
1025 /*
1026 * Finally insert the address on the interface
1027 */
1028 addrP->next = currif->addr;
1029 currif->addr = addrP;
1030
1031 parent = currif;
1032
1033 /**
1034 * Let's deal with the virtual interface now.
1035 */
1036 if (vname[0]) {
1037 netaddr *tmpaddr;
1038
1039 currif = parent->childs;
1040
1041 while (currif != NULL) {
1042 if (strcmp(vname, currif->name) == 0) {
1043 break;
1044 }
1045 currif = currif->next;
1046 }
1047 if (currif == NULL) {
1048 currif = (netif *)malloc(sizeof(netif));
1049 if (currif) {
1050 currif->name = strdup(vname);
1051 if (currif->name == NULL) {
1052 free(currif);
1053 currif = NULL;
1054 }
1055 }
1056 if (currif == NULL) {
1057 JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
1058 return ifs;
1059 }
1060 currif->index = index;
1061 currif->addr = NULL;
1062 /* Need to duplicate the addr entry? */
1063 currif->virtual = 1;
1064 currif->childs = NULL;
1065 currif->next = parent->childs;
1066 parent->childs = currif;
1067 }
1068
1069 tmpaddr = (netaddr *) malloc(sizeof(netaddr));
1070 if (tmpaddr == NULL) {
1071 JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
1072 return ifs;
1073 }
1074 memcpy(tmpaddr, addrP, sizeof(netaddr));
1075 /**
1076 * Let's duplicate the address and broadcast address structures
1077 * if there are any.
1078 */
1079 if (addrP->addr != NULL) {
1080 tmpaddr->addr = malloc(new_addrlen);
1081 if (tmpaddr->addr != NULL)
1082 memcpy(tmpaddr->addr, addrP->addr, new_addrlen);
1083 }
1084 if (addrP->brdcast != NULL) {
1085 tmpaddr->brdcast = malloc(new_addrlen);
1086 if (tmpaddr->brdcast != NULL)
1087 memcpy(tmpaddr->brdcast, addrP->brdcast, new_addrlen);
1088 }
1089 tmpaddr->next = currif->addr;
1090 currif->addr = tmpaddr;
1091 }
1092
1093 return ifs;
1094}
1095
1096/**
1097 * Get flags from a NetworkInterface.
1098 */
1099static short getFlags(JNIEnv *env, jstring name) {
1100 int sock;
1101 struct ifreq if2;
1102 jboolean isCopy;
1103 const char* name_utf;
1104 short ret = -1;
1105
1106 sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
1107 if (sock < 0) {
1108 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1109 "Socket creation failed");
1110 return -1;
1111 }
1112
1113 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
1114 memset((char *) &if2, 0, sizeof(if2));
1115 strcpy(if2.ifr_name, name_utf);
1116
1117 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) >= 0) {
1118 ret = if2.ifr_flags;
1119 } else {
1120 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1121 "IOCTL failed");
1122 ret = -1;
1123 }
1124 close(sock);
1125 /* release the UTF string and interface list */
1126 (*env)->ReleaseStringUTFChars(env, name, name_utf);
1127
1128 return ret;
1129}
1130
1131/**
1132 * Returns the IPv4 broadcast address of a named interface, if it exists.
1133 * Returns 0 if it doesn't have one.
1134 */
1135static struct sockaddr *getBroadcast(JNIEnv *env, const char *ifname) {
1136 int sock;
1137 unsigned int mask;
1138 struct sockaddr *ret = NULL;
1139 struct ifreq if2;
1140 short flag;
1141
1142 sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
1143 if (sock < 0) {
1144 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1145 "Socket creation failed");
1146 return ret;
1147 }
1148
1149 memset((char *) &if2, 0, sizeof(if2));
1150 strcpy(if2.ifr_name, ifname);
1151 /* Let's make sure the interface does have a broadcast address */
1152 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) >= 0) {
1153 flag = if2.ifr_flags;
1154 } else {
1155 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1156 "IOCTL failed");
1157 }
1158 if (flag & IFF_BROADCAST) {
1159 /* It does, let's retrieve it*/
1160 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&if2) >= 0) {
1161 ret = (struct sockaddr*) malloc(sizeof(struct sockaddr));
1162 memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr));
1163 } else {
1164 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1165 "IOCTL failed");
1166 }
1167 }
1168 close(sock);
1169 return ret;
1170}
1171
1172/**
1173 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
1174 * interface, if it has one, otherwise return -1.
1175 */
1176static short getSubnet(JNIEnv *env, const char *ifname) {
1177 int sock;
1178 unsigned int mask;
1179 short ret;
1180 struct ifreq if2;
1181
1182 sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
1183 if (sock < 0) {
1184 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1185 "Socket creation failed");
1186 return -1;
1187 }
1188
1189 memset((char *) &if2, 0, sizeof(if2));
1190 strcpy(if2.ifr_name, ifname);
1191 if (ioctl(sock, SIOCGIFNETMASK, (char *)&if2) >= 0) {
1192 mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
1193 ret = 0;
1194 while (mask) {
1195 mask <<= 1;
1196 ret++;
1197 }
1198 close(sock);
1199 return ret;
1200 }
1201 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1202 "IOCTL failed");
1203 close(sock);
1204 return -1;
1205}
1206
1207#ifdef __solaris__
1208#define DEV_PREFIX "/dev/"
1209
1210/**
1211 * Solaris specific DLPI code to get hardware address from a device.
1212 * Unfortunately, at least up to Solaris X, you have to have special
1213 * privileges (i.e. be root).
1214 */
1215static int getMacFromDevice(JNIEnv *env, const char* ifname, unsigned char* retbuf) {
1216 char style1dev[MAXPATHLEN];
1217 int fd;
1218 dl_phys_addr_req_t dlpareq;
1219 dl_phys_addr_ack_t *dlpaack;
1220 struct strbuf msg;
1221 char buf[128];
1222 int flags = 0;
1223
1224 /**
1225 * Device is in /dev
1226 * e.g.: /dev/bge0
1227 */
1228 strcpy(style1dev, DEV_PREFIX);
1229 strcat(style1dev, ifname);
1230 if ((fd = open(style1dev, O_RDWR)) == -1) {
1231 /*
1232 * Can't open it. We probably are missing the privilege.
1233 * We'll have to try something else
1234 */
1235 return 0;
1236 }
1237 dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
1238 dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
1239 msg.buf = (char *)&dlpareq;
1240 msg.len = DL_PHYS_ADDR_REQ_SIZE;
1241 if (putmsg(fd, &msg, NULL, 0) < 0) {
1242 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1243 "putmsg failed");
1244 return -1;
1245 }
1246 dlpaack = (dl_phys_addr_ack_t *)buf;
1247 msg.buf = (char *)buf;
1248 msg.len = 0;
1249 msg.maxlen = sizeof (buf);
1250 if (getmsg(fd, &msg, NULL, &flags) < 0) {
1251 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1252 "getmsg failed");
1253 return -1;
1254 }
1255 if (msg.len < DL_PHYS_ADDR_ACK_SIZE ||
1256 dlpaack->dl_primitive != DL_PHYS_ADDR_ACK) {
1257 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
1258 "Couldn't obtain phys addr\n");
1259 return -1;
1260 }
1261
1262 memcpy(retbuf, &buf[dlpaack->dl_addr_offset], dlpaack->dl_addr_length);
1263 return dlpaack->dl_addr_length;
1264}
1265#endif
1266
1267/**
1268 * Get the Hardware address (usually MAC address) for the named interface.
1269 * return puts the data in buf, and returns the length, in byte, of the
1270 * MAC address. Returns -1 if there is no hardware address on that interface.
1271 */
1272int getMacAddress(JNIEnv *env, const struct in_addr* addr, const char* ifname,
1273 unsigned char *buf) {
1274 int sock;
1275#ifdef __linux__
1276 static struct ifreq ifr;
1277 int i;
1278
1279 sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
1280
1281 if (sock < 0) {
1282 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1283 "Socket creation failed");
1284 return -1;
1285 }
1286
1287 strcpy(ifr.ifr_name, ifname);
1288
1289 if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
1290 fprintf(stderr, "SIOCIFHWADDR: %s\n",
1291 strerror(errno));
1292 close(sock);
1293 return -1;
1294 }
1295 memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
1296 close(sock);
1297 for (i = 0; i < IFHWADDRLEN; i++) {
1298 if (buf[i] != 0)
1299 return IFHWADDRLEN;
1300 }
1301 /*
1302 * All bytes to 0 means no hardware address.
1303 */
1304 return -1;
1305#else
1306 struct arpreq arpreq;
1307 struct sockaddr_in* sin;
1308 struct sockaddr_in ipAddr;
1309 int len;
1310
1311 /**
1312 * On Solaris we have to use DLPI, but it will only work if we have
1313 * privileged access (i.e. root). If that fails, we try a lookup
1314 * in the ARP table, which requires an IPv4 address.
1315 */
1316 if ((len = getMacFromDevice(env, ifname, buf)) > 0) {
1317 return len;
1318 }
1319 if (addr == NULL) {
1320 /**
1321 * No IPv4 address for that interface, so can't do an ARP lookup.
1322 */
1323 return -1;
1324 }
1325 sin = (struct sockaddr_in *) &arpreq.arp_pa;
1326 memset((char *) &arpreq, 0, sizeof(struct arpreq));
1327 ipAddr.sin_port = 0;
1328 ipAddr.sin_family = AF_INET;
1329 memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
1330 memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
1331 arpreq.arp_flags= ATF_PUBL;
1332 sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
1333
1334 if (sock < 0) {
1335 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1336 "Socket creation failed");
1337 return -1;
1338 }
1339
1340 if (ioctl(sock, SIOCGARP, &arpreq) >= 0) {
1341 close(sock);
1342 memcpy(buf, &arpreq.arp_ha.sa_data[0], 6);
1343 return 6;
1344 }
1345
1346 if (errno != ENXIO) {
1347 // "No such device or address" means no hardware address, so it's
1348 // normal don't throw an exception
1349 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1350 "IOCTL failed");
1351 }
1352 close(sock);
1353#endif
1354 return -1;
1355}
1356
1357/*
1358 * Class: java_net_NetworkInterface
1359 * Method: isUp0
1360 * Signature: (Ljava/lang/String;I)Z
1361 */
1362JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0
1363 (JNIEnv *env, jclass cls, jstring name, jint index) {
1364 short val;
1365
1366 val = getFlags(env, name);
1367 if ( (val & IFF_UP) && (val & IFF_RUNNING))
1368 return JNI_TRUE;
1369 return JNI_FALSE;
1370}
1371
1372/*
1373 * Class: java_net_NetworkInterface
1374 * Method: isP2P0
1375 * Signature: (Ljava/lang/String;I)Z
1376 */
1377JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0
1378 (JNIEnv *env, jclass cls, jstring name, jint index) {
1379 if (getFlags(env, name) & IFF_POINTOPOINT)
1380 return JNI_TRUE;
1381 return JNI_FALSE;
1382}
1383
1384/*
1385 * Class: java_net_NetworkInterface
1386 * Method: isLoopback0
1387 * Signature: (Ljava/lang/String;I)Z
1388 */
1389JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0
1390 (JNIEnv *env, jclass cls, jstring name, jint index) {
1391 if (getFlags(env, name) & IFF_LOOPBACK)
1392 return JNI_TRUE;
1393 return JNI_FALSE;
1394}
1395
1396/*
1397 * Class: java_net_NetworkInterface
1398 * Method: supportsMulticast0
1399 * Signature: (Ljava/lang/String;I)Z
1400 */
1401JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0
1402(JNIEnv *env, jclass cls, jstring name, jint index) {
1403 short val;
1404
1405 val = getFlags(env, name);
1406 if (val & IFF_MULTICAST)
1407 return JNI_TRUE;
1408 return JNI_FALSE;
1409}
1410
1411/*
1412 * Class: java_net_NetworkInterface
1413 * Method: getMacAddr0
1414 * Signature: ([bLjava/lang/String;I)[b
1415 */
1416JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0(JNIEnv *env, jclass class, jbyteArray addrArray, jstring name, jint index) {
1417 jint addr;
1418 jbyte caddr[4];
1419 struct in_addr iaddr;
1420 jbyteArray ret = NULL;
1421 unsigned char mac[16];
1422 int len;
1423 jboolean isCopy;
1424 const char* name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
1425
1426 if (!IS_NULL(addrArray)) {
1427 (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
1428 addr = ((caddr[0]<<24) & 0xff000000);
1429 addr |= ((caddr[1] <<16) & 0xff0000);
1430 addr |= ((caddr[2] <<8) & 0xff00);
1431 addr |= (caddr[3] & 0xff);
1432 iaddr.s_addr = htonl(addr);
1433 len = getMacAddress(env, &iaddr, name_utf, mac);
1434 } else {
1435 len = getMacAddress(env, NULL, name_utf, mac);
1436 }
1437 if (len > 0) {
1438 ret = (*env)->NewByteArray(env, len);
1439 if (IS_NULL(ret)) {
1440 /* we may have memory to free at the end of this */
1441 goto fexit;
1442 }
1443 (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *) (mac));
1444 }
1445 fexit:
1446 /* release the UTF string and interface list */
1447 (*env)->ReleaseStringUTFChars(env, name, name_utf);
1448 return ret;
1449}
1450
1451/*
1452 * Class: java_net_NetworkInterface
1453 * Method: getMTU0
1454 * Signature: ([bLjava/lang/String;I)I
1455 */
1456
1457JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclass class, jstring name, jint index) {
1458 jboolean isCopy;
1459 int sock;
1460 struct ifreq if2;
1461 int ret = -1;
1462 const char* name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
1463
1464 sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
1465 if (sock < 0) {
1466 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1467 "Socket creation failed");
1468 } else {
1469
1470#ifdef __linux__
1471 memset((char *) &if2, 0, sizeof(if2));
1472 strcpy(if2.ifr_name, name_utf);
1473
1474 if (ioctl(sock, SIOCGIFMTU, (char *)&if2) >= 0) {
1475 ret= if2.ifr_mtu;
1476 } else {
1477 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1478 "IOCTL failed");
1479 }
1480#else /* Solaris */
1481 struct lifreq lifr;
1482 memset((caddr_t)&lifr, 0, sizeof(lifr));
1483 strcpy((caddr_t)&(lifr.lifr_name), name_utf);
1484 if (ioctl(sock, SIOCGLIFMTU, (caddr_t)&lifr) >= 0) {
1485 ret = lifr.lifr_mtu;
1486 } else {
1487 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
1488 "IOCTL failed");
1489 }
1490#endif
1491 close(sock);
1492 }
1493 /* release the UTF string and interface list */
1494 (*env)->ReleaseStringUTFChars(env, name, name_utf);
1495 return ret;
1496}