Reputation: 843
Our current system is receiving connections via sockets, and we need to determine the private IP of a connected client. Currently, we can easily access the public IP via connectingSocket.getInetAddress()
, but what's the best way to get the private address?
Thanks!
Upvotes: 2
Views: 872
Reputation: 24421
It would be illogical and insecure to expect to be able to get a private IP from a public request. NAT and proxies hide the private IP for several reasons, one being security. Additionally, you have no idea how many levels of NAT'ing or proxies or VLANs, etc that you incoming connections may be crossing before it finally gets to your service.
If you control the client (ie: it is your own java client, etc), you can encode the information within your request using several different techniques (could be in a header, part of the payload, etc), but if it is a generic client that is connecting then you have no control over that information.
Maybe the best question to ask is why you would want/need that information? If it is for logging or tracking of individuals, then keep in mind that private IPs can change at any moment (most people use DHCP) and the majority of people do not have any kind of DHCP logging/history enabled. Consequently to track back which piece of hardware initiated the connection through the public ip->private ip would be an impossible task.
Upvotes: 3
Reputation: 181290
The simple answer is straightforward: you can't.
The more complex answer: If you can control the way your clients connect to your java sockets (i.e. force them to use HTTP over a PROXY) you might have the proxy add that information to a special header. Or, if you use a proprietary protocol, you can encode the private IP on the protocol itself. But, there is no general way of obtaining that information. NAT simply makes the private IP address go away.
Upvotes: 8