]> git.tuebingen.mpg.de Git - paraslash.git/blob - dccp_send.c
Move send_chunk() from send_common.c to http_send.c.
[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 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         return mps;
101 }
102
103 static int dccp_send_fec(struct sender_client *sc, char *buf, size_t len)
104 {
105         int ret = write_nonblock(sc->fd, buf, len);
106
107         if (ret < 0)
108                 dccp_shutdown_client(sc);
109         return ret;
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
118         sc = accept_sender_client(dss, rfds);
119         if (!sc)
120                 return;
121
122         /* If CCID identifiable, present client as <host>#<port>~<ccid> */
123         tx_ccid = dccp_get_tx_ccid(sc->fd);
124         if (tx_ccid != -1) {
125                 char *tmp = make_message("%s~%d", sc->name, tx_ccid);
126
127                 free(sc->name);
128                 sc->name = tmp;
129         }
130         /*
131          * Bypass unused CCID paths: the sender does not receive application data
132          * from the client; by shutting down this unused communication path we can
133          * reduce processing costs a bit. See analogous comment in dccp_recv.c.
134          */
135         if (shutdown(sc->fd, SHUT_RD) < 0) {
136                 PARA_WARNING_LOG("%s\n", strerror(errno));
137                 shutdown_client(sc, dss);
138                 return;
139         }
140         dfc = para_calloc(sizeof(*dfc));
141         sc->private_data = dfc;
142         dfc->fcp.slices_per_group       = 4;
143         dfc->fcp.data_slices_per_group  = 3;
144         dfc->fcp.init_fec               = dccp_init_fec;
145         dfc->fcp.send_fec               = dccp_send_fec;
146         dfc->fc = vss_add_fec_client(sc, &dfc->fcp);
147 }
148
149 static int dccp_com_on(__a_unused struct sender_command_data *scd)
150 {
151         return generic_com_on(dss, IPPROTO_DCCP);
152 }
153
154 static int dccp_com_off(__a_unused struct sender_command_data *scd)
155 {
156         generic_com_off(dss);
157         return 1;
158 }
159
160
161 static int dccp_com_deny(struct sender_command_data *scd)
162 {
163         generic_com_deny(scd, dss);
164         return 1;
165 }
166
167 static int dccp_com_allow(struct sender_command_data *scd)
168 {
169         generic_com_allow(scd, dss);
170         return 1;
171 }
172
173 /**
174  * Return list of available CCIDs or warning, in static buffer.
175  */
176 static const char *dccp_list_available_ccids(void)
177 {
178         /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
179         static char list[DCCP_MAX_HOST_CCIDS * 4];
180         uint8_t *ccids;
181         int i, len, nccids;
182
183         nccids = dccp_available_ccids(&ccids);
184         if (nccids < 0) {
185                 snprintf(list, sizeof(list), "Unable to query available CCIDs");
186         } else {
187                 for (i = len = 0; i < nccids; i++)
188                         len += snprintf(list + len, sizeof(list) - len,
189                                         "%s%d", i ? " " : "", ccids[i]);
190         }
191         return list;
192 }
193
194 static char *dccp_info(void)
195 {
196         char *info = get_sender_info(dss, "dccp");
197         char *ret  = make_message("%s" "\tsupported ccids: %s\n",
198                                   info, dccp_list_available_ccids());
199         free(info);
200         return ret;
201 }
202
203 /**
204  * The init function of the dccp sender.
205  *
206  * \param s pointer to the dccp sender struct.
207  *
208  * It initializes all function pointers of \a s and starts
209  * listening on the given port.
210  */
211 void dccp_send_init(struct sender *s)
212 {
213         int ret;
214
215         s->info = dccp_info;
216         s->send = NULL;
217         s->pre_select = dccp_pre_select;
218         s->post_select = dccp_post_select;
219         s->shutdown_clients = dccp_shutdown_clients;
220         s->help = generic_sender_help;
221         s->client_cmds[SENDER_ON] = dccp_com_on;
222         s->client_cmds[SENDER_OFF] = dccp_com_off;
223         s->client_cmds[SENDER_DENY] = dccp_com_deny;
224         s->client_cmds[SENDER_ALLOW] = dccp_com_allow;
225         s->client_cmds[SENDER_ADD] = NULL;
226         s->client_cmds[SENDER_DELETE] = NULL;
227
228         init_sender_status(dss, conf.dccp_access_arg, conf.dccp_access_given,
229                 conf.dccp_port_arg, conf.dccp_max_clients_arg,
230                 conf.dccp_default_deny_given);
231         ret = generic_com_on(dss, IPPROTO_DCCP);
232         if (ret < 0)
233                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
234 }