759caa3d5646bf15d0b71cec0bb6932149a7c3c3
[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         t->error = add_rn_output(rn, tmpbuf, packet_size);
86         if (t->error < 0)
87                 return;
88 success:
89         t->error = 1;
90 }
91
92 static void udp_shutdown(void)
93 {
94         return;
95 }
96
97 static void udp_recv_close(struct receiver_node *rn)
98 {
99         struct private_udp_recv_data *purd = rn->private_data;
100
101         if (purd->fd >= 0)
102                 close(purd->fd);
103         free(rn->private_data);
104         free(rn->buf);
105 }
106
107 static void *udp_recv_parse_config(int argc, char **argv)
108 {
109         int ret;
110         struct udp_recv_args_info *tmp =
111                 para_calloc(sizeof(struct udp_recv_args_info));
112
113         ret = udp_recv_cmdline_parser(argc, argv, tmp)? -E_UDP_SYNTAX : 1;
114         if (ret >= 0)
115                 return tmp;
116         free(tmp);
117         return NULL;
118 }
119
120 /*
121  * Perform AF-independent joining of multicast receive addresses.
122  *
123  * \param fd    Bound socket descriptor.
124  * \param iface The receiving multicast interface, or NULL for the default.
125  *
126  * \return Zero if okay, negative on error.
127  */
128 static int mcast_receiver_setup(int fd, const char *iface)
129 {
130         struct sockaddr_storage ss;
131         socklen_t sslen = sizeof(ss);
132         int id = iface == NULL ? 0 : if_nametoindex(iface);
133
134         if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0)
135                 goto err;
136
137         if (iface != NULL && id == 0)
138                 PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
139
140         switch (ss.ss_family) {
141         case AF_INET:
142                 if (IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr))) {
143 #ifdef HAVE_IP_MREQN
144                         struct ip_mreqn m4;
145
146                         m4.imr_address.s_addr   = INADDR_ANY;
147                         m4.imr_ifindex          = id;
148 #else
149                         struct ip_mreq m4;
150
151                         m4.imr_interface.s_addr = INADDR_ANY;
152                         if (id != 0)
153                                 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported.");
154
155 #endif
156                         m4.imr_multiaddr        = ((struct sockaddr_in *)&ss)->sin_addr;
157
158                         if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m4, sizeof(m4)) < 0)
159                                 break;
160                 }
161                 return 0;
162         case AF_INET6:
163                 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
164                         struct ipv6_mreq m6;
165
166                         memset(&m6, 0, sizeof(m6));
167                         memcpy(&m6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
168                         m6.ipv6mr_interface = id;
169                         if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &m6, sizeof(m6)) < 0)
170                                 break;
171                 }
172                 return 0;
173         default:
174                 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
175                 return -E_ADDRESS_LOOKUP;
176         }
177 err:
178         return -ERRNO_TO_PARA_ERROR(errno);
179 }
180
181 static int udp_recv_open(struct receiver_node *rn)
182 {
183         struct private_udp_recv_data *purd;
184         struct udp_recv_args_info *c = rn->conf;
185         char  *iface = c->iface_given ? c->iface_arg : NULL;
186         int ret;
187
188         rn->buf = para_calloc(UDP_RECV_CHUNK_SIZE);
189         rn->private_data = para_calloc(sizeof(struct private_udp_recv_data));
190         purd = rn->private_data;
191
192         ret = makesock(AF_UNSPEC, IPPROTO_UDP, 1, c->host_arg, c->port_arg);
193         if (ret < 0)
194                 goto err;
195         purd->fd = ret;
196
197         ret = mcast_receiver_setup(purd->fd, iface);
198         if (ret < 0) {
199                 close(purd->fd);
200                 return ret;
201         }
202
203         ret = mark_fd_nonblocking(purd->fd);
204         if (ret < 0)
205                 goto err;
206         PARA_NOTICE_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
207                 c->port_arg, purd->fd);
208         return purd->fd;
209 err:
210         free(rn->private_data);
211         free(rn->buf);
212         return ret;
213 }
214
215 /**
216  * The init function of the udp receiver.
217  *
218  * \param r Pointer to the receiver struct to initialize.
219  *
220  * Initialize all function pointers of \a r.
221  */
222 void udp_recv_init(struct receiver *r)
223 {
224         struct udp_recv_args_info dummy;
225
226         udp_recv_cmdline_parser_init(&dummy);
227         r->shutdown = udp_shutdown;
228         r->open = udp_recv_open;
229         r->close = udp_recv_close;
230         r->pre_select = udp_recv_pre_select;
231         r->post_select = udp_recv_post_select;
232         r->parse_config = udp_recv_parse_config;
233         r->help = (struct ggo_help) {
234                 .short_help = udp_recv_args_info_help,
235                 .detailed_help = udp_recv_args_info_detailed_help
236         };
237 }