168ddf4f18ba3e91ebdd290eeddbdb51b08930d2
[paraslash.git] / dccp_recv.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /*
20  * based on client.c of dccp-cs-0.01.tar.bz2,
21  * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
22  */
23
24 #include "para.h"
25 #include "error.h"
26 #include "dccp.h"
27 #include "recv.h"
28 #include "string.h"
29 #include "net.h"
30
31 #include "dccp_recv.cmdline.h"
32
33 /* needed by getaddrinfo */
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netdb.h>
37
38
39 #define DCCP_BUFSIZE 4096
40
41 /**
42  * data specific to the dccp receiver
43  *
44  * \sa receiver receiver_node
45  */
46 struct private_dccp_recv_data {
47         /** the file descriptor for the dccp socket */
48         int fd;
49 };
50
51
52 static void dccp_recv_close(struct receiver_node *rn)
53 {
54
55         struct private_dccp_recv_data *pdd = rn->private_data;
56
57         if (pdd && pdd->fd > 0)
58                 close(pdd->fd);
59         free(rn->buf);
60         rn->buf = NULL;
61         free(rn->private_data);
62         rn->private_data = NULL;
63 }
64
65
66 static int dccp_recv_open(struct receiver_node *rn)
67 {
68         struct private_dccp_recv_data *pdd;
69         struct gengetopt_args_info *conf = rn->conf;
70         int ret;
71         struct addrinfo *ai;
72         char *tmp;
73
74         rn->buf = para_calloc(DCCP_BUFSIZE);
75         rn->private_data = para_calloc(sizeof(struct private_dccp_recv_data));
76         pdd = rn->private_data;
77         ret = dccp_get_socket();
78         if (ret < 0)
79                 goto err_out;
80         pdd->fd = ret;
81
82         tmp = make_message("%d", conf->port_arg);
83         ret = getaddrinfo(conf->host_arg, tmp, NULL, &ai);
84         free(tmp);
85         if (ret) {
86                 ret = -E_ADDR_INFO;
87                 goto err_out;
88         }
89         ret = dccp_set_socket(pdd->fd);
90         if (ret < 0)
91                 goto err_out;
92         PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg, conf->port_arg);
93         ret = -E_DCCP_CONNECT;
94         if (connect(pdd->fd, ai->ai_addr, ai->ai_addrlen) < 0)
95                 goto err_out;
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 gengetopt_args_info *tmp = para_calloc(sizeof(struct gengetopt_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 int dccp_recv_pre_select(struct receiver_node *rn, fd_set *rfds,
118                 __unused fd_set *wfds, __unused struct timeval *timeout)
119 {
120         struct private_dccp_recv_data *pdd = rn->private_data;
121
122         if (!pdd)
123                 return -1;
124         FD_SET(pdd->fd, rfds);
125         return pdd->fd;
126 }
127
128 static int dccp_recv_post_select(struct receiver_node *rn, int select_ret,
129                 fd_set *rfds, __unused fd_set *wfds)
130 {
131         int ret;
132         struct private_dccp_recv_data *pdd = rn->private_data;
133
134         if (!select_ret || !pdd || !FD_ISSET(pdd->fd, rfds))
135                 return 1; /* nothing to do */
136         if (rn->loaded >= DCCP_BUFSIZE)
137                 return -E_DCCP_OVERRUN;
138         ret = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
139                 DCCP_BUFSIZE - rn->loaded);
140         if (ret <= 0) {
141                 PARA_INFO_LOG("%s\n", ret? PARA_STRERROR(-ret) : "eof");
142                 return ret;
143         }
144         rn->loaded += ret;
145         return 1;
146 }
147
148 /**
149  * the init function of the dccp receiver
150  *
151  * \param r pointer to the receiver struct to initialize
152  *
153  * Initialize all function pointers of \a r
154  */
155 void dccp_recv_init(struct receiver *r)
156 {
157         r->shutdown = dccp_shutdown;
158         r->open = dccp_recv_open;
159         r->close = dccp_recv_close;
160         r->pre_select = dccp_recv_pre_select;
161         r->post_select = dccp_recv_post_select;
162         r->parse_config = dccp_recv_parse_config;
163 }