@Override protected boolean resolve(String ip) { try { dns.getByName(ip); return true; } catch (UnknownHostException e) { return false; } }
/** * @see org.apache.james.protocols.smtp.DNSService#getByName(java.lang.String) */ public InetAddress getByName(String host) throws UnknownHostException { return dns.getByName(host); }
@Override protected String resolve(String host) throws UnknownHostException { return dns.getByName(host).getHostName(); }
@Override protected String resolve(String host) throws UnknownHostException { return dns.getByName(host).getHostName(); }
/** * Get a Inet6Network for the given String. * * @param netspec * the String which is will converted to InetNetwork * @return network the InetNetwork * @throws java.net.UnknownHostException */ private InetNetwork getV6FromString(String netspec) throws UnknownHostException { if (netspec.endsWith("*")) { throw new UnsupportedOperationException("Wildcard for IPv6 not supported"); } // Netmask can be separated with % netspec = netspec.replaceAll("%", "/"); if (netspec.indexOf('/') == -1) { netspec += "/32768"; } return new Inet6Network(dnsService.getByName(netspec.substring(0, netspec.indexOf('/'))), new Integer(netspec.substring(netspec.indexOf('/') + 1))); }
/** * Simple localhost resolution. * * @throws UnknownHostException */ @Test public void testLocalhost() throws UnknownHostException { assertThat(DNS_SERVER.getByName("localhost").toString()).isEqualTo("localhost/127.0.0.1"); String localHost = DNS_SERVER.getHostName(InetAddress.getByName("127.0.0.1")); // We only can check if the returned localhost is not empty. Its value // depends on the hosts file. assertThat(localHost.length() > 0).isTrue(); }
/** * Simple apache.org resolution. * * @throws UnknownHostException */ @Test @Ignore(value = "It requires internet connection!") public void testApache() throws UnknownHostException { //TODO: move to some sort of Live tests assertThat(DNS_SERVER.getByName("www.apache.org").toString().startsWith("www.apache.org")).isTrue(); } }
/** * The given String may represent an IP address or a host name. * * @param hostIP * the ipAddress or host name to check * @see #matchInetNetwork(InetAddress) */ public boolean matchInetNetwork(String hostIP) { InetAddress ip; try { ip = dnsServer.getByName(hostIP); } catch (UnknownHostException uhe) { LOGGER.info("Cannot resolve address for {}: {}", hostIP, uhe.getMessage()); return false; } return matchInetNetwork(ip); }
/** * Get a Inet4Network for the given String. * * @param netspec * the String which is will converted to InetNetwork * @return network the InetNetwork * @throws java.net.UnknownHostException */ private InetNetwork getV4FromString(String netspec) throws UnknownHostException { if (netspec.endsWith("*")) { netspec = normalizeV4FromAsterisk(netspec); } else { int iSlash = netspec.indexOf('/'); if (iSlash == -1) { netspec += "/255.255.255.255"; } else if (netspec.indexOf('.', iSlash) == -1) { netspec = normalizeV4FromCIDR(netspec); } } return new Inet4Network(dnsService.getByName(netspec.substring(0, netspec.indexOf('/'))), dnsService.getByName(netspec.substring(netspec.indexOf('/') + 1))); }
@Override public Collection<MailAddress> match(Mail mail) { String host = mail.getRemoteAddr(); try { // Have to reverse the octets first StringBuilder sb = new StringBuilder(); StringTokenizer st = new StringTokenizer(host, " .", false); while (st.hasMoreTokens()) { sb.insert(0, st.nextToken() + "."); } // Add the network prefix for this blacklist sb.append(network); // Try to look it up dnsServer.getByName(sb.toString()); // If we got here, that's bad... it means the host // was found in the blacklist return mail.getRecipients(); } catch (UnknownHostException uhe) { // This is good... it's not on the list return null; } } }
public MXHostAddressIterator(Iterator<String> hosts, int defaultPort, DNSService dns, boolean useSingleIP) { checkNotNull(hosts, "Hosts is null"); checkNotNull(dns, "Dns is null"); final List<HostAddress> hAddresses = Lists.newArrayList(); while (hosts.hasNext()) { String nextHostname = hosts.next(); Map.Entry<String, String> hostAndPort = extractHostAndPort(nextHostname, defaultPort); try { final Collection<InetAddress> addrs; if (useSingleIP) { addrs = ImmutableList.of(dns.getByName(hostAndPort.getKey())); } else { addrs = dns.getAllByName(hostAndPort.getKey()); } for (InetAddress addr : addrs) { hAddresses.add(new HostAddress(hostAndPort.getKey(), "smtp://" + addr.getHostAddress() + ":" + hostAndPort.getValue())); } } catch (UnknownHostException uhe) { // this should never happen, since we just got // this host from mxHosts, which should have // already done this check. String logBuffer = "Couldn't resolve IP address for discovered host " + hostAndPort.getKey() + "."; LOGGER.error(logBuffer); } } addresses = hAddresses.iterator(); }
dnsService.getByName(address);
dnsService.getByName(address);
String ip = dnsService.getByName(mxRec).getHostAddress();
String ip = dnsService.getByName(mxRec).getHostAddress();