8e0268e6a3e1d32cc98e379b4e804e1b6093e9e0
[strongswan.git] / src / charon / control / controller.c
1 /**
2 * @file controller.c
3 *
4 * @brief Implementation of controller_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 #include "controller.h"
24
25 #include <daemon.h>
26 #include <library.h>
27 #include <processing/job_queue.h>
28 #include <processing/jobs/initiate_job.h>
29
30
31 typedef struct private_controller_t private_controller_t;
32
33 /**
34 * Private data of an stroke_t object.
35 */
36 struct private_controller_t {
37
38 /**
39 * Public part of stroke_t object.
40 */
41 controller_t public;
42 };
43
44 /**
45 * Implementation of controller_t.initiate.
46 */
47 static status_t initiate(private_controller_t *this,
48 peer_cfg_t *peer_cfg, child_cfg_t *child_cfg,
49 bool(*cb)(void*,signal_t,level_t,ike_sa_t*,char*,va_list),
50 void *param)
51 {
52 ike_sa_t *ours = NULL;
53 job_t *job;
54 status_t retval;
55
56 charon->bus->set_listen_state(charon->bus, TRUE);
57
58 job = (job_t*)initiate_job_create(peer_cfg, child_cfg);
59 charon->job_queue->add(charon->job_queue, job);
60
61 while (TRUE)
62 {
63 level_t level;
64 signal_t signal;
65 int thread;
66 ike_sa_t *ike_sa;
67 char* format;
68 va_list args;
69
70 signal = charon->bus->listen(charon->bus, &level, &thread,
71 &ike_sa, &format, &args);
72
73 if (ike_sa == ours || ours == NULL)
74 {
75 if (!cb(param, signal, level, ike_sa, format, args))
76 {
77 charon->bus->set_listen_state(charon->bus, FALSE);
78 return NEED_MORE;
79 }
80 }
81
82 switch (signal)
83 {
84 case CHILD_UP_SUCCESS:
85 if (ike_sa == ours)
86 {
87 retval = SUCCESS;
88 break;
89 }
90 continue;
91 case CHILD_UP_FAILED:
92 case IKE_UP_FAILED:
93 if (ike_sa == ours)
94 {
95 retval = FAILED;
96 break;
97 }
98 continue;
99 case CHILD_UP_START:
100 case IKE_UP_START:
101 if (ours == NULL)
102 {
103 ours = ike_sa;
104 }
105 continue;
106 default:
107 continue;
108 }
109 break;
110 }
111 charon->bus->set_listen_state(charon->bus, FALSE);
112 return retval;
113 }
114
115 /**
116 * Implementation of stroke_t.destroy.
117 */
118 static void destroy(private_controller_t *this)
119 {
120 free(this);
121 }
122
123 /*
124 * Described in header-file
125 */
126 controller_t *controller_create(void)
127 {
128 private_controller_t *this = malloc_thing(private_controller_t);
129
130 this->public.initiate = (status_t(*)(controller_t*,peer_cfg_t*,child_cfg_t*,bool(*)(void*,signal_t,level_t,ike_sa_t*,char*,va_list),void*))initiate;
131 this->public.destroy = (void (*)(controller_t*))destroy;
132
133 return &this->public;
134 }