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_select(int *max_fileno, fd_set *rfds,
40 __a_unused fd_set *wfds)
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);
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.
54 * NB: This feature is only available on Linux > 2.6.30; on older kernels
55 * ENOPROTOOPT ("Protocol not available") will be returned.
57 static int dccp_get_tx_ccid(int sockfd)
60 socklen_t len = sizeof(tx_ccid);
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));
70 static void dccp_shutdown_client(struct sender_client *sc)
72 struct dccp_fec_client *dfc = sc->private_data;
74 vss_del_fec_client(dfc->fc);
75 shutdown_client(sc, dss);
78 static void dccp_shutdown_clients(void)
80 struct sender_client *sc, *tmp;
82 list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
83 dccp_shutdown_client(sc);
86 static void dccp_shutdown(void)
88 dccp_shutdown_clients();
89 generic_acl_deplete(&dss->acl);
90 free_sender_status(dss);
93 /** * Obtain current MPS according to RFC 4340, sec. 14. */
94 static int dccp_init_fec(struct sender_client *sc)
97 socklen_t ml = sizeof(mps);
98 uint32_t mss; /* max slize size */
100 /* If call fails, return some sensible minimum value */
101 ret = getsockopt(sc->fd, SOL_DCCP, DCCP_SOCKOPT_GET_CUR_MPS, &mps, &ml);
103 PARA_NOTICE_LOG("can not determine MPS: %s\n", strerror(errno));
104 mps = generic_max_transport_msg_size(sc->fd) - DCCP_MAX_HEADER;
106 PARA_INFO_LOG("current MPS = %d bytes\n", mps);
108 mss = OPT_UINT32_VAL(DCCP_MAX_SLICE_SIZE);
109 if (mss > 0 && mss <= INT_MAX)
110 mps = PARA_MIN(mps, (int)mss);
114 static void dccp_send_fec(struct sender_client *sc, char *buf, size_t len)
116 int ret = xwrite(sc->fd, buf, len);
119 dccp_shutdown_client(sc);
122 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
124 struct sender_client *sc;
125 struct dccp_fec_client *dfc;
129 sc = accept_sender_client(dss, rfds);
133 /* If CCID identifiable, present client as <host>#<port>~<ccid> */
134 tx_ccid = dccp_get_tx_ccid(sc->fd);
136 char *tmp = make_message("%s~%d", sc->name, tx_ccid);
142 * Bypass unused CCID paths: the sender does not receive application data
143 * from the client; by shutting down this unused communication path we can
144 * reduce processing costs a bit. See analogous comment in \ref dccp_recv.c.
146 if (shutdown(sc->fd, SHUT_RD) < 0) {
147 PARA_WARNING_LOG("%s\n", strerror(errno));
148 shutdown_client(sc, dss);
151 dfc = para_calloc(sizeof(*dfc));
152 sc->private_data = dfc;
153 k = OPT_UINT32_VAL(DCCP_DATA_SLICES_PER_GROUP);
154 n = OPT_UINT32_VAL(DCCP_SLICES_PER_GROUP);
155 if (k == 0 || n == 0 || k >= n) {
156 PARA_WARNING_LOG("invalid FEC parameters, using defaults\n");
157 dfc->fcp.data_slices_per_group = 3;
158 dfc->fcp.slices_per_group = 4;
160 dfc->fcp.data_slices_per_group = k;
161 dfc->fcp.slices_per_group = n;
163 dfc->fcp.init_fec = dccp_init_fec;
164 dfc->fcp.send_fec = dccp_send_fec;
165 dfc->fcp.need_periodic_header = false;
166 dfc->fc = vss_add_fec_client(sc, &dfc->fcp);
169 static int dccp_com_on(__a_unused struct sender_command_data *scd)
171 generic_com_on(dss, IPPROTO_DCCP);
175 static int dccp_com_off(__a_unused struct sender_command_data *scd)
177 dccp_shutdown_clients();
178 generic_com_off(dss);
183 static int dccp_com_deny(struct sender_command_data *scd)
185 generic_com_deny(scd, dss);
189 static int dccp_com_allow(struct sender_command_data *scd)
191 generic_com_allow(scd, dss);
196 * Return list of available CCIDs or warning, in static buffer.
198 static const char *dccp_list_available_ccids(void)
200 /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
201 static char list[DCCP_MAX_HOST_CCIDS * 4];
205 nccids = dccp_available_ccids(&ccids);
207 snprintf(list, sizeof(list), "Unable to query available CCIDs");
209 for (i = len = 0; i < nccids; i++)
210 len += snprintf(list + len, sizeof(list) - len,
211 "%s%d", i ? " " : "", ccids[i]);
216 static char *dccp_status(void)
218 char *status = generic_sender_status(dss, "dccp");
219 char *result = make_message("%ssupported ccids: %s\n", status,
220 dccp_list_available_ccids());
226 * Initialize the client list and the access control list and listen on the
229 static void dccp_send_init(void)
231 init_sender_status(dss, OPT_RESULT(DCCP_ACCESS),
232 OPT_RESULT(DCCP_LISTEN_ADDRESS),
233 OPT_UINT32_VAL(DCCP_PORT), OPT_UINT32_VAL(DCCP_MAX_CLIENTS),
234 OPT_GIVEN(DCCP_DEFAULT_DENY));
235 generic_com_on(dss, IPPROTO_DCCP);
241 * This sender offers congestion control not available in plain TCP. Most
242 * methods of the sender structure are implemented as simple wrappers for the
243 * generic functions defined in \ref send_common.c. Like UDP streams, DCCP
244 * streams are sent FEC-encoded.
246 const struct sender dccp_sender = {
248 .init = dccp_send_init,
249 .shutdown = dccp_shutdown,
250 .pre_select = dccp_pre_select,
251 .post_select = dccp_post_select,
252 .shutdown_clients = dccp_shutdown_clients,
254 [SENDER_on] = dccp_com_on,
255 [SENDER_off] = dccp_com_off,
256 [SENDER_deny] = dccp_com_deny,
257 [SENDER_allow] = dccp_com_allow,
259 .help = generic_sender_help,
260 .status = dccp_status,