Merge branch 'master' into next
[paraslash.git] / udp_recv.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2 /** \file udp_recv.c Paraslash's udp receiver */
3
4 #include <netinet/in.h>
5 #include <regex.h>
6 #include <sys/socket.h>
7 #include <net/if.h>
8 #include <sys/types.h>
9 #include <arpa/inet.h>
10 #include <sys/un.h>
11 #include <netdb.h>
12 #include <lopsub.h>
13
14 #include "recv_cmd.lsg.h"
15 #include "para.h"
16 #include "error.h"
17 #include "portable_io.h"
18 #include "list.h"
19 #include "sched.h"
20 #include "buffer_tree.h"
21 #include "recv.h"
22 #include "string.h"
23 #include "net.h"
24 #include "fd.h"
25
26 static void udp_recv_pre_select(struct sched *s, void *context)
27 {
28 struct receiver_node *rn = context;
29
30 if (generic_recv_pre_select(s, rn) <= 0)
31 return;
32 para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
33 }
34
35 static int udp_check_eof(size_t sz, struct iovec iov[2])
36 {
37 if (sz < FEC_EOF_PACKET_LEN)
38 return 0;
39 if (iov[0].iov_len >= FEC_EOF_PACKET_LEN) {
40 if (memcmp(iov[0].iov_base, FEC_EOF_PACKET,
41 FEC_EOF_PACKET_LEN) != 0)
42 return 0;
43 return -E_RECV_EOF;
44 }
45 if (memcmp(iov[0].iov_base, FEC_EOF_PACKET, iov[0].iov_len) != 0)
46 return 0;
47 if (memcmp(iov[1].iov_base, &FEC_EOF_PACKET[iov[0].iov_len],
48 FEC_EOF_PACKET_LEN - iov[0].iov_len) != 0)
49 return 0;
50 return -E_RECV_EOF;
51 }
52
53 static int udp_recv_post_select(__a_unused struct sched *s, void *context)
54 {
55 struct receiver_node *rn = context;
56 struct btr_node *btrn = rn->btrn;
57 size_t num_bytes;
58 struct iovec iov[2];
59 int ret, readv_ret, iovcnt;
60
61 ret = task_get_notification(rn->task);
62 if (ret < 0)
63 goto out;
64 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
65 if (ret <= 0)
66 goto out;
67 iovcnt = btr_pool_get_buffers(rn->btrp, iov);
68 ret = -E_UDP_OVERRUN;
69 if (iovcnt == 0)
70 goto out;
71 ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
72 if (num_bytes == 0)
73 goto out;
74 readv_ret = ret;
75 ret = udp_check_eof(num_bytes, iov);
76 if (ret < 0)
77 goto out;
78 if (iov[0].iov_len >= num_bytes)
79 btr_add_output_pool(rn->btrp, num_bytes, btrn);
80 else { /* both buffers contain data */
81 btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
82 btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len,
83 btrn);
84 }
85 ret = readv_ret;
86 out:
87 if (ret < 0) {
88 btr_remove_node(&rn->btrn);
89 close(rn->fd);
90 rn->fd = -1;
91 }
92 return ret;
93 }
94
95 static void udp_recv_close(struct receiver_node *rn)
96 {
97 if (rn->fd >= 0)
98 close(rn->fd);
99 btr_pool_free(rn->btrp);
100 }
101
102 /*
103 * Perform AF-independent joining of multicast receive addresses.
104 *
105 * \param fd Bound socket descriptor.
106 * \param iface The receiving multicast interface, or NULL for the default.
107 *
108 * \return Zero if okay, negative on error.
109 */
110 static int mcast_receiver_setup(int fd, const char *iface)
111 {
112 struct sockaddr_storage ss;
113 socklen_t sslen = sizeof(ss);
114 int id = iface == NULL ? 0 : if_nametoindex(iface);
115
116 if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0)
117 goto err;
118 assert(ss.ss_family == AF_INET || ss.ss_family == AF_INET6);
119
120 if (iface != NULL && id == 0)
121 PARA_WARNING_LOG("could not resolve interface %s, using default\n",
122 iface);
123
124 switch (ss.ss_family) {
125 case AF_INET:
126 if (IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr))) {
127 #ifdef HAVE_IP_MREQN
128 struct ip_mreqn m4;
129
130 m4.imr_address.s_addr = INADDR_ANY;
131 m4.imr_ifindex = id;
132 #else
133 struct ip_mreq m4;
134
135 m4.imr_interface.s_addr = INADDR_ANY;
136 if (id != 0)
137 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported\n");
138
139 #endif
140 m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
141
142 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
143 &m4, sizeof(m4)) < 0)
144 break;
145 }
146 return 0;
147 case AF_INET6:
148 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
149 struct ipv6_mreq m6;
150
151 memset(&m6, 0, sizeof(m6));
152 memcpy(&m6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
153 m6.ipv6mr_interface = id;
154 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &m6, sizeof(m6)) < 0)
155 break;
156 }
157 return 0;
158 }
159 err:
160 return -ERRNO_TO_PARA_ERROR(errno);
161 }
162
163 static int udp_recv_open(struct receiver_node *rn)
164 {
165 struct lls_parse_result *lpr = rn->lpr;
166 const char *iface = RECV_CMD_OPT_STRING_VAL(UDP, IFACE, lpr);
167 const char *host = RECV_CMD_OPT_STRING_VAL(UDP, HOST, lpr);
168 uint32_t port = RECV_CMD_OPT_UINT32_VAL(UDP, PORT, lpr);
169 int ret;
170
171 ret = makesock(IPPROTO_UDP, 1, host, port, NULL);
172 if (ret < 0)
173 return ret;
174 rn->fd = ret;
175 ret = mcast_receiver_setup(rn->fd, iface);
176 if (ret < 0)
177 goto err;
178 ret = mark_fd_nonblocking(rn->fd);
179 if (ret < 0)
180 goto err;
181 PARA_INFO_LOG("receiving from %s:%u, fd=%d\n", host, port, rn->fd);
182 rn->btrp = btr_pool_new("udp_recv", 320 * 1024);
183 return rn->fd;
184 err:
185 close(rn->fd);
186 return ret;
187 }
188
189 const struct receiver lsg_recv_cmd_com_udp_user_data = {
190 .open = udp_recv_open,
191 .close = udp_recv_close,
192 .pre_select = udp_recv_pre_select,
193 .post_select = udp_recv_post_select,
194 };