2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file udp_send.c Para_server's udp sender. */
14 #include "server.cmdline.h"
24 #include "portable_io.h"
28 #include "close_on_fork.h"
29 #include "chunk_queue.h"
31 /** Describes one entry in the list of targets for the udp sender. */
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
];
41 /** The list of queued chunks for this fd. */
42 struct chunk_queue
*cq
;
43 struct fec_client
*fc
;
44 struct fec_client_parms fcp
;
47 static struct list_head targets
;
48 static int sender_status
;
50 static void udp_close_target(struct udp_target
*ut
)
55 del_close_on_fork_list(ut
->fd
);
61 static void udp_delete_target(struct udp_target
*ut
, const char *msg
)
63 PARA_NOTICE_LOG("deleting %s#%d (%s) from list\n", ut
->host
,
66 vss_del_fec_client(ut
->fc
);
72 * Perform AF-independent multicast sender setup.
74 * \param fd The connected socket descriptor.
75 * \param ttl UDPv4 multicast TTL or UDPv6 multicast number of hops.
76 * Use -1 to mean default, 0..255 otherwise.
77 * \param iface The outgoing multicast interface, or NULL for the default.
79 * \return Zero if okay, negative on error.
81 static int mcast_sender_setup(struct udp_target
*ut
, int ttl
, char *iface
)
83 struct sockaddr_storage ss
;
84 socklen_t sslen
= sizeof(ss
);
87 int id
= iface
== NULL
? 0 : if_nametoindex(iface
);
89 if (getpeername(ut
->fd
, (struct sockaddr
*)&ss
, &sslen
) < 0)
92 if (iface
!= NULL
&& id
== 0)
93 PARA_WARNING_LOG("could not resolve interface %s, using default", iface
);
95 /* RFC 3493, 5.2: -1 means 'use kernel default' */
96 if (ttl
< 0 || ttl
> 255)
99 switch (ss
.ss_family
) {
101 if (!IN_MULTICAST(htonl(((struct sockaddr_in
*)&ss
)->sin_addr
.s_addr
)))
107 memset(&mn
, 0, sizeof(mn
));
109 if (setsockopt(ut
->fd
, IPPROTO_IP
, IP_MULTICAST_IF
, &mn
, sizeof(mn
)) < 0)
112 PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
116 * Enable receiving multicast messages generated on the local host
117 * At least on Linux, this is enabled by default.
119 if (setsockopt(ut
->fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, &on
, sizeof(on
)) < 0)
122 /* Default: use local subnet (do not flood out into the WAN) */
125 if (setsockopt(ut
->fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, &ttl
, sizeof(ttl
)) < 0)
129 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)&ss
)->sin6_addr
))
132 setsockopt(ut
->fd
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
, &id
, sizeof(id
)) < 0)
134 if (setsockopt(ut
->fd
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &on
, sizeof(on
)) < 0)
136 if (setsockopt(ut
->fd
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &ttl
, sizeof(ttl
)) < 0)
140 PARA_ERROR_LOG("address family %d not supported", ss
.ss_family
);
141 return -E_ADDRESS_LOOKUP
;
144 return -ERRNO_TO_PARA_ERROR(errno
);
147 /** The maximal size of the per-target chunk queue. */
148 #define UDP_CQ_BYTES 40000
150 static int udp_init_session(struct udp_target
*ut
)
155 if (ut
->fd
>= 0) /* nothing to do */
158 ret
= makesock(AF_UNSPEC
, IPPROTO_UDP
, 0, ut
->host
, ut
->port
);
163 if (conf
.udp_mcast_iface_given
)
164 iface
= conf
.udp_mcast_iface_arg
;
166 ret
= mcast_sender_setup(ut
, conf
.udp_ttl_arg
, iface
);
172 ret
= mark_fd_nonblocking(ut
->fd
);
177 add_close_on_fork_list(ut
->fd
);
178 ut
->cq
= cq_new(UDP_CQ_BYTES
);
179 PARA_NOTICE_LOG("sending to udp %s#%d\n", ut
->host
, ut
->port
);
183 static void udp_shutdown_targets(void)
185 struct udp_target
*ut
, *tmp
;
186 const char *buf
= NULL
;
187 size_t len
= 0; /* STFU, gcc */
189 list_for_each_entry_safe(ut
, tmp
, &targets
, node
) {
193 len
= vss_get_fec_eof_packet(&buf
);
194 write(ut
->fd
, buf
, len
);
195 udp_close_target(ut
);
199 static int udp_com_on(__a_unused
struct sender_command_data
*scd
)
201 sender_status
= SENDER_ON
;
205 static int udp_com_off(__a_unused
struct sender_command_data
*scd
)
207 udp_shutdown_targets();
208 sender_status
= SENDER_OFF
;
212 static int udp_com_delete(struct sender_command_data
*scd
)
214 struct udp_target
*ut
, *tmp
;
216 list_for_each_entry_safe(ut
, tmp
, &targets
, node
) {
217 /* Unspecified port means wildcard port match */
218 if (scd
->port
> 0 && scd
->port
!= ut
->port
)
220 if (strcmp(ut
->host
, scd
->host
))
222 udp_delete_target(ut
, "com_delete");
227 static int udp_send_fec(char *buf
, size_t len
, void *private_data
)
229 struct udp_target
*ut
= private_data
;
230 int ret
= udp_init_session(ut
);
234 ret
= send_queued_chunks(ut
->fd
, ut
->cq
, 0);
239 if (!ret
) { /* still data left in the queue */
240 ret
= cq_enqueue(ut
->cq
, buf
, len
);
244 ret
= write_nonblock(ut
->fd
, buf
, len
, 0);
248 ret
= cq_enqueue(ut
->cq
, buf
+ ret
, len
- ret
);
254 udp_delete_target(ut
, para_strerror(-ret
));
258 static void udp_add_target(const char *host
, int port
)
260 struct udp_target
*ut
= para_calloc(sizeof(struct udp_target
));
262 strncpy(ut
->host
, host
, sizeof(ut
->host
));
263 ut
->port
= port
> 0 ? port
: conf
.udp_default_port_arg
;
264 ut
->fd
= -1; /* not yet connected */
265 PARA_INFO_LOG("adding to target list (%s#%d)\n",
267 para_list_add(&ut
->node
, &targets
);
268 ut
->fcp
.slices_per_group
= 16;
269 ut
->fcp
.data_slices_per_group
= 14;
270 ut
->fcp
.max_slice_bytes
= 1400;
271 ut
->fcp
.send
= udp_send_fec
;
272 ut
->fcp
.private_data
= ut
;
273 vss_add_fec_client(&ut
->fcp
, &ut
->fc
);
276 static int udp_com_add(struct sender_command_data
*scd
)
278 udp_add_target(scd
->host
, scd
->port
);
282 static char *udp_info(void)
284 struct udp_target
*ut
;
285 char *ret
, *tgts
= NULL
;
287 list_for_each_entry(ut
, &targets
, node
) {
288 bool is_v6
= strchr(ut
->host
, ':') != NULL
;
289 char *tmp
= make_message("%s%s%s%s:%d ", tgts
? : "",
290 is_v6
? "[" : "", ut
->host
,
291 is_v6
? "]" : "", ut
->port
);
300 (sender_status
== SENDER_ON
)? "on" : "off",
301 conf
.udp_default_port_arg
,
302 tgts
? tgts
: "(none)"
308 static void udp_init_target_list(void)
310 char host
[MAX_HOSTLEN
];
313 INIT_LIST_HEAD(&targets
);
314 for (i
= 0; i
< conf
.udp_target_given
; i
++)
315 if (parse_url(conf
.udp_target_arg
[i
], host
,
316 sizeof(host
), &port
) == NULL
)
317 PARA_CRIT_LOG("syntax error for udp target option "
318 "#%d, ignoring\n", i
);
320 udp_add_target(host
, port
);
323 static char *udp_help(void)
327 "usage: {add|delete} host[:port]\n"
328 "examples: add 224.0.1.38:1500 (IPv4 multicast)\n"
329 " add 10.10.1.42 (using default port)\n"
330 " add [FF05::42]:1500 (IPv6 multicast)\n"
331 " add [::1] (IPv6 localhost/default port)\n"
332 " delete myhost.net (host with port wildcard)\n"
333 " delete [badc0de::1] (IPv6 with port wildcard)\n"
338 * The init function of para_server's udp sender.
340 * \param s Pointer to the http sender struct.
342 * It initializes all function pointers of \a s and the list of udp targets.
344 void udp_send_init(struct sender
*s
)
346 INIT_LIST_HEAD(&targets
);
350 s
->pre_select
= NULL
;
351 s
->post_select
= NULL
;
352 s
->shutdown_clients
= udp_shutdown_targets
;
353 s
->client_cmds
[SENDER_ON
] = udp_com_on
;
354 s
->client_cmds
[SENDER_OFF
] = udp_com_off
;
355 s
->client_cmds
[SENDER_DENY
] = NULL
;
356 s
->client_cmds
[SENDER_ALLOW
] = NULL
;
357 s
->client_cmds
[SENDER_ADD
] = udp_com_add
;
358 s
->client_cmds
[SENDER_DELETE
] = udp_com_delete
;
359 sender_status
= SENDER_OFF
;
360 udp_init_target_list();
361 if (!conf
.udp_no_autostart_given
)
362 sender_status
= SENDER_ON
;
363 PARA_DEBUG_LOG("udp sender init complete\n");