Merge topic branch t/openssl-3 into master
[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 "sched.h"
28 #include "send.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_monitor(struct sched *s)
40 {
41 unsigned n;
42
43 FOR_EACH_LISTEN_FD(n, dss)
44 if (dss->listen_fds[n] >= 0)
45 sched_monitor_readfd(dss->listen_fds[n], s);
46 }
47
48 #ifndef DCCP_SOCKOPT_TX_CCID
49 #define DCCP_SOCKOPT_TX_CCID 14 /**< Set/get the TX CCID. */
50 #endif
51
52 /**
53 * Query the TX CCID used on the sender-client half connection.
54 * \param sockfd Server socket descriptor to query (after accept(2)).
55 * \return CCID number > 0, -1 on error/incompatibility.
56 *
57 * NB: This feature is only available on Linux > 2.6.30; on older kernels
58 * ENOPROTOOPT ("Protocol not available") will be returned.
59 */
60 static int dccp_get_tx_ccid(int sockfd)
61 {
62 int tx_ccid;
63 socklen_t len = sizeof(tx_ccid);
64
65 if (getsockopt(sockfd, SOL_DCCP,
66 DCCP_SOCKOPT_TX_CCID, &tx_ccid, &len) < 0) {
67 PARA_WARNING_LOG("DCCP_SOCKOPT_TX_CCID: %s\n", strerror(errno));
68 return -1;
69 }
70 return tx_ccid;
71 }
72
73 static void dccp_shutdown_client(struct sender_client *sc)
74 {
75 struct dccp_fec_client *dfc = sc->private_data;
76
77 vss_del_fec_client(dfc->fc);
78 shutdown_client(sc, dss);
79 }
80
81 static void dccp_shutdown_clients(void)
82 {
83 struct sender_client *sc, *tmp;
84
85 list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
86 dccp_shutdown_client(sc);
87 }
88
89 static void dccp_shutdown(void)
90 {
91 dccp_shutdown_clients();
92 generic_acl_deplete(&dss->acl);
93 free_sender_status(dss);
94 }
95
96 #ifndef DCCP_SOCKOPT_GET_CUR_MPS
97 #define DCCP_SOCKOPT_GET_CUR_MPS 5 /**< Max packet size, RFC 4340, 14. */
98 #endif
99
100 /** Estimated worst-case length of a DCCP header including options. */
101 #define DCCP_MAX_HEADER 128
102
103 /** * Obtain current MPS according to RFC 4340, sec. 14. */
104 static int dccp_init_fec(struct sender_client *sc)
105 {
106 int mps, ret;
107 socklen_t ml = sizeof(mps);
108 uint32_t mss; /* max slize size */
109
110 /* If call fails, return some sensible minimum value */
111 ret = getsockopt(sc->fd, SOL_DCCP, DCCP_SOCKOPT_GET_CUR_MPS, &mps, &ml);
112 if (ret < 0) {
113 PARA_NOTICE_LOG("can not determine MPS: %s\n", strerror(errno));
114 mps = generic_max_transport_msg_size(sc->fd) - DCCP_MAX_HEADER;
115 }
116 PARA_INFO_LOG("current MPS = %d bytes\n", mps);
117 assert(mps > 0);
118 mss = OPT_UINT32_VAL(DCCP_MAX_SLICE_SIZE);
119 if (mss > 0 && mss <= INT_MAX)
120 mps = PARA_MIN(mps, (int)mss);
121 return mps;
122 }
123
124 static void dccp_send_fec(struct sender_client *sc, char *buf, size_t len)
125 {
126 int ret = xwrite(sc->fd, buf, len);
127
128 if (ret < 0)
129 dccp_shutdown_client(sc);
130 }
131
132 static void dccp_post_monitor(__a_unused struct sched *s)
133 {
134 struct sender_client *sc;
135 struct dccp_fec_client *dfc;
136 int tx_ccid;
137 uint32_t k, n;
138
139 sc = accept_sender_client(dss);
140 if (!sc)
141 return;
142
143 /* If CCID identifiable, present client as <host>#<port>~<ccid> */
144 tx_ccid = dccp_get_tx_ccid(sc->fd);
145 if (tx_ccid != -1) {
146 char *tmp = make_message("%s~%d", sc->name, tx_ccid);
147
148 free(sc->name);
149 sc->name = tmp;
150 }
151 /*
152 * Bypass unused CCID paths: the sender does not receive application data
153 * from the client; by shutting down this unused communication path we can
154 * reduce processing costs a bit. See analogous comment in \ref dccp_recv.c.
155 */
156 if (shutdown(sc->fd, SHUT_RD) < 0) {
157 PARA_WARNING_LOG("%s\n", strerror(errno));
158 shutdown_client(sc, dss);
159 return;
160 }
161 dfc = zalloc(sizeof(*dfc));
162 sc->private_data = dfc;
163 k = OPT_UINT32_VAL(DCCP_DATA_SLICES_PER_GROUP);
164 n = OPT_UINT32_VAL(DCCP_SLICES_PER_GROUP);
165 if (k == 0 || n == 0 || k >= n) {
166 PARA_WARNING_LOG("invalid FEC parameters, using defaults\n");
167 dfc->fcp.data_slices_per_group = 3;
168 dfc->fcp.slices_per_group = 4;
169 } else {
170 dfc->fcp.data_slices_per_group = k;
171 dfc->fcp.slices_per_group = n;
172 }
173 dfc->fcp.init_fec = dccp_init_fec;
174 dfc->fcp.send_fec = dccp_send_fec;
175 dfc->fcp.need_periodic_header = false;
176 dfc->fc = vss_add_fec_client(sc, &dfc->fcp);
177 }
178
179 static int dccp_com_on(__a_unused struct sender_command_data *scd)
180 {
181 generic_com_on(dss, IPPROTO_DCCP);
182 return 1;
183 }
184
185 static int dccp_com_off(__a_unused struct sender_command_data *scd)
186 {
187 dccp_shutdown_clients();
188 generic_com_off(dss);
189 return 1;
190 }
191
192
193 static int dccp_com_deny(struct sender_command_data *scd)
194 {
195 generic_com_deny(scd, dss);
196 return 1;
197 }
198
199 static int dccp_com_allow(struct sender_command_data *scd)
200 {
201 generic_com_allow(scd, dss);
202 return 1;
203 }
204
205 /**
206 * Return list of available CCIDs or warning, in static buffer.
207 */
208 static const char *dccp_list_available_ccids(void)
209 {
210 /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
211 static char list[DCCP_MAX_HOST_CCIDS * 4];
212 uint8_t *ccids;
213 int i, len, nccids;
214
215 nccids = dccp_available_ccids(&ccids);
216 if (nccids < 0) {
217 snprintf(list, sizeof(list), "Unable to query available CCIDs");
218 } else {
219 for (i = len = 0; i < nccids; i++)
220 len += snprintf(list + len, sizeof(list) - len,
221 "%s%d", i ? " " : "", ccids[i]);
222 }
223 return list;
224 }
225
226 static char *dccp_status(void)
227 {
228 char *status = generic_sender_status(dss, "dccp");
229 char *result = make_message("%ssupported ccids: %s\n", status,
230 dccp_list_available_ccids());
231 free(status);
232 return result;
233 }
234
235 /*
236 * Initialize the client list and the access control list and listen on the
237 * dccp port.
238 */
239 static void dccp_send_init(void)
240 {
241 init_sender_status(dss, OPT_RESULT(DCCP_ACCESS),
242 OPT_RESULT(DCCP_LISTEN_ADDRESS),
243 OPT_UINT32_VAL(DCCP_PORT), OPT_UINT32_VAL(DCCP_MAX_CLIENTS),
244 OPT_GIVEN(DCCP_DEFAULT_DENY));
245 if (OPT_GIVEN(DCCP_NO_AUTOSTART))
246 return;
247 generic_com_on(dss, IPPROTO_DCCP);
248 }
249
250 /**
251 * The DCCP sender.
252 *
253 * This sender offers congestion control not available in plain TCP. Most
254 * methods of the sender structure are implemented as simple wrappers for the
255 * generic functions defined in \ref send_common.c. Like UDP streams, DCCP
256 * streams are sent FEC-encoded.
257 */
258 const struct sender dccp_sender = {
259 .name = "dccp",
260 .init = dccp_send_init,
261 .shutdown = dccp_shutdown,
262 .pre_monitor = dccp_pre_monitor,
263 .post_monitor = dccp_post_monitor,
264 .shutdown_clients = dccp_shutdown_clients,
265 .client_cmds = {
266 [SENDER_on] = dccp_com_on,
267 [SENDER_off] = dccp_com_off,
268 [SENDER_deny] = dccp_com_deny,
269 [SENDER_allow] = dccp_com_allow,
270 },
271 .help = generic_sender_help,
272 .status = dccp_status,
273 };