2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 /** \file udp_recv.c Paraslash's udp receiver */
9 #include <sys/socket.h>
14 #include "portable_io.h"
19 #include "udp_recv.cmdline.h"
25 /** The size of the receiver node buffer. */
26 #define UDP_RECV_CHUNK_SIZE (128 * 1024)
28 * Data specific to the udp receiver.
30 * \sa \ref receiver, \ref receiver_node.
32 struct private_udp_recv_data
{
33 /** The socket file descriptor. */
37 static void udp_recv_pre_select(struct sched
*s
, struct task
*t
)
39 struct receiver_node
*rn
= container_of(t
, struct receiver_node
, task
);
40 struct private_udp_recv_data
*purd
= rn
->private_data
;
42 para_fd_set(purd
->fd
, &s
->rfds
, &s
->max_fileno
);
45 static int enough_space(size_t nbytes
, size_t loaded
)
47 return nbytes
+ loaded
< UDP_RECV_CHUNK_SIZE
;
50 static int add_rn_output(struct receiver_node
*rn
, char *buf
, size_t len
)
54 if (!enough_space(len
, rn
->loaded
))
55 return -E_UDP_OVERRUN
;
56 memcpy(rn
->buf
+ rn
->loaded
, buf
, len
);
61 static void udp_recv_post_select(__a_unused
struct sched
*s
, struct task
*t
)
63 struct receiver_node
*rn
= container_of(t
, struct receiver_node
, task
);
64 struct private_udp_recv_data
*purd
= rn
->private_data
;
66 char tmpbuf
[UDP_RECV_CHUNK_SIZE
];
69 if (rn
->output_error
&& *rn
->output_error
< 0) {
70 t
->error
= *rn
->output_error
;
73 if (!FD_ISSET(purd
->fd
, &s
->rfds
))
75 ret
= recv_bin_buffer(purd
->fd
, tmpbuf
, UDP_RECV_CHUNK_SIZE
);
77 if (is_errno(ret
, EINTR
) || is_errno(ret
, EAGAIN
))
82 t
->error
= -E_RECV_EOF
;
86 if (packet_size
>= FEC_EOF_PACKET_LEN
)
87 if (!memcmp(tmpbuf
, FEC_EOF_PACKET
, FEC_EOF_PACKET_LEN
))
89 t
->error
= add_rn_output(rn
, tmpbuf
, packet_size
);
96 static void udp_shutdown(void)
101 static void udp_recv_close(struct receiver_node
*rn
)
103 struct private_udp_recv_data
*purd
= rn
->private_data
;
107 free(rn
->private_data
);
111 static void *udp_recv_parse_config(int argc
, char **argv
)
114 struct udp_recv_args_info
*tmp
=
115 para_calloc(sizeof(struct udp_recv_args_info
));
117 ret
= udp_recv_cmdline_parser(argc
, argv
, tmp
)? -E_UDP_SYNTAX
: 1;
125 * Perform AF-independent joining of multicast receive addresses.
127 * \param fd Bound socket descriptor.
128 * \param iface The receiving multicast interface, or NULL for the default.
130 * \return Zero if okay, negative on error.
132 static int mcast_receiver_setup(int fd
, const char *iface
)
134 struct sockaddr_storage ss
;
135 socklen_t sslen
= sizeof(ss
);
136 int id
= iface
== NULL
? 0 : if_nametoindex(iface
);
138 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0)
141 if (iface
!= NULL
&& id
== 0)
142 PARA_WARNING_LOG("could not resolve interface %s, using default", iface
);
144 switch (ss
.ss_family
) {
146 if (IN_MULTICAST(htonl(((struct sockaddr_in
*)&ss
)->sin_addr
.s_addr
))) {
150 m4
.imr_address
.s_addr
= INADDR_ANY
;
155 m4
.imr_interface
.s_addr
= INADDR_ANY
;
157 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported.");
160 m4
.imr_multiaddr
= ((struct sockaddr_in
*)&ss
)->sin_addr
;
162 if (setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
, &m4
, sizeof(m4
)) < 0)
167 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)&ss
)->sin6_addr
)) {
170 memset(&m6
, 0, sizeof(m6
));
171 memcpy(&m6
.ipv6mr_multiaddr
, &((struct sockaddr_in6
*)&ss
)->sin6_addr
, 16);
172 m6
.ipv6mr_interface
= id
;
173 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
, &m6
, sizeof(m6
)) < 0)
178 PARA_ERROR_LOG("address family %d not supported", ss
.ss_family
);
179 return -E_ADDRESS_LOOKUP
;
182 return -ERRNO_TO_PARA_ERROR(errno
);
185 static int udp_recv_open(struct receiver_node
*rn
)
187 struct private_udp_recv_data
*purd
;
188 struct udp_recv_args_info
*c
= rn
->conf
;
189 char *iface
= c
->iface_given
? c
->iface_arg
: NULL
;
192 rn
->buf
= para_calloc(UDP_RECV_CHUNK_SIZE
);
193 rn
->private_data
= para_calloc(sizeof(struct private_udp_recv_data
));
194 purd
= rn
->private_data
;
196 ret
= makesock(AF_UNSPEC
, IPPROTO_UDP
, 1, c
->host_arg
, c
->port_arg
);
201 ret
= mcast_receiver_setup(purd
->fd
, iface
);
207 ret
= mark_fd_nonblocking(purd
->fd
);
210 PARA_NOTICE_LOG("receiving from %s:%d, fd=%d\n", c
->host_arg
,
211 c
->port_arg
, purd
->fd
);
214 free(rn
->private_data
);
220 * The init function of the udp receiver.
222 * \param r Pointer to the receiver struct to initialize.
224 * Initialize all function pointers of \a r.
226 void udp_recv_init(struct receiver
*r
)
228 struct udp_recv_args_info dummy
;
230 udp_recv_cmdline_parser_init(&dummy
);
231 r
->shutdown
= udp_shutdown
;
232 r
->open
= udp_recv_open
;
233 r
->close
= udp_recv_close
;
234 r
->pre_select
= udp_recv_pre_select
;
235 r
->post_select
= udp_recv_post_select
;
236 r
->parse_config
= udp_recv_parse_config
;
237 r
->help
= (struct ggo_help
) {
238 .short_help
= udp_recv_args_info_help
,
239 .detailed_help
= udp_recv_args_info_detailed_help
241 udp_recv_cmdline_parser_free(&dummy
);