updown: Use plugin features to register listener and attribute handler
[strongswan.git] / src / libcharon / plugins / updown / updown_plugin.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "updown_plugin.h"
17 #include "updown_listener.h"
18 #include "updown_handler.h"
19
20 #include <daemon.h>
21 #include <hydra.h>
22
23 typedef struct private_updown_plugin_t private_updown_plugin_t;
24
25 /**
26 * private data of updown plugin
27 */
28 struct private_updown_plugin_t {
29
30 /**
31 * implements plugin interface
32 */
33 updown_plugin_t public;
34
35 /**
36 * Listener interface, listens to CHILD_SA state changes
37 */
38 updown_listener_t *listener;
39
40 /**
41 * Attribute handler, to pass DNS servers to updown
42 */
43 updown_handler_t *handler;
44 };
45
46 METHOD(plugin_t, get_name, char*,
47 private_updown_plugin_t *this)
48 {
49 return "updown";
50 }
51
52 /**
53 * Register listener
54 */
55 static bool plugin_cb(private_updown_plugin_t *this,
56 plugin_feature_t *feature, bool reg, void *cb_data)
57 {
58 if (reg)
59 {
60 if (lib->settings->get_bool(lib->settings,
61 "charon.plugins.updown.dns_handler", FALSE))
62 {
63 this->handler = updown_handler_create();
64 hydra->attributes->add_handler(hydra->attributes,
65 &this->handler->handler);
66 }
67 this->listener = updown_listener_create(this->handler);
68 charon->bus->add_listener(charon->bus, &this->listener->listener);
69 }
70 else
71 {
72 charon->bus->remove_listener(charon->bus, &this->listener->listener);
73 this->listener->destroy(this->listener);
74 if (this->handler)
75 {
76 this->handler->destroy(this->handler);
77 hydra->attributes->remove_handler(hydra->attributes,
78 &this->handler->handler);
79 }
80 }
81 return TRUE;
82 }
83
84 METHOD(plugin_t, get_features, int,
85 private_updown_plugin_t *this, plugin_feature_t *features[])
86 {
87 static plugin_feature_t f[] = {
88 PLUGIN_CALLBACK((plugin_feature_callback_t)plugin_cb, NULL),
89 PLUGIN_PROVIDE(CUSTOM, "updown"),
90 };
91 *features = f;
92 return countof(f);
93 }
94
95 METHOD(plugin_t, destroy, void,
96 private_updown_plugin_t *this)
97 {
98 free(this);
99 }
100
101 /*
102 * see header file
103 */
104 plugin_t *updown_plugin_create()
105 {
106 private_updown_plugin_t *this;
107
108 INIT(this,
109 .public = {
110 .plugin = {
111 .get_name = _get_name,
112 .get_features = _get_features,
113 .destroy = _destroy,
114 },
115 },
116 );
117
118 return &this->public.plugin;
119 }