Пример #1
0
/* Stop the sampler if we were currently sampling, do nothing otherwise or 
 * if sampler.stop == NULL */
static void samplerStop(MeasTaskState *measState)
{
	Sampler *sampler = &measState->sampler;
	StreamState *streamState = &measState->streamState;

	if (sampler->stop == NULL  ||  measState->measStatus != SAMPLING)
		return;

	switchStdout(streamState); /* Switch stdout to file */
	sampler->stop(&measState->samplerData, measState->samplerState);
	switchStdout(streamState); /* Switch stdout back */
}
Пример #2
0
/* Sample and return sampler.sample(), or do nothing if sampler.sample == 
 * NULL and return SAMPLER_OK. Note that this would be a pretty useless sampler 
 * in the latter case... */
static SamplerSignal samplerSample(MeasTaskState *measState)
{
	Sampler *sampler = &measState->sampler;
	StreamState *streamState = &measState->streamState;

	if (sampler->sample == NULL)
		return SAMPLER_OK;

	switchStdout(streamState); /* Switch stdout to file */
	SamplerSignal ret = sampler->sample(&measState->samplerData, 
				measState->samplerState);
	switchStdout(streamState); /* Switch stdout back */

	return ret;
}
Пример #3
0
/* Returns the state pointer that gets returned from sampler.start(), or 
 * NULL if sampler.start == NULL */
static void *samplerStart(MeasTaskState *measState)
{
	Sampler *sampler = &measState->sampler;
	StreamState *streamState = &measState->streamState;
	void *ret;

	switchStdout(streamState); /* Switch stdout to file */
	if (measState->measConf.measureHeader != NULL)
		printf("%s", measState->measConf.measureHeader);

	if (sampler->header != NULL)
		printf("%s", sampler->header);

	if (sampler->start == NULL)
		ret = NULL;
	else
		ret = sampler->start(&measState->samplerData, 
					sampler->samplerConf);

	switchStdout(streamState); /* Switch stdout back */

	return ret;
}
Пример #4
0
static int print_match_save(const struct xt_entry_match *e,
	const struct ipt_ip *ip)
{
	char buf[BUFSIZ];
	const struct xtables_match *match =
		xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);

	if (match) {
#ifdef OLD_IPTABLES
		ptr += sprintf(ptr," -m %s", e->u.user.name);
#else
		ptr += sprintf(ptr, " -m %s", match->alias ? match->alias(e) : e->u.user.name);
#endif

		/* some matches don't provide a save function */
		if (match->save){
			memset(buf, 0, sizeof(buf));
			switchStdout("/dev/null");
			setvbuf(stdout, buf, _IOLBF, BUFSIZ);
			match->save(ip, e);
			fflush(stdout);
			setbuf(stdout, NULL);
			revertStdout();
			ptr += sprintf(ptr, "%s", buf);
		}
	}
	else {
		if (e->u.match_size) {
			fprintf(stderr,
				"Can't find library for match `%s'\n",
				e->u.user.name);
			exit(1);
		}
	}
	return 0;
}
Пример #5
0
/* We want this to be readable, so only print out neccessary fields.
* Because that's the kind of world I want to live in.  */
extern EXPORT const char* output_rule4(const struct ipt_entry *e, void *h, const char *chain, int counters)
{
	const struct xt_entry_target *t;
	const char *target_name;
	char buf[BUFSIZ];

	/* print counters for iptables-save */
	if (counters > 0)
		ptr += sprintf(ptr,"[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);

	/* print chain name */
	ptr += sprintf(ptr,"-A %s", chain);

	/* Print IP part. */
	print_ip("-s", e->ip.src.s_addr, e->ip.smsk.s_addr,
		e->ip.invflags & IPT_INV_SRCIP);

	print_ip("-d", e->ip.dst.s_addr, e->ip.dmsk.s_addr,
		e->ip.invflags & IPT_INV_DSTIP);

	print_iface('i', e->ip.iniface, e->ip.iniface_mask,
		e->ip.invflags & IPT_INV_VIA_IN);

	print_iface('o', e->ip.outiface, e->ip.outiface_mask,
		e->ip.invflags & IPT_INV_VIA_OUT);

	print_proto(e->ip.proto, e->ip.invflags & XT_INV_PROTO);

	if (e->ip.flags & IPT_F_FRAG)
		ptr += sprintf(ptr,"%s -f",
		e->ip.invflags & IPT_INV_FRAG ? " !" : "");

	/* Print matchinfo part */
	if (e->target_offset) {
		IPT_MATCH_ITERATE(e, print_match_save, &e->ip);
	}

	/* print counters for iptables -R */
	if (counters < 0)
		ptr += sprintf(ptr," -c %llu %llu", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);

	/* Print target name */
	target_name = iptc_get_target(e, h);
#ifdef OLD_IPTABLES
	if (target_name && (*target_name != '\0'))
#ifdef IPT_F_GOTO
		ptr += sprintf(ptr," -%c %s", e->ip.flags & IPT_F_GOTO ? 'g' : 'j', target_name);
#else
		ptr += sprintf(ptr," -j %s", target_name);
#endif
#endif

	/* Print targinfo part */
	t = ipt_get_target((struct ipt_entry *)e);
	if (t->u.user.name[0]) {
		const struct xtables_target *target =
			xtables_find_target(t->u.user.name, XTF_TRY_LOAD);

		if (!target) {
			fprintf(stderr, "Can't find library for target `%s'\n",
				t->u.user.name);
			return NULL;
		}
		
#ifndef OLD_IPTABLES
		ptr += sprintf(ptr, " -j %s", target->alias ? target->alias(t) : target_name);
#endif

		if (target){
			if (target->save){
				memset(buf, 0, sizeof(buf));
				switchStdout("/dev/null");
				setvbuf(stdout, buf, _IOLBF, BUFSIZ);
				target->save(&e->ip, t);
				fflush(stdout);
				setbuf(stdout, NULL);
				revertStdout();
				ptr += sprintf(ptr, "%s", buf);
			}
			else {
				/* If the target size is greater than xt_entry_target
				* there is something to be saved, we just don't know
				* how to print it */
				if (t->u.target_size !=
					sizeof(struct xt_entry_target)) {
					fprintf(stderr, "Target `%s' is missing "
						"save function\n",
						t->u.user.name);
					return NULL;
				}
			}
		}
	}

#ifndef OLD_IPTABLES
	else if (target_name && (*target_name != '\0')){
#ifdef IPT_F_GOTO
		ptr += sprintf(ptr, " -%c %s", e->ip.flags & IPT_F_GOTO ? 'g' : 'j', target_name);
#else
		ptr += sprintf(ptr, " -j %s", target_name);
#endif
	}
#endif

	*ptr = '\0';
	ptr = buffer;

	return buffer;
}