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 */
10 #include <sys/socket.h>
15 #include "portable_io.h"
20 #include "udp_recv.cmdline.h"
26 /** The size of the receiver node buffer. */
27 #define UDP_RECV_CHUNK_SIZE (128 * 1024)
29 * Data specific to the udp receiver.
31 * \sa \ref receiver, \ref receiver_node.
33 struct private_udp_recv_data
{
34 /** The socket file descriptor. */
38 static void udp_recv_pre_select(struct sched
*s
, struct task
*t
)
40 struct receiver_node
*rn
= container_of(t
, struct receiver_node
, task
);
41 struct private_udp_recv_data
*purd
= rn
->private_data
;
43 para_fd_set(purd
->fd
, &s
->rfds
, &s
->max_fileno
);
46 static int enough_space(size_t nbytes
, size_t loaded
)
48 return nbytes
+ loaded
< UDP_RECV_CHUNK_SIZE
;
51 static int add_rn_output(struct receiver_node
*rn
, char *buf
, size_t len
)
55 if (!enough_space(len
, rn
->loaded
))
56 return -E_UDP_OVERRUN
;
57 memcpy(rn
->buf
+ rn
->loaded
, buf
, len
);
62 static void udp_recv_post_select(__a_unused
struct sched
*s
, struct task
*t
)
64 struct receiver_node
*rn
= container_of(t
, struct receiver_node
, task
);
65 struct private_udp_recv_data
*purd
= rn
->private_data
;
67 char tmpbuf
[UDP_RECV_CHUNK_SIZE
];
70 if (rn
->output_error
&& *rn
->output_error
< 0) {
71 t
->error
= *rn
->output_error
;
74 if (!FD_ISSET(purd
->fd
, &s
->rfds
))
76 ret
= recv_bin_buffer(purd
->fd
, tmpbuf
, UDP_RECV_CHUNK_SIZE
);
78 if (is_errno(ret
, EINTR
) || is_errno(ret
, EAGAIN
))
83 t
->error
= -E_RECV_EOF
;
87 if (packet_size
>= FEC_EOF_PACKET_LEN
)
88 if (!memcmp(tmpbuf
, FEC_EOF_PACKET
, FEC_EOF_PACKET_LEN
))
90 t
->error
= add_rn_output(rn
, tmpbuf
, packet_size
);
97 static void udp_shutdown(void)
102 static void udp_recv_close(struct receiver_node
*rn
)
104 struct private_udp_recv_data
*purd
= rn
->private_data
;
108 free(rn
->private_data
);
112 static void *udp_recv_parse_config(int argc
, char **argv
)
115 struct udp_recv_args_info
*tmp
=
116 para_calloc(sizeof(struct udp_recv_args_info
));
118 ret
= udp_recv_cmdline_parser(argc
, argv
, tmp
)? -E_UDP_SYNTAX
: 1;
126 * Perform AF-independent joining of multicast receive addresses.
128 * \param fd Bound socket descriptor.
129 * \param iface The receiving multicast interface, or NULL for the default.
131 * \return Zero if okay, negative on error.
133 static int mcast_receiver_setup(int fd
, const char *iface
)
135 struct sockaddr_storage ss
;
136 socklen_t sslen
= sizeof(ss
);
137 int id
= iface
== NULL
? 0 : if_nametoindex(iface
);
139 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0)
142 if (iface
!= NULL
&& id
== 0)
143 PARA_WARNING_LOG("could not resolve interface %s, using default", iface
);
145 switch (ss
.ss_family
) {
147 if (IN_MULTICAST(htonl(((struct sockaddr_in
*)&ss
)->sin_addr
.s_addr
))) {
151 m4
.imr_address
.s_addr
= INADDR_ANY
;
156 m4
.imr_interface
.s_addr
= INADDR_ANY
;
158 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported.");
161 m4
.imr_multiaddr
= ((struct sockaddr_in
*)&ss
)->sin_addr
;
163 if (setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
, &m4
, sizeof(m4
)) < 0)
168 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)&ss
)->sin6_addr
)) {
171 memset(&m6
, 0, sizeof(m6
));
172 memcpy(&m6
.ipv6mr_multiaddr
, &((struct sockaddr_in6
*)&ss
)->sin6_addr
, 16);
173 m6
.ipv6mr_interface
= id
;
174 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
, &m6
, sizeof(m6
)) < 0)
179 PARA_ERROR_LOG("address family %d not supported", ss
.ss_family
);
180 return -E_ADDRESS_LOOKUP
;
183 return -ERRNO_TO_PARA_ERROR(errno
);
186 static int udp_recv_open(struct receiver_node
*rn
)
188 struct private_udp_recv_data
*purd
;
189 struct udp_recv_args_info
*c
= rn
->conf
;
190 char *iface
= c
->iface_given
? c
->iface_arg
: NULL
;
193 rn
->buf
= para_calloc(UDP_RECV_CHUNK_SIZE
);
194 rn
->private_data
= para_calloc(sizeof(struct private_udp_recv_data
));
195 purd
= rn
->private_data
;
197 ret
= makesock(AF_UNSPEC
, IPPROTO_UDP
, 1, c
->host_arg
, c
->port_arg
);
202 ret
= mcast_receiver_setup(purd
->fd
, iface
);
208 ret
= mark_fd_nonblocking(purd
->fd
);
211 PARA_NOTICE_LOG("receiving from %s:%d, fd=%d\n", c
->host_arg
,
212 c
->port_arg
, purd
->fd
);
215 free(rn
->private_data
);
221 * The init function of the udp receiver.
223 * \param r Pointer to the receiver struct to initialize.
225 * Initialize all function pointers of \a r.
227 void udp_recv_init(struct receiver
*r
)
229 struct udp_recv_args_info dummy
;
231 udp_recv_cmdline_parser_init(&dummy
);
232 r
->shutdown
= udp_shutdown
;
233 r
->open
= udp_recv_open
;
234 r
->close
= udp_recv_close
;
235 r
->pre_select
= udp_recv_pre_select
;
236 r
->post_select
= udp_recv_post_select
;
237 r
->parse_config
= udp_recv_parse_config
;
238 r
->help
= (struct ggo_help
) {
239 .short_help
= udp_recv_args_info_help
,
240 .detailed_help
= udp_recv_args_info_detailed_help