C:/work/RevealedSystems/EyeSpy-RS/eyespy/src/swig/extendedTCPTableLib/ExtendedXportTable.cxx File Reference

#include "Winsock2.h"
#include "Iphlpapi.h"
#include <stdio.h>
#include <tchar.h>
#include <psapi.h>

Include dependency graph for ExtendedXportTable.cxx:

Go to the source code of this file.

Classes

struct  V4Table

Defines

#define MALLOC(x)   HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x)   HeapFree(GetProcessHeap(), 0, (x))

Functions

void clearERROR_MESSAGE ()
bool tableArrayOutOfRange (unsigned int idx)
bool indexOutOfRange (unsigned int idx)
void populateTCPV4Table ()
void populateUDPV4Table ()
BOOL GetProcessName (DWORD processID, TCHAR *szProcessName)
void updateV4Tables ()
unsigned int getV4TableSize ()
unsigned int getV4Pid (unsigned int idx) throw (const char *)
char * getV4ProcessName (unsigned int idx) throw (const char *)
unsigned int getV4SrcPort (unsigned int idx) throw (const char *)
unsigned int getV4State (unsigned int idx) throw (const char *)
char * getV4Transport (unsigned int idx) throw (const char *)

Variables

char ERROR_MESSAGE [4096]
const unsigned int maxSize = 10000
unsigned int tableSize
V4Table tableArray [maxSize]


Define Documentation

#define FREE (  )     HeapFree(GetProcessHeap(), 0, (x))

Definition at line 26 of file ExtendedXportTable.cxx.

Referenced by populateTCPV4Table(), and populateUDPV4Table().

#define MALLOC (  )     HeapAlloc(GetProcessHeap(), 0, (x))

Definition at line 25 of file ExtendedXportTable.cxx.

Referenced by populateTCPV4Table(), and populateUDPV4Table().


Function Documentation

void clearERROR_MESSAGE (  ) 

Definition at line 287 of file ExtendedXportTable.cxx.

References ERROR_MESSAGE.

Referenced by getV4Pid(), getV4ProcessName(), getV4SrcPort(), getV4State(), getV4Transport(), populateTCPV4Table(), and populateUDPV4Table().

00288 {
00289         //Nulls string may cause problems back in Pythonland
00290         strcpy(ERROR_MESSAGE, "NOERROR");
00291 }

Here is the caller graph for this function:

BOOL GetProcessName ( DWORD  processID,
TCHAR *  szProcessName 
)

Definition at line 318 of file ExtendedXportTable.cxx.

Referenced by populateTCPV4Table(), and populateUDPV4Table().

00319 {
00320  
00321     BOOL bRC = FALSE;
00322  
00323     // Get a handle to the process.
00324  
00325     HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
00326                                    PROCESS_VM_READ,
00327                                    FALSE, processID );
00328  
00329     // Get the process name.
00330  
00331     if (NULL != hProcess )
00332     {
00333         HMODULE hMod;
00334         DWORD cbNeeded;
00335  
00336         if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
00337              &cbNeeded) )
00338         {
00339             GetModuleBaseName( hProcess, hMod, szProcessName,
00340                                MAX_PATH );
00341  
00342             bRC = TRUE;
00343         }
00344                 else
00345                 {
00346                         strcpy(szProcessName, "UNKNOWN");
00347                 }
00348     }
00349         else
00350         {
00351                 strcpy(szProcessName, "UNKNOWN");
00352         }
00353  
00354     CloseHandle( hProcess );
00355  
00356     return bRC;
00357 }

Here is the caller graph for this function:

unsigned int getV4Pid ( unsigned int  idx  )  throw (const char *)

Definition at line 66 of file ExtendedXportTable.cxx.

References clearERROR_MESSAGE(), ERROR_MESSAGE, indexOutOfRange(), V4Table.Pid, and tableArrayOutOfRange().

00067 {
00068         clearERROR_MESSAGE();
00069 
00070         if (tableArrayOutOfRange(idx) || indexOutOfRange(idx)) throw ERROR_MESSAGE;
00071 
00072         clearERROR_MESSAGE();
00073 
00074         return tableArray[idx].Pid;
00075                 
00076 }

Here is the call graph for this function:

char* getV4ProcessName ( unsigned int  idx  )  throw (const char *)

Definition at line 78 of file ExtendedXportTable.cxx.

References clearERROR_MESSAGE(), ERROR_MESSAGE, indexOutOfRange(), V4Table.processName, and tableArrayOutOfRange().

00079 {
00080         clearERROR_MESSAGE();
00081 
00082         if (tableArrayOutOfRange(idx) || indexOutOfRange(idx)) throw ERROR_MESSAGE;
00083 
00084         clearERROR_MESSAGE();
00085 
00086         return tableArray[idx].processName;
00087                 
00088 }

Here is the call graph for this function:

unsigned int getV4SrcPort ( unsigned int  idx  )  throw (const char *)

Definition at line 91 of file ExtendedXportTable.cxx.

References clearERROR_MESSAGE(), ERROR_MESSAGE, indexOutOfRange(), V4Table.srcPort, and tableArrayOutOfRange().

00092 {
00093         clearERROR_MESSAGE();
00094 
00095         if (tableArrayOutOfRange(idx) || indexOutOfRange(idx)) throw ERROR_MESSAGE;
00096 
00097         clearERROR_MESSAGE();
00098 
00099         return tableArray[idx].srcPort; 
00100 }

Here is the call graph for this function:

unsigned int getV4State ( unsigned int  idx  )  throw (const char *)

Definition at line 103 of file ExtendedXportTable.cxx.

References clearERROR_MESSAGE(), ERROR_MESSAGE, indexOutOfRange(), V4Table.state, and tableArrayOutOfRange().

00104 {
00105         clearERROR_MESSAGE();
00106 
00107         if (tableArrayOutOfRange(idx) || indexOutOfRange(idx)) throw ERROR_MESSAGE;
00108 
00109         clearERROR_MESSAGE();
00110 
00111         return tableArray[idx].state;   
00112 }

Here is the call graph for this function:

unsigned int getV4TableSize (  ) 

Definition at line 63 of file ExtendedXportTable.cxx.

References tableSize.

00063 {return tableSize;}

char* getV4Transport ( unsigned int  idx  )  throw (const char *)

Definition at line 114 of file ExtendedXportTable.cxx.

References clearERROR_MESSAGE(), ERROR_MESSAGE, indexOutOfRange(), tableArrayOutOfRange(), and V4Table.transport.

00115 {
00116         clearERROR_MESSAGE();
00117 
00118         if (tableArrayOutOfRange(idx) || indexOutOfRange(idx)) throw ERROR_MESSAGE;
00119 
00120         clearERROR_MESSAGE();
00121 
00122         return tableArray[idx].transport;       
00123 }

Here is the call graph for this function:

bool indexOutOfRange ( unsigned int  idx  ) 

Definition at line 304 of file ExtendedXportTable.cxx.

References ERROR_MESSAGE, and tableSize.

Referenced by getV4Pid(), getV4ProcessName(), getV4SrcPort(), getV4State(), and getV4Transport().

00305 {
00306         if (idx >= tableSize)
00307         {
00308                 sprintf(ERROR_MESSAGE, "Index out of Range: Index:%d, Current Table Size:%d", idx, tableSize);
00309                 return true;
00310         }
00311 
00312         return false;
00313 }

Here is the caller graph for this function:

void populateTCPV4Table (  ) 

Definition at line 142 of file ExtendedXportTable.cxx.

References clearERROR_MESSAGE(), ERROR_MESSAGE, FREE, GetProcessName(), MALLOC, V4Table.Pid, V4Table.srcPort, V4Table.state, and tableSize.

Referenced by updateV4Tables().

00143 {       
00144         clearERROR_MESSAGE();   
00145 
00146         MIB_TCPTABLE_OWNER_PID * pTcpTable;
00147 
00148         DWORD dwSize = 0;
00149         DWORD dwRetVal = 0;     
00150 
00151         pTcpTable = ( MIB_TCPTABLE_OWNER_PID *) MALLOC(sizeof ( MIB_TCPTABLE_OWNER_PID));
00152 
00153         if (pTcpTable == NULL) 
00154         {
00155                 strcpy(ERROR_MESSAGE, "Could not allocate memory for TCP Table");
00156                 throw ERROR_MESSAGE;
00157         }
00158 
00159         dwSize = sizeof (MIB_TCPTABLE_OWNER_PID);
00160 
00161         if ((dwRetVal = GetExtendedTcpTable (pTcpTable, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL , 0)) == ERROR_INSUFFICIENT_BUFFER) 
00162         {
00163                 FREE(pTcpTable);
00164                 pTcpTable = (MIB_TCPTABLE_OWNER_PID *) MALLOC(dwSize);
00165     
00166                 if (pTcpTable == NULL) 
00167                 {            
00168                         strcpy(ERROR_MESSAGE, "Could not allocate memory for TCP Table");
00169                         throw ERROR_MESSAGE;
00170 
00171                         return;
00172                 }
00173         }
00174 
00175         // Make a second call to GetTcpTable to get
00176         // the actual data we require
00177 
00178         if ((dwRetVal = GetExtendedTcpTable (pTcpTable, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL , 0)) == NO_ERROR)
00179         {
00180                 //
00181         }
00182         else
00183         {
00184                 strcpy(ERROR_MESSAGE, "Could not allocate memory for TCP Table");
00185                 FREE(pTcpTable);
00186 
00187                 throw ERROR_MESSAGE;
00188         }
00189 
00190         char processName[1024];
00191 
00192 
00193         for (int i = 0; i < pTcpTable->dwNumEntries; i++)
00194         {
00195                 GetProcessName(pTcpTable->table[i].dwOwningPid, processName);                           
00196 
00197                 //getTCPProcName(&pTcpTable->table[i], processName, processPath);
00198 
00199 
00200                 tableArray[tableSize].Pid = pTcpTable->table[i].dwOwningPid;
00201                 strcpy(tableArray[i].processName, processName);
00202                 tableArray[tableSize].srcPort = ntohs(pTcpTable->table[i].dwLocalPort);
00203                 strcpy(tableArray[tableSize].transport, "TCP");
00204                 tableArray[tableSize].state = pTcpTable->table[i].dwState;
00205 
00206                 tableSize ++;
00207         }
00208 
00209         FREE(pTcpTable);
00210 }

Here is the call graph for this function:

Here is the caller graph for this function:

void populateUDPV4Table (  ) 

Definition at line 216 of file ExtendedXportTable.cxx.

References clearERROR_MESSAGE(), ERROR_MESSAGE, FREE, GetProcessName(), MALLOC, V4Table.Pid, V4Table.srcPort, V4Table.state, and tableSize.

Referenced by updateV4Tables().

00217 {       
00218         clearERROR_MESSAGE();
00219 
00220         MIB_UDPTABLE_OWNER_PID* pUDPTable;
00221 
00222         DWORD dwSize = 0;
00223         DWORD dwRetVal = 0;     
00224 
00225         pUDPTable = ( MIB_UDPTABLE_OWNER_PID  *) MALLOC(sizeof ( MIB_UDPTABLE_OWNER_PID ));
00226 
00227         if (pUDPTable == NULL) 
00228         {               
00229                 strcpy(ERROR_MESSAGE, "Error allocating UDP table memory");
00230         throw ERROR_MESSAGE;
00231         }
00232 
00233         dwSize = sizeof (MIB_UDPTABLE_OWNER_PID);
00234 
00235         if ((dwRetVal = GetExtendedUdpTable (pUDPTable, &dwSize, TRUE, AF_INET,  UDP_TABLE_OWNER_PID, 0)) == ERROR_INSUFFICIENT_BUFFER) 
00236         {
00237                 FREE(pUDPTable);
00238                 pUDPTable = (MIB_UDPTABLE_OWNER_PID *) MALLOC(dwSize);
00239     
00240                 if (pUDPTable == NULL) 
00241                 {            
00242                         strcpy(ERROR_MESSAGE, "Error allocating UDP table memory");
00243                         throw ERROR_MESSAGE;
00244                 }
00245         }
00246 
00247 
00248         // Make a second call to GetTcpTable to get
00249         // the actual data we require
00250 
00251         if ((dwRetVal = GetExtendedUdpTable (pUDPTable, &dwSize, TRUE, AF_INET, UDP_TABLE_OWNER_PID, 0)) == NO_ERROR)
00252         {
00253                 //
00254         }
00255         else
00256         {
00257                 strcpy(ERROR_MESSAGE, "Error allocating UDP table memory");
00258                 FREE(pUDPTable);
00259 
00260         throw ERROR_MESSAGE;
00261         }
00262 
00263         char procName[1024];
00264 
00265         for (int i = 0; i < pUDPTable->dwNumEntries; i++)
00266         {
00267                 GetProcessName(pUDPTable->table[i].dwOwningPid, procName);              
00268                 strcpy(tableArray[tableSize].processName, procName);
00269 
00270                 tableArray[tableSize].Pid = pUDPTable->table[i].dwOwningPid;            
00271                 tableArray[tableSize].srcPort =  ntohs(pUDPTable->table[i].dwLocalPort);
00272                 tableArray[tableSize].state = -1;
00273                 strcpy(tableArray[tableSize].transport,  "UDP");
00274 
00275                 tableSize++;
00276         }
00277 
00278         FREE(pUDPTable);
00279 
00280 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool tableArrayOutOfRange ( unsigned int  idx  ) 

Definition at line 293 of file ExtendedXportTable.cxx.

References ERROR_MESSAGE, and maxSize.

Referenced by getV4Pid(), getV4ProcessName(), getV4SrcPort(), getV4State(), and getV4Transport().

00294 {
00295         if (idx >= maxSize)
00296         {
00297                 sprintf(ERROR_MESSAGE, "Index exceeded size of table: Index:%d, Max Table Size:%d", idx, maxSize);
00298                 return true;
00299         }
00300 
00301         return false;
00302 }

Here is the caller graph for this function:

void updateV4Tables (  ) 

Definition at line 130 of file ExtendedXportTable.cxx.

References populateTCPV4Table(), populateUDPV4Table(), and tableSize.

00131 {
00132         tableSize = 0;
00133 
00134         // initialize the table
00135         memset(tableArray, 0, sizeof(tableArray) );
00136 
00137         populateTCPV4Table();
00138         populateUDPV4Table();   
00139 }

Here is the call graph for this function:


Variable Documentation

char ERROR_MESSAGE[4096]

const unsigned int maxSize = 10000

Definition at line 40 of file ExtendedXportTable.cxx.

Referenced by tableArrayOutOfRange().

Definition at line 55 of file ExtendedXportTable.cxx.

unsigned int tableSize


Generated on Mon Mar 30 00:26:10 2009 for EyeSpy by  doxygen 1.5.8