⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basic.cpp

📁 linux 上http email 协议分析程序 主要能够处理大数据量的主干网的应用
💻 CPP
字号:
#include <stdio.h>#include <string.h>#include <ctype.h>#include <assert.h>#include <iostream>#include <fstream>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/types.h>#include <unistd.h>#include <netinet/in.h>#include <netinet/ip.h>#include <netinet/tcp.h>#include <netinet/udp.h>#include <BaseConst.h>#include <Utility.h>#include "Consts.h"#include "Basic.h" bool Cval::isSpecails(char c){	//if(strchr("()<>[]@.,;:=",c))	if(strchr("()<>[]@,;:=",c))		return true;	if(c==0x22)		return true;	if(c=='\\')		return true;	return false;}bool Cval::isDelimiter(char c){	if(isspace(c))		return true;	if(Cval::isSpecails(c))		return true;	return false;	}bool Cval::getNextAtom(char *&pval,char *sz_buf){	if(*pval==0)			return false;	while(isspace(*pval))	{		if(*pval==0)			return false;		pval++;	}		if(isDelimiter(*pval))	{				if(*pval=='(')		{				int iloop=1;			char ctag='(';			char etag=')';			*sz_buf++=*pval++;			while(iloop>0 && *pval)				{				if(*sz_buf==ctag)					iloop++;					if(*sz_buf==etag)					iloop--;				*sz_buf++=*pval++;			}			*sz_buf=0;			if(iloop==0)				return true;			return false;		}		if(*pval==0x22 )		{			*sz_buf++=*pval++;			while(*pval && *pval!=0x22)			{				*sz_buf++=*pval++;				}			if(*pval)			{				*sz_buf++=*pval++;				*sz_buf=0;				return true;			}			return false;		}		*sz_buf++=*pval++;	}	else	{		while(*pval)		{			if(!isDelimiter(*pval))				*sz_buf++=*pval++;			else		 		break;		}	}	//printf("atom=[%s]\n",s);	*sz_buf=0;	return true;}bool Cval::isEndBoudary(char *szline){	int len=strlen(szline);	if(szline[len-1]=='-' && szline[len-2]=='-')		return true;	return false;}int Cval::getEncodingType(char *pval){	//if()			if(!strncasecmp(szencode_7bit,pval,strlen(szencode_7bit)))		return encode_7bit;	if(!strncasecmp(szencode_8bit,pval,strlen(szencode_8bit)))		return encode_8bit;		if(!strncasecmp(szencode_binary,pval,strlen(szencode_binary)))		return encode_binary;	if(!strncasecmp(szencode_qp,pval,strlen(szencode_qp)))		return encode_qp;	if(!strncasecmp(szencode_base64,pval,strlen(szencode_base64)))		return encode_base64;	return encode_unknown;}int Cval::getContentType(char *pval){	if(!strncasecmp(sztype_text,pval,strlen(sztype_text)))		return type_text;	if(!strncasecmp(sztype_image,pval,strlen(sztype_image)))		return type_image;	if(!strncasecmp(sztype_audio,pval,strlen(sztype_audio)))		return type_audio;	if(!strncasecmp(sztype_application,pval,strlen(sztype_application)))		return type_application;	if(!strncasecmp(sztype_video,pval,strlen(sztype_video)))		return type_video;	if(!strncasecmp(sztype_message,pval,strlen(sztype_message)))		return type_message;	if(!strncasecmp(sztype_multipart,pval,strlen(sztype_multipart)))		return type_multipart;	return type_err;}bool Cval::getContentTypeValue(char *pval,char *pout){	return true;}bool Cval::getFileName(char *pval,char *pout){	return true;}void reverseHead(struct ST_TCP_STREAM *phead){	unsigned long ip;    unsigned short port;	//phead->protocol;		ip=phead->src_ip;	phead->src_ip=phead->dst_ip;	phead->dst_ip=ip;	port=phead->src_port;	phead->src_port=phead->dst_port;	phead->dst_port=port;}bool isspmon(struct ST_TCP_STREAM *phead){	if(phead->isInWatch!=0)		return true;	else		return false;}void logHead(struct ST_TCP_STREAM *phead){	struct in_addr inaddr;	char bufsp[128];	char bufdp[128];	//if(phead->isInWatch==0)	//	return;	inaddr.s_addr = phead->src_ip;	sprintf(bufsp,"%s ",inet_ntoa(inaddr));		inaddr.s_addr = phead->dst_ip;	sprintf(bufdp,"%s ",inet_ntoa(inaddr));		int isp=ntohs(phead->src_port);	int idp=ntohs(phead->dst_port);            TLog::toLog("%s::%d-%d %s:%d-%s:%d %d",TUTime::YYYYMMDDHHMMSS(),phead->protocol,phead->isInWatch,bufsp,isp,bufdp,idp,phead->length);               }int tfrMonth(char *szmonth){	if(strcmp(szmonth,"Jan")==0)		return 1;	if(strcmp(szmonth,"Feb")==0)		return 2;	if(strcmp(szmonth,"Mar")==0)		return 3;	if(strcmp(szmonth,"Apr")==0)		return 4;	if(strcmp(szmonth,"May")==0)		return 5;	if(strcmp(szmonth,"Jun")==0)		return 6;	if(strcmp(szmonth,"Jul")==0)		return 7;	if(strcmp(szmonth,"Aug")==0)		return 8;	if(strcmp(szmonth,"Sep")==0)		return 9;	if(strcmp(szmonth,"Oct")==0)		return 10;	if(strcmp(szmonth,"Nov")==0)		return 11;	if(strcmp(szmonth,"Dec")==0)		return 12;	return 0;}void traceIpFile(char *buff, int lenbuf,char *dir){	struct iphdr *piph; 			// IP卤篓头	struct tcphdr *ptcph;		// TCP卤篓头	piph=(struct iphdr *)(buff ) ;	if(piph->version!=4)//ipv4		return;	if(piph->protocol!=6)//TCP协议		return;	ptcph=(struct tcphdr*)(buff + 4*piph->ihl);	/*	int iplen=ntohs(piph->tot_len);	int tcphlen=m_ptcph->doff*4;	int iphlen=4*m_piph->ihl;	int datalen=iplen-tcphlen-iphlen;	*/		struct in_addr inaddr;	char filename[128];			inaddr.s_addr = piph->saddr;	sprintf(filename,"%s/%s::%d--",dir,inet_ntoa(inaddr),ntohs(ptcph->source));	//printf("filename=%s\n",filename);	inaddr.s_addr = piph->daddr;	sprintf(filename+strlen(filename),"%s::%d--",inet_ntoa(inaddr),ntohs(ptcph->dest));	//printf("filename=%s\n",filename);	strcat(filename,".");	strcat(filename,TUTime::YYYYMMDDHHMMSS());	//printf("filename=%s\n",filename);	//printf("save stream [%s]\n",filename);	//strcpy(filename,"log/tmp.s");	#ifdef DEBUG		printf("trace [%s]\n",filename);		TLog::toLog("file=[%s]\n",filename);	#endif	ofstream *pofslog= new ofstream(filename,ios::trunc);	for(int i=0;i<lenbuf;i++)			*pofslog<<buff[i];	//printf("flush\n");	pofslog->flush();	//printf("close\n");	pofslog->close();		delete pofslog;}void traceFile(char *buff, int lenbuf,char *dir){	struct in_addr inaddr;	char filename[128];	struct ST_TCP_STREAM theHead;	memcpy(&theHead,buff,sizeof(theHead));		inaddr.s_addr = theHead.src_ip;	sprintf(filename,"%s/%s::%d--",dir,inet_ntoa(inaddr),ntohs(theHead.src_port));	//printf("filename=%s\n",filename);	inaddr.s_addr = theHead.dst_ip;	sprintf(filename+strlen(filename),"%s::%d--",inet_ntoa(inaddr),ntohs(theHead.dst_port));	//printf("filename=%s\n",filename);	strcat(filename,".");	strcat(filename,TUTime::YYYYMMDDHHMMSS());	//printf("filename=%s\n",filename);	//printf("save stream [%s]\n",filename);	//strcpy(filename,"log/tmp.s");	#ifdef DEBUG		printf("trace [%s]\n",filename);		TLog::toLog("file=[%s]\n",filename);	#endif	ofstream *pofslog= new ofstream(filename,ios::trunc);	for(int i=0;i<lenbuf;i++)			*pofslog<<buff[i];	//printf("flush\n");	pofslog->flush();	//printf("close\n");	pofslog->close();		delete pofslog;}void saveFile(char *buff, int lenbuf,char *dir){		char filename[128];	char buf[128];	int pid=getpid();	sprintf(buf,"%d",pid);	strcpy(filename,dir);	strcat(filename,"/");	strcat(filename,buf);		ofstream *pofslog= new ofstream(filename,ios::trunc);	for(int i=0;i<lenbuf;i++)			*pofslog<<buff[i];	//printf("flush\n");	pofslog->flush();	//printf("close\n");	pofslog->close();	delete pofslog;}void saveIpFile(char *buff, int lenbuf,char *dir){	struct in_addr inaddr;	char filename[128];	struct ST_TCP_STREAM theHead;	strcpy(filename,dir);	strcat(filename,"/");	strcat(filename,"tmp.s");		ofstream *pofslog= new ofstream(filename,ios::trunc);	for(int i=0;i<lenbuf;i++)			*pofslog<<buff[i];	//printf("flush\n");	pofslog->flush();	//printf("close\n");	pofslog->close();	delete pofslog;}//#ifdef FROMFILEint readFile(char *outbuf){		long int save_pos,size_of_file;    char cfilename[100];	printf("input filename:\n");	scanf("%s",cfilename);	//getchar();	FILE *fp=fopen(cfilename,"r");	if(fp==NULL){			printf("%s is not exist\n",cfilename);							//sleep(10);			return 0;	}	/* Save the current position. */	save_pos = ftell( fp );	/* Jump to the end of the file. */	fseek( fp, 0L, SEEK_END );	/* Get the end position. */	size_of_file = ftell( fp );		fseek( fp, save_pos, SEEK_SET );	fread(outbuf,1,size_of_file,fp);	fclose(fp);	return size_of_file;	}//#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -