include tcp in the names, might want to add udp or whatever later

This commit is contained in:
Doug MacEachern 2005-05-10 14:50:20 +00:00
parent d5c3c18371
commit dc55b40c5e
1 changed files with 49 additions and 49 deletions

View File

@ -7,10 +7,10 @@ import java.util.Map;
public class NetPortMap { public class NetPortMap {
private Sigar sigar; private Sigar sigar;
private Map inbound; private Map tcpInbound;
private Map outbound; private Map tcpOutbound;
private int[] states; private int[] tcpStates;
private int inboundTotal, outboundTotal; private int tcpInboundTotal, tcpOutboundTotal;
public NetPortMap() { public NetPortMap() {
this.sigar = new Sigar(); this.sigar = new Sigar();
@ -36,12 +36,12 @@ public class NetPortMap {
} }
public void stat(int flags) throws SigarException { public void stat(int flags) throws SigarException {
this.inbound = new HashMap(); this.tcpInbound = new HashMap();
this.outbound = new HashMap(); this.tcpOutbound = new HashMap();
this.inboundTotal = this.outboundTotal = 0; this.tcpInboundTotal = this.tcpOutboundTotal = 0;
this.states = new int[NetFlags.TCP_UNKNOWN]; this.tcpStates = new int[NetFlags.TCP_UNKNOWN];
for (int i=0; i<this.states.length; i++) { for (int i=0; i<this.tcpStates.length; i++) {
this.states[i] = 0; this.tcpStates[i] = 0;
} }
NetConnection[] connections = NetConnection[] connections =
@ -51,16 +51,16 @@ public class NetPortMap {
NetConnection conn = connections[i]; NetConnection conn = connections[i];
int state = conn.getState(); int state = conn.getState();
this.states[state]++; this.tcpStates[state]++;
//first pass, get listening port numbers //first pass, get listening port numbers
if (state == NetFlags.TCP_LISTEN) { if (state == NetFlags.TCP_LISTEN) {
Long port = new Long(conn.getLocalPort()); Long port = new Long(conn.getLocalPort());
Map addresses = (Map)this.inbound.get(port); Map addresses = (Map)this.tcpInbound.get(port);
if (addresses == null) { if (addresses == null) {
addresses = new HashMap(); addresses = new HashMap();
this.inbound.put(port, addresses); this.tcpInbound.put(port, addresses);
} }
} }
} }
@ -75,21 +75,21 @@ public class NetPortMap {
Long port = new Long(conn.getLocalPort()); Long port = new Long(conn.getLocalPort());
Map addresses = (Map)this.inbound.get(port); Map addresses = (Map)this.tcpInbound.get(port);
String ip = conn.getRemoteAddress(); String ip = conn.getRemoteAddress();
if (addresses == null) { if (addresses == null) {
this.outboundTotal++; this.tcpOutboundTotal++;
ip = conn.getRemoteAddress(); ip = conn.getRemoteAddress();
port = new Long(conn.getRemotePort()); port = new Long(conn.getRemotePort());
addresses = (Map)this.outbound.get(port); addresses = (Map)this.tcpOutbound.get(port);
if (addresses == null) { if (addresses == null) {
addresses = new HashMap(); addresses = new HashMap();
this.outbound.put(port, addresses); this.tcpOutbound.put(port, addresses);
} }
} }
else { else {
this.inboundTotal++; this.tcpInboundTotal++;
} }
IpEntry entry = (IpEntry)addresses.get(ip); IpEntry entry = (IpEntry)addresses.get(ip);
@ -106,77 +106,77 @@ public class NetPortMap {
* key == Listening tcp port on the local machine. * key == Listening tcp port on the local machine.
* value == List of connected remote addresses. * value == List of connected remote addresses.
*/ */
public Map getInboundConnections() { public Map getTcpInboundConnections() {
return this.inbound; return this.tcpInbound;
} }
public Map getOutboundConnections() { public Map getTcpOutboundConnections() {
return this.outbound; return this.tcpOutbound;
} }
public int getInboundConnectionsTotal() { public int getTcpInboundConnectionsTotal() {
return this.inboundTotal; return this.tcpInboundTotal;
} }
public int getOutboundConnectionsTotal() { public int getTcpOutboundConnectionsTotal() {
return this.outboundTotal; return this.tcpOutboundTotal;
} }
public int[] getStates() { public int[] getTcpStates() {
return this.states; return this.tcpStates;
} }
//state counters //state counters
public int getTcpEstablished() { public int getTcpEstablished() {
return this.states[NetFlags.TCP_ESTABLISHED]; return this.tcpStates[NetFlags.TCP_ESTABLISHED];
} }
public int getTcpSynSent() { public int getTcpSynSent() {
return this.states[NetFlags.TCP_SYN_SENT]; return this.tcpStates[NetFlags.TCP_SYN_SENT];
} }
public int getTcpSynRecv() { public int getTcpSynRecv() {
return this.states[NetFlags.TCP_SYN_RECV]; return this.tcpStates[NetFlags.TCP_SYN_RECV];
} }
public int getTcpFinWait1() { public int getTcpFinWait1() {
return this.states[NetFlags.TCP_FIN_WAIT1]; return this.tcpStates[NetFlags.TCP_FIN_WAIT1];
} }
public int getTcpFinWait2() { public int getTcpFinWait2() {
return this.states[NetFlags.TCP_FIN_WAIT2]; return this.tcpStates[NetFlags.TCP_FIN_WAIT2];
} }
public int getTcpTimeWait() { public int getTcpTimeWait() {
return this.states[NetFlags.TCP_TIME_WAIT]; return this.tcpStates[NetFlags.TCP_TIME_WAIT];
} }
public int getTcpClose() { public int getTcpClose() {
return this.states[NetFlags.TCP_CLOSE]; return this.tcpStates[NetFlags.TCP_CLOSE];
} }
public int getTcpCloseWait() { public int getTcpCloseWait() {
return this.states[NetFlags.TCP_CLOSE_WAIT]; return this.tcpStates[NetFlags.TCP_CLOSE_WAIT];
} }
public int getTcpLastAck() { public int getTcpLastAck() {
return this.states[NetFlags.TCP_LAST_ACK]; return this.tcpStates[NetFlags.TCP_LAST_ACK];
} }
public int getTcpListen() { public int getTcpListen() {
return this.states[NetFlags.TCP_LISTEN]; return this.tcpStates[NetFlags.TCP_LISTEN];
} }
public int getTcpClosing() { public int getTcpClosing() {
return this.states[NetFlags.TCP_CLOSING]; return this.tcpStates[NetFlags.TCP_CLOSING];
} }
public int getTcpIdle() { public int getTcpIdle() {
return this.states[NetFlags.TCP_IDLE]; return this.tcpStates[NetFlags.TCP_IDLE];
} }
public int getTcpBound() { public int getTcpBound() {
return this.states[NetFlags.TCP_BOUND]; return this.tcpStates[NetFlags.TCP_BOUND];
} }
private static void dumpConnections(Map map) { private static void dumpConnections(Map map) {
@ -194,16 +194,16 @@ public class NetPortMap {
NetPortMap map = new NetPortMap(); NetPortMap map = new NetPortMap();
map.stat(); map.stat();
System.out.println(map.inboundTotal + System.out.println(map.tcpInboundTotal +
" Inbound Connections..."); " Inbound TCP Connections...");
dumpConnections(map.getInboundConnections()); dumpConnections(map.getTcpInboundConnections());
System.out.println("\n" + map.outboundTotal + System.out.println("\n" + map.tcpOutboundTotal +
" Outbound Connections..."); " Outbound TCP Connections...");
dumpConnections(map.getOutboundConnections()); dumpConnections(map.getTcpOutboundConnections());
System.out.println("\nStates..."); System.out.println("\nTCP States...");
int[] states = map.getStates(); int[] states = map.getTcpStates();
for (int i=1; i<states.length; i++) { for (int i=1; i<states.length; i++) {
System.out.println(NetConnection.getStateString(i) + "=" + System.out.println(NetConnection.getStateString(i) + "=" +
states[i]); states[i]);