Example #1
0
/** Parses arguments and loads modules
 * @retval 0     ok
 * @retval 1     error, main() should exit (eg. wrong arg. given)
 * @retval 2     ok, but main() should exit (eg. on --version or --help) */
static int parse_argv(struct mg *mg, int argc, char *argv[])
{
	int i, c;

	static char *short_opts = "hvdV";
	static struct option long_opts[] = {
		/* name, has_arg, NULL, short_ch */
		{ "verbose",    0, NULL,  1  },
		{ "debug",      1, NULL,  2  },
		{ "help",       0, NULL,  3  },
		{ "version",    0, NULL,  4  },
		{ "id",         1, NULL,  5  },
		{ "root",       1, NULL,  6  },
		{ "sess",       1, NULL,  7  },
		{ "world",      0, NULL,  8  },
		{ 0, 0, 0, 0 }
	};

	for (;;) {
		c = getopt_long(argc, argv, short_opts, long_opts, &i);
		if (c == -1) break; /* end of options */

		switch (c) {
			case 'V':
			case  1 : debug = 5; break;
			case  2 : debug = atoi(optarg); break;
			case 'h':
			case  3 : help(); return 2;
			case 'v':
			case  4 : version(); return 2;
			case  5 : mg->options.myid = atoi(optarg); break;
			case  6 : mg->options.stats_root = mmatic_strdup(mg->mm, optarg); break;
			case  7 : mg->options.stats_sess = mmatic_strdup(mg->mm, optarg); break;
			case  8 : mg->options.world = true; break;
			default: help(); return 1;
		}
	}

	/* get traffic file path */
	if (argc - optind > 0) {
		mg->options.traf_file = argv[optind++];
	} else {
		help();
		return 1;
	}

	/* get configuration file path */
	if (argc - optind > 0)
		mg->options.conf_file = argv[optind++];

	return 0;
}
Example #2
0
/** Parses arguments and loads modules
 * @retval 0     ok
 * @retval 1     error, main() should exit (eg. wrong arg. given)
 * @retval 2     ok, but main() should exit (eg. on --version or --help) */
static int parse_argv(int argc, char *argv[])
{
	int i, c;

	static char *short_opts = "hvVf:d:c:s:";
	static struct option long_opts[] = {
		/* name, has_arg, NULL, short_ch */
		{ "verbose",    0, NULL,  1  },
		{ "debug",      1, NULL,  2  },
		{ "help",       0, NULL,  3  },
		{ "version",    0, NULL,  4  },
		{ 0, 0, 0, 0 }
	};

	/* defaults */
	debug = 0;
	fd->dir = "./flowdump";
	fd->colnum = 1;

	for (;;) {
		c = getopt_long(argc, argv, short_opts, long_opts, &i);
		if (c == -1) break; /* end of options */

		switch (c) {
			case 'V':
			case  1 : debug = 5; break;
			case  2 : debug = atoi(optarg); break;
			case 'h':
			case  3 : help(); return 2;
			case 'v':
			case  4 : version(); return 2;
			case 'f': fd->filter = mmatic_strdup(fd->mm, optarg); break;
			case 'd': fd->dir = mmatic_strdup(fd->mm, optarg); break;
			case 'c': fd->colnum = atoi(optarg); break;
			case 's': fd->value = mmatic_strdup(fd->mm, optarg); break;
			default: help(); return 1;
		}
	}

	if (argc - optind > 1) {
		fd->pcap_file = mmatic_strdup(fd->mm, argv[optind]);
		fd->arff_file = mmatic_strdup(fd->mm, argv[optind+1]);
	} else {
		help();
		return 1;
	}

	return 0;
}
Example #3
0
static void cache_update()
{
	char buf[BUFSIZ], *ptr, *cm;
	int i;
	unsigned int id;

	if (!fd->afh)
		return;

	if (thash_count(fd->cache) > 50000000)
		return;

	while (thash_count(fd->cache) < 10000000 && fgets(buf, sizeof buf, fd->afh)) {
		if (!isdigit(buf[0]))
			continue;

		ptr = buf;
		cm = strchr(ptr, ',');
		if (!cm)
			continue;
		else
			*cm = '\0';

		/* get flow id */
		id = atoi(ptr);

		/* get flow target */
		for (i = 1; i < fd->colnum; i++) {
			ptr = cm + 1;
			cm = strchr(ptr, ',');
			if (!cm) {
				cm = strchr(ptr, '\n');
				break;
			}
		}

		if (cm)
			*cm = '\0';

		for (i = 0; ptr[i]; i++) {
			if (!isalnum(ptr[i]))
				ptr[i] = '_';
		}

		thash_uint_set(fd->cache, id, mmatic_strdup(fd->mm, ptr));
	}

	if (feof(fd->afh)) {
		fclose(fd->afh);
		fd->afh = NULL;
	}
}
Example #4
0
static bool handle(struct req *req)
{
	time_t t;
	struct tm *tmp;
	char buf[200];

	t = time(NULL);
	tmp = localtime(&t);
	strftime(buf, sizeof(buf), "%F %T", tmp);

	uth_set_char(req->reply, "date", mmatic_strdup(buf, req));
	uth_set_char(req->reply, "config variable", uth_char(req->mod->cfg, "variable"));

	return true;
}
Example #5
0
/** Parse traffic file
 * @retval 0 success
 * @retval 1 syntax error
 * @retval 2 logic error
 */
static int parse_traffic(struct mg *mg)
{
	FILE *fp;
	const char *file;
	char buf[BUFSIZ];
	uint32_t line_num = 0;
	struct line *line;
	int i, rc;
	char *rest, *errmsg;
	struct mgp_line *pl;

	file = mg->options.traf_file;
	fp = fopen(file, "r");
	if (!fp) {
		dbg(0, "could not open traffic file: %s: %s\n", file, strerror(errno));
		return 1;
	}

	while (fgets(buf, sizeof buf, fp)) {
		line_num++;
		if (line_num >= N(mg->lines))
			die("Too many lines in the traffic file");

		/* skip comments */
		if (buf[0] == '#' || buf[0] == '\r' || buf[0] == '\n')
			continue;

		/* parse line */
		pl = mgp_parse_line(mg->mm, buf, 8, &rest, &errmsg,
			"s", "ms", "iface", "src", "dst", "rate", "noack", "cmd", NULL);
		if (!pl) {
			dbg(0, "%s: line %d: parse error: %s\n", file, line_num, errmsg);
			return 1;
		}

		/* ...and rewrite into struct line */
		line = mmatic_zalloc(mg->mm, sizeof *line);
		mg->lines[line_num] = line;

		line->mg = mg;
		line->line_num = line_num;
		line->contents = mmatic_strdup(mg->mm, buf);
		line->stats = stats_create(mg->mm);

		/* time */
		line->tv.tv_sec = mgp_get_int(pl, "s", 0);
		line->tv.tv_usec = mgp_get_int(pl, "ms", 0) * 1000;

		/* interface */
		i = mgp_get_int(pl, "iface", 0);
		if (i >= IFINDEX_MAX) {
			dbg(0, "%s: line %d: too big interface number: %d\n", file, line_num, i);
			return 1;
		}
		line->interface = &mg->interface[i];
		if (line->interface->fd <= 0) {
			dbg(0, "%s: line %d: interface not opened: %d\n", file, line_num, i);
			return 2;
		}

		/* src/dst */
		line->srcid = mgp_get_int(pl, "src", 1);
		line->dstid = mgp_get_int(pl, "dst", 1);
		line->my    = (line->srcid == mg->options.myid);

		/* rate/noack */
		line->rate = mgp_get_float(pl, "rate", 0) * 2.0;  /* driver uses "half-rates"; NB: "auto" => 0 */
		line->noack = mgp_get_int(pl, "noack", 0);

		/*
		 * command
		 */
		line->cmd = mgp_get_string(pl, "cmd", "");
		if (!line->cmd) {
			dbg(0, "%s: line %d: no line command\n", file, line_num);
			return 2;
		}

		/* find command handlers */
		if (!find_line_cmd(line)) {
			dbg(0, "%s: line %d: invalid command: %s\n", file, line_num, line->cmd);
			return 2;
		}

		/* initialize scheduler of outgoing frames */
		mgs_setup(&line->schedule, mg, line->cmd_timeout, line);

		/* call command initializer */
		rc = line->cmd_init(line, rest);
		if (rc != 0)
			return rc;
	}

	fclose(fp);
	return 0;
}
Example #6
0
/** Parses arguments and loads modules
 * @retval 0     ok
 * @retval 1     error, main() should exit (eg. wrong arg. given)
 * @retval 2     ok, but main() should exit (eg. on --version or --help) */
static int parse_argv(struct flowcalc *fc, int argc, char *argv[])
{
	int i, c;
	char *d, *s;

	static char *short_opts = "hvVf:r:d:e:an:t:l";
	static struct option long_opts[] = {
		/* name, has_arg, NULL, short_ch */
		{ "verbose",    0, NULL,  1  },
		{ "debug",      1, NULL,  2  },
		{ "help",       0, NULL,  3  },
		{ "version",    0, NULL,  4  },
		{ 0, 0, 0, 0 }
	};

	/* defaults */
	debug = 0;
	fc->dir = MYDIR;

	for (;;) {
		c = getopt_long(argc, argv, short_opts, long_opts, &i);
		if (c == -1) break; /* end of options */

		switch (c) {
			case 'V':
			case  1 : debug = 5; break;
			case  2 : debug = atoi(optarg); break;
			case 'h':
			case  3 : help(); return 2;
			case 'v':
			case  4 : version(); return 2;
			case 'f': fc->filter = mmatic_strdup(fc->mm, optarg); break;
			case 'r': fc->relation = mmatic_strdup(fc->mm, optarg); break;
			case 'd': fc->dir = mmatic_strdup(fc->mm, optarg); break;
			case 'e':
				s = mmatic_strdup(fc->mm, optarg);
				while ((d = strchr(s, ','))) {
					*d = 0;
					tlist_push(fc->modules, s);
					s = d + 1;
				}
				tlist_push(fc->modules, s);
				break;
			case 'l': fc->list = true; break;
			case 'a': fc->any = true; break;
			case 'n': fc->n = strtoul(optarg, NULL, 10); break;
			case 't': fc->t = strtod(optarg, NULL); break;
			default: help(); return 1;
		}
	}

	if (fc->list) {
		tlist_flush(fc->modules);
		return 0;
	}

	if (argc - optind > 0) {
		fc->file = mmatic_strdup(fc->mm, argv[optind]);
	} else {
		help();
		return 1;
	}

	return 0;
}