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