public boolean isReachable(InetAddress addr, int timeout, NetworkInterface netif, int ttl)
     throws IOException {
   byte[] ifaddr = null;
   int scope = -1;
   int netif_scope = -1;
   if (netif != null) {
     /*
      * Let's make sure we bind to an address of the proper family.
      * Which means same family as addr because at this point it could
      * be either an IPv6 address or an IPv4 address (case of a dual
      * stack system).
      */
     java.util.Enumeration<InetAddress> it = netif.getInetAddresses();
     InetAddress inetaddr = null;
     while (it.hasMoreElements()) {
       inetaddr = it.nextElement();
       if (inetaddr.getClass().isInstance(addr)) {
         ifaddr = inetaddr.getAddress();
         if (inetaddr instanceof Inet6Address) {
           netif_scope = ((Inet6Address) inetaddr).getScopeId();
         }
         break;
       }
     }
     if (ifaddr == null) {
       // Interface doesn't support the address family of
       // the destination
       return false;
     }
   }
   if (addr instanceof Inet6Address) scope = ((Inet6Address) addr).getScopeId();
   return isReachable0(addr.getAddress(), scope, timeout, ifaddr, ttl, netif_scope);
 }
  /** Block datagrams from given source if a memory to receive all datagrams. */
  void block(MembershipKeyImpl key, InetAddress source) throws IOException {
    assert key.channel() == this;
    assert key.sourceAddress() == null;

    synchronized (stateLock) {
      if (!key.isValid()) throw new IllegalStateException("key is no longer valid");
      if (source.isAnyLocalAddress())
        throw new IllegalArgumentException("Source address is a wildcard address");
      if (source.isMulticastAddress())
        throw new IllegalArgumentException("Source address is multicast address");
      if (source.getClass() != key.group().getClass())
        throw new IllegalArgumentException("Source address is different type to group");

      int n;
      if (key instanceof MembershipKeyImpl.Type6) {
        MembershipKeyImpl.Type6 key6 = (MembershipKeyImpl.Type6) key;
        n = Net.block6(fd, key6.groupAddress(), key6.index(), Net.inet6AsByteArray(source));
      } else {
        MembershipKeyImpl.Type4 key4 = (MembershipKeyImpl.Type4) key;
        n = Net.block4(fd, key4.groupAddress(), key4.interfaceAddress(), Net.inet4AsInt(source));
      }
      if (n == IOStatus.UNAVAILABLE) {
        // ancient kernel
        throw new UnsupportedOperationException();
      }
    }
  }
  /** Joins channel's socket to the given group/interface and optional source address. */
  private MembershipKey innerJoin(InetAddress group, NetworkInterface interf, InetAddress source)
      throws IOException {
    if (!group.isMulticastAddress())
      throw new IllegalArgumentException("Group not a multicast address");

    // check multicast address is compatible with this socket
    if (group instanceof Inet4Address) {
      if (family == StandardProtocolFamily.INET6 && !Net.canIPv6SocketJoinIPv4Group())
        throw new IllegalArgumentException("IPv6 socket cannot join IPv4 multicast group");
    } else if (group instanceof Inet6Address) {
      if (family != StandardProtocolFamily.INET6)
        throw new IllegalArgumentException("Only IPv6 sockets can join IPv6 multicast group");
    } else {
      throw new IllegalArgumentException("Address type not supported");
    }

    // check source address
    if (source != null) {
      if (source.isAnyLocalAddress())
        throw new IllegalArgumentException("Source address is a wildcard address");
      if (source.isMulticastAddress())
        throw new IllegalArgumentException("Source address is multicast address");
      if (source.getClass() != group.getClass())
        throw new IllegalArgumentException("Source address is different type to group");
    }

    SecurityManager sm = System.getSecurityManager();
    if (sm != null) sm.checkMulticast(group);

    synchronized (stateLock) {
      if (!isOpen()) throw new ClosedChannelException();

      // check the registry to see if we are already a member of the group
      if (registry == null) {
        registry = new MembershipRegistry();
      } else {
        // return existing membership key
        MembershipKey key = registry.checkMembership(group, interf, source);
        if (key != null) return key;
      }

      MembershipKeyImpl key;
      if ((family == StandardProtocolFamily.INET6)
          && ((group instanceof Inet6Address) || Net.canJoin6WithIPv4Group())) {
        int index = interf.getIndex();
        if (index == -1) throw new IOException("Network interface cannot be identified");

        // need multicast and source address as byte arrays
        byte[] groupAddress = Net.inet6AsByteArray(group);
        byte[] sourceAddress = (source == null) ? null : Net.inet6AsByteArray(source);

        // join the group
        int n = Net.join6(fd, groupAddress, index, sourceAddress);
        if (n == IOStatus.UNAVAILABLE) throw new UnsupportedOperationException();

        key =
            new MembershipKeyImpl.Type6(
                this, group, interf, source, groupAddress, index, sourceAddress);

      } else {
        // need IPv4 address to identify interface
        Inet4Address target = Net.anyInet4Address(interf);
        if (target == null) throw new IOException("Network interface not configured for IPv4");

        int groupAddress = Net.inet4AsInt(group);
        int targetAddress = Net.inet4AsInt(target);
        int sourceAddress = (source == null) ? 0 : Net.inet4AsInt(source);

        // join the group
        int n = Net.join4(fd, groupAddress, targetAddress, sourceAddress);
        if (n == IOStatus.UNAVAILABLE) throw new UnsupportedOperationException();

        key =
            new MembershipKeyImpl.Type4(
                this, group, interf, source, groupAddress, targetAddress, sourceAddress);
      }

      registry.add(key);
      return key;
    }
  }
Exemple #4
0
  public static InetAddress getLocalHost() {
    InetAddress localHost = null;
    try {
      Properties props = new Properties();
      File propsFile = new File("localhost.properties");
      /* Try to open the localhost.properties file */

      /* If it's there */
      if (propsFile.exists()) {
        try {
          props.load(new FileInputStream(propsFile));
          /* Load its contents */

          String localHostProp = props.getProperty("localhost.address");
          if (localHostProp != null) localHost = InetAddress.getByName(localHostProp);
          /*
           * Try to set localhost to the localhost.address
           * property
           */
        } catch (IOException e) {;
        }
      }

      Class networkInterface = Class.forName("java.net.NetworkInterface");
      Enumeration interfaces =
          (Enumeration)
              networkInterface
                  .getMethod("getNetworkInterfaces", (Class[]) null)
                  .invoke(null, (Object[]) null);
      /* Get all network interfaces */

      /* For all the interfaces */
      while (localHost == null && interfaces.hasMoreElements()) {
        Object interfaze = interfaces.nextElement();
        Enumeration addresses =
            (Enumeration)
                interfaze
                    .getClass()
                    .getMethod("getInetAddresses", (Class[]) null)
                    .invoke(interfaze, (Object[]) null);
        /* Get the next interface and its associated addresses */

        /* For all its associated addresses */
        while (localHost == null && addresses.hasMoreElements()) {
          InetAddress address = (InetAddress) addresses.nextElement();
          Boolean isLoopback =
              (Boolean)
                  address
                      .getClass()
                      .getMethod("isLoopbackAddress", (Class[]) null)
                      .invoke(address, (Object[]) null);

          if (address.getAddress().length == 4 && !isLoopback.booleanValue()) localHost = address;
          /*
           * If it's a 32-bit (IPv4) address and it's not a
           * loopback,
           * that's the one we want
           */
        }
      }

      if (localHost == null) localHost = InetAddress.getLocalHost();
      /* If all else fails, fall back on InetAddress.getLocalHost() */
    } catch (Exception e) {
      try {
        if (localHost == null) localHost = InetAddress.getLocalHost();
        /*
         * Use InetAddress.getLocalHost() if we don't have
         * java.net.NetworkInterfaces (i.e., on J2ME PP)
         */
      } catch (UnknownHostException e2) {
        e2.printStackTrace();
        System.exit(1);
      }
    }

    return localHost;
  }