2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
20 * based on server.c of dccp-cs-0.01.tar.bz2,
21 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
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;
39 /** describes one connected client */
41 /** the dccp socket */
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 */
50 static void dccp_pre_select(__unused struct audio_format *af, int *max_fileno, fd_set *rfds,
51 __unused fd_set *wfds)
55 FD_SET(listen_fd, rfds);
56 *max_fileno = MAX(*max_fileno, listen_fd);
59 static void dccp_post_select(__unused struct audio_format *af, fd_set *rfds,
60 __unused fd_set *wfds)
62 struct dccp_client *dc;
65 if (!FD_ISSET(listen_fd, rfds))
67 dc = para_calloc(sizeof(struct dccp_client));
68 ret = para_accept(listen_fd, &dc->addr, sizeof(struct sockaddr_in));
70 PARA_ERROR_LOG("%s", PARA_STRERROR(-ret));
73 PARA_NOTICE_LOG("connection from %s\n", inet_ntoa(dc->addr.sin_addr));
75 list_add(&dc->node, &clients);
78 static int dccp_open(void)
80 struct sockaddr_in servaddr;
83 ret = dccp_get_socket();
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));
95 ret = dccp_set_socket(listen_fd);
98 ret = listen(listen_fd, 0);
100 return -E_DCCP_LISTEN;
101 PARA_DEBUG_LOG("listening on fd %d\n", listen_fd);
105 static void dccp_shutdown_client(struct dccp_client *dc)
107 PARA_DEBUG_LOG("shutting down %s (fd %d)\n", inet_ntoa(dc->addr.sin_addr),
114 static int dccp_write(int fd, const char *buf, size_t len)
116 size_t send, written = 0;
119 send = MIN(1024, len - written);
120 ret = write(fd, buf + written, send);
130 return -E_DCCP_WRITE;
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)
137 struct dccp_client *dc, *tmp;
141 if (listen_fd < 0 || !len)
144 list_for_each_entry_safe(dc, tmp, &clients, node) {
145 ret = write_ok(dc->fd);
147 dccp_shutdown_client(dc);
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) {
159 PARA_ERROR_LOG("header write: %d/%d (%s)\n",
160 ret, header_len, ret < 0?
162 dccp_shutdown_client(dc);
166 ret = write_ok(dc->fd);
168 dccp_shutdown_client(dc);
174 // PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
175 ret = dccp_write(dc->fd, buf, len);
177 dccp_shutdown_client(dc);
181 static void dccp_shutdown_clients(void)
183 struct dccp_client *dc, *tmp;
185 list_for_each_entry_safe(dc, tmp, &clients, node)
186 dccp_shutdown_client(dc);
189 static char *dccp_info(void)
193 struct dccp_client *dc, *tmp;
196 list_for_each_entry_safe(dc, tmp, &clients, node)
198 buf = make_message("dccp connected clients: %d\n", num_clients);
202 static char *dccp_help(void)
204 return make_message("no help available\n");
207 void dccp_send_init(struct sender *s)
211 INIT_LIST_HEAD(&clients);
214 s->pre_select = dccp_pre_select;
215 s->post_select = dccp_post_select;
216 s->shutdown_clients = dccp_shutdown_clients;
218 s->client_cmds[SENDER_ON] = NULL;
219 s->client_cmds[SENDER_OFF] = NULL;
220 s->client_cmds[SENDER_DENY] = NULL;
221 s->client_cmds[SENDER_ALLOW] = NULL;
222 s->client_cmds[SENDER_ADD] = NULL;
223 s->client_cmds[SENDER_DELETE] = NULL;
227 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
229 s->status = SENDER_ON;