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