vici: Add a control backend, currently to initiate connections by name
[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
21 #include <library.h>
22 #include <daemon.h>
23
24 typedef struct private_vici_plugin_t private_vici_plugin_t;
25
26 /**
27 * Private members of vici_plugin_t
28 */
29 struct private_vici_plugin_t {
30
31 /**
32 * public functions
33 */
34 vici_plugin_t public;
35
36 /**
37 * Dispatcher, creating socket
38 */
39 vici_dispatcher_t *dispatcher;
40
41 /**
42 * Query commands
43 */
44 vici_query_t *query;
45
46 /**
47 * Control commands
48 */
49 vici_control_t *control;
50 };
51
52 METHOD(plugin_t, get_name, char*,
53 private_vici_plugin_t *this)
54 {
55 return "vici";
56 }
57
58 /**
59 * Register vici plugin features
60 */
61 static bool register_vici(private_vici_plugin_t *this,
62 plugin_feature_t *feature, bool reg, void *data)
63 {
64 if (reg)
65 {
66 char *uri;
67
68 uri = lib->settings->get_str(lib->settings, "%s.plugins.vici.socket",
69 VICI_DEFAULT_URI, lib->ns);
70 this->dispatcher = vici_dispatcher_create(uri);
71 if (this->dispatcher)
72 {
73 this->query = vici_query_create(this->dispatcher);
74 this->control = vici_control_create(this->dispatcher);
75 return TRUE;
76 }
77 return FALSE;
78 }
79 else
80 {
81 this->control->destroy(this->control);
82 this->query->destroy(this->query);
83 this->dispatcher->destroy(this->dispatcher);
84 }
85 return TRUE;
86 }
87
88 METHOD(plugin_t, get_features, int,
89 private_vici_plugin_t *this, plugin_feature_t *features[])
90 {
91 static plugin_feature_t f[] = {
92 PLUGIN_CALLBACK((plugin_feature_callback_t)register_vici, NULL),
93 PLUGIN_PROVIDE(CUSTOM, "vici"),
94 };
95 *features = f;
96 return countof(f);
97 }
98
99 METHOD(plugin_t, destroy, void,
100 private_vici_plugin_t *this)
101 {
102 free(this);
103 }
104
105 /*
106 * see header file
107 */
108 plugin_t *vici_plugin_create()
109 {
110 private_vici_plugin_t *this;
111
112 INIT(this,
113 .public = {
114 .plugin = {
115 .get_name = _get_name,
116 .reload = (void*)return_false,
117 .get_features = _get_features,
118 .destroy = _destroy,
119 },
120 },
121 );
122
123 return &this->public.plugin;
124 }