annotate src/solaris/native/java/net/NetworkInterface.c @ 5523:7151aa54f673

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