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