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