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; }
struct dt_env * dt_create(const char *socket_path, unsigned num_workers) { fstrm_res res; struct dt_env *env; struct fstrm_iothr_options *fopt; struct fstrm_unix_writer_options *fuwopt; struct fstrm_writer *fw; struct fstrm_writer_options *fwopt; verbose(VERB_OPS, "attempting to connect to dnstap socket %s", socket_path); log_assert(socket_path != NULL); log_assert(num_workers > 0); env = (struct dt_env *) calloc(1, sizeof(struct dt_env)); if (!env) return NULL; fwopt = fstrm_writer_options_init(); res = fstrm_writer_options_add_content_type(fwopt, DNSTAP_CONTENT_TYPE, sizeof(DNSTAP_CONTENT_TYPE) - 1); log_assert(res == fstrm_res_success); fuwopt = fstrm_unix_writer_options_init(); fstrm_unix_writer_options_set_socket_path(fuwopt, socket_path); fw = fstrm_unix_writer_init(fuwopt, fwopt); log_assert(fw != NULL); fopt = fstrm_iothr_options_init(); fstrm_iothr_options_set_num_input_queues(fopt, num_workers); env->iothr = fstrm_iothr_init(fopt, &fw); if (env->iothr == NULL) { verbose(VERB_DETAIL, "dt_create: fstrm_iothr_init() failed"); fstrm_writer_destroy(&fw); free(env); env = NULL; } fstrm_iothr_options_destroy(&fopt); fstrm_unix_writer_options_destroy(&fuwopt); fstrm_writer_options_destroy(&fwopt); return env; }
static fstrm_res process_start_frame(struct fstrm_reader *r, struct fstrm_writer_options *wopt) { fstrm_res res; const struct fstrm_control *control = NULL; size_t n_content_type = 0; const uint8_t *content_type = NULL; size_t len_content_type = 0; res = fstrm_reader_get_control(r, FSTRM_CONTROL_START, &control); if (res != fstrm_res_success) return res; fputs("FSTRM_CONTROL_START.\n", stderr); res = fstrm_control_get_num_field_content_type(control, &n_content_type); if (res != fstrm_res_success) return res; if (n_content_type > 0) { res = fstrm_control_get_field_content_type(control, 0, &content_type, &len_content_type); if (res != fstrm_res_success) return res; fprintf(stderr, "FSTRM_CONTROL_FIELD_CONTENT_TYPE (%zd bytes).\n ", len_content_type); print_string(content_type, len_content_type, stderr); fputc('\n', stderr); } if (wopt != NULL && content_type != NULL) { res = fstrm_writer_options_add_content_type(wopt, content_type, len_content_type); if (res != fstrm_res_success) return res; } return fstrm_res_success; }