Java: How to get the IP address of a local interface that can reach the remote IP address?

I have a Java application that registers a server component with a service provider and then sends the service name to the client. The client uses the service name to get the address from the service provider on the server. However, the server has several interfaces, of which 1 client gets access, so the service must be registered with the correct IP address. We found the client through broadcast, so I have a client IP address and an enumeration of the network interfaces of computers. How can I map IP to an interface without knowing what the IP client netmask is?

I spontaneously imagine turning all addresses into ints and dragging local ips with their netmask and looking for the โ€œbest matchโ€, but I wonder if there is a better way?

(this is a solution for the enterprise (tm), so shutting down the service provider is not an option, at least not with a political campaign in the first place;))

+4
source share
4 answers

In my opinion, a service provider has interfaces in several different networks. Each client is located in only one of these networks and, thus, can exchange data with only one of the interfaces. The service needs to find out which interface is available to the client so that it can send the correct address. You (the service developer) do not have control over the network environment in which the service is deployed, and therefore cannot use network-level solutions such as DNS.

In many network settings, routing is symmetrical. That is, the route from host A to host B is the same as from host B to host A. If you know that this applies to all environments in which your service will be deployed, you can safely assume that the address used to connect to the client is available to the client. This address could be detected, for example, by connecting a DatagramSocket to a client, and then calling its getLocalAddress() method.

If the routing is asymmetric, I donโ€™t know how to determine if a particular interface is accessible to the client using only the java.net API. If you control the clientโ€™s implementation, you can include the broadcast source package (which should be the server interface visible to him). In addition, you did not provide enough information about the scenario to give specific recommendations. For example, it would be useful to know which protocol is used for broadcast advertising and client response to it.

+2
source

It depends on how you do it.

Code example?

For InetAddress, there is a ".getHostAddress ()" method, which may be useful.

0
source

This is usually done using DNS. The interface must be specified in DNS, so you can contact it by name, not at servicehost.example.com, not 192.0.2.42.

0
source

This is more of a network administration problem than an application.

Note that the server (hosting the Java application) is multi-user. it can be three addresses: 1. 99.88.77.66 (public IP) 2. 10.10.10.10 (private, assigned within the country IP)

Which IP address a potential client can access depends on the network configuration.

The client can access the public IP address, but it will gain access to your IP address due to NAT. Another example would be your client IP address 192.168.10.10 through NAT, it gains access to the IP address 10.10.10.10.

You cannot calculate the correct IP address (for publishing your service) using any type of prefix mapping.

You mentioned that this is a corporate solution, and complex network configuration is common in such an environment.

In DNS, the DNS server can be myapp.company.com. It depends on the DNS object to resolve the correct IP address for the client. You may need to coordinate with a network guy with proper DNS support, the solution should be trivial and more reliable.

0
source

Source: https://habr.com/ru/post/1312164/


All Articles