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

📄 spalarm.c

📁 Linux平台环境下
💻 C
📖 第 1 页 / 共 3 页
字号:
	memset(maskC,'0',sizeof(maskC));
	memset(maskD,'0',sizeof(maskD));
	memset(netmask,'0',sizeof(netmask));
	strncpy(netmask,mask,16);
	netmask[15] = '\0';

	for(i=0;i<16;i++)
	{
		if(netmask[i]=='.')
		{
			if(j>2)
				return returnvalue;
			suffix[j] = i;
			j++;
		}
	}
	if (j!=3)
		return returnvalue;

	if(((suffix[2]-suffix[1])<2) || ((suffix[2]-suffix[1])>4))
		return returnvalue;
	if(((suffix[1]-suffix[0])<2) || ((suffix[1]-suffix[0])>4))
		return returnvalue;
	
	for(i=0;i<suffix[0];i++)
	{
		maskA[i] = netmask[i];
	}
	for(i=suffix[0]+1,j=0;i<suffix[1];i++,j++)
	{
		maskB[j] = netmask[i];
	}
	for(i=suffix[1]+1,j=0;i<suffix[2];i++,j++)
	{
		maskC[j] = netmask[i];
	}
	for(i=suffix[2]+1,j=0;i<strlen(netmask);i++,j++)
	{
		maskD[j] = netmask[i];
	}

	mamk_a = ChangeChar2Int(maskA[0])*100+ChangeChar2Int(maskA[1])*10+ChangeChar2Int(maskA[2])*1;
	mask_b = ChangeChar2Int(maskB[0])*100+ChangeChar2Int(maskB[1])*10+ChangeChar2Int(maskB[2])*1;
	mask_c = ChangeChar2Int(maskC[0])*100+ChangeChar2Int(maskC[1])*10+ChangeChar2Int(maskC[2])*1;
	mask_d = ChangeChar2Int(maskD[0])*100+ChangeChar2Int(maskD[1])*10+ChangeChar2Int(maskD[2])*1;
	if(mask_d>mask_c || mask_d>mask_b || mask_d>mamk_a || mask_c>mask_b || mask_c>mamk_a || mask_b>mamk_a)
		return returnvalue;

	if(mask_d==0)//255.255.255.0
	{
		if(mask_c==0)//255.x.0.0
		{
			if(mask_b==0)//x.0.0.0
			{
				if(GetMask(mamk_a)<0)
					returnvalue = -1;
				else
					returnvalue = 8+GetMask(mask_b);
			}
			else//255.x.0.0
			{
				if(mamk_a==255)
				{
					if(GetMask(mask_b)<0)
						returnvalue = -1;
					else
						returnvalue = 8+GetMask(mask_b);
				}
				else
					returnvalue = -1;
			}
		}
		else//255.255.x.0
		{
			if(mask_b==255 && mamk_a==255)
			{
				if(GetMask(mask_c)<0)
					returnvalue = -1;
				else
					returnvalue = 16+GetMask(mask_c);
			}
			else
				returnvalue = -1;
		}
	}
	else//255.255.255.x
	{
		if(mask_c==255 && mask_b==255 && mamk_a==255)
		{
			if(GetMask(mask_d)<0)
				returnvalue = -1;
			else
				returnvalue = 24+GetMask(mask_d);
		}
		else
			returnvalue = -1;
	}
	if(mask_d==0 && mask_c==0 && mask_b==0 && mamk_a==0)
		returnvalue = 0;

	return returnvalue;
}


int GetMask(int x)
{
	switch(x)
	{
	case 128:
		return 1;
	case 192:
		return 2;
	case 224:
		return 3;
	case 240:
		return 4;
	case 248:
		return 5;
	case 252:
		return 6;
	case 254:
		return 7;
	case 255:
		return 8;
	default :
		return -1;
		
	}
}

int ChangeChar2Int(char x)
{
	switch(x)
	{
	case '0':
		return 0;
	case '1':
		return 1;
	case '2':
		return 2;
	case '3':
		return 3;
	case '4':
		return 4;
	case '5':
		return 5;
	case '6':
		return 6;
	case '7':
		return 7;
	case '8':
		return 8;
	case '9':
		return 9;
	default :
		return 0;
		
	}
}

void IpAndMask(char *ip, char *mask, char *des)
{
	char netmask[16];
	memset(netmask, '0', sizeof(netmask));

	strncpy(netmask, mask, 16);
	netmask[15] = '\0';

	int i = CheckNetMask(netmask);
	if (i == -1)
	{
		memcpy(des, ip, 20);
	}
	else
	{
		int j=0;
		for(j=0; j<16; j++)
		{
			des[j] = ip[j];
		}
		char temp[5];
		memset(temp, 0, sizeof(temp));
		sprintf(temp, "/%d", i);
		strcat(des, temp);
	}
}

//判断是否启用邮件告警(判断use_mail属性值)
int is_usemail(void)
{
	char value[5];
	memset(value, 0, sizeof(value));

	if(CC_FindConfig_Value(FILEPATH, "General", "use_mail", value) != 1)
		return -1;//读取"use_mail"属性异常
	
	int ret = -1;
	ret = atoi(value);
	return ret;//0:不使用邮件告警, 1:使用邮件告警
}

//找到其他配置信息并填充邮件头信息结构体
int findotherinfo(cc_mailhead *my_mail_info)
{
	char value[51];
	
	memset(value, 0, sizeof(value));
	if (CC_FindConfig_Value(FILEPATH, "MailConfig", "servername", value) != 1)
		return -2;//读取邮件服务器名"servername"发生异常情况	
	else
		strcpy((*my_mail_info).servername, value);//获得邮件服务器名
	#ifdef	_TEST_DEBUG_                      
		tests(value);
	#endif   
	
	memset(value, 0, sizeof(value));
	if (CC_FindConfig_Value(FILEPATH, "MailConfig", "use_serverchk", value) != 1)
		return -3;//读取是否启用服务器验证"use_serverchk"发生异常情况	
	else
		strcpy((*my_mail_info).use_serverchk, value);//是否启用服务器验证
	#ifdef	_TEST_DEBUG_                      
		tests(value);
	#endif
		
	if (memcmp((*my_mail_info).use_serverchk, "1", strlen((*my_mail_info).use_serverchk)) == 0)
	{
		memset(value, 0, sizeof(value));
		if (CC_FindConfig_Value(FILEPATH, "MailConfig", "username", value) != 1)
			return -3;//读取用户名"username"发生异常情况	
		else
			strcpy((*my_mail_info).username, value);//获得用户名
		#ifdef	_TEST_DEBUG_                      
			tests(value);
		#endif   
		

		memset(value, 0, sizeof(value));
		if (CC_FindConfig_Value(FILEPATH, "MailConfig", "password", value) != 1)
			return -4;//读取用户密码"password"发生异常情况	
		else
			strcpy((*my_mail_info).password, value);//获得用户密码
		#ifdef	_TEST_DEBUG_                      
			tests(value);
		#endif  
	}
	 
	memset(value, 0, sizeof(value));
	if (CC_FindConfig_Value(FILEPATH, "MailConfig", "addr_source", value) != 1)
		return -5;//读取发信邮箱名"addr_source"发生异常情况	
	else
		strcpy((*my_mail_info).addr_source, value);//获得发信邮箱名
	#ifdef	_TEST_DEBUG_                      
		tests(value);
	#endif   


	memset(value, 0, sizeof(value));
	if (CC_FindConfig_Value(FILEPATH, "MailConfig", "addr_dest", value) != 1)
		return -6;//读取收信邮箱名"addr_dest"发生异常情况	
	else
		strcpy((*my_mail_info).addr_dest, value);//获得收信邮箱名
	#ifdef	_TEST_DEBUG_                      
		tests(value);
	#endif   
	
	strcpy((*my_mail_info).title, "Mflow带宽管理器告警信息");

	return 1;//邮件头信息齐全
}

void build_msg(char *ip,char *mask,int direction, char *alarmtime, char *alarmmsg, char* content)
{	
	char temp[25];
	memset(temp, 0, sizeof(temp));
	IpAndMask(ip, mask, temp);
	
	strcat(content, "\r\n");
	strcat(content, "告警时间: ");
	strcat(content, alarmtime);
	strcat(content, " IP地址: ");
	strcat(content, temp);
	strcat(content, " 告警信息: ");
	strcat(content, alarmmsg);
		
	strcat(content, " 方向: ");
	if(direction == 0)
		strcat(content,"LAN->WAN");
	else
	if (direction == 1)
		strcat(content,"WAN->LAN");
	else
		strcat(content,"总流量");
	strcat(content, "\r\n");
}

int MailMsg(char *ip,char *mask,int direction, char *alarmtime, char *alarmmsg)
{
	int ret = 0;

	if (-1 == CC_FindConfig_File(DNS_CONFIGFILE_PATH))//没有找到DNS服务器配置文件
		return 0;
	if (-1 == CC_FindConfig_File(FILEPATH))//未找到配置文件
		return 0;
	if(CC_FindConfig_Item(FILEPATH, "General", "use_mail") != 1)//use_mail属性不存在
		return 0;
	if (is_usemail() != 1)//0或-1---代表不启用邮件告警
		return 0;
	
	cc_mailhead my_mail_info;
	memset(&my_mail_info, 0, sizeof(cc_mailhead));
	
	if (findotherinfo(&my_mail_info) == !1)//其他配置信息不全
		return 0;
	
	char mail_msg[180];
	memset(mail_msg, 0, sizeof(mail_msg));
	
	build_msg(ip, mask, direction, alarmtime, alarmmsg, mail_msg);

	ret = Send_Mail(&my_mail_info, mail_msg);//发送邮件	
	//int_tests("Send_Mail return code ", ret);
	if (ret != 1)
		return 0;
	else
		return 1;
}

int SetDBTable(int type, char *ip,char *mask,int direction, int flow_setting, unsigned long long flow_now)
{
	//tests("in SetDBTable()");
	char alarmtime[30] ,way[10], flag[2], msg[50],unit[10];
	int	 cFlag = -1;
	int	 ret = -1;
	char sql[255];
	MYSQL SmartQoS;
	
	memset(way,0,sizeof(way));
	memset(alarmtime,0,sizeof(alarmtime));
	memset(msg,0,sizeof(msg));
	memset(flag,0,sizeof(flag));
	memset(unit,0,sizeof(unit));

	sprintf(way,"%d",direction);
	get_ctime(alarmtime);
	
	if (type == 0)
		sprintf(unit, "Mb/s");
	else
		sprintf(unit, "MB");
	sprintf(msg, "当前流量:%llu%s, 超出(等于)设定值:%d%s",flow_now,unit,flow_setting,unit);
	
	ret = MailMsg(ip, mask, direction, alarmtime, msg);
	sprintf(flag, "%d", ret);
	
	mysql_init(&SmartQoS);
	cFlag = (int)mysql_real_connect(&SmartQoS,DB_HOST_NAME,DB_USER_NAME,DB_USER_PWD,DB_DBAS_NAME,(unsigned int)mysql_port,(const char*)NULL,(unsigned int)0);	
	if(cFlag){
		memset(sql,0,sizeof(sql));
		sprintf(sql,"INSERT INTO Alarm_Msg (ip,mask,direction,message,alarmtime,flag) VALUES('");
		strcat(sql, ip);
		strcat(sql, "','");
		strcat(sql, mask);
		strcat(sql, "',");
		strcat(sql, way);
		strcat(sql, ",'");
		strcat(sql, msg);
		strcat(sql, "','");
		strcat(sql, alarmtime);
		strcat(sql, "',");
		strcat(sql, flag);
		strcat(sql, ")");
		//tests(sql);
		ret=mysql_real_query(&SmartQoS,sql,strlen(sql));
		mysql_close(&SmartQoS);
	}
	else{
		return 0;
	}
	//tests("Out SetDBTable()");
	return 1;
}

unsigned long long  GetFlux(int eth)
{
	char buf[256];
	char bytes[20];
	unsigned long long thisbytes_1,thisbytes_2,currate;
	time_t ct_1,ct_2;
	FILE *fp = NULL;
	
	memset(buf,0,sizeof(buf));
	memset(bytes,0,sizeof(bytes));
	sprintf(buf,"/sbin/tc -s qdisc ls dev eth%d",eth);
	thisbytes_1 = 0;
	ct_1 = time(NULL);
	if((fp = popen(buf,"r")) == NULL){	
		thisbytes_1 = 0;
	}
	else{ 
		while((fgets(buf,sizeof(buf),fp)) != NULL){ 
			if((strstr(buf,"qdisc htb"))!=NULL){
				if((fgets(buf,sizeof(buf),fp)) != NULL){
					if((strstr(buf,"Sent"))!=NULL){ 
						if((strstr(buf,"bytes"))!=NULL){
							//tests(buf);
							sscanf(buf,"%*s %s",bytes);
							thisbytes_1 = (atoll(bytes)+1);
							break;
						} 
					} 
				}
			}
		} 
		fflush(fp);
		pclose(fp);
	} 
	sleep(5);
	memset(buf,0,sizeof(buf));
	memset(bytes,0,sizeof(bytes));
	sprintf(buf,"/sbin/tc -s qdisc ls dev eth%d",eth);
	thisbytes_2 = 0;
	ct_2 = time(NULL);
	if((fp = popen(buf,"r")) == NULL)	
		thisbytes_2 = 0;
	else{ 
		while((fgets(buf,sizeof(buf),fp)) != NULL){
			if((strstr(buf,"qdisc htb"))!=NULL){
				if((fgets(buf,sizeof(buf),fp)) != NULL){
					if((strstr(buf,"Sent"))!=NULL){ 
						if((strstr(buf,"bytes"))!=NULL){ 
							//tests(buf);
							sscanf(buf,"%*s %s",bytes);
							thisbytes_2 = (atoll(bytes)+1);
							break;
						} 
					}
				} 
			}
		} 
		fflush(fp);
		pclose(fp);
	}
		
	memset(buf, 0, sizeof(buf));
	sprintf(buf, "eth%d : thisbytes_2=%llu  thisbytes_1=%llu, difftime=%lf",eth, thisbytes_2, thisbytes_1, difftime(ct_2,ct_1));
	//tests(buf);
	if ((thisbytes_2 -thisbytes_1)<=0)
		return 0;	
	
	currate = ((thisbytes_2 -thisbytes_1)*8/difftime(ct_2,ct_1))/(1024*1024);
	
	sprintf(buf, "((thisbytes_2 -thisbytes_1)*8/difftime(ct_2,ct_1))/(1024*1024) = %llu", currate);
	//tests(buf);
	if (currate<0)
		return 0;
	return currate;
}

int GetControlProject(void)//得到需要控制的地址,网段 返回1:查询成功  0:查询失败
{
	int	 cFlag = -1;
	int	 ret = -1;
	int	 count = 0 , tlen = 0 ;
	char sql[255];
	MYSQL SmartQoS;
	MYSQL_RES *res;
	MYSQL_ROW row;
	tablecount = 0;
	memset(sql,0,sizeof(sql));
	mysql_init(&SmartQoS);
	cFlag = (int)mysql_real_connect(&SmartQoS,DB_HOST_NAME,DB_USER_NAME,DB_USER_PWD,DB_DBAS_NAME,(unsigned int)mysql_port,(const char*)NULL,(unsigned int)0);	
	if(cFlag){
		g_DBisEror = 1;
		memset(sql,0,sizeof(sql));
		sprintf(sql,"select * from Control order by id");//State==1表示流量还没有达到设定值,==0表示已达标,并且已限制.
		ret=mysql_real_query(&SmartQoS,sql,strlen(sql));
		if (ret == 0){ 
			res = mysql_store_result(&SmartQoS) ;
			tlen = (int) mysql_num_rows(res);
			if (tlen <= 0){				
				mysql_free_result(res);
				mysql_close(&SmartQoS);
				//tests("System Run Message!------DBcount=0!\n");
				return 0;
			}

			for(count = 0; count < tlen; count++ ){
				row = mysql_fetch_row(res);
				if(row[0] != NULL)
					strcpy(sp_control_table[tablecount].id,row[0]);
				else
					continue ;

				if(row[2] != NULL)
					strcpy(sp_control_table[tablecount].ip,row[2]);
				else
					continue ;
				
				if(row[3] != NULL)
					strcpy(sp_control_table[tablecount].mask,row[3]);
				else
					continue ;
				
				if(row[4] != NULL)
					strcpy(sp_control_table[tablecount].direction,row[4]);
				else
					continue ;
				
				if(row[5] != NULL)
					strcpy(sp_control_table[tablecount].flow,row[5]);
				else
					continue ;
				
				if(row[6] != NULL)
					strcpy(sp_control_table[tablecount].time_b,row[6]);
				else
					continue ;

				if(row[7] != NULL)
					strcpy(sp_control_table[tablecount].time_e,row[7]);
				else
					continue ;
				
				if (row[8] != NULL)
					strcpy(sp_control_table[tablecount].state, row[8]);
				else
					continue;

				if (row[10] != NULL)
					strcpy(sp_control_table[tablecount].stattype, row[10]);
				else
					continue;

				if(row[1] != NULL)
					strcpy(sp_control_table[tablecount].firewallid,row[1]);

				tablecount++;
				if (tablecount>=255)
					break;
			}
			mysql_free_result(res);  
			mysql_close(&SmartQoS);
		}
		else{
			mysql_close(&SmartQoS);
			return 0;
		}
	}
	else{
		return 0;
	}
	return 1;
}

//增加生效标记
int AddFlag(int i)
{
	if (interdiction_tablecount<255)
	{
		strcpy(sp_interdiction_table[interdiction_tablecount].ip, sp_control_table[i].ip);
		strcpy(sp_interdiction_table[interdiction_tablecount].mask, sp_control_table[i].mask);
		strcpy(sp_interdiction_table[interdiction_tablecount].direction, sp_control_table[i].direction);
		strcpy(sp_interdiction_table[interdiction_tablecount].time_b, sp_control_table[i].time_b);
		strcpy(sp_interdiction_table[interdiction_tablecount].time_e, sp_control_table[i].time_e);
		strcpy(sp_interdiction_table[interdiction_tablecount].id, sp_control_table[i].id);
		interdiction_tablecount++;
	}
	return 1;
}


//时间范围是否交叉或当前时间是否不在该流量策略生效时间范围之内
int istimeacross(int i, int j)
{
	//时间范围交叉
	if (comparetime(sp_control_table[i].time_b, now) == 0 && comparetime(sp_interdiction_table[j].time_e, now)==0)
		return 1;
	if (comparetime(sp_control_table[i].time_e, now) == 0 &&  comparetime(sp_interdiction_table[j].time_b, now)==0)
		return 1;
	
	if ( comparetime(sp_control_table[i].time_e, sp_interdiction_table[j].time_b)>=0 
	   &&comparetime(sp_control_table[i].time_e, sp_interdiction_table[j].time_e)<=0 )
		return 1;
	if ( comparetime(sp_control_table[i].time_b, sp_interdiction_table[j].time_b)>=0 
	   &&comparetime(sp_control_table[i].time_b, sp_interdiction_table[j].time_e)<=0 )
		return 1;
	if ( comparetime(sp_interdiction_table[j].time_e, sp_control_table[i].time_b)>=0 
	   &&comparetime(sp_interdiction_table[j].time_e, sp_control_table[i].time_e)<=0 )
		return 1;
	if ( comparetime(sp_interdiction_table[j].time_b, sp_control_table[i].time_b)>=0 
	   &&comparetime(sp_interdiction_table[j].time_b, sp_control_table[i].time_e)<=0 )

⌨️ 快捷键说明

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