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