added HA resync option to (re-)integrate nodes to a cluster
[strongswan.git] / src / charon / plugins / ha_sync / ha_sync_socket.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 "ha_sync_socket.h"
19
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <pthread.h>
25
26 #include <daemon.h>
27 #include <utils/host.h>
28
29 typedef struct private_ha_sync_socket_t private_ha_sync_socket_t;
30
31 /**
32 * Private data of an ha_sync_socket_t object.
33 */
34 struct private_ha_sync_socket_t {
35
36 /**
37 * Public ha_sync_socket_t interface.
38 */
39 ha_sync_socket_t public;
40
41 /**
42 * UDP communication socket fd
43 */
44 int fd;
45 };
46
47 /**
48 * Implementation of ha_sync_socket_t.push
49 */
50 static void push(private_ha_sync_socket_t *this, ha_sync_message_t *message)
51 {
52 chunk_t data;
53
54 data = message->get_encoding(message);
55 if (send(this->fd, data.ptr, data.len, 0) < data.len)
56 {
57 DBG1(DBG_CFG, "pushing HA sync message failed: %s", strerror(errno));
58 }
59 }
60
61 /**
62 * Implementation of ha_sync_socket_t.pull
63 */
64 static ha_sync_message_t *pull(private_ha_sync_socket_t *this)
65 {
66 while (TRUE)
67 {
68 ha_sync_message_t *message;
69 char buf[1024];
70 int oldstate;
71 ssize_t len;
72
73 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
74 len = recv(this->fd, buf, sizeof(buf), 0);
75 pthread_setcancelstate(oldstate, NULL);
76 if (len <= 0)
77 {
78 if (errno != EINTR)
79 {
80 DBG1(DBG_CFG, "pulling HA sync message failed: %s",
81 strerror(errno));
82 sleep(1);
83 }
84 continue;
85 }
86 message = ha_sync_message_parse(chunk_create(buf, len));
87 if (message)
88 {
89 return message;
90 }
91 }
92 }
93
94 /**
95 * read local/remote node address from config
96 */
97 static host_t *get_host_config(char *key)
98 {
99 char *value;
100 host_t *host;
101
102 value = lib->settings->get_str(lib->settings,
103 "charon.plugins.ha_sync.%s", NULL, key);
104 if (!value)
105 {
106 DBG1(DBG_CFG, "no %s node specified for HA sync", key);
107 return NULL;
108 }
109 host = host_create_from_dns(value, 0, HA_SYNC_PORT);
110 if (!host)
111 {
112 DBG1(DBG_CFG, "%s node '%s' is invalid", key, value);
113 }
114 return host;
115 }
116
117 /**
118 * Open and connect the HA sync socket
119 */
120 static bool open_socket(private_ha_sync_socket_t *this)
121 {
122 host_t *local, *remote;
123 bool success = TRUE;
124
125 local = get_host_config("local");
126 remote = get_host_config("remote");
127 if (!local || !remote)
128 {
129 DESTROY_IF(local);
130 DESTROY_IF(remote);
131 return FALSE;
132 }
133
134 this->fd = socket(local->get_family(local), SOCK_DGRAM, 0);
135 if (!this->fd)
136 {
137 DESTROY_IF(local);
138 DESTROY_IF(remote);
139 DBG1(DBG_CFG, "opening HA sync socket failed: %s", strerror(errno));
140 return FALSE;
141 }
142
143 if (bind(this->fd, local->get_sockaddr(local),
144 *local->get_sockaddr_len(local)) == -1)
145 {
146 DBG1(DBG_CFG, "binding HA sync socket failed: %s", strerror(errno));
147 close(this->fd);
148 success = FALSE;
149 }
150 if (connect(this->fd, remote->get_sockaddr(remote),
151 *remote->get_sockaddr_len(remote)) == -1)
152 {
153 DBG1(DBG_CFG, "connecting HA sync socket failed: %s", strerror(errno));
154 close(this->fd);
155 success = FALSE;
156 }
157 local->destroy(local);
158 remote->destroy(remote);
159 return success;
160 }
161
162 /**
163 * Implementation of ha_sync_socket_t.destroy.
164 */
165 static void destroy(private_ha_sync_socket_t *this)
166 {
167 close(this->fd);
168 free(this);
169 }
170
171 /**
172 * See header
173 */
174 ha_sync_socket_t *ha_sync_socket_create()
175 {
176 private_ha_sync_socket_t *this = malloc_thing(private_ha_sync_socket_t);
177
178 this->public.push = (void(*)(ha_sync_socket_t*, ha_sync_message_t*))push;
179 this->public.pull = (ha_sync_message_t*(*)(ha_sync_socket_t*))pull;
180 this->public.destroy = (void(*)(ha_sync_socket_t*))destroy;
181
182 if (!open_socket(this))
183 {
184 free(this);
185 return NULL;
186 }
187 return &this->public;
188 }
189