Merge branch 'refs/heads/t/si_update'
[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 #include <lopsub.h>
22
23 #include "server.lsg.h"
24 #include "para.h"
25 #include "error.h"
26 #include "string.h"
27 #include "afh.h"
28 #include "server.h"
29 #include "net.h"
30 #include "list.h"
31 #include "send.h"
32 #include "sched.h"
33 #include "vss.h"
34 #include "fd.h"
35 #include "close_on_fork.h"
36 #include "chunk_queue.h"
37 #include "acl.h"
38
39 static struct sender_status dccp_sender_status, *dss = &dccp_sender_status;
40
41 struct dccp_fec_client {
42 struct fec_client_parms fcp;
43 struct fec_client *fc;
44 };
45
46 static void dccp_pre_select(int *max_fileno, fd_set *rfds,
47 __a_unused fd_set *wfds)
48 {
49 if (dss->listen_fd >= 0)
50 para_fd_set(dss->listen_fd, rfds, max_fileno);
51 }
52
53 /**
54 * Query the TX CCID used on the sender-client half connection.
55 * \param sockfd Server socket descriptor to query (after accept(2)).
56 * \return CCID number > 0, -1 on error/incompatibility.
57 *
58 * NB: This feature is only available on Linux > 2.6.30; on older kernels
59 * ENOPROTOOPT ("Protocol not available") will be returned.
60 */
61 static int dccp_get_tx_ccid(int sockfd)
62 {
63 int tx_ccid;
64 socklen_t len = sizeof(tx_ccid);
65
66 if (getsockopt(sockfd, SOL_DCCP,
67 DCCP_SOCKOPT_TX_CCID, &tx_ccid, &len) < 0) {
68 PARA_WARNING_LOG("DCCP_SOCKOPT_TX_CCID: %s\n", strerror(errno));
69 return -1;
70 }
71 return tx_ccid;
72 }
73
74 static void dccp_shutdown_client(struct sender_client *sc)
75 {
76 struct dccp_fec_client *dfc = sc->private_data;
77
78 vss_del_fec_client(dfc->fc);
79 shutdown_client(sc, dss);
80 }
81
82 static void dccp_shutdown_clients(void)
83 {
84 struct sender_client *sc, *tmp;
85
86 list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
87 dccp_shutdown_client(sc);
88 }
89
90 /** * Obtain current MPS according to RFC 4340, sec. 14. */
91 static int dccp_init_fec(struct sender_client *sc)
92 {
93 int mps, ret;
94 socklen_t ml = sizeof(mps);
95 uint32_t mss; /* max slize size */
96
97 /* If call fails, return some sensible minimum value */
98 ret = getsockopt(sc->fd, SOL_DCCP, DCCP_SOCKOPT_GET_CUR_MPS, &mps, &ml);
99 if (ret < 0) {
100 PARA_NOTICE_LOG("can not determine MPS: %s\n", strerror(errno));
101 mps = generic_max_transport_msg_size(sc->fd) - DCCP_MAX_HEADER;
102 }
103 PARA_INFO_LOG("current MPS = %d bytes\n", mps);
104 assert(mps > 0);
105 mss = OPT_UINT32_VAL(DCCP_MAX_SLICE_SIZE);
106 if (mss > 0 && mss <= INT_MAX)
107 mps = PARA_MIN(mps, (int)mss);
108 return mps;
109 }
110
111 static void dccp_send_fec(struct sender_client *sc, char *buf, size_t len)
112 {
113 int ret = xwrite(sc->fd, buf, len);
114
115 if (ret < 0)
116 dccp_shutdown_client(sc);
117 }
118
119 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
120 {
121 struct sender_client *sc;
122 struct dccp_fec_client *dfc;
123 int tx_ccid;
124 uint32_t k, n;
125
126 sc = accept_sender_client(dss, rfds);
127 if (!sc)
128 return;
129
130 /* If CCID identifiable, present client as <host>#<port>~<ccid> */
131 tx_ccid = dccp_get_tx_ccid(sc->fd);
132 if (tx_ccid != -1) {
133 char *tmp = make_message("%s~%d", sc->name, tx_ccid);
134
135 free(sc->name);
136 sc->name = tmp;
137 }
138 /*
139 * Bypass unused CCID paths: the sender does not receive application data
140 * from the client; by shutting down this unused communication path we can
141 * reduce processing costs a bit. See analogous comment in \ref dccp_recv.c.
142 */
143 if (shutdown(sc->fd, SHUT_RD) < 0) {
144 PARA_WARNING_LOG("%s\n", strerror(errno));
145 shutdown_client(sc, dss);
146 return;
147 }
148 dfc = para_calloc(sizeof(*dfc));
149 sc->private_data = dfc;
150 k = OPT_UINT32_VAL(DCCP_DATA_SLICES_PER_GROUP);
151 n = OPT_UINT32_VAL(DCCP_SLICES_PER_GROUP);
152 if (k == 0 || n == 0 || k >= n) {
153 PARA_WARNING_LOG("invalid FEC parameters, using defaults\n");
154 dfc->fcp.data_slices_per_group = 3;
155 dfc->fcp.slices_per_group = 4;
156 } else {
157 dfc->fcp.data_slices_per_group = k;
158 dfc->fcp.slices_per_group = n;
159 }
160 dfc->fcp.init_fec = dccp_init_fec;
161 dfc->fcp.send_fec = dccp_send_fec;
162 dfc->fcp.need_periodic_header = false;
163 dfc->fc = vss_add_fec_client(sc, &dfc->fcp);
164 }
165
166 static int dccp_com_on(__a_unused struct sender_command_data *scd)
167 {
168 return generic_com_on(dss, IPPROTO_DCCP);
169 }
170
171 static int dccp_com_off(__a_unused struct sender_command_data *scd)
172 {
173 dccp_shutdown_clients();
174 generic_com_off(dss);
175 return 1;
176 }
177
178
179 static int dccp_com_deny(struct sender_command_data *scd)
180 {
181 generic_com_deny(scd, dss);
182 return 1;
183 }
184
185 static int dccp_com_allow(struct sender_command_data *scd)
186 {
187 generic_com_allow(scd, dss);
188 return 1;
189 }
190
191 /**
192 * Return list of available CCIDs or warning, in static buffer.
193 */
194 static const char *dccp_list_available_ccids(void)
195 {
196 /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
197 static char list[DCCP_MAX_HOST_CCIDS * 4];
198 uint8_t *ccids;
199 int i, len, nccids;
200
201 nccids = dccp_available_ccids(&ccids);
202 if (nccids < 0) {
203 snprintf(list, sizeof(list), "Unable to query available CCIDs");
204 } else {
205 for (i = len = 0; i < nccids; i++)
206 len += snprintf(list + len, sizeof(list) - len,
207 "%s%d", i ? " " : "", ccids[i]);
208 }
209 return list;
210 }
211
212 static char *dccp_status(void)
213 {
214 char *status = generic_sender_status(dss, "dccp");
215 char *result = make_message("%ssupported ccids: %s\n", status,
216 dccp_list_available_ccids());
217 free(status);
218 return result;
219 }
220
221 /**
222 * The init function of the dccp sender.
223 *
224 * \param s pointer to the dccp sender struct.
225 *
226 * It initializes all function pointers of \a s and starts
227 * listening on the given port.
228 */
229 void dccp_send_init(struct sender *s)
230 {
231 int ret;
232
233 s->status = dccp_status;
234 s->send = NULL;
235 s->pre_select = dccp_pre_select;
236 s->post_select = dccp_post_select;
237 s->shutdown_clients = dccp_shutdown_clients;
238 s->resolve_target = NULL;
239 s->help = generic_sender_help;
240 s->client_cmds[SENDER_on] = dccp_com_on;
241 s->client_cmds[SENDER_off] = dccp_com_off;
242 s->client_cmds[SENDER_deny] = dccp_com_deny;
243 s->client_cmds[SENDER_allow] = dccp_com_allow;
244 s->client_cmds[SENDER_add] = NULL;
245 s->client_cmds[SENDER_delete] = NULL;
246
247 init_sender_status(dss, OPT_RESULT(DCCP_ACCESS),
248 OPT_UINT32_VAL(DCCP_PORT), OPT_UINT32_VAL(DCCP_MAX_CLIENTS),
249 OPT_GIVEN(DCCP_DEFAULT_DENY));
250 ret = generic_com_on(dss, IPPROTO_DCCP);
251 if (ret < 0)
252 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
253 }