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