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