/*************************************************************************

nport\routin.cpp

	-by Miles Chen (stainboyx@hotmail.com) 2009-2-22

*************************************************************************/

#include "iris-int.h"

BOOL InitialListUI(HWND hList)
{
	LVCOLUMNW col;

	//Extended Styles
	ListView_SetExtendedListViewStyle(hList, LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);

	//Color
	ListView_SetTextColor(hList, RGB(64, 128, 128));

	//Column Headers
	col.mask = LVCF_TEXT | LVCF_WIDTH;

	col.pszText = L"PID";
	col.cx = 100;
	ListView_InsertColumn(hList, 0, &col);

	col.pszText = L"Port";
	col.cx = 100;
	ListView_InsertColumn(hList, 1, &col);

	col.pszText = L"Status";
	col.cx = 100;
	ListView_InsertColumn(hList, 2, &col);

	col.pszText = L"Path";
	col.cx = 235;
	ListView_InsertColumn(hList, 3, &col);


	return TRUE;
}


BOOL UpdatePortList(HWND hList)
{
	LVITEMW lvi;
	WCHAR szBuffer[MAX_CHAR];

    // Declare and initialize variables
    PMIB_TCPTABLE_OWNER_PID pTcpTable;
    DWORD dwSize = 0;
    DWORD dwRetVal = 0;

    //char szLocalAddr[128];
    //char szRemoteAddr[128];

    //struct in_addr IpAddr;

    //int i;

	// clear
	ListView_DeleteAllItems(hList);

	pTcpTable = (PMIB_TCPTABLE_OWNER_PID)Marshal::AllocCoTaskMem(sizeof (MIB_TCPTABLE_OWNER_PID));
    if (pTcpTable == NULL) {
        //printf("Error allocating memory\n");
        return 1;
    }

    dwSize = sizeof (MIB_TCPTABLE_OWNER_PID);
// Make an initial call to GetTcpTable to
// get the necessary size into the dwSize variable
    if ((dwRetVal = GetExtendedTcpTable(
                pTcpTable, 
                &dwSize, 
                TRUE,
                AF_INET,
                TCP_TABLE_OWNER_PID_LISTENER,
                NULL)) ==
        ERROR_INSUFFICIENT_BUFFER) {
			Marshal::FreeCoTaskMem(pTcpTable);
        pTcpTable = (PMIB_TCPTABLE_OWNER_PID)Marshal::AllocCoTaskMem(dwSize);
        if (pTcpTable == NULL) {
            //printf("Error allocating memory\n");
            return 1;
        }
    }
// Make a second call to GetTcpTable to get
// the actual data we require
    if ((dwRetVal = GetExtendedTcpTable(
                pTcpTable, 
                &dwSize, 
                TRUE,
                AF_INET,
                TCP_TABLE_OWNER_PID_LISTENER,
                NULL)) == NO_ERROR) {
        //printf("\tNumber of entries: %d\n", (int) pTcpTable->dwNumEntries);
        for (int i = 0; i < (int) pTcpTable->dwNumEntries; i++) {
   //         IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwLocalAddr;
			//String::CopyTo(szLocalAddr, inet_ntoa(IpAddr));
   //         IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwRemoteAddr;
   //         String::CopyTo(szRemoteAddr, inet_ntoa(IpAddr));

            //printf("\n\tTCP[%d] State: %ld - ", i,
            //       pTcpTable->table[i].dwState);
            /*switch (pTcpTable->table[i].dwState) {
            case MIB_TCP_STATE_CLOSED:
                printf("CLOSED\n");
                break;
            case MIB_TCP_STATE_LISTEN:
                printf("LISTEN\n");
                break;
            case MIB_TCP_STATE_SYN_SENT:
                printf("SYN-SENT\n");
                break;
            case MIB_TCP_STATE_SYN_RCVD:
                printf("SYN-RECEIVED\n");
                break;
            case MIB_TCP_STATE_ESTAB:
                printf("ESTABLISHED\n");
                break;
            case MIB_TCP_STATE_FIN_WAIT1:
                printf("FIN-WAIT-1\n");
                break;
            case MIB_TCP_STATE_FIN_WAIT2:
                printf("FIN-WAIT-2 \n");
                break;
            case MIB_TCP_STATE_CLOSE_WAIT:
                printf("CLOSE-WAIT\n");
                break;
            case MIB_TCP_STATE_CLOSING:
                printf("CLOSING\n");
                break;
            case MIB_TCP_STATE_LAST_ACK:
                printf("LAST-ACK\n");
                break;
            case MIB_TCP_STATE_TIME_WAIT:
                printf("TIME-WAIT\n");
                break;
            case MIB_TCP_STATE_DELETE_TCB:
                printf("DELETE-TCB\n");
                break;
            default:
                printf("UNKNOWN dwState value\n");
                break;
            }*/
            //printf("\tTCP[%d] Local Addr: %s\n", i, szLocalAddr);
            //printf("\tTCP[%d] Local Port: %d \n", i,
            //       ntohs((u_short)pTcpTable->table[i].dwLocalPort));
            //printf("\tTCP[%d] Remote Addr: %s\n", i, szRemoteAddr);
            //printf("\tTCP[%d] Remote Port: %d\n", i,
            //       ntohs((u_short)pTcpTable->table[i].dwRemotePort));
            //printf("\tTCP[%d] PID: %d\n", i, pTcpTable->table[i].dwOwningPid);


			//
			lvi.iItem = i;

			lvi.mask = LVIF_TEXT | LVIF_PARAM;
			lvi.iSubItem = 0;
			wsprintfW(szBuffer, L"%d", pTcpTable->table[i].dwOwningPid);
			lvi.pszText = szBuffer;
			ListView_InsertItem(hList, &lvi);

			lvi.mask = LVIF_TEXT;
			lvi.iSubItem = 1;
			wsprintfW(szBuffer, L"%d", ntohs((u_short)pTcpTable->table[i].dwLocalPort));
			lvi.pszText = szBuffer;
			ListView_SetItem(hList, &lvi);

			lvi.mask = LVIF_TEXT;
			lvi.iSubItem = 2;
			lvi.pszText = L"Listen";
			ListView_SetItem(hList, &lvi);

			lvi.mask = LVIF_TEXT;
			lvi.iSubItem = 3;
			if(Process::GetProcessFileName(pTcpTable->table[i].dwOwningPid, szBuffer, MAX_CHAR))
				lvi.pszText = szBuffer;
			else
				lvi.pszText = L"";
			ListView_SetItem(hList, &lvi);


        }
    } else {
        //printf("\tGetTcpTable failed with %d\n", dwRetVal);
    }
        Marshal::FreeCoTaskMem(pTcpTable);
        //return 1;

    return 0;    
}
