InetAddress.getByName returning wrong IP address - java

I have been using the following code on a server.
public SocketServer(int port,String inetAddress) throws IOException {
this.port = port;
this.ia = InetAddress.getByName(inetAddress);
log.info(String.format("Internet Address %s using port %d for provided IP Address %s", this.ia.toString() ,this.port ,inetAddress.toString()));
s = new ServerSocket(port,50,this.ia );
}
This works fine on my local server but on production it is providing wrong address.
Production server do contain following type of IPs:-
Private IP
VPN IP
Public IP
I am providing private IP and expecting the server to connect using that private IP but instead it is connecting using the VPN IP.
One more thing i though to do was to use InetAddress.getByAddress() but i am unable to convert my IP in string to a byte array.
Can anyone suggest me any solution in this regard?

If I am not mistaken this might be a problem related to DNS. InetAddress.getByName(String host) will return the first IP address assigned to a certain domain name.
I.e. if in your /etc/hosts file you have something like this
192.168.1.1 sandbox1
192.168.1.2 sandbox1
The code
InetAddress.getByName("sandbox1")
will always give you 192.168.1.1
Hope this helps!

Related

IPAddress are returning different on LAN net and ZONG 4G

IPAddress are returning different on LAN net and ZONG 4G.
I want to know the IP addresses of clients in java application so i can restrict the users for login and other roles.
But problem is that when i run below code on LAN net it returns correct IPV4 address, But if I connect Zong 4G Device it only returns 192.168.10.100 on any computer.
How to get IP Address of client?
My code:
InetAddress address = InetAddress.getLocalHost();
String ip = address.getHostAddress();
String host = address.getHostName();
System.out.println("IP Address = " + ip);
System.out.println("host= " + host);
192.168.10.100 mean that your client is behind the NAT. There is thing called UPnP that may help you (I'm not expert and not really sure).
Alternative approach is to connect to server and ask it what it thing your IP is. It also have limitation, clients behind same NAT will have same IP.
In general building security based on IP address is bad idea.

Getting client static IP address

I am deplyong a web application in the Internet and I am checking whether the user's login is valid by checking its client IP address (e.g. 192.168.2.XXX). Actually, I have found a working code (below). This code was completely working before, but after some time, its output is not the same anymore. Now, this code only gives me 1 IP address which is the server's IP address. What is wrong with my code? How can I get the client's static IP address rather than the server's IP address? I have also tried other solutions such as getRemoteAddr() and request.getHeader("PROXY-CLIENT-IP") but it is not working.
Java:
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if(ipAddress == null)
ipAddress = InetAddress.getLocalHost().getHostAddress();
Your are mixing two levels of abstractions and that is rarely a good thing.
The header X-FORWARDED_FOR is inserted by a load balancer or proxy. If the client reaches the server directly, then this header isn't present and you are executing this code InetAddress.getLocalHost().getHostAddress();. Which does exactly what you say: It is retrieving the IP address of the host where this piece of code is running, i.e. the web server.
See also here: Getting the client IP address: REMOTE_ADDR, HTTP_X_FORWARDED_FOR, what else could be useful?

why ip address differs when wireless is off?

public class Main {
public static void main(String[] args) throws IOException {
InetAddress myIp = null;
try {
myIp = InetAddress.getLocalHost();
} catch (UnknownHostException ex) {
System.out.println("Exception cought.");
System.exit(0);
}
System.out.println(myIp);
}
}
I have this simple question that why my ip address is different when my wireless is off?
it's still the same computer, so why does it change? (isn't this a unique number?)
The IP address of the computer depends on the network it's connected to (and indeed, the same machine may have more than one, if it has multiple adapers).
So if I connect my machine to one of my networks, it may have the address 192.168.10.7 whereas on another of my networks, it may be 192.168.17.12. It can vary between connections as well, although in practice they tend to be a bit sticky. (It depends on how the DHCP server is configured.)
Your adapter can be configured with a fixed address, but if you do that, it has to be an address the network it's connecting to has reserved for it. Otherwise it may not work at all ("No route to host") or may conflict with another machine using the network.
.An IP address is the address of a network adapter within a specific local network.
It will be different when connected to different networks.
When not connected to any network, it will either be a link-local address or an auto-configuration address.
You might want the MAC address, which is the hardware address of a single network adapter and is not very likely to change.
The provided code returns HOSTNAME/IP-Address(xx.xx.xx.xx).
Hostname is your computer name ex: MY-PC and then you get the IP corresponding to it.
When you are connected to a network, InetAddress.getLocalHost() asks the DHCP server in the network "what is the address of MY-PC (the name of your computer)", the DHCP replies -> 33.44.55.66
Try the following CMD commands when both connected and disconnected.
\>hostname
MY-PC
\>nslookup MY-PC
44.55.66.77
When you are not connected to a network there are two possibilities:
You do not get a hostname (default is localhost)
You do get a hostname, but there is no DHCP server on the network to return an IPaddress,
so you get loopback - 127.0.0.1
If you want to "call" your computer on the network locally, use the loopback http://www.pcmag.com/encyclopedia/term/57812/loopback-address
Hope this helps
No. You're confusing IP and MAC addresses. The MAC address is a serial number of hardware(but may be programatically changed on certain chipsets).
The IP address is either software-determined or determined by the network. It can differ between networks or even with time.
IP addresses are (usually) interface specific, not machine specific.
If your machine only has one interface the difference is moot, but it matters if (for example) you have both wired and wireless ethernet.
Also note that if you do have both and attempt to use them both at the same time on the same subnet that things will likely get very confused!

Error 0:0:0:0:0:0:0:1%0 getting IP V6 client

I have this function to get the HostAddress from my request (HttpServletRequest) on Java. But using Jetty 7.x and my IP is ipV6 I have always this error with iPv6 address.
My function:
xxxx.getIP(request, false);
public static String getIP(HttpServletRequest request, boolean proxy) {
String ip = "";
log.debug("X-getHeaderNames ["+ request.getHeaderNames()+"]");
if (proxy) {
ip = XFordwardedInetAddressUtil.getAddressFromRequest(request);
} else {
String _ip = request.getRemoteAddr();
ip = InetAddresses.forString(_ip).getHostAddress();
}
return ip;
}
The error:
DEBUG: org.encuestame.core.exception.EnMeMappingExceptionResolver - Resolving exception from handler [org.encuestame.mvc.controller.TweetPollController#4fc23996]: java.lang.IllegalArgumentException: '0:0:0:0:0:0:0:1%0' is not an IP string literal.
java.lang.IllegalArgumentException: '0:0:0:0:0:0:0:1%0' is not an IP string literal.
at org.encuestame.utils.net.InetAddresses.forString(InetAddresses.java:59)
at org.encuestame.core.util.EnMeUtils.getIP(EnMeUtils.java:210)
at org.encuestame.mvc.controller.AbstractBaseOperations.getIpClient(AbstractBaseOperations.java:262)
at org.encuestame.mvc.controller.TweetPollController.detailTweetPollController(TweetPollController.java:332)
at org.encuestame.mvc.controller.TweetPollController$$FastClassByCGLIB$$6990b004.invoke()
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedIntercepto
I know the iPv6 localhost format should be '0:0:0:0:0:0:0:1' but my request always return this string '0:0:0:0:0:0:0:1%0'
Anyone can help me?
The problem is that the class you're using (org.encuestame.utils.net.InetAddresses) clearly doesn't support IPv6. Try using the java InetAddress class that Joachim mentioned in his answer.
When you're using a link local address, the % should be included in the address.
This is due to the fact that the computer needs to know which interface/zone the request came from to be able to reply out the correct interface.
If you're using correctly configured, Internet routable IPv6 addresses, the zone index will not be a part of the address.
In this case, I can't see a way to solve your problem for localhost/link local testing except to filter out anything after the % sign, or use another class that works with link local addresses to parse the address.
EDIT: Here's another - similar - question I didn't see earlier.

Server Socket Error

i have got an error in this line:
new ServerSocket(2106, 50, InetAddress.getByName("83.4.200.1"));
Error log:
Exception in thread "main" java.net.BindException: Cannot assign requested address: JVM_Bind
83.4.200.1 is my ip, when i put there 127.0.0.1 or 192.168.1.2 with same port, everything is working perfect. I have checked all ports by writing netstat -a -n, but 2106 isnt there.
Thanks a lot for reading this, i hope that u can help me with my problem
Your routers address is 83.4.200.1. It's important to note that this isn't the address that your computer responds to, but rather the internal network address 192.168.1.2. If you want to connect to your program from outside the router, you needs to set up port forwarding for 2106 on the router.
1. If you want to access this Server with IP: "83.4.200.1" through Internet, then it must
be your static ip, rather than an dynamic one.
2. Try to run this code with a private ip address or public ip address which is assigned to your pc in LAN (ie. Without internet..JUST WITH WIRELESS CONNECTION).
3. Private ip or Public ip has No meaning until and unless you are on INTERNET.. TILL THEN YOU CAN USE BOTH, AS ITS LAN.
4. Private ip ranges
Class A : 10.0.0.0 - 10.0.0.255
Class B : 172.16.0.0 - 172.31.255.255
Class C : 192.168.0.0 - 192.168.255.255
5. Public is given by your service provider, which will be anyone OUT of the private ip range. If your ip is not static, there is hardly or none of your chances to access the server over internet, there are sites that gives static ip out of your dynamic ips.
83.4.200.1 is my ip
It is the IP address of your router.
It isn't an IP address of the host you are running your code in, so you can't bind to it. You need to bind to a local address of that host, and arrange port forwarding from the router to your host. Most usually the bind-address is best omitted altogether, just specifying a port, in which case the socket will listen on all local IP addresses.

Categories