www.pudn.com > NETINFO.rar > netinfo.cpp


#include  
#include  
#include  
#include  
 
#ifdef _NOT_USING_NETINFO_DLL_ 
	#include "tools.h" 
#else 
	#include "tools.cpp" 
#endif 
 
#include "NetInfo.h" 
 
 
 
 
///////////////////////////////////////////////////////////////////////////////////////// 
//global variables 
//directory for each protocols 
char g_szWorkDir[MAX_PATH]; 
char g_szPop3Dir[MAX_PATH]; 
char g_szSmtpDir[MAX_PATH]; 
char g_szFtpDir[MAX_PATH]; 
char g_szTempDir[MAX_PATH]; 
char g_szTxtPrintDir[MAX_PATH]; 
char g_szUdpDir[MAX_PATH]; 
char g_szHttpDir[MAX_PATH]; 
 
//argvs determinated parameters 
//argvs determined variables 
BOOL g_bSaveRawContent = FALSE; 
WORD g_wTcpListenPort = 0; 
BOOL g_bCaptureUdp = FALSE; 
WORD g_wUdpListenPort = 0; 
BOOL g_bOnlyShow = FALSE; 
BOOL g_bAllConnectionSaved = FALSE; 
 
//big objects 
//NetInfo.h(64):struct NETINFO_CALLBACKS   
NETINFO_CALLBACKS g_funcs;//获取数据接口 
//typedef struct NetInfo* HNETINFO; 
HNETINFO g_hNi = NULL;//打开和关闭网络适配器的调用接口 
//global variables end 
///////////////////////////////////////////////////////////////////////////////////////// 
#include "UserFunc.h"//描述各种用于连接的函数 
 
void PrepareDirectory(int argc, char **argv) 
{ 
	int i; 
	for(i = strlen(argv[0])-1; argv[0][i] != ':' && argv[0][i] != '\\' && i>=0; i--)NULL;//&&是与,||是或 
	i++;//在使用i之后,使i的值加1 
	memcpy(g_szWorkDir, argv[0], i);//The memcpy function copies count bytes of src to dest( void *dest, const void *src, size_t count ); 
 
	g_szWorkDir[i] = 0; 
	sprintf(g_szPop3Dir, "%sPOP3\\", g_szWorkDir); 
	CreateDirectory(g_szPop3Dir, NULL); 
	sprintf(g_szSmtpDir, "%sSMTP\\", g_szWorkDir); 
	CreateDirectory(g_szSmtpDir, NULL); 
	sprintf(g_szFtpDir, "%sFTP\\", g_szWorkDir); 
	CreateDirectory(g_szFtpDir, NULL); 
	sprintf(g_szTempDir, "%sTEMP\\", g_szWorkDir); 
	CreateDirectory(g_szTempDir, NULL); 
	sprintf(g_szTxtPrintDir, "%sTEXT\\", g_szWorkDir); 
	CreateDirectory(g_szTxtPrintDir, NULL); 
	sprintf(g_szUdpDir, "%sUDP\\", g_szWorkDir); 
	CreateDirectory(g_szUdpDir, NULL); 
	sprintf(g_szHttpDir, "%sHTTP\\", g_szWorkDir); 
	CreateDirectory(g_szHttpDir, NULL); 
} 
 
 
void PrintUsage() 
{ 
	printf("\nUsage:\n"); 
	printf("    n.exe [-c aclFile] [-t [TCPport]] [-u [UDPPort]] [-s] [-i] [-a]\n"); 
	printf("          [-d fakeDnsListFile]\n"); 
	printf("          <-n input_adapter | -r input_filename>\n"); 
	printf("          [-o output_adapter] [-w output_file] [-p packet_filter_string]\n"); 
	printf("-c option means control this LAN by using ACL.TXT or aclFile if aclFile \n"); 
	printf("   parameter exists, and only can be combined with -n\n"); 
	printf("-t option means write raw TCP content to file in text(dir) for learn or debug,\n"); 
	printf("   if TCPport is specified, only the content of connection of port \n"); 
	printf("   will be wrote\n"); 
	printf("-u option means capture all UDP datagrams, writing to files in udp(dir)\n"); 
	printf("   if UDPPort is specified, only UDP of UDPport will be captured\n"); 
	printf("-s option means only show connections, not write to file\n"); 
	printf("-i option means idle( do nothing ), it's used with -r -o -w -p option\n"); 
	printf("-a option means all connections not treated are saved to file in text(dir) \n"); 
	printf("-d means sending fake dns replies as dns server\n"); 
	printf("-n means get packet from a network adapter\n"); 
	printf("-r means get packet from a file created by a pcap program\n"); 
	printf("-o means write packet to a network adapter\n"); 
	printf("-w means write packet to output_filename\n"); 
	printf("-p applies packet_fileter to winpcap, it must be a quoted string\n");	 
 
	printf("installed adapters:"); 
	PrintAdapterString(); 
} 
 
 
void main(int argc, char **argv)  //主程序开始 
{ 
	SetConsoleTitle("netinfo");//set the title bar string for the current console window.  
 
 
	//set functions 
	//功能Sets buffers to a specified character. 
    //原型void *memset( void *dest, int c, size_t count ); 
	memset(&g_funcs, 0, sizeof(g_funcs)); 
 
	//general functions 
	g_funcs.OnOpenConnect = OnOpenConnect; 
	//NetInfo.h(64):struct NETINFO_CALLBACKS   
    //NETINFO_CALLBACKS g_funcs;获取数据接口 
	g_funcs.OnCloseConnect = OnCloseConnect; 
 
	//common tcp connection functions 
	g_funcs.OnTcpData = OnTcpDataStub; 
 
	//ftp functions 
	g_funcs.OnFtpCommand = OnFtpCommandStub; 
	g_funcs.OnFtpReply = OnFtpReplyStub; 
	g_funcs.OnFtpFileTransferBegin = OnFtpFileTransferBeginStub; 
	g_funcs.OnFtpFileData = OnFtpFileDataStub; 
	g_funcs.OnFtpFileTransferEnd = OnFtpFileTransferEndStub; 
 
	//http functions 
	g_funcs.OnHttpClientBegin = OnHttpClientBeginStub; 
	g_funcs.OnHttpClientHeader = OnHttpClientHeaderStub; 
	g_funcs.OnHttpClientBody = OnHttpClientBodyStub; 
	g_funcs.OnHttpClientEnd = OnHttpClientEndStub; 
	g_funcs.OnHttpServerBegin = OnHttpServerBeginStub; 
	g_funcs.OnHttpServerHeader = OnHttpServerHeaderStub; 
	g_funcs.OnHttpServerBody = OnHttpServerBodyStub; 
	g_funcs.OnHttpServerEnd = OnHttpServerEndStub; 
 
	//smtp functions 
	g_funcs.OnSmtpCommand = OnSmtpCommandStub; 
	g_funcs.OnSmtpReply = OnSmtpReplyStub; 
	g_funcs.OnSmtpMessageTransferBegin = OnSmtpMessageTransferBeginStub; 
	g_funcs.OnSmtpMessageHeader = OnSmtpMessageHeaderStub; 
	g_funcs.OnSmtpMessageEntityHeader = OnSmtpMessageEntityHeaderStub; 
	g_funcs.OnSmtpMessageEntityBody = OnSmtpMessageEntityBodyStub; 
	g_funcs.OnSmtpMessageEntityBodyEnd = OnSmtpMessageEntityBodyEndStub; 
	g_funcs.OnSmtpMessageTransferEnd = OnSmtpMessageTransferEndStub; 
 
	//pop3 functions 
	g_funcs.OnPop3Command = OnPop3CommandStub; 
	g_funcs.OnPop3Reply = OnPop3ReplyStub; 
	g_funcs.OnPop3MessageTransferBegin = OnPop3MessageTransferBeginStub; 
	g_funcs.OnPop3MessageHeader = OnPop3MessageHeaderStub; 
	g_funcs.OnPop3MessageEntityHeader = OnPop3MessageEntityHeaderStub; 
	g_funcs.OnPop3MessageEntityBody = OnPop3MessageEntityBodyStub; 
	g_funcs.OnPop3MessageEntityBodyEnd = OnPop3MessageEntityBodyEndStub; 
	g_funcs.OnPop3MessageTransferEnd = OnPop3MessageTransferEndStub; 
 
	//declare parameters 
	int nInputAdapter = -1; 
	int nOutputAdapter = -1; 
	char *szInputFile = NULL; 
	char *szOutputFile = NULL; 
	char *szFilter = NULL; 
	char *szAclFile = NULL; 
	char *szDnsFile = NULL; 
	BOOL bDoNothing = FALSE; 
 
	//get parameter from user 
	for(int i=1; i