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