546056a04282b9b87c27b9a4cfb811fd005f761d
[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( 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(fd_set *rfds, __a_unused fd_set *wfds)
63 {
64 struct dccp_client *dc;
65 int ret;
66
67 if (!FD_ISSET(listen_fd, rfds))
68 return;
69 dc = para_calloc(sizeof(struct dccp_client));
70 ret = para_accept(listen_fd, &dc->addr, sizeof(struct sockaddr_in));
71 if (ret < 0) {
72 PARA_ERROR_LOG("%s", PARA_STRERROR(-ret));
73 return;
74 }
75 PARA_NOTICE_LOG("connection from %s\n", inet_ntoa(dc->addr.sin_addr));
76 dc->fd = ret;
77 list_add(&dc->node, &clients);
78 }
79
80 static int dccp_open(void)
81 {
82 struct sockaddr_in servaddr;
83 int ret;
84
85 ret = dccp_get_socket();
86 if (ret < 0)
87 return ret;
88 listen_fd = ret;
89
90 bzero(&servaddr, sizeof(servaddr));
91 servaddr.sin_family = AF_INET;
92 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
93 servaddr.sin_port = htons(conf.dccp_port_arg);
94 ret = bind(listen_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
95 if (ret < 0)
96 return -E_DCCP_BIND;
97 ret = dccp_set_socket(listen_fd);
98 if (ret < 0)
99 return ret;
100 ret = listen(listen_fd, 0);
101 if (ret < 0)
102 return -E_DCCP_LISTEN;
103 PARA_DEBUG_LOG("listening on fd %d\n", listen_fd);
104 return 1;
105 }
106
107 static void dccp_shutdown_client(struct dccp_client *dc)
108 {
109 PARA_DEBUG_LOG("shutting down %s (fd %d)\n", inet_ntoa(dc->addr.sin_addr),
110 dc->fd);
111 close(dc->fd);
112 list_del(&dc->node);
113 free(dc);
114 }
115
116 static int dccp_write(int fd, const char *buf, size_t len)
117 {
118 size_t send, written = 0;
119 int ret;
120 again:
121 send = PARA_MIN(1024, len - written);
122 ret = write(fd, buf + written, send);
123 if (ret < 0)
124 goto err_out;
125 written += ret;
126 if (written >= len)
127 return written;
128 ret = write_ok(fd);
129 if (ret > 0)
130 goto again;
131 err_out:
132 return -E_DCCP_WRITE;
133 }
134
135 static void dccp_send(long unsigned current_chunk,
136 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
137 {
138 struct dccp_client *dc, *tmp;
139 int ret, header_len;
140 char *header_buf;
141
142 if (listen_fd < 0 || !len)
143 return;
144
145 list_for_each_entry_safe(dc, tmp, &clients, node) {
146 ret = write_ok(dc->fd);
147 if (ret < 0) {
148 dccp_shutdown_client(dc);
149 continue;
150 }
151 if (!ret)
152 continue;
153 if (!dc->header_sent && current_chunk) {
154 header_buf = afs_get_header(&header_len);
155 if (!header_buf || header_len <= 0)
156 continue; /* header not yet available */
157 ret = dccp_write(dc->fd, header_buf, header_len);
158 if (ret != header_len) {
159 int err = errno;
160 PARA_ERROR_LOG("header write: %d/%d (%s)\n",
161 ret, header_len, ret < 0?
162 strerror(err) : "");
163 dccp_shutdown_client(dc);
164 continue;
165 }
166 dc->header_sent = 1;
167 ret = write_ok(dc->fd);
168 if (ret < 0) {
169 dccp_shutdown_client(dc);
170 continue;
171 }
172 if (!ret)
173 continue;
174 }
175 // PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
176 ret = dccp_write(dc->fd, buf, len);
177 if (ret != len)
178 dccp_shutdown_client(dc);
179 }
180 }
181
182 static void dccp_shutdown_clients(void)
183 {
184 struct dccp_client *dc, *tmp;
185
186 list_for_each_entry_safe(dc, tmp, &clients, node)
187 dccp_shutdown_client(dc);
188 }
189
190 static char *dccp_info(void)
191 {
192 static char *buf;
193 int num_clients = 0;
194 struct dccp_client *dc, *tmp;
195
196 free(buf);
197 list_for_each_entry_safe(dc, tmp, &clients, node)
198 num_clients++;
199 buf = make_message("dccp connected clients: %d\n", num_clients);
200 return buf;
201 }
202
203 static char *dccp_help(void)
204 {
205 return make_message("no help available\n");
206 }
207
208 /**
209 * the init function of the dccp sender
210 *
211 * \param s pointer to the dccp sender struct
212 *
213 * It initializes all function pointers of \a s and starts
214 * listening on the given port.
215 */
216 void dccp_send_init(struct sender *s)
217 {
218 int ret;
219
220 INIT_LIST_HEAD(&clients);
221 s->info = dccp_info;
222 s->send = dccp_send;
223 s->pre_select = dccp_pre_select;
224 s->post_select = dccp_post_select;
225 s->shutdown_clients = dccp_shutdown_clients;
226 s->help = dccp_help;
227 s->client_cmds[SENDER_ON] = NULL;
228 s->client_cmds[SENDER_OFF] = NULL;
229 s->client_cmds[SENDER_DENY] = NULL;
230 s->client_cmds[SENDER_ALLOW] = NULL;
231 s->client_cmds[SENDER_ADD] = NULL;
232 s->client_cmds[SENDER_DELETE] = NULL;
233 self = s;
234 ret = dccp_open();
235 if (ret < 0) {
236 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
237 s->status = SENDER_OFF;
238 } else
239 s->status = SENDER_ON;
240 }