|
16 | 16 |
|
17 | 17 | import static java.util.Objects.isNull; |
18 | 18 |
|
| 19 | +import java.net.Inet4Address; |
19 | 20 | import java.net.InetAddress; |
20 | 21 | import java.net.NetworkInterface; |
21 | 22 | import java.net.SocketException; |
|
56 | 57 | import org.eclipse.kura.identity.PasswordConfiguration; |
57 | 58 | import org.eclipse.kura.identity.PasswordStrengthVerificationService; |
58 | 59 | import org.eclipse.kura.identity.Permission; |
| 60 | +import org.eclipse.kura.net.IP4Address; |
59 | 61 | import org.eclipse.kura.net.IPAddress; |
60 | 62 | import org.eclipse.kura.net.NetInterface; |
61 | 63 | import org.eclipse.kura.net.NetInterfaceAddress; |
@@ -754,36 +756,62 @@ private String extractDockerInterfaceAddress(NetInterface<?> netInterface) { |
754 | 756 | return null; |
755 | 757 | } |
756 | 758 |
|
757 | | - List<? extends NetInterfaceAddress> addresses = netInterface.getNetInterfaceAddresses(); |
758 | | - if (addresses.isEmpty()) { |
| 759 | + return getAddressNetworkService(netInterface.getNetInterfaceAddresses()); |
| 760 | + } |
| 761 | + |
| 762 | + private String getAddressNetworkService(List<? extends NetInterfaceAddress> addresses) { |
| 763 | + if (addresses == null) { |
759 | 764 | return null; |
760 | 765 | } |
761 | 766 |
|
762 | | - IPAddress ipAddress = addresses.get(0).getAddress(); |
763 | | - if (ipAddress != null) { |
764 | | - logger.debug("Found docker0 interface with address: {}", ipAddress.getHostAddress()); |
765 | | - return ipAddress.getHostAddress(); |
| 767 | + IPAddress candidate = null; |
| 768 | + |
| 769 | + for (final NetInterfaceAddress address : addresses) { |
| 770 | + candidate = address.getAddress(); |
| 771 | + |
| 772 | + if (candidate instanceof IP4Address) { |
| 773 | + break; |
| 774 | + } |
766 | 775 | } |
767 | | - return null; |
| 776 | + |
| 777 | + return candidate != null ? candidate.getHostAddress() : null; |
768 | 778 | } |
769 | 779 |
|
770 | 780 | private String getDockerBridgeViaJavaApi() { |
771 | 781 | try { |
772 | 782 | NetworkInterface dockerInterface = NetworkInterface.getByName("docker0"); |
773 | 783 | if (dockerInterface != null) { |
774 | | - Enumeration<InetAddress> addresses = dockerInterface.getInetAddresses(); |
775 | | - if (addresses.hasMoreElements()) { |
776 | | - String address = addresses.nextElement().getHostAddress(); |
777 | | - logger.debug("Found docker0 interface with address (via Java API): {}", address); |
778 | | - return address; |
779 | | - } |
| 784 | + return getAddressJavaAPI(dockerInterface); |
780 | 785 | } |
781 | 786 | } catch (Exception e) { |
782 | 787 | logger.debug("Failed to detect docker0 interface using Java NetworkInterface API", e); |
783 | 788 | } |
784 | 789 | return null; |
785 | 790 | } |
786 | 791 |
|
| 792 | + private String getAddressJavaAPI(NetworkInterface nif) throws SocketException { |
| 793 | + final Enumeration<? extends InetAddress> addresses = nif.getInetAddresses(); |
| 794 | + |
| 795 | + InetAddress candidate = null; |
| 796 | + |
| 797 | + while (addresses.hasMoreElements()) { |
| 798 | + final InetAddress addr = addresses.nextElement(); |
| 799 | + |
| 800 | + if (!isValidAddress(addr, nif)) { |
| 801 | + continue; |
| 802 | + } |
| 803 | + |
| 804 | + candidate = addr; |
| 805 | + |
| 806 | + if (candidate instanceof Inet4Address) { |
| 807 | + break; |
| 808 | + } |
| 809 | + } |
| 810 | + |
| 811 | + return candidate != null ? candidate.getHostAddress() : null; |
| 812 | + |
| 813 | + } |
| 814 | + |
787 | 815 | private String getHostPrimaryIpAddress() { |
788 | 816 | try { |
789 | 817 | Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces(); |
@@ -812,15 +840,14 @@ private String getAddressFromInterface(NetworkInterface nif) throws SocketExcept |
812 | 840 | return null; |
813 | 841 | } |
814 | 842 |
|
815 | | - Enumeration<InetAddress> nadrs = nif.getInetAddresses(); |
816 | | - while (nadrs.hasMoreElements()) { |
817 | | - InetAddress adr = nadrs.nextElement(); |
818 | | - if (isValidAddress(adr, nif)) { |
819 | | - logger.debug("Using host primary IP address: {}", adr.getHostAddress()); |
820 | | - return adr.getHostAddress(); |
821 | | - } |
| 843 | + final String result = getAddressJavaAPI(nif); |
| 844 | + |
| 845 | + if (result != null) { |
| 846 | + logger.debug("Using host primary IP address: {}", result); |
822 | 847 | } |
823 | | - return null; |
| 848 | + |
| 849 | + return result; |
| 850 | + |
824 | 851 | } |
825 | 852 |
|
826 | 853 | private boolean isValidNetworkInterface(NetworkInterface nif) throws SocketException { |
|
0 commit comments