The opus audio format handler.
[paraslash.git] / udp_recv.c
1 /*
2 * Copyright (C) 2005-2013 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 <regex.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 "buffer_tree.h"
19 #include "recv.h"
20 #include "udp_recv.cmdline.h"
21 #include "string.h"
22 #include "net.h"
23 #include "fd.h"
24
25 static void udp_recv_pre_select(struct sched *s, struct task *t)
26 {
27 struct receiver_node *rn = container_of(t, struct receiver_node, task);
28
29 if (generic_recv_pre_select(s, t) <= 0)
30 return;
31 para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
32 }
33
34 static int udp_check_eof(size_t sz, struct iovec iov[2])
35 {
36 if (sz < FEC_EOF_PACKET_LEN)
37 return 0;
38 if (iov[0].iov_len >= FEC_EOF_PACKET_LEN) {
39 if (memcmp(iov[0].iov_base, FEC_EOF_PACKET,
40 FEC_EOF_PACKET_LEN) != 0)
41 return 0;
42 return -E_RECV_EOF;
43 }
44 if (memcmp(iov[0].iov_base, FEC_EOF_PACKET, iov[0].iov_len) != 0)
45 return 0;
46 if (memcmp(iov[1].iov_base, FEC_EOF_PACKET + iov[0].iov_len,
47 FEC_EOF_PACKET_LEN - iov[0].iov_len) != 0)
48 return 0;
49 return -E_RECV_EOF;
50 }
51
52 static int udp_recv_post_select(__a_unused struct sched *s, struct task *t)
53 {
54 struct receiver_node *rn = container_of(t, struct receiver_node, task);
55 struct btr_node *btrn = rn->btrn;
56 size_t num_bytes;
57 struct iovec iov[2];
58 int ret, readv_ret, iovcnt;
59
60 ret = task_get_notification(t);
61 if (ret < 0)
62 goto out;
63 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
64 if (ret <= 0)
65 goto out;
66 iovcnt = btr_pool_get_buffers(rn->btrp, iov);
67 ret = -E_UDP_OVERRUN;
68 if (iovcnt == 0)
69 goto out;
70 ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
71 if (num_bytes == 0)
72 goto out;
73 readv_ret = ret;
74 ret = udp_check_eof(num_bytes, iov);
75 if (ret < 0)
76 goto out;
77 if (iov[0].iov_len >= num_bytes)
78 btr_add_output_pool(rn->btrp, num_bytes, btrn);
79 else { /* both buffers contain data */
80 btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
81 btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len,
82 btrn);
83 }
84 ret = readv_ret;
85 out:
86 if (ret < 0) {
87 btr_remove_node(&rn->btrn);
88 close(rn->fd);
89 rn->fd = -1;
90 }
91 return ret;
92 }
93
94 static void udp_recv_close(struct receiver_node *rn)
95 {
96 if (rn->fd >= 0)
97 close(rn->fd);
98 btr_pool_free(rn->btrp);
99 }
100
101 static void *udp_recv_parse_config(int argc, char **argv)
102 {
103 int ret;
104 struct udp_recv_args_info *tmp =
105 para_calloc(sizeof(struct udp_recv_args_info));
106
107 ret = udp_recv_cmdline_parser(argc, argv, tmp)? -E_UDP_SYNTAX : 1;
108 if (ret >= 0)
109 return tmp;
110 free(tmp);
111 return NULL;
112 }
113
114 /*
115 * Perform AF-independent joining of multicast receive addresses.
116 *
117 * \param fd Bound socket descriptor.
118 * \param iface The receiving multicast interface, or NULL for the default.
119 *
120 * \return Zero if okay, negative on error.
121 */
122 static int mcast_receiver_setup(int fd, const char *iface)
123 {
124 struct sockaddr_storage ss;
125 socklen_t sslen = sizeof(ss);
126 int id = iface == NULL ? 0 : if_nametoindex(iface);
127
128 if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0)
129 goto err;
130 assert(ss.ss_family == AF_INET || ss.ss_family == AF_INET6);
131
132 if (iface != NULL && id == 0)
133 PARA_WARNING_LOG("could not resolve interface %s, using default\n",
134 iface);
135
136 switch (ss.ss_family) {
137 case AF_INET:
138 if (IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr))) {
139 #ifdef HAVE_IP_MREQN
140 struct ip_mreqn m4;
141
142 m4.imr_address.s_addr = INADDR_ANY;
143 m4.imr_ifindex = id;
144 #else
145 struct ip_mreq m4;
146
147 m4.imr_interface.s_addr = INADDR_ANY;
148 if (id != 0)
149 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported\n");
150
151 #endif
152 m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
153
154 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
155 &m4, sizeof(m4)) < 0)
156 break;
157 }
158 return 0;
159 case AF_INET6:
160 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
161 struct ipv6_mreq m6;
162
163 memset(&m6, 0, sizeof(m6));
164 memcpy(&m6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
165 m6.ipv6mr_interface = id;
166 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &m6, sizeof(m6)) < 0)
167 break;
168 }
169 return 0;
170 }
171 err:
172 return -ERRNO_TO_PARA_ERROR(errno);
173 }
174
175 static int udp_recv_open(struct receiver_node *rn)
176 {
177 struct udp_recv_args_info *c = rn->conf;
178 char *iface = c->iface_given ? c->iface_arg : NULL;
179 int ret;
180
181 ret = makesock(IPPROTO_UDP, 1, c->host_arg, c->port_arg, NULL);
182 if (ret < 0)
183 goto err;
184 rn->fd = ret;
185
186 ret = mcast_receiver_setup(rn->fd, iface);
187 if (ret < 0) {
188 close(rn->fd);
189 goto err;
190 }
191
192 ret = mark_fd_nonblocking(rn->fd);
193 if (ret < 0) {
194 close(rn->fd);
195 goto err;
196 }
197 PARA_INFO_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
198 c->port_arg, rn->fd);
199 rn->btrp = btr_pool_new("udp_recv", 320 * 1024);
200 return rn->fd;
201 err:
202 return ret;
203 }
204
205 static void udp_recv_free_config(void *conf)
206 {
207 udp_recv_cmdline_parser_free(conf);
208 free(conf);
209 }
210
211 /**
212 * The init function of the udp receiver.
213 *
214 * \param r Pointer to the receiver struct to initialize.
215 *
216 * Initialize all function pointers of \a r.
217 */
218 void udp_recv_init(struct receiver *r)
219 {
220 struct udp_recv_args_info dummy;
221
222 udp_recv_cmdline_parser_init(&dummy);
223 r->open = udp_recv_open;
224 r->close = udp_recv_close;
225 r->pre_select = udp_recv_pre_select;
226 r->post_select = udp_recv_post_select;
227 r->parse_config = udp_recv_parse_config;
228 r->free_config = udp_recv_free_config;
229 r->help = (struct ggo_help) {
230 .short_help = udp_recv_args_info_help,
231 .detailed_help = udp_recv_args_info_detailed_help
232 };
233 udp_recv_cmdline_parser_free(&dummy);
234 }