Add btr support for the dccp receiver.
[paraslash.git] / dccp_recv.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_recv.c paraslash's dccp receiver */
8
9 /*
10 * based on client.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
18 #include "para.h"
19 #include "error.h"
20 #include "list.h"
21 #include "sched.h"
22 #include "ggo.h"
23 #include "recv.h"
24 #include "string.h"
25 #include "net.h"
26 #include "fd.h"
27 #include "buffer_tree.h"
28
29 #include "dccp_recv.cmdline.h"
30
31 /** the size of the output buffer */
32 #define DCCP_BUFSIZE 40960
33
34 /**
35 * data specific to the dccp receiver
36 *
37 * \sa receiver receiver_node
38 */
39 struct private_dccp_recv_data {
40 /** the file descriptor for the dccp socket */
41 int fd;
42 struct btr_pool *btrp;
43 };
44
45
46 static void dccp_recv_close(struct receiver_node *rn)
47 {
48
49 struct private_dccp_recv_data *pdd = rn->private_data;
50
51 if (pdd && pdd->fd > 0)
52 close(pdd->fd);
53 free(rn->buf);
54 rn->buf = NULL;
55 free(rn->private_data);
56 rn->private_data = NULL;
57 }
58
59
60 static int dccp_recv_open(struct receiver_node *rn)
61 {
62 struct private_dccp_recv_data *pdd;
63 struct dccp_recv_args_info *conf = rn->conf;
64 int fd, ret = makesock(AF_UNSPEC, IPPROTO_DCCP, 0, conf->host_arg,
65 conf->port_arg);
66
67 if (ret < 0)
68 return ret;
69 fd = ret;
70 /*
71 * Disable unused CCIDs: the receiver does not send any application
72 * data to the server. By shutting down this unused path we reduce
73 * internal processing costs, as the unused CCIDs (in the kernel) are
74 * then bypassed.
75 */
76 if (shutdown(fd, SHUT_WR) < 0) {
77 ret = -ERRNO_TO_PARA_ERROR(errno);
78 goto err;
79 }
80 ret = mark_fd_nonblocking(fd);
81 if (ret < 0)
82 goto err;
83 rn->buf = para_calloc(DCCP_BUFSIZE);
84 rn->private_data = pdd = para_calloc(sizeof(struct private_dccp_recv_data));
85 pdd->btrp = btr_pool_new("dccp_recv", 320 * 1024);
86 pdd->fd = fd;
87 return 1;
88 err:
89 close(fd);
90 return ret;
91 }
92
93 static void *dccp_recv_parse_config(int argc, char **argv)
94 {
95 struct dccp_recv_args_info *tmp = para_calloc(sizeof(struct dccp_recv_args_info));
96
97 if (!dccp_recv_cmdline_parser(argc, argv, tmp))
98 return tmp;
99 free(tmp);
100 return NULL;
101 }
102
103 static void dccp_recv_pre_select(struct sched *s, struct task *t)
104 {
105 struct receiver_node *rn = container_of(t, struct receiver_node, task);
106 struct private_dccp_recv_data *pdd = rn->private_data;
107
108 if (rn->btrn)
109 if (generic_recv_pre_select(s, t) <= 0)
110 return;
111 t->error = 0;
112 para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
113 }
114
115 static void dccp_recv_post_select(struct sched *s, struct task *t)
116 {
117 struct receiver_node *rn = container_of(t, struct receiver_node, task);
118 struct private_dccp_recv_data *pdd = rn->private_data;
119 struct btr_node *btrn = rn->btrn;
120 int ret;
121
122 if (btrn) {
123 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
124 if (ret < 0)
125 goto err;
126 if (ret == 0)
127 return;
128 } else {
129 if (rn->output_error && *rn->output_error < 0) {
130 t->error = *rn->output_error;
131 return;
132 }
133 }
134 if (!FD_ISSET(pdd->fd, &s->rfds))
135 return; /* nothing to do */
136 if (rn->loaded >= DCCP_BUFSIZE) {
137 t->error = -E_DCCP_OVERRUN;
138 return;
139 }
140 if (btrn) {
141 char *buf;
142 size_t sz;
143
144 sz = btr_pool_get_buffer(pdd->btrp, &buf);
145 ret = -E_DCCP_OVERRUN;
146 if (sz == 0)
147 goto err;
148 //buf = para_malloc(HTTP_RECV_READ_BUF_SIZE);
149 //sz = HTTP_RECV_READ_BUF_SIZE;
150 ret = recv_bin_buffer(pdd->fd, buf, sz);
151 if (ret == 0)
152 ret = -E_RECV_EOF;
153 if (ret < 0)
154 goto err;
155 btr_add_output_pool(pdd->btrp, ret, btrn);
156 return;
157 }
158 t->error = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
159 DCCP_BUFSIZE - rn->loaded);
160 if (t->error > 0) {
161 rn->loaded += t->error;
162 return;
163 }
164 if (!t->error)
165 t->error = -E_RECV_EOF;
166 return;
167 err:
168 if (btrn)
169 btr_remove_node(rn->btrn);
170 t->error = ret;
171 }
172
173 static void dccp_recv_free_config(void *conf)
174 {
175 dccp_recv_cmdline_parser_free(conf);
176 }
177
178 /**
179 * The init function of the dccp receiver.
180 *
181 * \param r Pointer to the receiver struct to initialize.
182 *
183 * Initialize all function pointers of \a r.
184 */
185 void dccp_recv_init(struct receiver *r)
186 {
187 struct dccp_recv_args_info dummy;
188
189 dccp_recv_cmdline_parser_init(&dummy);
190 r->open = dccp_recv_open;
191 r->close = dccp_recv_close;
192 r->pre_select = dccp_recv_pre_select;
193 r->post_select = dccp_recv_post_select;
194 r->parse_config = dccp_recv_parse_config;
195 r->free_config = dccp_recv_free_config;
196 r->help = (struct ggo_help) {
197 .short_help = dccp_recv_args_info_help,
198 .detailed_help = dccp_recv_args_info_detailed_help
199 };
200 dccp_recv_cmdline_parser_free(&dummy);
201 }