sdlz_helper.c

来自「非常好的dns解析软件」· C语言 代码 · 共 532 行 · 第 1/2 页

C
532
字号
	return (ISC_R_SUCCESS); cleanup:	/* get rid of temp_str */	if (temp_str != NULL)		isc_mem_free(mctx, temp_str); flag_fail:	/* get rid of what was build of the query list */	if (tql != NULL)		destroy_querylist(mctx, &tql);	return result;}/*% * build a query string from query segments, and dynamic segments * dynamic segments replace where the tokens %zone%, %record%, %client% * used to be in our queries from named.conf */char *sdlzh_build_querystring(isc_mem_t *mctx, query_list_t *querylist){	query_segment_t *tseg = NULL;	unsigned int length = 0;	char *qs = NULL;	REQUIRE(mctx != NULL);	REQUIRE(querylist != NULL);	/* start at the top of the list */	tseg = ISC_LIST_HEAD(*querylist);	while (tseg != NULL) {		/*		 * if this is a query segment, use the		 * precalculated string length		 */		if (tseg->direct == isc_boolean_true)			length += tseg->strlen;		else	/* calculate string length for dynamic segments. */			length += strlen(* (char**) tseg->sql);		/* get the next segment */		tseg = ISC_LIST_NEXT(tseg, link);	}	/* allocate memory for the string */	qs = isc_mem_allocate(mctx, length + 1);	/* couldn't allocate memory,  We need more ram! */	if (qs == NULL)		return NULL;	/* start at the top of the list again */	tseg = ISC_LIST_HEAD(*querylist);	/* copy the first item in the list to the query string */	if (tseg->direct == isc_boolean_true)	/* query segment */		strcpy(qs, tseg->sql);	else		strcpy(qs, * (char**) tseg->sql); /* dynamic segment */	/* concatonate the rest of the segments */	while ((tseg = ISC_LIST_NEXT(tseg, link)) != NULL) {		if (tseg->direct == isc_boolean_true)			/* query segments */			strcat(qs, tseg->sql);		else			/* dynamic segments */			strcat(qs, * (char**) tseg->sql);	}	return qs;}/*% constructs a sql dbinstance (DBI) */isc_result_tsdlzh_build_sqldbinstance(isc_mem_t *mctx, const char *allnodes_str,			 const char *allowxfr_str, const char *authority_str,			 const char *findzone_str, const char *lookup_str,			 const char *countzone_str, dbinstance_t **dbi){	isc_result_t result;	dbinstance_t *db = NULL;	REQUIRE(dbi != NULL && *dbi == NULL);	REQUIRE(mctx != NULL);	/* allocate and zero memory for driver structure */	db = isc_mem_get(mctx, sizeof(dbinstance_t));	if (db == NULL) {		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,			      "Could not allocate memory for "			      "database instance object.");		return (ISC_R_NOMEMORY);	}	memset(db, 0, sizeof(dbinstance_t));	db->dbconn = NULL;	db->client = NULL;	db->record = NULL;	db->zone = NULL;	db->mctx = NULL;	db->query_buf = NULL;	db->allnodes_q = NULL;	db->allowxfr_q = NULL;	db->authority_q = NULL;	db->findzone_q = NULL;	db->countzone_q = NULL;	db->lookup_q = NULL;	/* attach to the memory context */	isc_mem_attach(mctx, &db->mctx);	/* initialize the reference count mutex */	result = isc_mutex_init(&db->instance_lock);	if (result != ISC_R_SUCCESS) {		UNEXPECTED_ERROR(__FILE__, __LINE__,				 "isc_mutex_init() failed: %s",				 isc_result_totext(result));		goto cleanup;	}	/* build the all nodes query list */	result = build_querylist(mctx, allnodes_str, &db->zone,				 &db->record, &db->client,				 &db->allnodes_q, SDLZH_REQUIRE_ZONE);	/* if unsuccessful, log err msg and cleanup */	if (result != ISC_R_SUCCESS) {		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,			      "Could not build all nodes query list");		goto cleanup;	}	/* build the allow zone transfer query list */	result = build_querylist(mctx, allowxfr_str, &db->zone,				 &db->record, &db->client,				 &db->allowxfr_q,				 SDLZH_REQUIRE_ZONE | SDLZH_REQUIRE_CLIENT);	/* if unsuccessful, log err msg and cleanup */	if (result != ISC_R_SUCCESS) {		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,			      "Could not build allow xfr query list");		goto cleanup;	}	/* build the authority query, query list */	result = build_querylist(mctx, authority_str, &db->zone,				 &db->record, &db->client,				 &db->authority_q, SDLZH_REQUIRE_ZONE);	/* if unsuccessful, log err msg and cleanup */	if (result != ISC_R_SUCCESS) {		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,			      "Could not build authority query list");		goto cleanup;	}	/* build findzone query, query list */	result = build_querylist(mctx, findzone_str, &db->zone,				 &db->record, &db->client,				 &db->findzone_q, SDLZH_REQUIRE_ZONE);	/* if unsuccessful, log err msg and cleanup */	if (result != ISC_R_SUCCESS) {		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,			      "Could not build find zone query list");		goto cleanup;	}	/* build countzone query, query list */	result = build_querylist(mctx, countzone_str, &db->zone,				 &db->record, &db->client,				 &db->countzone_q, SDLZH_REQUIRE_ZONE);	/* if unsuccessful, log err msg and cleanup */	if (result != ISC_R_SUCCESS) {		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,			      "Could not build count zone query list");		goto cleanup;	}	/* build lookup query, query list */	result = build_querylist(mctx, lookup_str, &db->zone,				 &db->record, &db->client,				 &db->lookup_q, SDLZH_REQUIRE_RECORD);	/* if unsuccessful, log err msg and cleanup */	if (result != ISC_R_SUCCESS) {		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,			      "Could not build lookup query list");		goto cleanup;	}	/* pass back the db instance */	*dbi = (dbinstance_t *) db;	/* return success */	return (ISC_R_SUCCESS); cleanup:	/* destroy whatever was build of the db instance */	destroy_sqldbinstance(db);	/* return failure */	return (ISC_R_FAILURE);}voidsdlzh_destroy_sqldbinstance(dbinstance_t *dbi){	isc_mem_t *mctx;	/* save mctx for later */	mctx = dbi->mctx;	/* destroy any query lists we created */	destroy_querylist(mctx, &dbi->allnodes_q);	destroy_querylist(mctx, &dbi->allowxfr_q);	destroy_querylist(mctx, &dbi->authority_q);	destroy_querylist(mctx, &dbi->findzone_q);	destroy_querylist(mctx, &dbi->countzone_q);	destroy_querylist(mctx, &dbi->lookup_q);	/* get rid of the mutex */	isc_mutex_destroy(&dbi->instance_lock);	/* return, and detach the memory */	isc_mem_put(mctx, dbi, sizeof(dbinstance_t));	isc_mem_detach(&mctx);}char *sdlzh_get_parameter_value(isc_mem_t *mctx, const char *input, const char* key){	int keylen;	char *keystart;	char value[255];	int i;	if (key == NULL || input == NULL || strlen(input) < 1)		return NULL;	keylen = strlen(key);	if (keylen < 1)		return NULL;	keystart = strstr(input, key);	if (keystart == NULL)		return NULL;	REQUIRE(mctx != NULL);	for (i = 0; i < 255; i++) {		value[i] = keystart[keylen + i];		if (value[i] == ' ' || value[i] == '\0') {			value[i] = '\0';			break;		}	}	return isc_mem_strdup(mctx, value);}#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?