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);
}
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));
}
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;
}
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;
}
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;
}
Aggregations