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