示例#1
0
void ipt_qoslimit(int chain)
{
	char *buf;
	char *g;
	char *p;
	char *ibw,*obw;//bandwidth
	char seq[4];//mark number
	int iSeq = 10;
	char *ipaddr_old;
	char ipaddr[30];//ip address
	char *dlrate,*dlceil;//guaranteed rate & maximum rate for download
	char *ulrate,*ulceil;//guaranteed rate & maximum rate for upload
	char *priority;//priority
	char *lanipaddr; //lan ip address
	char *lanmask; //lan netmask
	char *tcplimit,*udplimit;//tcp connection limit & udp packets per second
	int priority_num;
	char *qosl_tcp,*qosl_udp;
	int i, address_type;

	//qos1 is enable
	if (!nvram_get_int("new_qoslimit_enable")) return;
	
	//read qos1rules from nvram
	g = buf = strdup(nvram_safe_get("new_qoslimit_rules"));

	ibw = nvram_safe_get("qos_ibw");  // Read from QOS setting - KRP
	obw = nvram_safe_get("qos_obw");  // Read from QOS setting - KRP
	
	lanipaddr = nvram_safe_get("lan_ipaddr");
	lanmask = nvram_safe_get("lan_netmask");
	
	qosl_tcp = nvram_safe_get("qosl_tcp");
	qosl_udp = nvram_safe_get("qosl_udp");
	
	//MANGLE
	if (chain == 1)
	{
		if (nvram_get_int("qosl_enable") == 1) {
			ipt_write(
			"-A POSTROUTING ! -s %s/%s -d %s/%s -j MARK --set-mark 100\n"
			"-A PREROUTING  -s %s/%s ! -d %s/%s -j MARK --set-mark 100\n"
			,lanipaddr,lanmask,lanipaddr,lanmask
			,lanipaddr,lanmask,lanipaddr,lanmask);
		}

		//shibby br1
		if (nvram_get_int("limit_br1_enable") == 1) {

			char *lan1_ipaddr; //lan1 ip address
			char *lan1_mask; //lan1 netmask

			lan1_ipaddr = nvram_safe_get("lan1_ipaddr");
			lan1_mask = nvram_safe_get("lan1_netmask");

			ipt_write(
				"-A POSTROUTING -d %s/%s -j MARK --set-mark 401\n"
				"-A PREROUTING -s %s/%s -j MARK --set-mark 501\n"
				,lan1_ipaddr,lan1_mask
				,lan1_ipaddr,lan1_mask);
		}

		//shibby br2
		if (nvram_get_int("limit_br2_enable") == 1) {

			char *lan2_ipaddr; //lan2 ip address
			char *lan2_mask; //lan2 netmask

			lan2_ipaddr = nvram_safe_get("lan2_ipaddr");
			lan2_mask = nvram_safe_get("lan2_netmask");

			ipt_write(
				"-A POSTROUTING -d %s/%s -j MARK --set-mark 601\n"
				"-A PREROUTING -s %s/%s -j MARK --set-mark 701\n"
				,lan2_ipaddr,lan2_mask
				,lan2_ipaddr,lan2_mask);
		}
		//shibby br3
		if (nvram_get_int("limit_br3_enable") == 1) {

			char *lan3_ipaddr; //lan3 ip address
			char *lan3_mask; //lan3 netmask

			lan3_ipaddr = nvram_safe_get("lan3_ipaddr");
			lan3_mask = nvram_safe_get("lan3_netmask");

			ipt_write(
				"-A POSTROUTING -d %s/%s -j MARK --set-mark 801\n"
				"-A PREROUTING -s %s/%s -j MARK --set-mark 901\n"
				,lan3_ipaddr,lan3_mask
				,lan3_ipaddr,lan3_mask);
		}
	}
	
	//NAT
	if (chain == 2)
	{
		if (nvram_get_int("qosl_enable") == 1) {
			if (nvram_get_int("qosl_tcp") > 0) {
				ipt_write(
					"-A PREROUTING -s %s/%s -p tcp --syn -m connlimit --connlimit-above %s -j DROP\n"
				,lanipaddr,lanmask,qosl_tcp);
			}
			
			if (nvram_get_int("qosl_udp") > 0) {
				ipt_write(
					"-A PREROUTING -s %s/%s -p udp -m limit --limit %s/sec -j ACCEPT\n"
				,lanipaddr,lanmask,qosl_udp);
			}
		}
	}
	
	while (g) {
		/*
		ipaddr_old<dlrate<dlceil<ulrate<ulceil<priority<tcplimit<udplimit
		*/
		if ((p = strsep(&g, ">")) == NULL) break;
		i = vstrsep(p, "<", &ipaddr_old, &dlrate, &dlceil, &ulrate, &ulceil, &priority, &tcplimit, &udplimit);
		if (i!=8) continue;

		priority_num = atoi(priority);
		if ((priority_num < 0) || (priority_num > 5)) continue;

		if (!strcmp(ipaddr_old,"")) continue;
		
		address_checker (&address_type, ipaddr_old, ipaddr);
		sprintf(seq,"%d",iSeq);
		iSeq++; 

		if (!strcmp(dlceil,"")) strcpy(dlceil, dlrate);
		if (strcmp(dlrate,"") && strcmp(dlceil, "")) {
			if(chain == 1) {
				switch (address_type)
				{
					case IP_ADDRESS:
						ipt_write(
							"-A POSTROUTING ! -s %s/%s -d %s -j MARK --set-mark %s\n"
							,lanipaddr,lanmask,ipaddr,seq);
						break;
					case MAC_ADDRESS:
						break;
					case IP_RANGE:
						ipt_write(
							"-A POSTROUTING ! -s %s/%s -m iprange --dst-range  %s -j MARK --set-mark %s\n"
							,lanipaddr,lanmask,ipaddr,seq);
						break;
				}
			}
		}
		
		if (!strcmp(ulceil,"")) strcpy(ulceil, ulrate);
		if (strcmp(ulrate,"") && strcmp(ulceil, "")) {
			if (chain == 1) {
				switch (address_type)
				{
					case IP_ADDRESS:
						ipt_write(
							"-A PREROUTING -s %s ! -d %s/%s -j MARK --set-mark %s\n"
							,ipaddr,lanipaddr,lanmask,seq);
						break;
					case MAC_ADDRESS:
						ipt_write(
							"-A PREROUTING -m mac --mac-source %s ! -d %s/%s  -j MARK --set-mark %s\n"
							,ipaddr,lanipaddr,lanmask,seq);
						break;
					case IP_RANGE:
						ipt_write(
							"-A PREROUTING -m iprange --src-range %s ! -d %s/%s -j MARK --set-mark %s\n"
							,ipaddr,lanipaddr,lanmask,seq);
						break;
				}
			}
		}
		
		if(atoi(tcplimit) > 0){
			if (chain == 2) {
				switch (address_type)
				{
						case IP_ADDRESS:
							ipt_write(
							"-A PREROUTING -s %s -p tcp --syn -m connlimit --connlimit-above %s -j DROP\n"
							,ipaddr,tcplimit);
							break;
						case MAC_ADDRESS:
							ipt_write(
							"-A PREROUTING -m mac --mac-source %s -p tcp --syn -m connlimit --connlimit-above %s -j DROP\n"
							,ipaddr,tcplimit);
							break;
						case IP_RANGE:
							ipt_write(
							"-A PREROUTING -m iprange --src-range %s -p tcp --syn -m connlimit --connlimit-above %s -j DROP\n"
							,ipaddr,tcplimit);
							break;
				}
			}
		}
		if(atoi(udplimit) > 0){
			if (chain == 2) {
				switch (address_type)
				{
					case IP_ADDRESS:
						ipt_write(
							"-A PREROUTING -s %s -p udp -m limit --limit %s/sec -j ACCEPT\n"
							,ipaddr,udplimit);
						break;
					case MAC_ADDRESS:
						ipt_write(
							"-A PREROUTING -m mac --mac-source %s -p udp -m limit --limit %s/sec -j ACCEPT\n"
							,ipaddr,udplimit);
						break;
					case IP_RANGE:
						ipt_write(
							"-A PREROUTING -m iprange --src-range %s -p udp -m limit --limit %s/sec -j ACCEPT\n"
							,ipaddr,udplimit);
						break;
				}
			}
		}
	}
	free(buf);
}
示例#2
0
// read nvram into files
void new_qoslimit_start(void)
{
	FILE *tc;
	char *buf;
	char *g;
	char *p;
	char *ibw,*obw;//bandwidth
	char seq[4];//mark number
	int iSeq = 10;
	char *ipaddr_old; 
	char ipaddr[30];//ip address
	char *dlrate,*dlceil;//guaranteed rate & maximum rate for download
	char *ulrate,*ulceil;//guaranteed rate & maximum rate for upload
	char *priority;//priority
	char *lanipaddr; //lan ip address
	char *lanmask; //lan netmask
	char *tcplimit,*udplimit;//tcp connection limit & udp packets per second
	int priority_num;
	char *dlr,*dlc,*ulr,*ulc; //download / upload - rate / ceiling
	int i, address_type;
	int s[6];
	char *waniface; //shibby

	//qos1 is enable
	if (!nvram_get_int("new_qoslimit_enable")) return;

	//read qos1rules from nvram
	g = buf = strdup(nvram_safe_get("new_qoslimit_rules"));

	ibw = nvram_safe_get("qos_ibw");  
	obw = nvram_safe_get("qos_obw");  

	lanipaddr = nvram_safe_get("lan_ipaddr");
	lanmask = nvram_safe_get("lan_netmask");
	waniface = nvram_safe_get("wan_iface"); //shibby

	dlr = nvram_safe_get("qosl_dlr"); //Qos limit download rate
	dlc = nvram_safe_get("qosl_dlc"); //download ceiling
	ulr = nvram_safe_get("qosl_ulr"); //upload rate
	ulc = nvram_safe_get("qosl_ulc"); //upload ceiling
	
	if ((tc = fopen(qoslimitfn, "w")) == NULL) return;

	fprintf(tc,
		"#!/bin/sh\n"
		"tc qdisc del dev br0 root 2>/dev/null\n"
		"tc qdisc del dev %s root 2>/dev/null\n"
		"\n"
		"TCA=\"tc class add dev br0\"\n"
		"TFA=\"tc filter add dev br0\"\n"
		"TQA=\"tc qdisc add dev br0\"\n"
		"\n"
		"SFQ=\"sfq perturb 10\"\n"
		"\n"
		"TCAU=\"tc class add dev %s\"\n"
		"TFAU=\"tc filter add dev %s\"\n"
		"TQAU=\"tc qdisc add dev %s\"\n"
		"\n"
		"tc qdisc add dev br0 root handle 1: htb\n"
		"tc class add dev br0 parent 1: classid 1:1 htb rate %skbit\n"
		"\n"
		"tc qdisc add dev %s root handle 2: htb\n"
		"tc class add dev %s parent 2: classid 2:1 htb rate %skbit\n"
		"\n"
		,waniface
		,waniface
		,waniface
		,waniface
		,ibw
		,waniface
		,waniface,obw
	);
	
	if ((nvram_get_int("qosl_enable") == 1) && strcmp(dlr,"") && strcmp(ulr,"")) {
		if (!strcmp(dlc,"")) strcpy(dlc, dlr);
		if (!strcmp(ulc,"")) strcpy(ulc, ulr);
		fprintf(tc,
		"$TCA parent 1:1 classid 1:100 htb rate %skbit ceil %skbit prio 3\n"
		"$TQA parent 1:100 handle 100: $SFQ\n"
		"$TFA parent 1:0 prio 3 protocol ip handle 100 fw flowid 1:100\n"
		"\n"
		"$TCAU parent 2:1 classid 2:100 htb rate %skbit ceil %skbit prio 3\n"
		"$TQAU parent 2:100 handle 100: $SFQ\n"
		"$TFAU parent 2:0 prio 3 protocol ip handle 100 fw flowid 2:100\n"
		"\n"
		,dlr,dlc
		,ulr,ulc);
	}
		
	while (g) {
		/*
		ipaddr_old<dlrate<dlceil<ulrate<ulceil<priority<tcplimit<udplimit
		*/
		if ((p = strsep(&g, ">")) == NULL) break;
		i = vstrsep(p, "<", &ipaddr_old, &dlrate, &dlceil, &ulrate, &ulceil, &priority, &tcplimit, &udplimit);
		if (i!=8) continue;

		priority_num = atoi(priority);
		if ((priority_num < 0) || (priority_num > 5)) continue;

		if (!strcmp(ipaddr_old,"")) continue;
		
		address_checker(&address_type, ipaddr_old, ipaddr);
		sprintf(seq,"%d",iSeq);
		iSeq++;
		if (!strcmp(dlceil,"")) strcpy(dlceil, dlrate);
		if (strcmp(dlrate,"") && strcmp(dlceil, "")) {
			if (address_type != MAC_ADDRESS) {
				fprintf(tc,
					"$TCA parent 1:1 classid 1:%s htb rate %skbit ceil %skbit prio %s\n"
					"$TQA parent 1:%s handle %s: $SFQ\n"
					"$TFA parent 1:0 prio %s protocol ip handle %s fw flowid 1:%s\n"
					"\n"
					,seq,dlrate,dlceil,priority
					,seq,seq
					,priority,seq,seq);
			}
			else if (address_type == MAC_ADDRESS ) {
				sscanf(ipaddr, "%02X:%02X:%02X:%02X:%02X:%02X",&s[0],&s[1],&s[2],&s[3],&s[4],&s[5]);
				
				fprintf(tc,
					"$TCA parent 1:1 classid 1:%s htb rate %skbit ceil %skbit prio %s\n"
					"$TQA parent 1:%s handle %s: $SFQ\n"
					"$TFA parent 1:0 protocol ip prio %s u32 match u16 0x0800 0xFFFF at -2 match u32 0x%02X%02X%02X%02X 0xFFFFFFFF at -12 match u16 0x%02X%02X 0xFFFF at -14 flowid 1:%s\n"
					"\n"
					,seq,dlrate,dlceil,priority
					,seq,seq
					,priority,s[2],s[3],s[4],s[5],s[0],s[1],seq);
			}
		}
		
		if (!strcmp(ulceil,"")) strcpy(ulceil, dlrate);
		if (strcmp(ulrate,"") && strcmp(ulceil, "")) {
			fprintf(tc,
				"$TCAU parent 2:1 classid 2:%s htb rate %skbit ceil %skbit prio %s\n"
				"$TQAU parent 2:%s handle %s: $SFQ\n"
				"$TFAU parent 2:0 prio %s protocol ip handle %s fw flowid 2:%s\n"
				"\n"
				,seq,ulrate,ulceil,priority
				,seq,seq
				,priority,seq,seq);
		}
	}
	free(buf);

/* shibby - limit br1 */
	if (nvram_get_int("limit_br1_enable") == 1) {

		char *dlr_1,*dlc_1,*ulr_1,*ulc_1,*prio_1;

		dlr_1 = nvram_safe_get("limit_br1_dlr"); //Qos limit download rate
		dlc_1 = nvram_safe_get("limit_br1_dlc"); //download ceiling
		ulr_1 = nvram_safe_get("limit_br1_ulr"); //upload rate
		ulc_1 = nvram_safe_get("limit_br1_ulc"); //upload ceiling
		prio_1 = nvram_safe_get("limit_br1_prio"); //priority

		if (!strcmp(dlc_1,"")) strcpy(dlc_1, dlr_1);
		if (!strcmp(ulc_1,"")) strcpy(ulc_1, ulr_1);

		//download for br1
		fprintf(tc,
		"TCA1=\"tc class add dev br1\"\n"
		"TFA1=\"tc filter add dev br1\"\n"
		"TQA1=\"tc qdisc add dev br1\"\n"
		"tc qdisc del dev br1 root\n"
		"tc qdisc add dev br1 root handle 4: htb\n"
		"tc class add dev br1 parent 4: classid 4:1 htb rate %skbit\n"
		"$TCA1 parent 4:1 classid 4:401 htb rate %skbit ceil %skbit prio %s\n"
		"$TQA1 parent 4:401 handle 401: $SFQ\n"
		"$TFA1 parent 4:0 prio %s protocol ip handle 401 fw flowid 4:401\n"
		,ibw
		,dlr_1,dlc_1,prio_1
		,prio_1);

		//upload from br1
		fprintf(tc,
		"$TCAU parent 2:1 classid 2:501 htb rate %skbit ceil %skbit prio %s\n"
		"$TQAU parent 2:501 handle 501: $SFQ\n"
		"$TFAU parent 2:0 prio %s protocol ip handle 501 fw flowid 2:501\n"
		,ulr_1,ulc_1,prio_1
		,prio_1);
	}

/* shibby - limit br2 */
	if (nvram_get_int("limit_br2_enable") == 1) {

		char *dlr_2,*dlc_2,*ulr_2,*ulc_2,*prio_2;

		dlr_2 = nvram_safe_get("limit_br2_dlr"); //Qos limit download rate
		dlc_2 = nvram_safe_get("limit_br2_dlc"); //download ceiling
		ulr_2 = nvram_safe_get("limit_br2_ulr"); //upload rate
		ulc_2 = nvram_safe_get("limit_br2_ulc"); //upload ceiling
		prio_2 = nvram_safe_get("limit_br2_prio"); //priority

		if (!strcmp(dlc_2,"")) strcpy(dlc_2, dlr_2);
		if (!strcmp(ulc_2,"")) strcpy(ulc_2, ulr_2);

		//download for br2
		fprintf(tc,
		"TCA2=\"tc class add dev br2\"\n"
		"TFA2=\"tc filter add dev br2\"\n"
		"TQA2=\"tc qdisc add dev br2\"\n"
		"tc qdisc del dev br2 root\n"
		"tc qdisc add dev br2 root handle 6: htb\n"
		"tc class add dev br2 parent 6: classid 6:1 htb rate %skbit\n"
		"$TCA2 parent 6:1 classid 6:601 htb rate %skbit ceil %skbit prio %s\n"
		"$TQA2 parent 6:601 handle 601: $SFQ\n"
		"$TFA2 parent 6:0 prio %s protocol ip handle 601 fw flowid 6:601\n"
		,ibw
		,dlr_2,dlc_2,prio_2
		,prio_2);

		//upload from br2
		fprintf(tc,
		"$TCAU parent 2:1 classid 2:701 htb rate %skbit ceil %skbit prio %s\n"
		"$TQAU parent 2:701 handle 701: $SFQ\n"
		"$TFAU parent 2:0 prio %s protocol ip handle 701 fw flowid 2:701\n"
		,ulr_2,ulc_2,prio_2
		,prio_2);
	}

/* shibby - limit br3 */
	if (nvram_get_int("limit_br3_enable") == 1) {

		char *dlr_3,*dlc_3,*ulr_3,*ulc_3,*prio_3;

		dlr_3 = nvram_safe_get("limit_br3_dlr"); //Qos limit download rate
		dlc_3 = nvram_safe_get("limit_br3_dlc"); //download ceiling
		ulr_3 = nvram_safe_get("limit_br3_ulr"); //upload rate
		ulc_3 = nvram_safe_get("limit_br3_ulc"); //upload ceiling
		prio_3 = nvram_safe_get("limit_br3_prio"); //priority

		if (!strcmp(dlc_3,"")) strcpy(dlc_3, dlr_3);
		if (!strcmp(ulc_3,"")) strcpy(ulc_3, ulr_3);

		//download for br3
		fprintf(tc,
		"TCA3=\"tc class add dev br3\"\n"
		"TFA3=\"tc filter add dev br3\"\n"
		"TQA3=\"tc qdisc add dev br3\"\n"
		"tc qdisc del dev br3 root\n"
		"tc qdisc add dev br3 root handle 8: htb\n"
		"tc class add dev br3 parent 8: classid 8:1 htb rate %skbit\n"
		"$TCA3 parent 8:1 classid 8:801 htb rate %skbit ceil %skbit prio %s\n"
		"$TQA3 parent 8:801 handle 801: $SFQ\n"
		"$TFA3 parent 8:0 prio %s protocol ip handle 801 fw flowid 8:801\n"
		,ibw
		,dlr_3,dlc_3,prio_3
		,prio_3);

		//upload from br3
		fprintf(tc,
		"$TCAU parent 2:1 classid 2:901 htb rate %skbit ceil %skbit prio %s\n"
		"$TQAU parent 2:901 handle 901: $SFQ\n"
		"$TFAU parent 2:0 prio %s protocol ip handle 901 fw flowid 2:901\n"
		,ulr_3,ulc_3,prio_3
		,prio_3);
	}


	fclose(tc);
	chmod(qoslimitfn, 0700);
	
	//fake start
	eval((char *)qoslimitfn, "start");
}
示例#3
0
文件: qos.c 项目: chazikai24/asuswrt
static int start_bandwidth_limiter(void)
{
	FILE *f = NULL;
	char *buf, *g, *p;
	char *enable, *addr, *dlc, *upc, *prio;
	int class = 0;
	int s[6]; // strip mac address
	int addr_type;
	char addr_new[30];

	if ((f = fopen(qosfn, "w")) == NULL) return -2;
	fprintf(f,
		"#!/bin/sh\n"
		"WAN=%s\n"
		"tc qdisc del dev $WAN root 2>/dev/null\n"
		"tc qdisc del dev $WAN ingress 2>/dev/null\n"
		"tc qdisc del dev br0 root 2>/dev/null\n"
		"tc qdisc del dev br0 ingress 2>/dev/null\n"
		"\n"
		"TQAU=\"tc qdisc add dev $WAN\"\n"
		"TCAU=\"tc class add dev $WAN\"\n"
		"TFAU=\"tc filter add dev $WAN\"\n"
		"SFQ=\"sfq perturb 10\"\n"
		"TQA=\"tc qdisc add dev br0\"\n"
		"TCA=\"tc class add dev br0\"\n"
		"TFA=\"tc filter add dev br0\"\n"
		"\n"
		"$TQA root handle 1: htb\n"
		"$TCA parent 1: classid 1:1 htb rate 1024000kbit\n"
		"\n"
		"$TQAU root handle 2: htb\n"
		"$TCAU parent 2: classid 2:1 htb rate 1024000kbit\n"
		, get_wan_ifname(0)
	);

	/* ASUSWRT
	qos_bw_rulelist :
		enable>addr>DL-Ceil>UL-Ceil>prio
		enable : enable or disable this rule
		addr : (source) IP or MAC or IP-range
		DL-Ceil : the max download bandwidth
		UL-Ceil : the max upload bandwidth
		prio : priority for client
	*/

	g = buf = strdup(nvram_safe_get("qos_bw_rulelist"));
	while (g) {
		if ((p = strsep(&g, "<")) == NULL) break;
		if ((vstrsep(p, ">", &enable, &addr, &dlc, &upc, &prio)) != 5) continue;
		if (!strcmp(enable, "0")) continue;

		address_checker(&addr_type, addr, addr_new);
		class = atoi(prio) + 10;
		if (addr_type == TYPE_MAC)
		{
			sscanf(addr_new, "%02X:%02X:%02X:%02X:%02X:%02X",&s[0],&s[1],&s[2],&s[3],&s[4],&s[5]);
			fprintf(f,
				"\n"
				"$TCA parent 1:1 classid 1:%d htb rate %skbit ceil %skbit prio %s\n"
				"$TQA parent 1:%d handle %d: $SFQ\n"
				"$TFA parent 1: protocol ip prio %s u32 match u16 0x0800 0xFFFF at -2 match u32 0x%02X%02X%02X%02X 0xFFFFFFFF at -12 match u16 0x%02X%02X 0xFFFF at -14 flowid 1:%d"
				"\n"
				"$TCAU parent 2:1 classid 2:%d htb rate %skbit ceil %skbit prio %s\n"
				"$TQAU parent 2:%d handle %d: $SFQ\n"
				"$TFAU parent 2: prio %s protocol ip handle %d fw flowid 2:%d\n"
				, class, dlc, dlc, prio
				, class, class
				, prio, s[2], s[3], s[4], s[5], s[0], s[1], class
				, class, upc, upc, prio
				, class, class
				, prio, class, class
			);
		}
		else if (addr_type == TYPE_IP || addr_type == TYPE_IPRANGE)
示例#4
0
文件: run.c 项目: magden/project
/* Second run*/
int run2(input_line * input, int linenum, char *input_fileName)
{
	char addr_array[NUM_OF_DIGITS+1], instr_array[NUM_OF_DIGITS+2], fileName[FILE_NAME_SIZE];/*TEMP ARRAYS FOR CODE SAVING*/
	int  x, line_count = 0;
	FILE *ob, *ext, *ent;
	/*Files opening*/
	sprintf(fileName, "%s.ob", input_fileName);
	ob = fopen(fileName, "w");
	if (ob == NULL)
	{
		printf("Error! Can't create %s file.\n", fileName);
		return 0;
	}
	sprintf(fileName, "%s.ext", input_fileName);
	ext = fopen(fileName, "w");
	if (ext == NULL)
	{
		printf("Error! Can't create %s file.\n", fileName);
		return 0;
	}
	sprintf(fileName, "%s.ent", input_fileName);
	ent = fopen(fileName, "w");
	if (ent == NULL)
	{
		printf("Error! Can't create %s file.\n", fileName);
		return 0;
	}
	/*Prints how many instructions and data.*/
	fprintf(ob, "\t%s   %s\n", base_to_base_con((IC - START - DC), BASE, addr_array, 0), base_to_base_con(DC, BASE, instr_array, NO_LZ));
	/*line_count++;*/
	for (i = 0; i < linenum; i++)
	{
		if (!input[i].line_compl)
		{
			address_checker(input[i].instruction, input[i].line_number);

			/*Check if a command like MOV1 or MOV2, add1 or add2 etc... */
			x = input[i].how_many;
			while (x != 0)
			{
				/* The first line of of instruction(not operands).*/
				fprintf(ob, "%s\t%s\t\n", base_to_base_con(line_count + LINE_OFFSET, BASE, addr_array, NO_LZ), base_to_base_con(converte(*(input[i].instruction)).data, BASE, instr_array, LZ));
				line_count++;

				/*If two registers are in the instruction. In machine language they must be in ONE line.*/
				if ((input[i].instruction->dest_addr == 3) && (input[i].instruction->src_addr == 3))
				{
					int dest, src;
					int tmp1 = 0;
					int tmp2 = 0;
					int fin;
					int y = MAX_OP_NAME;
					int z = 1;

					if (input[i].src_opr[0] == '$')
					{
						if (check_for_last_oper()){
							while (z != MAX_OP_NAME) /*this loop is converting back the last reg value(in base4) to int*/
							{
								tmp1 += (last_dest_opr[y - z] - '0')*(int)(power(4, z - 1));
								z++;
							}
						}
						else
						{
							ERROR("The $$ can't be used at this line.Because no were operands before this instruction.", input[i].line_number);
							error_flag = TRUE;
						}
					}
					else
					{
						src = (input[i].src_opr[1]) - '0';
						tmp1 = src << 7;
						strcpy(last_dest_opr, base_to_base_con(tmp1, BASE, instr_array, LZ));/*Save this reg for may be next $$*/
					}
					dest = (input[i].dest_opr[1]) - '0';
					tmp2 = dest << 2;
					fin = tmp1 | tmp2;
					fprintf(ob, "%s\t%s\n", base_to_base_con(line_count + LINE_OFFSET, BASE, addr_array, NO_LZ), base_to_base_con(fin, BASE, instr_array, LZ));
					line_count++;
				}
				else
				{
					if (input[i].instruction->gr_code == 2)
					{
						if (input[i].src_opr)
							operand_extractor(input[i].src_opr, input[i].instruction->src_addr, ob, ext, &line_count, input[i].line_number, TRUE);
						if (input[i].dest_opr)/*The last argumet of the funct"TRUE" means that is's source operand(not dest)  */
							operand_extractor(input[i].dest_opr, input[i].instruction->dest_addr, ob, ext, &line_count, input[i].line_number, FALSE);
					}
					if (input[i].instruction->gr_code == 1)
						operand_extractor(input[i].dest_opr, input[i].instruction->dest_addr, ob, ext, &line_count, input[i].line_number, TRUE);

				}
				x--;
			}
		}
	}

	/*Write .ob file*/
	for (i = 0; i < DC; i++)
	{
		fprintf(ob, "%s\t%s\n", base_to_base_con(line_count + LINE_OFFSET, BASE, addr_array, NO_LZ), base_to_base_con(data_array[i].data, BASE, instr_array, LZ));
		line_count++;
	}

	/*Write .ent file*/
	for (i = 0; i<entryCount; i++)
	{

		if ((tmp_node = search_node(array_entry[i], instructions_table)))
			fprintf(ent, "\t%s\t\t%s\n", array_entry[i], base_to_base_con(tmp_node->num, BASE, addr_array, NO_LZ));

		else if ((tmp_node = search_node(array_entry[i], data_table)))
			fprintf(ent, "\t%s\t%s\n", array_entry[i], base_to_base_con(tmp_node->num, BASE, addr_array, NO_LZ));

		else
		{
			fprintf(stderr, "ERROR: Ca't fund address for the entry %s.\n", array_entry[i]);
			error_flag = TRUE;
		}
	}

	/*Close all files*/
	fclose(ob);
	fclose(ext);
	fclose(ent);

	/*Delete all created files in case of failure*/
	if (error_flag)
	{
		sprintf(fileName, "%s.ob", input_fileName);
		remove(fileName);
		sprintf(fileName, "%s.ext", input_fileName);
		remove(fileName);
		sprintf(fileName, "%s.ent", input_fileName);
		remove(fileName);
	}

	/*Tester if no externs provided.*/
	else if (externCount == 0)
	{
		sprintf(fileName, "%s.ext", input_fileName);
		remove(fileName);
	}
	/*Tester if no entrys provided.*/
	else if (entryCount == 0)
	{
		sprintf(fileName, "%s.ent", input_fileName);
		remove(fileName);
	}
	return 0;
}
示例#5
0
文件: qos.c 项目: chazikai24/asuswrt
static int add_bandwidth_limiter_rules(char *pcWANIF)
{
	FILE *fn = NULL;
	char *buf, *g, *p;
	char *enable, *addr, *dlc, *upc, *prio;
	char lan_addr[32];
	char addr_new[32];
	int addr_type;

	if ((fn = fopen(mangle_fn, "w")) == NULL) return -2;
	del_iQosRules(); // flush all rules in mangle table

#ifdef RTCONFIG_RALINK
	const char *action = "CONNMARK --set-return";
#else
	const char *action = "MARK --set-mark";
#endif

	/* ASUSWRT
	qos_bw_rulelist :
		enable>addr>DL-Ceil>UL-Ceil>prio
		enable : enable or disable this rule
		addr : (source) IP or MAC or IP-range
		DL-Ceil : the max download bandwidth
		UL-Ceil : the max upload bandwidth
		prio : priority for client
	*/

	memset(lan_addr, 0, sizeof(lan_addr));
	sprintf(lan_addr, "%s/%s", nvram_safe_get("lan_ipaddr"), nvram_safe_get("lan_netmask"));

	fprintf(fn,
		"*mangle\n"
		":PREROUTING ACCEPT [0:0]\n"
		":OUTPUT ACCEPT [0:0]\n"
		);

	g = buf = strdup(nvram_safe_get("qos_bw_rulelist"));
	while (g) {
		if ((p = strsep(&g, "<")) == NULL) break;
		if ((vstrsep(p, ">", &enable, &addr, &dlc, &upc, &prio)) != 5) continue;
		if (!strcmp(enable, "0")) continue;
		memset(addr_new, 0, sizeof(addr_new));
		address_checker(&addr_type, addr, addr_new);
		//_dprintf("[BWLIT] %s: addr_type=%d, addr=%s, add_new=%s, lan_addr=%s\n", __FUNCTION__, addr_type, addr, addr_new, lan_addr);

		if (addr_type == TYPE_IP){
			fprintf(fn,
				"-A POSTROUTING ! -s %s -d %s -j %s %d\n"
				"-A PREROUTING -s %s ! -d %s -j %s %d\n"
				, lan_addr, addr_new, action, atoi(prio)+10
				, addr_new, lan_addr, action, atoi(prio)+10
				);
		}
		else if (addr_type == TYPE_MAC){
			fprintf(fn,
				"-A PREROUTING -m mac --mac-source %s ! -d %s  -j %s %d\n"
				, addr_new, lan_addr, action, atoi(prio)+10
				);
		}
		else if (addr_type == TYPE_IPRANGE){
			fprintf(fn,
				"-A POSTROUTING ! -s %s -m iprange --dst-range %s -j %s %d\n"
				"-A PREROUTING -m iprange --src-range %s ! -d %s -j %s %d\n"
				, lan_addr, addr_new, action, atoi(prio)+10
				, addr_new, lan_addr, action, atoi(prio)+10
				);
		}
	}
	free(buf);

	fprintf(fn, "COMMIT\n");
	fclose(fn);
	chmod(mangle_fn, 0700);
	eval("iptables-restore", (char*)mangle_fn);
	_dprintf("[BWLIT] %s: create rules\n", __FUNCTION__);

	return 0;
}