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