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