configure: Check for broken snprintf().
[paraslash.git] / dccp_recv.c
1 /*
2 * Copyright (C) 2006-2009 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 #include <dirent.h>
17
18 #include "para.h"
19 #include "error.h"
20 #include "list.h"
21 #include "sched.h"
22 #include "ggo.h"
23 #include "recv.h"
24 #include "string.h"
25 #include "net.h"
26 #include "fd.h"
27 #include "buffer_tree.h"
28
29 #include "dccp_recv.cmdline.h"
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 struct btr_pool *btrp;
40 };
41
42
43 static void dccp_recv_close(struct receiver_node *rn)
44 {
45
46 struct private_dccp_recv_data *pdd = rn->private_data;
47
48 if (pdd && pdd->fd > 0)
49 close(pdd->fd);
50 btr_pool_free(pdd->btrp);
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->private_data = pdd = para_calloc(sizeof(struct private_dccp_recv_data));
80 pdd->btrp = btr_pool_new("dccp_recv", 320 * 1024);
81 pdd->fd = fd;
82 return 1;
83 err:
84 close(fd);
85 return ret;
86 }
87
88 static void *dccp_recv_parse_config(int argc, char **argv)
89 {
90 struct dccp_recv_args_info *tmp = para_calloc(sizeof(struct dccp_recv_args_info));
91
92 if (!dccp_recv_cmdline_parser(argc, argv, tmp))
93 return tmp;
94 free(tmp);
95 return NULL;
96 }
97
98 static void dccp_recv_pre_select(struct sched *s, struct task *t)
99 {
100 struct receiver_node *rn = container_of(t, struct receiver_node, task);
101 struct private_dccp_recv_data *pdd = rn->private_data;
102
103 t->error = 0;
104 if (generic_recv_pre_select(s, t) <= 0)
105 return;
106 para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
107 }
108
109 static void dccp_recv_post_select(struct sched *s, struct task *t)
110 {
111 struct receiver_node *rn = container_of(t, struct receiver_node, task);
112 struct private_dccp_recv_data *pdd = rn->private_data;
113 struct btr_node *btrn = rn->btrn;
114 int ret;
115 char *buf;
116 size_t sz;
117
118 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
119 if (ret < 0)
120 goto err;
121 if (ret == 0)
122 return;
123 if (!FD_ISSET(pdd->fd, &s->rfds))
124 return; /* nothing to do */
125 ret = -E_DCCP_OVERRUN;
126 sz = btr_pool_get_buffer(pdd->btrp, &buf);
127 if (sz == 0)
128 goto err;
129 ret = recv_bin_buffer(pdd->fd, buf, sz);
130 if (ret == 0)
131 ret = -E_RECV_EOF;
132 if (ret < 0)
133 goto err;
134 btr_add_output_pool(pdd->btrp, ret, btrn);
135 return;
136 err:
137 btr_remove_node(rn->btrn);
138 t->error = ret;
139 }
140
141 static void dccp_recv_free_config(void *conf)
142 {
143 dccp_recv_cmdline_parser_free(conf);
144 free(conf);
145 }
146
147 /**
148 * The init function of the dccp receiver.
149 *
150 * \param r Pointer to the receiver struct to initialize.
151 *
152 * Initialize all function pointers of \a r.
153 */
154 void dccp_recv_init(struct receiver *r)
155 {
156 struct dccp_recv_args_info dummy;
157
158 dccp_recv_cmdline_parser_init(&dummy);
159 r->open = dccp_recv_open;
160 r->close = dccp_recv_close;
161 r->pre_select = dccp_recv_pre_select;
162 r->post_select = dccp_recv_post_select;
163 r->parse_config = dccp_recv_parse_config;
164 r->free_config = dccp_recv_free_config;
165 r->help = (struct ggo_help) {
166 .short_help = dccp_recv_args_info_help,
167 .detailed_help = dccp_recv_args_info_detailed_help
168 };
169 dccp_recv_cmdline_parser_free(&dummy);
170 }