2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file dccp_send.c paraslash's dccp sender */
10 * based on server.c of dccp-cs-0.01.tar.bz2,
11 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
23 #include "close_on_fork.h"
24 #include "server.cmdline.h"
26 /** the list of connected clients **/
27 static struct list_head clients;
28 static int listen_fd = -1;
29 static struct sender *self;
31 /** describes one connected client */
33 /** the dccp socket */
35 /** address information about the client */
36 struct sockaddr_in addr;
37 /** the position of this client in the client list */
38 struct list_head node;
39 /** non-zero if audio file header has been sent */
43 static void dccp_pre_select( int *max_fileno, fd_set *rfds,
44 __a_unused fd_set *wfds)
48 FD_SET(listen_fd, rfds);
49 *max_fileno = PARA_MAX(*max_fileno, listen_fd);
52 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
54 struct dccp_client *dc;
57 if (!FD_ISSET(listen_fd, rfds))
59 dc = para_calloc(sizeof(struct dccp_client));
60 ret = para_accept(listen_fd, &dc->addr, sizeof(struct sockaddr_in));
62 PARA_ERROR_LOG("%s", PARA_STRERROR(-ret));
65 PARA_NOTICE_LOG("connection from %s\n", inet_ntoa(dc->addr.sin_addr));
67 para_list_add(&dc->node, &clients);
68 add_close_on_fork_list(dc->fd);
69 mark_fd_nonblock(dc->fd);
72 static int dccp_open(void)
74 struct sockaddr_in servaddr;
77 ret = dccp_get_socket();
82 memset(&servaddr, 0, sizeof(servaddr));
83 servaddr.sin_family = AF_INET;
84 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
85 servaddr.sin_port = htons(conf.dccp_port_arg);
86 ret = bind(listen_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
89 ret = dccp_set_socket(listen_fd);
92 ret = listen(listen_fd, 0);
94 return -E_DCCP_LISTEN;
95 PARA_DEBUG_LOG("listening on fd %d\n", listen_fd);
96 add_close_on_fork_list(listen_fd);
97 mark_fd_nonblock(listen_fd);
101 static void dccp_shutdown_client(struct dccp_client *dc)
103 PARA_DEBUG_LOG("shutting down %s (fd %d)\n", inet_ntoa(dc->addr.sin_addr),
106 del_close_on_fork_list(dc->fd);
111 static int dccp_write(int fd, const char *buf, size_t len)
116 while (written < len) {
117 ret = write(fd, buf + written, PARA_MIN(1024, len - written));
119 * Error handling: CCID3 has a sending wait queue which fills up and is
120 * emptied asynchronously. The EAGAIN case means that there is currently
121 * no space in the wait queue, but this can change at any moment and is
122 * thus not an error condition. Keep polling until an entry becomes free.
124 if (ret < 0 && errno == EAGAIN)
127 return -E_DCCP_WRITE;
133 static void dccp_send(long unsigned current_chunk,
134 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
136 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 && current_chunk) {
153 header_buf = vss_get_header(&header_len);
154 if (header_buf && header_len > 0) {
155 ret = dccp_write(dc->fd, header_buf, header_len);
156 if (ret != header_len) {
158 PARA_ERROR_LOG("header write: %d/%u (%s)\n",
159 ret, header_len, ret < 0?
161 dccp_shutdown_client(dc);
165 ret = write_ok(dc->fd);
167 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");
208 * the init function of the dccp sender
210 * \param s pointer to the dccp sender struct
212 * It initializes all function pointers of \a s and starts
213 * listening on the given port.
215 void dccp_send_init(struct sender *s)
219 INIT_LIST_HEAD(&clients);
222 s->pre_select = dccp_pre_select;
223 s->post_select = dccp_post_select;
224 s->shutdown_clients = dccp_shutdown_clients;
226 s->client_cmds[SENDER_ON] = NULL;
227 s->client_cmds[SENDER_OFF] = NULL;
228 s->client_cmds[SENDER_DENY] = NULL;
229 s->client_cmds[SENDER_ALLOW] = NULL;
230 s->client_cmds[SENDER_ADD] = NULL;
231 s->client_cmds[SENDER_DELETE] = NULL;
235 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
236 s->status = SENDER_OFF;
238 s->status = SENDER_ON;