3537423468d64bd396aa62ad736a4f2703a30536
[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 * Glib main loop for a thread, handles DBUS calls
39 */
40 GMainLoop *loop;
41
42 /**
43 * credential set registered at the daemon
44 */
45 nm_creds_t *creds;
46 };
47
48 /**
49 * NM plugin processing routine, creates and handles NMVPNPlugin
50 */
51 static job_requeue_t run(private_nm_plugin_t *this)
52 {
53 NMStrongswanPlugin *plugin;
54 GMainLoop *loop;
55
56 plugin = nm_strongswan_plugin_new(this->creds);
57
58 this->loop = loop = g_main_loop_new(NULL, FALSE);
59 g_main_loop_run(loop);
60
61 g_main_loop_unref(loop);
62 g_object_unref(plugin);
63
64 return JOB_REQUEUE_NONE;
65 }
66
67 /**
68 * Implementation of plugin_t.destroy
69 */
70 static void destroy(private_nm_plugin_t *this)
71 {
72 if (this->loop)
73 {
74 g_main_loop_quit(this->loop);
75 }
76 charon->credentials->remove_set(charon->credentials, &this->creds->set);
77 this->creds->destroy(this->creds);
78 free(this);
79 }
80
81 /*
82 * see header file
83 */
84 plugin_t *plugin_create()
85 {
86 private_nm_plugin_t *this = malloc_thing(private_nm_plugin_t);
87
88 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
89
90 this->loop = NULL;
91 g_type_init ();
92 if (!g_thread_supported())
93 {
94 g_thread_init(NULL);
95 }
96
97 this->creds = nm_creds_create();
98 charon->credentials->add_set(charon->credentials, &this->creds->set);
99
100 charon->processor->queue_job(charon->processor,
101 (job_t*)callback_job_create((callback_job_cb_t)run, this, NULL, NULL));
102
103 return &this->public.plugin;
104 }
105