]> git.tuebingen.mpg.de Git - paraslash.git/blob - dccp_send.c
Remove some unused includes from {dccp,http}_send.c.
[paraslash.git] / dccp_send.c
1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file dccp_send.c Paraslash's dccp sender. */
4
5 /*
6  * based on server.c of dccp-cs-0.01.tar.bz2,
7  * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
8  */
9
10 #include <netinet/in.h>
11 #include <sys/socket.h>
12 #include <regex.h>
13 #include <sys/types.h>
14 #include <arpa/inet.h>
15 #include <sys/un.h>
16 #include <netdb.h>
17 #include <lopsub.h>
18
19 #include "server.lsg.h"
20 #include "para.h"
21 #include "error.h"
22 #include "string.h"
23 #include "afh.h"
24 #include "server.h"
25 #include "net.h"
26 #include "list.h"
27 #include "send.h"
28 #include "sched.h"
29 #include "vss.h"
30 #include "fd.h"
31
32 static struct sender_status dccp_sender_status, *dss = &dccp_sender_status;
33
34 struct dccp_fec_client {
35         struct fec_client_parms fcp;
36         struct fec_client *fc;
37 };
38
39 static void dccp_pre_select(int *max_fileno, fd_set *rfds,
40                 __a_unused fd_set *wfds)
41 {
42         if (dss->listen_fd >= 0)
43                 para_fd_set(dss->listen_fd, rfds, max_fileno);
44 }
45
46 /**
47  * Query the TX CCID used on the sender-client half connection.
48  * \param sockfd Server socket descriptor to query (after accept(2)).
49  * \return CCID number > 0, -1 on error/incompatibility.
50  *
51  * NB: This feature is only available on Linux > 2.6.30; on older kernels
52  * ENOPROTOOPT ("Protocol not available") will be returned.
53  */
54 static int dccp_get_tx_ccid(int sockfd)
55 {
56         int tx_ccid;
57         socklen_t len = sizeof(tx_ccid);
58
59         if (getsockopt(sockfd, SOL_DCCP,
60                        DCCP_SOCKOPT_TX_CCID, &tx_ccid, &len) < 0) {
61                 PARA_WARNING_LOG("DCCP_SOCKOPT_TX_CCID: %s\n", strerror(errno));
62                 return -1;
63         }
64         return tx_ccid;
65 }
66
67 static void dccp_shutdown_client(struct sender_client *sc)
68 {
69         struct dccp_fec_client *dfc = sc->private_data;
70
71         vss_del_fec_client(dfc->fc);
72         shutdown_client(sc, dss);
73 }
74
75 static void dccp_shutdown_clients(void)
76 {
77         struct sender_client *sc, *tmp;
78
79         list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
80                 dccp_shutdown_client(sc);
81 }
82
83 /** * Obtain current MPS according to RFC 4340, sec. 14. */
84 static int dccp_init_fec(struct sender_client *sc)
85 {
86         int mps, ret;
87         socklen_t ml = sizeof(mps);
88         uint32_t mss; /* max slize size */
89
90         /* If call fails, return some sensible minimum value */
91         ret = getsockopt(sc->fd, SOL_DCCP, DCCP_SOCKOPT_GET_CUR_MPS, &mps, &ml);
92         if (ret < 0) {
93                 PARA_NOTICE_LOG("can not determine MPS: %s\n", strerror(errno));
94                 mps = generic_max_transport_msg_size(sc->fd) - DCCP_MAX_HEADER;
95         }
96         PARA_INFO_LOG("current MPS = %d bytes\n", mps);
97         assert(mps > 0);
98         mss = OPT_UINT32_VAL(DCCP_MAX_SLICE_SIZE);
99         if (mss > 0 && mss <= INT_MAX)
100                 mps = PARA_MIN(mps, (int)mss);
101         return mps;
102 }
103
104 static void dccp_send_fec(struct sender_client *sc, char *buf, size_t len)
105 {
106         int ret = xwrite(sc->fd, buf, len);
107
108         if (ret < 0)
109                 dccp_shutdown_client(sc);
110 }
111
112 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
113 {
114         struct sender_client *sc;
115         struct dccp_fec_client *dfc;
116         int tx_ccid;
117         uint32_t k, n;
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 \ref 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         k = OPT_UINT32_VAL(DCCP_DATA_SLICES_PER_GROUP);
144         n = OPT_UINT32_VAL(DCCP_SLICES_PER_GROUP);
145         if (k == 0 || n == 0 || k >= n) {
146                 PARA_WARNING_LOG("invalid FEC parameters, using defaults\n");
147                 dfc->fcp.data_slices_per_group = 3;
148                 dfc->fcp.slices_per_group = 4;
149         } else {
150                 dfc->fcp.data_slices_per_group = k;
151                 dfc->fcp.slices_per_group = n;
152         }
153         dfc->fcp.init_fec               = dccp_init_fec;
154         dfc->fcp.send_fec               = dccp_send_fec;
155         dfc->fcp.need_periodic_header   = false;
156         dfc->fc = vss_add_fec_client(sc, &dfc->fcp);
157 }
158
159 static int dccp_com_on(__a_unused struct sender_command_data *scd)
160 {
161         return generic_com_on(dss, IPPROTO_DCCP);
162 }
163
164 static int dccp_com_off(__a_unused struct sender_command_data *scd)
165 {
166         dccp_shutdown_clients();
167         generic_com_off(dss);
168         return 1;
169 }
170
171
172 static int dccp_com_deny(struct sender_command_data *scd)
173 {
174         generic_com_deny(scd, dss);
175         return 1;
176 }
177
178 static int dccp_com_allow(struct sender_command_data *scd)
179 {
180         generic_com_allow(scd, dss);
181         return 1;
182 }
183
184 /**
185  * Return list of available CCIDs or warning, in static buffer.
186  */
187 static const char *dccp_list_available_ccids(void)
188 {
189         /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
190         static char list[DCCP_MAX_HOST_CCIDS * 4];
191         uint8_t *ccids;
192         int i, len, nccids;
193
194         nccids = dccp_available_ccids(&ccids);
195         if (nccids < 0) {
196                 snprintf(list, sizeof(list), "Unable to query available CCIDs");
197         } else {
198                 for (i = len = 0; i < nccids; i++)
199                         len += snprintf(list + len, sizeof(list) - len,
200                                         "%s%d", i ? " " : "", ccids[i]);
201         }
202         return list;
203 }
204
205 static char *dccp_status(void)
206 {
207         char *status = generic_sender_status(dss, "dccp");
208         char *result = make_message("%ssupported ccids: %s\n", status,
209                 dccp_list_available_ccids());
210         free(status);
211         return result;
212 }
213
214 /*
215  * Initialize the client list and the access control list and listen on the
216  * dccp port.
217  */
218 static void dccp_send_init(void)
219 {
220         int ret;
221
222         init_sender_status(dss, OPT_RESULT(DCCP_ACCESS),
223                 OPT_UINT32_VAL(DCCP_PORT), OPT_UINT32_VAL(DCCP_MAX_CLIENTS),
224                 OPT_GIVEN(DCCP_DEFAULT_DENY));
225         ret = generic_com_on(dss, IPPROTO_DCCP);
226         if (ret < 0)
227                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
228 }
229
230 /**
231  * The DCCP sender.
232  *
233  * This sender offers congestion control not available in plain TCP. Most
234  * methods of the sender structure are implemented as simple wrappers for the
235  * generic functions defined in \ref send_common.c. Like UDP streams, DCCP
236  * streams are sent FEC-encoded.
237  */
238 const struct sender dccp_sender = {
239         .name = "dccp",
240         .init = dccp_send_init,
241         .shutdown = dccp_shutdown_clients,
242         .pre_select = dccp_pre_select,
243         .post_select = dccp_post_select,
244         .shutdown_clients = dccp_shutdown_clients,
245         .client_cmds = {
246                 [SENDER_on] = dccp_com_on,
247                 [SENDER_off] = dccp_com_off,
248                 [SENDER_deny] = dccp_com_deny,
249                 [SENDER_allow] = dccp_com_allow,
250         },
251         .help = generic_sender_help,
252         .status = dccp_status,
253 };