annotate src/solaris/native/java/net/NetworkInterface.c @ 6030:eefd9678efbd

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