Beispiel #1
0
static void
tcpeek_listener_stat_json(int soc, const char *method) {
	struct tcpeek_filter *filter;
	struct tcpeek_stat *stat;
	char success[128], failure[128], buf[512];
	int isrefresh = 0, isfirst = 1;

	pthread_mutex_lock(&g.session.mutex);
	send(soc, "[", 1, 0);
	lnklist_iter_init(g.filter);
	while(lnklist_iter_hasnext(g.filter)) {
		filter = lnklist_iter_next(g.filter);
		if(!filter->stat) {
			continue;
		}
		if(strisequal(method, "REFRESH")) {
			isrefresh = 1;
		}
		stat = filter->stat;
		snprintf(success, sizeof(success), "{\"total\":%u,\"dupsyn\":%u,\"dupsynack\":%u}",
			stat[0].success.total - (isrefresh ? stat[1].success.total : 0),
			stat[0].success.dupsyn - (isrefresh ? stat[1].success.dupsyn : 0),
			stat[0].success.dupsynack - (isrefresh ? stat[1].success.dupsynack : 0)
		);
		if(strisequal(method, "REFRESH")) {
			snprintf(failure, sizeof(failure), "{\"total\":%u,\"timeout\":%u,\"reject\":%u}",
				stat[0].failure.total - (isrefresh ? stat[1].failure.total : 0),
				stat[0].failure.timeout - (isrefresh ? stat[1].failure.timeout : 0),
				(stat[0].failure.reject - (isrefresh ? stat[1].failure.reject : 0)) + (stat[0].failure.unreach - (isrefresh ? stat[1].failure.unreach : 0))
			);
		}
		else {
			snprintf(failure, sizeof(failure), "{\"total\":%u,\"timeout\":%u,\"reject\":%u, \"unreach\":%u}",
				stat[0].failure.total - (isrefresh ? stat[1].failure.total : 0),
				stat[0].failure.timeout - (isrefresh ? stat[1].failure.timeout : 0),
				stat[0].failure.reject - (isrefresh ? stat[1].failure.reject : 0),
				stat[0].failure.unreach - (isrefresh ? stat[1].failure.unreach : 0)
			);
		}
		snprintf(buf, sizeof(buf), "%s{\"%s\":{\"success\":%s,\"failure\":%s}}", isfirst ? "" : ",", filter->name, success, failure);
		send(soc, buf, strlen(buf), 0);
		if(strisequal(method, "REFRESH")) {
			memcpy(&stat[1], &stat[0], sizeof(struct tcpeek_stat));
		}
		if(isfirst) isfirst = 0;
	}
    if(!strisequal(method, "REFRESH")) {
        struct pcap_stat ps;
        memset(&ps, 0, sizeof(ps));
        pcap_stats(g.pcap.pcap, &ps);
        snprintf(buf, sizeof(buf), ",{\"pcap\":{\"stats\":{\"recv\":%u,\"drop\":%u,\"ifdrop\":%u}}}", ps.ps_recv, ps.ps_drop, ps.ps_ifdrop);
        send(soc, buf, strlen(buf), 0);
    }
	send(soc, "]", 1, 0);
	pthread_mutex_unlock(&g.session.mutex);
}
Beispiel #2
0
static void
tcpeek_init_filter_and_stat(void) {
	char *expression;
	struct tcpeek_filter *filter;

	lnklist_iter_init(g.option.expression);
	while(lnklist_iter_hasnext(g.option.expression)) {
		expression = lnklist_iter_next(g.option.expression);
		filter = tcpeek_filter_create();
		if(tcpeek_filter_parse(filter, expression) == -1) {
			tcpeek_filter_destroy(filter);
			error_abort("filter '%s' parse error.", expression);
		}
		if(!lnklist_add(g.filter, filter, filter->stat ? lnklist_size(g.filter) : 0)) {
			tcpeek_filter_destroy(filter);
			error_abort("can't allocate.");
		}
	}
}