1
2
3
4
5 package oshi.software.os;
6
7 import java.net.InetAddress;
8 import java.net.UnknownHostException;
9 import java.util.Arrays;
10 import java.util.List;
11
12 import oshi.annotation.concurrent.Immutable;
13 import oshi.annotation.concurrent.ThreadSafe;
14
15
16
17
18 @ThreadSafe
19 public interface InternetProtocolStats {
20
21
22
23
24
25
26
27
28
29 TcpStats getTCPv4Stats();
30
31
32
33
34
35
36
37 TcpStats getTCPv6Stats();
38
39
40
41
42
43
44 UdpStats getUDPv4Stats();
45
46
47
48
49
50
51
52 UdpStats getUDPv6Stats();
53
54
55
56
57
58
59 List<IPConnection> getConnections();
60
61
62
63
64 @Immutable
65 final class TcpStats {
66 private final long connectionsEstablished;
67 private final long connectionsActive;
68 private final long connectionsPassive;
69 private final long connectionFailures;
70 private final long connectionsReset;
71 private final long segmentsSent;
72 private final long segmentsReceived;
73 private final long segmentsRetransmitted;
74 private final long inErrors;
75 private final long outResets;
76
77 public TcpStats(long connectionsEstablished, long connectionsActive, long connectionsPassive,
78 long connectionFailures, long connectionsReset, long segmentsSent, long segmentsReceived,
79 long segmentsRetransmitted, long inErrors, long outResets) {
80 this.connectionsEstablished = connectionsEstablished;
81 this.connectionsActive = connectionsActive;
82 this.connectionsPassive = connectionsPassive;
83 this.connectionFailures = connectionFailures;
84 this.connectionsReset = connectionsReset;
85 this.segmentsSent = segmentsSent;
86 this.segmentsReceived = segmentsReceived;
87 this.segmentsRetransmitted = segmentsRetransmitted;
88 this.inErrors = inErrors;
89 this.outResets = outResets;
90 }
91
92
93
94
95
96
97
98 public long getConnectionsEstablished() {
99 return connectionsEstablished;
100 }
101
102
103
104
105
106
107
108
109 public long getConnectionsActive() {
110 return connectionsActive;
111 }
112
113
114
115
116
117
118
119
120 public long getConnectionsPassive() {
121 return connectionsPassive;
122 }
123
124
125
126
127
128
129
130
131 public long getConnectionFailures() {
132 return connectionFailures;
133 }
134
135
136
137
138
139
140
141 public long getConnectionsReset() {
142 return connectionsReset;
143 }
144
145
146
147
148
149
150
151 public long getSegmentsSent() {
152 return segmentsSent;
153 }
154
155
156
157
158
159
160
161 public long getSegmentsReceived() {
162 return segmentsReceived;
163 }
164
165
166
167
168
169
170
171 public long getSegmentsRetransmitted() {
172 return segmentsRetransmitted;
173 }
174
175
176
177
178
179
180 public long getInErrors() {
181 return inErrors;
182 }
183
184
185
186
187
188
189 public long getOutResets() {
190 return outResets;
191 }
192
193 @Override
194 public String toString() {
195 return "TcpStats [connectionsEstablished=" + connectionsEstablished + ", connectionsActive="
196 + connectionsActive + ", connectionsPassive=" + connectionsPassive + ", connectionFailures="
197 + connectionFailures + ", connectionsReset=" + connectionsReset + ", segmentsSent=" + segmentsSent
198 + ", segmentsReceived=" + segmentsReceived + ", segmentsRetransmitted=" + segmentsRetransmitted
199 + ", inErrors=" + inErrors + ", outResets=" + outResets + "]";
200 }
201 }
202
203
204
205
206 @Immutable
207 final class UdpStats {
208 private final long datagramsSent;
209 private final long datagramsReceived;
210 private final long datagramsNoPort;
211 private final long datagramsReceivedErrors;
212
213 public UdpStats(long datagramsSent, long datagramsReceived, long datagramsNoPort,
214 long datagramsReceivedErrors) {
215 this.datagramsSent = datagramsSent;
216 this.datagramsReceived = datagramsReceived;
217 this.datagramsNoPort = datagramsNoPort;
218 this.datagramsReceivedErrors = datagramsReceivedErrors;
219 }
220
221
222
223
224
225
226 public long getDatagramsSent() {
227 return datagramsSent;
228 }
229
230
231
232
233
234
235 public long getDatagramsReceived() {
236 return datagramsReceived;
237 }
238
239
240
241
242
243
244
245 public long getDatagramsNoPort() {
246 return datagramsNoPort;
247 }
248
249
250
251
252
253
254
255 public long getDatagramsReceivedErrors() {
256 return datagramsReceivedErrors;
257 }
258
259 @Override
260 public String toString() {
261 return "UdpStats [datagramsSent=" + datagramsSent + ", datagramsReceived=" + datagramsReceived
262 + ", datagramsNoPort=" + datagramsNoPort + ", datagramsReceivedErrors=" + datagramsReceivedErrors
263 + "]";
264 }
265 }
266
267
268
269
270 enum TcpState {
271 UNKNOWN, CLOSED, LISTEN, SYN_SENT, SYN_RECV, ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT, CLOSING, LAST_ACK,
272 TIME_WAIT, NONE;
273 }
274
275
276
277
278 @Immutable
279 final class IPConnection {
280 private final String type;
281 private final byte[] localAddress;
282 private final int localPort;
283 private final byte[] foreignAddress;
284 private final int foreignPort;
285 private final TcpState state;
286 private final int transmitQueue;
287 private final int receiveQueue;
288 private int owningProcessId;
289
290 public IPConnection(String type, byte[] localAddress, int localPort, byte[] foreignAddress, int foreignPort,
291 TcpState state, int transmitQueue, int receiveQueue, int owningProcessId) {
292 this.type = type;
293 this.localAddress = Arrays.copyOf(localAddress, localAddress.length);
294 this.localPort = localPort;
295 this.foreignAddress = Arrays.copyOf(foreignAddress, foreignAddress.length);
296 this.foreignPort = foreignPort;
297 this.state = state;
298 this.transmitQueue = transmitQueue;
299 this.receiveQueue = receiveQueue;
300 this.owningProcessId = owningProcessId;
301 }
302
303
304
305
306
307
308 public String getType() {
309 return type;
310 }
311
312
313
314
315
316
317
318
319
320
321 public byte[] getLocalAddress() {
322 return Arrays.copyOf(localAddress, localAddress.length);
323 }
324
325
326
327
328
329
330 public int getLocalPort() {
331 return localPort;
332 }
333
334
335
336
337
338
339
340
341
342
343 public byte[] getForeignAddress() {
344 return Arrays.copyOf(foreignAddress, foreignAddress.length);
345 }
346
347
348
349
350
351
352 public int getForeignPort() {
353 return foreignPort;
354 }
355
356
357
358
359
360
361 public TcpState getState() {
362 return state;
363 }
364
365
366
367
368
369
370 public int getTransmitQueue() {
371 return transmitQueue;
372 }
373
374
375
376
377
378
379 public int getReceiveQueue() {
380 return receiveQueue;
381 }
382
383
384
385
386
387
388 public int getowningProcessId() {
389 return owningProcessId;
390 }
391
392 @Override
393 public String toString() {
394 String localIp = "*";
395 try {
396 localIp = InetAddress.getByAddress(localAddress).toString();
397 } catch (UnknownHostException e) {
398 }
399 String foreignIp = "*";
400 try {
401 foreignIp = InetAddress.getByAddress(foreignAddress).toString();
402 } catch (UnknownHostException e) {
403 }
404 return "IPConnection [type=" + type + ", localAddress=" + localIp + ", localPort=" + localPort
405 + ", foreignAddress=" + foreignIp + ", foreignPort=" + foreignPort + ", state=" + state
406 + ", transmitQueue=" + transmitQueue + ", receiveQueue=" + receiveQueue + ", owningProcessId="
407 + owningProcessId + "]";
408 }
409 }
410 }