udp_recv: Kill non-btr code.
[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         t->error = 0;
109         if (generic_recv_pre_select(s, t) <= 0)
110                 return;
111         para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
112 }
113
114 static void dccp_recv_post_select(struct sched *s, struct task *t)
115 {
116         struct receiver_node *rn = container_of(t, struct receiver_node, task);
117         struct private_dccp_recv_data *pdd = rn->private_data;
118         struct btr_node *btrn = rn->btrn;
119         int ret;
120         char *buf;
121         size_t sz;
122
123         ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
124         if (ret < 0)
125                 goto err;
126         if (ret == 0)
127                 return;
128         if (!FD_ISSET(pdd->fd, &s->rfds))
129                 return; /* nothing to do */
130         if (rn->loaded >= DCCP_BUFSIZE) {
131                 t->error = -E_DCCP_OVERRUN;
132                 return;
133         }
134         ret = -E_DCCP_OVERRUN;
135         sz = btr_pool_get_buffer(pdd->btrp, &buf);
136         if (sz == 0)
137                 goto err;
138         ret = recv_bin_buffer(pdd->fd, buf, sz);
139         if (ret == 0)
140                 ret = -E_RECV_EOF;
141         if (ret < 0)
142                 goto err;
143         btr_add_output_pool(pdd->btrp, ret, btrn);
144         return;
145 err:
146         btr_remove_node(rn->btrn);
147         t->error = ret;
148 }
149
150 static void dccp_recv_free_config(void *conf)
151 {
152         dccp_recv_cmdline_parser_free(conf);
153 }
154
155 /**
156  * The init function of the dccp receiver.
157  *
158  * \param r Pointer to the receiver struct to initialize.
159  *
160  * Initialize all function pointers of \a r.
161  */
162 void dccp_recv_init(struct receiver *r)
163 {
164         struct dccp_recv_args_info dummy;
165
166         dccp_recv_cmdline_parser_init(&dummy);
167         r->open = dccp_recv_open;
168         r->close = dccp_recv_close;
169         r->pre_select = dccp_recv_pre_select;
170         r->post_select = dccp_recv_post_select;
171         r->parse_config = dccp_recv_parse_config;
172         r->free_config = dccp_recv_free_config;
173         r->help = (struct ggo_help) {
174                 .short_help = dccp_recv_args_info_help,
175                 .detailed_help = dccp_recv_args_info_detailed_help
176         };
177         dccp_recv_cmdline_parser_free(&dummy);
178 }