You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NetworkUtilsImpl.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.process;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import com.google.common.net.InetAddresses;
  23. import java.io.IOException;
  24. import java.net.InetAddress;
  25. import java.net.NetworkInterface;
  26. import java.net.ServerSocket;
  27. import java.net.SocketException;
  28. import java.net.UnknownHostException;
  29. import java.util.Collections;
  30. import java.util.HashSet;
  31. import java.util.Optional;
  32. import java.util.OptionalInt;
  33. import java.util.Set;
  34. import java.util.function.Predicate;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37. import static java.lang.String.format;
  38. public class NetworkUtilsImpl implements NetworkUtils {
  39. private static final Set<Integer> PORTS_ALREADY_ALLOCATED = new HashSet<>();
  40. private static final int PORT_MAX_TRIES = 50;
  41. private static final Logger LOG = LoggerFactory.getLogger(NetworkUtilsImpl.class);
  42. public static final NetworkUtils INSTANCE = new NetworkUtilsImpl();
  43. NetworkUtilsImpl() {
  44. // prevent instantiation
  45. }
  46. @Override
  47. public int getNextLoopbackAvailablePort() {
  48. return getNextAvailablePort(InetAddress.getLoopbackAddress(), PortAllocator.INSTANCE);
  49. }
  50. @Override
  51. public OptionalInt getNextAvailablePort(String hostOrAddress) {
  52. return OptionalInt.of(toInetAddress(hostOrAddress)
  53. .map(t -> getNextAvailablePort(t, PortAllocator.INSTANCE))
  54. .orElseThrow(() -> new IllegalArgumentException(format("Can not resolve address %s", hostOrAddress))));
  55. }
  56. /**
  57. * Warning - the allocated ports are kept in memory and are never clean-up. Besides the memory consumption,
  58. * that means that ports already allocated are never freed. As a consequence
  59. * no more than ~64512 calls to this method are allowed.
  60. */
  61. @VisibleForTesting
  62. static int getNextAvailablePort(InetAddress address, PortAllocator portAllocator) {
  63. for (int i = 0; i < PORT_MAX_TRIES; i++) {
  64. int port = portAllocator.getAvailable(address);
  65. if (isValidPort(port)) {
  66. PORTS_ALREADY_ALLOCATED.add(port);
  67. return port;
  68. }
  69. }
  70. throw new IllegalStateException("Fail to find an available port on " + address);
  71. }
  72. private static boolean isValidPort(int port) {
  73. return port > 1023 && !PORTS_ALREADY_ALLOCATED.contains(port);
  74. }
  75. static class PortAllocator {
  76. private static final PortAllocator INSTANCE = new PortAllocator();
  77. int getAvailable(InetAddress address) {
  78. try (ServerSocket socket = new ServerSocket(0, 50, address)) {
  79. return socket.getLocalPort();
  80. } catch (IOException e) {
  81. throw new IllegalStateException("Fail to find an available port on " + address, e);
  82. }
  83. }
  84. }
  85. @Override
  86. public String getHostname() {
  87. try {
  88. return InetAddress.getLocalHost().getHostName();
  89. } catch (UnknownHostException e) {
  90. LOG.trace("Failed to get hostname", e);
  91. return "unresolved hostname";
  92. }
  93. }
  94. @Override
  95. public Optional<InetAddress> toInetAddress(String hostOrAddress) {
  96. try {
  97. if (InetAddresses.isInetAddress(hostOrAddress)) {
  98. return Optional.of(InetAddresses.forString(hostOrAddress));
  99. }
  100. return Optional.of(InetAddress.getByName(hostOrAddress));
  101. } catch (UnknownHostException e) {
  102. LOG.trace("toInetAddress({}) failed", hostOrAddress, e);
  103. return Optional.empty();
  104. }
  105. }
  106. @Override
  107. public boolean isLocal(String hostOrAddress) {
  108. try {
  109. Optional<InetAddress> inetAddress = toInetAddress(hostOrAddress);
  110. if (inetAddress.isPresent()) {
  111. var addr = inetAddress.get();
  112. if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) {
  113. return true;
  114. }
  115. return NetworkInterface.getByInetAddress(addr) != null;
  116. }
  117. return false;
  118. } catch (SocketException e) {
  119. LOG.trace("isLocalInetAddress({}) failed", hostOrAddress, e);
  120. return false;
  121. }
  122. }
  123. @Override
  124. public boolean isLoopback(String hostOrAddress) {
  125. return toInetAddress(hostOrAddress)
  126. .filter(InetAddress::isLoopbackAddress)
  127. .isPresent();
  128. }
  129. @Override
  130. public Optional<InetAddress> getLocalInetAddress(Predicate<InetAddress> predicate) {
  131. try {
  132. return Collections.list(NetworkInterface.getNetworkInterfaces()).stream()
  133. .flatMap(ni -> Collections.list(ni.getInetAddresses()).stream())
  134. .filter(a -> a.getHostAddress() != null)
  135. .filter(predicate)
  136. .findFirst();
  137. } catch (SocketException e) {
  138. LOG.trace("getLocalInetAddress(Predicate<InetAddress>) failed", e);
  139. throw new IllegalStateException("Can not retrieve network interfaces", e);
  140. }
  141. }
  142. }