Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/java/net/NetworkInterface.c
32287 views
1
/*
2
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 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.
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
37
#if defined(__solaris__)
38
#include <sys/dlpi.h>
39
#include <fcntl.h>
40
#include <stropts.h>
41
#include <sys/sockio.h>
42
#endif
43
44
#if defined(__linux__)
45
#include <sys/ioctl.h>
46
#include <sys/utsname.h>
47
#include <stdio.h>
48
#endif
49
50
#if defined(_AIX)
51
#include <sys/ioctl.h>
52
#include <netinet/in6_var.h>
53
#include <sys/ndd_var.h>
54
#include <sys/kinfo.h>
55
#endif
56
57
#if defined(_ALLBSD_SOURCE)
58
#include <sys/param.h>
59
#include <sys/ioctl.h>
60
#include <sys/sockio.h>
61
#if defined(__APPLE__)
62
#include <net/ethernet.h>
63
#include <net/if_var.h>
64
#include <net/if_dl.h>
65
#include <netinet/in_var.h>
66
#include <ifaddrs.h>
67
#endif
68
#endif
69
70
#include "jvm.h"
71
#include "jni_util.h"
72
#include "net_util.h"
73
74
#if defined(__linux__)
75
#define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
76
#elif defined(__solaris__)
77
#ifndef SIOCGLIFHWADDR
78
#define SIOCGLIFHWADDR _IOWR('i', 192, struct lifreq)
79
#endif
80
#define DEV_PREFIX "/dev/"
81
#endif
82
83
#ifdef LIFNAMSIZ
84
#define IFNAMESIZE LIFNAMSIZ
85
#else
86
#define IFNAMESIZE IFNAMSIZ
87
#endif
88
89
#define CHECKED_MALLOC3(_pointer, _type, _size) \
90
do { \
91
_pointer = (_type)malloc(_size); \
92
if (_pointer == NULL) { \
93
JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); \
94
return ifs; /* return untouched list */ \
95
} \
96
} while(0)
97
98
typedef struct _netaddr {
99
struct sockaddr *addr;
100
struct sockaddr *brdcast;
101
short mask;
102
int family; /* to make searches simple */
103
struct _netaddr *next;
104
} netaddr;
105
106
typedef struct _netif {
107
char *name;
108
int index;
109
char virtual;
110
netaddr *addr;
111
struct _netif *childs;
112
struct _netif *next;
113
} netif;
114
115
/************************************************************************
116
* NetworkInterface
117
*/
118
119
#include "java_net_NetworkInterface.h"
120
121
/************************************************************************
122
* NetworkInterface
123
*/
124
jclass ni_class;
125
jfieldID ni_nameID;
126
jfieldID ni_indexID;
127
jfieldID ni_descID;
128
jfieldID ni_addrsID;
129
jfieldID ni_bindsID;
130
jfieldID ni_virutalID;
131
jfieldID ni_childsID;
132
jfieldID ni_parentID;
133
jfieldID ni_defaultIndexID;
134
jmethodID ni_ctrID;
135
136
static jclass ni_ibcls;
137
static jmethodID ni_ibctrID;
138
static jfieldID ni_ibaddressID;
139
static jfieldID ni_ib4broadcastID;
140
static jfieldID ni_ib4maskID;
141
142
/** Private methods declarations **/
143
static jobject createNetworkInterface(JNIEnv *env, netif *ifs);
144
static int getFlags0(JNIEnv *env, jstring ifname);
145
146
static netif *enumInterfaces(JNIEnv *env);
147
static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs);
148
149
#if defined(AF_INET6)
150
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs);
151
#endif
152
153
static netif *addif(JNIEnv *env, int sock, const char *if_name, netif *ifs,
154
struct sockaddr *ifr_addrP,
155
struct sockaddr *ifr_broadaddrP,
156
int family, short prefix);
157
static void freeif(netif *ifs);
158
159
static int openSocket(JNIEnv *env, int proto);
160
static int openSocketWithFallback(JNIEnv *env, const char *ifname);
161
162
static short translateIPv4AddressToPrefix(struct sockaddr_in *addr);
163
static short translateIPv6AddressToPrefix(struct sockaddr_in6 *addr);
164
165
static int getIndex(int sock, const char *ifname);
166
static int getFlags(int sock, const char *ifname, int *flags);
167
static int getMacAddress(JNIEnv *env, const char *ifname,
168
const struct in_addr *addr, unsigned char *buf);
169
static int getMTU(JNIEnv *env, int sock, const char *ifname);
170
171
#if defined(__solaris__)
172
static int getMacFromDevice(JNIEnv *env, const char *ifname,
173
unsigned char *retbuf);
174
#endif
175
176
/******************* Java entry points *****************************/
177
178
/*
179
* Class: java_net_NetworkInterface
180
* Method: init
181
* Signature: ()V
182
*/
183
JNIEXPORT void JNICALL Java_java_net_NetworkInterface_init
184
(JNIEnv *env, jclass cls)
185
{
186
ni_class = (*env)->FindClass(env, "java/net/NetworkInterface");
187
CHECK_NULL(ni_class);
188
ni_class = (*env)->NewGlobalRef(env, ni_class);
189
CHECK_NULL(ni_class);
190
ni_nameID = (*env)->GetFieldID(env, ni_class, "name", "Ljava/lang/String;");
191
CHECK_NULL(ni_nameID);
192
ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
193
CHECK_NULL(ni_indexID);
194
ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs",
195
"[Ljava/net/InetAddress;");
196
CHECK_NULL(ni_addrsID);
197
ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings",
198
"[Ljava/net/InterfaceAddress;");
199
CHECK_NULL(ni_bindsID);
200
ni_descID = (*env)->GetFieldID(env, ni_class, "displayName",
201
"Ljava/lang/String;");
202
CHECK_NULL(ni_descID);
203
ni_virutalID = (*env)->GetFieldID(env, ni_class, "virtual", "Z");
204
CHECK_NULL(ni_virutalID);
205
ni_childsID = (*env)->GetFieldID(env, ni_class, "childs",
206
"[Ljava/net/NetworkInterface;");
207
CHECK_NULL(ni_childsID);
208
ni_parentID = (*env)->GetFieldID(env, ni_class, "parent",
209
"Ljava/net/NetworkInterface;");
210
CHECK_NULL(ni_parentID);
211
ni_ctrID = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
212
CHECK_NULL(ni_ctrID);
213
ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
214
CHECK_NULL(ni_ibcls);
215
ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
216
CHECK_NULL(ni_ibcls);
217
ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
218
CHECK_NULL(ni_ibctrID);
219
ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address",
220
"Ljava/net/InetAddress;");
221
CHECK_NULL(ni_ibaddressID);
222
ni_ib4broadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast",
223
"Ljava/net/Inet4Address;");
224
CHECK_NULL(ni_ib4broadcastID);
225
ni_ib4maskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
226
CHECK_NULL(ni_ib4maskID);
227
ni_defaultIndexID = (*env)->GetStaticFieldID(env, ni_class, "defaultIndex",
228
"I");
229
CHECK_NULL(ni_defaultIndexID);
230
231
initInetAddressIDs(env);
232
}
233
234
/*
235
* Class: java_net_NetworkInterface
236
* Method: getByName0
237
* Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
238
*/
239
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
240
(JNIEnv *env, jclass cls, jstring name)
241
{
242
netif *ifs, *curr;
243
jboolean isCopy;
244
const char* name_utf;
245
jobject obj = NULL;
246
247
if (name != NULL) {
248
name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
249
} else {
250
JNU_ThrowNullPointerException(env, "network interface name is NULL");
251
return NULL;
252
}
253
254
if (name_utf == NULL) {
255
if (!(*env)->ExceptionCheck(env))
256
JNU_ThrowOutOfMemoryError(env, NULL);
257
return NULL;
258
}
259
260
ifs = enumInterfaces(env);
261
if (ifs == NULL) {
262
(*env)->ReleaseStringUTFChars(env, name, name_utf);
263
return NULL;
264
}
265
266
// search the list of interfaces based on name
267
curr = ifs;
268
while (curr != NULL) {
269
if (strcmp(name_utf, curr->name) == 0) {
270
break;
271
}
272
curr = curr->next;
273
}
274
275
// if found create a NetworkInterface
276
if (curr != NULL) {
277
obj = createNetworkInterface(env, curr);
278
}
279
280
// release the UTF string and interface list
281
(*env)->ReleaseStringUTFChars(env, name, name_utf);
282
freeif(ifs);
283
284
return obj;
285
}
286
287
/*
288
* Class: java_net_NetworkInterface
289
* Method: getByIndex0
290
* Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
291
*/
292
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
293
(JNIEnv *env, jclass cls, jint index)
294
{
295
netif *ifs, *curr;
296
jobject obj = NULL;
297
298
if (index <= 0) {
299
return NULL;
300
}
301
302
ifs = enumInterfaces(env);
303
if (ifs == NULL) {
304
return NULL;
305
}
306
307
// search the list of interfaces based on index
308
curr = ifs;
309
while (curr != NULL) {
310
if (index == curr->index) {
311
break;
312
}
313
curr = curr->next;
314
}
315
316
// if found create a NetworkInterface
317
if (curr != NULL) {
318
obj = createNetworkInterface(env, curr);
319
}
320
321
// release the interface list
322
freeif(ifs);
323
324
return obj;
325
}
326
327
/*
328
* Class: java_net_NetworkInterface
329
* Method: getByInetAddress0
330
* Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
331
*/
332
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
333
(JNIEnv *env, jclass cls, jobject iaObj)
334
{
335
netif *ifs, *curr;
336
jobject obj = NULL;
337
jboolean match = JNI_FALSE;
338
#if defined(AF_INET6)
339
int family = getInetAddress_family(env, iaObj);
340
JNU_CHECK_EXCEPTION_RETURN(env, NULL);
341
342
if (family == IPv4) {
343
family = AF_INET;
344
} else if (family == IPv6) {
345
family = AF_INET6;
346
} else {
347
return NULL; // Invalid family
348
}
349
#else
350
int family = AF_INET;
351
#endif
352
ifs = enumInterfaces(env);
353
if (ifs == NULL) {
354
return NULL;
355
}
356
357
curr = ifs;
358
while (curr != NULL) {
359
netaddr *addrP = curr->addr;
360
361
// iterate through each address on the interface
362
while (addrP != NULL) {
363
364
if (family == addrP->family) {
365
if (family == AF_INET) {
366
int address1 = htonl(
367
((struct sockaddr_in *)addrP->addr)->sin_addr.s_addr);
368
int address2 = getInetAddress_addr(env, iaObj);
369
if ((*env)->ExceptionCheck(env)) {
370
goto cleanup;
371
}
372
if (address1 == address2) {
373
match = JNI_TRUE;
374
break;
375
}
376
}
377
#if defined(AF_INET6)
378
if (family == AF_INET6) {
379
jbyte *bytes = (jbyte *)&(
380
((struct sockaddr_in6*)addrP->addr)->sin6_addr);
381
jbyte caddr[16];
382
int i;
383
getInet6Address_ipaddress(env, iaObj, (char *)caddr);
384
i = 0;
385
while (i < 16) {
386
if (caddr[i] != bytes[i]) {
387
break;
388
}
389
i++;
390
}
391
if (i >= 16) {
392
match = JNI_TRUE;
393
break;
394
}
395
}
396
#endif
397
}
398
399
if (match) {
400
break;
401
}
402
addrP = addrP->next;
403
}
404
405
if (match) {
406
break;
407
}
408
curr = curr->next;
409
}
410
411
// if found create a NetworkInterface
412
if (match) {
413
obj = createNetworkInterface(env, curr);
414
}
415
416
cleanup:
417
// release the interface list
418
freeif(ifs);
419
420
return obj;
421
}
422
423
/*
424
* Class: java_net_NetworkInterface
425
* Method: getAll
426
* Signature: ()[Ljava/net/NetworkInterface;
427
*/
428
JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll
429
(JNIEnv *env, jclass cls)
430
{
431
netif *ifs, *curr;
432
jobjectArray netIFArr;
433
jint arr_index, ifCount;
434
435
ifs = enumInterfaces(env);
436
if (ifs == NULL) {
437
return NULL;
438
}
439
440
// count the interfaces
441
ifCount = 0;
442
curr = ifs;
443
while (curr != NULL) {
444
ifCount++;
445
curr = curr->next;
446
}
447
448
// allocate a NetworkInterface array
449
netIFArr = (*env)->NewObjectArray(env, ifCount, cls, NULL);
450
if (netIFArr == NULL) {
451
freeif(ifs);
452
return NULL;
453
}
454
455
// iterate through the interfaces, create a NetworkInterface instance
456
// for each array element and populate the object
457
curr = ifs;
458
arr_index = 0;
459
while (curr != NULL) {
460
jobject netifObj;
461
462
netifObj = createNetworkInterface(env, curr);
463
if (netifObj == NULL) {
464
freeif(ifs);
465
return NULL;
466
}
467
468
// put the NetworkInterface into the array
469
(*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
470
471
curr = curr->next;
472
}
473
474
// release the interface list
475
freeif(ifs);
476
477
return netIFArr;
478
}
479
480
/*
481
* Class: java_net_NetworkInterface
482
* Method: isUp0
483
* Signature: (Ljava/lang/String;I)Z
484
*/
485
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0
486
(JNIEnv *env, jclass cls, jstring name, jint index)
487
{
488
int ret = getFlags0(env, name);
489
return ((ret & IFF_UP) && (ret & IFF_RUNNING)) ? JNI_TRUE : JNI_FALSE;
490
}
491
492
/*
493
* Class: java_net_NetworkInterface
494
* Method: isP2P0
495
* Signature: (Ljava/lang/String;I)Z
496
*/
497
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0
498
(JNIEnv *env, jclass cls, jstring name, jint index)
499
{
500
int ret = getFlags0(env, name);
501
return (ret & IFF_POINTOPOINT) ? JNI_TRUE : JNI_FALSE;
502
}
503
504
/*
505
* Class: java_net_NetworkInterface
506
* Method: isLoopback0
507
* Signature: (Ljava/lang/String;I)Z
508
*/
509
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0
510
(JNIEnv *env, jclass cls, jstring name, jint index)
511
{
512
int ret = getFlags0(env, name);
513
return (ret & IFF_LOOPBACK) ? JNI_TRUE : JNI_FALSE;
514
}
515
516
/*
517
* Class: java_net_NetworkInterface
518
* Method: supportsMulticast0
519
* Signature: (Ljava/lang/String;I)Z
520
*/
521
JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0
522
(JNIEnv *env, jclass cls, jstring name, jint index)
523
{
524
int ret = getFlags0(env, name);
525
return (ret & IFF_MULTICAST) ? JNI_TRUE : JNI_FALSE;
526
}
527
528
/*
529
* Class: java_net_NetworkInterface
530
* Method: getMacAddr0
531
* Signature: ([bLjava/lang/String;I)[b
532
*/
533
JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0
534
(JNIEnv *env, jclass cls, jbyteArray addrArray, jstring name, jint index)
535
{
536
jint addr;
537
jbyte caddr[4];
538
struct in_addr iaddr;
539
jbyteArray ret = NULL;
540
unsigned char mac[16];
541
int len;
542
jboolean isCopy;
543
const char *name_utf;
544
545
if (name != NULL) {
546
name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
547
} else {
548
JNU_ThrowNullPointerException(env, "network interface name is NULL");
549
return NULL;
550
}
551
552
if (name_utf == NULL) {
553
if (!(*env)->ExceptionCheck(env))
554
JNU_ThrowOutOfMemoryError(env, NULL);
555
return NULL;
556
}
557
558
if (!IS_NULL(addrArray)) {
559
(*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
560
addr = ((caddr[0]<<24) & 0xff000000);
561
addr |= ((caddr[1] <<16) & 0xff0000);
562
addr |= ((caddr[2] <<8) & 0xff00);
563
addr |= (caddr[3] & 0xff);
564
iaddr.s_addr = htonl(addr);
565
len = getMacAddress(env, name_utf, &iaddr, mac);
566
} else {
567
len = getMacAddress(env, name_utf, NULL, mac);
568
}
569
570
if (len > 0) {
571
ret = (*env)->NewByteArray(env, len);
572
if (!IS_NULL(ret)) {
573
(*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *)(mac));
574
}
575
}
576
577
// release the UTF string and interface list
578
(*env)->ReleaseStringUTFChars(env, name, name_utf);
579
580
return ret;
581
}
582
583
/*
584
* Class: java_net_NetworkInterface
585
* Method: getMTU0
586
* Signature: ([bLjava/lang/String;I)I
587
*/
588
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0
589
(JNIEnv *env, jclass cls, jstring name, jint index)
590
{
591
jboolean isCopy;
592
int sock, ret = -1;
593
const char* name_utf = NULL;
594
595
if (name != NULL) {
596
name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
597
} else {
598
JNU_ThrowNullPointerException(env, "network interface name is NULL");
599
return ret;
600
}
601
602
if (name_utf == NULL) {
603
if (!(*env)->ExceptionCheck(env))
604
JNU_ThrowOutOfMemoryError(env, NULL);
605
return ret;
606
}
607
608
if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
609
(*env)->ReleaseStringUTFChars(env, name, name_utf);
610
return JNI_FALSE;
611
}
612
613
ret = getMTU(env, sock, name_utf);
614
615
(*env)->ReleaseStringUTFChars(env, name, name_utf);
616
617
close(sock);
618
return ret;
619
}
620
621
/*** Private methods definitions ****/
622
623
static int getFlags0(JNIEnv *env, jstring name) {
624
jboolean isCopy;
625
int ret, sock, flags = 0;
626
const char *name_utf;
627
628
if (name != NULL) {
629
name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
630
} else {
631
JNU_ThrowNullPointerException(env, "network interface name is NULL");
632
return -1;
633
}
634
635
if (name_utf == NULL) {
636
if (!(*env)->ExceptionCheck(env))
637
JNU_ThrowOutOfMemoryError(env, NULL);
638
return -1;
639
}
640
if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
641
(*env)->ReleaseStringUTFChars(env, name, name_utf);
642
return -1;
643
}
644
645
ret = getFlags(sock, name_utf, &flags);
646
647
close(sock);
648
(*env)->ReleaseStringUTFChars(env, name, name_utf);
649
650
if (ret < 0) {
651
NET_ThrowByNameWithLastError
652
(env, JNU_JAVANETPKG "SocketException", "getFlags() failed");
653
return -1;
654
}
655
656
return flags;
657
}
658
659
/*
660
* Creates a NetworkInterface object, populates the name, the index, and
661
* populates the InetAddress array based on the IP addresses for this
662
* interface.
663
*/
664
static jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
665
jobject netifObj;
666
jobject name;
667
jobjectArray addrArr;
668
jobjectArray bindArr;
669
jobjectArray childArr;
670
netaddr *addrs;
671
jint addr_index, addr_count, bind_index;
672
jint child_count, child_index;
673
netaddr *addrP;
674
netif *childP;
675
jobject tmp;
676
677
// create a NetworkInterface object and populate it
678
netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
679
CHECK_NULL_RETURN(netifObj, NULL);
680
name = (*env)->NewStringUTF(env, ifs->name);
681
CHECK_NULL_RETURN(name, NULL);
682
(*env)->SetObjectField(env, netifObj, ni_nameID, name);
683
(*env)->SetObjectField(env, netifObj, ni_descID, name);
684
(*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
685
(*env)->SetBooleanField(env, netifObj, ni_virutalID,
686
ifs->virtual ? JNI_TRUE : JNI_FALSE);
687
688
// count the number of addresses on this interface
689
addr_count = 0;
690
addrP = ifs->addr;
691
while (addrP != NULL) {
692
addr_count++;
693
addrP = addrP->next;
694
}
695
696
// create the array of InetAddresses
697
addrArr = (*env)->NewObjectArray(env, addr_count, ia_class, NULL);
698
if (addrArr == NULL) {
699
return NULL;
700
}
701
702
bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
703
if (bindArr == NULL) {
704
return NULL;
705
}
706
addrP = ifs->addr;
707
addr_index = 0;
708
bind_index = 0;
709
while (addrP != NULL) {
710
jobject iaObj = NULL;
711
jobject ibObj = NULL;
712
713
if (addrP->family == AF_INET) {
714
iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
715
if (iaObj) {
716
setInetAddress_addr(env, iaObj, htonl(
717
((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
718
JNU_CHECK_EXCEPTION_RETURN(env, NULL);
719
} else {
720
return NULL;
721
}
722
ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
723
if (ibObj) {
724
(*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
725
if (addrP->brdcast) {
726
jobject ia2Obj = NULL;
727
ia2Obj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
728
if (ia2Obj) {
729
setInetAddress_addr(env, ia2Obj, htonl(
730
((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
731
JNU_CHECK_EXCEPTION_RETURN(env, NULL);
732
(*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
733
} else {
734
return NULL;
735
}
736
}
737
(*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
738
(*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
739
} else {
740
return NULL;
741
}
742
}
743
#if defined(AF_INET6)
744
if (addrP->family == AF_INET6) {
745
int scope=0;
746
iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
747
if (iaObj) {
748
jboolean ret = setInet6Address_ipaddress(env, iaObj,
749
(char *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
750
if (ret == JNI_FALSE) {
751
return NULL;
752
}
753
754
scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
755
756
if (scope != 0) { /* zero is default value, no need to set */
757
setInet6Address_scopeid(env, iaObj, scope);
758
setInet6Address_scopeifname(env, iaObj, netifObj);
759
}
760
} else {
761
return NULL;
762
}
763
ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
764
if (ibObj) {
765
(*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
766
(*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
767
(*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
768
} else {
769
return NULL;
770
}
771
}
772
#endif
773
774
(*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
775
addrP = addrP->next;
776
}
777
778
// see if there is any virtual interface attached to this one.
779
child_count = 0;
780
childP = ifs->childs;
781
while (childP) {
782
child_count++;
783
childP = childP->next;
784
}
785
786
childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
787
if (childArr == NULL) {
788
return NULL;
789
}
790
791
// create the NetworkInterface instances for the sub-interfaces as well
792
child_index = 0;
793
childP = ifs->childs;
794
while(childP) {
795
tmp = createNetworkInterface(env, childP);
796
if (tmp == NULL) {
797
return NULL;
798
}
799
(*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
800
(*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
801
childP = childP->next;
802
}
803
(*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
804
(*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
805
(*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
806
807
// return the NetworkInterface
808
return netifObj;
809
}
810
811
/*
812
* Enumerates all interfaces
813
*/
814
static netif *enumInterfaces(JNIEnv *env) {
815
netif *ifs = NULL;
816
int sock;
817
818
sock = openSocket(env, AF_INET);
819
if (sock < 0 && (*env)->ExceptionOccurred(env)) {
820
return NULL;
821
}
822
823
// enumerate IPv4 addresses
824
ifs = enumIPv4Interfaces(env, sock, NULL);
825
close(sock);
826
827
// return partial list if an exception occurs in the middle of process ???
828
if (ifs == NULL && (*env)->ExceptionOccurred(env)) {
829
return NULL;
830
}
831
832
// If IPv6 is available then enumerate IPv6 addresses.
833
#if defined(AF_INET6)
834
// User can disable ipv6 explicitly by -Djava.net.preferIPv4Stack=true,
835
// so we have to call ipv6_available()
836
if (ipv6_available()) {
837
sock = openSocket(env, AF_INET6);
838
if (sock < 0 && (*env)->ExceptionOccurred(env)) {
839
freeif(ifs);
840
return NULL;
841
}
842
843
ifs = enumIPv6Interfaces(env, sock, ifs);
844
close(sock);
845
846
if ((*env)->ExceptionOccurred(env)) {
847
freeif(ifs);
848
return NULL;
849
}
850
}
851
#endif
852
853
return ifs;
854
}
855
856
/*
857
* Frees an interface list (including any attached addresses).
858
*/
859
static void freeif(netif *ifs) {
860
netif *currif = ifs;
861
netif *child = NULL;
862
863
while (currif != NULL) {
864
netaddr *addrP = currif->addr;
865
while (addrP != NULL) {
866
netaddr *next = addrP->next;
867
free(addrP);
868
addrP = next;
869
}
870
871
// don't forget to free the sub-interfaces
872
if (currif->childs != NULL) {
873
freeif(currif->childs);
874
}
875
876
ifs = currif->next;
877
free(currif);
878
currif = ifs;
879
}
880
}
881
882
static netif *addif(JNIEnv *env, int sock, const char *if_name, netif *ifs,
883
struct sockaddr *ifr_addrP,
884
struct sockaddr *ifr_broadaddrP,
885
int family, short prefix)
886
{
887
netif *currif = ifs, *parent;
888
netaddr *addrP;
889
char name[IFNAMESIZE], vname[IFNAMESIZE];
890
char *name_colonP;
891
int isVirtual = 0;
892
int addr_size;
893
894
// If the interface name is a logical interface then we remove the unit
895
// number so that we have the physical interface (eg: hme0:1 -> hme0).
896
// NetworkInterface currently doesn't have any concept of physical vs.
897
// logical interfaces.
898
strncpy(name, if_name, IFNAMESIZE);
899
name[IFNAMESIZE - 1] = '\0';
900
*vname = 0;
901
902
// Create and populate the netaddr node. If allocation fails
903
// return an un-updated list.
904
905
// Allocate for addr and brdcast at once
906
907
#if defined(AF_INET6)
908
addr_size = (family == AF_INET) ? sizeof(struct sockaddr_in)
909
: sizeof(struct sockaddr_in6);
910
#else
911
addr_size = sizeof(struct sockaddr_in);
912
#endif
913
914
CHECKED_MALLOC3(addrP, netaddr *, sizeof(netaddr) + 2 * addr_size);
915
addrP->addr = (struct sockaddr *)((char *)addrP + sizeof(netaddr));
916
memcpy(addrP->addr, ifr_addrP, addr_size);
917
918
addrP->family = family;
919
addrP->mask = prefix;
920
addrP->next = 0;
921
922
// for IPv4 add broadcast address
923
if (family == AF_INET && ifr_broadaddrP != NULL) {
924
addrP->brdcast = (struct sockaddr *)
925
((char *)addrP + sizeof(netaddr) + addr_size);
926
memcpy(addrP->brdcast, ifr_broadaddrP, addr_size);
927
} else {
928
addrP->brdcast = NULL;
929
}
930
931
// Deal with virtual interface with colon notation e.g. eth0:1
932
name_colonP = strchr(name, ':');
933
if (name_colonP != NULL) {
934
int flags = 0;
935
// This is a virtual interface. If we are able to access the parent
936
// we need to create a new entry if it doesn't exist yet *and* update
937
// the 'parent' interface with the new records.
938
*name_colonP = 0;
939
if (getFlags(sock, name, &flags) < 0 || flags < 0) {
940
// failed to access parent interface do not create parent.
941
// We are a virtual interface with no parent.
942
isVirtual = 1;
943
*name_colonP = ':';
944
} else {
945
// Got access to parent, so create it if necessary.
946
// Save original name to vname and truncate name by ':'
947
memcpy(vname, name, sizeof(vname));
948
vname[name_colonP - name] = ':';
949
}
950
}
951
952
// Check if this is a "new" interface. Use the interface name for
953
// matching because index isn't supported on Solaris 2.6 & 7.
954
while (currif != NULL) {
955
if (strcmp(name, currif->name) == 0) {
956
break;
957
}
958
currif = currif->next;
959
}
960
961
// If "new" then create a netif structure and insert it into the list.
962
if (currif == NULL) {
963
CHECKED_MALLOC3(currif, netif *, sizeof(netif) + IFNAMESIZE);
964
currif->name = (char *)currif + sizeof(netif);
965
strncpy(currif->name, name, IFNAMESIZE);
966
currif->name[IFNAMESIZE - 1] = '\0';
967
currif->index = getIndex(sock, name);
968
currif->addr = NULL;
969
currif->childs = NULL;
970
currif->virtual = isVirtual;
971
currif->next = ifs;
972
ifs = currif;
973
}
974
975
// Finally insert the address on the interface
976
addrP->next = currif->addr;
977
currif->addr = addrP;
978
979
parent = currif;
980
981
// Deal with the virtual interface now.
982
if (vname[0]) {
983
netaddr *tmpaddr;
984
985
currif = parent->childs;
986
987
while (currif != NULL) {
988
if (strcmp(vname, currif->name) == 0) {
989
break;
990
}
991
currif = currif->next;
992
}
993
994
if (currif == NULL) {
995
CHECKED_MALLOC3(currif, netif *, sizeof(netif) + IFNAMESIZE);
996
currif->name = (char *)currif + sizeof(netif);
997
strncpy(currif->name, vname, IFNAMESIZE);
998
currif->name[IFNAMESIZE - 1] = '\0';
999
currif->index = getIndex(sock, vname);
1000
currif->addr = NULL; // Need to duplicate the addr entry?
1001
currif->virtual = 1;
1002
currif->childs = NULL;
1003
currif->next = parent->childs;
1004
parent->childs = currif;
1005
}
1006
1007
CHECKED_MALLOC3(tmpaddr, netaddr *, sizeof(netaddr) + 2 * addr_size);
1008
memcpy(tmpaddr, addrP, sizeof(netaddr));
1009
if (addrP->addr != NULL) {
1010
tmpaddr->addr = (struct sockaddr *)
1011
((char*)tmpaddr + sizeof(netaddr));
1012
memcpy(tmpaddr->addr, addrP->addr, addr_size);
1013
}
1014
1015
if (addrP->brdcast != NULL) {
1016
tmpaddr->brdcast = (struct sockaddr *)
1017
((char *)tmpaddr + sizeof(netaddr) + addr_size);
1018
memcpy(tmpaddr->brdcast, addrP->brdcast, addr_size);
1019
}
1020
1021
tmpaddr->next = currif->addr;
1022
currif->addr = tmpaddr;
1023
}
1024
1025
return ifs;
1026
}
1027
1028
/*
1029
* Determines the prefix value for an AF_INET subnet address.
1030
*/
1031
static short translateIPv4AddressToPrefix(struct sockaddr_in *addr) {
1032
short prefix = 0;
1033
unsigned int mask = ntohl(addr->sin_addr.s_addr);
1034
while (mask) {
1035
mask <<= 1;
1036
prefix++;
1037
}
1038
return prefix;
1039
}
1040
1041
/*
1042
* Determines the prefix value for an AF_INET6 subnet address.
1043
*/
1044
static short translateIPv6AddressToPrefix(struct sockaddr_in6 *addr) {
1045
short prefix = 0;
1046
u_char *addrBytes = (u_char *)&(addr->sin6_addr);
1047
unsigned int byte, bit;
1048
1049
for (byte = 0; byte < sizeof(struct in6_addr); byte++, prefix += 8) {
1050
if (addrBytes[byte] != 0xff) {
1051
break;
1052
}
1053
}
1054
if (byte != sizeof(struct in6_addr)) {
1055
for (bit = 7; bit != 0; bit--, prefix++) {
1056
if (!(addrBytes[byte] & (1 << bit))) {
1057
break;
1058
}
1059
}
1060
for (; bit != 0; bit--) {
1061
if (addrBytes[byte] & (1 << bit)) {
1062
prefix = 0;
1063
break;
1064
}
1065
}
1066
if (prefix > 0) {
1067
byte++;
1068
for (; byte < sizeof(struct in6_addr); byte++) {
1069
if (addrBytes[byte]) {
1070
prefix = 0;
1071
}
1072
}
1073
}
1074
}
1075
1076
return prefix;
1077
}
1078
1079
/*
1080
* Opens a socket for further ioct calls. proto is one of AF_INET or AF_INET6.
1081
*/
1082
static int openSocket(JNIEnv *env, int proto) {
1083
int sock;
1084
1085
if ((sock = JVM_Socket(proto, SOCK_DGRAM, 0)) < 0) {
1086
// If EPROTONOSUPPORT is returned it means we don't have
1087
// support for this proto so don't throw an exception.
1088
if (errno != EPROTONOSUPPORT) {
1089
NET_ThrowByNameWithLastError
1090
(env, JNU_JAVANETPKG "SocketException", "Socket creation failed");
1091
}
1092
return -1;
1093
}
1094
1095
return sock;
1096
}
1097
1098
/** Linux **/
1099
#if defined(__linux__)
1100
1101
#if defined(AF_INET6)
1102
/*
1103
* Opens a socket for further ioctl calls. Tries AF_INET socket first and
1104
* if it fails return AF_INET6 socket.
1105
*/
1106
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1107
int sock;
1108
1109
if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1110
if (errno == EPROTONOSUPPORT) {
1111
if ((sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1112
NET_ThrowByNameWithLastError
1113
(env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1114
return -1;
1115
}
1116
} else { // errno is not NOSUPPORT
1117
NET_ThrowByNameWithLastError
1118
(env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1119
return -1;
1120
}
1121
}
1122
1123
// Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or
1124
// IPv6 socket regardless of type of address of an interface.
1125
return sock;
1126
}
1127
#else
1128
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1129
return openSocket(env, AF_INET);
1130
}
1131
#endif
1132
1133
/*
1134
* Enumerates and returns all IPv4 interfaces on Linux.
1135
*/
1136
static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1137
struct ifconf ifc;
1138
struct ifreq *ifreqP;
1139
char *buf = NULL;
1140
unsigned i;
1141
1142
// do a dummy SIOCGIFCONF to determine the buffer size
1143
// SIOCGIFCOUNT doesn't work
1144
ifc.ifc_buf = NULL;
1145
if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1146
NET_ThrowByNameWithLastError
1147
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFCONF) failed");
1148
return ifs;
1149
}
1150
1151
// call SIOCGIFCONF to enumerate the interfaces
1152
CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
1153
ifc.ifc_buf = buf;
1154
if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1155
NET_ThrowByNameWithLastError
1156
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFCONF) failed");
1157
free(buf);
1158
return ifs;
1159
}
1160
1161
// iterate through each interface
1162
ifreqP = ifc.ifc_req;
1163
for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
1164
struct sockaddr addr, broadaddr, *broadaddrP = NULL;
1165
short prefix = 0;
1166
1167
// ignore non IPv4 addresses
1168
if (ifreqP->ifr_addr.sa_family != AF_INET) {
1169
continue;
1170
}
1171
1172
// save socket address
1173
memcpy(&addr, &(ifreqP->ifr_addr), sizeof(struct sockaddr));
1174
1175
// determine broadcast address, if applicable
1176
if ((ioctl(sock, SIOCGIFFLAGS, ifreqP) == 0) &&
1177
ifreqP->ifr_flags & IFF_BROADCAST) {
1178
1179
// restore socket address to ifreqP
1180
memcpy(&(ifreqP->ifr_addr), &addr, sizeof(struct sockaddr));
1181
1182
if (ioctl(sock, SIOCGIFBRDADDR, ifreqP) == 0) {
1183
memcpy(&broadaddr, &(ifreqP->ifr_broadaddr),
1184
sizeof(struct sockaddr));
1185
broadaddrP = &broadaddr;
1186
}
1187
}
1188
1189
// restore socket address to ifreqP
1190
memcpy(&(ifreqP->ifr_addr), &addr, sizeof(struct sockaddr));
1191
1192
// determine netmask
1193
if (ioctl(sock, SIOCGIFNETMASK, ifreqP) == 0) {
1194
prefix = translateIPv4AddressToPrefix(
1195
(struct sockaddr_in *)&(ifreqP->ifr_netmask));
1196
}
1197
1198
// add interface to the list
1199
ifs = addif(env, sock, ifreqP->ifr_name, ifs,
1200
&addr, broadaddrP, AF_INET, prefix);
1201
1202
// in case of exception, free interface list and buffer and return NULL
1203
if ((*env)->ExceptionOccurred(env)) {
1204
free(buf);
1205
freeif(ifs);
1206
return NULL;
1207
}
1208
}
1209
1210
// free buffer
1211
free(buf);
1212
return ifs;
1213
}
1214
1215
#if defined(AF_INET6)
1216
1217
/*
1218
* Enumerates and returns all IPv6 interfaces on Linux.
1219
*/
1220
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1221
FILE *f;
1222
char devname[21], addr6p[8][5];
1223
int prefix, scope, dad_status, if_idx;
1224
1225
if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
1226
while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
1227
addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1228
addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1229
&if_idx, &prefix, &scope, &dad_status, devname) != EOF) {
1230
1231
char addr6[40];
1232
struct sockaddr_in6 addr;
1233
1234
sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
1235
addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1236
addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
1237
1238
memset(&addr, 0, sizeof(struct sockaddr_in6));
1239
inet_pton(AF_INET6, addr6, (void*)addr.sin6_addr.s6_addr);
1240
1241
// set scope ID to interface index
1242
addr.sin6_scope_id = if_idx;
1243
1244
// add interface to the list
1245
ifs = addif(env, sock, devname, ifs, (struct sockaddr *)&addr,
1246
NULL, AF_INET6, (short)prefix);
1247
1248
// if an exception occurred then return the list as is
1249
if ((*env)->ExceptionOccurred(env)) {
1250
break;
1251
}
1252
}
1253
fclose(f);
1254
}
1255
return ifs;
1256
}
1257
1258
#endif /* AF_INET6 */
1259
1260
/*
1261
* Try to get the interface index.
1262
*/
1263
static int getIndex(int sock, const char *name) {
1264
struct ifreq if2;
1265
memset((char *)&if2, 0, sizeof(if2));
1266
strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);
1267
1268
if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
1269
return -1;
1270
}
1271
1272
return if2.ifr_ifindex;
1273
}
1274
1275
/*
1276
* Gets the Hardware address (usually MAC address) for the named interface.
1277
* On return puts the data in buf, and returns the length, in byte, of the
1278
* MAC address. Returns -1 if there is no hardware address on that interface.
1279
*/
1280
static int getMacAddress
1281
(JNIEnv *env, const char *ifname, const struct in_addr *addr,
1282
unsigned char *buf)
1283
{
1284
struct ifreq ifr;
1285
int i, sock;
1286
1287
if ((sock = openSocketWithFallback(env, ifname)) < 0) {
1288
return -1;
1289
}
1290
1291
memset((char *)&ifr, 0, sizeof(ifr));
1292
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
1293
if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
1294
NET_ThrowByNameWithLastError
1295
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFHWADDR) failed");
1296
close(sock);
1297
return -1;
1298
}
1299
1300
close(sock);
1301
memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
1302
1303
// all bytes to 0 means no hardware address
1304
for (i = 0; i < IFHWADDRLEN; i++) {
1305
if (buf[i] != 0)
1306
return IFHWADDRLEN;
1307
}
1308
1309
return -1;
1310
}
1311
1312
static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1313
struct ifreq if2;
1314
memset((char *)&if2, 0, sizeof(if2));
1315
strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1316
1317
if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
1318
NET_ThrowByNameWithLastError
1319
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFMTU) failed");
1320
return -1;
1321
}
1322
1323
return if2.ifr_mtu;
1324
}
1325
1326
static int getFlags(int sock, const char *ifname, int *flags) {
1327
struct ifreq if2;
1328
memset((char *)&if2, 0, sizeof(if2));
1329
strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1330
1331
if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
1332
return -1;
1333
}
1334
1335
if (sizeof(if2.ifr_flags) == sizeof(short)) {
1336
*flags = (if2.ifr_flags & 0xffff);
1337
} else {
1338
*flags = if2.ifr_flags;
1339
}
1340
return 0;
1341
}
1342
1343
#endif /* __linux__ */
1344
1345
/** AIX **/
1346
#if defined(_AIX)
1347
1348
#if defined(AF_INET6)
1349
/*
1350
* Opens a socket for further ioctl calls. Tries AF_INET socket first and
1351
* if it fails return AF_INET6 socket.
1352
*/
1353
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1354
int sock;
1355
1356
if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1357
if (errno == EPROTONOSUPPORT) {
1358
if ((sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1359
NET_ThrowByNameWithLastError
1360
(env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1361
return -1;
1362
}
1363
} else { // errno is not NOSUPPORT
1364
NET_ThrowByNameWithLastError
1365
(env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1366
return -1;
1367
}
1368
}
1369
1370
return sock;
1371
}
1372
#else
1373
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1374
return openSocket(env, AF_INET);
1375
}
1376
#endif
1377
1378
/*
1379
* Enumerates and returns all IPv4 interfaces on AIX.
1380
*/
1381
static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1382
struct ifconf ifc;
1383
struct ifreq *ifreqP;
1384
char *buf = NULL;
1385
unsigned i;
1386
1387
// call SIOCGSIZIFCONF to get the size of SIOCGIFCONF buffer
1388
if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
1389
NET_ThrowByNameWithLastError
1390
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGSIZIFCONF) failed");
1391
return ifs;
1392
}
1393
1394
// call CSIOCGIFCONF instead of SIOCGIFCONF where interface
1395
// records will always have sizeof(struct ifreq) length.
1396
// Be aware that only IPv4 data is complete this way.
1397
CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
1398
ifc.ifc_buf = buf;
1399
if (ioctl(sock, CSIOCGIFCONF, (char *)&ifc) < 0) {
1400
NET_ThrowByNameWithLastError
1401
(env, JNU_JAVANETPKG "SocketException", "ioctl(CSIOCGIFCONF) failed");
1402
free(buf);
1403
return ifs;
1404
}
1405
1406
// iterate through each interface
1407
ifreqP = ifc.ifc_req;
1408
for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
1409
struct sockaddr addr, broadaddr, *broadaddrP = NULL;
1410
short prefix = 0;
1411
1412
// ignore non IPv4 addresses
1413
if (ifreqP->ifr_addr.sa_family != AF_INET) {
1414
continue;
1415
}
1416
1417
// save socket address
1418
memcpy(&addr, &(ifreqP->ifr_addr), sizeof(struct sockaddr));
1419
1420
// determine broadcast address, if applicable
1421
if ((ioctl(sock, SIOCGIFFLAGS, ifreqP) == 0) &&
1422
ifreqP->ifr_flags & IFF_BROADCAST) {
1423
1424
// restore socket address to ifreqP
1425
memcpy(&(ifreqP->ifr_addr), &addr, sizeof(struct sockaddr));
1426
1427
if (ioctl(sock, SIOCGIFBRDADDR, ifreqP) == 0) {
1428
memcpy(&broadaddr, &(ifreqP->ifr_broadaddr),
1429
sizeof(struct sockaddr));
1430
broadaddrP = &broadaddr;
1431
}
1432
}
1433
1434
// restore socket address to ifreqP
1435
memcpy(&(ifreqP->ifr_addr), &addr, sizeof(struct sockaddr));
1436
1437
// determine netmask
1438
if (ioctl(sock, SIOCGIFNETMASK, ifreqP) == 0) {
1439
prefix = translateIPv4AddressToPrefix(
1440
(struct sockaddr_in *)&(ifreqP->ifr_addr));
1441
}
1442
1443
// add interface to the list
1444
ifs = addif(env, sock, ifreqP->ifr_name, ifs,
1445
&addr, broadaddrP, AF_INET, prefix);
1446
1447
// in case of exception, free interface list and buffer and return NULL
1448
if ((*env)->ExceptionOccurred(env)) {
1449
free(buf);
1450
freeif(ifs);
1451
return NULL;
1452
}
1453
}
1454
1455
// free buffer
1456
free(buf);
1457
return ifs;
1458
}
1459
1460
#if defined(AF_INET6)
1461
1462
/*
1463
* Enumerates and returns all IPv6 interfaces on AIX.
1464
*/
1465
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1466
struct ifconf ifc;
1467
struct ifreq *ifreqP;
1468
char *buf, *cp, *cplimit;
1469
1470
// call SIOCGSIZIFCONF to get size for SIOCGIFCONF buffer
1471
if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
1472
NET_ThrowByNameWithLastError
1473
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGSIZIFCONF) failed");
1474
return ifs;
1475
}
1476
1477
// call SIOCGIFCONF to enumerate the interfaces
1478
CHECKED_MALLOC3(buf, char *, ifc.ifc_len);
1479
ifc.ifc_buf = buf;
1480
if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
1481
NET_ThrowByNameWithLastError
1482
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFCONF) failed");
1483
free(buf);
1484
return ifs;
1485
}
1486
1487
// iterate through each interface
1488
ifreqP = ifc.ifc_req;
1489
cp = (char *)ifc.ifc_req;
1490
cplimit = cp + ifc.ifc_len;
1491
1492
for (; cp < cplimit;
1493
cp += (sizeof(ifreqP->ifr_name) +
1494
MAX((ifreqP->ifr_addr).sa_len, sizeof(ifreqP->ifr_addr))))
1495
{
1496
ifreqP = (struct ifreq *)cp;
1497
short prefix = 0;
1498
1499
// ignore non IPv6 addresses
1500
if (ifreqP->ifr_addr.sa_family != AF_INET6) {
1501
continue;
1502
}
1503
1504
// determine netmask
1505
struct in6_ifreq if6;
1506
memset((char *)&if6, 0, sizeof(if6));
1507
strncpy(if6.ifr_name, ifreqP->ifr_name, sizeof(if6.ifr_name) - 1);
1508
memcpy(&(if6.ifr_Addr), &(ifreqP->ifr_addr),
1509
sizeof(struct sockaddr_in6));
1510
if (ioctl(sock, SIOCGIFNETMASK6, (char *)&if6) >= 0) {
1511
prefix = translateIPv6AddressToPrefix(&(if6.ifr_Addr));
1512
}
1513
1514
// set scope ID to interface index
1515
((struct sockaddr_in6 *)&(ifreqP->ifr_addr))->sin6_scope_id =
1516
getIndex(sock, ifreqP->ifr_name);
1517
1518
// add interface to the list
1519
ifs = addif(env, sock, ifreqP->ifr_name, ifs,
1520
(struct sockaddr *)&(ifreqP->ifr_addr),
1521
NULL, AF_INET6, prefix);
1522
1523
// if an exception occurred then free the list
1524
if ((*env)->ExceptionOccurred(env)) {
1525
free(buf);
1526
freeif(ifs);
1527
return NULL;
1528
}
1529
}
1530
1531
// free buffer
1532
free(buf);
1533
return ifs;
1534
}
1535
1536
#endif /* AF_INET6 */
1537
1538
/*
1539
* Try to get the interface index.
1540
*/
1541
static int getIndex(int sock, const char *name) {
1542
int index = if_nametoindex(name);
1543
return (index == 0) ? -1 : index;
1544
}
1545
1546
/*
1547
* Gets the Hardware address (usually MAC address) for the named interface.
1548
* On return puts the data in buf, and returns the length, in byte, of the
1549
* MAC address. Returns -1 if there is no hardware address on that interface.
1550
*/
1551
static int getMacAddress
1552
(JNIEnv *env, const char *ifname, const struct in_addr *addr,
1553
unsigned char *buf)
1554
{
1555
int size;
1556
struct kinfo_ndd *nddp;
1557
void *end;
1558
1559
size = getkerninfo(KINFO_NDD, 0, 0, 0);
1560
if (size == 0) {
1561
return -1;
1562
}
1563
1564
if (size < 0) {
1565
perror("getkerninfo 1");
1566
return -1;
1567
}
1568
1569
nddp = (struct kinfo_ndd *)malloc(size);
1570
1571
if (!nddp) {
1572
JNU_ThrowOutOfMemoryError(env,
1573
"Network interface getMacAddress native buffer allocation failed");
1574
return -1;
1575
}
1576
1577
if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
1578
perror("getkerninfo 2");
1579
return -1;
1580
}
1581
1582
end = (void *)nddp + size;
1583
while ((void *)nddp < end) {
1584
if (!strcmp(nddp->ndd_alias, ifname) ||
1585
!strcmp(nddp->ndd_name, ifname)) {
1586
bcopy(nddp->ndd_addr, buf, 6);
1587
return 6;
1588
} else {
1589
nddp++;
1590
}
1591
}
1592
1593
return -1;
1594
}
1595
1596
static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1597
struct ifreq if2;
1598
memset((char *)&if2, 0, sizeof(if2));
1599
strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1600
1601
if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
1602
NET_ThrowByNameWithLastError
1603
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFMTU) failed");
1604
return -1;
1605
}
1606
1607
return if2.ifr_mtu;
1608
}
1609
1610
static int getFlags(int sock, const char *ifname, int *flags) {
1611
struct ifreq if2;
1612
memset((char *)&if2, 0, sizeof(if2));
1613
strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
1614
1615
if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
1616
return -1;
1617
}
1618
1619
if (sizeof(if2.ifr_flags) == sizeof(short)) {
1620
*flags = (if2.ifr_flags & 0xffff);
1621
} else {
1622
*flags = if2.ifr_flags;
1623
}
1624
return 0;
1625
}
1626
1627
#endif /* _AIX */
1628
1629
/** Solaris **/
1630
#if defined(__solaris__)
1631
1632
#if defined(AF_INET6)
1633
/*
1634
* Opens a socket for further ioctl calls. Tries AF_INET socket first and
1635
* if it fails return AF_INET6 socket.
1636
*/
1637
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1638
int sock, alreadyV6 = 0;
1639
struct lifreq if2;
1640
1641
if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1642
if (errno == EPROTONOSUPPORT) {
1643
if ((sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1644
NET_ThrowByNameWithLastError
1645
(env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1646
return -1;
1647
}
1648
alreadyV6 = 1;
1649
} else { // errno is not NOSUPPORT
1650
NET_ThrowByNameWithLastError
1651
(env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1652
return -1;
1653
}
1654
}
1655
1656
// Solaris requires that we have an IPv6 socket to query an interface
1657
// without an IPv4 address - check it here. POSIX 1 require the kernel to
1658
// return ENOTTY if the call is inappropriate for a device e.g. the NETMASK
1659
// for a device having IPv6 only address but not all devices follow the
1660
// standard so fall back on any error. It's not an ecologically friendly
1661
// gesture but more reliable.
1662
if (!alreadyV6) {
1663
memset((char *)&if2, 0, sizeof(if2));
1664
strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1665
if (ioctl(sock, SIOCGLIFNETMASK, (char *)&if2) < 0) {
1666
close(sock);
1667
if ((sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1668
NET_ThrowByNameWithLastError
1669
(env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1670
return -1;
1671
}
1672
}
1673
}
1674
1675
return sock;
1676
}
1677
#else
1678
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
1679
return openSocket(env, AF_INET);
1680
}
1681
#endif
1682
1683
/*
1684
* Enumerates and returns all IPv4 interfaces on Solaris.
1685
*/
1686
static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1687
struct lifconf ifc;
1688
struct lifreq *ifreqP;
1689
struct lifnum numifs;
1690
char *buf = NULL;
1691
unsigned i;
1692
1693
// call SIOCGLIFNUM to get the interface count
1694
numifs.lifn_family = AF_INET;
1695
numifs.lifn_flags = 0;
1696
if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
1697
NET_ThrowByNameWithLastError
1698
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFNUM) failed");
1699
return ifs;
1700
}
1701
1702
// call SIOCGLIFCONF to enumerate the interfaces
1703
ifc.lifc_len = numifs.lifn_count * sizeof(struct lifreq);
1704
CHECKED_MALLOC3(buf, char *, ifc.lifc_len);
1705
ifc.lifc_buf = buf;
1706
ifc.lifc_family = AF_INET;
1707
ifc.lifc_flags = 0;
1708
if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
1709
NET_ThrowByNameWithLastError
1710
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFCONF) failed");
1711
free(buf);
1712
return ifs;
1713
}
1714
1715
// iterate through each interface
1716
ifreqP = ifc.lifc_req;
1717
for (i = 0; i < numifs.lifn_count; i++, ifreqP++) {
1718
struct sockaddr addr, *broadaddrP = NULL;
1719
1720
// ignore non IPv4 addresses
1721
if (ifreqP->lifr_addr.ss_family != AF_INET) {
1722
continue;
1723
}
1724
1725
// save socket address
1726
memcpy(&addr, &(ifreqP->lifr_addr), sizeof(struct sockaddr));
1727
1728
// determine broadcast address, if applicable
1729
if ((ioctl(sock, SIOCGLIFFLAGS, ifreqP) == 0) &&
1730
ifreqP->lifr_flags & IFF_BROADCAST) {
1731
1732
// restore socket address to ifreqP
1733
memcpy(&(ifreqP->lifr_addr), &addr, sizeof(struct sockaddr));
1734
1735
// query broadcast address and set pointer to it
1736
if (ioctl(sock, SIOCGLIFBRDADDR, ifreqP) == 0) {
1737
broadaddrP = (struct sockaddr *)&(ifreqP->lifr_broadaddr);
1738
}
1739
}
1740
1741
// add to the list
1742
ifs = addif(env, sock, ifreqP->lifr_name, ifs,
1743
&addr, broadaddrP, AF_INET, (short)ifreqP->lifr_addrlen);
1744
1745
// if an exception occurred we return immediately
1746
if ((*env)->ExceptionOccurred(env)) {
1747
free(buf);
1748
return ifs;
1749
}
1750
}
1751
1752
// free buffer
1753
free(buf);
1754
return ifs;
1755
}
1756
1757
#if defined(AF_INET6)
1758
1759
/*
1760
* Enumerates and returns all IPv6 interfaces on Solaris.
1761
*/
1762
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1763
struct lifconf ifc;
1764
struct lifreq *ifreqP;
1765
struct lifnum numifs;
1766
char *buf = NULL;
1767
unsigned i;
1768
1769
// call SIOCGLIFNUM to get the interface count
1770
numifs.lifn_family = AF_INET6;
1771
numifs.lifn_flags = 0;
1772
if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
1773
NET_ThrowByNameWithLastError
1774
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFNUM) failed");
1775
return ifs;
1776
}
1777
1778
// call SIOCGLIFCONF to enumerate the interfaces
1779
ifc.lifc_len = numifs.lifn_count * sizeof(struct lifreq);
1780
CHECKED_MALLOC3(buf, char *, ifc.lifc_len);
1781
ifc.lifc_buf = buf;
1782
ifc.lifc_family = AF_INET6;
1783
ifc.lifc_flags = 0;
1784
if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
1785
NET_ThrowByNameWithLastError
1786
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFCONF) failed");
1787
free(buf);
1788
return ifs;
1789
}
1790
1791
// iterate through each interface
1792
ifreqP = ifc.lifc_req;
1793
for (i = 0; i < numifs.lifn_count; i++, ifreqP++) {
1794
1795
// ignore non IPv6 addresses
1796
if (ifreqP->lifr_addr.ss_family != AF_INET6) {
1797
continue;
1798
}
1799
1800
// set scope ID to interface index
1801
((struct sockaddr_in6 *)&(ifreqP->lifr_addr))->sin6_scope_id =
1802
getIndex(sock, ifreqP->lifr_name);
1803
1804
// add to the list
1805
ifs = addif(env, sock, ifreqP->lifr_name, ifs,
1806
(struct sockaddr *)&(ifreqP->lifr_addr),
1807
NULL, AF_INET6, (short)ifreqP->lifr_addrlen);
1808
1809
// if an exception occurred we return immediately
1810
if ((*env)->ExceptionOccurred(env)) {
1811
free(buf);
1812
return ifs;
1813
}
1814
}
1815
1816
// free buffer
1817
free(buf);
1818
return ifs;
1819
}
1820
1821
#endif /* AF_INET6 */
1822
1823
/*
1824
* Try to get the interface index.
1825
* (Not supported on Solaris 2.6 or 7)
1826
*/
1827
static int getIndex(int sock, const char *name) {
1828
struct lifreq if2;
1829
memset((char *)&if2, 0, sizeof(if2));
1830
strncpy(if2.lifr_name, name, sizeof(if2.lifr_name) - 1);
1831
1832
if (ioctl(sock, SIOCGLIFINDEX, (char *)&if2) < 0) {
1833
return -1;
1834
}
1835
1836
return if2.lifr_index;
1837
}
1838
1839
/*
1840
* Solaris specific DLPI code to get hardware address from a device.
1841
* Unfortunately, at least up to Solaris X, you have to have special
1842
* privileges (i.e. be root).
1843
*/
1844
static int getMacFromDevice
1845
(JNIEnv *env, const char *ifname, unsigned char *retbuf)
1846
{
1847
char style1dev[MAXPATHLEN];
1848
int fd;
1849
dl_phys_addr_req_t dlpareq;
1850
dl_phys_addr_ack_t *dlpaack;
1851
struct strbuf msg;
1852
char buf[128];
1853
int flags = 0;
1854
1855
// Device is in /dev. e.g.: /dev/bge0
1856
strcpy(style1dev, DEV_PREFIX);
1857
strcat(style1dev, ifname);
1858
if ((fd = open(style1dev, O_RDWR)) < 0) {
1859
// Can't open it. We probably are missing the privilege.
1860
// We'll have to try something else
1861
return 0;
1862
}
1863
1864
dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
1865
dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
1866
1867
msg.buf = (char *)&dlpareq;
1868
msg.len = DL_PHYS_ADDR_REQ_SIZE;
1869
1870
if (putmsg(fd, &msg, NULL, 0) < 0) {
1871
NET_ThrowByNameWithLastError
1872
(env, JNU_JAVANETPKG "SocketException", "putmsg() failed");
1873
return -1;
1874
}
1875
1876
dlpaack = (dl_phys_addr_ack_t *)buf;
1877
1878
msg.buf = (char *)buf;
1879
msg.len = 0;
1880
msg.maxlen = sizeof (buf);
1881
if (getmsg(fd, &msg, NULL, &flags) < 0) {
1882
NET_ThrowByNameWithLastError
1883
(env, JNU_JAVANETPKG "SocketException", "getmsg() failed");
1884
return -1;
1885
}
1886
1887
if (msg.len < DL_PHYS_ADDR_ACK_SIZE || dlpaack->dl_primitive != DL_PHYS_ADDR_ACK) {
1888
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
1889
"Couldn't obtain phys addr\n");
1890
return -1;
1891
}
1892
1893
memcpy(retbuf, &buf[dlpaack->dl_addr_offset], dlpaack->dl_addr_length);
1894
return dlpaack->dl_addr_length;
1895
}
1896
1897
/*
1898
* Gets the Hardware address (usually MAC address) for the named interface.
1899
* On return puts the data in buf, and returns the length, in byte, of the
1900
* MAC address. Returns -1 if there is no hardware address on that interface.
1901
*/
1902
static int getMacAddress
1903
(JNIEnv *env, const char *ifname, const struct in_addr *addr,
1904
unsigned char *buf)
1905
{
1906
struct lifreq if2;
1907
int len, i, sock;
1908
1909
if ((sock = openSocketWithFallback(env, ifname)) < 0) {
1910
return -1;
1911
}
1912
1913
// First, try the new (S11) SIOCGLIFHWADDR ioctl(). If that fails
1914
// try the old way.
1915
memset((char *)&if2, 0, sizeof(if2));
1916
strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1917
1918
if (ioctl(sock, SIOCGLIFHWADDR, &if2) != -1) {
1919
struct sockaddr_dl *sp;
1920
sp = (struct sockaddr_dl *)&if2.lifr_addr;
1921
memcpy(buf, &sp->sdl_data[0], sp->sdl_alen);
1922
close(sock);
1923
return sp->sdl_alen;
1924
}
1925
1926
// On Solaris we have to use DLPI, but it will only work if we have
1927
// privileged access (i.e. root). If that fails, we try a lookup
1928
// in the ARP table, which requires an IPv4 address.
1929
if (((len = getMacFromDevice(env, ifname, buf)) == 0) && (addr != NULL)) {
1930
struct arpreq arpreq;
1931
struct sockaddr_in *sin;
1932
struct sockaddr_in ipAddr;
1933
1934
len = 6; //???
1935
1936
sin = (struct sockaddr_in *)&arpreq.arp_pa;
1937
memset((char *)&arpreq, 0, sizeof(struct arpreq));
1938
ipAddr.sin_port = 0;
1939
ipAddr.sin_family = AF_INET;
1940
memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
1941
memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
1942
arpreq.arp_flags= ATF_PUBL;
1943
1944
if (ioctl(sock, SIOCGARP, &arpreq) < 0) {
1945
close(sock);
1946
return -1;
1947
}
1948
1949
memcpy(buf, &arpreq.arp_ha.sa_data[0], len);
1950
}
1951
close(sock);
1952
1953
// all bytes to 0 means no hardware address
1954
for (i = 0; i < len; i++) {
1955
if (buf[i] != 0)
1956
return len;
1957
}
1958
1959
return -1;
1960
}
1961
1962
static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1963
struct lifreq if2;
1964
memset((char *)&if2, 0, sizeof(if2));
1965
strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1966
1967
if (ioctl(sock, SIOCGLIFMTU, (char *)&if2) < 0) {
1968
NET_ThrowByNameWithLastError
1969
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFMTU) failed");
1970
return -1;
1971
}
1972
1973
return if2.lifr_mtu;
1974
}
1975
1976
static int getFlags(int sock, const char *ifname, int *flags) {
1977
struct lifreq if2;
1978
memset((char *)&if2, 0, sizeof(if2));
1979
strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
1980
1981
if (ioctl(sock, SIOCGLIFFLAGS, (char *)&if2) < 0) {
1982
return -1;
1983
}
1984
1985
*flags = if2.lifr_flags;
1986
return 0;
1987
}
1988
1989
#endif /* __solaris__ */
1990
1991
/** BSD **/
1992
#if defined(_ALLBSD_SOURCE)
1993
1994
#if defined(AF_INET6)
1995
/*
1996
* Opens a socket for further ioctl calls. Tries AF_INET socket first and
1997
* if it fails return AF_INET6 socket.
1998
*/
1999
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
2000
int sock;
2001
2002
if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
2003
if (errno == EPROTONOSUPPORT) {
2004
if ((sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
2005
NET_ThrowByNameWithLastError
2006
(env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
2007
return -1;
2008
}
2009
} else { // errno is not NOSUPPORT
2010
NET_ThrowByNameWithLastError
2011
(env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
2012
return -1;
2013
}
2014
}
2015
2016
return sock;
2017
}
2018
#else
2019
static int openSocketWithFallback(JNIEnv *env, const char *ifname) {
2020
return openSocket(env, AF_INET);
2021
}
2022
#endif
2023
2024
/*
2025
* Enumerates and returns all IPv4 interfaces on BSD.
2026
*/
2027
static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
2028
struct ifaddrs *ifa, *origifa;
2029
2030
if (getifaddrs(&origifa) != 0) {
2031
NET_ThrowByNameWithLastError
2032
(env, JNU_JAVANETPKG "SocketException", "getifaddrs() failed");
2033
return ifs;
2034
}
2035
2036
for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
2037
struct sockaddr *broadaddrP = NULL;
2038
2039
// ignore non IPv4 addresses
2040
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
2041
continue;
2042
2043
// set ifa_broadaddr, if there is one
2044
if ((ifa->ifa_flags & IFF_POINTOPOINT) == 0 &&
2045
ifa->ifa_flags & IFF_BROADCAST) {
2046
broadaddrP = ifa->ifa_dstaddr;
2047
}
2048
2049
// add interface to the list
2050
ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr,
2051
broadaddrP, AF_INET,
2052
translateIPv4AddressToPrefix((struct sockaddr_in *)
2053
ifa->ifa_netmask));
2054
2055
// if an exception occurred then free the list
2056
if ((*env)->ExceptionOccurred(env)) {
2057
freeifaddrs(origifa);
2058
freeif(ifs);
2059
return NULL;
2060
}
2061
}
2062
2063
// free ifaddrs buffer
2064
freeifaddrs(origifa);
2065
return ifs;
2066
}
2067
2068
#if defined(AF_INET6)
2069
2070
/*
2071
* Enumerates and returns all IPv6 interfaces on BSD.
2072
*/
2073
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
2074
struct ifaddrs *ifa, *origifa;
2075
2076
if (getifaddrs(&origifa) != 0) {
2077
NET_ThrowByNameWithLastError
2078
(env, JNU_JAVANETPKG "SocketException", "getifaddrs() failed");
2079
return ifs;
2080
}
2081
2082
for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
2083
// ignore non IPv6 addresses
2084
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6)
2085
continue;
2086
2087
// set scope ID to interface index
2088
((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id =
2089
getIndex(sock, ifa->ifa_name);
2090
2091
// add interface to the list
2092
ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, NULL,
2093
AF_INET6,
2094
translateIPv6AddressToPrefix((struct sockaddr_in6 *)
2095
ifa->ifa_netmask));
2096
2097
// if an exception occurred then free the list
2098
if ((*env)->ExceptionOccurred(env)) {
2099
freeifaddrs(origifa);
2100
freeif(ifs);
2101
return NULL;
2102
}
2103
}
2104
2105
// free ifaddrs buffer
2106
freeifaddrs(origifa);
2107
return ifs;
2108
}
2109
2110
#endif /* AF_INET6 */
2111
2112
/*
2113
* Try to get the interface index.
2114
*/
2115
static int getIndex(int sock, const char *name) {
2116
#if !defined(__FreeBSD__)
2117
int index = if_nametoindex(name);
2118
return (index == 0) ? -1 : index;
2119
#else
2120
struct ifreq if2;
2121
memset((char *)&if2, 0, sizeof(if2));
2122
strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);
2123
2124
if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
2125
return -1;
2126
}
2127
2128
return if2.ifr_index;
2129
#endif
2130
}
2131
2132
/*
2133
* Gets the Hardware address (usually MAC address) for the named interface.
2134
* On return puts the data in buf, and returns the length, in byte, of the
2135
* MAC address. Returns -1 if there is no hardware address on that interface.
2136
*/
2137
static int getMacAddress
2138
(JNIEnv *env, const char *ifname, const struct in_addr *addr,
2139
unsigned char *buf)
2140
{
2141
struct ifaddrs *ifa0, *ifa;
2142
struct sockaddr *saddr;
2143
int i;
2144
2145
// grab the interface list
2146
if (!getifaddrs(&ifa0)) {
2147
// cycle through the interfaces
2148
for (i = 0, ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next, i++) {
2149
saddr = ifa->ifa_addr;
2150
// link layer contains the MAC address
2151
if (saddr->sa_family == AF_LINK && !strcmp(ifname, ifa->ifa_name)) {
2152
struct sockaddr_dl *sadl = (struct sockaddr_dl *) saddr;
2153
// check the address has the correct length
2154
if (sadl->sdl_alen == ETHER_ADDR_LEN) {
2155
memcpy(buf, (sadl->sdl_data + sadl->sdl_nlen), ETHER_ADDR_LEN);
2156
freeifaddrs(ifa0);
2157
return ETHER_ADDR_LEN;
2158
}
2159
}
2160
}
2161
freeifaddrs(ifa0);
2162
}
2163
2164
return -1;
2165
}
2166
2167
static int getMTU(JNIEnv *env, int sock, const char *ifname) {
2168
struct ifreq if2;
2169
memset((char *)&if2, 0, sizeof(if2));
2170
strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
2171
2172
if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
2173
NET_ThrowByNameWithLastError
2174
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFMTU) failed");
2175
return -1;
2176
}
2177
2178
return if2.ifr_mtu;
2179
}
2180
2181
static int getFlags(int sock, const char *ifname, int *flags) {
2182
struct ifreq if2;
2183
memset((char *)&if2, 0, sizeof(if2));
2184
strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
2185
2186
if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
2187
return -1;
2188
}
2189
2190
if (sizeof(if2.ifr_flags) == sizeof(short)) {
2191
*flags = (if2.ifr_flags & 0xffff);
2192
} else {
2193
*flags = if2.ifr_flags;
2194
}
2195
return 0;
2196
}
2197
#endif /* _ALLBSD_SOURCE */
2198
2199