Merge branch 'refs/heads/t/mixer-fix'
[paraslash.git] / dccp_send.c
1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file dccp_send.c Paraslash's dccp sender. */
4
5 /*
6 * based on server.c of dccp-cs-0.01.tar.bz2,
7 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
8 */
9
10 #include <netinet/in.h>
11 #include <sys/socket.h>
12 #include <regex.h>
13 #include <sys/types.h>
14 #include <arpa/inet.h>
15 #include <sys/un.h>
16 #include <netdb.h>
17 #include <lopsub.h>
18
19 #include "server.lsg.h"
20 #include "para.h"
21 #include "error.h"
22 #include "string.h"
23 #include "afh.h"
24 #include "net.h"
25 #include "server.h"
26 #include "list.h"
27 #include "send.h"
28 #include "sched.h"
29 #include "vss.h"
30 #include "fd.h"
31
32 static struct sender_status dccp_sender_status, *dss = &dccp_sender_status;
33
34 struct dccp_fec_client {
35 struct fec_client_parms fcp;
36 struct fec_client *fc;
37 };
38
39 static void dccp_pre_select(int *max_fileno, fd_set *rfds,
40 __a_unused fd_set *wfds)
41 {
42 unsigned n;
43
44 FOR_EACH_LISTEN_FD(n, dss)
45 if (dss->listen_fds[n] >= 0)
46 para_fd_set(dss->listen_fds[n], rfds, max_fileno);
47 }
48
49 /**
50 * Query the TX CCID used on the sender-client half connection.
51 * \param sockfd Server socket descriptor to query (after accept(2)).
52 * \return CCID number > 0, -1 on error/incompatibility.
53 *
54 * NB: This feature is only available on Linux > 2.6.30; on older kernels
55 * ENOPROTOOPT ("Protocol not available") will be returned.
56 */
57 static int dccp_get_tx_ccid(int sockfd)
58 {
59 int tx_ccid;
60 socklen_t len = sizeof(tx_ccid);
61
62 if (getsockopt(sockfd, SOL_DCCP,
63 DCCP_SOCKOPT_TX_CCID, &tx_ccid, &len) < 0) {
64 PARA_WARNING_LOG("DCCP_SOCKOPT_TX_CCID: %s\n", strerror(errno));
65 return -1;
66 }
67 return tx_ccid;
68 }
69
70 static void dccp_shutdown_client(struct sender_client *sc)
71 {
72 struct dccp_fec_client *dfc = sc->private_data;
73
74 vss_del_fec_client(dfc->fc);
75 shutdown_client(sc, dss);
76 }
77
78 static void dccp_shutdown_clients(void)
79 {
80 struct sender_client *sc, *tmp;
81
82 list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
83 dccp_shutdown_client(sc);
84 }
85
86 static void dccp_shutdown(void)
87 {
88 dccp_shutdown_clients();
89 generic_acl_deplete(&dss->acl);
90 }
91
92 /** * Obtain current MPS according to RFC 4340, sec. 14. */
93 static int dccp_init_fec(struct sender_client *sc)
94 {
95 int mps, ret;
96 socklen_t ml = sizeof(mps);
97 uint32_t mss; /* max slize size */
98
99 /* If call fails, return some sensible minimum value */
100 ret = getsockopt(sc->fd, SOL_DCCP, DCCP_SOCKOPT_GET_CUR_MPS, &mps, &ml);
101 if (ret < 0) {
102 PARA_NOTICE_LOG("can not determine MPS: %s\n", strerror(errno));
103 mps = generic_max_transport_msg_size(sc->fd) - DCCP_MAX_HEADER;
104 }
105 PARA_INFO_LOG("current MPS = %d bytes\n", mps);
106 assert(mps > 0);
107 mss = OPT_UINT32_VAL(DCCP_MAX_SLICE_SIZE);
108 if (mss > 0 && mss <= INT_MAX)
109 mps = PARA_MIN(mps, (int)mss);
110 return mps;
111 }
112
113 static void dccp_send_fec(struct sender_client *sc, char *buf, size_t len)
114 {
115 int ret = xwrite(sc->fd, buf, len);
116
117 if (ret < 0)
118 dccp_shutdown_client(sc);
119 }
120
121 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
122 {
123 struct sender_client *sc;
124 struct dccp_fec_client *dfc;
125 int tx_ccid;
126 uint32_t k, n;
127
128 sc = accept_sender_client(dss, rfds);
129 if (!sc)
130 return;
131
132 /* If CCID identifiable, present client as <host>#<port>~<ccid> */
133 tx_ccid = dccp_get_tx_ccid(sc->fd);
134 if (tx_ccid != -1) {
135 char *tmp = make_message("%s~%d", sc->name, tx_ccid);
136
137 free(sc->name);
138 sc->name = tmp;
139 }
140 /*
141 * Bypass unused CCID paths: the sender does not receive application data
142 * from the client; by shutting down this unused communication path we can
143 * reduce processing costs a bit. See analogous comment in \ref dccp_recv.c.
144 */
145 if (shutdown(sc->fd, SHUT_RD) < 0) {
146 PARA_WARNING_LOG("%s\n", strerror(errno));
147 shutdown_client(sc, dss);
148 return;
149 }
150 dfc = para_calloc(sizeof(*dfc));
151 sc->private_data = dfc;
152 k = OPT_UINT32_VAL(DCCP_DATA_SLICES_PER_GROUP);
153 n = OPT_UINT32_VAL(DCCP_SLICES_PER_GROUP);
154 if (k == 0 || n == 0 || k >= n) {
155 PARA_WARNING_LOG("invalid FEC parameters, using defaults\n");
156 dfc->fcp.data_slices_per_group = 3;
157 dfc->fcp.slices_per_group = 4;
158 } else {
159 dfc->fcp.data_slices_per_group = k;
160 dfc->fcp.slices_per_group = n;
161 }
162 dfc->fcp.init_fec = dccp_init_fec;
163 dfc->fcp.send_fec = dccp_send_fec;
164 dfc->fcp.need_periodic_header = false;
165 dfc->fc = vss_add_fec_client(sc, &dfc->fcp);
166 }
167
168 static int dccp_com_on(__a_unused struct sender_command_data *scd)
169 {
170 generic_com_on(dss, IPPROTO_DCCP);
171 return 1;
172 }
173
174 static int dccp_com_off(__a_unused struct sender_command_data *scd)
175 {
176 dccp_shutdown_clients();
177 generic_com_off(dss);
178 return 1;
179 }
180
181
182 static int dccp_com_deny(struct sender_command_data *scd)
183 {
184 generic_com_deny(scd, dss);
185 return 1;
186 }
187
188 static int dccp_com_allow(struct sender_command_data *scd)
189 {
190 generic_com_allow(scd, dss);
191 return 1;
192 }
193
194 /**
195 * Return list of available CCIDs or warning, in static buffer.
196 */
197 static const char *dccp_list_available_ccids(void)
198 {
199 /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
200 static char list[DCCP_MAX_HOST_CCIDS * 4];
201 uint8_t *ccids;
202 int i, len, nccids;
203
204 nccids = dccp_available_ccids(&ccids);
205 if (nccids < 0) {
206 snprintf(list, sizeof(list), "Unable to query available CCIDs");
207 } else {
208 for (i = len = 0; i < nccids; i++)
209 len += snprintf(list + len, sizeof(list) - len,
210 "%s%d", i ? " " : "", ccids[i]);
211 }
212 return list;
213 }
214
215 static char *dccp_status(void)
216 {
217 char *status = generic_sender_status(dss, "dccp");
218 char *result = make_message("%ssupported ccids: %s\n", status,
219 dccp_list_available_ccids());
220 free(status);
221 return result;
222 }
223
224 /*
225 * Initialize the client list and the access control list and listen on the
226 * dccp port.
227 */
228 static void dccp_send_init(void)
229 {
230 init_sender_status(dss, OPT_RESULT(DCCP_ACCESS),
231 OPT_RESULT(DCCP_LISTEN_ADDRESS),
232 OPT_UINT32_VAL(DCCP_PORT), OPT_UINT32_VAL(DCCP_MAX_CLIENTS),
233 OPT_GIVEN(DCCP_DEFAULT_DENY));
234 generic_com_on(dss, IPPROTO_DCCP);
235 }
236
237 /**
238 * The DCCP sender.
239 *
240 * This sender offers congestion control not available in plain TCP. Most
241 * methods of the sender structure are implemented as simple wrappers for the
242 * generic functions defined in \ref send_common.c. Like UDP streams, DCCP
243 * streams are sent FEC-encoded.
244 */
245 const struct sender dccp_sender = {
246 .name = "dccp",
247 .init = dccp_send_init,
248 .shutdown = dccp_shutdown,
249 .pre_select = dccp_pre_select,
250 .post_select = dccp_post_select,
251 .shutdown_clients = dccp_shutdown_clients,
252 .client_cmds = {
253 [SENDER_on] = dccp_com_on,
254 [SENDER_off] = dccp_com_off,
255 [SENDER_deny] = dccp_com_deny,
256 [SENDER_allow] = dccp_com_allow,
257 },
258 .help = generic_sender_help,
259 .status = dccp_status,
260 };