List of usage examples for io.netty.util NetUtil isValidIpV6Address
public static boolean isValidIpV6Address(CharSequence ip)
From source file:com.linecorp.armeria.client.Endpoint.java
License:Apache License
private static Endpoint create(String host, int port) { requireNonNull(host, "host"); if (NetUtil.isValidIpV4Address(host)) { return new Endpoint(host, host, port, DEFAULT_WEIGHT, HostIpAddrType.IPv4); }//from ww w . ja va2 s . c o m if (NetUtil.isValidIpV6Address(host)) { final String ipV6Addr; if (host.charAt(0) == '[') { // Strip surrounding '[' and ']'. ipV6Addr = host.substring(1, host.length() - 1); } else { ipV6Addr = host; } return new Endpoint(ipV6Addr, ipV6Addr, port, DEFAULT_WEIGHT, HostIpAddrType.IPv6); } return new Endpoint(InternetDomainName.from(host).toString(), null, port, DEFAULT_WEIGHT, null); }
From source file:com.linecorp.armeria.client.Endpoint.java
License:Apache License
/** * Returns a new host endpoint with the specified IP address. * * @return the new endpoint with the specified IP address. * {@code this} if this endpoint has the same IP address. * * @throws IllegalStateException if this endpoint is not a host endpoint *//*from w w w. j av a2 s.com*/ public Endpoint withIpAddr(@Nullable String ipAddr) { ensureSingle(); if (ipAddr == null) { return withoutIpAddr(); } if (NetUtil.isValidIpV4Address(ipAddr)) { return withIpAddr(ipAddr, HostIpAddrType.IPv4); } if (NetUtil.isValidIpV6Address(ipAddr)) { if (ipAddr.charAt(0) == '[') { ipAddr = ipAddr.substring(1, ipAddr.length() - 1); } return withIpAddr(ipAddr, HostIpAddrType.IPv6); } throw new IllegalArgumentException("ipAddr: " + ipAddr + " (expected: an IP address)"); }
From source file:com.linecorp.armeria.spring.ArmeriaAutoConfiguration.java
License:Apache License
private static void configurePorts(ArmeriaSettings armeriaSettings, ServerBuilder server) { if (armeriaSettings.getPorts().isEmpty()) { server.port(new ServerPort(DEFAULT_PORT.getPort(), DEFAULT_PORT.getProtocol())); return;/*from w ww.ja va2 s.co m*/ } for (Port p : armeriaSettings.getPorts()) { final String ip = p.getIp(); final String iface = p.getIface(); final int port = p.getPort(); final SessionProtocol proto = p.getProtocol(); if (ip == null) { if (iface == null) { server.port(new ServerPort(port, proto)); } else { try { final Enumeration<InetAddress> e = NetworkInterface.getByName(iface).getInetAddresses(); while (e.hasMoreElements()) { server.port(new ServerPort(new InetSocketAddress(e.nextElement(), port), proto)); } } catch (SocketException e) { throw new IllegalStateException("Failed to find an iface: " + iface, e); } } } else if (iface == null) { if (NetUtil.isValidIpV4Address(ip) || NetUtil.isValidIpV6Address(ip)) { final byte[] bytes = NetUtil.createByteArrayFromIpAddressString(ip); try { server.port(new ServerPort(new InetSocketAddress(InetAddress.getByAddress(bytes), port), proto)); } catch (UnknownHostException e) { // Should never happen. throw new Error(e); } } else { throw new IllegalStateException("invalid IP address: " + ip); } } else { throw new IllegalStateException("A port cannot have both IP and iface: " + p); } } }
From source file:io.vertx.core.dns.impl.netty.UnixResolverDnsServerAddressStreamProvider.java
License:Apache License
private static Map<String, DnsServerAddresses> parse(File... etcResolverFiles) throws IOException { Map<String, DnsServerAddresses> domainToNameServerStreamMap = new HashMap<String, DnsServerAddresses>( etcResolverFiles.length << 1); for (File etcResolverFile : etcResolverFiles) { if (!etcResolverFile.isFile()) { continue; }//www . j a v a 2 s .c o m FileReader fr = new FileReader(etcResolverFile); BufferedReader br = null; try { br = new BufferedReader(fr); List<InetSocketAddress> addresses = new ArrayList<InetSocketAddress>(2); String domainName = etcResolverFile.getName(); int port = DNS_PORT; String line; while ((line = br.readLine()) != null) { line = line.trim(); char c; if (line.isEmpty() || (c = line.charAt(0)) == '#' || c == ';') { continue; } if (line.startsWith(NAMESERVER_ROW_LABEL)) { int i = indexOfNonWhiteSpace(line, NAMESERVER_ROW_LABEL.length()); if (i < 0) { throw new IllegalArgumentException("error parsing label " + NAMESERVER_ROW_LABEL + " in file " + etcResolverFile + ". value: " + line); } String maybeIP = line.substring(i); // There may be a port appended onto the IP address so we attempt to extract it. if (!NetUtil.isValidIpV4Address(maybeIP) && !NetUtil.isValidIpV6Address(maybeIP)) { i = maybeIP.lastIndexOf('.'); if (i + 1 >= maybeIP.length()) { throw new IllegalArgumentException("error parsing label " + NAMESERVER_ROW_LABEL + " in file " + etcResolverFile + ". invalid IP value: " + line); } port = Integer.parseInt(maybeIP.substring(i + 1)); maybeIP = maybeIP.substring(0, i); } addresses.add(SocketUtils.socketAddress(maybeIP, port)); } else if (line.startsWith(DOMAIN_ROW_LABEL)) { int i = indexOfNonWhiteSpace(line, DOMAIN_ROW_LABEL.length()); if (i < 0) { throw new IllegalArgumentException("error parsing label " + DOMAIN_ROW_LABEL + " in file " + etcResolverFile + " value: " + line); } domainName = line.substring(i); if (!addresses.isEmpty()) { putIfAbsent(domainToNameServerStreamMap, domainName, addresses); } addresses = new ArrayList<InetSocketAddress>(2); } else if (line.startsWith(PORT_ROW_LABEL)) { int i = indexOfNonWhiteSpace(line, PORT_ROW_LABEL.length()); if (i < 0) { throw new IllegalArgumentException("error parsing label " + PORT_ROW_LABEL + " in file " + etcResolverFile + " value: " + line); } port = Integer.parseInt(line.substring(i)); } else if (line.startsWith(SORTLIST_ROW_LABEL)) { logger.info("row type {} not supported. ignoring line: {}", SORTLIST_ROW_LABEL, line); } } if (!addresses.isEmpty()) { putIfAbsent(domainToNameServerStreamMap, domainName, addresses); } } finally { if (br == null) { fr.close(); } else { br.close(); } } } return domainToNameServerStreamMap; }