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