📄 masterdump.c
字号:
static isc_result_ttask_send(dns_dumpctx_t *dctx) { isc_event_t *event; event = isc_event_allocate(dctx->mctx, NULL, DNS_EVENT_DUMPQUANTUM, dump_quantum, dctx, sizeof(*event)); if (event == NULL) return (ISC_R_NOMEMORY); isc_task_send(dctx->task, &event); return (ISC_R_SUCCESS);}static isc_result_tdumpctx_create(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version, const dns_master_style_t *style, FILE *f, dns_dumpctx_t **dctxp, dns_masterformat_t format){ dns_dumpctx_t *dctx; isc_result_t result; isc_boolean_t relative; dctx = isc_mem_get(mctx, sizeof(*dctx)); if (dctx == NULL) return (ISC_R_NOMEMORY); dctx->mctx = NULL; dctx->f = f; dctx->dbiter = NULL; dctx->db = NULL; dctx->version = NULL; dctx->done = NULL; dctx->done_arg = NULL; dctx->task = NULL; dctx->nodes = 0; dctx->first = ISC_TRUE; dctx->canceled = ISC_FALSE; dctx->file = NULL; dctx->tmpfile = NULL; dctx->format = format; switch (format) { case dns_masterformat_text: dctx->dumpsets = dump_rdatasets_text; break; case dns_masterformat_raw: dctx->dumpsets = dump_rdatasets_raw; break; default: INSIST(0); break; } result = totext_ctx_init(style, &dctx->tctx); if (result != ISC_R_SUCCESS) { UNEXPECTED_ERROR(__FILE__, __LINE__, "could not set master file style"); goto cleanup; } isc_stdtime_get(&dctx->now); dns_db_attach(db, &dctx->db); dctx->do_date = dns_db_iscache(dctx->db); if (dctx->format == dns_masterformat_text && (dctx->tctx.style.flags & DNS_STYLEFLAG_REL_OWNER) != 0) { relative = ISC_TRUE; } else relative = ISC_FALSE; result = dns_db_createiterator(dctx->db, relative, &dctx->dbiter); if (result != ISC_R_SUCCESS) goto cleanup; result = isc_mutex_init(&dctx->lock); if (result != ISC_R_SUCCESS) goto cleanup; if (version != NULL) dns_db_attachversion(dctx->db, version, &dctx->version); else if (!dns_db_iscache(db)) dns_db_currentversion(dctx->db, &dctx->version); isc_mem_attach(mctx, &dctx->mctx); dctx->references = 1; dctx->magic = DNS_DCTX_MAGIC; *dctxp = dctx; return (ISC_R_SUCCESS); cleanup: if (dctx->dbiter != NULL) dns_dbiterator_destroy(&dctx->dbiter); if (dctx->db != NULL) dns_db_detach(&dctx->db); if (dctx != NULL) isc_mem_put(mctx, dctx, sizeof(*dctx)); return (result);}static isc_result_tdumptostreaminc(dns_dumpctx_t *dctx) { isc_result_t result; isc_buffer_t buffer; char *bufmem; isc_region_t r; dns_name_t *name; dns_fixedname_t fixname; unsigned int nodes; dns_masterrawheader_t rawheader; isc_uint32_t now32; isc_time_t start; bufmem = isc_mem_get(dctx->mctx, initial_buffer_length); if (bufmem == NULL) return (ISC_R_NOMEMORY); isc_buffer_init(&buffer, bufmem, initial_buffer_length); dns_fixedname_init(&fixname); name = dns_fixedname_name(&fixname); if (dctx->first) { switch (dctx->format) { case dns_masterformat_text: /* * If the database has cache semantics, output an * RFC2540 $DATE directive so that the TTLs can be * adjusted when it is reloaded. For zones it is not * really needed, and it would make the file * incompatible with pre-RFC2540 software, so we omit * it in the zone case. */ if (dctx->do_date) { result = dns_time32_totext(dctx->now, &buffer); RUNTIME_CHECK(result == ISC_R_SUCCESS); isc_buffer_usedregion(&buffer, &r); fprintf(dctx->f, "$DATE %.*s\n", (int) r.length, (char *) r.base); } break; case dns_masterformat_raw: r.base = (unsigned char *)&rawheader; r.length = sizeof(rawheader); isc_buffer_region(&buffer, &r); isc_buffer_putuint32(&buffer, dns_masterformat_raw); isc_buffer_putuint32(&buffer, DNS_RAWFORMAT_VERSION); if (sizeof(now32) != sizeof(dctx->now)) { /* * We assume isc_stdtime_t is a 32-bit integer, * which should be the case on most cases. * If it turns out to be uncommon, we'll need * to bump the version number and revise the * header format. */ isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL, DNS_LOGMODULE_MASTERDUMP, ISC_LOG_INFO, "dumping master file in raw " "format: stdtime is not 32bits"); now32 = 0; } else now32 = dctx->now; isc_buffer_putuint32(&buffer, now32); INSIST(isc_buffer_usedlength(&buffer) <= sizeof(rawheader)); result = isc_stdio_write(buffer.base, 1, isc_buffer_usedlength(&buffer), dctx->f, NULL); if (result != ISC_R_SUCCESS) return (result); isc_buffer_clear(&buffer); break; default: INSIST(0); } result = dns_dbiterator_first(dctx->dbiter); dctx->first = ISC_FALSE; } else result = ISC_R_SUCCESS; nodes = dctx->nodes; isc_time_now(&start); while (result == ISC_R_SUCCESS && (dctx->nodes == 0 || nodes--)) { dns_rdatasetiter_t *rdsiter = NULL; dns_dbnode_t *node = NULL; result = dns_dbiterator_current(dctx->dbiter, &node, name); if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) break; if (result == DNS_R_NEWORIGIN) { dns_name_t *origin = dns_fixedname_name(&dctx->tctx.origin_fixname); result = dns_dbiterator_origin(dctx->dbiter, origin); RUNTIME_CHECK(result == ISC_R_SUCCESS); if ((dctx->tctx.style.flags & DNS_STYLEFLAG_REL_DATA) != 0) dctx->tctx.origin = origin; dctx->tctx.neworigin = origin; } result = dns_db_allrdatasets(dctx->db, node, dctx->version, dctx->now, &rdsiter); if (result != ISC_R_SUCCESS) { dns_db_detachnode(dctx->db, &node); goto fail; } result = (dctx->dumpsets)(dctx->mctx, name, rdsiter, &dctx->tctx, &buffer, dctx->f); dns_rdatasetiter_destroy(&rdsiter); if (result != ISC_R_SUCCESS) { dns_db_detachnode(dctx->db, &node); goto fail; } dns_db_detachnode(dctx->db, &node); result = dns_dbiterator_next(dctx->dbiter); } /* * Work out how many nodes can be written in the time between * two requests to the nameserver. Smooth the resulting number and * use it as a estimate for the number of nodes to be written in the * next iteration. */ if (dctx->nodes != 0 && result == ISC_R_SUCCESS) { unsigned int pps = dns_pps; /* packets per second */ unsigned int interval; isc_uint64_t usecs; isc_time_t end; isc_time_now(&end); if (pps < 100) pps = 100; interval = 1000000 / pps; /* interval in usecs */ if (interval == 0) interval = 1; usecs = isc_time_microdiff(&end, &start); if (usecs == 0) { dctx->nodes = dctx->nodes * 2; if (dctx->nodes > 1000) dctx->nodes = 1000; } else { nodes = dctx->nodes * interval; nodes /= (unsigned int)usecs; if (nodes == 0) nodes = 1; else if (nodes > 1000) nodes = 1000; /* Smooth and assign. */ dctx->nodes = (nodes + dctx->nodes * 7) / 8; isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL, DNS_LOGMODULE_MASTERDUMP, ISC_LOG_DEBUG(1), "dumptostreaminc(%p) new nodes -> %d\n", dctx, dctx->nodes); } result = dns_dbiterator_pause(dctx->dbiter); RUNTIME_CHECK(result == ISC_R_SUCCESS); result = DNS_R_CONTINUE; } else if (result == ISC_R_NOMORE) result = ISC_R_SUCCESS; fail: isc_mem_put(dctx->mctx, buffer.base, buffer.length); return (result);}isc_result_tdns_master_dumptostreaminc(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version, const dns_master_style_t *style, FILE *f, isc_task_t *task, dns_dumpdonefunc_t done, void *done_arg, dns_dumpctx_t **dctxp){ dns_dumpctx_t *dctx = NULL; isc_result_t result; REQUIRE(task != NULL); REQUIRE(f != NULL); REQUIRE(done != NULL); result = dumpctx_create(mctx, db, version, style, f, &dctx, dns_masterformat_text); if (result != ISC_R_SUCCESS) return (result); isc_task_attach(task, &dctx->task); dctx->done = done; dctx->done_arg = done_arg; dctx->nodes = 100; result = task_send(dctx); if (result == ISC_R_SUCCESS) { dns_dumpctx_attach(dctx, dctxp); return (DNS_R_CONTINUE); } dns_dumpctx_detach(&dctx); return (result);}/* * Dump an entire database into a master file. */isc_result_tdns_master_dumptostream(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version, const dns_master_style_t *style, FILE *f){ return (dns_master_dumptostream2(mctx, db, version, style, dns_masterformat_text, f));}isc_result_tdns_master_dumptostream2(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version, const dns_master_style_t *style, dns_masterformat_t format, FILE *f){ dns_dumpctx_t *dctx = NULL; isc_result_t result; result = dumpctx_create(mctx, db, version, style, f, &dctx, format); if (result != ISC_R_SUCCESS) return (result); result = dumptostreaminc(dctx); INSIST(result != DNS_R_CONTINUE); dns_dumpctx_detach(&dctx); return (result);}static isc_result_topentmp(isc_mem_t *mctx, const char *file, char **tempp, FILE **fp) { FILE *f = NULL; isc_result_t result; char *tempname = NULL; int tempnamelen; tempnamelen = strlen(file) + 20; tempname = isc_mem_allocate(mctx, tempnamelen); if (tempname == NULL) return (ISC_R_NOMEMORY); result = isc_file_mktemplate(file, tempname, tempnamelen); if (result != ISC_R_SUCCESS) goto cleanup; result = isc_file_openunique(tempname, &f); if (result != ISC_R_SUCCESS) { isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL, DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR, "dumping master file: %s: open: %s", tempname, isc_result_totext(result)); goto cleanup; } *tempp = tempname; *fp = f; return (ISC_R_SUCCESS);cleanup: isc_mem_free(mctx, tempname); return (result);}isc_result_tdns_master_dumpinc(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version, const dns_master_style_t *style, const char *filename, isc_task_t *task, dns_dumpdonefunc_t done, void *done_arg, dns_dumpctx_t **dctxp){ return (dns_master_dumpinc2(mctx, db, version, style, filename, task, done, done_arg, dctxp, dns_masterformat_text));}isc_result_tdns_master_dumpinc2(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version, const dns_master_style_t *style, const char *filename, isc_task_t *task, dns_dumpdonefunc_t done, void *done_arg, dns_dumpctx_t **dctxp, dns_masterformat_t format){ FILE *f = NULL; isc_result_t result; char *tempname = NULL; char *file = NULL; dns_dumpctx_t *dctx = NULL; file = isc_mem_strdup(mctx, filename); if (file == NULL) return (ISC_R_NOMEMORY); result = opentmp(mctx, filename, &tempname, &f); if (result != ISC_R_SUCCESS) goto cleanup; result = dumpctx_create(mctx, db, version, style, f, &dctx, format); if (result != ISC_R_SUCCESS) { (void)isc_stdio_close(f); (void)isc_file_remove(tempname); goto cleanup; } isc_task_attach(task, &dctx->task); dctx->done = done; dctx->done_arg = done_arg; dctx->nodes = 100; dctx->file = file; file = NULL; dctx->tmpfile = tempname; tempname = NULL; result = task_send(dctx); if (result == ISC_R_SUCCESS) { dns_dumpctx_attach(dctx, dctxp); return (DNS_R_CONTINUE); } cleanup: if (dctx != NULL) dns_dumpctx_detach(&dctx); if (file != NULL) isc_mem_free(mctx, file); if (tempname != NULL) isc_mem_free(mctx, tempname); return (result);}isc_result_tdns_master_dump(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version, const dns_master_style_t *style, const char *filename){ return (dns_master_dump2(mctx, db, version, style, filename, dns_masterformat_text));}isc_result_tdns_master_dump2(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version, const dns_master_style_t *style, const char *filename, dns_masterformat_t format){ FILE *f = NULL; isc_result_t result; char *tempname; dns_dumpctx_t *dctx = NULL; result = opentmp(mctx, filename, &tempname, &f); if (result != ISC_R_SUCCESS) return (result); result = dumpctx_create(mctx, db, version, style, f, &dctx, format); if (result != ISC_R_SUCCESS) goto cleanup; result = dumptostreaminc(dctx); INSIST(result != DNS_R_CONTINUE); dns_dumpctx_detach(&dctx); result = closeandrename(f, result, tempname, filename); cleanup: isc_mem_free(mctx, tempname); return (result);}/* * Dump a database node into a master file. * XXX: this function assumes the text format. */isc_result_tdns_master_dumpnodetostream(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version, dns_dbnode_t *node, dns_name_t *name, const dns_master_style_t *style, FILE *f){ isc_result_t result; isc_buffer_t buffer; char *bufmem; isc_stdtime_t now; dns_totext_ctx_t ctx; dns_rdatasetiter_t *rdsiter = NULL; result = totext_ctx_init(style, &ctx); if (result != ISC_R_SUCCESS) { UNEXPECTED_ERROR(__FILE__, __LINE__, "could not set master file style"); return (ISC_R_UNEXPECTED); } isc_stdtime_get(&now); bufmem = isc_mem_get(mctx, initial_buffer_length); if (bufmem == NULL) return (ISC_R_NOMEMORY); isc_buffer_init(&buffer, bufmem, initial_buffer_length); result = dns_db_allrdatasets(db, node, version, now, &rdsiter); if (result != ISC_R_SUCCESS) goto failure; result = dump_rdatasets_text(mctx, name, rdsiter, &ctx, &buffer, f); if (result != ISC_R_SUCCESS) goto failure; dns_rdatasetiter_destroy(&rdsiter); result = ISC_R_SUCCESS; failure: isc_mem_put(mctx, buffer.base, buffer.length); return (result);}isc_result_tdns_master_dumpnode(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version, dns_dbnode_t *node, dns_name_t *name, const dns_master_style_t *style, const char *filename){ FILE *f = NULL; isc_result_t result; result = isc_stdio_open(filename, "w", &f); if (result != ISC_R_SUCCESS) { isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL, DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR, "dumping node to file: %s: open: %s", filename, isc_result_totext(result)); return (ISC_R_UNEXPECTED); } result = dns_master_dumpnodetostream(mctx, db, version, node, name, style, f); result = isc_stdio_close(f); if (result != ISC_R_SUCCESS) { isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL, DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR, "dumping master file: %s: close: %s", filename, isc_result_totext(result)); return (ISC_R_UNEXPECTED); } return (result);}isc_result_tdns_master_stylecreate(dns_master_style_t **stylep, unsigned int flags, unsigned int ttl_column, unsigned int class_column, unsigned int type_column, unsigned int rdata_column, unsigned int line_length, unsigned int tab_width, isc_mem_t *mctx){ dns_master_style_t *style; REQUIRE(stylep != NULL && *stylep == NULL); style = isc_mem_get(mctx, sizeof(*style)); if (style == NULL) return (ISC_R_NOMEMORY); style->flags = flags; style->ttl_column = ttl_column; style->class_column = class_column; style->type_column = type_column; style->rdata_column = rdata_column; style->line_length = line_length; style->tab_width = tab_width; *stylep = style; return (ISC_R_SUCCESS);}voiddns_master_styledestroy(dns_master_style_t **stylep, isc_mem_t *mctx) { dns_master_style_t *style; REQUIRE(stylep != NULL && *stylep != NULL); style = *stylep; *stylep = NULL; isc_mem_put(mctx, style, sizeof(*style));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -