fixed casting
[strongswan.git] / src / charon / control / interfaces / dbus_interface.c
1 /**
2 * @file dbus_interface.c
3 *
4 * @brief Implementation of dbus_interface_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #define DBUS_API_SUBJECT_TO_CHANGE
24 #include <dbus/dbus.h>
25 #include <NetworkManager/NetworkManager.h>
26 #include <NetworkManager/NetworkManagerVPN.h>
27 #include <stdlib.h>
28
29 #include "dbus_interface.h"
30
31 #include <library.h>
32 #include <daemon.h>
33
34
35 #define NM_DBUS_SERVICE_STRONG "org.freedesktop.NetworkManager.strongswan"
36 #define NM_DBUS_INTERFACE_STRONG "org.freedesktop.NetworkManager.strongswan"
37 #define NM_DBUS_PATH_STRONG "/org/freedesktop/NetworkManager/strongswan"
38
39 typedef struct private_dbus_interface_t private_dbus_interface_t;
40
41 /**
42 * Private data of an dbus_interface_t object.
43 */
44 struct private_dbus_interface_t {
45
46 /**
47 * Public part of dbus_t object.
48 */
49 dbus_interface_t public;
50
51 /**
52 * DBUS connection
53 */
54 DBusConnection* conn;
55
56 /**
57 * error value used here and there
58 */
59 DBusError err;
60
61 /**
62 * state of the daemon
63 */
64 NMVPNState state;
65
66 /**
67 * dispatcher thread for DBUS messages
68 */
69 pthread_t thread;
70 };
71
72 /**
73 * set daemon state and send StateChange signal to the bus
74 */
75 static void set_state(private_dbus_interface_t *this, NMVPNState state)
76 {
77 DBusMessage* msg;
78
79 msg = dbus_message_new_signal(NM_DBUS_PATH_STRONG, NM_DBUS_INTERFACE_STRONG, NM_DBUS_VPN_SIGNAL_STATE_CHANGE);
80
81 if (!dbus_message_append_args(msg, DBUS_TYPE_UINT32, &this->state,
82 DBUS_TYPE_UINT32, &state, DBUS_TYPE_INVALID) ||
83 !dbus_connection_send(this->conn, msg, NULL))
84 {
85 DBG1(DBG_CFG, "unable to send DBUS StateChange signal");
86 }
87 dbus_connection_flush(this->conn);
88 dbus_message_unref(msg);
89 this->state = state;
90 }
91
92 /**
93 * process NetworkManagers startConnection method call
94 */
95 static bool start_connection(private_dbus_interface_t *this, DBusMessage* msg)
96 {
97 DBusMessage *reply, *signal;
98 char *name, *user, **data, **passwords, **routes;
99 int data_count, passwords_count, routes_count;
100 u_int32_t me, other, p2p, netmask, mss;
101 char *dev, *domain, *banner;
102 const dbus_int32_t array[] = {};
103 const dbus_int32_t *varray = array;
104
105 if (!dbus_message_get_args(msg, &this->err,
106 DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &user,
107 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &passwords, &passwords_count,
108 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &data, &data_count,
109 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &routes, &routes_count,
110 DBUS_TYPE_INVALID))
111 {
112 return FALSE;
113 }
114 set_state(this, NM_VPN_STATE_STARTING);
115
116 reply = dbus_message_new_method_return(msg);
117 dbus_connection_send(this->conn, reply, NULL);
118
119 signal = dbus_message_new_signal(NM_DBUS_PATH_STRONG,
120 NM_DBUS_INTERFACE_STRONG,
121 NM_DBUS_VPN_SIGNAL_IP4_CONFIG);
122
123 me = other = p2p = mss = netmask = 0;
124 dev = domain = banner = "";
125 if (dbus_message_append_args(signal,
126 DBUS_TYPE_UINT32, &other,
127 DBUS_TYPE_STRING, &dev,
128 DBUS_TYPE_UINT32, &me,
129 DBUS_TYPE_UINT32, &p2p,
130 DBUS_TYPE_UINT32, &netmask,
131 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &varray, 0,
132 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &varray, 0,
133 DBUS_TYPE_UINT32, &mss,
134 DBUS_TYPE_STRING, &domain,
135 DBUS_TYPE_STRING, &banner))
136 {
137 dbus_connection_send(this->conn, signal, NULL);
138 }
139 dbus_message_unref(signal);
140
141 set_state(this, NM_VPN_STATE_STARTED);
142
143 dbus_connection_flush(this->conn);
144 dbus_message_unref(reply);
145 return TRUE;
146 }
147
148 /**
149 * process NetworkManagers stopConnection method call
150 */
151 static bool stop_connection(private_dbus_interface_t *this, DBusMessage* msg)
152 {
153 set_state(this, NM_VPN_STATE_STOPPING);
154 set_state(this, NM_VPN_STATE_STOPPED);
155 return FALSE;
156 }
157
158 /**
159 * process NetworkManagers getState method call
160 */
161 static bool get_state(private_dbus_interface_t *this, DBusMessage* msg)
162 {
163 DBusMessage* reply;
164 reply = dbus_message_new_method_return(msg);
165 if (!reply || !dbus_message_append_args(reply,
166 DBUS_TYPE_UINT32, &this->state,
167 DBUS_TYPE_INVALID))
168 {
169 return FALSE;
170 }
171 dbus_connection_send(this->conn, reply, NULL);
172 return TRUE;
173 }
174
175 /**
176 * Handle incoming messages
177 */
178 static DBusHandlerResult message_handler(DBusConnection *con, DBusMessage *msg,
179 private_dbus_interface_t *this)
180 {
181 bool handled;
182
183 if (dbus_message_is_method_call(msg, NM_DBUS_INTERFACE_STRONG,
184 "startConnection"))
185 {
186 handled = start_connection(this, msg);
187 }
188 else if (dbus_message_is_method_call(msg, NM_DBUS_INTERFACE_STRONG,
189 "stopConnection"))
190 {
191 handled = stop_connection(this, msg);
192 }
193 else if (dbus_message_is_method_call(msg, NM_DBUS_INTERFACE_STRONG,
194 "getState"))
195 {
196 handled = get_state(this, msg);
197 }
198 else
199 {
200 DBG1(DBG_CFG, "ignoring DBUS message %s.%s",
201 dbus_message_get_interface(msg), dbus_message_get_member(msg));
202 handled = FALSE;
203 }
204
205 if (handled)
206 {
207 return DBUS_HANDLER_RESULT_HANDLED;
208 }
209 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
210 }
211
212 /**
213 * Handle received signals
214
215 static DBusHandlerResult signal_handler(DBusConnection *con, DBusMessage *msg,
216 private_dbus_interface_t *this)
217 {
218 bool handled;
219
220 if (dbus_message_is_signal(msg, NM_DBUS_INTERFACE, "VPNConnectionStateChange"))
221 {
222 NMVPNState state;
223 char *name;
224
225 if (dbus_message_get_args(msg, &this->err, DBUS_TYPE_STRING, &name,
226 DBUS_TYPE_UINT32, &state, DBUS_TYPE_INVALID))
227 {
228 DBG1(DBG_CFG, "got state %d for %s", state, name);
229 }
230 handled = TRUE;
231 }
232 else
233 {
234 DBG1(DBG_CFG, "ignoring DBUS signal %s.%s",
235 dbus_message_get_interface(msg), dbus_message_get_member(msg));
236 handled = FALSE;
237 }
238 if (handled)
239 {
240 return DBUS_HANDLER_RESULT_HANDLED;
241 }
242 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
243 } */
244
245 /**
246 * dispatcher function processed by a seperate thread
247 */
248 static void dispatch(private_dbus_interface_t *this)
249 {
250 while (dbus_connection_read_write_dispatch(this->conn, -1))
251 {
252 /* nothing */
253 }
254 }
255
256 /**
257 * Implementation of interface_t.destroy.
258 */
259 static void destroy(private_dbus_interface_t *this)
260 {
261 pthread_cancel(this->thread);
262 pthread_join(this->thread, NULL);
263 dbus_error_free(&this->err);
264 free(this);
265 }
266
267 /*
268 * Described in header file
269 */
270 interface_t *interface_create()
271 {
272 int ret;
273 DBusObjectPathVTable v = {NULL, (void*)&message_handler, NULL, NULL, NULL, NULL};
274 private_dbus_interface_t *this = malloc_thing(private_dbus_interface_t);
275
276 this->public.interface.destroy = (void (*)(dbus_interface_t*))destroy;
277
278 dbus_error_init(&this->err);
279 this->conn = dbus_bus_get(DBUS_BUS_SYSTEM, &this->err);
280 if (dbus_error_is_set(&this->err))
281 {
282 DBG1(DBG_CFG, "unable to open DBUS connection: %s", this->err.message);
283 charon->kill(charon, "DBUS initialization failed");
284 }
285
286 ret = dbus_bus_request_name(this->conn, NM_DBUS_SERVICE_STRONG,
287 DBUS_NAME_FLAG_REPLACE_EXISTING , &this->err);
288 if (dbus_error_is_set(&this->err))
289 {
290 DBG1(DBG_CFG, "unable to set DBUS name: %s", this->err.message);
291 charon->kill(charon, "unable to set DBUS name");
292 }
293 if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
294 {
295 charon->kill(charon, "DBUS name already owned");
296 }
297 if (!dbus_connection_register_object_path(this->conn, NM_DBUS_PATH_STRONG, &v, this))
298 {
299 charon->kill(charon, "unable to register DBUS message handler");
300 }
301 /*
302 if (!dbus_connection_add_filter(this->conn, (void*)signal_handler, this, NULL))
303 {
304 charon->kill(charon, "unable to register DBUS signal handler");
305 }
306
307 dbus_bus_add_match(this->conn, "type='signal', "
308 "interface='" NM_DBUS_INTERFACE_VPN "',"
309 "path='" NM_DBUS_PATH_VPN "'", &this->err);
310 if (dbus_error_is_set (&this->err))
311 {
312 charon->kill(charon, "unable to add DBUS signal match");
313 }*/
314
315 this->state = NM_VPN_STATE_INIT;
316 set_state(this, NM_VPN_STATE_STOPPED);
317
318 if (pthread_create(&this->thread, NULL, (void*(*)(void*))dispatch, this) != 0)
319 {
320 charon->kill(charon, "unable to create stroke thread");
321 }
322
323 return &this->public;
324 }