Merge branch 'nm-gtk4'
[strongswan.git] / src / libcharon / plugins / dhcp / dhcp_plugin.c
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2010 Martin Willi
6 * Copyright (C) 2010 revosec AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include "dhcp_plugin.h"
20
21 #include <daemon.h>
22 #include <plugins/plugin_feature.h>
23
24 #include "dhcp_socket.h"
25 #include "dhcp_provider.h"
26
27 typedef struct private_dhcp_plugin_t private_dhcp_plugin_t;
28
29 /**
30 * private data of dhcp plugin
31 */
32 struct private_dhcp_plugin_t {
33
34 /**
35 * implements plugin interface
36 */
37 dhcp_plugin_t public;
38
39 /**
40 * DHCP communication socket
41 */
42 dhcp_socket_t *socket;
43
44 /**
45 * Attribute provider
46 */
47 dhcp_provider_t *provider;
48 };
49
50 METHOD(plugin_t, get_name, char*,
51 private_dhcp_plugin_t *this)
52 {
53 return "dhcp";
54 }
55
56 /**
57 * Register listener
58 */
59 static bool plugin_cb(private_dhcp_plugin_t *this,
60 plugin_feature_t *feature, bool reg, void *cb_data)
61 {
62 if (reg)
63 {
64 this->socket = dhcp_socket_create();
65
66 if (!this->socket)
67 {
68 return FALSE;
69 }
70 this->provider = dhcp_provider_create(this->socket);
71 charon->attributes->add_provider(charon->attributes,
72 &this->provider->provider);
73 }
74 else
75 {
76 charon->attributes->remove_provider(charon->attributes,
77 &this->provider->provider);
78 this->provider->destroy(this->provider);
79 this->socket->destroy(this->socket);
80 }
81 return TRUE;
82 }
83
84 METHOD(plugin_t, get_features, int,
85 private_dhcp_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, "dhcp"),
90 PLUGIN_DEPENDS(RNG, RNG_WEAK),
91 };
92 *features = f;
93 return countof(f);
94 }
95
96 METHOD(plugin_t, destroy, void,
97 private_dhcp_plugin_t *this)
98 {
99 free(this);
100 }
101
102 /**
103 * Plugin constructor.
104 */
105 plugin_t *dhcp_plugin_create()
106 {
107 private_dhcp_plugin_t *this;
108
109 if (!lib->caps->check(lib->caps, CAP_NET_BIND_SERVICE))
110 { /* required to bind DHCP socket (port 68) */
111 DBG1(DBG_NET, "dhcp plugin requires CAP_NET_BIND_SERVICE capability");
112 return NULL;
113 }
114 else if (!lib->caps->keep(lib->caps, CAP_NET_RAW))
115 { /* required to open DHCP receive socket (AF_PACKET). according to
116 * capabilities(7) it is also required to use the socket */
117 DBG1(DBG_NET, "dhcp plugin requires CAP_NET_RAW capability");
118 return NULL;
119 }
120
121 INIT(this,
122 .public = {
123 .plugin = {
124 .get_name = _get_name,
125 .get_features = _get_features,
126 .destroy = _destroy,
127 },
128 },
129 );
130
131 return &this->public.plugin;
132 }