View Javadoc
1   /*
2    * Copyright 2020-2024 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.software.os.linux;
6   
7   import static oshi.software.os.InternetProtocolStats.TcpState.CLOSED;
8   import static oshi.software.os.InternetProtocolStats.TcpState.CLOSE_WAIT;
9   import static oshi.software.os.InternetProtocolStats.TcpState.CLOSING;
10  import static oshi.software.os.InternetProtocolStats.TcpState.ESTABLISHED;
11  import static oshi.software.os.InternetProtocolStats.TcpState.FIN_WAIT_1;
12  import static oshi.software.os.InternetProtocolStats.TcpState.FIN_WAIT_2;
13  import static oshi.software.os.InternetProtocolStats.TcpState.LAST_ACK;
14  import static oshi.software.os.InternetProtocolStats.TcpState.LISTEN;
15  import static oshi.software.os.InternetProtocolStats.TcpState.SYN_RECV;
16  import static oshi.software.os.InternetProtocolStats.TcpState.SYN_SENT;
17  import static oshi.software.os.InternetProtocolStats.TcpState.TIME_WAIT;
18  import static oshi.software.os.InternetProtocolStats.TcpState.UNKNOWN;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import oshi.annotation.concurrent.ThreadSafe;
25  import oshi.driver.linux.proc.ProcessStat;
26  import oshi.driver.unix.NetStat;
27  import oshi.software.common.AbstractInternetProtocolStats;
28  import oshi.util.FileUtil;
29  import oshi.util.ParseUtil;
30  import oshi.util.platform.linux.ProcPath;
31  import oshi.util.tuples.Pair;
32  
33  /**
34   * Internet Protocol Stats implementation
35   */
36  @ThreadSafe
37  public class LinuxInternetProtocolStats extends AbstractInternetProtocolStats {
38  
39      @Override
40      public TcpStats getTCPv4Stats() {
41          return NetStat.queryTcpStats("netstat -st4");
42      }
43  
44      @Override
45      public UdpStats getUDPv4Stats() {
46          return NetStat.queryUdpStats("netstat -su4");
47      }
48  
49      @Override
50      public UdpStats getUDPv6Stats() {
51          return NetStat.queryUdpStats("netstat -su6");
52      }
53  
54      @Override
55      public List<IPConnection> getConnections() {
56          List<IPConnection> conns = new ArrayList<>();
57          Map<Long, Integer> pidMap = ProcessStat.querySocketToPidMap();
58          conns.addAll(queryConnections("tcp", 4, pidMap));
59          conns.addAll(queryConnections("tcp", 6, pidMap));
60          conns.addAll(queryConnections("udp", 4, pidMap));
61          conns.addAll(queryConnections("udp", 6, pidMap));
62          return conns;
63      }
64  
65      private static List<IPConnection> queryConnections(String protocol, int ipver, Map<Long, Integer> pidMap) {
66          List<IPConnection> conns = new ArrayList<>();
67          for (String s : FileUtil.readFile(ProcPath.NET + "/" + protocol + (ipver == 6 ? "6" : ""))) {
68              if (s.indexOf(':') >= 0) {
69                  String[] split = ParseUtil.whitespaces.split(s.trim());
70                  if (split.length > 9) {
71                      Pair<byte[], Integer> lAddr = parseIpAddr(split[1]);
72                      Pair<byte[], Integer> fAddr = parseIpAddr(split[2]);
73                      TcpState state = stateLookup(ParseUtil.hexStringToInt(split[3], 0));
74                      Pair<Integer, Integer> txQrxQ = parseHexColonHex(split[4]);
75                      long inode = ParseUtil.parseLongOrDefault(split[9], 0);
76                      conns.add(new IPConnection(protocol + ipver, lAddr.getA(), lAddr.getB(), fAddr.getA(), fAddr.getB(),
77                              state, txQrxQ.getA(), txQrxQ.getB(), pidMap.getOrDefault(inode, -1)));
78                  }
79              }
80          }
81          return conns;
82      }
83  
84      private static Pair<byte[], Integer> parseIpAddr(String s) {
85          int colon = s.indexOf(':');
86          if (colon > 0 && colon < s.length()) {
87              byte[] first = ParseUtil.hexStringToByteArray(s.substring(0, colon));
88              // Bytes are in __be32 endianness. we must invert each set of 4 bytes
89              for (int i = 0; i + 3 < first.length; i += 4) {
90                  byte tmp = first[i];
91                  first[i] = first[i + 3];
92                  first[i + 3] = tmp;
93                  tmp = first[i + 1];
94                  first[i + 1] = first[i + 2];
95                  first[i + 2] = tmp;
96              }
97              int second = ParseUtil.hexStringToInt(s.substring(colon + 1), 0);
98              return new Pair<>(first, second);
99          }
100         return new Pair<>(new byte[0], 0);
101     }
102 
103     private static Pair<Integer, Integer> parseHexColonHex(String s) {
104         int colon = s.indexOf(':');
105         if (colon > 0 && colon < s.length()) {
106             int first = ParseUtil.hexStringToInt(s.substring(0, colon), 0);
107             int second = ParseUtil.hexStringToInt(s.substring(colon + 1), 0);
108             return new Pair<>(first, second);
109         }
110         return new Pair<>(0, 0);
111     }
112 
113     private static TcpState stateLookup(int state) {
114         switch (state) {
115         case 0x01:
116             return ESTABLISHED;
117         case 0x02:
118             return SYN_SENT;
119         case 0x03:
120             return SYN_RECV;
121         case 0x04:
122             return FIN_WAIT_1;
123         case 0x05:
124             return FIN_WAIT_2;
125         case 0x06:
126             return TIME_WAIT;
127         case 0x07:
128             return CLOSED;
129         case 0x08:
130             return CLOSE_WAIT;
131         case 0x09:
132             return LAST_ACK;
133         case 0x0A:
134             return LISTEN;
135         case 0x0B:
136             return CLOSING;
137         case 0x00:
138         default:
139             return UNKNOWN;
140         }
141     }
142 }