1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file dccp_send.c Paraslash's dccp sender. */
6 * based on server.c of dccp-cs-0.01.tar.bz2,
7 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
10 #include <netinet/in.h>
11 #include <sys/socket.h>
13 #include <sys/types.h>
14 #include <arpa/inet.h>
19 #include "server.lsg.h"
32 static struct sender_status dccp_sender_status, *dss = &dccp_sender_status;
34 struct dccp_fec_client {
35 struct fec_client_parms fcp;
36 struct fec_client *fc;
39 static void dccp_pre_monitor(struct sched *s)
43 FOR_EACH_LISTEN_FD(n, dss)
44 if (dss->listen_fds[n] >= 0)
45 sched_monitor_readfd(dss->listen_fds[n], s);
48 #ifndef DCCP_SOCKOPT_TX_CCID
49 #define DCCP_SOCKOPT_TX_CCID 14 /**< Set/get the TX CCID. */
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.
57 * NB: This feature is only available on Linux > 2.6.30; on older kernels
58 * ENOPROTOOPT ("Protocol not available") will be returned.
60 static int dccp_get_tx_ccid(int sockfd)
63 socklen_t len = sizeof(tx_ccid);
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));
73 static void dccp_shutdown_client(struct sender_client *sc)
75 struct dccp_fec_client *dfc = sc->private_data;
77 vss_del_fec_client(dfc->fc);
78 shutdown_client(sc, dss);
81 static void dccp_shutdown_clients(void)
83 struct sender_client *sc, *tmp;
85 list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
86 dccp_shutdown_client(sc);
89 static void dccp_shutdown(void)
91 dccp_shutdown_clients();
92 generic_acl_deplete(&dss->acl);
93 free_sender_status(dss);
96 #ifndef DCCP_SOCKOPT_GET_CUR_MPS
97 #define DCCP_SOCKOPT_GET_CUR_MPS 5 /**< Max packet size, RFC 4340, 14. */
100 /** Estimated worst-case length of a DCCP header including options. */
101 #define DCCP_MAX_HEADER 128
103 /** * Obtain current MPS according to RFC 4340, sec. 14. */
104 static int dccp_init_fec(struct sender_client *sc)
107 socklen_t ml = sizeof(mps);
108 uint32_t mss; /* max slize size */
110 /* If call fails, return some sensible minimum value */
111 ret = getsockopt(sc->fd, SOL_DCCP, DCCP_SOCKOPT_GET_CUR_MPS, &mps, &ml);
113 PARA_NOTICE_LOG("can not determine MPS: %s\n", strerror(errno));
114 mps = generic_max_transport_msg_size(sc->fd) - DCCP_MAX_HEADER;
116 PARA_INFO_LOG("current MPS = %d bytes\n", mps);
118 mss = OPT_UINT32_VAL(DCCP_MAX_SLICE_SIZE);
119 if (mss > 0 && mss <= INT_MAX)
120 mps = PARA_MIN(mps, (int)mss);
124 static void dccp_send_fec(struct sender_client *sc, char *buf, size_t len)
126 int ret = xwrite(sc->fd, buf, len);
129 dccp_shutdown_client(sc);
132 static void dccp_post_monitor(__a_unused struct sched *s)
134 struct sender_client *sc;
135 struct dccp_fec_client *dfc;
139 sc = accept_sender_client(dss);
143 /* If CCID identifiable, present client as <host>#<port>~<ccid> */
144 tx_ccid = dccp_get_tx_ccid(sc->fd);
146 char *tmp = make_message("%s~%d", sc->name, tx_ccid);
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.
156 if (shutdown(sc->fd, SHUT_RD) < 0) {
157 PARA_WARNING_LOG("%s\n", strerror(errno));
158 shutdown_client(sc, dss);
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;
170 dfc->fcp.data_slices_per_group = k;
171 dfc->fcp.slices_per_group = n;
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);
179 static int dccp_com_on(__a_unused struct sender_command_data *scd)
181 generic_com_on(dss, IPPROTO_DCCP);
185 static int dccp_com_off(__a_unused struct sender_command_data *scd)
187 dccp_shutdown_clients();
188 generic_com_off(dss);
193 static int dccp_com_deny(struct sender_command_data *scd)
195 generic_com_deny(scd, dss);
199 static int dccp_com_allow(struct sender_command_data *scd)
201 generic_com_allow(scd, dss);
206 * Return list of available CCIDs or warning, in static buffer.
208 static const char *dccp_list_available_ccids(void)
210 /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
211 static char list[DCCP_MAX_HOST_CCIDS * 4];
215 nccids = dccp_available_ccids(&ccids);
217 snprintf(list, sizeof(list), "Unable to query available CCIDs");
219 for (i = len = 0; i < nccids; i++)
220 len += snprintf(list + len, sizeof(list) - len,
221 "%s%d", i ? " " : "", ccids[i]);
226 static char *dccp_status(void)
228 char *status = generic_sender_status(dss, "dccp");
229 char *result = make_message("%ssupported ccids: %s\n", status,
230 dccp_list_available_ccids());
236 * Initialize the client list and the access control list and listen on the
239 static void dccp_send_init(void)
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))
247 generic_com_on(dss, IPPROTO_DCCP);
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.
258 const struct sender dccp_sender = {
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,
266 [SENDER_on] = dccp_com_on,
267 [SENDER_off] = dccp_com_off,
268 [SENDER_deny] = dccp_com_deny,
269 [SENDER_allow] = dccp_com_allow,
271 .help = generic_sender_help,
272 .status = dccp_status,