Print this page


Split Close
Expand all
Collapse all
          --- old/src/solaris/native/java/net/NetworkInterface.c
          +++ new/src/solaris/native/java/net/NetworkInterface.c
↓ open down ↓ 86 lines elided ↑ open up ↑
  87   87  jfieldID ni_nameID;
  88   88  jfieldID ni_indexID;
  89   89  jfieldID ni_descID;
  90   90  jfieldID ni_addrsID;
  91   91  jfieldID ni_bindsID;
  92   92  jfieldID ni_virutalID;
  93   93  jfieldID ni_childsID;
  94   94  jfieldID ni_parentID;
  95   95  jmethodID ni_ctrID;
  96   96  
  97      -static jclass ni_iacls;
  98      -static jclass ni_ia4cls;
  99      -static jclass ni_ia6cls;
 100   97  static jclass ni_ibcls;
 101      -static jmethodID ni_ia4ctrID;
 102      -static jmethodID ni_ia6ctrID;
 103   98  static jmethodID ni_ibctrID;
 104      -static jfieldID ni_iaaddressID;
 105      -static jfieldID ni_iafamilyID;
 106      -static jfieldID ni_ia6ipaddressID;
 107   99  static jfieldID ni_ibaddressID;
 108  100  static jfieldID ni_ib4broadcastID;
 109  101  static jfieldID ni_ib4maskID;
 110  102  
 111  103  static jobject createNetworkInterface(JNIEnv *env, netif *ifs);
 112  104  
 113  105  static netif *enumInterfaces(JNIEnv *env);
 114  106  static netif *enumIPv4Interfaces(JNIEnv *env, netif *ifs);
 115  107  #ifdef AF_INET6
 116  108  static netif *enumIPv6Interfaces(JNIEnv *env, netif *ifs);
↓ open down ↓ 6 lines elided ↑ open up ↑
 123  115  static struct sockaddr *getBroadcast(JNIEnv *env, const char *ifname);
 124  116  static short getSubnet(JNIEnv *env, const char *ifname);
 125  117  
 126  118  /*
 127  119   * Class:     java_net_NetworkInterface
 128  120   * Method:    init
 129  121   * Signature: ()V
 130  122   */
 131  123  JNIEXPORT void JNICALL
 132  124  Java_java_net_NetworkInterface_init(JNIEnv *env, jclass cls) {
 133      -    ni_class = (*env)->FindClass(env,"java/net/NetworkInterface");
 134      -    ni_class = (*env)->NewGlobalRef(env, ni_class);
 135      -    ni_nameID = (*env)->GetFieldID(env, ni_class,"name", "Ljava/lang/String;");
 136      -    ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
 137      -    ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs", "[Ljava/net/InetAddress;");
 138      -    ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings", "[Ljava/net/InterfaceAddress;");
 139      -    ni_descID = (*env)->GetFieldID(env, ni_class, "displayName", "Ljava/lang/String;");
 140      -    ni_virutalID = (*env)->GetFieldID(env, ni_class, "virtual", "Z");
 141      -    ni_childsID = (*env)->GetFieldID(env, ni_class, "childs", "[Ljava/net/NetworkInterface;");
 142      -    ni_parentID = (*env)->GetFieldID(env, ni_class, "parent", "Ljava/net/NetworkInterface;");
 143      -    ni_ctrID = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
      125 +    if (ni_ib4maskID == NULL) {
      126 +        ni_class = (*env)->FindClass(env,"java/net/NetworkInterface");
      127 +        ni_class = (*env)->NewGlobalRef(env, ni_class);
      128 +        ni_nameID = (*env)->GetFieldID(env, ni_class,"name", "Ljava/lang/String;");
      129 +        ni_indexID = (*env)->GetFieldID(env, ni_class, "index", "I");
      130 +        ni_addrsID = (*env)->GetFieldID(env, ni_class, "addrs", "[Ljava/net/InetAddress;");
      131 +        ni_bindsID = (*env)->GetFieldID(env, ni_class, "bindings", "[Ljava/net/InterfaceAddress;");
      132 +        ni_descID = (*env)->GetFieldID(env, ni_class, "displayName", "Ljava/lang/String;");
      133 +        ni_virutalID = (*env)->GetFieldID(env, ni_class, "virtual", "Z");
      134 +        ni_childsID = (*env)->GetFieldID(env, ni_class, "childs", "[Ljava/net/NetworkInterface;");
      135 +        ni_parentID = (*env)->GetFieldID(env, ni_class, "parent", "Ljava/net/NetworkInterface;");
      136 +        ni_ctrID = (*env)->GetMethodID(env, ni_class, "<init>", "()V");
 144  137  
 145      -    ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
 146      -    ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
 147      -    ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
 148      -    ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
 149      -    ni_ia6cls = (*env)->FindClass(env, "java/net/Inet6Address");
 150      -    ni_ia6cls = (*env)->NewGlobalRef(env, ni_ia6cls);
 151      -    ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
 152      -    ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
 153      -    ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
 154      -    ni_ia6ctrID = (*env)->GetMethodID(env, ni_ia6cls, "<init>", "()V");
 155      -    ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
 156      -    ni_iaaddressID = (*env)->GetFieldID(env, ni_iacls, "address", "I");
 157      -    ni_iafamilyID = (*env)->GetFieldID(env, ni_iacls, "family", "I");
 158      -    ni_ia6ipaddressID = (*env)->GetFieldID(env, ni_ia6cls, "ipaddress", "[B");
 159      -    ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;");
 160      -    ni_ib4broadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
 161      -    ni_ib4maskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
      138 +        ni_ibcls = (*env)->FindClass(env, "java/net/InterfaceAddress");
      139 +        ni_ibcls = (*env)->NewGlobalRef(env, ni_ibcls);
      140 +        ni_ibctrID = (*env)->GetMethodID(env, ni_ibcls, "<init>", "()V");
      141 +        ni_ibaddressID = (*env)->GetFieldID(env, ni_ibcls, "address", "Ljava/net/InetAddress;");
      142 +        ni_ib4broadcastID = (*env)->GetFieldID(env, ni_ibcls, "broadcast", "Ljava/net/Inet4Address;");
      143 +        ni_ib4maskID = (*env)->GetFieldID(env, ni_ibcls, "maskLength", "S");
      144 +    }
      145 +
      146 +    init(env);
 162  147  }
 163  148  
 164  149  
 165  150  /*
 166  151   * Class:     java_net_NetworkInterface
 167  152   * Method:    getByName0
 168  153   * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 169  154   */
 170  155  JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
 171  156      (JNIEnv *env, jclass cls, jstring name) {
↓ open down ↓ 75 lines elided ↑ open up ↑
 247  232  /*
 248  233   * Class:     java_net_NetworkInterface
 249  234   * Method:    getByInetAddress0
 250  235   * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
 251  236   */
 252  237  JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
 253  238      (JNIEnv *env, jclass cls, jobject iaObj) {
 254  239  
 255  240      netif *ifs, *curr;
 256  241  #ifdef AF_INET6
 257      -    int family = (*env)->GetIntField(env, iaObj, ni_iafamilyID) == IPv4?
      242 +    int family = (*env)->GetIntField(env, iaObj, ia_familyID) == IPv4?
 258  243          AF_INET : AF_INET6;
 259  244  #else
 260  245      int family = AF_INET;
 261  246  #endif
 262  247      jobject obj = NULL;
 263  248      jboolean match = JNI_FALSE;
 264  249  
 265  250      ifs = enumInterfaces(env);
 266  251      if (ifs == NULL) {
 267  252          return NULL;
↓ open down ↓ 4 lines elided ↑ open up ↑
 272  257          netaddr *addrP = curr->addr;
 273  258  
 274  259          /*
 275  260           * Iterate through each address on the interface
 276  261           */
 277  262          while (addrP != NULL) {
 278  263  
 279  264              if (family == addrP->family) {
 280  265                  if (family == AF_INET) {
 281  266                      int address1 = htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr);
 282      -                    int address2 = (*env)->GetIntField(env, iaObj, ni_iaaddressID);
      267 +                    int address2 = (*env)->GetIntField(env, iaObj, ia_addressID);
 283  268  
 284  269                      if (address1 == address2) {
 285  270                          match = JNI_TRUE;
 286  271                          break;
 287  272                      }
 288  273                  }
 289  274  
 290  275  #ifdef AF_INET6
 291  276                  if (family == AF_INET6) {
 292  277                      jbyte *bytes = (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr);
 293      -                    jbyteArray ipaddress = (*env)->GetObjectField(env, iaObj, ni_ia6ipaddressID);
      278 +                    jbyteArray ipaddress = (*env)->GetObjectField(env, iaObj, ia6_ipaddressID);
 294  279                      jbyte caddr[16];
 295  280                      int i;
 296  281  
 297  282                      (*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddr);
 298  283                      i = 0;
 299  284                      while (i < 16) {
 300  285                          if (caddr[i] != bytes[i]) {
 301  286                              break;
 302  287                          }
 303  288                          i++;
↓ open down ↓ 123 lines elided ↑ open up ↑
 427  412      addr_count = 0;
 428  413      addrP = ifs->addr;
 429  414      while (addrP != NULL) {
 430  415          addr_count++;
 431  416          addrP = addrP->next;
 432  417      }
 433  418  
 434  419      /*
 435  420       * Create the array of InetAddresses
 436  421       */
 437      -    addrArr = (*env)->NewObjectArray(env, addr_count,  ni_iacls, NULL);
      422 +    addrArr = (*env)->NewObjectArray(env, addr_count,  ia_class, NULL);
 438  423      if (addrArr == NULL) {
 439  424          return NULL;
 440  425      }
 441  426  
 442  427      bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
 443  428      if (bindArr == NULL) {
 444  429        return NULL;
 445  430      }
 446  431      addrP = ifs->addr;
 447  432      addr_index = 0;
 448  433      bind_index = 0;
 449  434      while (addrP != NULL) {
 450  435          jobject iaObj = NULL;
 451  436          jobject ibObj = NULL;
 452  437  
 453  438          if (addrP->family == AF_INET) {
 454      -            iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
      439 +            iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 455  440              if (iaObj) {
 456      -                 (*env)->SetIntField(env, iaObj, ni_iaaddressID,
      441 +                 (*env)->SetIntField(env, iaObj, ia_addressID,
 457  442                       htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
 458  443              }
 459  444              ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
 460  445              if (ibObj) {
 461  446                (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
 462  447                if (addrP->brdcast) {
 463  448                  jobject ia2Obj = NULL;
 464      -                ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
      449 +                ia2Obj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
 465  450                  if (ia2Obj) {
 466      -                  (*env)->SetIntField(env, ia2Obj, ni_iaaddressID,
      451 +                  (*env)->SetIntField(env, ia2Obj, ia_addressID,
 467  452                                        htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
 468  453                    (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
 469  454                    (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
 470  455                  }
 471  456                }
 472  457                (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
 473  458              }
 474  459          }
 475  460  
 476  461  #ifdef AF_INET6
 477  462          if (addrP->family == AF_INET6) {
 478  463              int scope=0;
 479      -            iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
      464 +            iaObj = (*env)->NewObject(env, ia6_class, ia6_ctrID);
 480  465              if (iaObj) {
 481  466                  jbyteArray ipaddress = (*env)->NewByteArray(env, 16);
 482  467                  if (ipaddress == NULL) {
 483  468                      return NULL;
 484  469                  }
 485  470                  (*env)->SetByteArrayRegion(env, ipaddress, 0, 16,
 486  471                      (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
 487  472  #ifdef __linux__
 488  473                  if (!kernelIsV22()) {
 489  474                      scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
 490  475                  }
 491  476  #else
 492  477                  scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
 493  478  #endif
 494  479                  if (scope != 0) { /* zero is default value, no need to set */
 495  480                      (*env)->SetIntField(env, iaObj, ia6_scopeidID, scope);
 496  481                      (*env)->SetBooleanField(env, iaObj, ia6_scopeidsetID, JNI_TRUE);
 497  482                      (*env)->SetObjectField(env, iaObj, ia6_scopeifnameID, netifObj);
 498  483                  }
 499      -                (*env)->SetObjectField(env, iaObj, ni_ia6ipaddressID, ipaddress);
      484 +                (*env)->SetObjectField(env, iaObj, ia6_ipaddressID, ipaddress);
 500  485              }
 501  486              ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
 502  487              if (ibObj) {
 503  488                (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
 504  489                (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
 505  490                (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
 506  491              }
 507  492          }
 508  493  #endif
 509  494  
↓ open down ↓ 1058 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX