Major audio format handler cleanups.
[paraslash.git] / udp_recv.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 /** \file udp_recv.c Paraslash's udp receiver */
7
8 #include <dirent.h>
9 #include <sys/socket.h>
10 #include <net/if.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "portable_io.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "ggo.h"
18 #include "recv.h"
19 #include "udp_recv.cmdline.h"
20 #include "audiod.h"
21 #include "string.h"
22 #include "net.h"
23 #include "fd.h"
24
25 /** The size of the receiver node buffer. */
26 #define UDP_RECV_CHUNK_SIZE (128 * 1024)
27 /**
28 * Data specific to the udp receiver.
29 *
30 * \sa \ref receiver, \ref receiver_node.
31 */
32 struct private_udp_recv_data {
33 /** The socket file descriptor. */
34 int fd;
35 };
36
37 static void udp_recv_pre_select(struct sched *s, struct task *t)
38 {
39 struct receiver_node *rn = container_of(t, struct receiver_node, task);
40 struct private_udp_recv_data *purd = rn->private_data;
41
42 para_fd_set(purd->fd, &s->rfds, &s->max_fileno);
43 }
44
45 static int enough_space(size_t nbytes, size_t loaded)
46 {
47 return nbytes + loaded < UDP_RECV_CHUNK_SIZE;
48 }
49
50 static int add_rn_output(struct receiver_node *rn, char *buf, size_t len)
51 {
52 if (!len)
53 return 1;
54 if (!enough_space(len, rn->loaded))
55 return -E_UDP_OVERRUN;
56 memcpy(rn->buf + rn->loaded, buf, len);
57 rn->loaded += len;
58 return 1;
59 }
60
61 static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
62 {
63 struct receiver_node *rn = container_of(t, struct receiver_node, task);
64 struct private_udp_recv_data *purd = rn->private_data;
65 int ret;
66 char tmpbuf[UDP_RECV_CHUNK_SIZE];
67 size_t packet_size;
68
69 if (rn->output_error && *rn->output_error < 0) {
70 t->error = *rn->output_error;
71 return;
72 }
73 if (!FD_ISSET(purd->fd, &s->rfds))
74 return;
75 ret = recv_bin_buffer(purd->fd, tmpbuf, UDP_RECV_CHUNK_SIZE);
76 if (ret < 0) {
77 if (is_errno(ret, EINTR) || is_errno(ret, EAGAIN))
78 goto success;
79 t->error = ret;
80 return;
81 }
82 t->error = -E_RECV_EOF;
83 if (!ret)
84 return;
85 packet_size = ret;
86 if (packet_size >= FEC_EOF_PACKET_LEN)
87 if (!memcmp(tmpbuf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN))
88 return;
89 t->error = add_rn_output(rn, tmpbuf, packet_size);
90 if (t->error < 0)
91 return;
92 success:
93 t->error = 1;
94 }
95
96 static void udp_shutdown(void)
97 {
98 return;
99 }
100
101 static void udp_recv_close(struct receiver_node *rn)
102 {
103 struct private_udp_recv_data *purd = rn->private_data;
104
105 if (purd->fd >= 0)
106 close(purd->fd);
107 free(rn->private_data);
108 free(rn->buf);
109 }
110
111 static void *udp_recv_parse_config(int argc, char **argv)
112 {
113 int ret;
114 struct udp_recv_args_info *tmp =
115 para_calloc(sizeof(struct udp_recv_args_info));
116
117 ret = udp_recv_cmdline_parser(argc, argv, tmp)? -E_UDP_SYNTAX : 1;
118 if (ret >= 0)
119 return tmp;
120 free(tmp);
121 return NULL;
122 }
123
124 /*
125 * Perform AF-independent joining of multicast receive addresses.
126 *
127 * \param fd Bound socket descriptor.
128 * \param iface The receiving multicast interface, or NULL for the default.
129 *
130 * \return Zero if okay, negative on error.
131 */
132 static int mcast_receiver_setup(int fd, const char *iface)
133 {
134 struct sockaddr_storage ss;
135 socklen_t sslen = sizeof(ss);
136 int id = iface == NULL ? 0 : if_nametoindex(iface);
137
138 if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0)
139 goto err;
140
141 if (iface != NULL && id == 0)
142 PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
143
144 switch (ss.ss_family) {
145 case AF_INET:
146 if (IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr))) {
147 #ifdef HAVE_IP_MREQN
148 struct ip_mreqn m4;
149
150 m4.imr_address.s_addr = INADDR_ANY;
151 m4.imr_ifindex = id;
152 #else
153 struct ip_mreq m4;
154
155 m4.imr_interface.s_addr = INADDR_ANY;
156 if (id != 0)
157 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported.");
158
159 #endif
160 m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
161
162 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m4, sizeof(m4)) < 0)
163 break;
164 }
165 return 0;
166 case AF_INET6:
167 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
168 struct ipv6_mreq m6;
169
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)
174 break;
175 }
176 return 0;
177 default:
178 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
179 return -E_ADDRESS_LOOKUP;
180 }
181 err:
182 return -ERRNO_TO_PARA_ERROR(errno);
183 }
184
185 static int udp_recv_open(struct receiver_node *rn)
186 {
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;
190 int ret;
191
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;
195
196 ret = makesock(AF_UNSPEC, IPPROTO_UDP, 1, c->host_arg, c->port_arg);
197 if (ret < 0)
198 goto err;
199 purd->fd = ret;
200
201 ret = mcast_receiver_setup(purd->fd, iface);
202 if (ret < 0) {
203 close(purd->fd);
204 return ret;
205 }
206
207 ret = mark_fd_nonblocking(purd->fd);
208 if (ret < 0)
209 goto err;
210 PARA_NOTICE_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
211 c->port_arg, purd->fd);
212 return purd->fd;
213 err:
214 free(rn->private_data);
215 free(rn->buf);
216 return ret;
217 }
218
219 /**
220 * The init function of the udp receiver.
221 *
222 * \param r Pointer to the receiver struct to initialize.
223 *
224 * Initialize all function pointers of \a r.
225 */
226 void udp_recv_init(struct receiver *r)
227 {
228 struct udp_recv_args_info dummy;
229
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
240 };
241 }