6c55c4b19e34fe5d8353c98b376181e392604f94
[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 #include "para.h"
20 #include "error.h"
21 #include "dccp.h"
22 #include "recv.h"
23 #include "string.h"
24 #include "net.h"
25
26 #include "dccp_recv.cmdline.h"
27
28 /* needed by getaddrinfo */
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netdb.h>
32
33
34 #define DCCP_BUFSIZE 4096
35
36 /**
37  * data specific to the dccp receiver
38  *
39  * \sa receiver receiver_node
40  */
41 struct private_dccp_recv_data {
42         /** the file descriptor for the dccp socket */
43         int fd;
44 };
45
46
47 static void dccp_recv_close(struct receiver_node *rn)
48 {
49
50         struct private_dccp_recv_data *pdd = rn->private_data;
51
52         if (pdd && pdd->fd > 0)
53                 close(pdd->fd);
54         free(rn->buf);
55         rn->buf = NULL;
56         free(rn->private_data);
57         rn->private_data = NULL;
58 }
59
60
61 static int dccp_recv_open(struct receiver_node *rn)
62 {
63         struct private_dccp_recv_data *pdd;
64         struct gengetopt_args_info *conf = rn->conf;
65         int ret;
66         struct addrinfo *ai;
67         char *tmp;
68
69         rn->buf = para_calloc(DCCP_BUFSIZE);
70         rn->private_data = para_calloc(sizeof(struct private_dccp_recv_data));
71         pdd = rn->private_data;
72         ret = dccp_get_socket();
73         if (ret < 0)
74                 goto err_out;
75         pdd->fd = ret;
76
77         tmp = make_message("%d", conf->port_arg);
78         ret = getaddrinfo(conf->host_arg, tmp, NULL, &ai);
79         free(tmp);
80         if (ret) {
81                 ret = -E_ADDR_INFO;
82                 goto err_out;
83         }
84         ret = dccp_set_socket(pdd->fd);
85         if (ret < 0)
86                 goto err_out;
87         PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg, conf->port_arg);
88         ret = -E_DCCP_CONNECT;
89         if (connect(pdd->fd, ai->ai_addr, ai->ai_addrlen) < 0)
90                 goto err_out;
91         return 1;
92 err_out:
93         dccp_recv_close(rn);
94         return ret;
95 }
96
97 static void dccp_shutdown(void)
98 {
99         ; /* nothing to do */
100 }
101
102 static void *dccp_recv_parse_config(int argc, char **argv)
103 {
104         struct gengetopt_args_info *tmp = para_calloc(sizeof(struct gengetopt_args_info));
105
106         if (!dccp_recv_cmdline_parser(argc, argv, tmp))
107                 return tmp;
108         free(tmp);
109         return NULL;
110 }
111
112 static int dccp_recv_pre_select(struct receiver_node *rn, fd_set *rfds,
113                 __unused fd_set *wfds, __unused struct timeval *timeout)
114 {
115         struct private_dccp_recv_data *pdd = rn->private_data;
116
117         if (pdd)
118                 FD_SET(pdd->fd, rfds);
119         return pdd->fd;
120 }
121
122 static int dccp_recv_post_select(struct receiver_node *rn, int select_ret,
123                 fd_set *rfds, __unused fd_set *wfds)
124 {
125         int ret;
126         struct private_dccp_recv_data *pdd = rn->private_data;
127
128         if (!select_ret || !pdd || !FD_ISSET(pdd->fd, rfds))
129                 return 1; /* nothing to do */
130         if (rn->loaded >= DCCP_BUFSIZE)
131                 return -E_DCCP_OVERRUN;
132         ret = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
133                 DCCP_BUFSIZE - rn->loaded);
134         if (ret <= 0)
135                 return ret;
136         rn->loaded += ret;
137         return 1;
138 }
139
140 /**
141  * the init function of the dccp receiver
142  *
143  * \param r pointer to the receiver struct to initialize
144  *
145  * Initialize all function pointers of \a r
146  */
147 void dccp_recv_init(struct receiver *r)
148 {
149         r->shutdown = dccp_shutdown;
150         r->open = dccp_recv_open;
151         r->close = dccp_recv_close;
152         r->pre_select = dccp_recv_pre_select;
153         r->post_select = dccp_recv_post_select;
154         r->parse_config = dccp_recv_parse_config;
155 }