View Javadoc
1   /*
2    * Copyright 2017-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.software.os.mac;
6   
7   import static com.sun.jna.platform.unix.LibCAPI.HOST_NAME_MAX;
8   
9   import java.net.InetAddress;
10  import java.net.UnknownHostException;
11  import java.util.List;
12  
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  
16  import com.sun.jna.Native;
17  
18  import oshi.annotation.concurrent.ThreadSafe;
19  import oshi.jna.ByRef.CloseablePointerByReference;
20  import oshi.jna.platform.mac.SystemB;
21  import oshi.jna.platform.unix.CLibrary;
22  import oshi.jna.platform.unix.CLibrary.Addrinfo;
23  import oshi.software.common.AbstractNetworkParams;
24  import oshi.util.ExecutingCommand;
25  import oshi.util.ParseUtil;
26  
27  /**
28   * MacNetworkParams class.
29   */
30  @ThreadSafe
31  final class MacNetworkParams extends AbstractNetworkParams {
32  
33      private static final Logger LOG = LoggerFactory.getLogger(MacNetworkParams.class);
34  
35      private static final SystemB SYS = SystemB.INSTANCE;
36  
37      private static final String IPV6_ROUTE_HEADER = "Internet6:";
38  
39      private static final String DEFAULT_GATEWAY = "default";
40  
41      @Override
42      public String getDomainName() {
43          String hostname = "";
44          try {
45              hostname = InetAddress.getLocalHost().getHostName();
46          } catch (UnknownHostException e) {
47              LOG.error("Unknown host exception when getting address of local host: {}", e.getMessage());
48              return "";
49          }
50          try (Addrinfo hint = new Addrinfo(); CloseablePointerByReference ptr = new CloseablePointerByReference()) {
51              hint.ai_flags = CLibrary.AI_CANONNAME;
52              int res = SYS.getaddrinfo(hostname, null, hint, ptr);
53              if (res > 0) {
54                  if (LOG.isErrorEnabled()) {
55                      LOG.error("Failed getaddrinfo(): {}", SYS.gai_strerror(res));
56                  }
57                  return "";
58              }
59              Addrinfo info = new Addrinfo(ptr.getValue()); // NOSONAR
60              String canonname = info.ai_canonname.trim();
61              SYS.freeaddrinfo(ptr.getValue());
62              return canonname;
63          }
64      }
65  
66      @Override
67      public String getHostName() {
68          byte[] hostnameBuffer = new byte[HOST_NAME_MAX + 1];
69          if (0 != SYS.gethostname(hostnameBuffer, hostnameBuffer.length)) {
70              return super.getHostName();
71          }
72          return Native.toString(hostnameBuffer);
73      }
74  
75      @Override
76      public String getIpv4DefaultGateway() {
77          return searchGateway(ExecutingCommand.runNative("route -n get default"));
78      }
79  
80      @Override
81      public String getIpv6DefaultGateway() {
82          List<String> lines = ExecutingCommand.runNative("netstat -nr");
83          boolean v6Table = false;
84          for (String line : lines) {
85              if (v6Table && line.startsWith(DEFAULT_GATEWAY)) {
86                  String[] fields = ParseUtil.whitespaces.split(line);
87                  if (fields.length > 2 && fields[2].contains("G")) {
88                      return fields[1].split("%")[0];
89                  }
90              } else if (line.startsWith(IPV6_ROUTE_HEADER)) {
91                  v6Table = true;
92              }
93          }
94          return "";
95      }
96  }