add outbound connections, change existing stuff to be called inbound

This commit is contained in:
Doug MacEachern 2005-05-07 23:14:34 +00:00
parent b20fe9f6b6
commit 7851cc3299
1 changed files with 36 additions and 11 deletions

View File

@ -13,7 +13,8 @@ import java.util.StringTokenizer;
public class NetPortMap { public class NetPortMap {
private Sigar sigar; private Sigar sigar;
private Map clients; private Map inbound;
private Map outbound;
private int[] states; private int[] states;
private static final String SERVICE_FILE; private static final String SERVICE_FILE;
@ -130,7 +131,8 @@ public class NetPortMap {
} }
public void stat(int flags) throws SigarException { public void stat(int flags) throws SigarException {
this.clients = new HashMap(); this.inbound = new HashMap();
this.outbound = new HashMap();
this.states = new int[NetFlags.TCP_UNKNOWN]; this.states = new int[NetFlags.TCP_UNKNOWN];
for (int i=0; i<this.states.length; i++) { for (int i=0; i<this.states.length; i++) {
this.states[i] = 0; this.states[i] = 0;
@ -148,11 +150,11 @@ public class NetPortMap {
//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.clients.get(port); Map addresses = (Map)this.inbound.get(port);
if (addresses == null) { if (addresses == null) {
addresses = new HashMap(); addresses = new HashMap();
this.clients.put(port, addresses); this.inbound.put(port, addresses);
} }
} }
} }
@ -167,13 +169,20 @@ public class NetPortMap {
Long port = new Long(conn.getLocalPort()); Long port = new Long(conn.getLocalPort());
Map addresses = (Map)this.clients.get(port); Map addresses = (Map)this.inbound.get(port);
String ip = conn.getRemoteAddress();
if (addresses == null) { if (addresses == null) {
continue; ip = conn.getLocalAddress();
String key =
conn.getRemoteAddress() + ":" + conn.getRemotePort();
addresses = (Map)this.outbound.get(key);
if (addresses == null) {
addresses = new HashMap();
this.outbound.put(key, addresses);
}
} }
String ip = conn.getRemoteAddress();
IpEntry entry = (IpEntry)addresses.get(ip); IpEntry entry = (IpEntry)addresses.get(ip);
if (entry == null) { if (entry == null) {
entry = new IpEntry(); entry = new IpEntry();
@ -188,8 +197,12 @@ 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 getTcpClientConnections() { public Map getInboundConnections() {
return this.clients; return this.inbound;
}
public Map getOutboundConnections() {
return this.outbound;
} }
public int[] getStates() { public int[] getStates() {
@ -200,8 +213,8 @@ public class NetPortMap {
NetPortMap map = new NetPortMap(); NetPortMap map = new NetPortMap();
map.stat(); map.stat();
System.out.println("Client Connections..."); System.out.println("Inbound Connections...");
Map ports = map.getTcpClientConnections(); Map ports = map.getInboundConnections();
for (Iterator it = ports.entrySet().iterator(); for (Iterator it = ports.entrySet().iterator();
it.hasNext();) it.hasNext();)
@ -212,6 +225,18 @@ public class NetPortMap {
System.out.println(port + "=" + addresses); System.out.println(port + "=" + addresses);
} }
System.out.println("\nOutbound Connections...");
Map outbound = map.getOutboundConnections();
for (Iterator it = outbound.entrySet().iterator();
it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
String server = (String)entry.getKey();
Map addresses = (Map)entry.getValue();
System.out.println(server + "=" + addresses);
}
System.out.println("\nStates..."); System.out.println("\nStates...");
int[] states = map.getStates(); int[] states = map.getStates();
for (int i=1; i<states.length; i++) { for (int i=1; i<states.length; i++) {