コード例 #1
0
ファイル: writer.c プロジェクト: gitter-badger/knot
dt_writer_t* dt_writer_create(const char *file_path, const char *version)
{
    struct fstrm_file_options *fopt = NULL;
    struct fstrm_writer_options *wopt = NULL;
    dt_writer_t *writer = NULL;
    fstrm_res res;

    writer = calloc(1, sizeof(dt_writer_t));
    if (writer == NULL) {
        goto fail;
    }

    // Set "version".
    if (version != NULL) {
        writer->len_version = strlen(version);
        writer->version = strdup(version);
        if (!writer->version) {
            goto fail;
        }
    }

    // Open writer.
    fopt = fstrm_file_options_init();
    fstrm_file_options_set_file_path(fopt, file_path);
    wopt = fstrm_writer_options_init();
    fstrm_writer_options_add_content_type(wopt,
                                          (const uint8_t *) DNSTAP_CONTENT_TYPE,
                                          strlen(DNSTAP_CONTENT_TYPE));
    writer->fw = fstrm_file_writer_init(fopt, wopt);
    fstrm_file_options_destroy(&fopt);
    fstrm_writer_options_destroy(&wopt);
    if (writer->fw == NULL) {
        goto fail;
    }

    res = fstrm_writer_open(writer->fw);
    if (res != fstrm_res_success) {
        goto fail;
    }

    return writer;
fail:
    dt_writer_free(writer);
    return NULL;
}
コード例 #2
0
ファイル: fstrm_dump.c プロジェクト: farsightsec/fstrm
int main(int argc, char **argv)
{
	const char *input_fname = NULL;
	const char *output_fname = NULL;

	fstrm_res res = fstrm_res_failure;
	struct fstrm_file_options *fopt = NULL;
	struct fstrm_writer_options *wopt = NULL;
	struct fstrm_reader *r = NULL;
	struct fstrm_writer *w = NULL;

	int rv = EXIT_FAILURE;

	/* Args. */
	if (argc != 2 && argc != 3) {
		fprintf(stderr, "Usage: %s <INPUT FILE> [<OUTPUT FILE>]\n", argv[0]);
		fprintf(stderr, "Dumps a Frame Streams formatted input file.\n\n");
		return EXIT_FAILURE;
	}
	input_fname = argv[1];
	if (argc == 3)
		output_fname = argv[2];

	/* Line buffering. */
	setvbuf(stdout, NULL, _IOLBF, 0);
	setvbuf(stderr, NULL, _IOLBF, 0);

	/* Setup file reader options. */
	fopt = fstrm_file_options_init();
	fstrm_file_options_set_file_path(fopt, input_fname);

	/* Initialize file reader. */
	r = fstrm_file_reader_init(fopt, NULL);
	if (r == NULL) {
		fputs("Error: fstrm_file_reader_init() failed.\n", stderr);
		goto out;
	}
	res = fstrm_reader_open(r);
	if (res != fstrm_res_success) {
		fputs("Error: fstrm_reader_open() failed.\n", stderr);
		goto out;
	}

	if (output_fname != NULL) {
		/* Setup file writer options. */
		fstrm_file_options_set_file_path(fopt, output_fname);

		/* Setup writer options. */
		wopt = fstrm_writer_options_init();

		/* Copy "content type" from the reader's START frame. */
		res = process_start_frame(r, wopt);
		if (res != fstrm_res_success) {
			fputs("Error: process_start_frame() failed.\n", stderr);
			goto out;
		}

		/* Initialize file writer. */
		w = fstrm_file_writer_init(fopt, wopt);
		if (w == NULL) {
			fputs("Error: fstrm_file_writer_init() failed.\n", stderr);
			goto out;
		}
		res = fstrm_writer_open(w);
		if (res != fstrm_res_success) {
			fstrm_writer_destroy(&w);
			fputs("Error: fstrm_writer_open() failed.\n", stderr);
			goto out;
		}
	} else {
		/* Process the START frame. */
		res = process_start_frame(r, NULL);
		if (res != fstrm_res_success) {
			fprintf(stderr, "Error: process_start_frame() failed.\n");
			goto out;
		}
	}

	/* Loop over data frames. */
	for (;;) {
		const uint8_t *data;
		size_t len_data;

		res = fstrm_reader_read(r, &data, &len_data);
		if (res == fstrm_res_success) {
			/* Got a data frame. */
			res = print_data_frame(data, len_data);
			if (res != fstrm_res_success) {
				fprintf(stderr, "Error: print_data_frame() failed.\n");
				goto out;
			}
			if (w != NULL) {
				/* Write the data frame. */
				res = fstrm_writer_write(w, data, len_data);
				if (res != fstrm_res_success) {
					fprintf(stderr, "Error: write_data_frame() failed.\n");
					goto out;
				}
			}
		} else if (res == fstrm_res_stop) {
			/* Normal end of data stream. */
			res = print_stop_frame(r);
			if (res != fstrm_res_success) {
				fprintf(stderr, "Error: unable to read STOP frame.\n");
				goto out;
			}
			rv = EXIT_SUCCESS;
			break;
		} else {
			/* Abnormal end. */
			fprintf(stderr, "Error: fstrm_reader_read() failed.\n");
			goto out;
		}
	}

out:
	/* Cleanup options. */
	fstrm_file_options_destroy(&fopt);
	fstrm_writer_options_destroy(&wopt);

	/* Cleanup reader. */
	fstrm_reader_destroy(&r);

	/* Cleanup writer. */
	if (w != NULL) {
		res = fstrm_writer_close(w);
		if (res != fstrm_res_success) {
			fprintf(stderr, "Error: fstrm_writer_close() failed.\n");
			rv = EXIT_FAILURE;
		}
		fstrm_writer_destroy(&w);
	}

	return rv;
}