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