/* * Class: java_net_NetworkInterface * Method: getByInetAddress0 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface; */ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0 (JNIEnv *env, jclass cls, jobject iaObj) { netif *ifList, *curr; jint addr = getInetAddress_addr(env, iaObj); jobject netifObj = NULL; // Retained for now to support IPv4 only stack, java.net.preferIPv4Stack if (ipv6_available()) { return Java_java_net_NetworkInterface_getByInetAddress0_XP (env, cls, iaObj); } /* get the list of interfaces */ if (enumInterfaces(env, &ifList) < 0) { return NULL; } /* * Enumerate the addresses on each interface until we find a * matching address. */ curr = ifList; while (curr != NULL) { int count; netaddr *addrList; netaddr *addrP; /* enumerate the addresses on this interface */ count = enumAddresses_win(env, curr, &addrList); if (count < 0) { free_netif(ifList); return NULL; } /* iterate through each address */ addrP = addrList; while (addrP != NULL) { if ((unsigned long)addr == ntohl(addrP->addr.him4.sin_addr.s_addr)) { break; } addrP = addrP->next; } /* * Address matched so create NetworkInterface for this interface * and address list. */ if (addrP != NULL) { /* createNetworkInterface will free addrList */ netifObj = createNetworkInterface(env, curr, count, addrList); break; } /* on next interface */ curr = curr->next; } /* release the interface list */ free_netif(ifList); return netifObj; }
/* * Class: java_net_NetworkInterface * Method: getByInetAddress0 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface; */ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0 (JNIEnv *env, jclass cls, jobject iaObj) { netif *ifs, *curr; int family = (getInetAddress_family(env, iaObj) == IPv4) ? AF_INET : AF_INET6; jobject obj = NULL; jboolean match = JNI_FALSE; ifs = enumInterfaces(env); if (ifs == NULL) { return NULL; } curr = ifs; while (curr != NULL) { netaddr *addrP = curr->addr; /* * Iterate through each address on the interface */ while (addrP != NULL) { if (family == addrP->family) { if (family == AF_INET) { int address1 = htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr); int address2 = getInetAddress_addr(env, iaObj); if (address1 == address2) { match = JNI_TRUE; break; } } #ifdef AF_INET6 if (family == AF_INET6) { jbyte *bytes = (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr); jbyte caddr[16]; int i; getInet6Address_ipaddress(env, iaObj, (char *)caddr); i = 0; while (i < 16) { if (caddr[i] != bytes[i]) { break; } i++; } if (i >= 16) { match = JNI_TRUE; break; } } #endif } if (match) { break; } addrP = addrP->next; } if (match) { break; } curr = curr->next; } /* if found create a NetworkInterface */ if (match) {; obj = createNetworkInterface(env, curr); } freeif(ifs); return obj; }