File: plugins.c

package info (click to toggle)
nsd 2.3.6-1%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 1,268 kB
  • ctags: 1,840
  • sloc: ansic: 11,343; yacc: 742; makefile: 277; sh: 262; perl: 238
file content (230 lines) | stat: -rw-r--r-- 5,501 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * plugins.c -- set of routines to manage plugins.
 *
 * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
 *
 * See LICENSE for the license.
 *
 */

#include <config.h>

#ifdef PLUGINS

#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>

#include "nsd.h"
#include "plugins.h"

nsd_plugin_id_type maximum_plugin_count = 0;
static nsd_plugin_id_type plugin_count = 0;

struct nsd_plugin
{
	struct nsd_plugin *next;
	void *handle;
	nsd_plugin_id_type id;
	const nsd_plugin_descriptor_type *descriptor;
};
typedef struct nsd_plugin nsd_plugin_type;

static nsd_plugin_type *first_plugin = NULL;
static nsd_plugin_type **last_plugin = &first_plugin;

static int
register_data(
	const nsd_plugin_interface_type *nsd,
	nsd_plugin_id_type               plugin_id,
	const dname_type *               domain_name,
	void *                           data)
{
	domain_type *domain;
	
	assert(plugin_id < maximum_plugin_count);
	assert(domain_name);

	domain = domain_table_find(nsd->nsd->db->domains, domain_name);
	if (!domain)
		return 0;

	if (!domain->plugin_data) {
		domain->plugin_data
			= (void **) region_alloc_zero(
				nsd->nsd->db->region,
				maximum_plugin_count * sizeof(void *));
	}
	domain->plugin_data[plugin_id] = data;
	
	return 1;
}

static nsd_plugin_interface_type plugin_interface;

void
plugin_init(struct nsd *nsd)
{
	plugin_interface.nsd = nsd;
	plugin_interface.root_dname = dname_make(nsd->region, (const uint8_t *) "", 0);
	plugin_interface.register_data = register_data;
	plugin_interface.log_msg = log_msg;
	plugin_interface.xalloc = xalloc;
	plugin_interface.xrealloc = xrealloc;
	plugin_interface.free = free;
	plugin_interface.region_create = region_create;
	plugin_interface.region_destroy = region_destroy;
	plugin_interface.region_alloc = region_alloc;
	plugin_interface.region_free_all = region_free_all;
	plugin_interface.dname_parse = dname_parse;
	plugin_interface.dname_to_string = dname_to_string;
}

#define STR2(x) #x
#define STR(x) STR2(x)
int
plugin_load(struct nsd *nsd, const char *name, const char *arg)
{
	struct nsd_plugin *plugin;
	nsd_plugin_init_type *init;
	void *handle;
	const char *error;
	const char *init_name = STR(NSD_PLUGIN_INIT);
	const nsd_plugin_descriptor_type *descriptor;

	dlerror();		/* Discard previous errors (FreeBSD hack).  */
	
	handle = dlopen(name, RTLD_NOW);
	error = dlerror();
	if (error) {
		log_msg(LOG_ERR, "failed to load plugin: %s", error);
		return 0;
	}

	init = (nsd_plugin_init_type *) dlsym(handle, init_name);
	error = dlerror();
	if (error) {
		log_msg(LOG_ERR, "no plugin init function: %s", error);
		dlclose(handle);
		return 0;
	}

	descriptor = init(&plugin_interface, plugin_count, arg);
	if (!descriptor) {
		log_msg(LOG_ERR, "plugin initialization failed");
		dlclose(handle);
		return 0;
	}

	plugin = (nsd_plugin_type *) region_alloc(nsd->region,
					     sizeof(struct nsd_plugin));
	plugin->next = NULL;
	plugin->handle = handle;
	plugin->id = plugin_count;
	plugin->descriptor = descriptor;

	assert(*last_plugin == NULL);
	*last_plugin = plugin;
	last_plugin = &plugin->next;
	assert(*last_plugin == NULL);

	++plugin_count;
	
	log_msg(LOG_INFO, "Plugin %s %s loaded", descriptor->name, descriptor->version);
	
	return 1;
}

void
plugin_finalize_all(void)
{
	nsd_plugin_type *plugin;
	nsd_plugin_type *next;

	plugin = first_plugin;
	while (plugin) {
		if (plugin->descriptor->finalize) {
			plugin->descriptor->finalize(&plugin_interface,
						     plugin->id);
		}
		dlclose(plugin->handle);
		next = plugin->next;
		free(plugin);
		plugin = next;
	}
}

nsd_plugin_callback_result_type
plugin_database_reloaded(void)
{
	nsd_plugin_callback_result_type rc;
	nsd_plugin_type *plugin;

	for (plugin = first_plugin; plugin; plugin = plugin->next) {
		if (plugin->descriptor->reload) {
			rc = plugin->descriptor->reload(&plugin_interface,
							plugin->id);
			if (rc != NSD_PLUGIN_CONTINUE)
				return rc;
		}
	}
	return NSD_PLUGIN_CONTINUE;
}

#define MAKE_PERFORM_CALLBACKS(function_name, callback_name)		\
nsd_plugin_callback_result_type						\
function_name(								\
	nsd_plugin_callback_args_type *args,				\
	void **data)							\
{									\
	nsd_plugin_type *plugin;					\
	nsd_plugin_callback_type *callback;				\
	nsd_plugin_callback_result_type result;				\
									\
	args->data = NULL;						\
	for (plugin = first_plugin; plugin; plugin = plugin->next) {	\
		callback = plugin->descriptor->callback_name;		\
		if (callback) {						\
			if (data) {					\
				args->data = data[plugin->id];		\
			} else {					\
				args->data = NULL;			\
			}						\
			result = callback(&plugin_interface,		\
					  plugin->id, args);		\
			if (result != NSD_PLUGIN_CONTINUE) {		\
				return result;				\
			}						\
		}							\
	}								\
									\
	return NSD_PLUGIN_CONTINUE;					\
}

MAKE_PERFORM_CALLBACKS(query_received_callbacks, query_received)
MAKE_PERFORM_CALLBACKS(query_processed_callbacks, query_processed)

query_state_type
handle_callback_result(
	nsd_plugin_callback_result_type result,
	nsd_plugin_callback_args_type *args)
{
	switch (result) {
	case NSD_PLUGIN_CONTINUE:
	case NSD_PLUGIN_ANSWER:
		return QUERY_PROCESSED;
	case NSD_PLUGIN_ERROR:
		return query_error(args->query, args->result_code);
	case NSD_PLUGIN_ABANDON:
		return QUERY_DISCARDED;
	default:
		log_msg(LOG_WARNING, "bad callback result code %d from plugin",
			(int) result);
		return QUERY_DISCARDED;
	}
}

#endif /* PLUGINS */