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