http_recv/dccp_recv: simplify post_select()
[paraslash.git] / dccp_recv.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file dccp_recv.c paraslash's dccp receiver */
20
21 /*
22 * based on client.c of dccp-cs-0.01.tar.bz2,
23 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
24 */
25
26 #include "para.h"
27 #include "error.h"
28 #include "dccp.h"
29 #include "list.h"
30 #include "sched.h"
31 #include "recv.h"
32 #include "string.h"
33 #include "net.h"
34 #include "fd.h"
35
36 #include "dccp_recv.cmdline.h"
37
38 /* needed by getaddrinfo */
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netdb.h>
42
43 /** the size of the output buffer */
44 #define DCCP_BUFSIZE 40960
45
46 /**
47 * data specific to the dccp receiver
48 *
49 * \sa receiver receiver_node
50 */
51 struct private_dccp_recv_data {
52 /** the file descriptor for the dccp socket */
53 int fd;
54 };
55
56
57 static void dccp_recv_close(struct receiver_node *rn)
58 {
59
60 struct private_dccp_recv_data *pdd = rn->private_data;
61
62 if (pdd && pdd->fd > 0)
63 close(pdd->fd);
64 free(rn->buf);
65 rn->buf = NULL;
66 free(rn->private_data);
67 rn->private_data = NULL;
68 }
69
70
71 static int dccp_recv_open(struct receiver_node *rn)
72 {
73 struct private_dccp_recv_data *pdd;
74 struct dccp_recv_args_info *conf = rn->conf;
75 int ret;
76 struct addrinfo *ai;
77 char *tmp;
78
79 rn->buf = para_calloc(DCCP_BUFSIZE);
80 rn->private_data = para_calloc(sizeof(struct private_dccp_recv_data));
81 pdd = rn->private_data;
82 ret = dccp_get_socket();
83 if (ret < 0)
84 goto err_out;
85 pdd->fd = ret;
86
87 tmp = make_message("%d", conf->port_arg);
88 ret = getaddrinfo(conf->host_arg, tmp, NULL, &ai);
89 free(tmp);
90 if (ret) {
91 ret = -E_ADDR_INFO;
92 goto err_out;
93 }
94 ret = dccp_set_socket(pdd->fd);
95 if (ret < 0)
96 goto err_out;
97 PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg, conf->port_arg);
98 ret = -E_DCCP_CONNECT;
99 if (connect(pdd->fd, ai->ai_addr, ai->ai_addrlen) < 0)
100 goto err_out;
101 return 1;
102 err_out:
103 dccp_recv_close(rn);
104 return ret;
105 }
106
107 static void dccp_shutdown(void)
108 {
109 ; /* nothing to do */
110 }
111
112 static void *dccp_recv_parse_config(int argc, char **argv)
113 {
114 struct dccp_recv_args_info *tmp = para_calloc(sizeof(struct dccp_recv_args_info));
115
116 if (!dccp_recv_cmdline_parser(argc, argv, tmp))
117 return tmp;
118 free(tmp);
119 return NULL;
120 }
121
122 static void dccp_recv_pre_select(struct sched *s, struct task *t)
123 {
124 struct receiver_node *rn = t->private_data;
125 struct private_dccp_recv_data *pdd = rn->private_data;
126
127 t->ret = 1;
128 if (!pdd)
129 return;
130 para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
131 }
132
133 static void dccp_recv_post_select(struct sched *s, struct task *t)
134 {
135 struct receiver_node *rn = t->private_data;
136 struct private_dccp_recv_data *pdd = rn->private_data;
137
138 t->ret = -E_DCCP_RECV_EOF;
139 if (rn->output_eof && *rn->output_eof)
140 goto out;
141 t->ret = 1;
142 if (!s->select_ret || !pdd || !FD_ISSET(pdd->fd, &s->rfds))
143 goto out; /* nothing to do */
144 t->ret = -E_DCCP_OVERRUN;
145 if (rn->loaded >= DCCP_BUFSIZE)
146 goto out;
147 t->ret = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
148 DCCP_BUFSIZE - rn->loaded);
149 if (t->ret <= 0) {
150 if (!t->ret)
151 t->ret = -E_DCCP_RECV_EOF;
152 goto out;
153 }
154 rn->loaded += t->ret;
155 return;
156 out:
157 if (t->ret < 0)
158 rn->eof = 1;
159 }
160
161 /**
162 * the init function of the dccp receiver
163 *
164 * \param r pointer to the receiver struct to initialize
165 *
166 * Initialize all function pointers of \a r
167 */
168 void dccp_recv_init(struct receiver *r)
169 {
170 r->shutdown = dccp_shutdown;
171 r->open = dccp_recv_open;
172 r->close = dccp_recv_close;
173 r->pre_select = dccp_recv_pre_select;
174 r->post_select = dccp_recv_post_select;
175 r->parse_config = dccp_recv_parse_config;
176 }