2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
19 /** \file dccp_recv.c paraslash's dccp receiver */
22 * based on client.c of dccp-cs-0.01.tar.bz2,
23 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
36 #include "dccp_recv.cmdline.h"
38 /* needed by getaddrinfo */
39 #include <sys/types.h>
40 #include <sys/socket.h>
43 /** the size of the output buffer */
44 #define DCCP_BUFSIZE 40960
47 * data specific to the dccp receiver
49 * \sa receiver receiver_node
51 struct private_dccp_recv_data {
52 /** the file descriptor for the dccp socket */
57 static void dccp_recv_close(struct receiver_node *rn)
60 struct private_dccp_recv_data *pdd = rn->private_data;
62 if (pdd && pdd->fd > 0)
66 free(rn->private_data);
67 rn->private_data = NULL;
71 static int dccp_recv_open(struct receiver_node *rn)
73 struct private_dccp_recv_data *pdd;
74 struct dccp_recv_args_info *conf = rn->conf;
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();
87 tmp = make_message("%d", conf->port_arg);
88 ret = getaddrinfo(conf->host_arg, tmp, NULL, &ai);
94 ret = dccp_set_socket(pdd->fd);
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)
101 mark_fd_nonblock(pdd->fd);
108 static void dccp_shutdown(void)
110 ; /* nothing to do */
113 static void *dccp_recv_parse_config(int argc, char **argv)
115 struct dccp_recv_args_info *tmp = para_calloc(sizeof(struct dccp_recv_args_info));
117 if (!dccp_recv_cmdline_parser(argc, argv, tmp))
123 static void dccp_recv_pre_select(struct sched *s, struct task *t)
125 struct receiver_node *rn = t->private_data;
126 struct private_dccp_recv_data *pdd = rn->private_data;
129 para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
132 static void dccp_recv_post_select(struct sched *s, struct task *t)
134 struct receiver_node *rn = t->private_data;
135 struct private_dccp_recv_data *pdd = rn->private_data;
137 t->ret = -E_DCCP_RECV_EOF;
138 if (rn->output_eof && *rn->output_eof)
141 if (!s->select_ret || !FD_ISSET(pdd->fd, &s->rfds))
142 goto out; /* nothing to do */
143 t->ret = -E_DCCP_OVERRUN;
144 if (rn->loaded >= DCCP_BUFSIZE)
146 t->ret = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
147 DCCP_BUFSIZE - rn->loaded);
150 t->ret = -E_DCCP_RECV_EOF;
153 rn->loaded += t->ret;
161 * the init function of the dccp receiver
163 * \param r pointer to the receiver struct to initialize
165 * Initialize all function pointers of \a r
167 void dccp_recv_init(struct receiver *r)
169 r->shutdown = dccp_shutdown;
170 r->open = dccp_recv_open;
171 r->close = dccp_recv_close;
172 r->pre_select = dccp_recv_pre_select;
173 r->post_select = dccp_recv_post_select;
174 r->parse_config = dccp_recv_parse_config;