Example #1
0
static void heartbeat(int fd, short evtype, void *arg)
{
	static struct timeval now, diff, tv = {0, 0};
	struct mg *mg = arg;

	/* garbage collector - free whenever garbage > 128KB */
	if (mmatic_size(mg->mmtmp) > 1024 * 128) {
		mmatic_free(mg->mmtmp);
		mg->mmtmp = mmatic_create();
	}

	/* if no line generator is running and there was no packet to us in last 60 seconds - exit */
	if (mg->running == 0) {
		gettimeofday(&now, NULL);
		timersub(&now, &mg->last, &diff);

		if (diff.tv_sec >= 60) {
			dbg(0, "Finished, exiting...\n");
			mg->running--;
			event_base_loopexit(mg->evb, &tv);
			return;
		}
	}

	mgs_uschedule(&mg->hbs, HEARTBEAT_PERIOD);
}
Example #2
0
int main(int argc, char *argv[])
{
	mmatic *mm;
	struct flowcalc *fc;
	void *h;
	struct module *mod;
	char *name, *s;
	tlist *ls;
	void *pdata;

	/*
	 * initialization
	 */
	mm = mmatic_create();
	fc = mmatic_zalloc(mm, sizeof *fc);
	fc->mm = mm;
	fc->modules = tlist_create(NULL, mm);

	/* read options */
	if (parse_argv(fc, argc, argv))
		return 1;

	/* enable all modules found in given directory */
	if (tlist_count(fc->modules) == 0) {
		ls = pjf_ls(fc->dir, mm);
		tlist_iter_loop(ls, name) {
			s = strrchr(name, '.');
			if (s && streq(s, ".so")) {
				*s = 0;
				tlist_push(fc->modules, name);
			}
		}
Example #3
0
bool init(struct lfc *lfc, void **pdata)
{
    mmatic *mm;
    struct ndpi *ndpi;
    IPOQUE_PROTOCOL_BITMASK all;

    mm = mmatic_create();
    ndpi = mmatic_zalloc(mm, sizeof *ndpi);
    ndpi->mm = mm;
    ndpi->ids = thash_create_intkey(NULL, mm); // TODO: null ffn?

    ndpi->ipq = ipoque_init_detection_module(1000, ma, db); // TODO: 1000?
    if (!ndpi->ipq) {
        dbg(0, "ipoque_init_detection_module() failed\n");
        return false;
    }

    IPOQUE_BITMASK_SET_ALL(all);
    ipoque_set_protocol_detection_bitmask2(ndpi->ipq, &all);

    *pdata = ndpi;

    printf("%%%% ndpi 0.1 - nDPI\n");
    printf("@attribute ndpi_proto string\n");

    return true;
}
Example #4
0
struct spi_ep *ep_new_pkt(struct spi_source *source, spi_epaddr_t epa,
	const struct timeval *ts, void *data, uint32_t size)
{
	struct spi *spi = source->spi;
	struct spi_ep *ep;
	char *key;
	struct spi_pkt *pkt;
	mmatic *mm;

	key = _k(source, epa);
	ep = thash_get(spi->eps, key);
	if (!ep) {
		mm = mmatic_create();
		ep = mmatic_zalloc(mm, sizeof *ep);
		ep->mm = mm;
		ep->source = source;
		ep->epa = epa;
		thash_set(spi->eps, key, ep);

		source->eps++;

		dbg(8, "new ep %s\n", spi_epa2a(epa));
	}

	/* make packet */
	pkt = mmatic_zalloc(ep->mm, sizeof *pkt);
	pkt->size = size;
	pkt->payload = mmatic_zalloc(ep->mm, spi->options.N);
	memcpy(pkt->payload, data, spi->options.N);
	memcpy(&pkt->ts, ts, sizeof(struct timeval));

	/* update last packet time */
	memcpy(&ep->last, ts, sizeof(struct timeval));

	/* store packet */
	if (!ep->pkts)
		ep->pkts = tlist_create(mmatic_free, ep->mm);

	tlist_push(ep->pkts, pkt);

	/* generate event if pkts big enough */
	if (ep->gclock1 == 0 && tlist_count(ep->pkts) >= spi->options.C) {
		ep->gclock1++;
		spi_announce(spi, "endpointPacketsReady", 0, ep, false);
		dbg(7, "ep %s ready\n", spi_epa2a(epa));
	}

	return ep;
}
Example #5
0
int main(int argc, char *argv[])
{
	mmatic *mm;

	/*
	 * initialization
	 */
	mm = mmatic_create();
	fd = mmatic_zalloc(mm, sizeof *fd);
	fd->mm = mm;
	fd->cache = thash_create_intkey(mmatic_free, mm);
	fd->out_files = thash_create_strkey(trace_destroy_output, mm);

	/* catch SIGINT */
	signal(SIGINT, sigint);

	/* read options */
	if (parse_argv(argc, argv))
		return 1;

	/* file-system init */
	{
		if (streq(fd->arff_file, "-")) {
			fd->afh = stdin;
		} else {
			fd->afh = fopen(fd->arff_file, "r");
			if (!fd->afh) {
				cleanup();
				die("Reading input ARFF file '%s' failed: %s\n", fd->arff_file, strerror(errno));
			}
		}

		if (pjf_mkdir(fd->dir) != 0) {
			cleanup();
			die("Creating output directory '%s' failed\n", fd->dir);
		}
	}

	fd->lfc = lfc_init();
	lfc_register(fd->lfc, "flowdump", sizeof(struct flow), pkt, NULL, fd);

	if (!lfc_run(fd->lfc, fd->pcap_file, fd->filter)) {
		cleanup();
		die("Reading file '%s' failed\n", fd->pcap_file);
	}

	cleanup();
	return 0;
}
Example #6
0
__USE_LIBASN

int main(int argc, char *argv[])
{
	char buf[BUFSIZ];
	mmatic *mm = mmatic_create();
	xstr *xs = xstr_create("", mm);
	json *js = json_create(mm);

	while (fgets(buf, BUFSIZ, stdin))
		xstr_append(xs, buf);

	ut *parsed = json_parse(js, xstr_string(xs));
	if (ut_ok(parsed))
		printf("%s", json_print(js, parsed));
	else
		printf("%s\n", ut_err(parsed));

	return 0;
}
Example #7
0
int main(int argc, char *argv[])
{
	mmatic *mm = mmatic_create();
	mmatic *mmtmp = mmatic_create();
	struct mg *mg;
	int i;

	/*
	 * initialize and parse config
	 */

	mg = mmatic_zalloc(mm, sizeof(struct mg));
	mg->mm = mm;
	mg->mmtmp = mmtmp;
	apply_defaults(mg);

	/* get my id number from hostname */
	fetch_myid(mg);

	/* parse command line options */
	if (parse_argv(mg, argc, argv))
		return 1;

	/* parse configuration file options */
	if (mg->options.conf_file) {
		if (parse_config(mg))
			return 4;
	}

	/*
	 * config syntax looks OK, see if it is feasible
	 */

	/* initialize random number generator */
	srand48(mg->options.myid);

	/* init libevent */
	mg->evb = event_init();
	event_set_log_callback(libevent_log);

	/* init stats structures so mgstats_aggregator_add() used somewhere below works */
	mgstats_init(mg);

	/* attach to raw interfaces */
	if (mgi_init(mg, handle_packet) <= 0) {
		dbg(0, "no available interfaces found\n");
		return 2;
	}

	/* parse traffic file */
	if (parse_traffic(mg))
		return 3;

	/*
	 * all OK, prepare to start
	 */

	/* synchronize time reference point on all nodes */
	mgc_sync(mg);

	/* schedule stats writing */
	mgstats_start(mg);

	/* attach global stats */
	_stats_init(mg);

	/* schedule heartbeat and disk sync signals */
	heartbeat_init(mg);
	sync_init(mg);

	/* schedule the real work of this node: line generators */
	for (i = 1; i < TRAFFIC_LINE_MAX; i++) {
		if (!(mg->lines[i] && mg->lines[i]->my))
			continue;

		/* this will schedule first execution */
		mgs_sleep(mg->lines[i], NULL);
		mg->running++;
	}

	/* suppose last frame was received now */
	gettimeofday(&mg->last, NULL);

	/*
	 * start!
	 */

	dbg(0, "Starting\n");
	event_base_dispatch(mg->evb);

	/*******************************/

	/*
	 * cleanup after end of libevent loop
	 */

	event_base_free(mg->evb);
	mmatic_free(mg->mm);
	mmatic_free(mg->mmtmp);

	fflush(NULL);
	sync();

	return 0;
}