annotate src/solaris/native/java/net/NetworkInterface.c @ 2605:25050030a320

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