예제 #1
0
파일: kickfile.c 프로젝트: farsightsec/nmsg
char *
kickfile_time(void) {
	char *kt;
	char when[32];
	struct timespec ts;
	struct tm tm;
	time_t t;

	nmsg_timespec_get(&ts);
	t = (time_t) ts.tv_sec;
	gmtime_r(&t, &tm);
	strftime(when, sizeof(when), "%Y%m%d.%H%M.%s", &tm);
	nmsg_asprintf(&kt, "%s.%09ld", when, ts.tv_nsec);
	assert(kt != NULL);

	return (kt);
}
예제 #2
0
nmsg_res
_input_pres_read(nmsg_input_t input, nmsg_message_t *msg) {
	char line[1024];
	nmsg_res res;
	size_t sz;
	struct timespec ts;
	uint8_t *pbuf;

	while (fgets(line, sizeof(line), input->pres->fp) != NULL) {
		res = nmsg_msgmod_pres_to_payload(input->msgmod, input->clos,
						  line);
		if (res == nmsg_res_failure)
			return (res);
		if (res == nmsg_res_success)
			continue;
		if (res != nmsg_res_pbuf_ready)
			return (res);

		/* payload ready, finalize and convert to nmsg payload */
		nmsg_timespec_get(&ts);
		res = nmsg_msgmod_pres_to_payload_finalize(input->msgmod,
							   input->clos,
							   &pbuf, &sz);
		if (res != nmsg_res_success)
			return (res);
		*msg = nmsg_message_from_raw_payload(input->msgmod->plugin->vendor.id,
						     input->msgmod->plugin->msgtype.id,
						     pbuf, sz, &ts);
		if (*msg == NULL) {
			free(pbuf);
			return (nmsg_res_memfail);
		}

		return (nmsg_res_success);
	}

	return (nmsg_res_eof);
}
예제 #3
0
파일: input_nmsg.c 프로젝트: hstern/nmsg
nmsg_res
_input_nmsg_read_container_xs(nmsg_input_t input, Nmsg__Nmsg **nmsg) {
	int ret;
	nmsg_res res;
	uint8_t *buf;
	size_t buf_len;
	ssize_t msgsize = 0;
	xs_msg_t xmsg;
	xs_pollitem_t xitems[1];

	/* poll */
	xitems[0].socket = input->stream->xs;
	xitems[0].events = XS_POLLIN;
	ret = xs_poll(xitems, 1, NMSG_RBUF_TIMEOUT);
	if (ret == 0 || (ret == -1 && errno == EINTR))
		return (nmsg_res_again);
	else if (ret == -1)
		return (nmsg_res_read_failure);

	/* initialize XS message object */
	if (xs_msg_init(&xmsg))
		return (nmsg_res_failure);

	/* read the NMSG container */
	if (xs_recvmsg(input->stream->xs, &xmsg, 0) == -1) {
		res = nmsg_res_failure;
		goto out;
	}
	nmsg_timespec_get(&input->stream->now);

	/* get buffer from the XS message */
	buf = xs_msg_data(&xmsg);
	buf_len = xs_msg_size(&xmsg);
	if (buf_len < NMSG_HDRLSZ_V2) {
		res = nmsg_res_failure;
		goto out;
	}

	/* deserialize the NMSG header */
	res = _input_nmsg_deserialize_header(buf, buf_len, &msgsize, &input->stream->flags);
	if (res != nmsg_res_success)
		goto out;
	buf += NMSG_HDRLSZ_V2;

	/* the entire message must have been read by xs_recvmsg() */
	assert((size_t) msgsize == buf_len - NMSG_HDRLSZ_V2);

	/* unpack message */
	res = _input_nmsg_unpack_container(input, nmsg, buf, msgsize);

	/* update seqsrc counts */
	if (input->stream->verify_seqsrc && *nmsg != NULL) {
		struct nmsg_seqsrc *seqsrc = _input_seqsrc_get(input, *nmsg);
		if (seqsrc != NULL)
			_input_seqsrc_update(input, seqsrc, *nmsg);
	}

	/* expire old outstanding fragments */
	_input_frag_gc(input->stream);

out:
	xs_msg_close(&xmsg);
	return (res);
}