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