Print this page


Split Close
Expand all
Collapse all
          --- old/src/windows/native/java/net/NetworkInterface.c
          +++ new/src/windows/native/java/net/NetworkInterface.c
↓ open down ↓ 79 lines elided ↑ open up ↑
  80   80  /* Windows 95/98/ME running */
  81   81  static jboolean isW9x;
  82   82  
  83   83  /* Windows version supports */
  84   84  static jboolean os_supports_ipv6;
  85   85  
  86   86  /* various JNI ids */
  87   87  
  88   88  jclass ni_class;            /* NetworkInterface */
  89   89  
  90      -jmethodID ni_ctor;          /* NetworkInterface() */
       90 +jmethodID ni_ctrID;          /* NetworkInterface() */
  91   91  
  92   92  jfieldID ni_indexID;        /* NetworkInterface.index */
  93   93  jfieldID ni_addrsID;        /* NetworkInterface.addrs */
  94   94  jfieldID ni_bindsID;        /* NetworkInterface.bindings */
  95   95  jfieldID ni_nameID;         /* NetworkInterface.name */
  96   96  jfieldID ni_displayNameID;  /* NetworkInterface.displayName */
  97   97  jfieldID ni_childsID;       /* NetworkInterface.childs */
  98      -jclass ni_iacls;            /* InetAddress */
  99      -jfieldID ni_iaAddr;         /* InetAddress.address */
 100   98  
 101      -jclass ni_ia4cls;           /* Inet4Address */
 102      -jmethodID ni_ia4Ctor;       /* Inet4Address() */
 103      -
 104      -jclass ni_ia6cls;           /* Inet6Address */
 105      -jmethodID ni_ia6ctrID;      /* Inet6Address() */
 106      -jfieldID ni_ia6ipaddressID;
 107      -jfieldID ni_ia6ipaddressID;
 108      -
 109   99  jclass ni_ibcls;            /* InterfaceAddress */
 110  100  jmethodID ni_ibctrID;       /* InterfaceAddress() */
 111  101  jfieldID ni_ibaddressID;        /* InterfaceAddress.address */
 112  102  jfieldID ni_ibbroadcastID;      /* InterfaceAddress.broadcast */
 113  103  jfieldID ni_ibmaskID;           /* InterfaceAddress.maskLength */
 114  104  
 115  105  /*
 116  106   * Support routines to free netif and netaddr lists
 117  107   */
 118  108  void free_netif(netif *netifP) {
↓ open down ↓ 419 lines elided ↑ open up ↑
 538  528              return;
 539  529          }
 540  530      } else {
 541  531          enumInterfaces_fn = enumInterfaces_win;
 542  532          enumAddresses_fn = enumAddresses_win;
 543  533      }
 544  534  
 545  535      /*
 546  536       * Get the various JNI ids that we require
 547  537       */
 548      -    ni_class = (*env)->NewGlobalRef(env, cls);
 549      -    ni_nameID = (*env)->GetFieldID(env, ni_class, "name", "Ljava/lang/String;");
 550      -    ni_displayNameID = (*env)->GetFieldID(env, ni_class, "displayName", "Ljava/lang/String;");
 551      -    ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
 552      -    ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs", "[Ljava/net/InetAddress;");
 553      -    ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings", "[Ljava/net/InterfaceAddress;");
 554      -    ni_childsID = (*env)->GetFieldID(env, ni_class, "childs", "[Ljava/net/NetworkInterface;");
 555      -    ni_ctor = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
      538 +    if (ni_ibmaskID == NULL) {
      539 +        ni_class = (*env)->NewGlobalRef(env, cls);
      540 +        ni_nameID = (*env)->GetFieldID(env, ni_class, "name", "Ljava/lang/String;");
      541 +        ni_displayNameID = (*env)->GetFieldID(env, ni_class, "displayName", "Ljava/lang/String;");
      542 +        ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
      543 +        ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs", "[Ljava/net/InetAddress;");
      544 +        ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings", "[Ljava/net/InterfaceAddress;");
      545 +        ni_childsID = (*env)->GetFieldID(env, ni_class, "childs", "[Ljava/net/NetworkInterface;");
      546 +        ni_ctrID = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
 556  547  
 557      -    ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
 558      -    ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
 559      -    ni_iaAddr = (*env)->GetFieldID(env, ni_iacls, "address", "I");
      548 +        init(env);
 560  549  
 561      -    ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
 562      -    ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
 563      -    ni_ia4Ctor = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
 564      -
 565      -    ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
 566      -    ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
 567      -    ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
 568      -    ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
 569      -
 570      -    ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
 571      -    ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
 572      -    ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
 573      -    ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;");
 574      -    ni_ibbroadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
 575      -    ni_ibmaskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
 576      -
      550 +        ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
      551 +        ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
      552 +        ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
      553 +        ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;");
      554 +        ni_ibbroadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
      555 +        ni_ibmaskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
      556 +    }
 577  557  }
 578  558  
 579  559  /*
 580  560   * Create a NetworkInterface object, populate the name and index, and
 581  561   * populate the InetAddress array based on the IP addresses for this
 582  562   * interface.
 583  563   */
 584  564  jobject createNetworkInterface(JNIEnv *env, netif *ifs, int netaddrCount, netaddr *netaddrP)
 585  565  {
 586  566      jobject netifObj;
 587  567      jobject name, displayName;
 588  568      jobjectArray addrArr, bindsArr, childArr;
 589  569      netaddr *addrs;
 590  570      jint addr_index;
 591  571      jint bind_index;
 592  572  
 593  573      /*
 594  574       * Create a NetworkInterface object and populate it
 595  575       */
 596      -    netifObj = (*env)->NewObject(env, ni_class, ni_ctor);
      576 +    netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
 597  577      name = (*env)->NewStringUTF(env, ifs->name);
 598  578      if (ifs->dNameIsUnicode) {
 599  579          displayName = (*env)->NewString(env, (PWCHAR)ifs->displayName, wcslen ((PWCHAR)ifs->displayName));
 600  580      } else {
 601  581          displayName = (*env)->NewStringUTF(env, ifs->displayName);
 602  582      }
 603  583      if (netifObj == NULL || name == NULL || displayName == NULL) {
 604  584          return NULL;
 605  585      }
 606  586      (*env)->SetObjectField(env, netifObj, ni_nameID, name);
↓ open down ↓ 4 lines elided ↑ open up ↑
 611  591       * Get the IP addresses for this interface if necessary
 612  592       * Note that 0 is a valid number of addresses.
 613  593       */
 614  594      if (netaddrCount < 0) {
 615  595          netaddrCount = (*enumAddresses_fn)(env, ifs, &netaddrP);
 616  596          if ((*env)->ExceptionOccurred(env)) {
 617  597              free_netaddr(netaddrP);
 618  598              return NULL;
 619  599          }
 620  600      }
 621      -    addrArr = (*env)->NewObjectArray(env, netaddrCount, ni_iacls, NULL);
      601 +    addrArr = (*env)->NewObjectArray(env, netaddrCount, ia_class, NULL);
 622  602      if (addrArr == NULL) {
 623  603          free_netaddr(netaddrP);
 624  604          return NULL;
 625  605      }
 626  606  
 627  607      bindsArr = (*env)->NewObjectArray(env, netaddrCount, ni_ibcls, NULL);
 628  608      if (bindsArr == NULL) {
 629  609        free_netaddr(netaddrP);
 630  610        return NULL;
 631  611      }
 632  612      addrs = netaddrP;
 633  613      addr_index = 0;
 634  614      bind_index = 0;
 635  615      while (addrs != NULL) {
 636  616          jobject iaObj, ia2Obj;
 637  617          jobject ibObj = NULL;
 638  618          if (addrs->addr.him.sa_family == AF_INET) {
 639      -            iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
      619 +            iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 640  620              if (iaObj == NULL) {
 641  621                  free_netaddr(netaddrP);
 642  622                  return NULL;
 643  623              }
 644  624              /* default ctor will set family to AF_INET */
 645  625  
 646      -            (*env)->SetIntField(env, iaObj, ni_iaAddr, ntohl(addrs->addr.him4.sin_addr.s_addr));
      626 +            (*env)->SetIntField(env, iaObj, ia_addressID, ntohl(addrs->addr.him4.sin_addr.s_addr));
 647  627              if (addrs->mask != -1) {
 648  628                ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
 649  629                if (ibObj == NULL) {
 650  630                  free_netaddr(netaddrP);
 651  631                  return NULL;
 652  632                }
 653  633                (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
 654      -              ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4Ctor);
      634 +              ia2Obj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 655  635                if (ia2Obj == NULL) {
 656  636                  free_netaddr(netaddrP);
 657  637                  return NULL;
 658  638                }
 659      -              (*env)->SetIntField(env, ia2Obj, ni_iaAddr,
      639 +              (*env)->SetIntField(env, ia2Obj, ia_addressID,
 660  640                                    ntohl(addrs->brdcast.him4.sin_addr.s_addr));
 661  641                (*env)->SetObjectField(env, ibObj, ni_ibbroadcastID, ia2Obj);
 662  642                (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
 663  643                (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
 664  644              }
 665  645          } else /* AF_INET6 */ {
 666  646              int scope;
 667      -            iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
      647 +            iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
 668  648              if (iaObj) {
 669  649                  jbyteArray ipaddress = (*env)->NewByteArray(env, 16);
 670  650                  if (ipaddress == NULL) {
 671  651                      return NULL;
 672  652                  }
 673  653                  (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
 674  654                      (jbyte *)&(addrs->addr.him6.sin6_addr.s6_addr));
 675  655                  scope = addrs->addr.him6.sin6_scope_id;
 676  656                  if (scope != 0) { /* zero is default value, no need to set */
 677  657                      (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
 678  658                      (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
 679  659                      (*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj);
 680  660                  }
 681      -                (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
      661 +                (*env)->SetObjectField(env, iaObj, ia6_ipaddressID, ipaddress);
 682  662                  ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
 683  663                  if (ibObj == NULL) {
 684  664                    free_netaddr(netaddrP);
 685  665                    return NULL;
 686  666                  }
 687  667                  (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
 688  668                  (*env)->SetShortField(env, ibObj, ni_ibmaskID, addrs->mask);
 689  669                  (*env)->SetObjectArrayElement(env, bindsArr, bind_index++, ibObj);
 690  670              }
 691  671          }
↓ open down ↓ 110 lines elided ↑ open up ↑
 802  782  
 803  783  /*
 804  784   * Class:     java_net_NetworkInterface
 805  785   * Method:    getByInetAddress0
 806  786   * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
 807  787   */
 808  788  JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
 809  789      (JNIEnv *env, jclass cls, jobject iaObj)
 810  790  {
 811  791      netif *ifList, *curr;
 812      -    jint addr = (*env)->GetIntField(env, iaObj, ni_iaAddr);
      792 +    jint addr = (*env)->GetIntField(env, iaObj, ia_addressID);
 813  793      jobject netifObj = NULL;
 814  794  
 815  795      if (os_supports_ipv6 && ipv6_available()) {
 816  796          return Java_java_net_NetworkInterface_getByInetAddress0_XP (env, cls, iaObj);
 817  797      }
 818  798  
 819  799      /* get the list of interfaces */
 820  800      if ((*enumInterfaces_fn)(env, &ifList) < 0) {
 821  801          return NULL;
 822  802      }
↓ open down ↓ 253 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX