Search in sources :

Example 71 with Inet4Address

use of java.net.Inet4Address in project zm-mailbox by Zimbra.

the class GetServerNIFs method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext lc = getZimbraSoapContext(context);
    String ipAddressType = request.getAttribute(AdminConstants.A_TYPE, null);
    boolean ipV4 = false, ipV6 = false;
    if (StringUtil.equalIgnoreCase(ipAddressType, IPV6)) {
        ipV6 = true;
    } else if (StringUtil.equalIgnoreCase(ipAddressType, "both")) {
        ipV4 = true;
        ipV6 = true;
    } else {
        // ipv4 is the default type
        ipV4 = true;
    }
    Element serverEl = request.getElement(AdminConstants.E_SERVER);
    String method = serverEl.getAttribute(AdminConstants.A_BY);
    String serverName = serverEl.getText();
    Provisioning prov = Provisioning.getInstance();
    Server server = prov.get(Key.ServerBy.fromString(method), serverName);
    if (server == null) {
        throw ServiceException.INVALID_REQUEST("Cannot find server record for the host: " + serverName, null);
    }
    RemoteManager rmgr = RemoteManager.getRemoteManager(server);
    RemoteResult rr = rmgr.execute(RemoteCommands.ZM_SERVER_IPS);
    BufferedReader in = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(rr.getMStdout())));
    String line;
    Element response = lc.createElement(AdminConstants.GET_SERVER_NIFS_RESPONSE);
    try {
        while ((line = in.readLine()) != null) {
            Matcher IPmatcher = ADDR_PATTERN.matcher(line);
            Matcher maskMatcher = MASK_PATTERN.matcher(line);
            if (IPmatcher.find() && maskMatcher.find()) {
                String ipAddress = IPmatcher.group(VALUE_GROUP).toLowerCase();
                InetAddress addressType = InetAddress.getByName(ipAddress);
                if (addressType instanceof Inet6Address && !ipV6) {
                    continue;
                } else if (addressType instanceof Inet4Address && !ipV4) {
                    continue;
                }
                String type = (addressType instanceof Inet4Address) ? IPV4 : IPV6;
                Element elNIF = response.addElement(AdminConstants.E_NI);
                elNIF.addElement(AdminConstants.E_A).addAttribute(AdminConstants.A_N, IPmatcher.group(KEY_GROUP).toLowerCase()).addAttribute(AdminConstants.A_TYPE, type).setText(ipAddress);
                elNIF.addElement(AdminConstants.E_A).addAttribute(AdminConstants.A_N, maskMatcher.group(KEY_GROUP).toLowerCase()).setText(maskMatcher.group(VALUE_GROUP));
            }
        }
    } catch (IOException e) {
        throw ServiceException.FAILURE("exception occurred handling CLI command", e);
    }
    return response;
}
Also used : RemoteResult(com.zimbra.cs.rmgmt.RemoteResult) Inet4Address(java.net.Inet4Address) Server(com.zimbra.cs.account.Server) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) RemoteManager(com.zimbra.cs.rmgmt.RemoteManager) Element(com.zimbra.common.soap.Element) Inet6Address(java.net.Inet6Address) IOException(java.io.IOException) Provisioning(com.zimbra.cs.account.Provisioning) ByteArrayInputStream(java.io.ByteArrayInputStream) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) BufferedReader(java.io.BufferedReader) InetAddress(java.net.InetAddress)

Example 72 with Inet4Address

use of java.net.Inet4Address in project kcanotify by antest1.

the class KcaVpnService method getBuilder.

private Builder getBuilder(List<Rule> listAllowed, List<Rule> listRule) {
    SharedPreferences prefs = getSharedPreferences("pref", Context.MODE_PRIVATE);
    boolean subnet = prefs.getBoolean("subnet", false);
    boolean tethering = prefs.getBoolean("tethering", false);
    boolean lan = prefs.getBoolean("lan", false);
    boolean ip6 = prefs.getBoolean("ip6", true);
    boolean filter = prefs.getBoolean("filter", false);
    boolean system = prefs.getBoolean("manage_system", false);
    // Build VPN service
    Builder builder = new Builder();
    builder.setSession(getString(R.string.app_name));
    // VPN address
    String vpn4 = prefs.getString("vpn4", "10.1.10.1");
    Log.i(TAG, "vpn4=" + vpn4);
    builder.addAddress(vpn4, 32);
    if (ip6) {
        String vpn6 = prefs.getString("vpn6", "fd00:1:fd00:1:fd00:1:fd00:1");
        Log.i(TAG, "vpn6=" + vpn6);
        builder.addAddress(vpn6, 128);
    }
    // DNS address
    if (filter)
        for (InetAddress dns : getDns(KcaVpnService.this)) {
            if (ip6 || dns instanceof Inet4Address) {
                Log.i(TAG, "dns=" + dns);
                builder.addDnsServer(dns);
            }
        }
    // Subnet routing
    if (subnet) {
        // Exclude IP ranges
        List<IPUtil.CIDR> listExclude = new ArrayList<>();
        // localhost
        listExclude.add(new IPUtil.CIDR("127.0.0.0", 8));
        if (tethering) {
            // USB tethering 192.168.42.x
            // Wi-Fi tethering 192.168.43.x
            listExclude.add(new IPUtil.CIDR("192.168.42.0", 23));
            // Wi-Fi direct 192.168.49.x
            listExclude.add(new IPUtil.CIDR("192.168.49.0", 24));
        }
        if (lan) {
            try {
                Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
                while (nis.hasMoreElements()) {
                    NetworkInterface ni = nis.nextElement();
                    if (ni != null && ni.isUp() && !ni.isLoopback() && ni.getName() != null && !ni.getName().startsWith("tun"))
                        for (InterfaceAddress ia : ni.getInterfaceAddresses()) if (ia.getAddress() instanceof Inet4Address) {
                            IPUtil.CIDR local = new IPUtil.CIDR(ia.getAddress(), ia.getNetworkPrefixLength());
                            Log.i(TAG, "Excluding " + ni.getName() + " " + local);
                            listExclude.add(local);
                        }
                }
            } catch (SocketException ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
            }
        }
        Configuration config = getResources().getConfiguration();
        if (config.mcc == 310 && config.mnc == 260) {
            // T-Mobile Wi-Fi calling
            listExclude.add(new IPUtil.CIDR("66.94.2.0", 24));
            listExclude.add(new IPUtil.CIDR("66.94.6.0", 23));
            listExclude.add(new IPUtil.CIDR("66.94.8.0", 22));
            listExclude.add(new IPUtil.CIDR("208.54.0.0", 16));
        }
        // broadcast
        listExclude.add(new IPUtil.CIDR("224.0.0.0", 3));
        Collections.sort(listExclude);
        try {
            InetAddress start = InetAddress.getByName("0.0.0.0");
            for (IPUtil.CIDR exclude : listExclude) {
                Log.i(TAG, "Exclude " + exclude.getStart().getHostAddress() + "..." + exclude.getEnd().getHostAddress());
                for (IPUtil.CIDR include : IPUtil.toCIDR(start, IPUtil.minus1(exclude.getStart()))) try {
                    builder.addRoute(include.address, include.prefix);
                } catch (Throwable ex) {
                    Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                }
                start = IPUtil.plus1(exclude.getEnd());
            }
            for (IPUtil.CIDR include : IPUtil.toCIDR("224.0.0.0", "255.255.255.255")) try {
                builder.addRoute(include.address, include.prefix);
            } catch (Throwable ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
            }
        } catch (UnknownHostException ex) {
            Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        }
    } else {
        String addresses = prefs.getString("bypass_address", "");
        if (!addresses.equals("")) {
            List<IPUtil.CIDR> listExclude = new ArrayList<>();
            for (String cidr : addresses.split(",")) {
                String[] cidr_split = cidr.trim().split("/");
                listExclude.add(new IPUtil.CIDR(cidr_split[0], Integer.parseInt(cidr_split[1])));
            }
            Collections.sort(listExclude);
            try {
                InetAddress start = InetAddress.getByName("0.0.0.0");
                for (IPUtil.CIDR exclude : listExclude) {
                    for (IPUtil.CIDR include : IPUtil.toCIDR(start, IPUtil.minus1(exclude.getStart()))) try {
                        builder.addRoute(include.address, include.prefix);
                    } catch (Throwable ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }
                    start = IPUtil.plus1(exclude.getEnd());
                }
                for (IPUtil.CIDR include : IPUtil.toCIDR(start.getHostAddress(), "255.255.255.255")) try {
                    builder.addRoute(include.address, include.prefix);
                } catch (Throwable ex) {
                    Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                }
            } catch (UnknownHostException ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
            }
        } else
            builder.addRoute("0.0.0.0", 0);
    }
    Log.i(TAG, "IPv6=" + ip6);
    if (ip6)
        builder.addRoute("0:0:0:0:0:0:0:0", 0);
    // MTU
    int mtu = jni_get_mtu();
    Log.i(TAG, "MTU=" + mtu);
    builder.setMtu(mtu);
    return builder;
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) Configuration(android.content.res.Configuration) UnknownHostException(java.net.UnknownHostException) SharedPreferences(android.content.SharedPreferences) InterfaceAddress(java.net.InterfaceAddress) ArrayList(java.util.ArrayList) IPUtil(eu.faircode.netguard.IPUtil) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 73 with Inet4Address

use of java.net.Inet4Address in project geode by apache.

the class Server method formJMXServiceURLString.

private String formJMXServiceURLString(String host, int jmxPort) throws UnknownHostException {
    String jmxSerURL = "";
    InetAddress inetAddr = InetAddress.getByName(host);
    if (inetAddr instanceof Inet4Address) {
        // Create jmx service url for IPv4 address
        jmxSerURL = "service:jmx:rmi://" + host + "/jndi/rmi://" + host + ":" + jmxPort + "/jmxrmi";
    } else if (inetAddr instanceof Inet6Address) {
        // Create jmx service url for IPv6 address
        jmxSerURL = "service:jmx:rmi://[" + host + "]/jndi/rmi://[" + host + "]:" + jmxPort + "/jmxrmi";
    }
    return jmxSerURL;
}
Also used : Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 74 with Inet4Address

use of java.net.Inet4Address in project undertow by undertow-io.

the class MCMPAdvertiseTask method advertise.

static void advertise(final ModClusterContainer container, final MCMPConfig.AdvertiseConfig config, final XnioWorker worker) throws IOException {
    InetSocketAddress bindAddress;
    final InetAddress group = InetAddress.getByName(config.getAdvertiseAddress());
    if (group == null) {
        bindAddress = new InetSocketAddress(config.getAdvertisePort());
    } else {
        bindAddress = new InetSocketAddress(group, config.getAdvertisePort());
    }
    MulticastMessageChannel channel;
    try {
        channel = worker.createUdpServer(bindAddress, null, OptionMap.EMPTY);
    } catch (IOException e) {
        if (group != null && (linuxLike || windows)) {
            //try again with no group
            //see UNDERTOW-454
            UndertowLogger.ROOT_LOGGER.potentialCrossTalking(group, (group instanceof Inet4Address) ? "IPv4" : "IPv6", e.getLocalizedMessage());
            bindAddress = new InetSocketAddress(config.getAdvertisePort());
            channel = worker.createUdpServer(bindAddress, null, OptionMap.EMPTY);
        } else {
            throw e;
        }
    }
    final MCMPAdvertiseTask task = new MCMPAdvertiseTask(container, config, channel);
    //execute immediately, so there is no delay before load balancing starts working
    channel.getIoThread().execute(task);
    channel.getIoThread().executeAtInterval(task, config.getAdvertiseFrequency(), TimeUnit.MILLISECONDS);
}
Also used : Inet4Address(java.net.Inet4Address) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) InetAddress(java.net.InetAddress) MulticastMessageChannel(org.xnio.channels.MulticastMessageChannel)

Example 75 with Inet4Address

use of java.net.Inet4Address in project gerrit by GerritCodeReview.

the class SocketUtilTest method testIsIPv6.

@Test
public void testIsIPv6() throws UnknownHostException {
    final InetAddress ipv6 = getByName("1:2:3:4:5:6:7:8");
    assertTrue(ipv6 instanceof Inet6Address);
    assertTrue(isIPv6(ipv6));
    final InetAddress ipv4 = getByName("127.0.0.1");
    assertTrue(ipv4 instanceof Inet4Address);
    assertFalse(isIPv6(ipv4));
}
Also used : Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

Inet4Address (java.net.Inet4Address)184 InetAddress (java.net.InetAddress)85 Inet6Address (java.net.Inet6Address)45 NetworkInterface (java.net.NetworkInterface)39 UnknownHostException (java.net.UnknownHostException)28 LinkAddress (android.net.LinkAddress)24 SocketException (java.net.SocketException)23 IOException (java.io.IOException)22 ArrayList (java.util.ArrayList)19 InterfaceAddress (java.net.InterfaceAddress)17 ByteBuffer (java.nio.ByteBuffer)17 RouteInfo (android.net.RouteInfo)12 LinkProperties (android.net.LinkProperties)7 InetSocketAddress (java.net.InetSocketAddress)6 Test (org.junit.Test)6 WifiDisplay (android.hardware.display.WifiDisplay)5 WifiDisplaySessionInfo (android.hardware.display.WifiDisplaySessionInfo)5 DhcpResults (android.net.DhcpResults)5 IpConfiguration (android.net.IpConfiguration)5 IpAssignment (android.net.IpConfiguration.IpAssignment)5