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