Rename mark_fd_nonblock() to mark_fd_nonblocking().
[paraslash.git] / dccp_recv.c
1 /*
2 * Copyright (C) 2006-2007 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 "dccp.h"
20 #include "list.h"
21 #include "sched.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 /* needed by getaddrinfo */
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netdb.h>
33
34 /** the size of the output buffer */
35 #define DCCP_BUFSIZE 40960
36
37 /**
38 * data specific to the dccp receiver
39 *
40 * \sa receiver receiver_node
41 */
42 struct private_dccp_recv_data {
43 /** the file descriptor for the dccp socket */
44 int fd;
45 };
46
47
48 static void dccp_recv_close(struct receiver_node *rn)
49 {
50
51 struct private_dccp_recv_data *pdd = rn->private_data;
52
53 if (pdd && pdd->fd > 0)
54 close(pdd->fd);
55 free(rn->buf);
56 rn->buf = NULL;
57 free(rn->private_data);
58 rn->private_data = NULL;
59 }
60
61
62 static int dccp_recv_open(struct receiver_node *rn)
63 {
64 struct private_dccp_recv_data *pdd;
65 struct dccp_recv_args_info *conf = rn->conf;
66 int ret;
67 struct addrinfo *ai;
68 char *tmp;
69
70 rn->buf = para_calloc(DCCP_BUFSIZE);
71 rn->private_data = para_calloc(sizeof(struct private_dccp_recv_data));
72 pdd = rn->private_data;
73 ret = dccp_get_socket();
74 if (ret < 0)
75 goto err_out;
76 pdd->fd = ret;
77
78 tmp = make_message("%d", conf->port_arg);
79 ret = getaddrinfo(conf->host_arg, tmp, NULL, &ai);
80 free(tmp);
81 if (ret) {
82 ret = -E_ADDR_INFO;
83 goto err_out;
84 }
85 ret = dccp_set_socket(pdd->fd);
86 if (ret < 0)
87 goto err_out;
88 PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg, conf->port_arg);
89 ret = PARA_CONNECT(pdd->fd, ai->ai_addr);
90 freeaddrinfo(ai);
91 if (ret < 0) {
92 ret = -E_DCCP_CONNECT;
93 goto err_out;
94 }
95 mark_fd_nonblocking(pdd->fd);
96 return 1;
97 err_out:
98 dccp_recv_close(rn);
99 return ret;
100 }
101
102 static void dccp_shutdown(void)
103 {
104 ; /* nothing to do */
105 }
106
107 static void *dccp_recv_parse_config(int argc, char **argv)
108 {
109 struct dccp_recv_args_info *tmp = para_calloc(sizeof(struct dccp_recv_args_info));
110
111 if (!dccp_recv_cmdline_parser(argc, argv, tmp))
112 return tmp;
113 free(tmp);
114 return NULL;
115 }
116
117 static void dccp_recv_pre_select(struct sched *s, struct task *t)
118 {
119 struct receiver_node *rn = t->private_data;
120 struct private_dccp_recv_data *pdd = rn->private_data;
121
122 t->ret = 1;
123 para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
124 }
125
126 static void dccp_recv_post_select(struct sched *s, struct task *t)
127 {
128 struct receiver_node *rn = t->private_data;
129 struct private_dccp_recv_data *pdd = rn->private_data;
130
131 t->ret = -E_DCCP_RECV_EOF;
132 if (rn->output_eof && *rn->output_eof)
133 goto out;
134 t->ret = 1;
135 if (!s->select_ret || !FD_ISSET(pdd->fd, &s->rfds))
136 goto out; /* nothing to do */
137 t->ret = -E_DCCP_OVERRUN;
138 if (rn->loaded >= DCCP_BUFSIZE)
139 goto out;
140 t->ret = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
141 DCCP_BUFSIZE - rn->loaded);
142 if (t->ret <= 0) {
143 if (!t->ret)
144 t->ret = -E_DCCP_RECV_EOF;
145 goto out;
146 }
147 rn->loaded += t->ret;
148 return;
149 out:
150 if (t->ret < 0)
151 rn->eof = 1;
152 }
153
154 /**
155 * the init function of the dccp receiver
156 *
157 * \param r pointer to the receiver struct to initialize
158 *
159 * Initialize all function pointers of \a r
160 */
161 void dccp_recv_init(struct receiver *r)
162 {
163 r->shutdown = dccp_shutdown;
164 r->open = dccp_recv_open;
165 r->close = dccp_recv_close;
166 r->pre_select = dccp_recv_pre_select;
167 r->post_select = dccp_recv_post_select;
168 r->parse_config = dccp_recv_parse_config;
169 }