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>
35 #include "close_on_fork.h"
36 #include "server.cmdline.h"
38 /** the list of connected clients **/
39 static struct list_head clients;
40 static int listen_fd = -1;
41 static struct sender *self;
43 /** describes one connected client */
45 /** the dccp socket */
47 /** address information about the client */
48 struct sockaddr_in addr;
49 /** the position of this client in the client list */
50 struct list_head node;
51 /** non-zero if audio file header has been sent */
55 static void dccp_pre_select( int *max_fileno, fd_set *rfds,
56 __a_unused fd_set *wfds)
60 FD_SET(listen_fd, rfds);
61 *max_fileno = PARA_MAX(*max_fileno, listen_fd);
64 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
66 struct dccp_client *dc;
69 if (!FD_ISSET(listen_fd, rfds))
71 dc = para_calloc(sizeof(struct dccp_client));
72 ret = para_accept(listen_fd, &dc->addr, sizeof(struct sockaddr_in));
74 PARA_ERROR_LOG("%s", PARA_STRERROR(-ret));
77 PARA_NOTICE_LOG("connection from %s\n", inet_ntoa(dc->addr.sin_addr));
79 list_add(&dc->node, &clients);
80 add_close_on_fork_list(dc->fd);
81 mark_fd_nonblock(dc->fd);
84 static int dccp_open(void)
86 struct sockaddr_in servaddr;
89 ret = dccp_get_socket();
94 bzero(&servaddr, sizeof(servaddr));
95 servaddr.sin_family = AF_INET;
96 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
97 servaddr.sin_port = htons(conf.dccp_port_arg);
98 ret = bind(listen_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
101 ret = dccp_set_socket(listen_fd);
104 ret = listen(listen_fd, 0);
106 return -E_DCCP_LISTEN;
107 PARA_DEBUG_LOG("listening on fd %d\n", listen_fd);
108 add_close_on_fork_list(listen_fd);
109 mark_fd_nonblock(listen_fd);
113 static void dccp_shutdown_client(struct dccp_client *dc)
115 PARA_DEBUG_LOG("shutting down %s (fd %d)\n", inet_ntoa(dc->addr.sin_addr),
118 del_close_on_fork_list(dc->fd);
123 static int dccp_write(int fd, const char *buf, size_t len)
125 size_t send, written = 0;
128 send = PARA_MIN(1024, len - written);
129 ret = write(fd, buf + written, send);
139 return -E_DCCP_WRITE;
142 static void dccp_send(long unsigned current_chunk,
143 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
145 struct dccp_client *dc, *tmp;
149 if (listen_fd < 0 || !len)
152 list_for_each_entry_safe(dc, tmp, &clients, node) {
153 ret = write_ok(dc->fd);
155 dccp_shutdown_client(dc);
160 if (!dc->header_sent && current_chunk) {
161 header_buf = afs_get_header(&header_len);
162 if (header_buf && header_len > 0) {
163 ret = dccp_write(dc->fd, header_buf, header_len);
164 if (ret != header_len) {
166 PARA_ERROR_LOG("header write: %d/%d (%s)\n",
167 ret, header_len, ret < 0?
169 dccp_shutdown_client(dc);
173 ret = write_ok(dc->fd);
175 dccp_shutdown_client(dc);
182 // PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
183 ret = dccp_write(dc->fd, buf, len);
185 dccp_shutdown_client(dc);
189 static void dccp_shutdown_clients(void)
191 struct dccp_client *dc, *tmp;
193 list_for_each_entry_safe(dc, tmp, &clients, node)
194 dccp_shutdown_client(dc);
197 static char *dccp_info(void)
201 struct dccp_client *dc, *tmp;
204 list_for_each_entry_safe(dc, tmp, &clients, node)
206 buf = make_message("dccp connected clients: %d\n", num_clients);
210 static char *dccp_help(void)
212 return make_message("no help available\n");
216 * the init function of the dccp sender
218 * \param s pointer to the dccp sender struct
220 * It initializes all function pointers of \a s and starts
221 * listening on the given port.
223 void dccp_send_init(struct sender *s)
227 INIT_LIST_HEAD(&clients);
230 s->pre_select = dccp_pre_select;
231 s->post_select = dccp_post_select;
232 s->shutdown_clients = dccp_shutdown_clients;
234 s->client_cmds[SENDER_ON] = NULL;
235 s->client_cmds[SENDER_OFF] = NULL;
236 s->client_cmds[SENDER_DENY] = NULL;
237 s->client_cmds[SENDER_ALLOW] = NULL;
238 s->client_cmds[SENDER_ADD] = NULL;
239 s->client_cmds[SENDER_DELETE] = NULL;
243 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
244 s->status = SENDER_OFF;
246 s->status = SENDER_ON;