English 中文(简体)
Cannot从获得东道方名 东道国
原标题:Cannot get hostname from getHostName
  • 时间:2009-12-14 06:41:01
  •  标签:
  • java

我试图利用这种方法获得东道国名/子宫名称。 不幸的是,我只能得到当地托管,而不是其他电脑。

private String getHostName(String _strIP) {
    try {
        InetAddress inetAddress = InetAddress.getByName(_strIP);
        System.out.println("getHostAddress : " + inetAddress.getHostAddress());
        System.out.println("getHostName : " + inetAddress.getHostName());
        System.out.println("getCanonicalHostName : " + inetAddress.getCanonicalHostName());
        return inetAddress.getHostName();            
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return strDefaultHostName;
}

the result (not localhost)

getHostAddress : 192.168.2.139
getHostName : 192.168.2.139
getCanonicalHostName : 192.168.2.139

the result (localhost)

getHostAddress : 127.0.0.1
getHostName : localhost
getCanonicalHostName : localhost

谢谢。

最佳回答

页: 1 然后将IP编号退回。

问题回答

我们大致确定了问题在于什么答案。

我认为,你只能把东道国名列入你们的档案来解决这个问题。

%SystemRoot%system32driversetchosts

这是你重新寻找的档案;这里界定了当地托管人。 你们想给你们想要解决的每一个东道国打上一个名字和地址。

我从未尝试过。 如果做不到工作,你会回钱。


<>Update>

以上是“快车”解决办法。 这实质上意味着,如果有人手工改变你对东道国的IP地址,则某人必须同时修改<代码>>> 东道方/代码>关于希望访问这些东道国的任何机器的档案。

另一种选择是操作你自己的计算机服务器。 你们仍然需要在东道国处理变化时更新IP地址,但你只需要在一个地方这样做,而且你们在整个网络中都能够向前推进和扭转姓名的解决。 这需要建立更多的机构,但从长远来看,更加容易维持。

http://www.dns.net/dnsrd/servers/windows.html http://www.dns.net/dnsrd/servers/windows.html。

他们提到,微软DNS服务器的“设计”是一个可怕的解决办法(直至Windows 2003服务器的安装),但至少提到了两种替代方法,一种是商业的,一种是免费的。 BIND是目前把大部分因特网连接起来的,DNS-Wise,而且他们也拥有一个Windows港。

Looking at the source for InetAddress.getHostName() (Sun JDK8)...

这种方法符合以下逻辑:

  1. Loops through the available sun.net.spi.nameservice.NameService s
  2. Performs a reverse DNS lookup - e.g. 192.168.0.23 -> frodo.baggins.com.au
  3. *Checks with the java.lang.SecurityManager, to see if "we have permission to connect" to hostname
  4. *Performs a forward DNS lookup on the hostname, to prevent spoofing - e.g. frodo.baggins.com.au -> 192.168.0.99
  5. If forward lookup result matches the original address (e.g. 192.168.0.23 == 192.168.0.99?), return hostname, otherwise return getHostAddress()

* 第3或4步向<条码>安全Exception>>/ Un knownHostException, 返回代码>getHostAddress(

对我来说,#2成功解决了东道国名称,但未能在“# Un knownHostException上签字。


TLDR; you must fulfill ALL of the following requirements:

  1. the SecurityManager must provide permission to access the host
  2. you must be able to forward AND reverse DNS lookup your InetAddress
  3. the forward lookup details MUST match the reverse lookup details

只有到那时, Java才会给你主名。


OR, you could bypass these steps with the following method, and just get the hostname.

@SuppressWarnings("unchecked")
public static String getHostName(InetAddress addr) {
    String host = null;
    List<NameService> nameServicesImpl = new ArrayList<>();
    try {
        // do naughty things...
        Field nameServices = InetAddress.class.getDeclaredField("nameServices");
        nameServices.setAccessible(true);
        nameServicesImpl = (List<NameService>) nameServices.get(null);
    } catch (Throwable t) {
        throw new RuntimeException("Got caught doing naughty things.", t);
    }
    for (NameService nameService : nameServicesImpl) {
        try {
            // lookup the hostname...
            host = nameService.getHostByAddr(addr.getAddress());
        } catch (Throwable t) {
            // NOOP: problem getting hostname from this name service, continue looping...
        }
    }
    return host != null ? host : addr.getHostAddress();
}

The javadoc of InetAddress.getCanonicalHostName() 说:

为这一IP地址找到完全合格的域名。 最佳努力方法,即我们可能无法根据基本系统配置返回全国住户抽样调查。

如果是一位安全主管,这种方法首先将其核对方法与东道方名称和-1称呼,以作为其论点,看看是否允许打电话的代码知道这一IP地址的主名,即与东道国联系。 如果不允许开展这项行动,它将归还IP地址/地址的文字代表。

我认为,你的系统配置是正确的。 你们是否从一片 app子里跑?

Reply Feedback for Carl Smotricz

Great answer, but we still don t know if the host name has been updated or not... This is something like we hardcode.

感谢你们

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a  #  symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
192.168.2.139       dev-testing

问题可能是多方面原因造成的。

Reason 1: the IP address doesn t have a hostname

这可能是最常见的原因,与安保管理人员毫无关系。

如果IP地址不向东道方表示决心,因为没有东道方名称,那么你将期望getHostName(>null或扔下<> Un knownHostException,但情况并非如此。 相反,getHostName()简单地将IP地址重新定位为背后。 由于我所不知的原因,这一共同局势没有记载。

因此,如果IP地址与getHostName()所返还的结果相同,那么,东道方名称就不存在。

Detailed explanation

The following JDK code is the cause of this undocumented problem: https://github.com/openjdk/jdk/blob/jdk-17+35/src/java.base/share/classes/java/net/InetAddress.java#L697

public class InetAddress implements java.io.Serializable {
    private static String getHostFromNameService(InetAddress addr, boolean check) {
        String host = null;
            try {
                // first lookup the hostname
                host = nameService.getHostByAddr(addr.getAddress());

                /* check to see if calling code is allowed to know
                 * the hostname for this IP address, ie, connect to the host
                 */
                if (check) {
                    @SuppressWarnings("removal")
                    SecurityManager sec = System.getSecurityManager();
                    if (sec != null) {
                        sec.checkConnect(host, -1);
                    }
                }

                /* now get all the IP addresses for this hostname,
                 * and make sure one of them matches the original IP
                 * address. We do this to try and prevent spoofing.
                 */

                InetAddress[] arr = InetAddress.getAllByName0(host, check);
                boolean ok = false;

                if(arr != null) {
                    for(int i = 0; !ok && i < arr.length; i++) {
                        ok = addr.equals(arr[i]);
                    }
                }

                //XXX: if it looks a spoof just return the address?
                if (!ok) {
                    host = addr.getHostAddress();
                    return host;
                }
            } catch (SecurityException e) {
                host = addr.getHostAddress();
            } catch (UnknownHostException e) {
                host = addr.getHostAddress();
                // let next provider resolve the hostname
            }
        return host;
    }
}

因此,所发生的情况是,IP-address被通过到。 姓名服务。

Lookup the host corresponding to the IP address provided
@param addr byte array representing an IP address
@return {@code String} representing the host name mapping
@throws UnknownHostException if no host found for the specified IP address

dNameservice.getHostByAddr(>,在IP没有东道方名但的情况下,将“无名HostException 。 InetAddress.getHostFromNameservice() 吞s了这一例外,而是回到了所提供的IP-address本身!! 海事组织本应放弃这一例外,而不是吞.,因为吞.使客户更难确定是否存在东道名称。

You can check if the IP address has a hostname by using the nslookup commandline tool: nslookup 192.168.2.139. If it returns something like: ** server can t find 139.2.168.192.in-addr.arpa: NXDOMAIN (Linux) or *** can t find 192.168.2.139: Non-existent domain (Windows) then there is no hostname.

Reason 2: a security manager is applied

否则,Java确实有一位安全主管。 在这种情况下,这一理由不适用。

一位安保主管是确定申请的安全政策的对象。 如果你有一位安全主管,如果你是问题的根源,那么你就应当检查一下,它是否允许你向已解决的东道方(如果有的话)开一个记号。 为此,首先使用<代码>slookup 192.168.2.139<>/code>,并核实是否解决了东道国名。 如果东道国名称得不到解决,那么你的问题是“Reason 1”造成的。 如果它确实向东道国表示决心,例如<密码> myhostname,那么就会尝试:

SecurityManager sec = System.getSecurityManager();
if (sec != null) {
    sec.checkConnect("myhostname", -1);
}

如果checkConnect( 接着,安全管理公司积极开展活动,并造成问题。 因此,你可以研究你如何能够没收你的安全。 管理者解决该问题。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...