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