aac_afh.c: Fix serious memory leak.
[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 "para.h"
15 #include "error.h"
16 #include "dccp.h"
17 #include "list.h"
18 #include "sched.h"
19 #include "recv.h"
20 #include "string.h"
21 #include "net.h"
22 #include "fd.h"
23
24 #include "dccp_recv.cmdline.h"
25
26 /* needed by getaddrinfo */
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netdb.h>
30
31 /** the size of the output buffer */
32 #define DCCP_BUFSIZE 40960
33
34 /**
35 * data specific to the dccp receiver
36 *
37 * \sa receiver receiver_node
38 */
39 struct private_dccp_recv_data {
40 /** the file descriptor for the dccp socket */
41 int fd;
42 };
43
44
45 static void dccp_recv_close(struct receiver_node *rn)
46 {
47
48 struct private_dccp_recv_data *pdd = rn->private_data;
49
50 if (pdd && pdd->fd > 0)
51 close(pdd->fd);
52 free(rn->buf);
53 rn->buf = NULL;
54 free(rn->private_data);
55 rn->private_data = NULL;
56 }
57
58
59 static int dccp_recv_open(struct receiver_node *rn)
60 {
61 struct private_dccp_recv_data *pdd;
62 struct dccp_recv_args_info *conf = rn->conf;
63 int ret;
64 struct addrinfo *ai;
65 char *tmp;
66
67 rn->buf = para_calloc(DCCP_BUFSIZE);
68 rn->private_data = para_calloc(sizeof(struct private_dccp_recv_data));
69 pdd = rn->private_data;
70 ret = dccp_get_socket();
71 if (ret < 0)
72 goto err_out;
73 pdd->fd = ret;
74
75 tmp = make_message("%d", conf->port_arg);
76 ret = getaddrinfo(conf->host_arg, tmp, NULL, &ai);
77 free(tmp);
78 if (ret) {
79 ret = -E_ADDR_INFO;
80 goto err_out;
81 }
82 ret = dccp_set_socket(pdd->fd);
83 if (ret < 0)
84 goto err_out;
85 PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg, conf->port_arg);
86 ret = connect(pdd->fd, ai->ai_addr, ai->ai_addrlen);
87 freeaddrinfo(ai);
88 if (ret < 0) {
89 ret = -E_DCCP_CONNECT;
90 goto err_out;
91 }
92 mark_fd_nonblock(pdd->fd);
93 return 1;
94 err_out:
95 dccp_recv_close(rn);
96 return ret;
97 }
98
99 static void dccp_shutdown(void)
100 {
101 ; /* nothing to do */
102 }
103
104 static void *dccp_recv_parse_config(int argc, char **argv)
105 {
106 struct dccp_recv_args_info *tmp = para_calloc(sizeof(struct dccp_recv_args_info));
107
108 if (!dccp_recv_cmdline_parser(argc, argv, tmp))
109 return tmp;
110 free(tmp);
111 return NULL;
112 }
113
114 static void dccp_recv_pre_select(struct sched *s, struct task *t)
115 {
116 struct receiver_node *rn = t->private_data;
117 struct private_dccp_recv_data *pdd = rn->private_data;
118
119 t->ret = 1;
120 para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
121 }
122
123 static void dccp_recv_post_select(struct sched *s, struct task *t)
124 {
125 struct receiver_node *rn = t->private_data;
126 struct private_dccp_recv_data *pdd = rn->private_data;
127
128 t->ret = -E_DCCP_RECV_EOF;
129 if (rn->output_eof && *rn->output_eof)
130 goto out;
131 t->ret = 1;
132 if (!s->select_ret || !FD_ISSET(pdd->fd, &s->rfds))
133 goto out; /* nothing to do */
134 t->ret = -E_DCCP_OVERRUN;
135 if (rn->loaded >= DCCP_BUFSIZE)
136 goto out;
137 t->ret = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
138 DCCP_BUFSIZE - rn->loaded);
139 if (t->ret <= 0) {
140 if (!t->ret)
141 t->ret = -E_DCCP_RECV_EOF;
142 goto out;
143 }
144 rn->loaded += t->ret;
145 return;
146 out:
147 if (t->ret < 0)
148 rn->eof = 1;
149 }
150
151 /**
152 * the init function of the dccp receiver
153 *
154 * \param r pointer to the receiver struct to initialize
155 *
156 * Initialize all function pointers of \a r
157 */
158 void dccp_recv_init(struct receiver *r)
159 {
160 r->shutdown = dccp_shutdown;
161 r->open = dccp_recv_open;
162 r->close = dccp_recv_close;
163 r->pre_select = dccp_recv_pre_select;
164 r->post_select = dccp_recv_post_select;
165 r->parse_config = dccp_recv_parse_config;
166 }