client: Do not leak buffer tree node on exit.
[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 "vss.h"
28 #include "fd.h"
29 #include "close_on_fork.h"
30 #include "chunk_queue.h"
31 #include "server.cmdline.h"
32 #include "acl.h"
33
34 static struct sender_status dccp_sender_status, *dss = &dccp_sender_status;
35
36 struct dccp_fec_client {
37         struct fec_client_parms fcp;
38         struct fec_client *fc;
39 };
40
41 static void dccp_pre_select(int *max_fileno, fd_set *rfds,
42                 __a_unused fd_set *wfds)
43 {
44         if (dss->listen_fd >= 0)
45                 para_fd_set(dss->listen_fd, rfds, max_fileno);
46 }
47
48 /**
49  * Query the TX CCID used on the sender-client half connection.
50  * \param sockfd Server socket descriptor to query (after accept(2)).
51  * \return CCID number > 0, -1 on error/incompatibility.
52  *
53  * NB: This feature is only available on Linux > 2.6.30; on older kernels
54  * ENOPROTOOPT ("Protocol not available") will be returned.
55  */
56 static int dccp_get_tx_ccid(int sockfd)
57 {
58         int tx_ccid;
59         socklen_t len = sizeof(tx_ccid);
60
61         if (getsockopt(sockfd, SOL_DCCP,
62                        DCCP_SOCKOPT_TX_CCID, &tx_ccid, &len) < 0) {
63                 PARA_WARNING_LOG("DCCP_SOCKOPT_TX_CCID: %s\n", strerror(errno));
64                 return -1;
65         }
66         return tx_ccid;
67 }
68
69 static void dccp_shutdown_client(struct sender_client *sc)
70 {
71         struct dccp_fec_client *dfc = sc->private_data;
72
73         vss_del_fec_client(dfc->fc);
74         shutdown_client(sc, dss);
75 }
76
77 static void dccp_shutdown_clients(void)
78 {
79         struct sender_client *sc, *tmp;
80
81         list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
82                 dccp_shutdown_client(sc);
83 }
84
85 /** * Obtain current MPS according to RFC 4340, sec. 14. */
86 static int dccp_init_fec(struct sender_client *sc)
87 {
88         int mps, ret;
89         socklen_t ml = sizeof(mps);
90
91         /* If call fails, return some sensible minimum value */
92         ret = getsockopt(sc->fd, SOL_DCCP, DCCP_SOCKOPT_GET_CUR_MPS, &mps, &ml);
93         if (ret < 0) {
94                 PARA_NOTICE_LOG("can not determine MPS: %s\n", strerror(errno));
95                 mps = generic_max_transport_msg_size(sc->fd) - DCCP_MAX_HEADER;
96         }
97         PARA_INFO_LOG("current MPS = %d bytes\n", mps);
98         assert(mps > 0);
99         if (conf.dccp_max_slice_size_arg > 0)
100                 mps = PARA_MIN(mps, conf.dccp_max_slice_size_arg);
101         return mps;
102 }
103
104 static int dccp_send_fec(struct sender_client *sc, char *buf, size_t len)
105 {
106         int ret = write_nonblock(sc->fd, buf, len);
107
108         if (ret < 0)
109                 dccp_shutdown_client(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->fcp.data_slices_per_group  = conf.dccp_data_slices_per_group_arg;
144         dfc->fcp.slices_per_group       = conf.dccp_slices_per_group_arg;
145         dfc->fcp.init_fec               = dccp_init_fec;
146         dfc->fcp.send_fec               = dccp_send_fec;
147         dfc->fcp.need_periodic_header   = false;
148         dfc->fc = vss_add_fec_client(sc, &dfc->fcp);
149 }
150
151 static int dccp_com_on(__a_unused struct sender_command_data *scd)
152 {
153         return generic_com_on(dss, IPPROTO_DCCP);
154 }
155
156 static int dccp_com_off(__a_unused struct sender_command_data *scd)
157 {
158         dccp_shutdown_clients();
159         generic_com_off(dss);
160         return 1;
161 }
162
163
164 static int dccp_com_deny(struct sender_command_data *scd)
165 {
166         generic_com_deny(scd, dss);
167         return 1;
168 }
169
170 static int dccp_com_allow(struct sender_command_data *scd)
171 {
172         generic_com_allow(scd, dss);
173         return 1;
174 }
175
176 /**
177  * Return list of available CCIDs or warning, in static buffer.
178  */
179 static const char *dccp_list_available_ccids(void)
180 {
181         /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
182         static char list[DCCP_MAX_HOST_CCIDS * 4];
183         uint8_t *ccids;
184         int i, len, nccids;
185
186         nccids = dccp_available_ccids(&ccids);
187         if (nccids < 0) {
188                 snprintf(list, sizeof(list), "Unable to query available CCIDs");
189         } else {
190                 for (i = len = 0; i < nccids; i++)
191                         len += snprintf(list + len, sizeof(list) - len,
192                                         "%s%d", i ? " " : "", ccids[i]);
193         }
194         return list;
195 }
196
197 static char *dccp_info(void)
198 {
199         char *info = get_sender_info(dss, "dccp");
200         char *ret  = make_message("%s" "\tsupported ccids: %s\n",
201                                   info, dccp_list_available_ccids());
202         free(info);
203         return ret;
204 }
205
206 /**
207  * The init function of the dccp sender.
208  *
209  * \param s pointer to the dccp sender struct.
210  *
211  * It initializes all function pointers of \a s and starts
212  * listening on the given port.
213  */
214 void dccp_send_init(struct sender *s)
215 {
216         int ret, k, n;
217
218         s->info = dccp_info;
219         s->send = NULL;
220         s->pre_select = dccp_pre_select;
221         s->post_select = dccp_post_select;
222         s->shutdown_clients = dccp_shutdown_clients;
223         s->resolve_target = NULL;
224         s->help = generic_sender_help;
225         s->client_cmds[SENDER_ON] = dccp_com_on;
226         s->client_cmds[SENDER_OFF] = dccp_com_off;
227         s->client_cmds[SENDER_DENY] = dccp_com_deny;
228         s->client_cmds[SENDER_ALLOW] = dccp_com_allow;
229         s->client_cmds[SENDER_ADD] = NULL;
230         s->client_cmds[SENDER_DELETE] = NULL;
231
232         k = conf.dccp_data_slices_per_group_arg;
233         n = conf.dccp_slices_per_group_arg;
234
235         if (k <= 0 || n <= 0 || k >= n) {
236                 PARA_WARNING_LOG("invalid FEC parameters, using defaults\n");
237                 conf.dccp_data_slices_per_group_arg = 3;
238                 conf.dccp_slices_per_group_arg = 4;
239         }
240
241         init_sender_status(dss, conf.dccp_access_arg, conf.dccp_access_given,
242                 conf.dccp_port_arg, conf.dccp_max_clients_arg,
243                 conf.dccp_default_deny_given);
244         ret = generic_com_on(dss, IPPROTO_DCCP);
245         if (ret < 0)
246                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
247 }