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 #define DCCP_RETRIES 100
125 static int dccp_write(int fd
, const char *buf
, size_t len
)
127 size_t size
, written
= 0;
128 int ret
, retries
= 0;
130 size
= PARA_MIN(1024, len
- written
);
131 ret
= write(fd
, buf
+ written
, size
);
133 if (errno
!= EAGAIN
|| !retries
++ > DCCP_RETRIES
)
135 PARA_DEBUG_LOG("EAGAIN #%zd@%zd/%d\n", retries
, written
, len
);
146 return -E_DCCP_WRITE
;
149 static void dccp_send(long unsigned current_chunk
,
150 __a_unused
long unsigned chunks_sent
, const char *buf
, size_t len
)
152 struct dccp_client
*dc
, *tmp
;
156 if (listen_fd
< 0 || !len
)
159 list_for_each_entry_safe(dc
, tmp
, &clients
, node
) {
160 ret
= write_ok(dc
->fd
);
162 dccp_shutdown_client(dc
);
167 if (!dc
->header_sent
&& current_chunk
) {
168 header_buf
= afs_get_header(&header_len
);
169 if (header_buf
&& header_len
> 0) {
170 ret
= dccp_write(dc
->fd
, header_buf
, header_len
);
171 if (ret
!= header_len
) {
173 PARA_ERROR_LOG("header write: %d/%d (%s)\n",
174 ret
, header_len
, ret
< 0?
176 dccp_shutdown_client(dc
);
180 ret
= write_ok(dc
->fd
);
182 dccp_shutdown_client(dc
);
189 // PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
190 ret
= dccp_write(dc
->fd
, buf
, len
);
192 dccp_shutdown_client(dc
);
196 static void dccp_shutdown_clients(void)
198 struct dccp_client
*dc
, *tmp
;
200 list_for_each_entry_safe(dc
, tmp
, &clients
, node
)
201 dccp_shutdown_client(dc
);
204 static char *dccp_info(void)
208 struct dccp_client
*dc
, *tmp
;
211 list_for_each_entry_safe(dc
, tmp
, &clients
, node
)
213 buf
= make_message("dccp connected clients: %d\n", num_clients
);
217 static char *dccp_help(void)
219 return make_message("no help available\n");
223 * the init function of the dccp sender
225 * \param s pointer to the dccp sender struct
227 * It initializes all function pointers of \a s and starts
228 * listening on the given port.
230 void dccp_send_init(struct sender
*s
)
234 INIT_LIST_HEAD(&clients
);
237 s
->pre_select
= dccp_pre_select
;
238 s
->post_select
= dccp_post_select
;
239 s
->shutdown_clients
= dccp_shutdown_clients
;
241 s
->client_cmds
[SENDER_ON
] = NULL
;
242 s
->client_cmds
[SENDER_OFF
] = NULL
;
243 s
->client_cmds
[SENDER_DENY
] = NULL
;
244 s
->client_cmds
[SENDER_ALLOW
] = NULL
;
245 s
->client_cmds
[SENDER_ADD
] = NULL
;
246 s
->client_cmds
[SENDER_DELETE
] = NULL
;
250 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
251 s
->status
= SENDER_OFF
;
253 s
->status
= SENDER_ON
;