65101f868e8498a9dace616282c1d06934934956
[strongswan.git] / src / charon / plugins / ha / ha_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
16 #include "ha_plugin.h"
17 #include "ha_ike.h"
18 #include "ha_child.h"
19 #include "ha_socket.h"
20 #include "ha_tunnel.h"
21 #include "ha_dispatcher.h"
22 #include "ha_segments.h"
23 #include "ha_ctl.h"
24
25 #include <daemon.h>
26 #include <config/child_cfg.h>
27
28 typedef struct private_ha_plugin_t private_ha_plugin_t;
29
30 /**
31 * private data of ha plugin
32 */
33 struct private_ha_plugin_t {
34
35 /**
36 * implements plugin interface
37 */
38 ha_plugin_t public;
39
40 /**
41 * Communication socket
42 */
43 ha_socket_t *socket;
44
45 /**
46 * Tunnel securing sync messages.
47 */
48 ha_tunnel_t *tunnel;
49
50 /**
51 * IKE_SA synchronization
52 */
53 ha_ike_t *ike;
54
55 /**
56 * CHILD_SA synchronization
57 */
58 ha_child_t *child;
59
60 /**
61 * Dispatcher to process incoming messages
62 */
63 ha_dispatcher_t *dispatcher;
64
65 /**
66 * Active/Passive segment management
67 */
68 ha_segments_t *segments;
69
70 /**
71 * Interface to control segments at kernel level
72 */
73 ha_kernel_t *kernel;
74
75 /**
76 * Segment control interface via FIFO
77 */
78 ha_ctl_t *ctl;
79 };
80
81 /**
82 * Implementation of plugin_t.destroy
83 */
84 static void destroy(private_ha_plugin_t *this)
85 {
86 DESTROY_IF(this->ctl);
87 charon->bus->remove_listener(charon->bus, &this->segments->listener);
88 charon->bus->remove_listener(charon->bus, &this->ike->listener);
89 charon->bus->remove_listener(charon->bus, &this->child->listener);
90 this->ike->destroy(this->ike);
91 this->child->destroy(this->child);
92 this->dispatcher->destroy(this->dispatcher);
93 this->segments->destroy(this->segments);
94 this->kernel->destroy(this->kernel);
95 this->socket->destroy(this->socket);
96 DESTROY_IF(this->tunnel);
97 free(this);
98 }
99
100 /*
101 * see header file
102 */
103 plugin_t *plugin_create()
104 {
105 private_ha_plugin_t *this;
106 char *local, *remote, *secret, *virtuals;
107 u_int count;
108 bool fifo;
109
110 local = lib->settings->get_str(lib->settings,
111 "charon.plugins.ha.local", NULL);
112 remote = lib->settings->get_str(lib->settings,
113 "charon.plugins.ha.remote", NULL);
114 virtuals = lib->settings->get_str(lib->settings,
115 "charon.plugins.ha.virtuals", "");
116 secret = lib->settings->get_str(lib->settings,
117 "charon.plugins.ha.secret", NULL);
118 fifo = lib->settings->get_bool(lib->settings,
119 "charon.plugins.ha.fifo_interface", FALSE);
120 count = min(SEGMENTS_MAX, lib->settings->get_int(lib->settings,
121 "charon.plugins.ha.segment_count", 1));
122 if (!local || !remote)
123 {
124 DBG1(DBG_CFG, "HA config misses local/remote address");
125 return NULL;
126 }
127
128 this = malloc_thing(private_ha_plugin_t);
129
130 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
131 this->tunnel = NULL;
132 this->ctl = NULL;
133
134 this->socket = ha_socket_create(local, remote);
135 if (!this->socket)
136 {
137 free(this);
138 return NULL;
139 }
140 this->kernel = ha_kernel_create(count, virtuals);
141 if (!this->kernel)
142 {
143 this->socket->destroy(this->socket);
144 free(this);
145 return NULL;
146 }
147
148 if (secret)
149 {
150 this->tunnel = ha_tunnel_create(local, remote, secret);
151 }
152 this->segments = ha_segments_create(this->socket, this->kernel,
153 this->tunnel, local, remote, count);
154 if (fifo)
155 {
156 this->ctl = ha_ctl_create(this->segments);
157 }
158 this->dispatcher = ha_dispatcher_create(this->socket, this->segments);
159 this->ike = ha_ike_create(this->socket, this->tunnel);
160 this->child = ha_child_create(this->socket, this->tunnel);
161 charon->bus->add_listener(charon->bus, &this->segments->listener);
162 charon->bus->add_listener(charon->bus, &this->ike->listener);
163 charon->bus->add_listener(charon->bus, &this->child->listener);
164
165 return &this->public.plugin;
166 }
167