fcd95ed6bcc638c7eaf1eadfa60d0c3597a39c3f
[strongswan.git] / src / libcharon / plugins / vici / vici_plugin.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
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 "vici_plugin.h"
17 #include "vici_dispatcher.h"
18 #include "vici_query.h"
19 #include "vici_control.h"
20 #include "vici_cred.h"
21 #include "vici_config.h"
22 #include "vici_logger.h"
23
24 #include <library.h>
25 #include <daemon.h>
26
27 typedef struct private_vici_plugin_t private_vici_plugin_t;
28
29 /**
30 * Private members of vici_plugin_t
31 */
32 struct private_vici_plugin_t {
33
34 /**
35 * public functions
36 */
37 vici_plugin_t public;
38
39 /**
40 * Dispatcher, creating socket
41 */
42 vici_dispatcher_t *dispatcher;
43
44 /**
45 * Query commands
46 */
47 vici_query_t *query;
48
49 /**
50 * Control commands
51 */
52 vici_control_t *control;
53
54 /**
55 * Credential backend
56 */
57 vici_cred_t *cred;
58
59 /**
60 * Configuration backend
61 */
62 vici_config_t *config;
63
64 /**
65 * Generic debug logger
66 */
67 vici_logger_t *logger;
68 };
69
70 METHOD(plugin_t, get_name, char*,
71 private_vici_plugin_t *this)
72 {
73 return "vici";
74 }
75
76 /**
77 * Register vici plugin features
78 */
79 static bool register_vici(private_vici_plugin_t *this,
80 plugin_feature_t *feature, bool reg, void *data)
81 {
82 if (reg)
83 {
84 char *uri;
85
86 uri = lib->settings->get_str(lib->settings, "%s.plugins.vici.socket",
87 VICI_DEFAULT_URI, lib->ns);
88 this->dispatcher = vici_dispatcher_create(uri);
89 if (this->dispatcher)
90 {
91 this->query = vici_query_create(this->dispatcher);
92 this->control = vici_control_create(this->dispatcher);
93 this->cred = vici_cred_create(this->dispatcher);
94 this->config = vici_config_create(this->dispatcher);
95 this->logger = vici_logger_create(this->dispatcher);
96
97 charon->backends->add_backend(charon->backends,
98 &this->config->backend);
99 charon->bus->add_logger(charon->bus, &this->logger->logger);
100 return TRUE;
101 }
102 return FALSE;
103 }
104 else
105 {
106 charon->bus->remove_logger(charon->bus, &this->logger->logger);
107 charon->backends->remove_backend(charon->backends,
108 &this->config->backend);
109
110 this->logger->destroy(this->logger);
111 this->config->destroy(this->config);
112 this->cred->destroy(this->cred);
113 this->control->destroy(this->control);
114 this->query->destroy(this->query);
115 this->dispatcher->destroy(this->dispatcher);
116 }
117 return TRUE;
118 }
119
120 METHOD(plugin_t, get_features, int,
121 private_vici_plugin_t *this, plugin_feature_t *features[])
122 {
123 static plugin_feature_t f[] = {
124 PLUGIN_CALLBACK((plugin_feature_callback_t)register_vici, NULL),
125 PLUGIN_PROVIDE(CUSTOM, "vici"),
126 };
127 *features = f;
128 return countof(f);
129 }
130
131 METHOD(plugin_t, destroy, void,
132 private_vici_plugin_t *this)
133 {
134 free(this);
135 }
136
137 /*
138 * see header file
139 */
140 plugin_t *vici_plugin_create()
141 {
142 private_vici_plugin_t *this;
143
144 INIT(this,
145 .public = {
146 .plugin = {
147 .get_name = _get_name,
148 .reload = (void*)return_false,
149 .get_features = _get_features,
150 .destroy = _destroy,
151 },
152 },
153 );
154
155 return &this->public.plugin;
156 }