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