]> git.tuebingen.mpg.de Git - paraslash.git/blob - dccp_recv.c
5329852db53533bb652d0c997b7fdc55b76932b1
[paraslash.git] / dccp_recv.c
1 /*
2  * Copyright (C) 2006-2007 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 <sys/types.h>
15 #include <dirent.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "dccp.h"
20 #include "list.h"
21 #include "sched.h"
22 #include "recv.h"
23 #include "string.h"
24 #include "net.h"
25 #include "fd.h"
26
27 #include "dccp_recv.cmdline.h"
28
29 /** the size of the output buffer */
30 #define DCCP_BUFSIZE 40960
31
32 /**
33  * data specific to the dccp receiver
34  *
35  * \sa receiver receiver_node
36  */
37 struct private_dccp_recv_data {
38         /** the file descriptor for the dccp socket */
39         int fd;
40 };
41
42
43 static void dccp_recv_close(struct receiver_node *rn)
44 {
45
46         struct private_dccp_recv_data *pdd = rn->private_data;
47
48         if (pdd && pdd->fd > 0)
49                 close(pdd->fd);
50         free(rn->buf);
51         rn->buf = NULL;
52         free(rn->private_data);
53         rn->private_data = NULL;
54 }
55
56
57 static int dccp_recv_open(struct receiver_node *rn)
58 {
59         struct private_dccp_recv_data *pdd;
60         struct dccp_recv_args_info *conf = rn->conf;
61         int ret = makesock(AF_UNSPEC, IPPROTO_DCCP, 0, conf->host_arg, conf->port_arg);
62
63         if (ret < 0)
64                 return ret;
65
66         rn->buf = para_calloc(DCCP_BUFSIZE);
67         rn->private_data = pdd = para_calloc(sizeof(struct private_dccp_recv_data));
68
69         pdd->fd = ret;
70         mark_fd_nonblocking(pdd->fd);
71         return 1;
72 }
73
74 static void dccp_shutdown(void)
75 {
76         ; /* nothing to do */
77 }
78
79 static void *dccp_recv_parse_config(int argc, char **argv)
80 {
81         struct dccp_recv_args_info *tmp = para_calloc(sizeof(struct dccp_recv_args_info));
82
83         if (!dccp_recv_cmdline_parser(argc, argv, tmp))
84                 return tmp;
85         free(tmp);
86         return NULL;
87 }
88
89 static void dccp_recv_pre_select(struct sched *s, struct task *t)
90 {
91         struct receiver_node *rn = t->private_data;
92         struct private_dccp_recv_data *pdd = rn->private_data;
93
94         t->ret = 1;
95         para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
96 }
97
98 static void dccp_recv_post_select(struct sched *s, struct task *t)
99 {
100         struct receiver_node *rn = t->private_data;
101         struct private_dccp_recv_data *pdd = rn->private_data;
102
103         t->ret = -E_DCCP_RECV_EOF;
104         if (rn->output_eof && *rn->output_eof)
105                 goto out;
106         t->ret = 1;
107         if (!s->select_ret || !FD_ISSET(pdd->fd, &s->rfds))
108                 goto out; /* nothing to do */
109         t->ret = -E_DCCP_OVERRUN;
110         if (rn->loaded >= DCCP_BUFSIZE)
111                 goto out;
112         t->ret = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
113                 DCCP_BUFSIZE - rn->loaded);
114         if (t->ret <= 0) {
115                 if (!t->ret)
116                         t->ret = -E_DCCP_RECV_EOF;
117                 goto out;
118         }
119         rn->loaded += t->ret;
120         return;
121 out:
122         if (t->ret < 0)
123                 rn->eof = 1;
124 }
125
126 /**
127  * the init function of the dccp receiver
128  *
129  * \param r pointer to the receiver struct to initialize
130  *
131  * Initialize all function pointers of \a r
132  */
133 void dccp_recv_init(struct receiver *r)
134 {
135         r->shutdown = dccp_shutdown;
136         r->open = dccp_recv_open;
137         r->close = dccp_recv_close;
138         r->pre_select = dccp_recv_pre_select;
139         r->post_select = dccp_recv_post_select;
140         r->parse_config = dccp_recv_parse_config;
141 }