08_refactor-makesock.diff
[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 = para_connect_simple(IPPROTO_DCCP, conf->host_arg,
61 conf->port_arg);
62 if (ret < 0)
63 return ret;
64 fd = ret;
65 /*
66 * Disable unused CCIDs: the receiver does not send any application
67 * data to the server. By shutting down this unused path we reduce
68 * internal processing costs, as the unused CCIDs (in the kernel) are
69 * then bypassed.
70 */
71 if (shutdown(fd, SHUT_WR) < 0) {
72 ret = -ERRNO_TO_PARA_ERROR(errno);
73 goto err;
74 }
75 ret = mark_fd_nonblocking(fd);
76 if (ret < 0)
77 goto err;
78 rn->private_data = pdd = para_calloc(sizeof(struct private_dccp_recv_data));
79 pdd->btrp = btr_pool_new("dccp_recv", 320 * 1024);
80 pdd->fd = fd;
81 return 1;
82 err:
83 close(fd);
84 return ret;
85 }
86
87 static void *dccp_recv_parse_config(int argc, char **argv)
88 {
89 struct dccp_recv_args_info *tmp = para_calloc(sizeof(struct dccp_recv_args_info));
90
91 if (!dccp_recv_cmdline_parser(argc, argv, tmp))
92 return tmp;
93 free(tmp);
94 return NULL;
95 }
96
97 static void dccp_recv_pre_select(struct sched *s, struct task *t)
98 {
99 struct receiver_node *rn = container_of(t, struct receiver_node, task);
100 struct private_dccp_recv_data *pdd = rn->private_data;
101
102 t->error = 0;
103 if (generic_recv_pre_select(s, t) <= 0)
104 return;
105 para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
106 }
107
108 static void dccp_recv_post_select(struct sched *s, struct task *t)
109 {
110 struct receiver_node *rn = container_of(t, struct receiver_node, task);
111 struct private_dccp_recv_data *pdd = rn->private_data;
112 struct btr_node *btrn = rn->btrn;
113 struct iovec iov[2];
114 int ret, iovcnt;
115
116 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
117 if (ret < 0)
118 goto err;
119 if (ret == 0)
120 return;
121 if (!FD_ISSET(pdd->fd, &s->rfds))
122 return; /* nothing to do */
123 iovcnt = btr_pool_get_buffers(pdd->btrp, iov);
124 ret = -E_DCCP_OVERRUN;
125 if (iovcnt == 0)
126 goto err;
127 ret = para_readv(pdd->fd, iov, iovcnt);
128 if (ret == 0)
129 ret = -E_RECV_EOF;
130 if (ret < 0)
131 goto err;
132 if (ret <= iov[0].iov_len) /* only the first buffer was filled */
133 btr_add_output_pool(pdd->btrp, ret, btrn);
134 else { /* both buffers contain data */
135 btr_add_output_pool(pdd->btrp, iov[0].iov_len, btrn);
136 btr_add_output_pool(pdd->btrp, ret - iov[0].iov_len, btrn);
137 }
138 return;
139 err:
140 btr_remove_node(rn->btrn);
141 t->error = ret;
142 }
143
144 static void dccp_recv_free_config(void *conf)
145 {
146 dccp_recv_cmdline_parser_free(conf);
147 free(conf);
148 }
149
150 /**
151 * The init function of the dccp receiver.
152 *
153 * \param r Pointer to the receiver struct to initialize.
154 *
155 * Initialize all function pointers of \a r.
156 */
157 void dccp_recv_init(struct receiver *r)
158 {
159 struct dccp_recv_args_info dummy;
160
161 dccp_recv_cmdline_parser_init(&dummy);
162 r->open = dccp_recv_open;
163 r->close = dccp_recv_close;
164 r->pre_select = dccp_recv_pre_select;
165 r->post_select = dccp_recv_post_select;
166 r->parse_config = dccp_recv_parse_config;
167 r->free_config = dccp_recv_free_config;
168 r->help = (struct ggo_help) {
169 .short_help = dccp_recv_args_info_help,
170 .detailed_help = dccp_recv_args_info_detailed_help
171 };
172 dccp_recv_cmdline_parser_free(&dummy);
173 }