5c1cdf02c2fb9e1be95783f65cfeb12994a15056
[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 <net/if.h>
15 #include <osl.h>
16
17 #include "server.cmdline.h"
18 #include "para.h"
19 #include "error.h"
20 #include "string.h"
21 #include "afh.h"
22 #include "afs.h"
23 #include "server.h"
24 #include "list.h"
25 #include "send.h"
26 #include "vss.h"
27 #include "portable_io.h"
28 #include "net.h"
29 #include "fd.h"
30 #include "sched.h"
31 #include "close_on_fork.h"
32 #include "chunk_queue.h"
33
34 /** Describes one entry in the list of targets for the udp sender. */
35 struct udp_target {
36 /** The hostname (DNS name or IPv4/v6 address string). */
37 char host[MAX_HOSTLEN];
38 /** The UDP port. */
39 int port;
40 /** Common sender client data */
41 struct sender_client *sc;
42 /** The opaque structure returned by vss_add_fec_client(). */
43 struct fec_client *fc;
44 /** The FEC parameters for this target. */
45 struct fec_client_parms fcp;
46 };
47
48 static struct list_head targets;
49 static int sender_status;
50
51 static void udp_close_target(struct sender_client *sc)
52 {
53 if (sc->fd < 0)
54 return;
55 close(sc->fd);
56 del_close_on_fork_list(sc->fd);
57 cq_destroy(sc->cq);
58 sc->cq = NULL;
59 sc->fd = -1;
60 }
61
62 static void udp_delete_target(struct udp_target *ut, const char *msg)
63 {
64
65 PARA_NOTICE_LOG("deleting %s#%d (%s) from list\n", ut->host,
66 ut->port, msg);
67 udp_close_target(ut->sc);
68 vss_del_fec_client(ut->fc);
69 list_del(&ut->sc->node);
70 free(ut->sc);
71 free(ut);
72 }
73
74 /**
75 * Perform AF-independent multicast sender setup.
76 *
77 * \param sc The connected sender client.
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 sender_client *sc, 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(sc->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(sc->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(sc->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(sc->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(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
136 break;
137 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
138 break;
139 if (setsockopt(sc->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 sender_client *sc)
154 {
155 struct udp_target *ut = sc->private_data;
156 int ret;
157 char *iface = NULL;
158
159 if (sc->fd >= 0) /* nothing to do */
160 return 0;
161
162 ret = para_connect_simple(IPPROTO_UDP, ut->host, ut->port);
163 if (ret < 0)
164 return ret;
165 sc->fd = ret;
166
167 if (conf.udp_mcast_iface_given)
168 iface = conf.udp_mcast_iface_arg;
169
170 ret = mcast_sender_setup(sc, conf.udp_ttl_arg, iface);
171 if (ret < 0) {
172 close(sc->fd);
173 return ret;
174 }
175
176 ret = mark_fd_nonblocking(sc->fd);
177 if (ret < 0) {
178 close(sc->fd);
179 return ret;
180 }
181 add_close_on_fork_list(sc->fd);
182 sc->cq = cq_new(UDP_CQ_BYTES);
183 PARA_NOTICE_LOG("sending to udp %s#%d\n", ut->host, ut->port);
184 return 1;
185 }
186
187 static void udp_shutdown_targets(void)
188 {
189 struct sender_client *sc, *tmp;
190 const char *buf = NULL;
191 size_t len = 0; /* STFU, gcc */
192
193 list_for_each_entry_safe(sc, tmp, &targets, node) {
194 int ubuntu_glibc_headers_suck;
195 if (sc->fd < 0)
196 continue;
197 if (!buf)
198 len = vss_get_fec_eof_packet(&buf);
199 /* ignore return value, we're closing the target anyway. */
200 ubuntu_glibc_headers_suck = write(sc->fd, buf, len); /* STFU */
201 udp_close_target(sc);
202 }
203 }
204
205 static int udp_com_on(__a_unused struct sender_command_data *scd)
206 {
207 sender_status = SENDER_ON;
208 return 1;
209 }
210
211 static int udp_com_off(__a_unused struct sender_command_data *scd)
212 {
213 udp_shutdown_targets();
214 sender_status = SENDER_OFF;
215 return 1;
216 }
217
218 static int udp_com_delete(struct sender_command_data *scd)
219 {
220 struct sender_client *sc, *tmp;
221
222 list_for_each_entry_safe(sc, tmp, &targets, node) {
223 struct udp_target *ut = sc->private_data;
224
225 /* Unspecified port means wildcard port match */
226 if (scd->port > 0 && scd->port != ut->port)
227 continue;
228 if (strcmp(ut->host, scd->host))
229 continue;
230 udp_delete_target(ut, "com_delete");
231 }
232 return 1;
233 }
234
235 static int udp_send_fec(struct sender_client *sc, char *buf, size_t len)
236 {
237 struct udp_target *ut = sc->private_data;
238 int ret;
239
240 if (sender_status == SENDER_OFF)
241 return 0;
242 ret = udp_init_session(sc);
243 if (ret < 0)
244 goto fail;
245 ret = send_queued_chunks(sc->fd, sc->cq, 0);
246 if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED))
247 ret = 0;
248 if (ret < 0)
249 goto fail;
250 if (!len)
251 return 0;
252 if (!ret) { /* still data left in the queue */
253 ret = cq_force_enqueue(sc->cq, buf, len);
254 assert(ret >= 0);
255 }
256 ret = write_nonblock(sc->fd, buf, len, 0);
257 if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED))
258 ret = 0;
259 if (ret < 0)
260 goto fail;
261 if (ret != len) {
262 ret = cq_force_enqueue(sc->cq, buf + ret, len - ret);
263 assert(ret >= 0);
264 }
265 return 1;
266 fail:
267 udp_delete_target(ut, para_strerror(-ret));
268 return ret;
269 }
270
271 static void udp_add_target(struct sender_command_data *scd)
272 {
273 struct udp_target *ut = para_calloc(sizeof(*ut));
274
275 strncpy(ut->host, scd->host, sizeof(ut->host));
276 ut->port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
277
278 ut->fcp.slices_per_group = scd->slices_per_group;
279 ut->fcp.data_slices_per_group = scd->data_slices_per_group;
280 ut->fcp.max_slice_bytes = scd->max_slice_bytes;
281 ut->fcp.init_fec = NULL; /* FIXME */
282 ut->fcp.send_fec = udp_send_fec;
283
284 ut->sc = para_calloc(sizeof(*ut->sc));
285 ut->sc->fd = -1; /* not yet connected */
286 ut->sc->private_data = ut;
287 ut->fc = vss_add_fec_client(ut->sc, &ut->fcp);
288
289 PARA_INFO_LOG("adding to target list (%s#%d)\n", ut->host, ut->port);
290 para_list_add(&ut->sc->node, &targets);
291 }
292
293 static int udp_com_add(struct sender_command_data *scd)
294 {
295 udp_add_target(scd);
296 return 1;
297 }
298
299 static char *udp_info(void)
300 {
301 struct sender_client *sc;
302 char *ret, *tgts = NULL;
303
304 list_for_each_entry(sc, &targets, node) {
305 struct udp_target *ut = sc->private_data;
306 bool is_v6 = strchr(ut->host, ':') != NULL;
307 char *tmp = make_message("%s%s%s%s:%d/%u:%u:%u ", tgts ? : "",
308 is_v6 ? "[" : "", ut->host,
309 is_v6 ? "]" : "", ut->port,
310 ut->fcp.max_slice_bytes,
311 ut->fcp.data_slices_per_group,
312 ut->fcp.slices_per_group
313 );
314 free(tgts);
315 tgts = tmp;
316 }
317 ret = make_message(
318 "udp sender:\n"
319 "\tstatus: %s\n"
320 "\tport: %s\n"
321 "\ttargets: %s\n",
322 (sender_status == SENDER_ON)? "on" : "off",
323 stringify_port(conf.udp_default_port_arg, "udp"),
324 tgts? tgts : "(none)"
325 );
326 free(tgts);
327 return ret;
328 }
329
330 static void udp_init_target_list(void)
331 {
332 struct sender_command_data scd;
333 int i;
334
335 INIT_LIST_HEAD(&targets);
336 for (i = 0; i < conf.udp_target_given; i++) {
337 if (parse_fec_url(conf.udp_target_arg[i], &scd) < 0) {
338 PARA_CRIT_LOG("syntax error for udp target option "
339 "#%d, ignoring\n", i);
340 continue;
341 }
342 udp_add_target(&scd);
343 }
344 }
345
346 static char *udp_help(void)
347 {
348 return make_message(
349 "usage: {on|off}\n"
350 "usage: {add|delete} host[:port][/packet_size:k:n]\n"
351 "examples: add 224.0.1.38:1500 (IPv4 multicast)\n"
352 " add 224.0.1.38:1500/1400:14:16\n"
353 " (likewise, using 1400 byte packets, with 14\n"
354 " data slices per 16 slice FEC group)\n"
355 " add 10.10.1.42 (using default port)\n"
356 " add [FF05::42]:1500 (IPv6 multicast)\n"
357 " add [::1] (IPv6 localhost/default port)\n"
358 " delete myhost.net (host with port wildcard)\n"
359 " delete [badc0de::1] (IPv6 with port wildcard)\n"
360 );
361 }
362
363 /**
364 * The init function of para_server's udp sender.
365 *
366 * \param s Pointer to the udp sender struct.
367 *
368 * It initializes all function pointers of \a s and the list of udp targets.
369 */
370 void udp_send_init(struct sender *s)
371 {
372 INIT_LIST_HEAD(&targets);
373 s->info = udp_info;
374 s->help = udp_help;
375 s->send = NULL;
376 s->pre_select = NULL;
377 s->post_select = NULL;
378 s->shutdown_clients = udp_shutdown_targets;
379 s->client_cmds[SENDER_ON] = udp_com_on;
380 s->client_cmds[SENDER_OFF] = udp_com_off;
381 s->client_cmds[SENDER_DENY] = NULL;
382 s->client_cmds[SENDER_ALLOW] = NULL;
383 s->client_cmds[SENDER_ADD] = udp_com_add;
384 s->client_cmds[SENDER_DELETE] = udp_com_delete;
385 sender_status = SENDER_OFF;
386 udp_init_target_list();
387 if (!conf.udp_no_autostart_given)
388 sender_status = SENDER_ON;
389 PARA_DEBUG_LOG("udp sender init complete\n");
390 }