示例#1
0
文件: bwm.c 项目: NieHao/Tomato-RAF
void asp_netdev(int argc, char **argv)
{
	FILE *f;
	char buf[256];
	int64_t rx, tx;
	char *p;
	char *ifname;
	char comma;
	char *exclude;
	int sfd;
	struct ifreq ifr;

	exclude = nvram_safe_get("rstats_exclude");
	web_puts("\n\nnetdev={");
	if ((f = fopen("/proc/net/dev", "r")) != NULL) {
		fgets(buf, sizeof(buf), f);	// header
		fgets(buf, sizeof(buf), f);	// "
		comma = ' ';

		if ((sfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
			_dprintf("[%s %d]: error opening socket %m\n", __FUNCTION__, __LINE__);
		}

		while (fgets(buf, sizeof(buf), f)) {
			if ((p = strchr(buf, ':')) == NULL) continue;
			*p = 0;
			if ((ifname = strrchr(buf, ' ')) == NULL) ifname = buf;
				else ++ifname;
//			if (strncmp(ifname, "ppp", 3) == 0) ifname = "ppp";
			if ((strcmp(ifname, "lo") == 0) || (find_word(exclude, ifname))) continue;

			// skip down interfaces
			if (sfd >= 0) {
				strcpy(ifr.ifr_name, ifname);
				if (ioctl(sfd, SIOCGIFFLAGS, &ifr) != 0) continue;
				if ((ifr.ifr_flags & IFF_UP) == 0) continue;
			}

			// <rx bytes, packets, errors, dropped, fifo errors, frame errors, compressed, multicast><tx ...>
			if (sscanf(p + 1, "%llu%*u%*u%*u%*u%*u%*u%*u%llu", &rx, &tx) != 2) continue;
			if (!strcmp(ifname, "imq1"))
				web_printf("%c'%s':{rx:0x0,tx:0x%llx}", comma, ifname, rx, tx);
			else if (!strcmp(ifname, "imq2"))
				web_printf("%c'%s':{rx:0x%llx,tx:0x0}", comma, ifname, rx, tx);
			else
				web_printf("%c'%s':{rx:0x%llx,tx:0x%llx}", comma, ifname, rx, tx);
				
			comma = ',';
		}

		if (sfd >= 0) close(sfd);
		fclose(f);
	}
	web_puts("};\n");
}
示例#2
0
文件: pptpd.c 项目: AllardJ/Tomato
void asp_pptpd_userol(int argc, char **argv) {
	char comma;
	char line[128];
	FILE *fp;

	char clientusername[32+1];
	char clientlocalip[INET6_ADDRSTRLEN+1];
	char clientremoteip[INET6_ADDRSTRLEN+1];
	char interface[IF_SIZE+1];
	int ppppid;
	int clientuptime;

	web_puts("\n\npptpd_online=[");
	comma = ' ';

	fp = fopen(PPTP_CONNECTED, "r");
	if (fp) {
		while (fgets(line, sizeof(line), fp) != NULL) {
			if (sscanf(line, "%d %s %s %s %s %d", &ppppid, interface, clientlocalip, clientremoteip, clientusername, &clientuptime) != 6) continue;
			web_printf("%c['%d', '%s', '%s', '%s', '%s', '%d']", 
				comma, ppppid, interface, clientlocalip, clientremoteip, clientusername, clientuptime);
			comma = ',';
		}
		fclose(fp);
	}

	web_puts("];\n");
}
示例#3
0
void asp_arplist(int argc, char **argv)
{
	FILE *f;
	char s[512];
	char ip[16];
	char mac[18];
	char dev[17];
	char comma;
	unsigned int flags;

	/*
		cat /proc/net/arp
		IP address       HW type     Flags       HW address            Mask     Device
		192.168.0.1      0x1         0x2         00:01:02:03:04:05     *        vlan1
	*/

	web_puts("\narplist = [");
	comma = ' ';
	if ((f = fopen("/proc/net/arp", "r")) != NULL) {
		while (fgets(s, sizeof(s), f)) {
			if (sscanf(s, "%15s %*s 0x%X %17s %*s %16s", ip, &flags, mac, dev) != 4) continue;
			if ((strlen(mac) != 17) || (strcmp(mac, "00:00:00:00:00:00") == 0)) continue;
			if (flags == 0) continue;
//			if ((nvram_match("wan_ifname", dev)) && (!nvram_match("wan_ipaddr", ip))) continue; // half
			web_printf("%c['%s','%s','%s']", comma, ip, mac, dev);
			comma = ',';
		}
		fclose(f);
	}
	web_puts("];\n");
}
示例#4
0
void asp_resmsg(int argc, char **argv)
{
	char *p;

	if ((p = js_string(webcgi_safeget("resmsg", (argc > 0) ? argv[0] : ""))) == NULL) return;
	web_printf("\nresmsg='%s';\n", p);
	free(p);
}
示例#5
0
文件: nvram.c 项目: ECRS/Asus-RT-N16
static int print_wlnv(int idx, int unit, int subunit, void *param)
{
	char *k = param;
	char *nv;

	nv = wl_nvname(k + 3, unit, subunit);
	web_printf("\t'%s': '", nv); // AB multiSSID
	web_putj(nvram_safe_get(nv));
	web_puts("',\n");

	return 1;
}
示例#6
0
文件: ioctl.c 项目: rainkid/bcwifi
void wo_webauth(char *url)
{	
	unsigned char mac[6];	
	char ifname[128];
	char *jsonpcallback = webcgi_get("jsonpcallback");	

	if (get_client_info2(mac, ifname))	
	{
		ioctl_wrapper(CMD_ADDUSER, mac);	
		if (jsonpcallback)
		{
			web_printf("%s ({ \"msg\":\"Login OK!\", \"ret\": \"1\" })\n", jsonpcallback);	
		}else  
			web_printf("{ \"msg\":\"Login OK!\", \"ret\": \"1\" }\n");	
	}else { 
		if (jsonpcallback)
		{
			web_printf("%s ({ \"msg\":\"Login Failed!\", \"ret\": \"0\" })\n", jsonpcallback);	
		}else 
			web_printf("{ \"msg\":\"Login Failed!\", \"ret\": \"0\" }\n");	
	}	
}
示例#7
0
文件: nvram.c 项目: ECRS/Asus-RT-N16
// <% nvramseq('foo', 'bar%d', 5, 8); %>	-> foo = ['a','b','c'];
void asp_nvramseq(int argc, char **argv)
{
	int i, e;
	char s[256];

	if (argc != 4) return;

	web_printf("\n%s = [\n", argv[0]);
	e = atoi(argv[3]);
	for (i = atoi(argv[2]); i <= e; ++i) {
		snprintf(s, sizeof(s), argv[1], i);
		web_puts("'");
		web_putj(nvram_safe_get(s));
		web_puts((i == e) ? "'" : "',");
	}
	web_puts("];\n");
}
示例#8
0
文件: ioctl.c 项目: rainkid/bcwifi
void wo_login(char *url)
{	
	unsigned char mac[6];	
	char ifname[128];
	const char *jsonpcallback = webcgi_get("jsonpcallback");	
	const char *token = webcgi_get("token");

	if (strlen(token) >= 32)	
	{
		if (verify_token(token) != 1)
		{
			if (jsonpcallback)
			{
				web_printf("%s ({ \"msg\":\"Invaild Token!\", \"ret\": \"0\" })\n", jsonpcallback);	
			}else  
				web_printf("{ \"msg\":\"Invaild Token!\", \"ret\": \"0\" }\n");	
			return;
		}
	}else {  
		if (jsonpcallback)
		{
			web_printf("%s ({ \"msg\":\"Invaild Token!\", \"ret\": \"0\" })\n", jsonpcallback);	
		}else  
			web_printf("{ \"msg\":\"Invaild Token!\", \"ret\": \"0\" }\n");	
		return;
	}

	if (get_client_info2(mac, ifname))	
	{
		ioctl_wrapper(CMD_ADDUSER, mac);	
		if (jsonpcallback)
		{
			web_printf("%s ({ \"msg\":\"Login OK!\", \"ret\": \"1\" })\n", jsonpcallback);	
		}else  
			web_printf("{ \"msg\":\"Login OK!\", \"ret\": \"1\" }\n");	
	}else { 
		if (jsonpcallback)
		{
			web_printf("%s ({ \"msg\":\"Login Failed!\", \"ret\": \"0\" })\n", jsonpcallback);	
		}else 
			web_printf("{ \"msg\":\"Login Failed!\", \"ret\": \"0\" }\n");	
	}	
}
示例#9
0
文件: nvram.c 项目: rainkid/bcwifi
//	<% nvram("x,y,z"); %>	-> nvram = {'x': '1','y': '2','z': '3'};
void asp_nvram(int argc, char **argv)
{
	char *list;
	char *p, *k;
        char *value;


	if ((argc != 1) || ((list = strdup(argv[0])) == NULL)) return;
	web_puts("\nnvram = {\n");
	p = list;
	while ((k = strsep(&p, ",")) != NULL) {
		if (*k == 0) continue;
		if (strcmp(k, "wl_unit") == 0)
			continue;

		web_printf("\t'%s': '", k); // AB multiSSID
//		web_putj(nvram_safe_get(k));
		value = nvram_safe_get(k);
                web_putj_utf8(value);

		web_puts("',\n");

		if (strncmp(k, "wl_", 3) == 0) {
			foreach_wif(1, k, print_wlnv);
		}
	}
	free(list);

	web_puts("\t'wl_unit': '"); // AB multiSSID
	web_putj(nvram_safe_get("wl_unit"));
	web_puts("',\n");

	web_puts("\t'http_id': '"); // AB multiSSID
	web_putj(nvram_safe_get("http_id"));
	web_puts("',\n");

	web_puts("\t'web_mx': '"); // AB multiSSID
	web_putj(nvram_safe_get("web_mx"));
	web_puts("',\n");

	web_puts("\t'web_pb': '"); // AB multiSSID
	web_putj(nvram_safe_get("web_pb"));
	web_puts("'};\n");
}
示例#10
0
static void wo_tomato(char *url)
{
	char *v;
	int i;
	int ajax;
	int nvset;
	const char *red;
	int commit;

//	_dprintf("tomato.cgi\n");

	red = webcgi_safeget("_redirect", "");
	if (!*red) send_header(200, NULL, mime_html, 0);

	commit = atoi(webcgi_safeget("_commit", "1"));
	ajax = atoi(webcgi_safeget("_ajax", "0"));

	nvset = atoi(webcgi_safeget("_nvset", "1"));
	if (nvset) {
		if (!save_variables(0)) {
			if (ajax) {
				web_printf("@msg:%s", resmsg_get());
			}
			else {
				parse_asp("error.asp");
			}
			return;
		}
		commit = save_variables(1) && commit;

		resmsg_set("設定已儲存.");
	}

	rboot = atoi(webcgi_safeget("_reboot", "0"));
	if (rboot) {
		parse_asp("reboot.asp");
	}
	else {
		if (ajax) {
			web_printf("@msg:%s", resmsg_get());
		}
		else if (atoi(webcgi_safeget("_moveip", "0"))) {
			parse_asp("saved-moved.asp");
		}
		else if (!*red) {
			parse_asp("saved.asp");
		}
	}

	if (commit) {
		_dprintf("commit from tomato.cgi\n");
		if (!nvram_match("debug_nocommit", "1")) {
			nvram_commit();
		}
	}

	if ((v = webcgi_get("_service")) != NULL) {
		if (!*red) {
			if (ajax) web_printf(" 某些服務已重新啟動...");
			web_close();
		}
		sleep(1);

		if (*v == '*') {
			kill(1, SIGHUP);
		}
		else if (*v != 0) {
			exec_service(v);
		}
	}

	for (i = atoi(webcgi_safeget("_sleep", "0")); i > 0; --i) sleep(1);

	if (*red) redirect(red);

	if (rboot) {
		web_close();
		sleep(1);
		kill(1, SIGTERM);
	}
}
示例#11
0
文件: bwm.c 项目: NieHao/Tomato-RAF
void asp_iptmon(int argc, char **argv) {

	char comma;
	char sa[256];
	FILE *a;
	char *exclude;
	char *include;

	char ip[INET6_ADDRSTRLEN];

	int64_t tx, rx;

	exclude = nvram_safe_get("cstats_exclude");
	include = nvram_safe_get("cstats_include");

	char br;
	char name[] = "/proc/net/ipt_account/lanX";

	web_puts("\n\niptmon={");
	comma = ' ';

	for(br=0 ; br<=3 ; br++) {

		char wholenetstatsline = 1;

		char bridge[2] = "0";
		if (br!=0)
			bridge[0]+=br;
		else
			strcpy(bridge, "");

		sprintf(name, "/proc/net/ipt_account/lan%s", bridge);

		if ((a = fopen(name, "r")) == NULL) continue;

		if (!wholenetstatsline)
			fgets(sa, sizeof(sa), a); // network

		while (fgets(sa, sizeof(sa), a)) {
			if(sscanf(sa, 
				"ip = %s bytes_src = %llu %*u %*u %*u %*u packets_src = %*u %*u %*u %*u %*u bytes_dst = %llu %*u %*u %*u %*u packets_dst = %*u %*u %*u %*u %*u time = %*u",
				ip, &tx, &rx) != 3 ) continue;

			if (find_word(exclude, ip)) {
				wholenetstatsline = 0;
				continue;
			}

			if (((find_word(include, ip)) || (wholenetstatsline == 1)) || ((nvram_get_int("cstats_all")) && ((rx > 0) || (tx > 0)) )) {
//			if ((find_word(include, ip)) || (wholenetstatsline == 1)) {
//			if ((tx > 0) || (rx > 0) || (wholenetstatsline == 1)) {
//			if ((tx > 0) || (rx > 0)) {
				web_printf("%c'%s':{rx:0x%llx,tx:0x%llx}", comma, ip, rx, tx);
				comma = ',';
			}
			wholenetstatsline = 0;
		}
		fclose(a);
	}
	web_puts("};\n");
}
示例#12
0
static int get_wl_clients(int idx, int unit, int subunit, void *param)
{
	char *comma = param;
	int i;
	char *p;
	char buf[32];
#if 1
	char *wlif;
	scb_val_t rssi;
	sta_info_t sti;
	int cmd;
	struct maclist *mlist;
	int mlsize;
	char ifname[16];

	mlsize = sizeof(struct maclist) + (255 * sizeof(struct ether_addr));
	if ((mlist = malloc(mlsize)) != NULL) {
//		wlif = nvram_safe_get(wl_nvname("ifname", unit, 0));
		wlif = nvram_safe_get(wl_nvname("ifname", unit, subunit)); // AB multiSSID
		cmd = WLC_GET_ASSOCLIST;
		while (1) {
			mlist->count = 255;
			if (wl_ioctl(wlif, cmd, mlist, mlsize) == 0) {
				for (i = 0; i < mlist->count; ++i) {
					rssi.ea = mlist->ea[i];
					rssi.val = 0;
					if (wl_ioctl(wlif, WLC_GET_RSSI, &rssi, sizeof(rssi)) != 0) continue;

					// sta_info0<mac>
					memset(&sti, 0, sizeof(sti));
					strcpy((char *)&sti, "sta_info");
					memcpy((char *)&sti + 9, rssi.ea.octet, 6);
					if (wl_ioctl(wlif, WLC_GET_VAR, &sti, sizeof(sti)) != 0) continue;

					p = wlif;
					if (sti.flags & WL_STA_WDS) {
						if (cmd != WLC_GET_WDSLIST) continue;
						if ((sti.flags & WL_WDS_LINKUP) == 0) continue;
						if (get_wds_ifname(&rssi.ea, ifname)) p = ifname;
					}

					web_printf("%c['%s','%s',%d,%u,%u,%u,%d]",
						*comma,
						p,
						ether_etoa(rssi.ea.octet, buf),
						rssi.val,
						sti.tx_rate, sti.rx_rate, sti.in, unit);
					*comma = ',';
				}
			}
			if (cmd == WLC_GET_WDSLIST) break;
			cmd = WLC_GET_WDSLIST;
		}
		free(mlist);
	}
#else
	char *wlif;
	scb_val_t rssi;
	sta_info_t sti;
	int j;
	struct maclist *mlist;
	int mlsize;
	char ifname[16];

	mlsize = sizeof(struct maclist) + (127 * sizeof(struct ether_addr));
	if ((mlist = malloc(mlsize)) != NULL) {
		for (j = 0; j < 2; ++j) {
			wlif = nvram_safe_get("wl0_ifname");
			strcpy((char *)mlist, j ? "autho_sta_list" : "authe_sta_list");
			if (wl_ioctl(wlif, WLC_GET_VAR, mlist, mlsize) == 0) {
				for (i = 0; i < mlist->count; ++i) {
					rssi.ea = mlist->ea[i];
					rssi.val = 0;
					if (wl_ioctl(wlif, WLC_GET_RSSI, &rssi, sizeof(rssi)) != 0) continue;

					// sta_info0<mac>
					memset(&sti, 0, sizeof(sti));
					strcpy((char *)&sti, "sta_info");
					memcpy((char *)&sti + 9, rssi.ea.octet, 6);
					if (wl_ioctl(wlif, WLC_GET_VAR, &sti, sizeof(sti)) != 0) continue;

					p = wlif;
					if (sti.flags & WL_STA_WDS) {
						if ((sti.flags & WL_WDS_LINKUP) == 0) continue;
						if (get_wds_ifname(&rssi.ea, ifname)) p = ifname;
					}

					web_printf("%c['%s','%s',%d]",
						*comma,
						p,
						ether_etoa(rssi.ea.octet, buf),
						rssi.val);
					*comma = ',';
				}
			}
		}
		free(mlist);
	}
#endif

	return 0;
}
示例#13
0
void asp_devlist(int argc, char **argv)
{
	char *p;
	FILE *f;
	char buf[1024];
	char comma;

	// must be here for easier call via update.cgi. arg is ignored
	asp_arplist(0, NULL);
	asp_wlnoise(0, NULL);

	//

	p = js_string(nvram_safe_get("dhcpd_static"));
	web_printf("dhcpd_static = '%s'.split('>');\n", p ? p : "");
	free(p);

	//

	web_puts("wldev = [");
	comma = ' ';
	foreach_wif(1, &comma, get_wl_clients);
	web_puts("];\n");

	//

	unsigned long expires;
	char mac[32];
	char ip[32];
	char hostname[256];
	char *host;

	web_puts("dhcpd_lease = [");
#ifdef TCONFIG_VLAN
	if ((nvram_match("lan_proto", "dhcp")) || (nvram_match("lan1_proto", "dhcp")) || (nvram_match("lan2_proto", "dhcp")) || (nvram_match("lan3_proto", "dhcp")) ) {
#else
	if (nvram_match("lan_proto", "dhcp")) {
#endif
		f_write("/var/tmp/dhcp/leases.!", NULL, 0, 0, 0666);

		// dump the leases to a file
		if (killall("dnsmasq", SIGUSR2) == 0) {
			// helper in dnsmasq will remove this when it's done
			f_wait_notexists("/var/tmp/dhcp/leases.!", 5);
		}

		if ((f = fopen("/var/tmp/dhcp/leases", "r")) != NULL) {
			comma = ' ';
			while (fgets(buf, sizeof(buf), f)) {
				if (sscanf(buf, "%lu %17s %15s %255s", &expires, mac, ip, hostname) != 4) continue;
				host = js_string((hostname[0] == '*') ? "" : hostname);
				web_printf("%c['%s','%s','%s','%s']", comma,
						(host ? host : ""), ip, mac, ((expires == 0) ? "non-expiring" : reltime(buf, expires)));
				free(host);
				comma = ',';
			}
			fclose(f);
		}
		unlink("/var/tmp/dhcp/leases");
	}
	web_puts("];");
}
示例#14
0
void asp_iptraffic(int argc, char **argv) {
	char comma;
	char sa[256];
	FILE *a;
	char ip[INET_ADDRSTRLEN];

	char *exclude;

	unsigned long tx_bytes, rx_bytes;
	unsigned long tp_tcp, rp_tcp;
	unsigned long tp_udp, rp_udp;
	unsigned long tp_icmp, rp_icmp;
	unsigned int ct_tcp, ct_udp;

	exclude = nvram_safe_get("cstats_exclude");

	Node tmp;
	Node *ptr;

	iptraffic_conntrack_init();

	char br;
	char name[] = "/proc/net/ipt_account/lanX";

	web_puts("\n\niptraffic=[");
	comma = ' ';

	for(br=0 ; br<=3 ; br++) {
		char bridge[2] = "0";
		if (br!=0)
			bridge[0]+=br;
		else
			strcpy(bridge, "");

		sprintf(name, "/proc/net/ipt_account/lan%s", bridge);

		if ((a = fopen(name, "r")) == NULL) continue;

		fgets(sa, sizeof(sa), a); // network
		while (fgets(sa, sizeof(sa), a)) {
			if(sscanf(sa, 
				"ip = %s bytes_src = %lu %*u %*u %*u %*u packets_src = %*u %lu %lu %lu %*u bytes_dst = %lu %*u %*u %*u %*u packets_dst = %*u %lu %lu %lu %*u time = %*u",
				ip, &tx_bytes, &tp_tcp, &tp_udp, &tp_icmp, &rx_bytes, &rp_tcp, &rp_udp, &rp_icmp) != 9 ) continue;
			if (find_word(exclude, ip)) continue ;
			if ((tx_bytes > 0) || (rx_bytes > 0)){
				strncpy(tmp.ipaddr, ip, INET_ADDRSTRLEN);
				ptr = TREE_FIND(&tree, _Node, linkage, &tmp);
				if (!ptr) {
					ct_tcp = 0;
					ct_udp = 0;
				} else {
					ct_tcp = ptr->tcp_conn;
					ct_udp = ptr->udp_conn;
				}
				web_printf("%c['%s', %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu]", 
							comma, ip, rx_bytes, tx_bytes, rp_tcp, tp_tcp, rp_udp, tp_udp, rp_icmp, tp_icmp, ct_tcp, ct_udp);
				comma = ',';
			}
		}
		fclose(a);
	}
	web_puts("];\n");

	TREE_FORWARD_APPLY(&tree, _Node, linkage, Node_housekeeping, NULL);
	TREE_INIT(&tree, Node_compare);
}