Fix stream grabbing.
[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
28 #include "dccp_recv.cmdline.h"
29
30 /** the size of the output buffer */
31 #define DCCP_BUFSIZE 40960
32
33 /**
34  * data specific to the dccp receiver
35  *
36  * \sa receiver receiver_node
37  */
38 struct private_dccp_recv_data {
39         /** the file descriptor for the dccp socket */
40         int fd;
41 };
42
43
44 static void dccp_recv_close(struct receiver_node *rn)
45 {
46
47         struct private_dccp_recv_data *pdd = rn->private_data;
48
49         if (pdd && pdd->fd > 0)
50                 close(pdd->fd);
51         free(rn->buf);
52         rn->buf = NULL;
53         free(rn->private_data);
54         rn->private_data = NULL;
55 }
56
57
58 static int dccp_recv_open(struct receiver_node *rn)
59 {
60         struct private_dccp_recv_data *pdd;
61         struct dccp_recv_args_info *conf = rn->conf;
62         int fd, ret = makesock(AF_UNSPEC, IPPROTO_DCCP, 0, conf->host_arg,
63                 conf->port_arg);
64
65         if (ret < 0)
66                 return ret;
67         fd = ret;
68         /*
69          * Disable unused CCIDs: the receiver does not send any application
70          * data to the server. By shutting down this unused path we reduce
71          * internal processing costs, as the unused CCIDs (in the kernel) are
72          * then bypassed.
73          */
74         if (shutdown(fd, SHUT_WR) < 0) {
75                 ret = -ERRNO_TO_PARA_ERROR(errno);
76                 goto err;
77         }
78         ret = mark_fd_nonblocking(fd);
79         if (ret < 0)
80                 goto err;
81         rn->buf = para_calloc(DCCP_BUFSIZE);
82         rn->private_data = pdd = para_calloc(sizeof(struct private_dccp_recv_data));
83         pdd->fd = fd;
84         return 1;
85 err:
86         close(fd);
87         return ret;
88 }
89
90 static void dccp_shutdown(void)
91 {
92         ; /* nothing to do */
93 }
94
95 static void *dccp_recv_parse_config(int argc, char **argv)
96 {
97         struct dccp_recv_args_info *tmp = para_calloc(sizeof(struct dccp_recv_args_info));
98
99         if (!dccp_recv_cmdline_parser(argc, argv, tmp))
100                 return tmp;
101         free(tmp);
102         return NULL;
103 }
104
105 static void dccp_recv_pre_select(struct sched *s, struct task *t)
106 {
107         struct receiver_node *rn = container_of(t, struct receiver_node, task);
108         struct private_dccp_recv_data *pdd = rn->private_data;
109
110         t->error = 0;
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
119         if (rn->output_error && *rn->output_error < 0) {
120                 t->error = *rn->output_error;
121                 return;
122         }
123         if (!FD_ISSET(pdd->fd, &s->rfds))
124                 return; /* nothing to do */
125         if (rn->loaded >= DCCP_BUFSIZE) {
126                 t->error = -E_DCCP_OVERRUN;
127                 return;
128         }
129         t->error = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
130                 DCCP_BUFSIZE - rn->loaded);
131         if (t->error > 0) {
132                 rn->loaded += t->error;
133                 return;
134         }
135         if (!t->error)
136                 t->error = -E_RECV_EOF;
137 }
138
139 static void dccp_recv_free_config(void *conf)
140 {
141         dccp_recv_cmdline_parser_free(conf);
142 }
143
144 /**
145  * The init function of the dccp receiver.
146  *
147  * \param r Pointer to the receiver struct to initialize.
148  *
149  * Initialize all function pointers of \a r.
150  */
151 void dccp_recv_init(struct receiver *r)
152 {
153         struct dccp_recv_args_info dummy;
154
155         dccp_recv_cmdline_parser_init(&dummy);
156         r->shutdown = dccp_shutdown;
157         r->open = dccp_recv_open;
158         r->close = dccp_recv_close;
159         r->pre_select = dccp_recv_pre_select;
160         r->post_select = dccp_recv_post_select;
161         r->parse_config = dccp_recv_parse_config;
162         r->free_config = dccp_recv_free_config;
163         r->help = (struct ggo_help) {
164                 .short_help = dccp_recv_args_info_help,
165                 .detailed_help = dccp_recv_args_info_detailed_help
166         };
167         dccp_recv_cmdline_parser_free(&dummy);
168 }