gui: Use lsu_merge_config_file_options().
[paraslash.git] / dccp_send.c
1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file dccp_send.c Paraslash's dccp sender. */
4
5 /*
6  * based on server.c of dccp-cs-0.01.tar.bz2,
7  * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
8  */
9
10 #include <netinet/in.h>
11 #include <sys/socket.h>
12 #include <regex.h>
13 #include <sys/types.h>
14 #include <arpa/inet.h>
15 #include <sys/un.h>
16 #include <netdb.h>
17 #include <lopsub.h>
18
19 #include "server.lsg.h"
20 #include "para.h"
21 #include "error.h"
22 #include "string.h"
23 #include "afh.h"
24 #include "net.h"
25 #include "server.h"
26 #include "list.h"
27 #include "send.h"
28 #include "sched.h"
29 #include "vss.h"
30 #include "fd.h"
31 #include "close_on_fork.h"
32 #include "chunk_queue.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         uint32_t mss; /* max slize size */
92
93         /* If call fails, return some sensible minimum value */
94         ret = getsockopt(sc->fd, SOL_DCCP, DCCP_SOCKOPT_GET_CUR_MPS, &mps, &ml);
95         if (ret < 0) {
96                 PARA_NOTICE_LOG("can not determine MPS: %s\n", strerror(errno));
97                 mps = generic_max_transport_msg_size(sc->fd) - DCCP_MAX_HEADER;
98         }
99         PARA_INFO_LOG("current MPS = %d bytes\n", mps);
100         assert(mps > 0);
101         mss = OPT_UINT32_VAL(DCCP_MAX_SLICE_SIZE);
102         if (mss > 0 && mss <= INT_MAX)
103                 mps = PARA_MIN(mps, (int)mss);
104         return mps;
105 }
106
107 static void dccp_send_fec(struct sender_client *sc, char *buf, size_t len)
108 {
109         int ret = xwrite(sc->fd, buf, len);
110
111         if (ret < 0)
112                 dccp_shutdown_client(sc);
113 }
114
115 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
116 {
117         struct sender_client *sc;
118         struct dccp_fec_client *dfc;
119         int tx_ccid;
120         uint32_t k, n;
121
122         sc = accept_sender_client(dss, rfds);
123         if (!sc)
124                 return;
125
126         /* If CCID identifiable, present client as <host>#<port>~<ccid> */
127         tx_ccid = dccp_get_tx_ccid(sc->fd);
128         if (tx_ccid != -1) {
129                 char *tmp = make_message("%s~%d", sc->name, tx_ccid);
130
131                 free(sc->name);
132                 sc->name = tmp;
133         }
134         /*
135          * Bypass unused CCID paths: the sender does not receive application data
136          * from the client; by shutting down this unused communication path we can
137          * reduce processing costs a bit. See analogous comment in \ref dccp_recv.c.
138          */
139         if (shutdown(sc->fd, SHUT_RD) < 0) {
140                 PARA_WARNING_LOG("%s\n", strerror(errno));
141                 shutdown_client(sc, dss);
142                 return;
143         }
144         dfc = para_calloc(sizeof(*dfc));
145         sc->private_data = dfc;
146         k = OPT_UINT32_VAL(DCCP_DATA_SLICES_PER_GROUP);
147         n = OPT_UINT32_VAL(DCCP_SLICES_PER_GROUP);
148         if (k == 0 || n == 0 || k >= n) {
149                 PARA_WARNING_LOG("invalid FEC parameters, using defaults\n");
150                 dfc->fcp.data_slices_per_group = 3;
151                 dfc->fcp.slices_per_group = 4;
152         } else {
153                 dfc->fcp.data_slices_per_group = k;
154                 dfc->fcp.slices_per_group = n;
155         }
156         dfc->fcp.init_fec               = dccp_init_fec;
157         dfc->fcp.send_fec               = dccp_send_fec;
158         dfc->fcp.need_periodic_header   = false;
159         dfc->fc = vss_add_fec_client(sc, &dfc->fcp);
160 }
161
162 static int dccp_com_on(__a_unused struct sender_command_data *scd)
163 {
164         generic_com_on(dss, IPPROTO_DCCP);
165         return 1;
166 }
167
168 static int dccp_com_off(__a_unused struct sender_command_data *scd)
169 {
170         dccp_shutdown_clients();
171         generic_com_off(dss);
172         return 1;
173 }
174
175
176 static int dccp_com_deny(struct sender_command_data *scd)
177 {
178         generic_com_deny(scd, dss);
179         return 1;
180 }
181
182 static int dccp_com_allow(struct sender_command_data *scd)
183 {
184         generic_com_allow(scd, dss);
185         return 1;
186 }
187
188 /**
189  * Return list of available CCIDs or warning, in static buffer.
190  */
191 static const char *dccp_list_available_ccids(void)
192 {
193         /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
194         static char list[DCCP_MAX_HOST_CCIDS * 4];
195         uint8_t *ccids;
196         int i, len, nccids;
197
198         nccids = dccp_available_ccids(&ccids);
199         if (nccids < 0) {
200                 snprintf(list, sizeof(list), "Unable to query available CCIDs");
201         } else {
202                 for (i = len = 0; i < nccids; i++)
203                         len += snprintf(list + len, sizeof(list) - len,
204                                         "%s%d", i ? " " : "", ccids[i]);
205         }
206         return list;
207 }
208
209 static char *dccp_status(void)
210 {
211         char *status = generic_sender_status(dss, "dccp");
212         char *result = make_message("%ssupported ccids: %s\n", status,
213                 dccp_list_available_ccids());
214         free(status);
215         return result;
216 }
217
218 /**
219  * The init function of the dccp sender.
220  *
221  * \param s pointer to the dccp sender struct.
222  *
223  * It initializes all function pointers of \a s and starts
224  * listening on the given port.
225  */
226 void dccp_send_init(struct sender *s)
227 {
228         s->status = dccp_status;
229         s->send = NULL;
230         s->pre_select = dccp_pre_select;
231         s->post_select = dccp_post_select;
232         s->shutdown_clients = dccp_shutdown_clients;
233         s->resolve_target = NULL;
234         s->help = generic_sender_help;
235         s->client_cmds[SENDER_on] = dccp_com_on;
236         s->client_cmds[SENDER_off] = dccp_com_off;
237         s->client_cmds[SENDER_deny] = dccp_com_deny;
238         s->client_cmds[SENDER_allow] = dccp_com_allow;
239         s->client_cmds[SENDER_add] = NULL;
240         s->client_cmds[SENDER_delete] = NULL;
241
242         init_sender_status(dss, OPT_RESULT(DCCP_ACCESS),
243                 OPT_UINT32_VAL(DCCP_PORT), OPT_UINT32_VAL(DCCP_MAX_CLIENTS),
244                 OPT_GIVEN(DCCP_DEFAULT_DENY));
245         generic_com_on(dss, IPPROTO_DCCP);
246 }