f906dcaa291cb688cef0579f9aaa42723ed9da9f
[strongswan.git] / src / charon / plugins / nm / nm_plugin.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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 * $Id$
16 */
17
18 #include "nm_plugin.h"
19 #include "nm_service.h"
20 #include "nm_creds.h"
21
22 #include <daemon.h>
23 #include <processing/jobs/callback_job.h>
24
25 typedef struct private_nm_plugin_t private_nm_plugin_t;
26
27 /**
28 * private data of nm plugin
29 */
30 struct private_nm_plugin_t {
31
32 /**
33 * implements plugin interface
34 */
35 nm_plugin_t public;
36
37 /**
38 * NetworkManager service (VPNPlugin)
39 */
40 NMStrongswanPlugin *plugin;
41
42 /**
43 * Glib main loop for a thread, handles DBUS calls
44 */
45 GMainLoop *loop;
46
47 /**
48 * credential set registered at the daemon
49 */
50 nm_creds_t *creds;
51 };
52
53 /**
54 * NM plugin processing routine, creates and handles NMVPNPlugin
55 */
56 static job_requeue_t run(private_nm_plugin_t *this)
57 {
58 this->loop = g_main_loop_new(NULL, FALSE);
59 g_main_loop_run(this->loop);
60 g_main_loop_unref(this->loop);
61
62 return JOB_REQUEUE_NONE;
63 }
64
65 /**
66 * Implementation of plugin_t.destroy
67 */
68 static void destroy(private_nm_plugin_t *this)
69 {
70 if (this->loop)
71 {
72 g_main_loop_quit(this->loop);
73 }
74 if (this->plugin)
75 {
76 g_object_unref(this->plugin);
77 }
78 charon->credentials->remove_set(charon->credentials, &this->creds->set);
79 this->creds->destroy(this->creds);
80 free(this);
81 }
82
83 /*
84 * see header file
85 */
86 plugin_t *plugin_create()
87 {
88 private_nm_plugin_t *this = malloc_thing(private_nm_plugin_t);
89
90 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
91
92 this->loop = NULL;
93 g_type_init ();
94 if (!g_thread_supported())
95 {
96 g_thread_init(NULL);
97 }
98
99 this->creds = nm_creds_create();
100 charon->credentials->add_set(charon->credentials, &this->creds->set);
101 this->plugin = nm_strongswan_plugin_new(this->creds);
102 if (!this->plugin)
103 {
104 DBG1(DBG_CFG, "DBUS binding failed");
105 destroy(this);
106 return NULL;
107 }
108
109 charon->processor->queue_job(charon->processor,
110 (job_t*)callback_job_create((callback_job_cb_t)run, this, NULL, NULL));
111
112 return &this->public.plugin;
113 }
114