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.
27 #include "server.cmdline.h"
28 extern struct gengetopt_args_info conf;
29 /** the list of connected clients **/
30 static struct list_head clients;
31 static int listen_fd = -1;
32 static struct sender *self;
34 /** describes one connected client */
36 /** the dccp socket */
38 /** address information about the client */
39 struct sockaddr_in addr;
40 /** the position of this client in the client list */
41 struct list_head node;
42 int header_sent; /* non-zero if audio file header has been sent */
45 static void dccp_pre_select(__unused struct audio_format *af, int *max_fileno, fd_set *rfds,
46 __unused fd_set *wfds)
50 FD_SET(listen_fd, rfds);
51 *max_fileno = MAX(*max_fileno, listen_fd);
54 static void dccp_post_select(__unused struct audio_format *af, fd_set *rfds,
55 __unused fd_set *wfds)
57 struct dccp_client *dc;
60 if (!FD_ISSET(listen_fd, rfds))
62 PARA_NOTICE_LOG("%s", "accepting...\n");
63 dc = para_calloc(sizeof(struct dccp_client));
64 ret = para_accept(listen_fd, &dc->addr, sizeof(struct sockaddr_in));
66 PARA_ERROR_LOG("%s", PARA_STRERROR(-ret));
69 PARA_NOTICE_LOG("%s", "connection\n");
71 list_add(&dc->node, &clients);
74 static int dccp_open(void)
76 struct sockaddr_in servaddr;
79 ret = dccp_get_socket();
84 bzero(&servaddr, sizeof(servaddr));
85 servaddr.sin_family = AF_INET;
86 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
87 servaddr.sin_port = htons(conf.dccp_port_arg);
88 ret = bind(listen_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
91 ret = dccp_set_socket(listen_fd);
94 ret = listen(listen_fd, 0);
96 return -E_DCCP_LISTEN;
97 PARA_DEBUG_LOG("listening on fd %d\n", listen_fd);
101 static void dccp_shutdown_client(struct dccp_client *dc)
108 static void dccp_send(__unused struct audio_format *af,
109 long unsigned current_chunk,
110 __unused long unsigned chunks_sent, const char *buf, size_t len)
112 struct dccp_client *dc, *tmp;
116 if (listen_fd < 0 || !len)
119 list_for_each_entry_safe(dc, tmp, &clients, node) {
120 if (!_write_ok(dc->fd))
122 if (!dc->header_sent && af->get_header_info && current_chunk) {
123 header_buf = af->get_header_info(&header_len);
124 if (!header_buf || header_len <= 0)
125 continue; /* header not yet available */
126 ret = write(dc->fd, header_buf, header_len);
127 if (ret != header_len) {
128 dccp_shutdown_client(dc);
131 if (!_write_ok(dc->fd))
134 PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
135 ret = write(dc->fd, buf, len);
137 dccp_shutdown_client(dc);
141 static void dccp_shutdown_clients(void)
143 struct dccp_client *dc, *tmp;
145 list_for_each_entry_safe(dc, tmp, &clients, node)
146 dccp_shutdown_client(dc);
149 static char *dccp_info(void)
153 struct dccp_client *dc, *tmp;
156 list_for_each_entry_safe(dc, tmp, &clients, node)
158 buf = make_message("%d connected clients\n", num_clients);
162 static char *dccp_help(void)
164 return make_message("no help available\n");
167 void dccp_send_init(struct sender *s)
171 INIT_LIST_HEAD(&clients);
174 s->pre_select = dccp_pre_select;
175 s->post_select = dccp_post_select;
176 s->shutdown_clients = dccp_shutdown_clients;
178 s->client_cmds[SENDER_ON] = NULL;
179 s->client_cmds[SENDER_OFF] = NULL;
180 s->client_cmds[SENDER_DENY] = NULL;
181 s->client_cmds[SENDER_ALLOW] = NULL;
182 s->client_cmds[SENDER_ADD] = NULL;
183 s->client_cmds[SENDER_DELETE] = NULL;
187 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
189 s->status = SENDER_ON;