vss: unify calls to the FEC layer
[paraslash.git] / dccp_send.c
1 /*
2 * Copyright (C) 2006-2010 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file dccp_send.c Paraslash's dccp sender. */
8
9 /*
10 * based on server.c of dccp-cs-0.01.tar.bz2,
11 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
12 */
13
14 #include <regex.h>
15 #include <sys/types.h>
16 #include <dirent.h>
17 #include <osl.h>
18
19 #include "para.h"
20 #include "error.h"
21 #include "string.h"
22 #include "afh.h"
23 #include "afs.h"
24 #include "server.h"
25 #include "net.h"
26 #include "list.h"
27 #include "send.h"
28 #include "vss.h"
29 #include "fd.h"
30 #include "close_on_fork.h"
31 #include "chunk_queue.h"
32 #include "server.cmdline.h"
33 #include "acl.h"
34
35 /** Do not write more than that many bytes at once. */
36 #define DCCP_MAX_BYTES_PER_WRITE 1024
37
38 static struct sender_status dccp_sender_status, *dss = &dccp_sender_status;
39 static struct sender *self;
40
41
42 struct dccp_fec_client {
43 struct fec_client_parms fcp;
44 struct fec_client *fc;
45 struct sender_client *sc;
46 };
47
48 static void dccp_pre_select(int *max_fileno, fd_set *rfds,
49 __a_unused fd_set *wfds)
50 {
51 if (dss->listen_fd >= 0)
52 para_fd_set(dss->listen_fd, rfds, max_fileno);
53 }
54
55 /**
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.
59 *
60 * NB: This feature is only available on Linux > 2.6.30; on older kernels
61 * ENOPROTOOPT ("Protocol not available") will be returned.
62 */
63 static int dccp_get_tx_ccid(int sockfd)
64 {
65 int tx_ccid;
66 socklen_t len = sizeof(tx_ccid);
67
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));
71 return -1;
72 }
73 return tx_ccid;
74 }
75
76 static void dccp_shutdown_client(struct sender_client *sc)
77 {
78 struct dccp_fec_client *dfc = sc->private_data;
79
80 vss_del_fec_client(dfc->fc);
81 shutdown_client(sc, dss);
82 }
83
84 static void dccp_shutdown_clients(void)
85 {
86 struct sender_client *sc, *tmp;
87
88 list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
89 dccp_shutdown_client(sc);
90 }
91
92 static int dccp_open(void *client, struct fec_client_parms **fcp)
93 {
94 struct dccp_fec_client *dfc = client;
95
96 dfc->fcp.slices_per_group = 4;
97 dfc->fcp.data_slices_per_group = 3;
98 dfc->fcp.max_slice_bytes = 1472;
99 *fcp = &dfc->fcp;
100 return 1;
101 }
102
103 static int dccp_send_fec(char *buf, size_t len, void *private_data)
104 {
105 struct dccp_fec_client *dfc = private_data;
106 int ret = write_nonblock(dfc->sc->fd, buf, len, DCCP_MAX_BYTES_PER_WRITE);
107
108 if (ret < 0)
109 dccp_shutdown_client(dfc->sc);
110 return ret;
111 }
112
113 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
114 {
115 struct sender_client *sc;
116 struct dccp_fec_client *dfc;
117 int tx_ccid;
118
119 sc = accept_sender_client(dss, rfds);
120 if (!sc)
121 return;
122
123 /* If CCID identifiable, present client as <host>#<port>~<ccid> */
124 tx_ccid = dccp_get_tx_ccid(sc->fd);
125 if (tx_ccid != -1) {
126 char *tmp = make_message("%s~%d", sc->name, tx_ccid);
127
128 free(sc->name);
129 sc->name = tmp;
130 }
131 /*
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.
135 */
136 if (shutdown(sc->fd, SHUT_RD) < 0) {
137 PARA_WARNING_LOG("%s\n", strerror(errno));
138 shutdown_client(sc, dss);
139 return;
140 }
141 dfc = para_calloc(sizeof(*dfc));
142 sc->private_data = dfc;
143 dfc->sc = sc;
144 /* XXX RESOLVED LATER vss_add_fec_client(self, dfc, &dfc->fc); */
145 }
146
147 static int dccp_com_on(__a_unused struct sender_command_data *scd)
148 {
149 return generic_com_on(dss, IPPROTO_DCCP);
150 }
151
152 static int dccp_com_off(__a_unused struct sender_command_data *scd)
153 {
154 generic_com_off(dss);
155 return 1;
156 }
157
158
159 static int dccp_com_deny(struct sender_command_data *scd)
160 {
161 generic_com_deny(scd, dss);
162 return 1;
163 }
164
165 static int dccp_com_allow(struct sender_command_data *scd)
166 {
167 generic_com_allow(scd, dss);
168 return 1;
169 }
170
171 /**
172 * Return list of available CCIDs or warning, in static buffer.
173 */
174 static const char *dccp_list_available_ccids(void)
175 {
176 /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
177 static char list[DCCP_MAX_HOST_CCIDS * 4];
178 uint8_t *ccids;
179 int i, len, nccids;
180
181 nccids = dccp_available_ccids(&ccids);
182 if (nccids < 0) {
183 snprintf(list, sizeof(list), "Unable to query available CCIDs");
184 } else {
185 for (i = len = 0; i < nccids; i++)
186 len += snprintf(list + len, sizeof(list) - len,
187 "%s%d", i ? " " : "", ccids[i]);
188 }
189 return list;
190 }
191
192 static char *dccp_info(void)
193 {
194 char *info = get_sender_info(dss, "dccp");
195 char *ret = make_message("%s" "\tsupported ccids: %s\n",
196 info, dccp_list_available_ccids());
197 free(info);
198 return ret;
199 }
200
201 /**
202 * The init function of the dccp sender.
203 *
204 * \param s pointer to the dccp sender struct.
205 *
206 * It initializes all function pointers of \a s and starts
207 * listening on the given port.
208 */
209 void dccp_send_init(struct sender *s)
210 {
211 int ret;
212
213 s->info = dccp_info;
214 s->send = NULL;
215 s->open = dccp_open;
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;
227
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);
232 if (ret < 0)
233 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
234 self = s;
235 }