2 * Copyright (C) 2006-2010 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file dccp_send.c Paraslash's dccp sender. */
10 * based on server.c of dccp-cs-0.01.tar.bz2,
11 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
15 #include <sys/types.h>
30 #include "close_on_fork.h"
31 #include "chunk_queue.h"
32 #include "server.cmdline.h"
35 /** Do not write more than that many bytes at once. */
36 #define DCCP_MAX_BYTES_PER_WRITE 1024
38 static struct sender_status dccp_sender_status, *dss = &dccp_sender_status;
39 static struct sender *self;
42 struct dccp_fec_client {
43 struct fec_client_parms fcp;
44 struct fec_client *fc;
45 struct sender_client *sc;
48 static void dccp_pre_select(int *max_fileno, fd_set *rfds,
49 __a_unused fd_set *wfds)
51 if (dss->listen_fd >= 0)
52 para_fd_set(dss->listen_fd, rfds, max_fileno);
56 * Query the TX CCID used on the sender-client half connection.
57 * \param sockfd Server socket descriptor to query (after accept(2)).
58 * \return CCID number > 0, -1 on error/incompatibility.
60 * NB: This feature is only available on Linux > 2.6.30; on older kernels
61 * ENOPROTOOPT ("Protocol not available") will be returned.
63 static int dccp_get_tx_ccid(int sockfd)
66 socklen_t len = sizeof(tx_ccid);
68 if (getsockopt(sockfd, SOL_DCCP,
69 DCCP_SOCKOPT_TX_CCID, &tx_ccid, &len) < 0) {
70 PARA_WARNING_LOG("DCCP_SOCKOPT_TX_CCID: %s\n", strerror(errno));
76 static void dccp_shutdown_client(struct sender_client *sc)
78 struct dccp_fec_client *dfc = sc->private_data;
80 vss_del_fec_client(dfc->fc);
81 shutdown_client(sc, dss);
84 static void dccp_shutdown_clients(void)
86 struct sender_client *sc, *tmp;
88 list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
89 dccp_shutdown_client(sc);
92 static int dccp_open(void *client, struct fec_client_parms **fcp)
94 struct dccp_fec_client *dfc = client;
96 dfc->fcp.slices_per_group = 4;
97 dfc->fcp.data_slices_per_group = 3;
98 dfc->fcp.max_slice_bytes = 1472;
103 static int dccp_send_fec(char *buf, size_t len, void *private_data)
105 struct dccp_fec_client *dfc = private_data;
106 int ret = write_nonblock(dfc->sc->fd, buf, len, DCCP_MAX_BYTES_PER_WRITE);
109 dccp_shutdown_client(dfc->sc);
113 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
115 struct sender_client *sc;
116 struct dccp_fec_client *dfc;
119 sc = accept_sender_client(dss, rfds);
123 /* If CCID identifiable, present client as <host>#<port>~<ccid> */
124 tx_ccid = dccp_get_tx_ccid(sc->fd);
126 char *tmp = make_message("%s~%d", sc->name, tx_ccid);
132 * Bypass unused CCID paths: the sender does not receive application data
133 * from the client; by shutting down this unused communication path we can
134 * reduce processing costs a bit. See analogous comment in dccp_recv.c.
136 if (shutdown(sc->fd, SHUT_RD) < 0) {
137 PARA_WARNING_LOG("%s\n", strerror(errno));
138 shutdown_client(sc, dss);
141 dfc = para_calloc(sizeof(*dfc));
142 sc->private_data = dfc;
144 vss_add_fec_client(self, dfc, &dfc->fc);
147 static int dccp_com_on(__a_unused struct sender_command_data *scd)
149 return generic_com_on(dss, IPPROTO_DCCP);
152 static int dccp_com_off(__a_unused struct sender_command_data *scd)
154 generic_com_off(dss);
159 static int dccp_com_deny(struct sender_command_data *scd)
161 generic_com_deny(scd, dss);
165 static int dccp_com_allow(struct sender_command_data *scd)
167 generic_com_allow(scd, dss);
172 * Return list of available CCIDs or warning, in static buffer.
174 static const char *dccp_list_available_ccids(void)
176 /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
177 static char list[DCCP_MAX_HOST_CCIDS * 4];
181 nccids = dccp_available_ccids(&ccids);
183 snprintf(list, sizeof(list), "Unable to query available CCIDs");
185 for (i = len = 0; i < nccids; i++)
186 len += snprintf(list + len, sizeof(list) - len,
187 "%s%d", i ? " " : "", ccids[i]);
192 static char *dccp_info(void)
194 char *info = get_sender_info(dss, "dccp");
195 char *ret = make_message("%s" "\tsupported ccids: %s\n",
196 info, dccp_list_available_ccids());
202 * The init function of the dccp sender.
204 * \param s pointer to the dccp sender struct.
206 * It initializes all function pointers of \a s and starts
207 * listening on the given port.
209 void dccp_send_init(struct sender *s)
216 s->send_fec = dccp_send_fec;
217 s->pre_select = dccp_pre_select;
218 s->post_select = dccp_post_select;
219 s->shutdown_clients = dccp_shutdown_clients;
220 s->help = generic_sender_help;
221 s->client_cmds[SENDER_ON] = dccp_com_on;
222 s->client_cmds[SENDER_OFF] = dccp_com_off;
223 s->client_cmds[SENDER_DENY] = dccp_com_deny;
224 s->client_cmds[SENDER_ALLOW] = dccp_com_allow;
225 s->client_cmds[SENDER_ADD] = NULL;
226 s->client_cmds[SENDER_DELETE] = NULL;
228 init_sender_status(dss, conf.dccp_access_arg, conf.dccp_access_given,
229 conf.dccp_port_arg, conf.dccp_max_clients_arg,
230 conf.dccp_default_deny_given);
231 ret = generic_com_on(dss, IPPROTO_DCCP);
233 PARA_ERROR_LOG("%s\n", para_strerror(-ret));