05_dccp-supported-ccid-lookup.diff
[paraslash.git] / dccp_send.c
1 /*
2 * Copyright (C) 2006-2009 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 "vss.h"
28 #include "send.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 static void dccp_pre_select(int *max_fileno, fd_set *rfds,
41 __a_unused fd_set *wfds)
42 {
43 if (dss->listen_fd >= 0)
44 para_fd_set(dss->listen_fd, rfds, max_fileno);
45 }
46
47 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
48 {
49 struct sender_client *sc;
50
51 if (dss->listen_fd < 0 || !FD_ISSET(dss->listen_fd, rfds))
52 return;
53 sc = accept_sender_client(dss);
54 if (!sc)
55 return;
56 /*
57 * Bypass unused CCID paths: the sender does not receive application data
58 * from the client; by shutting down this unused communication path we can
59 * reduce processing costs a bit. See analogous comment in dccp_recv.c.
60 */
61 if (shutdown(sc->fd, SHUT_RD) >= 0)
62 return;
63 PARA_WARNING_LOG("%s\n", strerror(errno));
64 shutdown_client(sc, dss);
65 }
66
67 static void dccp_send(long unsigned current_chunk,
68 __a_unused long unsigned chunks_sent, const char *buf,
69 size_t len, const char *header_buf, size_t header_len)
70 {
71 struct sender_client *sc, *tmp;
72
73 list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
74 send_chunk(sc, dss, DCCP_MAX_BYTES_PER_WRITE, current_chunk, buf,
75 len, header_buf, header_len);
76 }
77
78 static void dccp_shutdown_clients(void)
79 {
80 shutdown_clients(dss);
81 }
82
83 static int dccp_com_on(__a_unused struct sender_command_data *scd)
84 {
85 return generic_com_on(dss, IPPROTO_DCCP);
86 }
87
88 static int dccp_com_off(__a_unused struct sender_command_data *scd)
89 {
90 generic_com_off(dss);
91 return 1;
92 }
93
94
95 static int dccp_com_deny(struct sender_command_data *scd)
96 {
97 generic_com_deny(scd, dss);
98 return 1;
99 }
100
101 static int dccp_com_allow(struct sender_command_data *scd)
102 {
103 generic_com_allow(scd, dss);
104 return 1;
105 }
106
107 /**
108 * Return list of available CCIDs or warning, in static buffer.
109 */
110 static const char *dccp_list_available_ccids(void)
111 {
112 uint8_t ccids[DCCP_MAX_HOST_CCIDS];
113 uint8_t nccids = sizeof(ccids), i, len;
114 /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
115 static char list[DCCP_MAX_HOST_CCIDS * 4];
116
117 if (dccp_available_ccids(ccids, &nccids) == NULL) {
118 snprintf(list, sizeof(list), "Unable to query available CCIDs");
119 } else {
120 for (i = len = 0; i < nccids; i++)
121 len += snprintf(list + len, sizeof(list) - len,
122 "%s%d", i ? " " : "", ccids[i]);
123 }
124 return list;
125 }
126
127 static char *dccp_info(void)
128 {
129 char *info = get_sender_info(dss, "dccp");
130 char *ret = make_message("%s" "\tsupported ccids: %s\n",
131 info, dccp_list_available_ccids());
132 free(info);
133 return ret;
134 }
135
136 /**
137 * The init function of the dccp sender.
138 *
139 * \param s pointer to the dccp sender struct.
140 *
141 * It initializes all function pointers of \a s and starts
142 * listening on the given port.
143 */
144 void dccp_send_init(struct sender *s)
145 {
146 int ret;
147
148 s->info = dccp_info;
149 s->send = dccp_send;
150 s->pre_select = dccp_pre_select;
151 s->post_select = dccp_post_select;
152 s->shutdown_clients = dccp_shutdown_clients;
153 s->help = generic_sender_help;
154 s->client_cmds[SENDER_ON] = dccp_com_on;
155 s->client_cmds[SENDER_OFF] = dccp_com_off;
156 s->client_cmds[SENDER_DENY] = dccp_com_deny;
157 s->client_cmds[SENDER_ALLOW] = dccp_com_allow;
158 s->client_cmds[SENDER_ADD] = NULL;
159 s->client_cmds[SENDER_DELETE] = NULL;
160
161 init_sender_status(dss, conf.dccp_access_arg, conf.dccp_access_given,
162 conf.dccp_port_arg, conf.dccp_max_clients_arg,
163 conf.dccp_default_deny_given);
164 ret = generic_com_on(dss, IPPROTO_DCCP);
165 if (ret < 0)
166 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
167 }