play: Convert com_quit() to lopsub.
[paraslash.git] / udp_recv.c
1 /*
2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
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 <netinet/in.h>
9 #include <regex.h>
10 #include <sys/socket.h>
11 #include <net/if.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
14 #include <sys/un.h>
15 #include <netdb.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "portable_io.h"
20 #include "list.h"
21 #include "sched.h"
22 #include "ggo.h"
23 #include "buffer_tree.h"
24 #include "recv.h"
25 #include "udp_recv.cmdline.h"
26 #include "string.h"
27 #include "net.h"
28 #include "fd.h"
29
30 static void udp_recv_pre_select(struct sched *s, void *context)
31 {
32 struct receiver_node *rn = context;
33
34 if (generic_recv_pre_select(s, rn) <= 0)
35 return;
36 para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
37 }
38
39 static int udp_check_eof(size_t sz, struct iovec iov[2])
40 {
41 if (sz < FEC_EOF_PACKET_LEN)
42 return 0;
43 if (iov[0].iov_len >= FEC_EOF_PACKET_LEN) {
44 if (memcmp(iov[0].iov_base, FEC_EOF_PACKET,
45 FEC_EOF_PACKET_LEN) != 0)
46 return 0;
47 return -E_RECV_EOF;
48 }
49 if (memcmp(iov[0].iov_base, FEC_EOF_PACKET, iov[0].iov_len) != 0)
50 return 0;
51 if (memcmp(iov[1].iov_base, &FEC_EOF_PACKET[iov[0].iov_len],
52 FEC_EOF_PACKET_LEN - iov[0].iov_len) != 0)
53 return 0;
54 return -E_RECV_EOF;
55 }
56
57 static int udp_recv_post_select(__a_unused struct sched *s, void *context)
58 {
59 struct receiver_node *rn = context;
60 struct btr_node *btrn = rn->btrn;
61 size_t num_bytes;
62 struct iovec iov[2];
63 int ret, readv_ret, iovcnt;
64
65 ret = task_get_notification(rn->task);
66 if (ret < 0)
67 goto out;
68 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
69 if (ret <= 0)
70 goto out;
71 iovcnt = btr_pool_get_buffers(rn->btrp, iov);
72 ret = -E_UDP_OVERRUN;
73 if (iovcnt == 0)
74 goto out;
75 ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
76 if (num_bytes == 0)
77 goto out;
78 readv_ret = ret;
79 ret = udp_check_eof(num_bytes, iov);
80 if (ret < 0)
81 goto out;
82 if (iov[0].iov_len >= num_bytes)
83 btr_add_output_pool(rn->btrp, num_bytes, btrn);
84 else { /* both buffers contain data */
85 btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
86 btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len,
87 btrn);
88 }
89 ret = readv_ret;
90 out:
91 if (ret < 0) {
92 btr_remove_node(&rn->btrn);
93 close(rn->fd);
94 rn->fd = -1;
95 }
96 return ret;
97 }
98
99 static void udp_recv_close(struct receiver_node *rn)
100 {
101 if (rn->fd >= 0)
102 close(rn->fd);
103 btr_pool_free(rn->btrp);
104 }
105
106 static void *udp_recv_parse_config(int argc, char **argv)
107 {
108 struct udp_recv_args_info *tmp = para_calloc(sizeof(*tmp));
109 udp_recv_cmdline_parser(argc, argv, tmp);
110 return tmp;
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 assert(ss.ss_family == AF_INET || ss.ss_family == AF_INET6);
130
131 if (iface != NULL && id == 0)
132 PARA_WARNING_LOG("could not resolve interface %s, using default\n",
133 iface);
134
135 switch (ss.ss_family) {
136 case AF_INET:
137 if (IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr))) {
138 #ifdef HAVE_IP_MREQN
139 struct ip_mreqn m4;
140
141 m4.imr_address.s_addr = INADDR_ANY;
142 m4.imr_ifindex = id;
143 #else
144 struct ip_mreq m4;
145
146 m4.imr_interface.s_addr = INADDR_ANY;
147 if (id != 0)
148 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported\n");
149
150 #endif
151 m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
152
153 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
154 &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 }
170 err:
171 return -ERRNO_TO_PARA_ERROR(errno);
172 }
173
174 static int udp_recv_open(struct receiver_node *rn)
175 {
176 struct udp_recv_args_info *c = rn->conf;
177 char *iface = c->iface_given ? c->iface_arg : NULL;
178 int ret;
179
180 ret = makesock(IPPROTO_UDP, 1, c->host_arg, c->port_arg, NULL);
181 if (ret < 0)
182 goto err;
183 rn->fd = ret;
184
185 ret = mcast_receiver_setup(rn->fd, iface);
186 if (ret < 0) {
187 close(rn->fd);
188 goto err;
189 }
190
191 ret = mark_fd_nonblocking(rn->fd);
192 if (ret < 0) {
193 close(rn->fd);
194 goto err;
195 }
196 PARA_INFO_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
197 c->port_arg, rn->fd);
198 rn->btrp = btr_pool_new("udp_recv", 320 * 1024);
199 return rn->fd;
200 err:
201 return ret;
202 }
203
204 static void udp_recv_free_config(void *conf)
205 {
206 udp_recv_cmdline_parser_free(conf);
207 free(conf);
208 }
209
210 /**
211 * The init function of the udp receiver.
212 *
213 * \param r Pointer to the receiver struct to initialize.
214 *
215 * Initialize all function pointers of \a r.
216 */
217 void udp_recv_init(struct receiver *r)
218 {
219 struct udp_recv_args_info dummy;
220
221 udp_recv_cmdline_parser_init(&dummy);
222 r->open = udp_recv_open;
223 r->close = udp_recv_close;
224 r->pre_select = udp_recv_pre_select;
225 r->post_select = udp_recv_post_select;
226 r->parse_config = udp_recv_parse_config;
227 r->free_config = udp_recv_free_config;
228 r->help = (struct ggo_help)DEFINE_GGO_HELP(udp_recv);
229 udp_recv_cmdline_parser_free(&dummy);
230 }