After I registered dnsjava as default Java DNS provider I get a problem. It can't resolve local addresses which described in /etc/hosts file on my Linux machine. This file look something like this:
127.0.0.1 localhost
127.0.1.1 servername
So if I try to resolve one of such names UnknownHostException happens:
org.xbill.DNS.Address.getByName("localhost");
org.xbill.DNS.Address.getByName("servername");
It's not a problem when you're using dnsjava along with default dns provider. Being a sole provider, dnsjava causes lots of errors in default libraries, which turn out to be highly dependent on localhost resolution capability. So, the question is: how to change behavior of dnsjava to resolve local hostnames?
Edit. Next code works fine:
java.net.InetAddress.getByName("localhost");
But java.net.InetAddress.getLocalHost() method throws:
java.net.UnknownHostException: servername
dnsjava is a DNS client library; it talks to DNS servers. /etc/hosts is not part of the DNS protocol nor does dnsjava know anything about it.
See this old post on a the dnsjava users mailing list from the guy who wrote it: http://old.nabble.com/DNS-Resolve-from-hosts-file-first-then-DNS-Server-td15431381.html
Nothing has changed in that regard.
If java.net.InetAddress.getByName() is working, then your DNS server is configured to respond to queries for localhost.
Related
We are using grpc spring boot starter on our Java application service in order to establish a connection to another 'server' service, so I define in the application.properties the following address:
grpc.client.name.address=static://service-name:port
When tried to connect it I got the following error message:
StatusRuntimeException: UNAVAILABLE: io exception
So I know for sure I have a connectivity issue. On the documentation it says regarding the static scheme:
A simple static list of IPs (both v4 and v6), that can be use connect to the server
So I guess this is not what I need to use. It seems the best option in my case is using the discovery scheme, but it doesn't contains any port...
What is the right scheme configuration I need to use to set the server address?
Wanted to share the resolution for this very annoying issue for those who will encounter the same problem in the future like I did.
So first, the scheme needs to be set indeed of dns type, like the following: grpc.client._name_.address=dns:///<service-name>:26502
but this alone is not enough. (at least in my case) The server was configured to run in PLAINTEXT, while my client, by default, was configured to run with TLS mode, so it must be set with grpc.client.__name__.negotiationType=PLAINTEXT property.
See the following documentation for further information
It caused by gRPC can't resolve addresss service-name:port;
If you use static, the value must be ip:port; The service-name need to be resolved as ip address;
If you are using register center like consul or eureka etc., you should use discovery:///service-name without specify port.
If you didn't use register center, only end to end with server, replace service-name as a ip like 127.0.0.1 which belong to server;
Or modify host config for parse service-name like below, the file on Linux is /etc/hosts
127.0.0.1 service-name
I'm in need to develop a java library which allows a traffic to be directed via proxy only for specified hosts.
The library is almost ready and working, but there is problem with resolving dns addresses via proxy.
In short words I extended CustomProxySelector class which has following logic:
public class CustomProxySelector extends ProxySelector {
public List<Proxy> select(URI uri) {
if (customProxyDefinedFor(uri)) {
return getCustomProxyFor(uri);
} else {
// use direct connection
}
}
}
All works fine if local dns can resolve host given as "uri" parameter (for example if I want stackoverflow.com to go via proxy it will work because my local dns can resolve stackoverflow.com).
The problem comes when there is a host which is not known to my local dns. For example the dns behind proxy knows how to resolve address like "host1.private.dmz" because this is special host only known behind proxy (the proxy acts really as reverse proxy here). JVM seems to first try to resolve "host1.private.dmz" to ip, and when it fails it ends with folowing stacktrace:
Caused by: java.net.UnknownHostException: host1.private.dmz
at java.net.InetAddress.getAllByName0(InetAddress.java:1259)
at java.net.InetAddress.getAllByName(InetAddress.java:1171)
at java.net.InetAddress.getAllByName(InetAddress.java:1105)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:247)
(...)
Because it fails to resolve the ip, my Custom ProxySelector is never used. Is there any option to force java not to resolve ip via localdns but via proxy?
If I give the ip address of host1.private.dmz (for example 10.100.12.13) all works ok. The communication is directed to my Custom Proxy Selector and the traffic goes via custom proxy without problem.
I solved this issue. The important thing to fix this problem is correct understanding that the problem does not lay in jvm but in application. Jvm does not try to resolve host1.private.dmz before calling custom proxy selector, it is the application itself.
If we have a look at last line of the stacktrace you can see that exception comes from mysql jdbc driver, so it is mysql driver who trys to resolve host1.private.dmz to IP address, before actually opening connection to that host. Therefore because application does not open a connection (because exception occurs when application trys to resolve dns), no proxy selector is called ("no connection" == "no proxy selector").
What can we do in such case?
If it is you who writes the application, simply don't resolve the IP by calling InetAddress.getAllByName() and directly open connection to host domain name (host1.private.dmz). If for some reason you need an IP than handle the exception (in case of exception try to open connection without resolving the address). If still this is not acceptable for you there is one more option. You can instruct jvm to use extra DNS server which is able to resolve IP of this domain. You can do this by setting following properties:
System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
System.setProperty("sun.net.spi.nameservice.nameservers", "10.200.2.3,100.40.70.5);
This should set extra dns server for your application.
There can however be one more problematic situation. An attempt to resolve domain name to ip might take place before you have the chance to set up extra dns servers. For example you might be running web application on Tomcat with database connection pool configured in Tomcat's context. In such case the exception "UnknownHostException" can happen before you set up extra dnses. In such case you can run this application by "proxifying it". Strictly in java you can do this by using jProxyLoader library (http://jproxyloader.sourceforge.net) , for example by running the application with following parameters:
-Djava.system.class.loader=net.sf.jproxyloader.JProxyLoader -DjplDnsServers=10.0.1.18
Above example will set up 10.0.1.18 as extra dns server (which is able to resolve the uknown domain name) at application startup. Thanks to this extra dns will already be available when application boots up.
You understand more about this problem, by having a look at jProxyLoader troubleshooting page: http://jproxyloader.sourceforge.net/troubleshooting.html
I am using a virtual machine on a server.
My local IP of the VM is 192.168.1.10
I am trying to achieve something which requires me to edit my /etc/hosts as provided in this link
http://www.thatisjava.com/java-tech/55200/
I am having similar problems, My console reads
RTP--- :DataAddress: /192.168.1.10
ControlAddress: /192.168.1.10
DataPort: 42050
ControlPort: 42051
java.io.IOException: Local Data AddressDoes not belong to any of this hosts local interfaces
java.io.IOException: Local Data AddressDoes not belong to any of this hosts local interfaces
at org.speechforge.cairo.rtp.RTPConsumer.init(RTPConsumer.java:181)
at org.speechforge.cairo.rtp.RTPConsumer.<init>(RTPConsumer.java:95)
at org.speechforge.cairo.rtp.server.RTPStreamReplicator.<init> (RTPStreamReplicator.java:69)
And some more.
The answer to the problem given is
I solved this one. The problem is JMF seems to use
InetAddress.getAllByName() which returns (at least in my case) only
single IP address no matter how many addresses I have defined on my
interfaces. The problem was solved by placing my IP address that I
wanted to use by session manager into /etc/hosts. It must be the first
line in /etc/hosts otherwise the other row that matches is used.
Unfortunately, I cant make out what He is trying to state. My /etc/hosts read
127.0.0.1 localhost
127.0.1.1 SparkVM104
So am I supposed to change 127.0.0.1 with my 192.168.1.10
or am I supposed to create an alias like 127.0.0.1/192.168.1.10
or Shall I just paste 192.168.1.10 in the top as
192.168.1.10
127.0.0.1 localhost
127.0.1.1 SparkVM104
Any help is appreciated.
Regards.
The format for /etc/hosts is
IP_address canonical_hostname [aliases...]
(see http://linux.die.net/man/5/hosts), so to list 192.168.1.10 first, the /etc/hosts file would have to look like this:
192.168.1.10 SomeHostName SomeOtherHostName
127.0.0.1 localhost
In /etc/hosts, localhost should map to 127.0.0.1 and nothing else, and your external hostname to your external IP address and nothing else. Some Linux distributions are stated to violate this rule and it breaks everything.
i have one server installed in my system.It takes one of the ip addresses and acts as own system.
In my java program i connect to that server using the following code.
ServiceInstance si = new ServiceInstance(new URL("https://10.100.13.36/sdk"), "user", "password", true);
true indicates the ignore certificate to be true.
when i tried to execute with false parameter it says the following error.
CertificateException: No subject alternative names matching IP address 10.100.13.36 found
i got here that i have to use the dns name instead of ip address in case of certificate mode of server.
So i found in the server configuration settings that the server is taking the DNS name as localhost.localdomain
so i tried with the url as https://localhost.localdomain/sdk
it has shown the error that localhost.locadomain has not been found.
i guess it has some problem with DNS name resolution with localhost as my system and localhost.localdomain as my server dns name.
can we find the dns name with giving ip address through java program.So that it finds the DNS name on its own and pass the value in the url.
can anybody give some suggestion on this.
Thank you..
A DNS name is assigned by your local DNS server. The name localhost.localdomain is usually assigned in the host file so various local operations function (e.g., system logging, sending email to the root user from periodic tasks). I would guess that your server is not registered in the local DNS server. It is rare that locally run virtual machines actually have a valid registration in DNS - this is especially true for Linux systems running in a Windows environment.
If it were registered in DNS and both your server and client were using the same DNS hierarchy, then all that you need to do is create an java.net.InetAddress using the IP address of the machine and call getCanonicalHostName() to retrieve its DNS name. If it does not have a name registered in the local DNS server, then you will simply get the IP address back.
Please help;
Am trying to deploy a web application.
The build is returning successful,and am not getting any errors on the sever.log
however am getting a http 503 error:the requested service() is not currently available
and also my
Java DB Database Process has this message
Warning: UnknkownHostException: intracare: intracare.
Could not listen on port 1527 on host localhost.
am using glassfish v3 prelude
Possibly you already have another application (perhaps another JavaDB instance?) listening on port 1527 - that could explain the "could not listen" message. Use netstat to check.
I think that UnknownHostException is related that intracare can't be resolved to an IP.
I would:
Ping that host.
Check C:\WINDOWS\system32\drivers\etc\hosts or /etc/hosts files.
Regards.
Check the DNS settings on the host. Your short names do not resolve correctly.
Mayby string you are typing as a host name is wrong? Maybe it is firewall problem?