3f276f93e93b1b82ee2706173c7802742f261912
[strongswan.git] / src / libcharon / plugins / connmark / connmark_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 "connmark_plugin.h"
17 #include "connmark_listener.h"
18
19 #include <daemon.h>
20
21 typedef struct private_connmark_plugin_t private_connmark_plugin_t;
22
23 /**
24 * private data of connmark plugin
25 */
26 struct private_connmark_plugin_t {
27
28 /**
29 * implements plugin interface
30 */
31 connmark_plugin_t public;
32
33 /**
34 * Listener installing netfilter rules
35 */
36 connmark_listener_t *listener;
37 };
38
39 METHOD(plugin_t, get_name, char*,
40 private_connmark_plugin_t *this)
41 {
42 return "connmark";
43 }
44
45 /**
46 * Register listener
47 */
48 static bool plugin_cb(private_connmark_plugin_t *this,
49 plugin_feature_t *feature, bool reg, void *cb_data)
50 {
51 if (reg)
52 {
53 charon->bus->add_listener(charon->bus, &this->listener->listener);
54 }
55 else
56 {
57 charon->bus->remove_listener(charon->bus, &this->listener->listener);
58 }
59 return TRUE;
60 }
61
62 METHOD(plugin_t, get_features, int,
63 private_connmark_plugin_t *this, plugin_feature_t *features[])
64 {
65 static plugin_feature_t f[] = {
66 PLUGIN_CALLBACK((plugin_feature_callback_t)plugin_cb, NULL),
67 PLUGIN_PROVIDE(CUSTOM, "connmark"),
68 };
69 *features = f;
70 return countof(f);
71 }
72
73 METHOD(plugin_t, destroy, void,
74 private_connmark_plugin_t *this)
75 {
76 this->listener->destroy(this->listener);
77 free(this);
78 }
79
80 /**
81 * Plugin constructor
82 */
83 plugin_t *connmark_plugin_create()
84 {
85 private_connmark_plugin_t *this;
86
87 if (!lib->caps->keep(lib->caps, CAP_NET_ADMIN))
88 {
89 DBG1(DBG_NET, "connmark plugin requires CAP_NET_ADMIN capability");
90 return NULL;
91 }
92
93 INIT(this,
94 .public = {
95 .plugin = {
96 .get_name = _get_name,
97 .get_features = _get_features,
98 .destroy = _destroy,
99 },
100 },
101 .listener = connmark_listener_create(),
102 );
103
104 return &this->public.plugin;
105 }