oss_post_select(): Kill unused attribute for the struct sched parameter.
[paraslash.git] / udp_send.c
1 /*
2  * Copyright (C) 2005-2009 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 <sys/time.h>
11 #include <dirent.h>
12 #include <sys/socket.h>
13 #include <net/if.h>
14
15 #include "server.cmdline.h"
16 #include "para.h"
17 #include "error.h"
18 #include "string.h"
19 #include "afh.h"
20 #include "afs.h"
21 #include "server.h"
22 #include "vss.h"
23 #include "list.h"
24 #include "send.h"
25 #include "portable_io.h"
26 #include "net.h"
27 #include "fd.h"
28 #include "sched.h"
29 #include "close_on_fork.h"
30 #include "chunk_queue.h"
31
32 /** Describes one entry in the list of targets for the udp sender. */
33 struct udp_target {
34         /** The position of this target in the list of targets. */
35         struct list_head node;
36         /** The hostname (DNS name or IPv4/v6 address string). */
37         char host[MAX_HOSTLEN];
38         /** The UDP port. */
39         int port;
40         /** The socket fd. */
41         int fd;
42         /** The list of queued chunks for this fd. */
43         struct chunk_queue *cq;
44         /** The opaque structure returned by vss_add_fec_client(). */
45         struct fec_client *fc;
46         /** The FEC parameters for this target. */
47         struct fec_client_parms fcp;
48 };
49
50 static struct list_head targets;
51 static int sender_status;
52
53 static void udp_close_target(struct udp_target *ut)
54 {
55         if (ut->fd < 0)
56                 return;
57         close(ut->fd);
58         del_close_on_fork_list(ut->fd);
59         cq_destroy(ut->cq);
60         ut->cq = NULL;
61         ut->fd = -1;
62 }
63
64 static void udp_delete_target(struct udp_target *ut, const char *msg)
65 {
66         PARA_NOTICE_LOG("deleting %s#%d (%s) from list\n", ut->host,
67                 ut->port, msg);
68         udp_close_target(ut);
69         vss_del_fec_client(ut->fc);
70         list_del(&ut->node);
71         free(ut);
72 }
73
74 /**
75  * Perform AF-independent multicast sender setup.
76  *
77  * \param fd    The connected socket descriptor.
78  * \param ttl   UDPv4 multicast TTL or UDPv6 multicast number of hops.
79  *              Use -1 to mean default, 0..255 otherwise.
80  * \param iface The outgoing multicast interface, or NULL for the default.
81  *
82  * \return Zero if okay, negative on error.
83  */
84 static int mcast_sender_setup(struct udp_target *ut, int ttl, char *iface)
85 {
86         struct sockaddr_storage ss;
87         socklen_t sslen = sizeof(ss);
88
89         const int on = 1;
90         int id = iface == NULL ? 0 : if_nametoindex(iface);
91
92         if (getpeername(ut->fd, (struct sockaddr *)&ss, &sslen) < 0)
93                 goto err;
94
95         if (iface != NULL && id == 0)
96                 PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
97
98         /* RFC 3493, 5.2: -1 means 'use kernel default' */
99         if (ttl < 0 || ttl > 255)
100                 ttl = -1;
101
102         switch (ss.ss_family) {
103         case AF_INET:
104                 if (!IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr)))
105                         return 0;
106                 if (id != 0) {
107 #ifdef HAVE_IP_MREQN
108                         struct ip_mreqn mn;
109
110                         memset(&mn, 0, sizeof(mn));
111                         mn.imr_ifindex = id;
112                         if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_IF, &mn, sizeof(mn)) < 0)
113                                 goto err;
114 #else
115                         PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
116 #endif
117                 }
118                 /*
119                  * Enable receiving multicast messages generated on the local host
120                  * At least on Linux, this is enabled by default.
121                  */
122                 if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &on, sizeof(on)) < 0)
123                         break;
124
125                 /* Default: use local subnet (do not flood out into the WAN) */
126                 if (ttl == -1)
127                         ttl = 1;
128                 if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
129                         break;
130                 return 0;
131         case AF_INET6:
132                 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr))
133                         return 0;
134                 if (id != 0 &&
135                     setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
136                         break;
137                 if (setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
138                         break;
139                 if (setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
140                         break;
141                 return 0;
142         default:
143                 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
144                 return -E_ADDRESS_LOOKUP;
145         }
146 err:
147         return -ERRNO_TO_PARA_ERROR(errno);
148 }
149
150 /** The maximal size of the per-target chunk queue. */
151 #define UDP_CQ_BYTES 40000
152
153 static int udp_init_session(struct udp_target *ut)
154 {
155         int ret;
156         char *iface = NULL;
157
158         if (ut->fd >= 0) /* nothing to do */
159                 return 0;
160
161         ret = makesock(AF_UNSPEC, IPPROTO_UDP, 0, ut->host, ut->port);
162         if (ret < 0)
163                 return ret;
164         ut->fd = ret;
165
166         if (conf.udp_mcast_iface_given)
167                 iface = conf.udp_mcast_iface_arg;
168
169         ret = mcast_sender_setup(ut, conf.udp_ttl_arg, iface);
170         if (ret < 0) {
171                 close(ut->fd);
172                 return ret;
173         }
174
175         ret = mark_fd_nonblocking(ut->fd);
176         if (ret < 0) {
177                 close(ut->fd);
178                 return ret;
179         }
180         add_close_on_fork_list(ut->fd);
181         ut->cq = cq_new(UDP_CQ_BYTES);
182         PARA_NOTICE_LOG("sending to udp %s#%d\n", ut->host, ut->port);
183         return 1;
184 }
185
186 static void udp_shutdown_targets(void)
187 {
188         struct udp_target *ut, *tmp;
189         const char *buf = NULL;
190         size_t len = 0; /* STFU, gcc */
191
192         list_for_each_entry_safe(ut, tmp, &targets, node) {
193                 if (ut->fd < 0)
194                         continue;
195                 if (!buf)
196                         len = vss_get_fec_eof_packet(&buf);
197                 write(ut->fd, buf, len);
198                 udp_close_target(ut);
199         }
200 }
201
202 static int udp_com_on(__a_unused struct sender_command_data *scd)
203 {
204         sender_status = SENDER_ON;
205         return 1;
206 }
207
208 static int udp_com_off(__a_unused struct sender_command_data *scd)
209 {
210         udp_shutdown_targets();
211         sender_status = SENDER_OFF;
212         return 1;
213 }
214
215 static int udp_com_delete(struct sender_command_data *scd)
216 {
217         struct udp_target *ut, *tmp;
218
219         list_for_each_entry_safe(ut, tmp, &targets, node) {
220                 /* Unspecified port means wildcard port match */
221                 if (scd->port > 0 && scd->port != ut->port)
222                         continue;
223                 if (strcmp(ut->host, scd->host))
224                         continue;
225                 udp_delete_target(ut, "com_delete");
226         }
227         return 1;
228 }
229
230 static int udp_send_fec(char *buf, size_t len, void *private_data)
231 {
232         struct udp_target *ut = private_data;
233         int ret = udp_init_session(ut);
234
235         if (ret < 0)
236                 goto fail;
237         ret = send_queued_chunks(ut->fd, ut->cq, 0);
238         if (ret < 0)
239                 goto fail;
240         if (!len)
241                 return 0;
242         if (!ret) { /* still data left in the queue */
243                 ret = cq_enqueue(ut->cq, buf, len);
244                 if (ret < 0)
245                         goto fail;
246         }
247         ret = write_nonblock(ut->fd, buf, len, 0);
248         if (ret < 0)
249                 goto fail;
250         if (ret != len) {
251                 ret = cq_enqueue(ut->cq, buf + ret, len - ret);
252                 if (ret < 0)
253                         goto fail;
254         }
255         return 1;
256 fail:
257         udp_delete_target(ut, para_strerror(-ret));
258         return ret;
259 }
260
261 static void udp_add_target(struct sender_command_data *scd)
262 {
263         struct udp_target *ut = para_calloc(sizeof(struct udp_target));
264
265         strncpy(ut->host, scd->host, sizeof(ut->host));
266         ut->port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
267         ut->fd = -1; /* not yet connected */
268         PARA_INFO_LOG("adding to target list (%s#%d)\n", ut->host, ut->port);
269         para_list_add(&ut->node, &targets);
270         ut->fcp.slices_per_group = scd->slices_per_group;
271         ut->fcp.data_slices_per_group = scd->data_slices_per_group;
272         ut->fcp.max_slice_bytes = scd->max_slice_bytes;
273         ut->fcp.send = udp_send_fec;
274         ut->fcp.private_data = ut;
275         vss_add_fec_client(&ut->fcp, &ut->fc);
276 }
277
278 static int udp_com_add(struct sender_command_data *scd)
279 {
280         udp_add_target(scd);
281         return 1;
282 }
283
284 static char *udp_info(void)
285 {
286         struct udp_target *ut;
287         char *ret, *tgts = NULL;
288
289         list_for_each_entry(ut, &targets, node) {
290                 bool is_v6 = strchr(ut->host, ':') != NULL;
291                 char *tmp = make_message("%s%s%s%s:%d/%u:%u:%u ", tgts ? : "",
292                         is_v6 ? "[" : "", ut->host,
293                         is_v6 ? "]" : "", ut->port,
294                         ut->fcp.max_slice_bytes,
295                         ut->fcp.data_slices_per_group,
296                         ut->fcp.slices_per_group
297                 );
298                 free(tgts);
299                 tgts = tmp;
300         }
301         ret = make_message(
302                 "udp sender:\n"
303                 "\tstatus: %s\n"
304                 "\tport: udp %d\n"
305                 "\ttargets: %s\n",
306                 (sender_status == SENDER_ON)? "on" : "off",
307                 conf.udp_default_port_arg,
308                 tgts? tgts : "(none)"
309         );
310         free(tgts);
311         return ret;
312 }
313
314 static void udp_init_target_list(void)
315 {
316         struct sender_command_data scd;
317         int i;
318
319         INIT_LIST_HEAD(&targets);
320         for (i = 0; i < conf.udp_target_given; i++) {
321                 if (parse_fec_url(conf.udp_target_arg[i], &scd) < 0) {
322                         PARA_CRIT_LOG("syntax error for udp target option "
323                                 "#%d, ignoring\n", i);
324                         continue;
325                 }
326                 udp_add_target(&scd);
327         }
328 }
329
330 static char *udp_help(void)
331 {
332         return make_message(
333                 "usage: {on|off}\n"
334                 "usage: {add|delete} host[:port][/packet_size:k:n]\n"
335                 "examples: add 224.0.1.38:1500  (IPv4 multicast)\n"
336                 "          add 224.0.1.38:1500/1400:14:16\n"
337                 "              (likewise, using 1400 byte packets, with 14\n"
338                 "               data slices per 16 slice FEC group)\n"
339                 "          add 10.10.1.42       (using default port)\n"
340                 "          add [FF05::42]:1500  (IPv6 multicast)\n"
341                 "          add [::1]            (IPv6 localhost/default port)\n"
342                 "          delete myhost.net    (host with port wildcard)\n"
343                 "          delete [badc0de::1]  (IPv6 with port wildcard)\n"
344         );
345 }
346
347 /**
348  * The init function of para_server's udp sender.
349  *
350  * \param s Pointer to the udp sender struct.
351  *
352  * It initializes all function pointers of \a s and the list of udp targets.
353  */
354 void udp_send_init(struct sender *s)
355 {
356         INIT_LIST_HEAD(&targets);
357         s->info = udp_info;
358         s->help = udp_help;
359         s->send = NULL;
360         s->pre_select = NULL;
361         s->post_select = NULL;
362         s->shutdown_clients = udp_shutdown_targets;
363         s->client_cmds[SENDER_ON] = udp_com_on;
364         s->client_cmds[SENDER_OFF] = udp_com_off;
365         s->client_cmds[SENDER_DENY] = NULL;
366         s->client_cmds[SENDER_ALLOW] = NULL;
367         s->client_cmds[SENDER_ADD] = udp_com_add;
368         s->client_cmds[SENDER_DELETE] = udp_com_delete;
369         sender_status = SENDER_OFF;
370         udp_init_target_list();
371         if (!conf.udp_no_autostart_given)
372                 sender_status = SENDER_ON;
373         PARA_DEBUG_LOG("udp sender init complete\n");
374 }