annotate src/solaris/native/java/net/NetworkInterface.c @ 3909:272483f6650b

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