udp_recv: Kill non-btr code.
[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 "string.h"
22 #include "net.h"
23 #include "fd.h"
24 #include "buffer_tree.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 struct btr_pool *btrp;
37 };
38
39 static void udp_recv_pre_select(struct sched *s, struct task *t)
40 {
41 struct receiver_node *rn = container_of(t, struct receiver_node, task);
42 struct private_udp_recv_data *purd = rn->private_data;
43
44 if (generic_recv_pre_select(s, t) <= 0)
45 return;
46 para_fd_set(purd->fd, &s->rfds, &s->max_fileno);
47 }
48
49 static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
50 {
51 struct receiver_node *rn = container_of(t, struct receiver_node, task);
52 struct private_udp_recv_data *purd = rn->private_data;
53 struct btr_node *btrn = rn->btrn;
54 int ret;
55 char *buf = NULL;
56 size_t packet_size, bufsize;
57
58 t->error = 0;
59 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
60 if (ret < 0)
61 goto err;
62 if (ret == 0)
63 return;
64 if (!FD_ISSET(purd->fd, &s->rfds))
65 return;
66 bufsize = btr_pool_get_buffer(purd->btrp, &buf);
67 ret = -E_UDP_OVERRUN;
68 if (bufsize == 0)
69 goto err;
70 ret = recv_bin_buffer(purd->fd, buf, bufsize);
71 if (ret == 0)
72 ret = -E_RECV_EOF;
73 if (ret < 0)
74 goto err;
75 packet_size = ret;
76 if (packet_size >= FEC_EOF_PACKET_LEN) {
77 if (!memcmp(buf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN)) {
78 PARA_INFO_LOG("received eof packet\n");
79 ret = -E_RECV_EOF;
80 goto err;
81 }
82 }
83 btr_add_output_pool(purd->btrp, packet_size, btrn);
84 return;
85 err:
86 btr_remove_node(btrn);
87 t->error = ret;
88 close(purd->fd);
89 purd->fd = -1;
90 }
91
92 static void udp_recv_close(struct receiver_node *rn)
93 {
94 struct private_udp_recv_data *purd = rn->private_data;
95
96 if (purd->fd >= 0)
97 close(purd->fd);
98 btr_pool_free(purd->btrp);
99 free(rn->private_data);
100 free(rn->buf);
101 }
102
103 static void *udp_recv_parse_config(int argc, char **argv)
104 {
105 int ret;
106 struct udp_recv_args_info *tmp =
107 para_calloc(sizeof(struct udp_recv_args_info));
108
109 ret = udp_recv_cmdline_parser(argc, argv, tmp)? -E_UDP_SYNTAX : 1;
110 if (ret >= 0)
111 return tmp;
112 free(tmp);
113 return NULL;
114 }
115
116 /*
117 * Perform AF-independent joining of multicast receive addresses.
118 *
119 * \param fd Bound socket descriptor.
120 * \param iface The receiving multicast interface, or NULL for the default.
121 *
122 * \return Zero if okay, negative on error.
123 */
124 static int mcast_receiver_setup(int fd, const char *iface)
125 {
126 struct sockaddr_storage ss;
127 socklen_t sslen = sizeof(ss);
128 int id = iface == NULL ? 0 : if_nametoindex(iface);
129
130 if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0)
131 goto err;
132
133 if (iface != NULL && id == 0)
134 PARA_WARNING_LOG("could not resolve interface %s, using default", 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.");
150
151 #endif
152 m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
153
154 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m4, sizeof(m4)) < 0)
155 break;
156 }
157 return 0;
158 case AF_INET6:
159 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
160 struct ipv6_mreq m6;
161
162 memset(&m6, 0, sizeof(m6));
163 memcpy(&m6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
164 m6.ipv6mr_interface = id;
165 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &m6, sizeof(m6)) < 0)
166 break;
167 }
168 return 0;
169 default:
170 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
171 return -E_ADDRESS_LOOKUP;
172 }
173 err:
174 return -ERRNO_TO_PARA_ERROR(errno);
175 }
176
177 static int udp_recv_open(struct receiver_node *rn)
178 {
179 struct private_udp_recv_data *purd;
180 struct udp_recv_args_info *c = rn->conf;
181 char *iface = c->iface_given ? c->iface_arg : NULL;
182 int ret;
183
184 rn->buf = para_calloc(UDP_RECV_CHUNK_SIZE);
185 rn->private_data = para_calloc(sizeof(struct private_udp_recv_data));
186 purd = rn->private_data;
187
188 ret = makesock(AF_UNSPEC, IPPROTO_UDP, 1, c->host_arg, c->port_arg);
189 if (ret < 0)
190 goto err;
191 purd->fd = ret;
192
193 ret = mcast_receiver_setup(purd->fd, iface);
194 if (ret < 0) {
195 close(purd->fd);
196 return ret;
197 }
198
199 ret = mark_fd_nonblocking(purd->fd);
200 if (ret < 0) {
201 close(purd->fd);
202 goto err;
203 }
204 PARA_INFO_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
205 c->port_arg, purd->fd);
206 purd->btrp = btr_pool_new("udp_recv", 320 * 1024);
207 return purd->fd;
208 err:
209 free(rn->private_data);
210 free(rn->buf);
211 return ret;
212 }
213
214 static void udp_recv_free_config(void *conf)
215 {
216 udp_recv_cmdline_parser_free(conf);
217 }
218
219 /**
220 * The init function of the udp receiver.
221 *
222 * \param r Pointer to the receiver struct to initialize.
223 *
224 * Initialize all function pointers of \a r.
225 */
226 void udp_recv_init(struct receiver *r)
227 {
228 struct udp_recv_args_info dummy;
229
230 udp_recv_cmdline_parser_init(&dummy);
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->free_config = udp_recv_free_config;
237 r->help = (struct ggo_help) {
238 .short_help = udp_recv_args_info_help,
239 .detailed_help = udp_recv_args_info_detailed_help
240 };
241 udp_recv_cmdline_parser_free(&dummy);
242 }