Moved segment configuration parsing to ha_sync_plugin
[strongswan.git] / src / charon / plugins / ha_sync / ha_sync_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_sync_plugin.h"
17 #include "ha_sync_ike.h"
18 #include "ha_sync_child.h"
19 #include "ha_sync_socket.h"
20 #include "ha_sync_tunnel.h"
21 #include "ha_sync_dispatcher.h"
22 #include "ha_sync_segments.h"
23 #include "ha_sync_ctl.h"
24
25 #include <daemon.h>
26 #include <config/child_cfg.h>
27
28 typedef struct private_ha_sync_plugin_t private_ha_sync_plugin_t;
29
30 /**
31 * private data of ha_sync plugin
32 */
33 struct private_ha_sync_plugin_t {
34
35 /**
36 * implements plugin interface
37 */
38 ha_sync_plugin_t public;
39
40 /**
41 * Communication socket
42 */
43 ha_sync_socket_t *socket;
44
45 /**
46 * Tunnel securing sync messages.
47 */
48 ha_sync_tunnel_t *tunnel;
49
50 /**
51 * IKE_SA synchronization
52 */
53 ha_sync_ike_t *ike;
54
55 /**
56 * CHILD_SA synchronization
57 */
58 ha_sync_child_t *child;
59
60 /**
61 * Dispatcher to process incoming messages
62 */
63 ha_sync_dispatcher_t *dispatcher;
64
65 /**
66 * Active/Passive segment management
67 */
68 ha_sync_segments_t *segments;
69
70 /**
71 * Segment control interface via FIFO
72 */
73 ha_sync_ctl_t *ctl;
74 };
75
76 /**
77 * Implementation of plugin_t.destroy
78 */
79 static void destroy(private_ha_sync_plugin_t *this)
80 {
81 DESTROY_IF(this->ctl);
82 charon->bus->remove_listener(charon->bus, &this->ike->listener);
83 charon->bus->remove_listener(charon->bus, &this->child->listener);
84 this->ike->destroy(this->ike);
85 this->child->destroy(this->child);
86 this->dispatcher->destroy(this->dispatcher);
87 this->segments->destroy(this->segments);
88 this->socket->destroy(this->socket);
89 DESTROY_IF(this->tunnel);
90 free(this);
91 }
92
93 /**
94 * Convert segment string to mask
95 */
96 static segment_mask_t parse_active(char *active)
97 {
98 enumerator_t *enumerator;
99 u_int segment;
100 segment_mask_t mask = 0;
101
102 enumerator = enumerator_create_token(active, ",", " ");
103 while (enumerator->enumerate(enumerator, &active))
104 {
105 segment = atoi(active);
106 if (segment > 0 && segment < SEGMENTS_MAX)
107 {
108 mask |= SEGMENTS_BIT(segment);
109 }
110 }
111 enumerator->destroy(enumerator);
112
113 return mask;
114 }
115
116 /*
117 * see header file
118 */
119 plugin_t *plugin_create()
120 {
121 private_ha_sync_plugin_t *this;
122 char *local, *remote, *secret;
123 segment_mask_t active;
124 u_int count;
125 bool fifo;
126
127 local = lib->settings->get_str(lib->settings,
128 "charon.plugins.ha_sync.local", NULL);
129 remote = lib->settings->get_str(lib->settings,
130 "charon.plugins.ha_sync.remote", NULL);
131 secret = lib->settings->get_str(lib->settings,
132 "charon.plugins.ha_sync.secret", NULL);
133 fifo = lib->settings->get_bool(lib->settings,
134 "charon.plugins.ha_sync.fifo_interface", FALSE);
135 count = min(SEGMENTS_MAX, lib->settings->get_int(lib->settings,
136 "charon.plugins.ha_sync.segment_count", 1));
137 active = parse_active(lib->settings->get_str(lib->settings,
138 "charon.plugins.ha_sync.active_segments", "1"));
139 if (!local || !remote)
140 {
141 DBG1(DBG_CFG, "HA sync config misses local/remote address");
142 return NULL;
143 }
144
145 this = malloc_thing(private_ha_sync_plugin_t);
146
147 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
148 this->tunnel = NULL;
149 this->ctl = NULL;
150
151 this->socket = ha_sync_socket_create(local, remote);
152 if (!this->socket)
153 {
154 free(this);
155 return NULL;
156 }
157 this->segments = ha_sync_segments_create(this->socket, count, active);
158 if (secret)
159 {
160 this->tunnel = ha_sync_tunnel_create(secret, local, remote);
161 }
162 if (fifo)
163 {
164 this->ctl = ha_sync_ctl_create(this->segments);
165 }
166 this->dispatcher = ha_sync_dispatcher_create(this->socket, this->segments);
167 this->ike = ha_sync_ike_create(this->socket, this->tunnel);
168 this->child = ha_sync_child_create(this->socket, this->tunnel);
169 charon->bus->add_listener(charon->bus, &this->ike->listener);
170 charon->bus->add_listener(charon->bus, &this->child->listener);
171
172 return &this->public.plugin;
173 }
174