Пример #1
0
int main (int argc, char **argv)
{
	/* data aq. */
	double backup_in, backup_out;
	double cur_in, cur_out;
	char dim_in[4], dim_out[4];

	/* getopt */
	int opt;
	extern char *optarg;
	int mode = OPT_NONE;
	int sampletime = SAMPLE_TIME;

	bzero(ifdata.if_name, sizeof(ifdata.if_name));

	/* getopt () */
	while ((opt = getopt(argc, argv, "d:hi:o:")) != EOF)
	{
		switch (opt)
		{
			case 'd':
				/* delay aka sample time */
				if (atoi(optarg) > 0) sampletime = atoi(optarg);
				else
				{
					fprintf(stderr, "dude, delay must be positive!\n");
					usage(argv[0], 1);
				}
				break;
			case 'i':
				/* interface */
				snprintf(ifdata.if_name, 9, "%s", optarg); // memcpy
				break;
			case 'h':
				/* usage */
				usage(argv[0], 0);
				break;
			case 'o':
				/* possible output options */
				mode |= getopmode((char *) optarg);
#ifdef CUSTOM_MODE
				/* check arg if running in custom mode */
				if (mode == OPT_CUSTOM)
				{
					/* when running in custom mode the last argument
					 * should be a string containing two %% spaceholders
					 * where incoming and outgoing traffic should
					 * be inserted
					 */
					p = argv[argc-1];
					if (((p = strstr(p, "%%%")) == NULL) ||
					    ((p = strstr(++p, "%%%")) == NULL))
						usage(argv[0], 1);
					/* is there a third argument? */
					if (strstr(++p, "%%%") != NULL)
						usage(argv[0], 1);
				}
#endif
				break;
			default:
				usage(argv[0], 1);
		}
	}
	/* sanity checks */
	if (ifdata.if_name[0] == '\0') /* no interface given */
		usage(argv[0], 1);
	if (checkinterface() != TRUE)
	{
		fprintf(stderr, "invalid interface %s\n", (char *) ifdata.if_name);
		return 1;
	}
	if (mode == OPT_NONE)
	{

		/* init in a sane state */
		get_stat();
		backup_in  = stats.rx_bytes;
		backup_out = stats.tx_bytes;
		bzero(dim_in, sizeof(dim_in));
		bzero(dim_out, sizeof(dim_out));

		/* main loop */
		for (;;)
		{
			/* update */
			get_stat();
			if (backup_in > stats.rx_bytes)
				cur_in = (int) stats.rx_bytes / sampletime;
			else
				cur_in = (int) (stats.rx_bytes - backup_in) / sampletime;
			if (backup_out > stats.tx_bytes)
				cur_out = (int) stats.tx_bytes / sampletime;
			else
				cur_out = (int) (stats.tx_bytes - backup_out) / sampletime;

			/* determine traffic dimension and recalc value
		 	 * currently MB/s, KB/s and B/s are supported
		 	 */
			if (cur_in > 1024*1024)
			{
				cur_in = cur_in / (1024*1024); snprintf(dim_in, 22, "MB");
			}
			else if (cur_in > 1024)
			{
				cur_in = cur_in / 1024; snprintf(dim_in, 2, "KB");
			}
			else
				snprintf(dim_in, 2, "B");

			if (cur_out > 1024*1024)
			{
				cur_out = cur_out / (1024*1024); snprintf(dim_out, 2, "MB");
			}
			else if (cur_out > 1024)
			{
				cur_out = cur_out / 1024; snprintf(dim_out, 2, "KB");
			}
			else
				snprintf(dim_out, 2, "B");

			/* print to screen */
			printf("in %lu %s/s out %lu %s/s\n",
					(unsigned long) cur_in, dim_in,
					(unsigned long) cur_out, dim_out);
			fflush(stdout);

			/* save 'new old' values */
			backup_in = stats.rx_bytes;
			backup_out = stats.tx_bytes;

			/* sleep */	
			sleep(sampletime);
		}
	}
	else
	{
		/* non-screen modes */
		get_stat();
		backup_in  = stats.rx_bytes;
		backup_out = stats.tx_bytes;
		bzero(dim_in, sizeof(dim_in));
		bzero(dim_out, sizeof(dim_out));
		sleep(sampletime);
		get_stat();

		if (backup_in > stats.rx_bytes)
			cur_in = (int) stats.rx_bytes / sampletime;
		else
			cur_in = (int) (stats.rx_bytes - backup_in) / sampletime;
		if (backup_out > stats.tx_bytes)
			cur_out = (int) stats.tx_bytes / sampletime;
		else
			cur_out = (int) (stats.tx_bytes - backup_out) / sampletime;
	
		/*	
		 * mode dependent output
		 */

		switch (mode)
		{
			case OPT_MRTG:
			case OPT_FULLMRTG:	/* for now */
				printf("%lu\n%lu\n",
					(unsigned long) cur_in, (unsigned long) cur_out);
				break;
#ifdef CUSTOM_MODE
			case OPT_CUSTOM:
				/* substitute %%% with %lu to insert values */
				p = argv[argc-1];
				p = strstr(p, "%%%");
				memcpy(p, "%lu", 3);
				p = strstr(++p, "%%%");
				memcpy(p, "%lu",3);
				printf(argv[argc-1],
					(unsigned long) cur_in, (unsigned long) cur_out);
				break;
#endif
			default:
				fprintf(stderr, "internal error in output. please report!\n");
		}
	}
	return 0;
}
Пример #2
0
int
main(int argc, char *argv[])
{
	const char *ifname, *cmd;
	unsigned char bnounit = 0;
	char *if_base = NULL;
	unsigned int unit_res = -1;
	int res = 0;

	if (argc < 2 ||
	  strncmp(argv[1],"-h",2) == 0 || strncmp(argv[1],"--h",3) == 0)
		usage();

	ifname = argv[1];

	if (argc == 2) {
		ieee80211_status(ifname);
		return 0;
	}

	cmd = argv[2];
	if (streq(cmd, "create")) {
		struct ieee80211_clone_params cp;
		struct ifreq ifr;

		memset(&ifr, 0, sizeof(ifr));

		memset(&cp, 0, sizeof(cp));
		strncpy(cp.icp_name, ifname, IFNAMSIZ);
		/* NB: station mode is the default */
		cp.icp_opmode = IEEE80211_M_STA;
		/* NB: default is to request a unique bssid/mac */
		cp.icp_flags = IEEE80211_CLONE_BSSID;

		while (argc > 3) {
			if (strcmp(argv[3], "wlanmode") == 0) {
				if (argc < 5)
					usage();
				cp.icp_opmode = (u_int16_t) getopmode(argv[4]);
				argc--;
				argv++;
			} else if (strcmp(argv[3], "wlandev") == 0) {
				if (argc < 5)
					usage();
				strncpy(ifr.ifr_name, argv[4], IFNAMSIZ);
				argc--;
				argv++;
			} else if (strcmp(argv[3], "nounit" ) == 0) {
				bnounit = 1;
			} else {
				int flag = getflag(argv[3]);
				if (flag < 0)
					cp.icp_flags &= ~(-flag);
				else
					cp.icp_flags |= flag;
			}
			argc--;
			argv++;
		}
		if (ifr.ifr_name[0] == '\0')
			errx(1, "no device specified with wlandev");

		res = if_split_name(cp.icp_name, &if_base, &unit_res);
		if (res < 0) {
			err(1, "if_split_name() - malloc");
		} else if ((res == 0) && (bnounit == 0)) {
			/* user gave a string only and using a unit */
			unit_res = if_find_unit(if_base);

			if (unit_res < 0) {
				err(1, "if_find_unit - failed");
			} else {
				snprintf(cp.icp_name + strlen(if_base),
					IFNAMSIZ - strlen(if_base), "%d", unit_res);
			}
		}

		free(if_base);
		if_base = NULL;

		ifr.ifr_data = (void *) &cp;
		vap_create(&ifr);
		printf("%s\n", ifr.ifr_name);
	} else if (streq(cmd, "destroy")) {
		vap_destroy(ifname);
	} else if (streq(cmd, "list")) {
		if (argc > 3) {
			const char *arg = argv[3];

			if (streq(arg, "sta"))
				list_stations(ifname);
			else if (streq(arg, "scan") || streq(arg, "ap"))
				list_scan(ifname);
			else if (streq(arg, "chan") || streq(arg, "freq"))
				list_channels(ifname, 1);
			else if (streq(arg, "active"))
				list_channels(ifname, 0);
			else if (streq(arg, "keys"))
				list_keys(ifname);
			else if (streq(arg, "caps"))
				list_capabilities(ifname);
			else if (streq(arg, "wme"))
				list_wme(ifname);
			else
				err(1, "unknown 'list' option: %s", arg);
		} else				/* NB: for compatibility */
			list_stations(ifname);
	} else
		usage();

	return 0;
}