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