annotate src/solaris/native/java/net/NetworkInterface.c @ 2545:887e525597f8

6931566: NetworkInterface is not working when interface name is more than 15 characters long Summary: Separate Linux and Solaris code, use lifreq under Solaris Reviewed-by: chegar
author dsamersoff
date Wed, 23 Jun 2010 17:25:50 +0400
parents 00cd9dc3c2b5
children a7f8f269f741
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);
dsamersoff@2545 132 static int getIndex(JNIEnv *env, 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 sock = openSocket(env, AF_INET6);
dsamersoff@2545 757 if (sock < 0 && (*env)->ExceptionOccurred(env)) {
dsamersoff@2545 758 freeif(ifs);
dsamersoff@2545 759 return NULL;
dsamersoff@2545 760 }
dsamersoff@2545 761
dsamersoff@2545 762 ifs = enumIPv6Interfaces(env, sock, ifs);
dsamersoff@2545 763 close(sock);
duke@0 764
duke@0 765 if ((*env)->ExceptionOccurred(env)) {
duke@0 766 freeif(ifs);
duke@0 767 return NULL;
duke@0 768 }
duke@0 769 #endif
duke@0 770
duke@0 771 return ifs;
duke@0 772 }
duke@0 773
dsamersoff@2545 774 #define CHECKED_MALLOC3(_pointer,_type,_size) \
dsamersoff@2545 775 do{ \
dsamersoff@2545 776 _pointer = (_type)malloc( _size ); \
dsamersoff@2545 777 if (_pointer == NULL) { \
dsamersoff@2545 778 JNU_ThrowOutOfMemoryError(env, "heap allocation failed"); \
dsamersoff@2545 779 return ifs; /* return untouched list */ \
dsamersoff@2545 780 } \
dsamersoff@2545 781 } while(0)
duke@0 782
duke@0 783
duke@0 784 /*
duke@0 785 * Free an interface list (including any attached addresses)
duke@0 786 */
duke@0 787 void freeif(netif *ifs) {
duke@0 788 netif *currif = ifs;
dsamersoff@2545 789 netif *child = NULL;
duke@0 790
duke@0 791 while (currif != NULL) {
duke@0 792 netaddr *addrP = currif->addr;
duke@0 793 while (addrP != NULL) {
duke@0 794 netaddr *next = addrP->next;
duke@0 795 free(addrP);
duke@0 796 addrP = next;
dsamersoff@2545 797 }
duke@0 798
dsamersoff@2545 799 /*
dsamersoff@2545 800 * Don't forget to free the sub-interfaces.
dsamersoff@2545 801 */
dsamersoff@2545 802 if (currif->childs != NULL) {
dsamersoff@2545 803 freeif(currif->childs);
dsamersoff@2545 804 }
duke@0 805
dsamersoff@2545 806 ifs = currif->next;
dsamersoff@2545 807 free(currif);
dsamersoff@2545 808 currif = ifs;
duke@0 809 }
duke@0 810 }
duke@0 811
dsamersoff@2545 812 netif *addif(JNIEnv *env, int sock, const char * if_name, netif *ifs, struct sockaddr* ifr_addrP, int family, short prefix) {
dsamersoff@2545 813 netif *currif = ifs, *parent;
duke@0 814 netaddr *addrP;
dsamersoff@2545 815
dsamersoff@2545 816 #ifdef __solaris__
dsamersoff@2545 817 char name[LIFNAMSIZ], vname[LIFNAMSIZ];
dsamersoff@2545 818 #else
dsamersoff@2545 819 char name[IFNAMSIZ], vname[IFNAMSIZ];
dsamersoff@2545 820 #endif
dsamersoff@2545 821
dsamersoff@2545 822 char *name_colonP;
dsamersoff@2545 823 int mask;
duke@0 824 int isVirtual = 0;
dsamersoff@2545 825 int addr_size;
duke@0 826
duke@0 827 /*
duke@0 828 * If the interface name is a logical interface then we
duke@0 829 * remove the unit number so that we have the physical
duke@0 830 * interface (eg: hme0:1 -> hme0). NetworkInterface
duke@0 831 * currently doesn't have any concept of physical vs.
duke@0 832 * logical interfaces.
duke@0 833 */
duke@0 834 strcpy(name, if_name);
dsamersoff@2545 835 *vname = 0;
duke@0 836
duke@0 837 /*
duke@0 838 * Create and populate the netaddr node. If allocation fails
duke@0 839 * return an un-updated list.
duke@0 840 */
dsamersoff@2545 841 /*Allocate for addr and brdcast at once*/
dsamersoff@2545 842
dsamersoff@2545 843 #ifdef AF_INET6
dsamersoff@2545 844 addr_size = (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
dsamersoff@2545 845 #else
dsamersoff@2545 846 addr_size = sizeof(struct sockaddr_in);
dsamersoff@2545 847 #endif
dsamersoff@2545 848
dsamersoff@2545 849 CHECKED_MALLOC3(addrP, netaddr *, sizeof(netaddr)+2*addr_size);
dsamersoff@2545 850 addrP->addr = (struct sockaddr *)( (char *) addrP+sizeof(netaddr) );
dsamersoff@2545 851 memcpy(addrP->addr, ifr_addrP, addr_size);
dsamersoff@2545 852
duke@0 853 addrP->family = family;
duke@0 854 addrP->brdcast = NULL;
duke@0 855 addrP->mask = prefix;
dsamersoff@2545 856 addrP->next = 0;
duke@0 857 if (family == AF_INET) {
duke@0 858 /*
duke@0 859 * Deal with brodcast addr & subnet mask
duke@0 860 */
dsamersoff@2545 861 struct sockaddr * brdcast_to = (struct sockaddr *) ((char *) addrP + sizeof(netaddr) + addr_size);
dsamersoff@2545 862 addrP->brdcast = getBroadcast(env, sock, name, brdcast_to );
duke@0 863
dsamersoff@2545 864 if (addrP->brdcast && (mask = getSubnet(env, sock, name)) != -1) {
dsamersoff@2545 865 addrP->mask = mask;
dsamersoff@2545 866 }
dsamersoff@2545 867 }
dsamersoff@2545 868
dsamersoff@2545 869 /**
dsamersoff@2545 870 * Deal with virtual interface with colon notaion e.g. eth0:1
dsamersoff@2545 871 */
dsamersoff@2545 872 name_colonP = strchr(name, ':');
dsamersoff@2545 873 if (name_colonP != NULL) {
duke@0 874 /**
duke@0 875 * This is a virtual interface. If we are able to access the parent
duke@0 876 * we need to create a new entry if it doesn't exist yet *and* update
duke@0 877 * the 'parent' interface with the new records.
duke@0 878 */
dsamersoff@2545 879 *name_colonP = 0;
dsamersoff@2545 880 if (getFlags(env,sock,name) < 0) {
chegar@894 881 // failed to access parent interface do not create parent.
chegar@894 882 // We are a virtual interface with no parent.
chegar@894 883 isVirtual = 1;
dsamersoff@2545 884 *name_colonP = ':';
duke@0 885 }
dsamersoff@2545 886 else{
dsamersoff@2545 887 // Got access to parent, so create it if necessary.
dsamersoff@2545 888 // Save original name to vname and truncate name by ':'
dsamersoff@2545 889 memcpy(vname, name, sizeof(vname) );
dsamersoff@2545 890 vname[name_colonP - name] = ':';
dsamersoff@2545 891 }
duke@0 892 }
duke@0 893
duke@0 894 /*
duke@0 895 * Check if this is a "new" interface. Use the interface
duke@0 896 * name for matching because index isn't supported on
duke@0 897 * Solaris 2.6 & 7.
duke@0 898 */
duke@0 899 while (currif != NULL) {
duke@0 900 if (strcmp(name, currif->name) == 0) {
duke@0 901 break;
duke@0 902 }
duke@0 903 currif = currif->next;
duke@0 904 }
duke@0 905
duke@0 906 /*
duke@0 907 * If "new" then create an netif structure and
duke@0 908 * insert it onto the list.
duke@0 909 */
duke@0 910 if (currif == NULL) {
dsamersoff@2545 911 CHECKED_MALLOC3(currif, netif *, sizeof(netif)+IFNAMSIZ );
dsamersoff@2545 912 currif->name = (char *) currif+sizeof(netif);
dsamersoff@2545 913 strcpy(currif->name, name);
dsamersoff@2545 914 currif->index = getIndex(env,sock,name);
dsamersoff@2545 915 currif->addr = NULL;
dsamersoff@2545 916 currif->childs = NULL;
dsamersoff@2545 917 currif->virtual = isVirtual;
dsamersoff@2545 918 currif->next = ifs;
dsamersoff@2545 919 ifs = currif;
duke@0 920 }
duke@0 921
duke@0 922 /*
duke@0 923 * Finally insert the address on the interface
duke@0 924 */
duke@0 925 addrP->next = currif->addr;
duke@0 926 currif->addr = addrP;
duke@0 927
duke@0 928 parent = currif;
duke@0 929
duke@0 930 /**
duke@0 931 * Let's deal with the virtual interface now.
duke@0 932 */
duke@0 933 if (vname[0]) {
dsamersoff@2545 934 netaddr *tmpaddr;
duke@0 935
dsamersoff@2545 936 currif = parent->childs;
duke@0 937
dsamersoff@2545 938 while (currif != NULL) {
dsamersoff@2545 939 if (strcmp(vname, currif->name) == 0) {
dsamersoff@2545 940 break;
dsamersoff@2545 941 }
dsamersoff@2545 942 currif = currif->next;
duke@0 943 }
dsamersoff@2545 944
dsamersoff@2545 945 if (currif == NULL) {
dsamersoff@2545 946 CHECKED_MALLOC3(currif, netif *, sizeof(netif)+ IFNAMSIZ );
dsamersoff@2545 947 currif->name = (char *) currif + sizeof(netif);
dsamersoff@2545 948 strcpy(currif->name, vname);
dsamersoff@2545 949 currif->index = getIndex(env,sock,vname);
dsamersoff@2545 950 currif->addr = NULL;
dsamersoff@2545 951 /* Need to duplicate the addr entry? */
dsamersoff@2545 952 currif->virtual = 1;
dsamersoff@2545 953 currif->childs = NULL;
dsamersoff@2545 954 currif->next = parent->childs;
dsamersoff@2545 955 parent->childs = currif;
duke@0 956 }
dsamersoff@2545 957
dsamersoff@2545 958 CHECKED_MALLOC3(tmpaddr, netaddr *, sizeof(netaddr)+2*addr_size);
dsamersoff@2545 959 memcpy(tmpaddr, addrP, sizeof(netaddr));
dsamersoff@2545 960 if (addrP->addr != NULL) {
dsamersoff@2545 961 tmpaddr->addr = (struct sockaddr *) ( (char*)tmpaddr + sizeof(netaddr) ) ;
dsamersoff@2545 962 memcpy(tmpaddr->addr, addrP->addr, addr_size);
duke@0 963 }
duke@0 964
dsamersoff@2545 965 if (addrP->brdcast != NULL) {
dsamersoff@2545 966 tmpaddr->brdcast = (struct sockaddr *) ((char *) tmpaddr + sizeof(netaddr)+addr_size);
dsamersoff@2545 967 memcpy(tmpaddr->brdcast, addrP->brdcast, addr_size);
dsamersoff@2545 968 }
dsamersoff@2545 969
dsamersoff@2545 970 tmpaddr->next = currif->addr;
dsamersoff@2545 971 currif->addr = tmpaddr;
duke@0 972 }
duke@0 973
duke@0 974 return ifs;
duke@0 975 }
duke@0 976
dsamersoff@2545 977 /* Open socket for further ioct calls
dsamersoff@2545 978 * proto is AF_INET/AF_INET6
duke@0 979 */
dsamersoff@2545 980 static int openSocket(JNIEnv *env, int proto){
dsamersoff@2545 981 int sock;
duke@0 982
dsamersoff@2545 983 if ((sock = JVM_Socket(proto, SOCK_DGRAM, 0)) < 0) {
dsamersoff@2545 984 /*
dsamersoff@2545 985 * If EPROTONOSUPPORT is returned it means we don't have
dsamersoff@2545 986 * support for this proto so don't throw an exception.
dsamersoff@2545 987 */
dsamersoff@2545 988 if (errno != EPROTONOSUPPORT) {
dsamersoff@2545 989 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "Socket creation failed");
dsamersoff@2545 990 }
dsamersoff@2545 991 return -1;
chegar@375 992 }
chegar@375 993
dsamersoff@2545 994 return sock;
dsamersoff@2545 995 }
chegar@375 996
dsamersoff@2545 997
dsamersoff@2545 998 /** Linux **/
dsamersoff@2545 999 #ifdef __linux__
dsamersoff@2545 1000 /* Open socket for further ioct calls, try v4 socket first and
dsamersoff@2545 1001 * if it falls return v6 socket
dsamersoff@2545 1002 */
dsamersoff@2545 1003
dsamersoff@2545 1004 #ifdef AF_INET6
dsamersoff@2545 1005 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
dsamersoff@2545 1006 int sock;
dsamersoff@2545 1007 struct ifreq if2;
dsamersoff@2545 1008
dsamersoff@2545 1009 if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
dsamersoff@2545 1010 if (errno == EPROTONOSUPPORT){
dsamersoff@2545 1011 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
dsamersoff@2545 1012 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
dsamersoff@2545 1013 return -1;
dsamersoff@2545 1014 }
dsamersoff@2545 1015 }
dsamersoff@2545 1016 else{ // errno is not NOSUPPORT
dsamersoff@2545 1017 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
dsamersoff@2545 1018 return -1;
dsamersoff@2545 1019 }
dsamersoff@2545 1020 }
dsamersoff@2545 1021
dsamersoff@2545 1022 /* Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or IPv6 socket regardless of type
dsamersoff@2545 1023 of address of an interface */
dsamersoff@2545 1024
dsamersoff@2545 1025 return sock;
dsamersoff@2545 1026 }
dsamersoff@2545 1027
dsamersoff@2545 1028 #else
dsamersoff@2545 1029 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
dsamersoff@2545 1030 return openSocket(env,AF_INET);
dsamersoff@2545 1031 }
dsamersoff@2545 1032 #endif
dsamersoff@2545 1033
dsamersoff@2545 1034 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
dsamersoff@2545 1035 struct ifconf ifc;
dsamersoff@2545 1036 struct ifreq *ifreqP;
dsamersoff@2545 1037 char *buf;
dsamersoff@2545 1038 int numifs;
dsamersoff@2545 1039 unsigned i;
dsamersoff@2545 1040
dsamersoff@2545 1041
dsamersoff@2545 1042 /* need to do a dummy SIOCGIFCONF to determine the buffer size.
dsamersoff@2545 1043 * SIOCGIFCOUNT doesn't work
dsamersoff@2545 1044 */
dsamersoff@2545 1045 ifc.ifc_buf = NULL;
dsamersoff@2545 1046 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
dsamersoff@2545 1047 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGIFCONF failed");
dsamersoff@2545 1048 return ifs;
chegar@375 1049 }
dsamersoff@2545 1050
dsamersoff@2545 1051 CHECKED_MALLOC3(buf,char *, ifc.ifc_len);
dsamersoff@2545 1052
dsamersoff@2545 1053 ifc.ifc_buf = buf;
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 (void) free(buf);
dsamersoff@2545 1057 return ifs;
dsamersoff@2545 1058 }
dsamersoff@2545 1059
dsamersoff@2545 1060 /*
dsamersoff@2545 1061 * Iterate through each interface
dsamersoff@2545 1062 */
dsamersoff@2545 1063 ifreqP = ifc.ifc_req;
dsamersoff@2545 1064 for (i=0; i<ifc.ifc_len/sizeof (struct ifreq); i++, ifreqP++) {
dsamersoff@2545 1065 /*
dsamersoff@2545 1066 * Add to the list
dsamersoff@2545 1067 */
dsamersoff@2545 1068 ifs = addif(env, sock, ifreqP->ifr_name, ifs, (struct sockaddr *) & (ifreqP->ifr_addr), AF_INET, 0);
dsamersoff@2545 1069
dsamersoff@2545 1070 /*
dsamersoff@2545 1071 * If an exception occurred then free the list
dsamersoff@2545 1072 */
dsamersoff@2545 1073 if ((*env)->ExceptionOccurred(env)) {
dsamersoff@2545 1074 free(buf);
dsamersoff@2545 1075 freeif(ifs);
dsamersoff@2545 1076 return NULL;
dsamersoff@2545 1077 }
dsamersoff@2545 1078 }
dsamersoff@2545 1079
dsamersoff@2545 1080 /*
dsamersoff@2545 1081 * Free socket and buffer
dsamersoff@2545 1082 */
dsamersoff@2545 1083 free(buf);
dsamersoff@2545 1084 return ifs;
dsamersoff@2545 1085 }
dsamersoff@2545 1086
dsamersoff@2545 1087
dsamersoff@2545 1088 /*
dsamersoff@2545 1089 * Enumerates and returns all IPv6 interfaces on Linux
dsamersoff@2545 1090 */
dsamersoff@2545 1091
dsamersoff@2545 1092 #ifdef AF_INET6
dsamersoff@2545 1093 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
dsamersoff@2545 1094 FILE *f;
dsamersoff@2545 1095 char addr6[40], devname[20];
dsamersoff@2545 1096 char addr6p[8][5];
dsamersoff@2545 1097 int plen, scope, dad_status, if_idx;
dsamersoff@2545 1098 uint8_t ipv6addr[16];
dsamersoff@2545 1099
dsamersoff@2545 1100 if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
dsamersoff@2545 1101 while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
dsamersoff@2545 1102 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7],
dsamersoff@2545 1103 &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
dsamersoff@2545 1104
dsamersoff@2545 1105 struct netif *ifs_ptr = NULL;
dsamersoff@2545 1106 struct netif *last_ptr = NULL;
dsamersoff@2545 1107 struct sockaddr_in6 addr;
dsamersoff@2545 1108
dsamersoff@2545 1109 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
dsamersoff@2545 1110 addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
dsamersoff@2545 1111 inet_pton(AF_INET6, addr6, ipv6addr);
dsamersoff@2545 1112
dsamersoff@2545 1113 memset(&addr, 0, sizeof(struct sockaddr_in6));
dsamersoff@2545 1114 memcpy((void*)addr.sin6_addr.s6_addr, (const void*)ipv6addr, 16);
dsamersoff@2545 1115
dsamersoff@2545 1116 addr.sin6_scope_id = if_idx;
dsamersoff@2545 1117
dsamersoff@2545 1118 ifs = addif(env, sock, devname, ifs, (struct sockaddr *)&addr, AF_INET6, plen);
dsamersoff@2545 1119
dsamersoff@2545 1120
dsamersoff@2545 1121 /*
dsamersoff@2545 1122 * If an exception occurred then return the list as is.
dsamersoff@2545 1123 */
dsamersoff@2545 1124 if ((*env)->ExceptionOccurred(env)) {
dsamersoff@2545 1125 fclose(f);
dsamersoff@2545 1126 return ifs;
dsamersoff@2545 1127 }
dsamersoff@2545 1128 }
dsamersoff@2545 1129 fclose(f);
dsamersoff@2545 1130 }
dsamersoff@2545 1131 return ifs;
dsamersoff@2545 1132 }
chegar@375 1133 #endif
duke@0 1134
dsamersoff@2545 1135
dsamersoff@2545 1136 static int getIndex(JNIEnv *env, int sock, const char *name){
dsamersoff@2545 1137 /*
dsamersoff@2545 1138 * Try to get the interface index
dsamersoff@2545 1139 * (Not supported on Solaris 2.6 or 7)
dsamersoff@2545 1140 */
dsamersoff@2545 1141 struct ifreq if2;
dsamersoff@2545 1142 strcpy(if2.ifr_name, name);
dsamersoff@2545 1143
dsamersoff@2545 1144 if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
dsamersoff@2545 1145 return -1;
dsamersoff@2545 1146 }
dsamersoff@2545 1147
dsamersoff@2545 1148 return if2.ifr_ifindex;
duke@0 1149 }
duke@0 1150
duke@0 1151 /**
duke@0 1152 * Returns the IPv4 broadcast address of a named interface, if it exists.
duke@0 1153 * Returns 0 if it doesn't have one.
duke@0 1154 */
dsamersoff@2545 1155 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
duke@0 1156 struct sockaddr *ret = NULL;
duke@0 1157 struct ifreq if2;
duke@0 1158
duke@0 1159 memset((char *) &if2, 0, sizeof(if2));
duke@0 1160 strcpy(if2.ifr_name, ifname);
dsamersoff@2545 1161
duke@0 1162 /* Let's make sure the interface does have a broadcast address */
dsamersoff@2545 1163 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
dsamersoff@2545 1164 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFFLAGS failed");
dsamersoff@2545 1165 return ret;
duke@0 1166 }
dsamersoff@2545 1167
dsamersoff@2545 1168 if (if2.ifr_flags & IFF_BROADCAST) {
dsamersoff@2545 1169 /* It does, let's retrieve it*/
dsamersoff@2545 1170 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&if2) < 0) {
dsamersoff@2545 1171 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFBRDADDR failed");
dsamersoff@2545 1172 return ret;
dsamersoff@2545 1173 }
dsamersoff@2545 1174
dsamersoff@2545 1175 ret = brdcast_store;
duke@0 1176 memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr));
duke@0 1177 }
dsamersoff@2545 1178
duke@0 1179 return ret;
duke@0 1180 }
duke@0 1181
duke@0 1182 /**
duke@0 1183 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
duke@0 1184 * interface, if it has one, otherwise return -1.
duke@0 1185 */
dsamersoff@2545 1186 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1187 unsigned int mask;
dsamersoff@2545 1188 short ret;
dsamersoff@2545 1189 struct ifreq if2;
dsamersoff@2545 1190
dsamersoff@2545 1191 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1192 strcpy(if2.ifr_name, ifname);
dsamersoff@2545 1193
dsamersoff@2545 1194 if (ioctl(sock, SIOCGIFNETMASK, (char *)&if2) < 0) {
dsamersoff@2545 1195 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFNETMASK failed");
dsamersoff@2545 1196 return -1;
dsamersoff@2545 1197 }
dsamersoff@2545 1198
dsamersoff@2545 1199 mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr);
dsamersoff@2545 1200 ret = 0;
dsamersoff@2545 1201 while (mask) {
dsamersoff@2545 1202 mask <<= 1;
dsamersoff@2545 1203 ret++;
dsamersoff@2545 1204 }
dsamersoff@2545 1205
dsamersoff@2545 1206 return ret;
dsamersoff@2545 1207 }
dsamersoff@2545 1208
dsamersoff@2545 1209 /**
dsamersoff@2545 1210 * Get the Hardware address (usually MAC address) for the named interface.
dsamersoff@2545 1211 * return puts the data in buf, and returns the length, in byte, of the
dsamersoff@2545 1212 * MAC address. Returns -1 if there is no hardware address on that interface.
dsamersoff@2545 1213 */
dsamersoff@2545 1214 static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf) {
dsamersoff@2545 1215 static struct ifreq ifr;
dsamersoff@2545 1216 int i;
dsamersoff@2545 1217
dsamersoff@2545 1218 strcpy(ifr.ifr_name, ifname);
dsamersoff@2545 1219 if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
dsamersoff@2545 1220 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFHWADDR failed");
dsamersoff@2545 1221 return -1;
dsamersoff@2545 1222 }
dsamersoff@2545 1223
dsamersoff@2545 1224 memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
dsamersoff@2545 1225
dsamersoff@2545 1226 /*
dsamersoff@2545 1227 * All bytes to 0 means no hardware address.
dsamersoff@2545 1228 */
dsamersoff@2545 1229
dsamersoff@2545 1230 for (i = 0; i < IFHWADDRLEN; i++) {
dsamersoff@2545 1231 if (buf[i] != 0)
dsamersoff@2545 1232 return IFHWADDRLEN;
dsamersoff@2545 1233 }
dsamersoff@2545 1234
dsamersoff@2545 1235 return -1;
dsamersoff@2545 1236 }
dsamersoff@2545 1237
dsamersoff@2545 1238 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1239 struct ifreq if2;
dsamersoff@2545 1240
dsamersoff@2545 1241 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1242 strcpy(if2.ifr_name, ifname);
dsamersoff@2545 1243
dsamersoff@2545 1244 if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) {
dsamersoff@2545 1245 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFMTU failed");
dsamersoff@2545 1246 return -1;
dsamersoff@2545 1247 }
dsamersoff@2545 1248
dsamersoff@2545 1249 return if2.ifr_mtu;
dsamersoff@2545 1250 }
dsamersoff@2545 1251
dsamersoff@2545 1252 static int getFlags(JNIEnv *env, int sock, const char *ifname) {
duke@0 1253 struct ifreq if2;
dsamersoff@2545 1254 int ret = -1;
duke@0 1255
duke@0 1256 memset((char *) &if2, 0, sizeof(if2));
duke@0 1257 strcpy(if2.ifr_name, ifname);
dsamersoff@2545 1258
dsamersoff@2545 1259 if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0){
dsamersoff@2545 1260 return -1;
duke@0 1261 }
dsamersoff@2545 1262
dsamersoff@2545 1263 return if2.ifr_flags;
duke@0 1264 }
duke@0 1265
dsamersoff@2545 1266 #endif
dsamersoff@2545 1267
dsamersoff@2545 1268 /** Solaris **/
duke@0 1269 #ifdef __solaris__
dsamersoff@2545 1270 /* Open socket for further ioct calls, try v4 socket first and
dsamersoff@2545 1271 * if it falls return v6 socket
dsamersoff@2545 1272 */
dsamersoff@2545 1273
dsamersoff@2545 1274 #ifdef AF_INET6
dsamersoff@2545 1275 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
dsamersoff@2545 1276 int sock, alreadyV6 = 0;
dsamersoff@2545 1277 struct lifreq if2;
dsamersoff@2545 1278
dsamersoff@2545 1279 if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
dsamersoff@2545 1280 if (errno == EPROTONOSUPPORT){
dsamersoff@2545 1281 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
dsamersoff@2545 1282 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
dsamersoff@2545 1283 return -1;
dsamersoff@2545 1284 }
dsamersoff@2545 1285
dsamersoff@2545 1286 alreadyV6=1;
dsamersoff@2545 1287 }
dsamersoff@2545 1288 else{ // errno is not NOSUPPORT
dsamersoff@2545 1289 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
dsamersoff@2545 1290 return -1;
dsamersoff@2545 1291 }
dsamersoff@2545 1292 }
dsamersoff@2545 1293
dsamersoff@2545 1294 /**
dsamersoff@2545 1295 * Solaris requires that we have IPv6 socket to query an
dsamersoff@2545 1296 * interface without IPv4 address - check it here
dsamersoff@2545 1297 * POSIX 1 require the kernell to return ENOTTY if the call is
dsamersoff@2545 1298 * unappropriate for device e.g. NETMASK for device having IPv6
dsamersoff@2545 1299 * only address but not all devices follows the standart so
dsamersoff@2545 1300 * fallback on any error. It's not an ecology friendly but more
dsamersoff@2545 1301 * reliable.
dsamersoff@2545 1302 */
dsamersoff@2545 1303
dsamersoff@2545 1304 if (! alreadyV6 ){
dsamersoff@2545 1305 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1306 strcpy(if2.lifr_name, ifname);
dsamersoff@2545 1307 if (ioctl(sock, SIOCGLIFNETMASK, (char *)&if2) < 0) {
dsamersoff@2545 1308 close(sock);
dsamersoff@2545 1309 if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){
dsamersoff@2545 1310 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
dsamersoff@2545 1311 return -1;
dsamersoff@2545 1312 }
dsamersoff@2545 1313 }
dsamersoff@2545 1314 }
dsamersoff@2545 1315
dsamersoff@2545 1316 return sock;
dsamersoff@2545 1317 }
dsamersoff@2545 1318
dsamersoff@2545 1319 #else
dsamersoff@2545 1320 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
dsamersoff@2545 1321 return openSocket(env,AF_INET);
dsamersoff@2545 1322 }
dsamersoff@2545 1323 #endif
dsamersoff@2545 1324
dsamersoff@2545 1325 /*
dsamersoff@2545 1326 * Enumerates and returns all IPv4 interfaces
dsamersoff@2545 1327 * (linux verison)
dsamersoff@2545 1328 */
dsamersoff@2545 1329
dsamersoff@2545 1330 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
dsamersoff@2545 1331 return enumIPvXInterfaces(env,sock, ifs, AF_INET);
dsamersoff@2545 1332 }
dsamersoff@2545 1333
dsamersoff@2545 1334 #ifdef AF_INET6
dsamersoff@2545 1335 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
dsamersoff@2545 1336 return enumIPvXInterfaces(env,sock, ifs, AF_INET6);
dsamersoff@2545 1337 }
dsamersoff@2545 1338 #endif
dsamersoff@2545 1339
dsamersoff@2545 1340 /*
dsamersoff@2545 1341 Enumerates and returns all interfaces on Solaris
dsamersoff@2545 1342 use the same code for IPv4 and IPv6
dsamersoff@2545 1343 */
dsamersoff@2545 1344 static netif *enumIPvXInterfaces(JNIEnv *env, int sock, netif *ifs, int family) {
dsamersoff@2545 1345 struct lifconf ifc;
dsamersoff@2545 1346 struct lifreq *ifr;
dsamersoff@2545 1347 int n;
dsamersoff@2545 1348 char *buf;
dsamersoff@2545 1349 struct lifnum numifs;
dsamersoff@2545 1350 unsigned bufsize;
dsamersoff@2545 1351
dsamersoff@2545 1352 /*
dsamersoff@2545 1353 * Get the interface count
dsamersoff@2545 1354 */
dsamersoff@2545 1355 numifs.lifn_family = family;
dsamersoff@2545 1356 numifs.lifn_flags = 0;
dsamersoff@2545 1357 if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
dsamersoff@2545 1358 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGLIFNUM failed");
dsamersoff@2545 1359 return ifs;
dsamersoff@2545 1360 }
dsamersoff@2545 1361
dsamersoff@2545 1362 /*
dsamersoff@2545 1363 * Enumerate the interface configurations
dsamersoff@2545 1364 */
dsamersoff@2545 1365 bufsize = numifs.lifn_count * sizeof (struct lifreq);
dsamersoff@2545 1366 CHECKED_MALLOC3(buf, char *, bufsize);
dsamersoff@2545 1367
dsamersoff@2545 1368 ifc.lifc_family = family;
dsamersoff@2545 1369 ifc.lifc_flags = 0;
dsamersoff@2545 1370 ifc.lifc_len = bufsize;
dsamersoff@2545 1371 ifc.lifc_buf = buf;
dsamersoff@2545 1372 if (ioctl(sock, SIOCGLIFCONF, (char *)&ifc) < 0) {
dsamersoff@2545 1373 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGLIFCONF failed");
dsamersoff@2545 1374 free(buf);
dsamersoff@2545 1375 return ifs;
dsamersoff@2545 1376 }
dsamersoff@2545 1377
dsamersoff@2545 1378 /*
dsamersoff@2545 1379 * Iterate through each interface
dsamersoff@2545 1380 */
dsamersoff@2545 1381 ifr = ifc.lifc_req;
dsamersoff@2545 1382 for (n=0; n<numifs.lifn_count; n++, ifr++) {
dsamersoff@2545 1383 int index = -1;
dsamersoff@2545 1384 struct lifreq if2;
dsamersoff@2545 1385
dsamersoff@2545 1386 /*
dsamersoff@2545 1387 * Ignore either IPv4 or IPv6 addresses
dsamersoff@2545 1388 */
dsamersoff@2545 1389 if (ifr->lifr_addr.ss_family != family) {
dsamersoff@2545 1390 continue;
dsamersoff@2545 1391 }
dsamersoff@2545 1392
dsamersoff@2545 1393 /* add to the list */
dsamersoff@2545 1394 ifs = addif(env, sock,ifr->lifr_name, ifs, (struct sockaddr *)&(ifr->lifr_addr),family, (short) ifr->lifr_addrlen);
dsamersoff@2545 1395
dsamersoff@2545 1396 /*
dsamersoff@2545 1397 * If an exception occurred we return immediately
dsamersoff@2545 1398 */
dsamersoff@2545 1399 if ((*env)->ExceptionOccurred(env)) {
dsamersoff@2545 1400 free(buf);
dsamersoff@2545 1401 return ifs;
dsamersoff@2545 1402 }
dsamersoff@2545 1403
dsamersoff@2545 1404 }
dsamersoff@2545 1405
dsamersoff@2545 1406 free(buf);
dsamersoff@2545 1407 return ifs;
dsamersoff@2545 1408 }
dsamersoff@2545 1409
dsamersoff@2545 1410 static int getIndex(JNIEnv *env, int sock, const char *name){
dsamersoff@2545 1411 /*
dsamersoff@2545 1412 * Try to get the interface index
dsamersoff@2545 1413 * (Not supported on Solaris 2.6 or 7)
dsamersoff@2545 1414 */
dsamersoff@2545 1415 struct lifreq if2;
dsamersoff@2545 1416 strcpy(if2.lifr_name, name);
dsamersoff@2545 1417
dsamersoff@2545 1418 if (ioctl(sock, SIOCGLIFINDEX, (char *)&if2) < 0) {
dsamersoff@2545 1419 return -1;
dsamersoff@2545 1420 }
dsamersoff@2545 1421
dsamersoff@2545 1422 return if2.lifr_index;
dsamersoff@2545 1423 }
dsamersoff@2545 1424
dsamersoff@2545 1425 /**
dsamersoff@2545 1426 * Returns the IPv4 broadcast address of a named interface, if it exists.
dsamersoff@2545 1427 * Returns 0 if it doesn't have one.
dsamersoff@2545 1428 */
dsamersoff@2545 1429 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
dsamersoff@2545 1430 struct sockaddr *ret = NULL;
dsamersoff@2545 1431 struct lifreq if2;
dsamersoff@2545 1432
dsamersoff@2545 1433 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1434 strcpy(if2.lifr_name, ifname);
dsamersoff@2545 1435
dsamersoff@2545 1436 /* Let's make sure the interface does have a broadcast address */
dsamersoff@2545 1437 if (ioctl(sock, SIOCGLIFFLAGS, (char *)&if2) < 0) {
dsamersoff@2545 1438 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFFLAGS failed");
dsamersoff@2545 1439 return ret;
dsamersoff@2545 1440 }
dsamersoff@2545 1441
dsamersoff@2545 1442 if (if2.lifr_flags & IFF_BROADCAST) {
dsamersoff@2545 1443 /* It does, let's retrieve it*/
dsamersoff@2545 1444 if (ioctl(sock, SIOCGLIFBRDADDR, (char *)&if2) < 0) {
dsamersoff@2545 1445 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFBRDADDR failed");
dsamersoff@2545 1446 return ret;
dsamersoff@2545 1447 }
dsamersoff@2545 1448
dsamersoff@2545 1449 ret = brdcast_store;
dsamersoff@2545 1450 memcpy(ret, &if2.lifr_broadaddr, sizeof(struct sockaddr));
dsamersoff@2545 1451 }
dsamersoff@2545 1452
dsamersoff@2545 1453 return ret;
dsamersoff@2545 1454 }
dsamersoff@2545 1455
dsamersoff@2545 1456 /**
dsamersoff@2545 1457 * Returns the IPv4 subnet prefix length (aka subnet mask) for the named
dsamersoff@2545 1458 * interface, if it has one, otherwise return -1.
dsamersoff@2545 1459 */
dsamersoff@2545 1460 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1461 unsigned int mask;
dsamersoff@2545 1462 short ret;
dsamersoff@2545 1463 struct lifreq if2;
dsamersoff@2545 1464
dsamersoff@2545 1465 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1466 strcpy(if2.lifr_name, ifname);
dsamersoff@2545 1467
dsamersoff@2545 1468 if (ioctl(sock, SIOCGLIFNETMASK, (char *)&if2) < 0) {
dsamersoff@2545 1469 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFNETMASK failed");
dsamersoff@2545 1470 return -1;
dsamersoff@2545 1471 }
dsamersoff@2545 1472
dsamersoff@2545 1473 mask = ntohl(((struct sockaddr_in*)&(if2.lifr_addr))->sin_addr.s_addr);
dsamersoff@2545 1474 ret = 0;
dsamersoff@2545 1475
dsamersoff@2545 1476 while (mask) {
dsamersoff@2545 1477 mask <<= 1;
dsamersoff@2545 1478 ret++;
dsamersoff@2545 1479 }
dsamersoff@2545 1480
dsamersoff@2545 1481 return ret;
dsamersoff@2545 1482 }
dsamersoff@2545 1483
dsamersoff@2545 1484
dsamersoff@2545 1485
dsamersoff@2545 1486 #define DEV_PREFIX "/dev/"
duke@0 1487
duke@0 1488 /**
duke@0 1489 * Solaris specific DLPI code to get hardware address from a device.
duke@0 1490 * Unfortunately, at least up to Solaris X, you have to have special
duke@0 1491 * privileges (i.e. be root).
duke@0 1492 */
duke@0 1493 static int getMacFromDevice(JNIEnv *env, const char* ifname, unsigned char* retbuf) {
dsamersoff@2545 1494 char style1dev[MAXPATHLEN];
dsamersoff@2545 1495 int fd;
dsamersoff@2545 1496 dl_phys_addr_req_t dlpareq;
dsamersoff@2545 1497 dl_phys_addr_ack_t *dlpaack;
dsamersoff@2545 1498 struct strbuf msg;
dsamersoff@2545 1499 char buf[128];
dsamersoff@2545 1500 int flags = 0;
duke@0 1501
dsamersoff@2545 1502 /**
dsamersoff@2545 1503 * Device is in /dev
dsamersoff@2545 1504 * e.g.: /dev/bge0
dsamersoff@2545 1505 */
dsamersoff@2545 1506 strcpy(style1dev, DEV_PREFIX);
dsamersoff@2545 1507 strcat(style1dev, ifname);
dsamersoff@2545 1508 if ((fd = open(style1dev, O_RDWR)) < 0) {
dsamersoff@2545 1509 /*
dsamersoff@2545 1510 * Can't open it. We probably are missing the privilege.
dsamersoff@2545 1511 * We'll have to try something else
dsamersoff@2545 1512 */
dsamersoff@2545 1513 return 0;
dsamersoff@2545 1514 }
duke@0 1515
dsamersoff@2545 1516 dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
dsamersoff@2545 1517 dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
dsamersoff@2545 1518
dsamersoff@2545 1519 msg.buf = (char *)&dlpareq;
dsamersoff@2545 1520 msg.len = DL_PHYS_ADDR_REQ_SIZE;
dsamersoff@2545 1521
dsamersoff@2545 1522 if (putmsg(fd, &msg, NULL, 0) < 0) {
dsamersoff@2545 1523 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "putmsg failed");
dsamersoff@2545 1524 return -1;
dsamersoff@2545 1525 }
dsamersoff@2545 1526
dsamersoff@2545 1527 dlpaack = (dl_phys_addr_ack_t *)buf;
dsamersoff@2545 1528
dsamersoff@2545 1529 msg.buf = (char *)buf;
dsamersoff@2545 1530 msg.len = 0;
dsamersoff@2545 1531 msg.maxlen = sizeof (buf);
dsamersoff@2545 1532 if (getmsg(fd, &msg, NULL, &flags) < 0) {
dsamersoff@2545 1533 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "getmsg failed");
dsamersoff@2545 1534 return -1;
dsamersoff@2545 1535 }
dsamersoff@2545 1536
dsamersoff@2545 1537 if (msg.len < DL_PHYS_ADDR_ACK_SIZE || dlpaack->dl_primitive != DL_PHYS_ADDR_ACK) {
dsamersoff@2545 1538 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Couldn't obtain phys addr\n");
dsamersoff@2545 1539 return -1;
dsamersoff@2545 1540 }
dsamersoff@2545 1541
dsamersoff@2545 1542 memcpy(retbuf, &buf[dlpaack->dl_addr_offset], dlpaack->dl_addr_length);
dsamersoff@2545 1543 return dlpaack->dl_addr_length;
duke@0 1544 }
duke@0 1545
duke@0 1546 /**
duke@0 1547 * Get the Hardware address (usually MAC address) for the named interface.
duke@0 1548 * return puts the data in buf, and returns the length, in byte, of the
duke@0 1549 * MAC address. Returns -1 if there is no hardware address on that interface.
duke@0 1550 */
dsamersoff@2545 1551 static int getMacAddress(JNIEnv *env, int sock, const char *ifname, const struct in_addr* addr, unsigned char *buf) {
dsamersoff@2545 1552 struct arpreq arpreq;
dsamersoff@2545 1553 struct sockaddr_in* sin;
dsamersoff@2545 1554 struct sockaddr_in ipAddr;
dsamersoff@2545 1555 int len, i;
duke@0 1556
dsamersoff@2545 1557 /**
dsamersoff@2545 1558 * On Solaris we have to use DLPI, but it will only work if we have
dsamersoff@2545 1559 * privileged access (i.e. root). If that fails, we try a lookup
dsamersoff@2545 1560 * in the ARP table, which requires an IPv4 address.
dsamersoff@2545 1561 */
dsamersoff@2545 1562 if ((len = getMacFromDevice(env, ifname, buf)) == 0) {
dsamersoff@2545 1563 /*DLPI failed - trying to do arp lookup*/
duke@0 1564
dsamersoff@2545 1565 if (addr == NULL) {
dsamersoff@2545 1566 /**
dsamersoff@2545 1567 * No IPv4 address for that interface, so can't do an ARP lookup.
dsamersoff@2545 1568 */
dsamersoff@2545 1569 return -1;
dsamersoff@2545 1570 }
dsamersoff@2545 1571
dsamersoff@2545 1572 len = 6; //???
dsamersoff@2545 1573
dsamersoff@2545 1574 sin = (struct sockaddr_in *) &arpreq.arp_pa;
dsamersoff@2545 1575 memset((char *) &arpreq, 0, sizeof(struct arpreq));
dsamersoff@2545 1576 ipAddr.sin_port = 0;
dsamersoff@2545 1577 ipAddr.sin_family = AF_INET;
dsamersoff@2545 1578 memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
dsamersoff@2545 1579 memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
dsamersoff@2545 1580 arpreq.arp_flags= ATF_PUBL;
dsamersoff@2545 1581
dsamersoff@2545 1582 if (ioctl(sock, SIOCGARP, &arpreq) < 0) {
dsamersoff@2545 1583 if (errno != ENXIO) {
dsamersoff@2545 1584 // "No such device or address" means no hardware address, so it's
dsamersoff@2545 1585 // normal don't throw an exception
dsamersoff@2545 1586 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL failed");
dsamersoff@2545 1587 return -1;
dsamersoff@2545 1588 }
dsamersoff@2545 1589 }
dsamersoff@2545 1590
dsamersoff@2545 1591 memcpy(buf, &arpreq.arp_ha.sa_data[0], len );
duke@0 1592 }
duke@0 1593
dsamersoff@2545 1594 /*
dsamersoff@2545 1595 * All bytes to 0 means no hardware address.
dsamersoff@2545 1596 */
duke@0 1597
dsamersoff@2545 1598 for (i = 0; i < len; i++) {
dsamersoff@2545 1599 if (buf[i] != 0)
dsamersoff@2545 1600 return len;
dsamersoff@2545 1601 }
dsamersoff@2545 1602
duke@0 1603 return -1;
duke@0 1604 }
duke@0 1605
dsamersoff@2545 1606 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1607 struct lifreq if2;
duke@0 1608
dsamersoff@2545 1609 memset((char *) &if2, 0, sizeof(if2));
dsamersoff@2545 1610 strcpy(if2.lifr_name, ifname);
dsamersoff@2545 1611
dsamersoff@2545 1612 if (ioctl(sock, SIOCGLIFMTU, (char *)&if2) < 0) {
dsamersoff@2545 1613 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFMTU failed");
dsamersoff@2545 1614 return -1;
dsamersoff@2545 1615 }
dsamersoff@2545 1616
dsamersoff@2545 1617 return if2.lifr_mtu;
duke@0 1618 }
duke@0 1619
dsamersoff@2545 1620
dsamersoff@2545 1621 static int getFlags(JNIEnv *env, int sock, const char *ifname) {
dsamersoff@2545 1622 struct lifreq lifr;
dsamersoff@2545 1623 memset((caddr_t)&lifr, 0, sizeof(lifr));
dsamersoff@2545 1624 strcpy((caddr_t)&(lifr.lifr_name), ifname);
dsamersoff@2545 1625
dsamersoff@2545 1626 if (ioctl(sock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
dsamersoff@2545 1627 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFFLAGS failed");
dsamersoff@2545 1628 return -1;
dsamersoff@2545 1629 }
dsamersoff@2545 1630
dsamersoff@2545 1631 return lifr.lifr_flags;
duke@0 1632 }
duke@0 1633
duke@0 1634
dsamersoff@2545 1635 #endif
duke@0 1636
duke@0 1637