File: example-plugin.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 (115 lines) | stat: -rw-r--r-- 2,645 bytes parent folder | download | duplicates (4)
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
/*
 * Example plugin for NSD.
 *
 * Compile with something like:
 *
 *   gcc -shared -Insd-src-dir example-plugin.c -o example-plugin.so
 *
 */

#include <nsd-plugin.h>

#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

static nsd_plugin_callback_result_type reload(
	const nsd_plugin_interface_type *nsd,
	nsd_plugin_id_type id);
static nsd_plugin_callback_type query_received;
static nsd_plugin_callback_type query_processed;

static
void finalize(const nsd_plugin_interface_type *nsd,
	      nsd_plugin_id_type               id ATTR_UNUSED)
{
	nsd->log_msg(LOG_NOTICE, "finalizing plugin");
}

/*
 * Define the plugin descriptor.
 */
static nsd_plugin_descriptor_type descriptor = {
	"Example plugin",
	"0.1",
	finalize,
	reload,
	query_received,
	query_processed
};

const nsd_plugin_descriptor_type *
NSD_PLUGIN_INIT(const nsd_plugin_interface_type *nsd,
		nsd_plugin_id_type id,
                const char *arg)
{
	nsd->log_msg(LOG_NOTICE, "Example plugin initializing (arg = %s)", arg);

	if (reload(nsd, id) != NSD_PLUGIN_CONTINUE) {
		return NULL;
	} else {
		return &descriptor;
	}
}

static nsd_plugin_callback_result_type
reload(const nsd_plugin_interface_type *nsd,
       nsd_plugin_id_type id)
{
	nsd->log_msg(LOG_NOTICE, "registering data");
	if (!nsd->register_data(nsd, id,
				nsd->dname_parse(nsd->nsd->db->region,
						 "nl",
						 nsd->root_dname),
				"hello, world!"))
	{
		nsd->log_msg(LOG_ERR, "Failed to register data");
		return NSD_PLUGIN_ERROR;
	} else {
		return NSD_PLUGIN_CONTINUE;
	}
}


static nsd_plugin_callback_result_type
query_received(
	const nsd_plugin_interface_type *nsd,
	nsd_plugin_id_type               id ATTR_UNUSED,
	nsd_plugin_callback_args_type   *args ATTR_UNUSED)
{
	switch (fork()) {
	case -1:
		/* error */
		nsd->log_msg(LOG_ERR, "Plugin fork failed: %s", strerror(errno));
		return NSD_PLUGIN_ABANDON;
	case 0:
		/* child */
		nsd->log_msg(LOG_NOTICE, "Plugin forked child");
		exit(0);
	default:
		/* parent */
		return NSD_PLUGIN_CONTINUE;
	}
}

static nsd_plugin_callback_result_type
query_processed(
	const nsd_plugin_interface_type *nsd,
	nsd_plugin_id_type               id ATTR_UNUSED,
	nsd_plugin_callback_args_type   *args)
{
	nsd->log_msg(LOG_NOTICE, "domain name: %s", nsd->dname_to_string(args->query->name));
	
	if (args->data) {
		nsd->log_msg(LOG_NOTICE, "Received query with plugin data %s",
			     (char *) args->data);
		args->result_code = RCODE_FORMAT;
		return NSD_PLUGIN_ERROR;
	} else {
		nsd->log_msg(LOG_NOTICE, "Received query without plugin data");
		return NSD_PLUGIN_CONTINUE;
	}
}