예제 #1
0
static int
fixedwrite(Dssfile_t* file, Dssrecord_t* record, Dssdisc_t* disc)
{
	if (sfwrite(file->io, record->data, sizeof(Netflow_t)) != sizeof(Netflow_t))
	{
		if (disc->errorf)
			(*disc->errorf)(NiL, disc, 2, "%swrite error", cxlocation(file->dss->cx, record));
		return -1;
	}
	return 0;
}
예제 #2
0
static int
fixedread(Dssfile_t* file, Dssrecord_t* record, Dssdisc_t* disc)
{
	register State_t*	state = (State_t*)file->data;
	register Netflow_t*	rp;

	if (!(rp = (Netflow_t*)sfreserve(file->io, sizeof(*rp), 0)))
	{
		if (sfvalue(file->io))
		{
			if (disc->errorf)
				(*disc->errorf)(NiL, disc, 2, "%slast record incomplete", cxlocation(file->dss->cx, record));
			return -1;
		}
		return 0;
	}
	if (state->swap)
	{
	}
	record->data = rp;
	record->size = sizeof(*rp);
	return 1;
}
예제 #3
0
static int
ftfread(register Dssfile_t* file, register Dssrecord_t* record, Dssdisc_t* disc)
{
	register State_t*	state = (State_t*)file->data;
	register Netflow_t*	rp = &state->record;
	register char*		fp;
	size_t			n;
	Nftime_t		boot;

	while (!state->count--)
	{
		if (state->data = (char*)sfreserve(file->io, state->chunk * state->size, 0))
		{
			state->count = state->chunk;
			break;
		}
		if (!(n = sfvalue(file->io)))
			return 0;
		if (!(state->chunk = n / state->size))
		{
			if (disc->errorf)
				(*disc->errorf)(NiL, disc, 2, "%slast packet incomplete", cxlocation(file->dss->cx, record));
			return -1;
		}
		state->count = 0;
	}
	memset(rp, 0, sizeof(*rp));
	rp->set = NETFLOW_SET_src_addrv4|NETFLOW_SET_dst_addrv4|NETFLOW_SET_hopv4;
	fp = state->data;
	state->data += state->size;
	switch (state->version)
	{
	case 1:
		if (n = state->swap & 3)
		{
			swapmem(n, &R1(fp)->unix_secs, &R1(fp)->unix_secs, (char*)&R1(fp)->input - (char*)&R1(fp)->unix_secs);
			swapmem(n, &R1(fp)->dPkts, &R1(fp)->dPkts, (char*)&R1(fp)->srcport - (char*)&R1(fp)->dPkts);
			if (n &= 1)
			{
				swapmem(n, &R1(fp)->input, &R1(fp)->input, (char*)&R1(fp)->dPkts - (char*)&R1(fp)->input);
				swapmem(n, &R1(fp)->srcport, &R1(fp)->srcport, (char*)&R1(fp)->prot - (char*)&R1(fp)->srcport);
			}
		}
		rp->src_addrv4 = R1(fp)->srcaddr;
		rp->dst_addrv4 = R1(fp)->dstaddr;
		rp->hopv4 = R1(fp)->nexthop;
		rp->input = R1(fp)->input;
		rp->output = R1(fp)->output;
		rp->packets = R1(fp)->dPkts;
		rp->bytes = R1(fp)->dOctets;
		rp->first = R1(fp)->First;
		rp->last = R1(fp)->Last;
		rp->src_port = R1(fp)->srcport;
		rp->dst_port = R1(fp)->dstport;
		rp->flags = 0;
		rp->tcp_flags = R1(fp)->tcp_flags;
		rp->protocol = R1(fp)->prot;
		rp->src_tos = R1(fp)->tos;
		rp->time = R1(fp)->unix_secs;
		rp->nsec = R1(fp)->unix_nsecs;
		rp->uptime = R1(fp)->sysUpTime;
		break;
	case 5:
		if (n = state->swap & 3)
		{
			swapmem(n, &R5(fp)->unix_secs, &R5(fp)->unix_secs, (char*)&R5(fp)->input - (char*)&R5(fp)->unix_secs);
			swapmem(n, &R5(fp)->dPkts, &R5(fp)->dPkts, (char*)&R5(fp)->srcport - (char*)&R5(fp)->dPkts);
			if (n &= 1)
			{
				swapmem(n, &R5(fp)->input, &R5(fp)->input, (char*)&R5(fp)->dPkts - (char*)&R5(fp)->input);
				swapmem(n, &R5(fp)->srcport, &R5(fp)->srcport, (char*)&R5(fp)->prot - (char*)&R5(fp)->srcport);
				swapmem(n, &R5(fp)->src_as, &R5(fp)->src_as, (char*)(R5(fp)+1) - (char*)&R5(fp)->src_as);
			}
		}
		rp->src_addrv4 = R5(fp)->srcaddr;
		rp->dst_addrv4 = R5(fp)->dstaddr;
		rp->hopv4 = R5(fp)->nexthop;
		rp->input = R5(fp)->input;
		rp->output = R5(fp)->output;
		rp->packets = R5(fp)->dPkts;
		rp->bytes = R5(fp)->dOctets;
		rp->first = R5(fp)->First;
		rp->last = R5(fp)->Last;
		rp->src_port = R5(fp)->srcport;
		rp->dst_port = R5(fp)->dstport;
		rp->flags = 0;
		rp->tcp_flags = R5(fp)->tcp_flags;
		rp->protocol = R5(fp)->prot;
		rp->src_tos = R5(fp)->tos;
		rp->engine_type = R5(fp)->engine_type;
		rp->engine_id = R5(fp)->engine_id;
		rp->src_as16 = R5(fp)->src_as;
		rp->dst_as16 = R5(fp)->dst_as;
		rp->src_maskv4 = R5(fp)->src_mask;
		rp->dst_maskv4 = R5(fp)->dst_mask;
		rp->time = R5(fp)->unix_secs;
		rp->nsec = R5(fp)->unix_nsecs;
		rp->uptime = R5(fp)->sysUpTime;
		break;
	case 6:
		if (n = state->swap & 3)
		{
			swapmem(n, &R6(fp)->unix_secs, &R6(fp)->unix_secs, (char*)&R6(fp)->input - (char*)&R6(fp)->unix_secs);
			swapmem(n, &R6(fp)->dPkts, &R6(fp)->dPkts, (char*)&R6(fp)->srcport - (char*)&R6(fp)->dPkts);
			if (n &= 1)
			{
				swapmem(n, &R6(fp)->input, &R6(fp)->input, (char*)&R6(fp)->dPkts - (char*)&R6(fp)->input);
				swapmem(n, &R6(fp)->srcport, &R6(fp)->srcport, (char*)&R6(fp)->prot - (char*)&R6(fp)->srcport);
				swapmem(n, &R6(fp)->src_as, &R6(fp)->src_as, (char*)(R6(fp)+1) - (char*)&R6(fp)->src_as);
			}
		}
		rp->src_addrv4 = R6(fp)->srcaddr;
		rp->dst_addrv4 = R6(fp)->dstaddr;
		rp->hopv4 = R6(fp)->nexthop;
		rp->input = R6(fp)->input;
		rp->output = R6(fp)->output;
		rp->packets = R6(fp)->dPkts;
		rp->bytes = R6(fp)->dOctets;
		rp->first = R6(fp)->First;
		rp->last = R6(fp)->Last;
		rp->src_port = R6(fp)->srcport;
		rp->dst_port = R6(fp)->dstport;
		rp->flags = 0;
		rp->tcp_flags = R6(fp)->tcp_flags;
		rp->protocol = R6(fp)->prot;
		rp->src_tos = R6(fp)->tos;
		rp->engine_type = R6(fp)->engine_type;
		rp->engine_id = R6(fp)->engine_id;
		rp->src_as16 = R6(fp)->src_as;
		rp->dst_as16 = R6(fp)->dst_as;
		rp->src_maskv4 = R6(fp)->src_mask;
		rp->dst_maskv4 = R6(fp)->dst_mask;
		rp->time = R6(fp)->unix_secs;
		rp->nsec = R6(fp)->unix_nsecs;
		rp->uptime = R6(fp)->sysUpTime;
		break;
	case 7:
		if (n = state->swap & 3)
		{
			swapmem(n, &R7(fp)->unix_secs, &R7(fp)->unix_secs, (char*)&R7(fp)->input - (char*)&R7(fp)->unix_secs);
			swapmem(n, &R7(fp)->dPkts, &R7(fp)->dPkts, (char*)&R7(fp)->srcport - (char*)&R7(fp)->dPkts);
			if (n &= 1)
			{
				swapmem(n, &R7(fp)->input, &R7(fp)->input, (char*)&R7(fp)->dPkts - (char*)&R7(fp)->input);
				swapmem(n, &R7(fp)->srcport, &R7(fp)->srcport, (char*)&R7(fp)->prot - (char*)&R7(fp)->srcport);
				swapmem(n, &R7(fp)->src_as, &R7(fp)->src_as, (char*)(R7(fp)+1) - (char*)&R7(fp)->src_as);
			}
		}
		rp->src_addrv4 = R7(fp)->srcaddr;
		rp->dst_addrv4 = R7(fp)->dstaddr;
		rp->hopv4 = R7(fp)->nexthop;
		rp->input = R7(fp)->input;
		rp->output = R7(fp)->output;
		rp->packets = R7(fp)->dPkts;
		rp->bytes = R7(fp)->dOctets;
		rp->first = R7(fp)->First;
		rp->last = R7(fp)->Last;
		rp->src_port = R7(fp)->srcport;
		rp->dst_port = R7(fp)->dstport;
		rp->flags = 0;
		rp->tcp_flags = R7(fp)->tcp_flags;
		rp->protocol = R7(fp)->prot;
		rp->src_tos = R7(fp)->tos;
		rp->engine_type = R7(fp)->engine_type;
		rp->engine_id = R7(fp)->engine_id;
		rp->src_as16 = R7(fp)->src_as;
		rp->dst_as16 = R7(fp)->dst_as;
		rp->src_maskv4 = R7(fp)->src_mask;
		rp->dst_maskv4 = R7(fp)->dst_mask;
		rp->time = R7(fp)->unix_secs;
		rp->nsec = R7(fp)->unix_nsecs;
		rp->uptime = R7(fp)->sysUpTime;
		break;
	}
	boot = ((Nftime_t)rp->time * MS - (Nftime_t)rp->uptime) * US + (Nftime_t)rp->nsec;
	rp->start = boot + (Nftime_t)rp->first * US;
	rp->end = boot + (Nftime_t)rp->last * US;
	record->size = sizeof(*rp);
	record->data = rp;
	return 1;
}
예제 #4
0
static int
dumpfwrite(Dssfile_t* file, Dssrecord_t* record, Dssdisc_t* disc)
{
	register State_t*	state = (State_t*)file->data;
	register Netflow_t*	rp = (Netflow_t*)record->data;
	register size_t		n;

	if (!state->count++)
	{
		state->swap = _ast_intswap;
		switch (rp->version)
		{
		case 1:
			state->flush = 24;
			n = 16;
			if (state->swap)
			{
				swapmem(1, &rp->version, state->data, 4);
				swapmem(3, &rp->uptime, state->data + 4, 12);
			}
			else
				memcpy(state->data, &rp->version, n);
			break;
		case 5:
			state->flush = 30;
			n = 24;
			if (state->swap)
			{
				swapmem(1, &rp->version, state->data, 4);
				swapmem(3, &rp->uptime, state->data + 4, 16);
				memcpy(state->data + 20, &rp->engine_type, 2);
				swapmem(1, &rp->sampler_interval, state->data + 22, 2);
				*(state->data + 23) |= rp->sampler_mode << 6;
			}
			else
			{
				memcpy(state->data, &rp->version, n);
				*(state->data + 22) |= rp->sampler_mode << 6;
			}
			break;
		case 7:
			state->flush = 27;
			n = 24;
			if (state->swap)
			{
				swapmem(1, &rp->version, state->data, 4);
				swapmem(3, &rp->uptime, state->data + 4, 16);
			}
			else
				memcpy(state->data, &rp->version, n);
			break;
		}
		state->next = state->data + n;
		state->version = rp->version;
	}
	if (rp->version != state->version)
	{
		if (disc->errorf)
			(*disc->errorf)(NiL, disc, 2, "%s%d: record version does not match header version %d", cxlocation(file->dss->cx, record), rp->version, state->version);
		return -1;
	}
	switch (rp->version)
	{
	case 1:
		n = sizeof(Rec_1_t);
		if (state->swap)
		{
			swapmem(3, &rp->src_addrv4, state->next, 12);
			swapmem(1, &rp->input, state->next + 12, 4);
			swapmem(3, &rp->packets, state->next + 16, 16);
			swapmem(1, &rp->src_port, state->next + 32, 4);
			memcpy(state->next + 36, &rp->flags, 4);
		}
		else
			memcpy(state->next, rp, n);
#if 0
		memcpy(&rp->pad1, state->next + 40, 4);
#endif
		*(state->next + 44) = rp->flags;
		break;
	case 5:
		n = sizeof(Rec_5_t);
		if (state->swap)
		{
			swapmem(3, &rp->src_addrv4, state->next, 12);
			swapmem(1, &rp->input, state->next + 12, 4);
			swapmem(3, &rp->packets, state->next + 16, 16);
			swapmem(1, &rp->src_port, state->next + 32, 4);
			memcpy(state->next + 36, &rp->flags, 4);
			swapmem(1, &rp->src_as16, state->next + 40, 4);
			memcpy(state->next + 44, &rp->src_maskv4, 2);
		}
		else
			memcpy(state->next, rp, n);
		*(state->next + 36) = 0;
		*(state->next + 46) = 0;
		*(state->next + 47) = 0;
		break;
	case 7:
		n = sizeof(Rec_7_t);
		if (state->swap)
		{
			swapmem(3, &rp->src_addrv4, state->next, 12);
			swapmem(1, &rp->input, state->next + 12, 4);
			swapmem(3, &rp->packets, state->next + 16, 16);
			swapmem(1, &rp->src_port, state->next + 32, 4);
			memcpy(state->next + 36, &rp->flags, 4);
			swapmem(1, &rp->src_as16, state->next + 40, 4);
			memcpy(state->next + 44, &rp->src_maskv4, 2);
			swapmem(1, &rp->router_scv4, state->next + 48, 4);
		}
		else
			memcpy(state->next, rp, n);
		break;
	}
	state->next += n;
	if (state->count >= state->flush)
	{
		((Hdr_1_t*)state->data)->count = state->count;
		if (state->swap)
			swapmem(1, &((Hdr_1_t*)state->data)->count, &((Hdr_1_t*)state->data)->count, 2);
		state->count = 0;
		if (sfwrite(file->io, state->data, NETFLOW_PACKET) != NETFLOW_PACKET)
		{
			if (disc->errorf)
				(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "%s: packet write error", file->format->name);
			return -1;
		}
	}
	return 0;
}
예제 #5
0
static int
dumpfread(register Dssfile_t* file, register Dssrecord_t* record, Dssdisc_t* disc)
{
	register State_t*	state = (State_t*)file->data;
	register Netflow_t*	rp = &state->record;
	Rec_1_t*		r1;
	int			n;

	while (!rp->count--)
	{
		if (!(state->data = (char*)sfreserve(file->io, NETFLOW_PACKET, 0)))
		{
			if (sfvalue(file->io))
			{
				if (disc->errorf)
					(*disc->errorf)(NiL, disc, 2, "%slast packet incomplete", cxlocation(file->dss->cx, record));
				return -1;
			}
			return 0;
		}
		state->swap = 0;
		n = ((Hdr_1_t*)state->data)->version;
		for (;;)
		{
			switch (n)
			{
			case 1:
				n = 16;
				if (state->swap)
				{
					swapmem(1, state->data, &rp->version, 4);
					swapmem(3, state->data + 4, &rp->uptime, 12);
				}
				else
					memcpy(&rp->version, state->data, n);
				break;
			case 5:
				n = 24;
				if (state->swap)
				{
					swapmem(1, state->data, &rp->version, 4);
					swapmem(3, state->data + 4, &rp->uptime, 16);
					memcpy(&rp->engine_type, state->data + 20, 2);
					swapmem(1, state->data + 22, &rp->sampler_interval, 2);
				}
				else
					memcpy(&rp->version, state->data, n);
				rp->sampler_mode = (rp->sampler_interval >> 14) & ((1<<2)-1);
				rp->sampler_interval &= ((1<<14)-1);
				break;
			case 7:
				n = 24;
				if (state->swap)
				{
					swapmem(1, state->data, &rp->version, 4);
					swapmem(3, state->data + 4, &rp->uptime, 16);
				}
				else
					memcpy(&rp->version, state->data, n);
				break;
			default:
				if (state->swap)
				{
					if (disc->errorf)
						(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "%sheader corrupted", cxlocation(file->dss->cx, record));
					return -1;
				}
				state->swap = 7;
				n = swapget(2, state->data, 2);
				continue;
			}
			state->boot = ((Nftime_t)rp->time * MS - (Nftime_t)rp->uptime) * US + (Nftime_t)rp->nsec;
			break;
		}
		state->data += n;
	}
	switch (rp->version)
	{
	case 1:
		r1 = (Rec_1_t*)state->data;
		if (state->swap)
		{
			swapmem(3, state->data, &rp->src_addrv4, 12);
			swapmem(1, state->data + 12, &rp->input, 4);
			swapmem(3, state->data + 16, &rp->packets, 16);
			swapmem(1, state->data + 32, &rp->src_port, 4);
			memcpy(&rp->flags, state->data + 36, 4);
		}
		else
			memcpy(rp, r1, sizeof(Rec_1_t));
		state->data += sizeof(Rec_1_t);
		rp->flags = r1->flags;
		memset(&rp->src_as16, 0, 12);
		break;
	case 5:
		if (state->swap)
		{
			swapmem(3, state->data, &rp->src_addrv4, 12);
			swapmem(1, state->data + 12, &rp->input, 4);
			swapmem(3, state->data + 16, &rp->packets, 16);
			swapmem(1, state->data + 32, &rp->src_port, 4);
			memcpy(&rp->flags, state->data + 36, 4);
			swapmem(1, state->data + 40, &rp->src_as16, 4);
			memcpy(&rp->src_maskv4, state->data + 44, 2);
		}
		else
			memcpy(rp, state->data, sizeof(Rec_5_t));
		state->data += sizeof(Rec_5_t);
		rp->flags = 0;
		break;
	case 7:
		if (state->swap)
		{
			swapmem(3, state->data, &rp->src_addrv4, 12);
			swapmem(1, state->data + 12, &rp->input, 4);
			swapmem(3, state->data + 16, &rp->packets, 16);
			swapmem(1, state->data + 32, &rp->src_port, 4);
			memcpy(&rp->flags, state->data + 36, 4);
			swapmem(1, state->data + 40, &rp->src_as16, 4);
			memcpy(&rp->src_maskv4, state->data + 44, 2);
			swapmem(1, state->data + 48, &rp->router_scv4, 4);
		}
		else
			memcpy(rp, state->data, sizeof(Rec_7_t));
		state->data += sizeof(Rec_7_t);
		break;
	}
	rp->start = state->boot + (Nftime_t)rp->first * US;
	rp->end = state->boot + (Nftime_t)rp->last * US;
	record->size = sizeof(*rp);
	record->data = rp;
	return 1;
}
예제 #6
0
static int
validate_sel(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc)
{
	register State_t*	state = (State_t*)expr->data;
	register Field_t*	field;
	register Cxconstraint_t*constraint;
	Cxoperand_t		o;
	Cxinstruction_t		x;
	Invalid_t		key;
	Invalid_t*		ip;
	size_t			n;

	for (field = state->field; field; field = field->next)
	{
		x.data.variable = field->variable;
		if ((*state->getf)(cx, &x, &o, NiL, NiL, data, disc))
			return -1;
		if (field->variable->format.map)
		{
			if (cxisstring(field->variable->type))
			{
				if (cxstr2num(cx, &field->variable->format, o.value.string.data, o.value.string.size, NiL))
				{
					if (state->verbose && disc->errorf)
						(*disc->errorf)(NiL, disc, 1, "%s%s: %-.*s: unknown map name", cxlocation(cx, data), field->variable->name, o.value.string.size, o.value.string.data);
					goto invalid;
				}
			}
			else if (cxisnumber(field->variable->type))
			{
				if (cxnum2str(cx, &field->variable->format, (Cxinteger_t)o.value.number, NiL))
				{
					if (state->verbose && disc->errorf)
						(*disc->errorf)(NiL, disc, 1, "%s%s: %I*d: unknown map value", cxlocation(cx, data), field->variable->name, sizeof(Cxinteger_t), (Cxinteger_t)o.value.number);
					goto invalid;
				}
			}
		}
		if (constraint = field->variable->format.constraint)
		{
			if (constraint->constraintf)
				;
			if (cxisnumber(field->variable->type))
			{
				if (constraint->min && o.value.number < constraint->min->number)
				{
					if (state->verbose && disc->errorf)
						(*disc->errorf)(NiL, disc, 1, "%s%s: %1.15Lg violates min constraint %1.15Lg", cxlocation(cx, data), field->variable->name, o.value.number, constraint->min->number);
					goto invalid;
				}
				if (constraint->max && o.value.number > constraint->max->number)
				{
					if (state->verbose && disc->errorf)
						(*disc->errorf)(NiL, disc, 1, "%s%s: %1.15Lg violates max constraint %1.15Lg", cxlocation(cx, data), field->variable->name, o.value.number, constraint->max->number);
					goto invalid;
				}
			}
			if (constraint->expression)
				;
			if (constraint->pattern)
				;
		}
		continue;
	invalid:
		if (state->invalid)
		{
			key.variable = field->variable;
			key.value = o.value;
			if (!(ip = (Invalid_t*)dtsearch(state->invalid, &key)))
			{
				n = cxsize(field->variable->type, &o.value);
				if (!(ip = vmnewof(state->vm, 0, Invalid_t, 1, n)))
				{
					if (disc->errorf)
						(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space");
					return -1;
				}
				*ip = key;
				ip->value = o.value;
				if (n)
				{
					ip->value.buffer.data = (void*)(ip + 1);
					memcpy(ip->value.buffer.data, o.value.buffer.data, n);
				}
				dtinsert(state->invalid, ip);
				ip->count = 0;
			}
			ip->count++;
		}
		if (state->setf && constraint && constraint->def)
		{
			o.type = field->variable->type;
			o.value = *constraint->def;
			if ((*state->setf)(cx, &x, &o, &o, NiL, data, disc))
				return -1;
			field->repaired++;
		}
		else if (state->discard)
		{
			field->discarded++;
			return 0;
		}
		else
			field->invalid++;
	}
	return 1;
}