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