dccp_recv: Avoid NULL pointer dereference.
[paraslash.git] / dccp_recv.c
1 /*
2 * Copyright (C) 2006-2011 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 <regex.h>
15 #include <sys/types.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "list.h"
20 #include "sched.h"
21 #include "ggo.h"
22 #include "recv.h"
23 #include "string.h"
24 #include "net.h"
25 #include "fd.h"
26 #include "buffer_tree.h"
27
28 #include "dccp_recv.cmdline.h"
29
30 /**
31 * data specific to the dccp receiver
32 *
33 * \sa receiver receiver_node
34 */
35 struct private_dccp_recv_data {
36 /** the file descriptor for the dccp socket */
37 int fd;
38 struct btr_pool *btrp;
39 };
40
41 static void dccp_recv_close(struct receiver_node *rn)
42 {
43 struct private_dccp_recv_data *pdd = rn->private_data;
44
45 if (!pdd)
46 return;
47 if (pdd->fd > 0)
48 close(pdd->fd);
49 btr_pool_free(pdd->btrp);
50 free(pdd);
51 rn->private_data = NULL;
52 }
53
54 static int dccp_recv_open(struct receiver_node *rn)
55 {
56 struct private_dccp_recv_data *pdd;
57 struct dccp_recv_args_info *conf = rn->conf;
58 struct flowopts *fo = NULL;
59 uint8_t *ccids = NULL;
60 int fd, ret, i;
61
62 /* Copy CCID preference list (u8 array required) */
63 if (conf->ccid_given) {
64 ccids = para_malloc(conf->ccid_given);
65 fo = flowopt_new();
66
67 for (i = 0; i < conf->ccid_given; i++)
68 ccids[i] = conf->ccid_arg[i];
69
70 OPT_ADD(fo, SOL_DCCP, DCCP_SOCKOPT_CCID, ccids, i);
71 }
72
73 fd = makesock(IPPROTO_DCCP, 0, conf->host_arg, conf->port_arg, fo);
74 free(ccids);
75 if (fd < 0)
76 return fd;
77 /*
78 * Disable unused CCIDs: the receiver does not send any application
79 * data to the server. By shutting down this unused path we reduce
80 * internal processing costs, as the unused CCIDs (in the kernel) are
81 * then bypassed.
82 */
83 if (shutdown(fd, SHUT_WR) < 0) {
84 ret = -ERRNO_TO_PARA_ERROR(errno);
85 goto err;
86 }
87 ret = mark_fd_nonblocking(fd);
88 if (ret < 0)
89 goto err;
90 rn->private_data = pdd = para_calloc(sizeof(struct private_dccp_recv_data));
91 pdd->btrp = btr_pool_new("dccp_recv", 320 * 1024);
92 pdd->fd = fd;
93 return 1;
94 err:
95 close(fd);
96 return ret;
97 }
98
99 /**
100 * Check whether the host supports the requested 'ccid' arguments.
101 * \param conf DCCP receiver arguments.
102 * \return True if all CCIDs requested in \a conf are supported.
103 */
104 static bool dccp_recv_ccid_support_check(struct dccp_recv_args_info *conf)
105 {
106 uint8_t *ccids;
107 int i, j, nccids;
108
109 nccids = dccp_available_ccids(&ccids);
110 if (nccids <= 0)
111 return false;
112
113 for (i = 0; i < conf->ccid_given; i++) {
114 for (j = 0; j < nccids && ccids[j] != conf->ccid_arg[i]; j++)
115 ;
116 if (j == nccids) {
117 PARA_ERROR_LOG("'CCID-%d' not supported on this host.\n",
118 conf->ccid_arg[i]);
119 return false;
120 }
121 }
122 return true;
123 }
124
125 static void *dccp_recv_parse_config(int argc, char **argv)
126 {
127 struct dccp_recv_args_info *tmp = para_calloc(sizeof(*tmp));
128
129 if (!dccp_recv_cmdline_parser(argc, argv, tmp) &&
130 dccp_recv_ccid_support_check(tmp))
131 return tmp;
132 free(tmp);
133 return NULL;
134 }
135
136 static void dccp_recv_pre_select(struct sched *s, struct task *t)
137 {
138 struct receiver_node *rn = container_of(t, struct receiver_node, task);
139 struct private_dccp_recv_data *pdd = rn->private_data;
140
141 t->error = 0;
142 if (generic_recv_pre_select(s, t) <= 0)
143 return;
144 para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
145 }
146
147 static void dccp_recv_post_select(struct sched *s, struct task *t)
148 {
149 struct receiver_node *rn = container_of(t, struct receiver_node, task);
150 struct private_dccp_recv_data *pdd = rn->private_data;
151 struct btr_node *btrn = rn->btrn;
152 struct iovec iov[2];
153 int ret, iovcnt;
154 size_t num_bytes;
155
156 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
157 if (ret <= 0)
158 goto out;
159 iovcnt = btr_pool_get_buffers(pdd->btrp, iov);
160 ret = -E_DCCP_OVERRUN;
161 if (iovcnt == 0)
162 goto out;
163 ret = readv_nonblock(pdd->fd, iov, iovcnt, &s->rfds, &num_bytes);
164 if (num_bytes == 0)
165 goto out;
166 if (num_bytes <= iov[0].iov_len) /* only the first buffer was filled */
167 btr_add_output_pool(pdd->btrp, num_bytes, btrn);
168 else { /* both buffers contain data */
169 btr_add_output_pool(pdd->btrp, iov[0].iov_len, btrn);
170 btr_add_output_pool(pdd->btrp, num_bytes - iov[0].iov_len, btrn);
171 }
172 out:
173 if (ret >= 0)
174 return;
175 btr_remove_node(rn->btrn);
176 t->error = ret;
177 }
178
179 static void dccp_recv_free_config(void *conf)
180 {
181 dccp_recv_cmdline_parser_free(conf);
182 free(conf);
183 }
184
185 /**
186 * The init function of the dccp receiver.
187 *
188 * \param r Pointer to the receiver struct to initialize.
189 *
190 * Initialize all function pointers of \a r.
191 */
192 void dccp_recv_init(struct receiver *r)
193 {
194 struct dccp_recv_args_info dummy;
195
196 dccp_recv_cmdline_parser_init(&dummy);
197 r->open = dccp_recv_open;
198 r->close = dccp_recv_close;
199 r->pre_select = dccp_recv_pre_select;
200 r->post_select = dccp_recv_post_select;
201 r->parse_config = dccp_recv_parse_config;
202 r->free_config = dccp_recv_free_config;
203 r->help = (struct ggo_help) {
204 .short_help = dccp_recv_args_info_help,
205 .detailed_help = dccp_recv_args_info_detailed_help
206 };
207 dccp_recv_cmdline_parser_free(&dummy);
208 }