Rename MIN, MAX, ABS macros
[paraslash.git] / dccp_send.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_send.c paraslash's dccp sender */
20
21 /*
22  * based on server.c of dccp-cs-0.01.tar.bz2,
23  * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
24  */
25
26 #include "server.h"
27 #include "net.h"
28 #include "list.h"
29 #include "afs.h"
30 #include "send.h"
31 #include "dccp.h"
32 #include "error.h"
33 #include "string.h"
34 #include "server.cmdline.h"
35 extern struct gengetopt_args_info conf;
36 /** the list of connected clients **/
37 static struct list_head clients;
38 static int listen_fd = -1;
39 static struct sender *self;
40
41 /** describes one connected client */
42 struct dccp_client {
43         /** the dccp socket */
44         int fd;
45         /** address information about the client */
46         struct sockaddr_in addr;
47         /** the position of this client in the client list */
48         struct list_head node;
49         /** non-zero if audio file header has been sent */
50         int header_sent;
51 };
52
53 static void dccp_pre_select(__a_unused struct audio_format *af, int *max_fileno, fd_set *rfds,
54                 __a_unused fd_set *wfds)
55 {
56         if (listen_fd < 0)
57                 return;
58         FD_SET(listen_fd, rfds);
59         *max_fileno = PARA_MAX(*max_fileno, listen_fd);
60 }
61
62 static void dccp_post_select(__a_unused struct audio_format *af, fd_set *rfds,
63                 __a_unused fd_set *wfds)
64 {
65         struct dccp_client *dc;
66         int ret;
67
68         if (!FD_ISSET(listen_fd, rfds))
69                 return;
70         dc = para_calloc(sizeof(struct dccp_client));
71         ret = para_accept(listen_fd, &dc->addr, sizeof(struct sockaddr_in));
72         if (ret < 0) {
73                 PARA_ERROR_LOG("%s", PARA_STRERROR(-ret));
74                 return;
75         }
76         PARA_NOTICE_LOG("connection from %s\n", inet_ntoa(dc->addr.sin_addr));
77         dc->fd = ret;
78         list_add(&dc->node, &clients);
79 }
80
81 static int dccp_open(void)
82 {
83         struct sockaddr_in servaddr;
84         int ret;
85
86         ret = dccp_get_socket();
87         if (ret < 0)
88                 return ret;
89         listen_fd = ret;
90
91         bzero(&servaddr, sizeof(servaddr));
92         servaddr.sin_family = AF_INET;
93         servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
94         servaddr.sin_port = htons(conf.dccp_port_arg);
95         ret = bind(listen_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
96         if (ret < 0)
97                 return -E_DCCP_BIND;
98         ret = dccp_set_socket(listen_fd);
99         if (ret < 0)
100                 return ret;
101         ret = listen(listen_fd, 0);
102         if (ret < 0)
103                 return -E_DCCP_LISTEN;
104         PARA_DEBUG_LOG("listening on fd %d\n", listen_fd);
105         return 1;
106 }
107
108 static void dccp_shutdown_client(struct dccp_client *dc)
109 {
110         PARA_DEBUG_LOG("shutting down %s (fd %d)\n", inet_ntoa(dc->addr.sin_addr),
111                 dc->fd);
112         close(dc->fd);
113         list_del(&dc->node);
114         free(dc);
115 }
116
117 static int dccp_write(int fd, const char *buf, size_t len)
118 {
119         size_t send, written = 0;
120         int ret;
121 again:
122         send = PARA_MIN(1024, len - written);
123         ret = write(fd, buf + written, send);
124         if (ret < 0)
125                 goto err_out;
126         written += ret;
127         if (written >= len)
128                 return written;
129         ret = write_ok(fd);
130         if (ret > 0)
131                 goto again;
132 err_out:
133         return -E_DCCP_WRITE;
134 }
135
136 static void dccp_send(__a_unused struct audio_format *af,
137                 long unsigned current_chunk,
138                 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
139 {
140         struct dccp_client *dc, *tmp;
141         int ret, header_len;
142         char *header_buf;
143
144         if (listen_fd < 0 || !len)
145                 return;
146
147         list_for_each_entry_safe(dc, tmp, &clients, node) {
148                 ret = write_ok(dc->fd);
149                 if (ret < 0) {
150                         dccp_shutdown_client(dc);
151                         continue;
152                 }
153                 if (!ret)
154                         continue;
155                 if (!dc->header_sent && af->get_header_info && current_chunk) {
156                         header_buf = af->get_header_info(&header_len);
157                         if (!header_buf || header_len <= 0)
158                                 continue; /* header not yet available */
159                         ret = dccp_write(dc->fd, header_buf, header_len);
160                         if (ret != header_len) {
161                                 int err = errno;
162                                 PARA_ERROR_LOG("header write: %d/%d (%s)\n",
163                                         ret, header_len, ret < 0?
164                                         strerror(err) : "");
165                                 dccp_shutdown_client(dc);
166                                 continue;
167                         }
168                         dc->header_sent = 1;
169                         ret = write_ok(dc->fd);
170                         if (ret < 0) {
171                                 dccp_shutdown_client(dc);
172                                 continue;
173                         }
174                         if (!ret)
175                                 continue;
176                 }
177 //              PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
178                 ret = dccp_write(dc->fd, buf, len);
179                 if (ret != len)
180                         dccp_shutdown_client(dc);
181         }
182 }
183
184 static void dccp_shutdown_clients(void)
185 {
186         struct dccp_client *dc, *tmp;
187
188         list_for_each_entry_safe(dc, tmp, &clients, node)
189                 dccp_shutdown_client(dc);
190 }
191
192 static char *dccp_info(void)
193 {
194         static char *buf;
195         int num_clients = 0;
196         struct dccp_client *dc, *tmp;
197
198         free(buf);
199         list_for_each_entry_safe(dc, tmp, &clients, node)
200                 num_clients++;
201         buf = make_message("dccp connected clients: %d\n", num_clients);
202         return buf;
203 }
204
205 static char *dccp_help(void)
206 {
207         return make_message("no help available\n");
208 }
209
210 /**
211  * the init function of the dccp sender
212  *
213  * \param s pointer to the dccp sender struct
214  *
215  * It initializes all function pointers of \a s and starts
216  * listening on the given port.
217  */
218 void dccp_send_init(struct sender *s)
219 {
220         int ret;
221
222         INIT_LIST_HEAD(&clients);
223         s->info = dccp_info;
224         s->send = dccp_send;
225         s->pre_select = dccp_pre_select;
226         s->post_select = dccp_post_select;
227         s->shutdown_clients = dccp_shutdown_clients;
228         s->help = dccp_help;
229         s->client_cmds[SENDER_ON] = NULL;
230         s->client_cmds[SENDER_OFF] = NULL;
231         s->client_cmds[SENDER_DENY] = NULL;
232         s->client_cmds[SENDER_ALLOW] = NULL;
233         s->client_cmds[SENDER_ADD] = NULL;
234         s->client_cmds[SENDER_DELETE] = NULL;
235         self = s;
236         ret = dccp_open();
237         if (ret < 0)
238                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
239         else
240                 s->status = SENDER_ON;
241 }