afh: Unify name of init functions.
[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 #include <lopsub.h>
17
18 #include "recv_cmd.lsg.h"
19 #include "para.h"
20 #include "error.h"
21 #include "portable_io.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "buffer_tree.h"
25 #include "recv.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 /*
107 * Perform AF-independent joining of multicast receive addresses.
108 *
109 * \param fd Bound socket descriptor.
110 * \param iface The receiving multicast interface, or NULL for the default.
111 *
112 * \return Zero if okay, negative on error.
113 */
114 static int mcast_receiver_setup(int fd, const char *iface)
115 {
116 struct sockaddr_storage ss;
117 socklen_t sslen = sizeof(ss);
118 int id = iface == NULL ? 0 : if_nametoindex(iface);
119
120 if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0)
121 goto err;
122 assert(ss.ss_family == AF_INET || ss.ss_family == AF_INET6);
123
124 if (iface != NULL && id == 0)
125 PARA_WARNING_LOG("could not resolve interface %s, using default\n",
126 iface);
127
128 switch (ss.ss_family) {
129 case AF_INET:
130 if (IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr))) {
131 #ifdef HAVE_IP_MREQN
132 struct ip_mreqn m4;
133
134 m4.imr_address.s_addr = INADDR_ANY;
135 m4.imr_ifindex = id;
136 #else
137 struct ip_mreq m4;
138
139 m4.imr_interface.s_addr = INADDR_ANY;
140 if (id != 0)
141 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported\n");
142
143 #endif
144 m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
145
146 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
147 &m4, sizeof(m4)) < 0)
148 break;
149 }
150 return 0;
151 case AF_INET6:
152 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
153 struct ipv6_mreq m6;
154
155 memset(&m6, 0, sizeof(m6));
156 memcpy(&m6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
157 m6.ipv6mr_interface = id;
158 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &m6, sizeof(m6)) < 0)
159 break;
160 }
161 return 0;
162 }
163 err:
164 return -ERRNO_TO_PARA_ERROR(errno);
165 }
166
167 static int udp_recv_open(struct receiver_node *rn)
168 {
169 struct lls_parse_result *lpr = rn->lpr;
170 const char *iface = RECV_CMD_OPT_STRING_VAL(UDP, IFACE, lpr);
171 const char *host = RECV_CMD_OPT_STRING_VAL(UDP, HOST, lpr);
172 uint32_t port = RECV_CMD_OPT_UINT32_VAL(UDP, PORT, lpr);
173 int ret;
174
175 ret = makesock(IPPROTO_UDP, 1, host, port, NULL);
176 if (ret < 0)
177 return ret;
178 rn->fd = ret;
179 ret = mcast_receiver_setup(rn->fd, iface);
180 if (ret < 0)
181 goto err;
182 ret = mark_fd_nonblocking(rn->fd);
183 if (ret < 0)
184 goto err;
185 PARA_INFO_LOG("receiving from %s:%u, fd=%d\n", host, port, rn->fd);
186 rn->btrp = btr_pool_new("udp_recv", 320 * 1024);
187 return rn->fd;
188 err:
189 close(rn->fd);
190 return ret;
191 }
192
193 const struct receiver lsg_recv_cmd_com_udp_user_data = {
194 .open = udp_recv_open,
195 .close = udp_recv_close,
196 .pre_select = udp_recv_pre_select,
197 .post_select = udp_recv_post_select,
198 };