command_util: Generate properly documented command header files.
[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 <sys/types.h>
15 #include <dirent.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
27 #include "dccp_recv.cmdline.h"
28
29 /** the size of the output buffer */
30 #define DCCP_BUFSIZE 40960
31
32 /**
33 * data specific to the dccp receiver
34 *
35 * \sa receiver receiver_node
36 */
37 struct private_dccp_recv_data {
38 /** the file descriptor for the dccp socket */
39 int fd;
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 free(rn->buf);
51 rn->buf = NULL;
52 free(rn->private_data);
53 rn->private_data = NULL;
54 }
55
56
57 static int dccp_recv_open(struct receiver_node *rn)
58 {
59 struct private_dccp_recv_data *pdd;
60 struct dccp_recv_args_info *conf = rn->conf;
61 int fd, ret = makesock(AF_UNSPEC, IPPROTO_DCCP, 0, conf->host_arg,
62 conf->port_arg);
63
64 if (ret < 0)
65 return ret;
66 fd = ret;
67 /*
68 * Disable unused CCIDs: the receiver does not send any application
69 * data to the server. By shutting down this unused path we reduce
70 * internal processing costs, as the unused CCIDs (in the kernel) are
71 * then bypassed.
72 */
73 if (shutdown(fd, SHUT_WR) < 0) {
74 ret = -ERRNO_TO_PARA_ERROR(errno);
75 goto err;
76 }
77 ret = mark_fd_nonblocking(fd);
78 if (ret < 0)
79 goto err;
80 rn->buf = para_calloc(DCCP_BUFSIZE);
81 rn->private_data = pdd = para_calloc(sizeof(struct private_dccp_recv_data));
82 pdd->fd = fd;
83 return 1;
84 err:
85 close(fd);
86 return ret;
87 }
88
89 static void dccp_shutdown(void)
90 {
91 ; /* nothing to do */
92 }
93
94 static void *dccp_recv_parse_config(int argc, char **argv)
95 {
96 struct dccp_recv_args_info *tmp = para_calloc(sizeof(struct dccp_recv_args_info));
97
98 if (!dccp_recv_cmdline_parser(argc, argv, tmp))
99 return tmp;
100 free(tmp);
101 return NULL;
102 }
103
104 static void dccp_recv_pre_select(struct sched *s, struct task *t)
105 {
106 struct receiver_node *rn = container_of(t, struct receiver_node, task);
107 struct private_dccp_recv_data *pdd = rn->private_data;
108
109 t->error = 0;
110 para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
111 }
112
113 static void dccp_recv_post_select(struct sched *s, struct task *t)
114 {
115 struct receiver_node *rn = container_of(t, struct receiver_node, task);
116 struct private_dccp_recv_data *pdd = rn->private_data;
117
118 if (rn->output_error && *rn->output_error < 0) {
119 t->error = *rn->output_error;
120 return;
121 }
122 if (!FD_ISSET(pdd->fd, &s->rfds))
123 return; /* nothing to do */
124 if (rn->loaded >= DCCP_BUFSIZE) {
125 t->error = -E_DCCP_OVERRUN;
126 return;
127 }
128 t->error = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
129 DCCP_BUFSIZE - rn->loaded);
130 if (t->error > 0) {
131 rn->loaded += t->error;
132 return;
133 }
134 if (!t->error)
135 t->error = -E_RECV_EOF;
136 }
137
138 /**
139 * The init function of the dccp receiver.
140 *
141 * \param r Pointer to the receiver struct to initialize.
142 *
143 * Initialize all function pointers of \a r.
144 */
145 void dccp_recv_init(struct receiver *r)
146 {
147 struct dccp_recv_args_info dummy;
148
149 dccp_recv_cmdline_parser_init(&dummy);
150 r->shutdown = dccp_shutdown;
151 r->open = dccp_recv_open;
152 r->close = dccp_recv_close;
153 r->pre_select = dccp_recv_pre_select;
154 r->post_select = dccp_recv_post_select;
155 r->parse_config = dccp_recv_parse_config;
156 r->help = (struct ggo_help) {
157 .short_help = dccp_recv_args_info_help,
158 .detailed_help = dccp_recv_args_info_detailed_help
159 };
160 }