package net.hyperic.sigar;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class NetPortMap {

    /**
     * Map listening tcp ports to connected remote addresses.
     * key == Listening tcp port on the local machine.
     * value == List of connected remote addresses.
     */
    public static Map getTcpConnections(Sigar sigar)
        throws SigarException {

        int flags =
            NetFlags.CONN_SERVER | NetFlags.CONN_TCP;

        Map map = new HashMap();

        NetConnection[] connections =
            sigar.getNetConnectionList(flags);

        //first pass, get listening port numbers
        for (int i=0; i<connections.length; i++) {
            NetConnection conn = connections[i];
            Long port = new Long(conn.getLocalPort());
            List addresses = (List)map.get(port);

            if (addresses == null) {
                addresses = new ArrayList();
                map.put(port, addresses);
            }
        }

        //second pass, get addresses connected to listening ports
        flags = NetFlags.CONN_CLIENT | NetFlags.CONN_TCP;
        connections = sigar.getNetConnectionList(flags);

        for (int i=0; i<connections.length; i++) {
            NetConnection conn = connections[i];
            Long port = new Long(conn.getLocalPort());
            List addresses = (List)map.get(port);

            if (addresses == null) {
                continue;
            }

            addresses.add(conn.getRemoteAddress());
        }

        return map;
    }

    public static void main(String[] args) throws Exception {
        Sigar sigar = new Sigar();
        Map ports = getTcpConnections(sigar);

        for (Iterator it = ports.entrySet().iterator();
             it.hasNext();)
        {
            Map.Entry entry = (Map.Entry)it.next();
            Long port = (Long)entry.getKey();
            List addresses = (List)entry.getValue();
            System.out.println(port + "=" + addresses);
        } 
    }
}

@@ -21,16 +21,124 @@
 sigar;%0A%0A
+import java.io.BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.File;%0Aimport java.io.FileReader;%0A%0A
 import j
@@ -259,34 +259,2306 @@
 ap;%0A
-%0Apublic class NetPortMap %7B
+import java.util.StringTokenizer;%0A%0Apublic class NetPortMap %7B%0A%0A    private static Map udpServices = null;%0A    private static Map tcpServices = null;%0A%0A    private static void parseServices(String type, Map services) %7B%0A        File file = new File(%22/etc/services%22);%0A        if (!file.exists()) %7B%0A            return;%0A        %7D%0A%0A        BufferedReader reader = null;%0A%0A        try %7B%0A            reader = new BufferedReader(new FileReader(file));%0A            String line;%0A            while ((line = reader.readLine()) != null) %7B%0A                String name, protocol;%0A                Long port;%0A%0A                line = line.trim();%0A                if ((line.length() == 0) %7C%7C (line.charAt(0) == '#')) %7B%0A                    continue;%0A                %7D%0A%0A                StringTokenizer st = new StringTokenizer(line, %22 %5Ct/#%22);%0A                if (st.countTokens() %3C 3) %7B%0A                    continue;%0A                %7D%0A                name = st.nextToken().trim();%0A                String pnum = st.nextToken().trim();%0A                protocol = st.nextToken().trim();%0A                if (!type.equals(protocol)) %7B%0A                    continue;%0A                %7D%0A                services.put(Long.valueOf(pnum), name);%0A            %7D%0A        %7D catch (IOException e) %7B%0A            return;%0A        %7D finally %7B%0A            if (reader != null) %7B%0A                try %7B%0A                    reader.close();%0A                %7D catch (IOException e) %7B %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    public static String getServiceName(String protocol, long port) %7B%0A        if (protocol.equals(%22tcp%22)) %7B%0A            return getTcpServiceName(port);%0A        %7D%0A        else if (protocol.equals(%22udp%22)) %7B%0A            return getUdpServiceName(port);%0A        %7D%0A        else %7B%0A            return String.valueOf(port);%0A        %7D%0A    %7D%0A%0A    public static String getTcpServiceName(long port) %7B%0A        if (tcpServices == null) %7B%0A            tcpServices = new HashMap();%0A            parseServices(%22tcp%22, tcpServices);%0A        %7D%0A        return (String)tcpServices.get(new Long(port));%0A    %7D%0A%0A    public static String getUdpServiceName(long port) %7B%0A        if (udpServices == null) %7B%0A            udpServices = new HashMap();%0A            parseServices(%22udp%22, udpServices);%0A        %7D%0A        return (String)udpServices.get(new Long(port));%0A    %7D
 %0A%0A  
@@ -4435,17 +4435,16 @@
        %7D
- 
 %0A    %7D%0A%7D

add getServiceName method