View Javadoc
1   /*
2    * Copyright 2020-2025 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.EnumMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import oshi.annotation.concurrent.ThreadSafe;
26  import oshi.driver.linux.proc.ProcessStat;
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      private final String tcpColon = "Tcp:";
40      private final String udpColon = "Udp:";
41      private final String udp6 = "Udp6";
42  
43      private enum TcpStat {
44          RtoAlgorithm, RtoMin, RtoMax, MaxConn, ActiveOpens, PassiveOpens, AttemptFails, EstabResets, CurrEstab, InSegs,
45          OutSegs, RetransSegs, InErrs, OutRsts, InCsumErrors;
46      }
47  
48      private enum UdpStat {
49          OutDatagrams, InDatagrams, NoPorts, InErrors, RcvbufErrors, SndbufErrors, InCsumErrors, IgnoredMulti, MemErrors;
50      }
51  
52      @Override
53      public TcpStats getTCPv4Stats() {
54          byte[] fileBytes = FileUtil.readAllBytes(ProcPath.SNMP, true);
55          List<String> lines = ParseUtil.parseByteArrayToStrings(fileBytes);
56          Map<TcpStat, Long> tcpData = new EnumMap<>(TcpStat.class);
57  
58          for (int line = 0; line < lines.size() - 1; line += 2) {
59              if (lines.get(line).startsWith(tcpColon) && lines.get(line + 1).startsWith(tcpColon)) {
60                  Map<TcpStat, String> parsedData = ParseUtil.stringToEnumMap(TcpStat.class,
61                          lines.get(line + 1).substring(tcpColon.length()).trim(), ' ');
62                  for (Map.Entry<TcpStat, String> entry : parsedData.entrySet()) {
63                      tcpData.put(entry.getKey(), ParseUtil.parseLongOrDefault(entry.getValue(), 0L));
64                  }
65                  break;
66              }
67          }
68  
69          return new TcpStats(tcpData.getOrDefault(TcpStat.CurrEstab, 0L), tcpData.getOrDefault(TcpStat.ActiveOpens, 0L),
70                  tcpData.getOrDefault(TcpStat.PassiveOpens, 0L), tcpData.getOrDefault(TcpStat.AttemptFails, 0L),
71                  tcpData.getOrDefault(TcpStat.EstabResets, 0L), tcpData.getOrDefault(TcpStat.OutSegs, 0L),
72                  tcpData.getOrDefault(TcpStat.InSegs, 0L), tcpData.getOrDefault(TcpStat.RetransSegs, 0L),
73                  tcpData.getOrDefault(TcpStat.InErrs, 0L), tcpData.getOrDefault(TcpStat.OutRsts, 0L));
74      }
75  
76      @Override
77      public UdpStats getUDPv4Stats() {
78          byte[] fileBytes = FileUtil.readAllBytes(ProcPath.SNMP, true);
79          List<String> lines = ParseUtil.parseByteArrayToStrings(fileBytes);
80          Map<UdpStat, Long> udpData = new EnumMap<>(UdpStat.class);
81  
82          for (int line = 0; line < lines.size() - 1; line += 2) {
83              if (lines.get(line).startsWith(udpColon) && lines.get(line + 1).startsWith(udpColon)) {
84                  Map<UdpStat, String> parsedData = ParseUtil.stringToEnumMap(UdpStat.class,
85                          lines.get(line + 1).substring(udpColon.length()).trim(), ' ');
86                  for (Map.Entry<UdpStat, String> entry : parsedData.entrySet()) {
87                      udpData.put(entry.getKey(), ParseUtil.parseLongOrDefault(entry.getValue(), 0L));
88                  }
89                  break;
90              }
91          }
92  
93          return new UdpStats(udpData.getOrDefault(UdpStat.OutDatagrams, 0L),
94                  udpData.getOrDefault(UdpStat.InDatagrams, 0L), udpData.getOrDefault(UdpStat.NoPorts, 0L),
95                  udpData.getOrDefault(UdpStat.InErrors, 0L));
96      }
97  
98      @Override
99      public UdpStats getUDPv6Stats() {
100         byte[] fileBytes = FileUtil.readAllBytes(ProcPath.SNMP6, true);
101         List<String> lines = ParseUtil.parseByteArrayToStrings(fileBytes);
102         long inDatagrams = 0;
103         long noPorts = 0;
104         long inErrors = 0;
105         long outDatagrams = 0;
106         int foundUDPv6StatsCount = 0;
107 
108         // Traverse bottom-to-top for efficiency as the /etc/proc/snmp6 file follows sequential format -> ip6, icmp6,
109         // udp6, udplite6 stats
110         for (int line = lines.size() - 1; line >= 0 && foundUDPv6StatsCount < 4; line--) {
111             if (lines.get(line).startsWith(udp6)) {
112                 String[] parts = lines.get(line).split("\\s+");
113                 switch (parts[0]) {
114                 case "Udp6InDatagrams":
115                     inDatagrams = ParseUtil.parseLongOrDefault(parts[1], 0L);
116                     foundUDPv6StatsCount++;
117                     break;
118                 case "Udp6NoPorts":
119                     noPorts = ParseUtil.parseLongOrDefault(parts[1], 0L);
120                     foundUDPv6StatsCount++;
121                     break;
122                 case "Udp6InErrors":
123                     inErrors = ParseUtil.parseLongOrDefault(parts[1], 0L);
124                     foundUDPv6StatsCount++;
125                     break;
126                 case "Udp6OutDatagrams":
127                     outDatagrams = ParseUtil.parseLongOrDefault(parts[1], 0L);
128                     foundUDPv6StatsCount++;
129                     break;
130                 default:
131                     break;
132                 }
133             }
134         }
135 
136         return new UdpStats(inDatagrams, noPorts, inErrors, outDatagrams);
137     }
138 
139     @Override
140     public List<IPConnection> getConnections() {
141         List<IPConnection> conns = new ArrayList<>();
142         Map<Long, Integer> pidMap = ProcessStat.querySocketToPidMap();
143         conns.addAll(queryConnections("tcp", 4, pidMap));
144         conns.addAll(queryConnections("tcp", 6, pidMap));
145         conns.addAll(queryConnections("udp", 4, pidMap));
146         conns.addAll(queryConnections("udp", 6, pidMap));
147         return conns;
148     }
149 
150     private static List<IPConnection> queryConnections(String protocol, int ipver, Map<Long, Integer> pidMap) {
151         List<IPConnection> conns = new ArrayList<>();
152         for (String s : FileUtil.readFile(ProcPath.NET + "/" + protocol + (ipver == 6 ? "6" : ""))) {
153             if (s.indexOf(':') >= 0) {
154                 String[] split = ParseUtil.whitespaces.split(s.trim());
155                 if (split.length > 9) {
156                     Pair<byte[], Integer> lAddr = parseIpAddr(split[1]);
157                     Pair<byte[], Integer> fAddr = parseIpAddr(split[2]);
158                     TcpState state = stateLookup(ParseUtil.hexStringToInt(split[3], 0));
159                     Pair<Integer, Integer> txQrxQ = parseHexColonHex(split[4]);
160                     long inode = ParseUtil.parseLongOrDefault(split[9], 0);
161                     conns.add(new IPConnection(protocol + ipver, lAddr.getA(), lAddr.getB(), fAddr.getA(), fAddr.getB(),
162                             state, txQrxQ.getA(), txQrxQ.getB(), pidMap.getOrDefault(inode, -1)));
163                 }
164             }
165         }
166         return conns;
167     }
168 
169     private static Pair<byte[], Integer> parseIpAddr(String s) {
170         int colon = s.indexOf(':');
171         if (colon > 0 && colon < s.length()) {
172             byte[] first = ParseUtil.hexStringToByteArray(s.substring(0, colon));
173             // Bytes are in __be32 endianness. we must invert each set of 4 bytes
174             for (int i = 0; i + 3 < first.length; i += 4) {
175                 byte tmp = first[i];
176                 first[i] = first[i + 3];
177                 first[i + 3] = tmp;
178                 tmp = first[i + 1];
179                 first[i + 1] = first[i + 2];
180                 first[i + 2] = tmp;
181             }
182             int second = ParseUtil.hexStringToInt(s.substring(colon + 1), 0);
183             return new Pair<>(first, second);
184         }
185         return new Pair<>(new byte[0], 0);
186     }
187 
188     private static Pair<Integer, Integer> parseHexColonHex(String s) {
189         int colon = s.indexOf(':');
190         if (colon > 0 && colon < s.length()) {
191             int first = ParseUtil.hexStringToInt(s.substring(0, colon), 0);
192             int second = ParseUtil.hexStringToInt(s.substring(colon + 1), 0);
193             return new Pair<>(first, second);
194         }
195         return new Pair<>(0, 0);
196     }
197 
198     private static TcpState stateLookup(int state) {
199         switch (state) {
200         case 0x01:
201             return ESTABLISHED;
202         case 0x02:
203             return SYN_SENT;
204         case 0x03:
205             return SYN_RECV;
206         case 0x04:
207             return FIN_WAIT_1;
208         case 0x05:
209             return FIN_WAIT_2;
210         case 0x06:
211             return TIME_WAIT;
212         case 0x07:
213             return CLOSED;
214         case 0x08:
215             return CLOSE_WAIT;
216         case 0x09:
217             return LAST_ACK;
218         case 0x0A:
219             return LISTEN;
220         case 0x0B:
221             return CLOSING;
222         case 0x00:
223         default:
224             return UNKNOWN;
225         }
226     }
227 }