Lines Matching refs:env

127 static jobject createNetworkInterface(JNIEnv *env, netif *ifs);
128 static int getFlags0(JNIEnv *env, jstring ifname);
130 static netif *enumInterfaces(JNIEnv *env);
131 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs);
134 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs);
137 static netif *addif(JNIEnv *env, int sock, const char * if_name, netif *ifs, struct sockaddr* ifr_addrP, int family, short prefix);
140 static int openSocket(JNIEnv *env, int proto);
141 static int openSocketWithFallback(JNIEnv *env, const char *ifname);
144 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *name, struct sockaddr *brdcast_store);
145 static short getSubnet(JNIEnv *env, int sock, const char *ifname);
149 static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf);
150 static int getMTU(JNIEnv *env, int sock, const char *ifname);
155 static netif *enumIPvXInterfaces(JNIEnv *env, int sock, netif *ifs, int family);
156 static int getMacFromDevice(JNIEnv *env, const char* ifname, unsigned char* retbuf);
172 Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls) {
173 ni_class = (*env)->FindClass(env,"java/net/NetworkInterface");
174 ni_class = (*env)->NewGlobalRef(env, ni_class);
175 ni_nameID = (*env)->GetFieldID(env, ni_class,"name", "Ljava/lang/String;");
176 ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
177 ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs", "[Ljava/net/InetAddress;");
178 ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings", "[Ljava/net/InterfaceAddress;");
179 ni_descID = (*env)->GetFieldID(env, ni_class, "displayName", "Ljava/lang/String;");
180 ni_virutalID = (*env)->GetFieldID(env, ni_class, "virtual", "Z");
181 ni_childsID = (*env)->GetFieldID(env, ni_class, "childs", "[Ljava/net/NetworkInterface;");
182 ni_parentID = (*env)->GetFieldID(env, ni_class, "parent", "Ljava/net/NetworkInterface;");
183 ni_ctrID = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
185 ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
186 ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
187 ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
188 ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
189 ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
190 ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
191 ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
192 ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
193 ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
194 ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
195 ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
196 ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
197 ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;");
198 ni_ib4broadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
199 ni_ib4maskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
200 ni_defaultIndexID = (*env)->GetStaticFieldID(env, ni_class, "defaultIndex", "I");
210 (JNIEnv *env, jclass cls, jstring name) {
217 ifs = enumInterfaces(env);
222 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
237 obj = createNetworkInterface(env, curr);
241 (*env)->ReleaseStringUTFChars(env, name, name_utf);
254 (JNIEnv *env, jclass cls, jint index) {
263 ifs = enumInterfaces(env);
281 obj = createNetworkInterface(env, curr);
294 (JNIEnv *env, jclass cls, jobject iaObj) {
299 int family = (getInetAddress_family(env, iaObj) == IPv4) ? AF_INET : AF_INET6;
307 ifs = enumInterfaces(env);
324 int address2 = getInetAddress_addr(env, iaObj);
335 jbyteArray ipaddress = (*env)->GetObjectField(env, iaObj, ni_ia6ipaddressID);
339 (*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddr);
370 obj = createNetworkInterface(env, curr);
384 (JNIEnv *env, jclass cls) {
390 ifs = enumInterfaces(env);
404 netIFArr = (*env)->NewObjectArray(env, ifCount, cls, NULL);
419 netifObj = createNetworkInterface(env, curr);
426 (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
441 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isUp0(JNIEnv *env, jclass cls, jstring name, jint index) {
442 int ret = getFlags0(env, name);
451 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isP2P0(JNIEnv *env, jclass cls, jstring name, jint index) {
452 int ret = getFlags0(env, name);
461 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopback0(JNIEnv *env, jclass cls, jstring name, jint index) {
462 int ret = getFlags0(env, name);
471 JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_supportsMulticast0(JNIEnv *env, jclass cls, jstring name, jint index) {
472 int ret = getFlags0(env, name);
481 JNIEXPORT jbyteArray JNICALL Java_java_net_NetworkInterface_getMacAddr0(JNIEnv *env, jclass class, jbyteArray addrArray, jstring name, jint index) {
492 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
494 if ((sock =openSocketWithFallback(env, name_utf)) < 0) {
495 (*env)->ReleaseStringUTFChars(env, name, name_utf);
501 (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
507 len = getMacAddress(env, sock, name_utf, &iaddr, mac);
509 len = getMacAddress(env, sock, name_utf,NULL, mac);
512 ret = (*env)->NewByteArray(env, len);
517 (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *) (mac));
521 (*env)->ReleaseStringUTFChars(env, name, name_utf);
533 JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0(JNIEnv *env, jclass class, jstring name, jint index) {
539 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
541 if ((sock =openSocketWithFallback(env, name_utf)) < 0) {
542 (*env)->ReleaseStringUTFChars(env, name, name_utf);
546 ret = getMTU(env, sock, name_utf);
548 (*env)->ReleaseStringUTFChars(env, name, name_utf);
556 static int getFlags0(JNIEnv *env, jstring name) {
562 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
564 if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
565 (*env)->ReleaseStringUTFChars(env, name, name_utf);
569 name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
574 (*env)->ReleaseStringUTFChars(env, name, name_utf);
577 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFFLAGS failed");
592 jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
608 netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
609 name = (*env)->NewStringUTF(env, ifs->name);
613 (*env)->SetObjectField(env, netifObj, ni_nameID, name);
614 (*env)->SetObjectField(env, netifObj, ni_descID, name);
615 (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
616 (*env)->SetBooleanField(env, netifObj, ni_virutalID, ifs->virtual ? JNI_TRUE : JNI_FALSE);
631 addrArr = (*env)->NewObjectArray(env, addr_count, ni_iacls, NULL);
636 bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
648 iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
650 setInetAddress_addr(env, iaObj, htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
652 ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
654 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
657 ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
659 setInetAddress_addr(env, ia2Obj, htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
660 (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
661 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
664 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
671 iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
673 jbyteArray ipaddress = (*env)->NewByteArray(env, 16);
677 (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
683 (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
684 (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
685 (*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj);
687 (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
689 ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
691 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
692 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
693 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
702 (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
716 childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
728 tmp = createNetworkInterface(env, childP);
732 (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
733 (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
736 (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
737 (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
738 (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
747 static netif *enumInterfaces(JNIEnv *env) {
755 sock = openSocket(env, AF_INET);
756 if (sock < 0 && (*env)->ExceptionOccurred(env)) {
760 ifs = enumIPv4Interfaces(env, sock, NULL);
763 if (ifs == NULL && (*env)->ExceptionOccurred(env)) {
779 sock = openSocket(env, AF_INET6);
780 if (sock < 0 && (*env)->ExceptionOccurred(env)) {
785 ifs = enumIPv6Interfaces(env, sock, ifs);
788 if ((*env)->ExceptionOccurred(env)) {
803 JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); \
837 netif *addif(JNIEnv *env, int sock, const char * if_name,
894 addrP->brdcast = getBroadcast(env, sock, name, brdcast_to );
896 if (addrP->brdcast && (mask = getSubnet(env, sock, name)) != -1) {
1014 static int openSocket(JNIEnv *env, int proto){
1023 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "Socket creation failed");
1039 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
1046 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1051 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1063 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
1064 return openSocket(env,AF_INET);
1068 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1081 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGIFCONF failed");
1089 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGIFCONF failed");
1102 ifs = addif(env, sock, ifreqP->ifr_name, ifs, (struct sockaddr *) & (ifreqP->ifr_addr), AF_INET, 0);
1107 if ((*env)->ExceptionOccurred(env)) {
1127 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1152 ifs = addif(env, sock, devname, ifs, (struct sockaddr *)&addr, AF_INET6, plen);
1158 if ((*env)->ExceptionOccurred(env)) {
1189 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
1198 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFFLAGS failed");
1205 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFBRDADDR failed");
1220 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
1229 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFNETMASK failed");
1248 static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf) {
1254 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFHWADDR failed");
1272 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1279 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFMTU failed");
1313 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
1320 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1327 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1348 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1358 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
1359 return openSocket(env,AF_INET);
1368 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1369 return enumIPvXInterfaces(env,sock, ifs, AF_INET);
1373 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1374 return enumIPvXInterfaces(env,sock, ifs, AF_INET6);
1382 static netif *enumIPvXInterfaces(JNIEnv *env, int sock, netif *ifs, int family) {
1396 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGLIFNUM failed");
1411 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "ioctl SIOCGLIFCONF failed");
1439 ifs = addif(env, sock,ifr->lifr_name, ifs, (struct sockaddr *)&(ifr->lifr_addr),family, (short) ifr->lifr_addrlen);
1444 if ((*env)->ExceptionOccurred(env)) {
1474 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
1483 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFFLAGS failed");
1490 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFBRDADDR failed");
1505 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
1514 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFNETMASK failed");
1538 static int getMacFromDevice(JNIEnv *env, const char* ifname, unsigned char* retbuf) {
1568 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "putmsg failed");
1578 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "getmsg failed");
1583 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Couldn't obtain phys addr\n");
1596 static int getMacAddress(JNIEnv *env, int sock, const char *ifname, const struct in_addr* addr, unsigned char *buf) {
1621 if ((len = getMacFromDevice(env, ifname, buf)) == 0) {
1660 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1667 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGLIFMTU failed");
1699 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
1706 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
1711 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
1720 static int openSocketWithFallback(JNIEnv *env, const char *ifname){
1721 return openSocket(env,AF_INET);
1728 static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) {
1732 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
1748 ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, AF_INET, 0);
1753 if ((*env)->ExceptionOccurred(env)) {
1802 static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
1808 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
1826 NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
1835 ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, AF_INET6,
1839 if ((*env)->ExceptionOccurred(env)) {
1881 static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) {
1890 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFFLAGS failed");
1897 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFBRDADDR failed");
1912 static short getSubnet(JNIEnv *env, int sock, const char *ifname) {
1921 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFNETMASK failed");
1940 static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf) {
1967 static int getMTU(JNIEnv *env, int sock, const char *ifname) {
1974 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFMTU failed");