aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-process/src/main/java/org/sonar/process/NetworkUtils.java
blob: 056c89784077b9913aa596bd724afbd6aa33a897 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.process;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.function.Predicate;

public interface NetworkUtils {

  int getNextLoopbackAvailablePort();

  OptionalInt getNextAvailablePort(String hostOrAddress);

  /**
   * Identifying the localhost machine
   * It will try to retrieve the hostname
   *
   * @return "hostname"
   */
  String getHostname();

  /**
   * Converts a text representation of an IP address or host name to
   * a {@link InetAddress}.
   * If text value references an IPv4 or IPv6 address, then DNS is
   * not used.
   */
  Optional<InetAddress> toInetAddress(String hostOrAddress);

  boolean isLocal(String hostOrAddress);

  boolean isLoopback(String hostOrAddress);

  boolean isIpv6Address(String hostOrAddress);

  /**
   * Returns the machine {@link InetAddress} that matches the specified
   * predicate. If multiple addresses match then a single one
   * is picked in a non deterministic way.
   */
  Optional<InetAddress> getLocalInetAddress(Predicate<InetAddress> predicate);

  /**
   * Returns a local {@link InetAddress} that is IPv4 and not
   * loopback. If multiple addresses match then a single one
   * is picked in a non deterministic way.
   */
  default Optional<InetAddress> getLocalNonLoopbackIpv4Address() {
    return getLocalInetAddress(a -> !a.isLoopbackAddress() && a instanceof Inet4Address);
  }

  /**
   * Returns a local {@link InetAddress} that is IPv6 and not
   * loopback. If multiple addresses match then a single one
   * is picked in a non deterministic way.
   */
  default Optional<InetAddress> getLocalNonLoopbackIpv6Address() {
    return getLocalInetAddress(a -> !a.isLoopbackAddress() && a instanceof Inet6Address);
  }
}