aft.c: Move definition of "buf" down a bit.
[paraslash.git] / dccp_recv.c
1 /*
2 * Copyright (C) 2006-2008 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 "list.h"
20 #include "sched.h"
21 #include "recv.h"
22 #include "string.h"
23 #include "net.h"
24 #include "fd.h"
25
26 #include "dccp_recv.cmdline.h"
27
28 /** the size of the output buffer */
29 #define DCCP_BUFSIZE 40960
30
31 /**
32 * data specific to the dccp receiver
33 *
34 * \sa receiver receiver_node
35 */
36 struct private_dccp_recv_data {
37 /** the file descriptor for the dccp socket */
38 int fd;
39 };
40
41
42 static void dccp_recv_close(struct receiver_node *rn)
43 {
44
45 struct private_dccp_recv_data *pdd = rn->private_data;
46
47 if (pdd && pdd->fd > 0)
48 close(pdd->fd);
49 free(rn->buf);
50 rn->buf = NULL;
51 free(rn->private_data);
52 rn->private_data = NULL;
53 }
54
55
56 static int dccp_recv_open(struct receiver_node *rn)
57 {
58 struct private_dccp_recv_data *pdd;
59 struct dccp_recv_args_info *conf = rn->conf;
60 int fd, ret = makesock(AF_UNSPEC, IPPROTO_DCCP, 0, conf->host_arg,
61 conf->port_arg);
62
63 if (ret < 0)
64 return ret;
65 fd = ret;
66 /*
67 * Disable unused CCIDs: the receiver does not send any application
68 * data to the server. By shutting down this unused path we reduce
69 * internal processing costs, as the unused CCIDs (in the kernel) are
70 * then bypassed.
71 */
72 if (shutdown(fd, SHUT_WR) < 0) {
73 ret = -ERRNO_TO_PARA_ERROR(errno);
74 goto err;
75 }
76 ret = mark_fd_nonblocking(fd);
77 if (ret < 0)
78 goto err;
79 rn->buf = para_calloc(DCCP_BUFSIZE);
80 rn->private_data = pdd = para_calloc(sizeof(struct private_dccp_recv_data));
81 pdd->fd = fd;
82 return 1;
83 err:
84 close(fd);
85 return ret;
86 }
87
88 static void dccp_shutdown(void)
89 {
90 ; /* nothing to do */
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 = t->private_data;
106 struct private_dccp_recv_data *pdd = rn->private_data;
107
108 t->ret = 1;
109 para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
110 }
111
112 static void dccp_recv_post_select(struct sched *s, struct task *t)
113 {
114 struct receiver_node *rn = t->private_data;
115 struct private_dccp_recv_data *pdd = rn->private_data;
116
117 if (rn->output_error && *rn->output_error) {
118 t->ret = *rn->output_error;
119 goto out;
120 }
121 t->ret = 1;
122 if (!s->select_ret || !FD_ISSET(pdd->fd, &s->rfds))
123 goto out; /* nothing to do */
124 t->ret = -E_DCCP_OVERRUN;
125 if (rn->loaded >= DCCP_BUFSIZE)
126 goto out;
127 t->ret = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
128 DCCP_BUFSIZE - rn->loaded);
129 if (t->ret <= 0) {
130 if (!t->ret)
131 t->ret = -E_RECV_EOF;
132 goto out;
133 }
134 rn->loaded += t->ret;
135 return;
136 out:
137 if (t->ret < 0)
138 rn->error = t->ret;
139 }
140
141 /**
142 * the init function of the dccp receiver
143 *
144 * \param r pointer to the receiver struct to initialize
145 *
146 * Initialize all function pointers of \a r
147 */
148 void dccp_recv_init(struct receiver *r)
149 {
150 r->shutdown = dccp_shutdown;
151 r->open = dccp_recv_open;
152 r->close = dccp_recv_close;
153 r->pre_select = dccp_recv_pre_select;
154 r->post_select = dccp_recv_post_select;
155 r->parse_config = dccp_recv_parse_config;
156 }