filter: Make ->open() optional.
[paraslash.git] / dccp_send.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
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 <netinet/in.h>
15 #include <sys/socket.h>
16 #include <regex.h>
17 #include <sys/types.h>
18 #include <arpa/inet.h>
19 #include <sys/un.h>
20 #include <netdb.h>
21
22 #include "para.h"
23 #include "error.h"
24 #include "string.h"
25 #include "afh.h"
26 #include "server.h"
27 #include "net.h"
28 #include "list.h"
29 #include "send.h"
30 #include "sched.h"
31 #include "vss.h"
32 #include "fd.h"
33 #include "close_on_fork.h"
34 #include "chunk_queue.h"
35 #include "server.cmdline.h"
36 #include "acl.h"
37
38 static struct sender_status dccp_sender_status, *dss = &dccp_sender_status;
39
40 struct dccp_fec_client {
41 struct fec_client_parms fcp;
42 struct fec_client *fc;
43 };
44
45 static void dccp_pre_select(int *max_fileno, fd_set *rfds,
46 __a_unused fd_set *wfds)
47 {
48 if (dss->listen_fd >= 0)
49 para_fd_set(dss->listen_fd, rfds, max_fileno);
50 }
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 /** * Obtain current MPS according to RFC 4340, sec. 14. */
90 static int dccp_init_fec(struct sender_client *sc)
91 {
92 int mps, ret;
93 socklen_t ml = sizeof(mps);
94
95 /* If call fails, return some sensible minimum value */
96 ret = getsockopt(sc->fd, SOL_DCCP, DCCP_SOCKOPT_GET_CUR_MPS, &mps, &ml);
97 if (ret < 0) {
98 PARA_NOTICE_LOG("can not determine MPS: %s\n", strerror(errno));
99 mps = generic_max_transport_msg_size(sc->fd) - DCCP_MAX_HEADER;
100 }
101 PARA_INFO_LOG("current MPS = %d bytes\n", mps);
102 assert(mps > 0);
103 if (conf.dccp_max_slice_size_arg > 0)
104 mps = PARA_MIN(mps, conf.dccp_max_slice_size_arg);
105 return mps;
106 }
107
108 static void dccp_send_fec(struct sender_client *sc, char *buf, size_t len)
109 {
110 int ret = xwrite(sc->fd, buf, len);
111
112 if (ret < 0)
113 dccp_shutdown_client(sc);
114 }
115
116 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
117 {
118 struct sender_client *sc;
119 struct dccp_fec_client *dfc;
120 int tx_ccid;
121
122 sc = accept_sender_client(dss, rfds);
123 if (!sc)
124 return;
125
126 /* If CCID identifiable, present client as <host>#<port>~<ccid> */
127 tx_ccid = dccp_get_tx_ccid(sc->fd);
128 if (tx_ccid != -1) {
129 char *tmp = make_message("%s~%d", sc->name, tx_ccid);
130
131 free(sc->name);
132 sc->name = tmp;
133 }
134 /*
135 * Bypass unused CCID paths: the sender does not receive application data
136 * from the client; by shutting down this unused communication path we can
137 * reduce processing costs a bit. See analogous comment in dccp_recv.c.
138 */
139 if (shutdown(sc->fd, SHUT_RD) < 0) {
140 PARA_WARNING_LOG("%s\n", strerror(errno));
141 shutdown_client(sc, dss);
142 return;
143 }
144 dfc = para_calloc(sizeof(*dfc));
145 sc->private_data = dfc;
146 dfc->fcp.data_slices_per_group = conf.dccp_data_slices_per_group_arg;
147 dfc->fcp.slices_per_group = conf.dccp_slices_per_group_arg;
148 dfc->fcp.init_fec = dccp_init_fec;
149 dfc->fcp.send_fec = dccp_send_fec;
150 dfc->fcp.need_periodic_header = false;
151 dfc->fc = vss_add_fec_client(sc, &dfc->fcp);
152 }
153
154 static int dccp_com_on(__a_unused struct sender_command_data *scd)
155 {
156 return generic_com_on(dss, IPPROTO_DCCP);
157 }
158
159 static int dccp_com_off(__a_unused struct sender_command_data *scd)
160 {
161 dccp_shutdown_clients();
162 generic_com_off(dss);
163 return 1;
164 }
165
166
167 static int dccp_com_deny(struct sender_command_data *scd)
168 {
169 generic_com_deny(scd, dss);
170 return 1;
171 }
172
173 static int dccp_com_allow(struct sender_command_data *scd)
174 {
175 generic_com_allow(scd, dss);
176 return 1;
177 }
178
179 /**
180 * Return list of available CCIDs or warning, in static buffer.
181 */
182 static const char *dccp_list_available_ccids(void)
183 {
184 /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
185 static char list[DCCP_MAX_HOST_CCIDS * 4];
186 uint8_t *ccids;
187 int i, len, nccids;
188
189 nccids = dccp_available_ccids(&ccids);
190 if (nccids < 0) {
191 snprintf(list, sizeof(list), "Unable to query available CCIDs");
192 } else {
193 for (i = len = 0; i < nccids; i++)
194 len += snprintf(list + len, sizeof(list) - len,
195 "%s%d", i ? " " : "", ccids[i]);
196 }
197 return list;
198 }
199
200 static char *dccp_status(void)
201 {
202 char *status = generic_sender_status(dss, "dccp");
203 char *result = make_message("%ssupported ccids: %s\n", status,
204 dccp_list_available_ccids());
205 free(status);
206 return result;
207 }
208
209 /**
210 * The init function of the dccp sender.
211 *
212 * \param s pointer to the dccp sender struct.
213 *
214 * It initializes all function pointers of \a s and starts
215 * listening on the given port.
216 */
217 void dccp_send_init(struct sender *s)
218 {
219 int ret, k, n;
220
221 s->status = dccp_status;
222 s->send = NULL;
223 s->pre_select = dccp_pre_select;
224 s->post_select = dccp_post_select;
225 s->shutdown_clients = dccp_shutdown_clients;
226 s->resolve_target = NULL;
227 s->help = generic_sender_help;
228 s->client_cmds[SENDER_on] = dccp_com_on;
229 s->client_cmds[SENDER_off] = dccp_com_off;
230 s->client_cmds[SENDER_deny] = dccp_com_deny;
231 s->client_cmds[SENDER_allow] = dccp_com_allow;
232 s->client_cmds[SENDER_add] = NULL;
233 s->client_cmds[SENDER_delete] = NULL;
234
235 k = conf.dccp_data_slices_per_group_arg;
236 n = conf.dccp_slices_per_group_arg;
237
238 if (k <= 0 || n <= 0 || k >= n) {
239 PARA_WARNING_LOG("invalid FEC parameters, using defaults\n");
240 conf.dccp_data_slices_per_group_arg = 3;
241 conf.dccp_slices_per_group_arg = 4;
242 }
243
244 init_sender_status(dss, conf.dccp_access_arg, conf.dccp_access_given,
245 conf.dccp_port_arg, conf.dccp_max_clients_arg,
246 conf.dccp_default_deny_given);
247 ret = generic_com_on(dss, IPPROTO_DCCP);
248 if (ret < 0)
249 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
250 }