I've been having an issue with DNS lookup for IPv6 addresses that I can't seem to find good information about.
I'm trying to lookup an IPv6 address using InetAddress.getByName("ipv6.local.com"). It throws an UnknownHostException error.
The weird part is I know the DNS server can be contacted because this works:
InetAddress.getByName("ipv4.local.com")
I also know the IPv6 record is working because I can run:
nslookup ipv6.local.com
and it properly returns 3ffe:b00:0:1:4678:3eff:fe36:16e8.
Likewise, if I run the following in C++, I get a result with the above address as well:
int errorCode = getaddrinfo("ipv6.local.com", "4242", &hints, &res);
I have also tried Inet6Address.getByName(), but this also throws UnknownHostException. So why do getaddrinfo() and nslookup work and not InetAddress.getByName()?
I am attempting the DNS lookup from an Android device (Galaxy Tab S2 8") running Android 6.0.1 on the same network as the DNS server. The DNS server has a record "ipv4.local.com" pointing to 192.168.0.190, and a record "ipv6.local.com" pointing to 3ffe:b00:0:1:4678:3eff:fe36:16e8.
The DNS server is explicitly set in Wi-Fi settings on the Android device, and is running on 192.168.0.182.
Any ideas?
I am not sure why IPv6 resolution is not working for you. Here is the example of Java code(java version "1.8.0_171") I tested:
package com.myjava.ip;
import java.net.InetAddress;
import java.net.UnknownHostException;
class MyIpByHost {
public static void main(String a[]){
try {
InetAddress host = InetAddress.getByName("ipv6.google.com");
System.out.println(host.getHostAddress());
} catch (UnknownHostException ex) {
ex.printStackTrace();
}
}
}
Output: java com/myjava/ip/MyIpByHost => 2404:6800:4003:c02:0:0:0:8a
This code returns IPv6 for endpoint "ipv6.google.com".
Java run C functions in it's backend to get work done. As you already know getaddrinfo() function of C supports IPv6, but gethostbyname() does not.
You may be running outdated version of java with underlying C function
gethostbyname() which does not support IPv6. I
would suggest you to upgrade Java which will use getaddrinfo().
Related
I have a Android application which consumes a webservice on a local network. There's a config screen where the user inform the server IP address, which is running Apache Tomcat.
I'm looking for a way to auto-detect the server based on the current connected wi-fi network.
i.e: The smartphone's IP is 10.1.1.90 and the server IP is 10.1.1.254.
Is there a way to achieve this? I'm thinking on using ping, but I don't know if is a good ideia.
The way I understand it, you need to discover IP of your tomcat server and connect it using your client.
I am assuming , both the server and client is in your control.
One simple way can be to use jGroups Cluster.
You can make your tomcat discoverable
Client can discover it using the name of the cluster you have provided .Refer the JChannel API that Jgroups uses
I simulated it making following server class
public class TomcatServer {
JChannel channel;
private void start() throws Exception {
channel = new JChannel(); // use the default config, udp.xml
channel.connect("TomcatCluster");
}
public static void main(String[] args) throws Exception {
new TomcatServer().start();
}
}
The simulated client class
public class MobileApp extends ReceiverAdapter {
JChannel channel;
private void start() throws Exception {
channel = new JChannel(); // use the default config, udp.xml
channel.setReceiver(this);
channel.connect("TomcatCluster");
channel.close();
}
public static void main(String args[]) throws Exception {
new MobileApp().start();
}
The client will provide you following information
GMS: address=MACHINENAME-47879, cluster=TomcatCluster, physical address=xxxxx:0:xxx:xxxx:xxxx:xxxx:xxx:xxxx:xxxx
** view: [MACHINENAME-31239|1] [MACHINENAME-31239, MACHINENAME-47879]
Where MACHINENAME-47879 is the client machine and port & MACHINENAME-31239 is the tomcat server name and port
Do you want to detect "a tomcat server" or "your tomcat server" ?
I mean, do you have any way to custom your server ? If it's the case, then you could create a very simple test page on your server (say a "Hello" JSP page), which your Android application could look for.
If your Android gets a "Hello" result with a GET request on http://<tomcat_ip>/hello.jsp, then you may assume that the tomcat is online.
If you can't add this test page, then you can test any page which the server is supposed to serve. (even a 404 page which sometimes is not configured well, and shows the tomcat version...)
Tomcat response headers can contain the xpoweredBy field that would advertise Tomcat if enabled. However it is most often disabled due security considerations, and even disabled by default. You however could re-enable it if you need to auto-detect exactly your Tomcat servers. From the other side, indeed, if you can place a web page on your server, you can simply place a marking page with the agreed signature.
If the server IP is unknown, I would propose the following ways to detect the server on the network:
The most straightforward way is to do the breadcast ping (ping -b broadcast_address where breadcast address can be computed here, for instance). All network devices that are configured so would reply, then verify as explained above which one is the server. However pinging broadcast address requires a rooted phone. Also the router may not support.
Your DHCP service (most likely your router) can often be configured to issue always the same IP address for the same MAC address of your server network card.
If the server is a desktop computer or laptop, it could show its address as QR code on display. It is possible for a smartphone to scan the code from the screen, and this is way easier than to enter IP address through the touchscreen. QR code can also include auto-generated password for extra security.
If there is wireless router with the possible login where both server and client are connected, the internal pages of that router often contain the relevant IP addresses. You would need to implement logging into the router and doing some screen scrapping.
I made an Android app which used a local server in the WLAN. I made the terminal (the phone) broadcast it's own IP address, which the server then picked up.
I used MultiCast class on the phone, which added the ip-address of itself to the payload. The server always has a thread in multicast read class that obains the payload of the packet (which is the terminals ip-address). Set the terminal in datagram read state and send the servers ip-address to terminal.
Maybe are better ways, but a great way to get the ip-addresses of unknown terminals in the network.
The way i had resolved this problem is with the use of enumerations.
public String getLocalIpAddress()
{
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception ex) {
}
return null;
}
}
I have been working on this project where two modules on different machines need to be in communication through RMI.
I start both client and server modules on my laptop. RMI seems to work correctly when i am at work and connected to work network, but when i am home, connected to my home network it does not work. It says remote object could not be found.
Here is the method i use at CLIENT side to get the reference to remote object
public static MyRMIApp getRemoteApp() throws RemoteException, NotBoundException, AccessException {
Registry registry = LocateRegistry.getRegistry("localhost", 28999); // tried 127.0.0.1 instead of localhost here, still not working
MyRMIApp app = (MyRMIApp) registry.lookup("COM");
return app;
}
Digging up a bit with some debugging, when i check the object value returned from getRemoteApp method, it shows me the end point is 67.215.65.132. Which is openDNS i am using to connect to internet. Shouldn't that be 127.0.0.1 ?
Then i used my mobile internet and tried again. It seems to be working but end-point is not 127.0.0.1 again it is the address assigned to me, which is 192.168.x.x
So can anybody please tell me what is wrong i am doing here ? I really would appreciate the help.
Oh and this is the piece of code at SERVER side
//Somwhere up top
private final static MyRMIApp rmiApp = new RMIServer();
//Down below
MyRMIApp stub = (MyRMIApp) UnicastRemoteObject.exportObject(rmiApp, 0);
Registry registry = LocateRegistry.createRegistry(28999);
registry.rebind("COM", stub);
See item A.1 of the RMI FAQ: specifically, 'The appropriate workaround is to set the system property java.rmi.server.hostname when starting the server.'
I was trying to make a jFrame which had a button & a text-area/label, the motive being able to retrieve my systems IP Address, the problem is, when I use this code
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
InetAddress ownIP=InetAddress.getLocalHost();
jTextField1.setText(ownIP.getHostAddress());
}
catch (Exception e)
{
jTextField1.setText(e.getMessage());
}
}
But then this gives me back the loop back IP Address, 127.0.0.1 :(
I have static IP configured on my system, but then too that IP does not show up
I use NetBeans IDE 7.0 & Ubuntu 11.04
You can obtain all IP addresses for your system. Use the NetworkInterface.getNetworkInterfaces() method to retrieve all of the network interfaces. For each of the returned interfaces, use the getInetAddresses() method to retrieve all of the associated addresses.
FWIW, InetAddress.getLocalHost().getHostAddress() gives me my real ip address.
FYI, I ran it from a unit test within Eclipse on a macbook.
I'm trying to write a simple program using Java that, given an IP in either version 4 or 6 format, will return its FQDN. The following code works fine when given an ipv4 address, but will only return the given address when an ipv6 address is entered.
InetAddress inet;
try { inet = InetAddress.getByName(theIpAddress); }
catch(UnknownHostException e) { System.out.println("Unknown Host"); return; }
System.out.println(inet.getHostAddress(););
System.out.println(inet.getHostName(););
Whenever I enter an ipv6 getHostName() will only return the same ipv6, even when I know that the ipv6 will resolve to a FQDN. Also, if I enter an ipv6 host name, such as ipv6.google.com, in place of theIpAddress, the exception will occur.
I'm new to this stuff so would appreciate any assistance. Thanks.
The problem was actually the version of Java I was running. Updating Java to 1.6.23, from 1.6.21, allowed ipv6s to resolve to their FQDN.
I've done a quick investigation of what's going on with hostname <-> ipv6 resolution in java 8, Windows 7.
Looks like 'default' NameService does not work with ipv6 at all!
But! Java comes with another, JNDI based NameService implementation called 'dns,sun'.
So, if you enable it using either
-Dsun.net.spi.nameservice.provider.1=dns,sun
or
System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
you will get bidirectional ip <-> hostname resolution for v4 and v6 addresses like this
InetAddress.getAllByName(...)
address.getHostName()
More info about java ipv6 you can find here http://docs.oracle.com/javase/8/docs/technotes/guides/net/ipv6_guide/
Try inet.getCanonicalHostName(); which "Gets the fully qualified domain name for this IP address."
If you construct the InetAddress using InetAddress.getByName(), getHostName() will return what you constructed it with. getCanonicalHostName() forces a reverse name lookup.
Using java.net.InetAddress it is not possible to have ipv6 and ipv4 name resolution etc. The bunch of static methods like getByName etc delegate the lookup to instance of Inet4(or 6)AddressImpl that does
public native InetAddress[] lookupAllHostAddr(String hostname) throws UnknownHostException;
Now the fun is a) all these are private/package local so there is not way to inject the impl classes to the InetAddress class b) Inet4(or 6)AddressImpl classes are themselves package local . So there is no way to say , do a ipv4 or ipv6 lookup/name resolution on the fly. I do not get the way all extension points were blocked for these classes making them of really very limited use and flexibility.
The real black-magic happens here , where InetAddress class statically initializes the impls, on what does outcome of method isIPv6Supported() dependent ?? My Linux setup supports ipv6 , i can do dns lookups for ipv6 hostnames like ipv6.google.com.
Will appreciate if anybody can point me to the direction of a good net library in java for ipv4/v6 utilities like lookup etc.
class InetAddressImplFactory {
static InetAddressImpl create() {
Object o;
if (isIPv6Supported()) {
o = InetAddress.loadImpl("Inet6AddressImpl");
} else {
o = InetAddress.loadImpl("Inet4AddressImpl");
}
return (InetAddressImpl)o;
}
static native boolean isIPv6Supported();
}
I set up a static IP and did port forwarding on my notebook, and now I have a static IP address, but it's relatively static, every time I re-start the machine, I get another address, and since I have a "static" IP I can now do Paypal IPN messaging. But how can I get this static IP from my Java program ? One way I can think of is to visit : http://portforward.com/ and on that page it tells me what my external IP is, so I can extract it with Java code, is there any other way that I can do in my Java code to get this info ?
The best solution is probably dynamic DNS. Essentially, you run a program on your computer (or router) that notifies a DNS server when your IP changes. Then, you can just tell PayPal the domain name.
There is a public service you can call with your script to retrieve your external IP address. Bear in mind they have changed the link location once. If you control your own server, you should probably write your own PHP script to simply return the address of the caller to the script.
http://www.whatismyip.com/faq/automation.asp - follow the developers link they provide
import java.net.*;
import java.io.*;
URL myExternalIP = new URL("PUT THE LINK HERE");
BufferedReader in = new BufferedReader(new InputStreamReader(
myExternalIP.openStream()));
String ip = in.readLine(); //you get the IP as a String
System.out.println(ip);
Your own server script is a simple one-liner. Create a file called whatismyip.php and here's the content.
<? echo $_SERVER['REMOTE_ADDR']?>
I see three ways of doing this:
As you discovered, querying an external server what IP you're apparently connecting from. This is simple but you require such a service to be available and, usually, that no transparent proxy messes with your results.
IGD, a sub-protocol of UPnP can give you the result very easily if your port forwarding devices supports it (many do). Google "IGD JAVA" for libraries and samples.
Register for a dynamic DNS service and then lookup your own DNS name.
You can use the NetworkInterface class:
Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
while(ifcs.hasMoreElements()){
NetworkInterface ifc = ifcs.nextElement();
System.out.println("Adresses of: "+ifc.getDisplayName());
Enumeration<InetAddress> adresses = ifc.getInetAddresses();
while(adresses.hasMoreElements()){
System.out.println(adresses.nextElement().getHostAddress());
}
}
This snippet will show you all of the interfaces and the IPs bound to them. You will need to look through the list to find the appropriate interface (see also NetworkInterface.getByName(String name)) And then look at the addresses for that interface. Once you have found the appropriate InetAdress you can use that to get the string or byte representation.