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.
19 /** \file dccp_send.c paraslash's dccp sender */
22 * based on server.c of dccp-cs-0.01.tar.bz2,
23 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
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;
41 /** describes one connected client */
43 /** the dccp socket */
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 */
53 static void dccp_pre_select( int *max_fileno, fd_set *rfds,
54 __a_unused fd_set *wfds)
58 FD_SET(listen_fd, rfds);
59 *max_fileno = PARA_MAX(*max_fileno, listen_fd);
62 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
64 struct dccp_client *dc;
67 if (!FD_ISSET(listen_fd, rfds))
69 dc = para_calloc(sizeof(struct dccp_client));
70 ret = para_accept(listen_fd, &dc->addr, sizeof(struct sockaddr_in));
72 PARA_ERROR_LOG("%s", PARA_STRERROR(-ret));
75 PARA_NOTICE_LOG("connection from %s\n", inet_ntoa(dc->addr.sin_addr));
77 list_add(&dc->node, &clients);
80 static int dccp_open(void)
82 struct sockaddr_in servaddr;
85 ret = dccp_get_socket();
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));
97 ret = dccp_set_socket(listen_fd);
100 ret = listen(listen_fd, 0);
102 return -E_DCCP_LISTEN;
103 PARA_DEBUG_LOG("listening on fd %d\n", listen_fd);
107 static void dccp_shutdown_client(struct dccp_client *dc)
109 PARA_DEBUG_LOG("shutting down %s (fd %d)\n", inet_ntoa(dc->addr.sin_addr),
116 static int dccp_write(int fd, const char *buf, size_t len)
118 size_t send, written = 0;
121 send = PARA_MIN(1024, len - written);
122 ret = write(fd, buf + written, send);
132 return -E_DCCP_WRITE;
135 static void dccp_send(long unsigned current_chunk,
136 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
138 struct dccp_client *dc, *tmp;
142 if (listen_fd < 0 || !len)
145 list_for_each_entry_safe(dc, tmp, &clients, node) {
146 ret = write_ok(dc->fd);
148 dccp_shutdown_client(dc);
153 if (!dc->header_sent && current_chunk) {
154 header_buf = afs_get_header(&header_len);
155 if (header_buf && header_len > 0) {
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);
175 // PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
176 ret = dccp_write(dc->fd, buf, len);
178 dccp_shutdown_client(dc);
182 static void dccp_shutdown_clients(void)
184 struct dccp_client *dc, *tmp;
186 list_for_each_entry_safe(dc, tmp, &clients, node)
187 dccp_shutdown_client(dc);
190 static char *dccp_info(void)
194 struct dccp_client *dc, *tmp;
197 list_for_each_entry_safe(dc, tmp, &clients, node)
199 buf = make_message("dccp connected clients: %d\n", num_clients);
203 static char *dccp_help(void)
205 return make_message("no help available\n");
209 * the init function of the dccp sender
211 * \param s pointer to the dccp sender struct
213 * It initializes all function pointers of \a s and starts
214 * listening on the given port.
216 void dccp_send_init(struct sender *s)
220 INIT_LIST_HEAD(&clients);
223 s->pre_select = dccp_pre_select;
224 s->post_select = dccp_post_select;
225 s->shutdown_clients = dccp_shutdown_clients;
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;
236 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
237 s->status = SENDER_OFF;
239 s->status = SENDER_ON;