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