Merge branch 't/command_util_replace_tabs'
[paraslash.git] / udp_send.c
1 /*
2 * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file udp_send.c Para_server's udp sender. */
8
9
10 #include <regex.h>
11 #include <sys/time.h>
12 #include <dirent.h>
13 #include <sys/socket.h>
14 #include <netinet/udp.h>
15 #include <net/if.h>
16 #include <osl.h>
17 #include <stdbool.h>
18
19 #include "server.cmdline.h"
20 #include "para.h"
21 #include "error.h"
22 #include "string.h"
23 #include "afh.h"
24 #include "afs.h"
25 #include "server.h"
26 #include "list.h"
27 #include "send.h"
28 #include "vss.h"
29 #include "portable_io.h"
30 #include "net.h"
31 #include "fd.h"
32 #include "sched.h"
33 #include "close_on_fork.h"
34
35 /**
36 * Time window during which ICMP Destination/Port Unreachable messages are
37 * ignored, covering transient receiver problems such as restarting the
38 * client, rebooting, reconfiguration, or handover.
39 */
40 #define UDP_MAX_UNREACHABLE_TIME 30
41
42 /** Describes one entry in the list of targets for the udp sender. */
43 struct udp_target {
44 /** Track time (seconds) of last ICMP Port Unreachable error */
45 time_t last_unreachable;
46 /** The opaque structure returned by vss_add_fec_client(). */
47 struct fec_client *fc;
48 /** The FEC parameters for this target. */
49 struct fec_client_parms fcp;
50 };
51
52 static struct list_head targets;
53 static int sender_status;
54
55 static void udp_close_target(struct sender_client *sc)
56 {
57 const char *buf;
58 size_t len = vss_get_fec_eof_packet(&buf);
59
60 /* ignore return value, closing the target anyway. */
61 (void)write(sc->fd, buf, len);
62 }
63
64 static void udp_delete_target(struct sender_client *sc, const char *msg)
65 {
66 struct udp_target *ut = sc->private_data;
67
68 PARA_NOTICE_LOG("deleting %s (%s) from list\n", sc->name, msg);
69 udp_close_target(sc);
70 close(sc->fd);
71 del_close_on_fork_list(sc->fd);
72 vss_del_fec_client(ut->fc);
73 list_del(&sc->node);
74 free(sc->name);
75 free(sc);
76 free(ut);
77 }
78
79 /**
80 * Perform AF-independent multicast sender setup.
81 *
82 * \param sc The connected sender client.
83 *
84 * \return Zero if okay, negative on error.
85 */
86 static int mcast_sender_setup(struct sender_client *sc)
87 {
88 struct sockaddr_storage ss;
89 socklen_t sslen = sizeof(ss);
90 int ttl = conf.udp_ttl_arg, id = 0;
91 const int on = 1;
92
93 if (conf.udp_mcast_iface_given) {
94 char *iface = conf.udp_mcast_iface_arg;
95
96 id = if_nametoindex(iface);
97 if (id == 0)
98 PARA_WARNING_LOG("could not resolve interface '%s', "
99 "using default", iface);
100 }
101
102 if (getpeername(sc->fd, (struct sockaddr *)&ss, &sslen) < 0)
103 goto err;
104 assert(ss.ss_family == AF_INET || ss.ss_family == AF_INET6);
105
106 /* RFC 3493, 5.2: -1 means 'use kernel default' */
107 if (ttl < 0 || ttl > 255)
108 ttl = -1;
109
110 switch (ss.ss_family) {
111 case AF_INET:
112 if (!IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr)))
113 return 0;
114 if (id != 0) {
115 #ifdef HAVE_IP_MREQN
116 struct ip_mreqn mn;
117
118 memset(&mn, 0, sizeof(mn));
119 mn.imr_ifindex = id;
120 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_IF, &mn, sizeof(mn)) < 0)
121 goto err;
122 #else
123 PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
124 #endif
125 }
126 /*
127 * Enable receiving multicast messages generated on the local host
128 * At least on Linux, this is enabled by default.
129 */
130 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &on, sizeof(on)) < 0)
131 break;
132
133 /* Default: use local subnet (do not flood out into the WAN) */
134 if (ttl == -1)
135 ttl = 1;
136 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
137 break;
138 return 0;
139 case AF_INET6:
140 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr))
141 return 0;
142 if (id != 0 &&
143 setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
144 break;
145 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
146 break;
147 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
148 break;
149 return 0;
150 }
151 err:
152 return -ERRNO_TO_PARA_ERROR(errno);
153 }
154
155 static void udp_init_session(struct sender_client *sc)
156 {
157 PARA_NOTICE_LOG("sending to udp %s\n", sc->name);
158 }
159
160 static void udp_shutdown_targets(void)
161 {
162 struct sender_client *sc, *tmp;
163 list_for_each_entry_safe(sc, tmp, &targets, node)
164 udp_close_target(sc);
165 }
166
167 static int udp_resolve_target(const char *url, struct sender_command_data *scd)
168 {
169 const char *result;
170 int ret, port;
171
172 ret = parse_fec_url(url, scd);
173 if (ret)
174 return ret;
175 port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
176
177 ret = para_connect_simple(IPPROTO_UDP, scd->host, port);
178 if (ret < 0)
179 return ret;
180
181 result = remote_name(ret);
182 close(ret);
183
184 if (!parse_url(result, scd->host, sizeof(scd->host), &scd->port))
185 return -E_ADDRESS_LOOKUP;
186 return 1;
187 }
188
189 static int udp_com_on(__a_unused struct sender_command_data *scd)
190 {
191 sender_status = SENDER_ON;
192 return 1;
193 }
194
195 static int udp_com_off(__a_unused struct sender_command_data *scd)
196 {
197 udp_shutdown_targets();
198 sender_status = SENDER_OFF;
199 return 1;
200 }
201
202 static struct sender_client *udp_lookup_target(struct sender_command_data *scd)
203 {
204 struct sender_client *sc, *tmp;
205 char host[MAX_HOSTLEN];
206 int32_t port;
207
208 list_for_each_entry_safe(sc, tmp, &targets, node) {
209 parse_url(sc->name, host, sizeof(host), &port);
210
211 /* Unspecified port means wildcard port match */
212 if (scd->port > 0 && scd->port != port)
213 continue;
214 if (strcmp(host, scd->host))
215 continue;
216 return sc;
217 }
218 return NULL;
219 }
220
221 static int udp_com_delete(struct sender_command_data *scd)
222 {
223 struct sender_client *sc = udp_lookup_target(scd);
224
225 if (sc) {
226 udp_delete_target(sc, "com_delete");
227 return 1;
228 }
229 PARA_NOTICE_LOG("not deleting non-existing target '%s'\n", scd->host);
230 return -E_TARGET_NOT_FOUND;
231 }
232
233 /** Initialize UDP session and set maximum payload size. */
234 static int udp_init_fec(struct sender_client *sc)
235 {
236 int mps;
237
238 udp_init_session(sc);
239 mps = generic_max_transport_msg_size(sc->fd) - sizeof(struct udphdr);
240 PARA_INFO_LOG("current MPS = %d bytes\n", mps);
241 return mps;
242 }
243
244 /** Check and clear socket error if any. */
245 static int udp_check_socket_state(struct sender_client *sc)
246 {
247 struct udp_target *ut = sc->private_data;
248 int ret;
249 socklen_t errlen = sizeof(ret);
250
251 if (getsockopt(sc->fd, SOL_SOCKET, SO_ERROR, &ret, &errlen) < 0) {
252 PARA_ERROR_LOG("SO_ERROR failed: %s\n", strerror(ret));
253 return 0;
254 } else if (ret == 0) {
255 return 0;
256 } else if (ret == ECONNREFUSED) {
257 time_t dist = now->tv_sec - ut->last_unreachable;
258
259 if (dist <= UDP_MAX_UNREACHABLE_TIME) {
260 return 0;
261 } else if (dist > 2 * UDP_MAX_UNREACHABLE_TIME) {
262 ut->last_unreachable = now->tv_sec;
263 return 0;
264 } else {
265 /*
266 * unreachable_time < dist <= 2 * unreachable_time
267 * No errors are allowed during this time window.
268 */
269 PARA_NOTICE_LOG("Evicting %s after %d seconds "
270 "of connection errors.\n",
271 sc->name, (int)dist);
272 }
273 }
274 return -ERRNO_TO_PARA_ERROR(ret);
275 }
276
277 static int udp_send_fec(struct sender_client *sc, char *buf, size_t len)
278 {
279 int ret;
280
281 if (sender_status == SENDER_OFF)
282 return 0;
283 if (len == 0)
284 return 0;
285 ret = udp_check_socket_state(sc);
286 if (ret < 0)
287 goto fail;
288 ret = write_nonblock(sc->fd, buf, len);
289 if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED)) {
290 /*
291 * Happens if meanwhile an ICMP Destination / Port Unreachable
292 * has arrived. Ignore, persistent errors will be caught above.
293 */
294 ret = 0;
295 }
296 if (ret < 0)
297 goto fail;
298 return 1;
299 fail:
300 udp_delete_target(sc, para_strerror(-ret));
301 return ret;
302 }
303
304 static int udp_com_add(struct sender_command_data *scd)
305 {
306 int ret;
307 struct udp_target *ut;
308 struct sender_client *sc = udp_lookup_target(scd);
309
310 if (sc) {
311 PARA_NOTICE_LOG("target %s already exists - not adding it\n",
312 sc->name);
313 return -E_TARGET_EXISTS;
314 }
315 ut = para_calloc(sizeof(*ut));
316 sc = para_calloc(sizeof(*sc));
317 ut->fcp.slices_per_group = scd->slices_per_group;
318 ut->fcp.data_slices_per_group = scd->data_slices_per_group;
319 ut->fcp.init_fec = udp_init_fec;
320 ut->fcp.send_fec = udp_send_fec;
321 ut->fcp.need_periodic_header = true;
322
323 sc->private_data = ut;
324 sc->fd = -1;
325 ret = para_connect_simple(IPPROTO_UDP, scd->host, scd->port);
326 if (ret < 0)
327 goto err;
328 sc->fd = ret;
329
330 ret = mcast_sender_setup(sc);
331 if (ret < 0)
332 goto err;
333 ret = mark_fd_nonblocking(sc->fd);
334 if (ret < 0)
335 goto err;
336 sc->name = para_strdup(remote_name(sc->fd));
337 PARA_INFO_LOG("adding to target list (%s)\n", sc->name);
338 ut->fc = vss_add_fec_client(sc, &ut->fcp);
339 para_list_add(&sc->node, &targets);
340 add_close_on_fork_list(sc->fd);
341 return 1;
342 err:
343 if (sc->fd >= 0)
344 close(sc->fd);
345 PARA_NOTICE_LOG("failed to set up %s:%d (%s)- not adding it\n",
346 scd->host, scd->port, para_strerror(-ret));
347 free(sc);
348 free(ut);
349 return ret;
350 }
351
352 static char *udp_info(void)
353 {
354 struct sender_client *sc;
355 char *ret, *tgts = NULL;
356
357 list_for_each_entry(sc, &targets, node) {
358 struct udp_target *ut = sc->private_data;
359 char *tmp = make_message("%s%s/%u:%u ",
360 tgts ? : "", sc->name,
361 ut->fcp.data_slices_per_group,
362 ut->fcp.slices_per_group
363 );
364 free(tgts);
365 tgts = tmp;
366 }
367 ret = make_message(
368 "udp sender:\n"
369 "\tstatus: %s\n"
370 "\tport: %s\n"
371 "\ttargets: %s\n",
372 (sender_status == SENDER_ON)? "on" : "off",
373 stringify_port(conf.udp_default_port_arg, "udp"),
374 tgts? tgts : "(none)"
375 );
376 free(tgts);
377 return ret;
378 }
379
380 static void udp_init_target_list(void)
381 {
382 struct sender_command_data scd;
383 int i;
384
385 INIT_LIST_HEAD(&targets);
386 for (i = 0; i < conf.udp_target_given; i++) {
387 if (udp_resolve_target(conf.udp_target_arg[i], &scd) < 0)
388 PARA_CRIT_LOG("not adding requested target '%s'\n",
389 conf.udp_target_arg[i]);
390 else
391 udp_com_add(&scd);
392 }
393 }
394
395 static char *udp_help(void)
396 {
397 return make_message(
398 "usage: {on|off}\n"
399 "usage: {add|delete} ip_address[:port][/[packet_size:]k:n]\n"
400 " - k is the number of data slices per FEC group\n"
401 " - n is the total number of slices in a FEC group\n"
402 " - packet_size reduces the slice size below path MTU\n\n"
403 "examples: add 224.0.1.38:1500 (IPv4 multicast)\n"
404 " add 224.0.1.38:8080/14:16 (explicit FEC)\n"
405 " add 10.10.1.42 (using default port)\n"
406 " add [FF05::42]:1500 (IPv6 multicast)\n"
407 " add [::1] (IPv6 localhost/default port)\n"
408 " delete myhost.net (host with port wildcard)\n"
409 " delete 10.1.2.3:456 (IPv4 with explicit port)\n"
410 " delete [badc0de::1] (IPv6 with port wildcard)\n"
411 );
412 }
413
414 /**
415 * The init function of para_server's udp sender.
416 *
417 * \param s Pointer to the udp sender struct.
418 *
419 * It initializes all function pointers of \a s and the list of udp targets.
420 */
421 void udp_send_init(struct sender *s)
422 {
423 INIT_LIST_HEAD(&targets);
424 s->info = udp_info;
425 s->help = udp_help;
426 s->send = NULL;
427 s->pre_select = NULL;
428 s->post_select = NULL;
429 s->shutdown_clients = udp_shutdown_targets;
430 s->resolve_target = udp_resolve_target;
431 s->client_cmds[SENDER_ON] = udp_com_on;
432 s->client_cmds[SENDER_OFF] = udp_com_off;
433 s->client_cmds[SENDER_DENY] = NULL;
434 s->client_cmds[SENDER_ALLOW] = NULL;
435 s->client_cmds[SENDER_ADD] = udp_com_add;
436 s->client_cmds[SENDER_DELETE] = udp_com_delete;
437 sender_status = SENDER_OFF;
438 udp_init_target_list();
439 if (!conf.udp_no_autostart_given)
440 sender_status = SENDER_ON;
441 PARA_DEBUG_LOG("udp sender init complete\n");
442 }