Example usage for org.apache.http.conn.util InetAddressUtils isIPv4Address

List of usage examples for org.apache.http.conn.util InetAddressUtils isIPv4Address

Introduction

In this page you can find the example usage for org.apache.http.conn.util InetAddressUtils isIPv4Address.

Prototype

public static boolean isIPv4Address(final String input) 

Source Link

Document

Checks whether the parameter is a valid IPv4 address

Usage

From source file:com.cubic9.android.droidglove.Main.java

/**
 * get IP Address of phone//w  w w  .j  ava  2s. c  om
 * @return IP address String or error message
 */
private String getIPAddress() {
    Enumeration<NetworkInterface> interfaces = null;
    try {
        interfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        new AlertDialog.Builder(Main.this).setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle(getString(R.string.dialog_error_title))
                .setMessage(getString(R.string.dialog_error_get_ip))
                .setPositiveButton(R.string.dialog_error_ok, null).create().show();
        e.printStackTrace();
    }

    while (interfaces.hasMoreElements()) {
        NetworkInterface network = interfaces.nextElement();
        Enumeration<InetAddress> addresses = network.getInetAddresses();

        while (addresses.hasMoreElements()) {
            String address = addresses.nextElement().getHostAddress();

            // If found not 127.0.0.1 and not 0.0.0.0, return it
            if (!"127.0.0.1".equals(address) && !"0.0.0.0".equals(address)
                    && InetAddressUtils.isIPv4Address(address)) {
                return address;
            }
        }
    }

    return "An error occured while getting IP address.";
}

From source file:org.dasein.cloud.google.network.FirewallSupport.java

@Override
public void revoke(@Nonnull String firewallId, @Nonnull Direction direction, @Nonnull Permission permission,
        @Nonnull String source, @Nonnull Protocol protocol, @Nonnull RuleTarget target, int beginPort,
        int endPort) throws CloudException, InternalException {
    if (!direction.equals(Direction.INGRESS))
        throw new InternalException("GCE does not support outbound firewall rules");
    if (!permission.equals(Permission.ALLOW))
        throw new InternalException("GCE does not support deny firewall rules");

    if (source.contains("/")) {
        String[] parts = source.split("/");
        if (!InetAddressUtils.isIPv4Address(parts[0]))
            throw new InternalException("GCE only supports valid IPv4 addresses or cidrs as source targets");
    } else {/*  w  ww . j a  v  a  2  s .co  m*/
        if (!InetAddressUtils.isIPv4Address(source))
            throw new InternalException("GCE only supports valid IPv4 addresses or cidrs as source targets");
        else
            source = source + "/32";
    }

    if (!target.getRuleTargetType().equals(RuleTargetType.VM)
            && !target.getRuleTargetType().equals(RuleTargetType.VLAN))
        throw new InternalException("GCE only supports VM or VLAN targets for firewall rules");

    FirewallRule rule = null;
    for (FirewallRule current : getRules(firewallId)) {
        if (!current.getSourceEndpoint().getCidr().equals(source))
            continue;
        if (!current.getProtocol().equals(protocol))
            continue;
        if (current.getDestinationEndpoint().getRuleTargetType().equals(RuleTargetType.VM)) {
            if (!current.getDestinationEndpoint().getProviderVirtualMachineId()
                    .equals(target.getProviderVirtualMachineId()))
                continue;
        } else if (current.getDestinationEndpoint().getRuleTargetType().equals(RuleTargetType.VLAN)) {
            if (!current.getDestinationEndpoint().getProviderVlanId().equals(target.getProviderVlanId()))
                continue;
        }
        if (current.getStartPort() != beginPort)
            continue;
        if (current.getEndPort() != endPort)
            continue;

        rule = current;
    }
    if (rule == null)
        throw new InternalException("The rule for " + direction.name() + ", " + permission.name() + ", "
                + source + ", " + beginPort + "-" + endPort + " does not exist");

    revoke(rule.getProviderRuleId());
}

From source file:nl.phanos.liteliveresultsclient.gui.ResultsWindows.java

private void ChangeIpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChangeIpActionPerformed
    // TODO add your handling code here:

    Object s = "";
    do {//  w ww .j a  v a  2 s . c o  m
        s = JOptionPane.showInputDialog(this, "Geef Ip van MacFinish op", "ip", JOptionPane.PLAIN_MESSAGE, null,
                null, clockServer.getIp());
    } while (!InetAddressUtils.isIPv4Address((String) s));
    clockServer.changeIp((String) s);
}

From source file:org.dasein.cloud.google.network.FirewallSupport.java

/**
 * The Dasein abstraction assumes that a firewall rule has only one source target, one destination target, one protocol and one distinct port range
 * However, the GCE abstraction defines groups of these against one single firewall rule.
 * Therefore, Dasein splits these groups into distinct parts. The result is that, when Dasein creates rules on GCE they will matchup but when
 * Dasein is reading rules created on the GCE side there is the potential for each one to map to multiple Dasein rules.
 * @param rules the GCE API rule response
 * @return A Dasein collection of FirewallRules
 *//*  www  .  ja  v  a  2  s  . c o  m*/
private @Nonnull Collection<FirewallRule> toFirewallRules(
        @Nonnull List<com.google.api.services.compute.model.Firewall> rules) {
    ArrayList<FirewallRule> firewallRules = new ArrayList<FirewallRule>();
    for (com.google.api.services.compute.model.Firewall googleRule : rules) {
        for (String sourceRange : googleRule.getSourceRanges()) {
            //Right now GCE only supports IPv4
            if (InetAddressUtils.isIPv4Address(sourceRange))
                sourceRange = sourceRange + "/32";
            RuleTarget sourceTarget = RuleTarget.getCIDR(sourceRange);
            String vLanId = googleRule.getNetwork().substring(googleRule.getNetwork().lastIndexOf("/") + 1);

            for (Allowed allowed : googleRule.getAllowed()) {
                Protocol protocol = Protocol.valueOf(allowed.getIPProtocol().toUpperCase());
                RuleTarget destinationTarget;
                int portStart = 0;
                int portEnd = 0;

                if (protocol != Protocol.ICMP) {
                    for (String portString : allowed.getPorts()) {
                        if (portString.indexOf("-") > 0) {
                            String[] parts = portString.split("-");
                            portStart = Integer.valueOf(parts[0]);
                            portEnd = Integer.valueOf(parts[1]);
                        } else
                            portStart = portEnd = Integer.valueOf(portString);

                        if (googleRule.getTargetTags() != null) {
                            for (String targetTag : googleRule.getTargetTags()) {
                                destinationTarget = RuleTarget.getVirtualMachine(targetTag);
                                FirewallRule rule = FirewallRule.getInstance(googleRule.getName(),
                                        "fw-" + vLanId, sourceTarget, Direction.INGRESS, protocol,
                                        Permission.ALLOW, destinationTarget, portStart, portEnd);
                                firewallRules.add(rule);
                            }
                        } else {
                            destinationTarget = RuleTarget.getVlan(vLanId);
                            FirewallRule rule = FirewallRule.getInstance(googleRule.getName(), "fw-" + vLanId,
                                    sourceTarget, Direction.INGRESS, protocol, Permission.ALLOW,
                                    destinationTarget, portStart, portEnd);
                            firewallRules.add(rule);
                        }
                    }
                } else {
                    if (googleRule.getTargetTags() != null) {
                        for (String targetTag : googleRule.getTargetTags()) {
                            destinationTarget = RuleTarget.getVirtualMachine(targetTag);
                            FirewallRule rule = FirewallRule.getInstance(googleRule.getName(), "fw-" + vLanId,
                                    sourceTarget, Direction.INGRESS, protocol, Permission.ALLOW,
                                    destinationTarget, portStart, portEnd);
                            firewallRules.add(rule);
                        }
                    } else {
                        destinationTarget = RuleTarget.getVlan(vLanId);
                        FirewallRule rule = FirewallRule.getInstance(googleRule.getName(), "fw-" + vLanId,
                                sourceTarget, Direction.INGRESS, protocol, Permission.ALLOW, destinationTarget,
                                portStart, portEnd);
                        firewallRules.add(rule);
                    }
                }
            }
        }
    }
    return firewallRules;
}

From source file:de.yaacc.upnp.server.contentdirectory.YaaccContentDirectory.java

/**
 * get the ip address of the device/*from   www  . ja  va2 s .co m*/
 * 
 * @return the address or null if anything went wrong
 * 
 */
public String getIpAddress() {
    String hostAddress = null;
    try {
        for (Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
                .getNetworkInterfaces(); networkInterfaces.hasMoreElements();) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            for (Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); inetAddresses
                    .hasMoreElements();) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (!inetAddress.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {

                    hostAddress = inetAddress.getHostAddress();
                }

            }
        }
    } catch (SocketException se) {
        Log.d(YaaccUpnpServerService.class.getName(), "Error while retrieving network interfaces", se);
    }
    // maybe wifi is off we have to use the loopback device
    hostAddress = hostAddress == null ? "0.0.0.0" : hostAddress;
    return hostAddress;
}

From source file:org.deviceconnect.android.deviceplugin.chromecast.profile.ChromeCastMediaPlayerProfile.java

/**
 * IP??//from  w  w w .  ja v  a2 s.c  o m
 * 
 * @param   ??
 * @return  IP
 */
private String getIpAddress() {
    try {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();
            Enumeration<InetAddress> ipAddrs = networkInterface.getInetAddresses();
            while (ipAddrs.hasMoreElements()) {
                InetAddress ip = (InetAddress) ipAddrs.nextElement();
                String ipStr = ip.getHostAddress();
                if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipStr)) {
                    return ipStr;
                }
            }
        }
    } catch (SocketException e) {
        if (BuildConfig.DEBUG) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.mozilla.SUTAgentAndroid.SUTAgentAndroid.java

public static InetAddress getLocalInetAddress() throws SocketException {
    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
        NetworkInterface intf = en.nextElement();
        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
            InetAddress inetAddress = enumIpAddr.nextElement();
            if (!inetAddress.isLoopbackAddress()
                    && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                return inetAddress;
            }/*  w w  w  .  j  a v a 2  s . c om*/
        }
    }

    return null;
}

From source file:de.mangelow.throughput.NotificationService.java

private String getIPAddress() {

    try {/*w w w.  j  a  v a  2  s  .com*/
        String ipv4;
        List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
        if (nilist.size() > 0) {
            for (NetworkInterface ni : nilist) {
                List<InetAddress> ialist = Collections.list(ni.getInetAddresses());
                if (ialist.size() > 0) {
                    for (InetAddress address : ialist) {
                        if (!address.isLoopbackAddress()
                                && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) {
                            return ipv4;
                        }
                    }
                }

            }
        }

    } catch (SocketException ex) {
        if (D)
            ex.printStackTrace();
    }

    return "";

}