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(__unused
struct audio_format
*af
, int *max_fileno
, fd_set
*rfds
,
54 __unused fd_set
*wfds
)
58 FD_SET(listen_fd
, rfds
);
59 *max_fileno
= MAX(*max_fileno
, listen_fd
);
62 static void dccp_post_select(__unused
struct audio_format
*af
, fd_set
*rfds
,
63 __unused fd_set
*wfds
)
65 struct dccp_client
*dc
;
68 if (!FD_ISSET(listen_fd
, rfds
))
70 dc
= para_calloc(sizeof(struct dccp_client
));
71 ret
= para_accept(listen_fd
, &dc
->addr
, sizeof(struct sockaddr_in
));
73 PARA_ERROR_LOG("%s", PARA_STRERROR(-ret
));
76 PARA_NOTICE_LOG("connection from %s\n", inet_ntoa(dc
->addr
.sin_addr
));
78 list_add(&dc
->node
, &clients
);
81 static int dccp_open(void)
83 struct sockaddr_in servaddr
;
86 ret
= dccp_get_socket();
91 bzero(&servaddr
, sizeof(servaddr
));
92 servaddr
.sin_family
= AF_INET
;
93 servaddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
94 servaddr
.sin_port
= htons(conf
.dccp_port_arg
);
95 ret
= bind(listen_fd
, (struct sockaddr
*)&servaddr
, sizeof(servaddr
));
98 ret
= dccp_set_socket(listen_fd
);
101 ret
= listen(listen_fd
, 0);
103 return -E_DCCP_LISTEN
;
104 PARA_DEBUG_LOG("listening on fd %d\n", listen_fd
);
108 static void dccp_shutdown_client(struct dccp_client
*dc
)
110 PARA_DEBUG_LOG("shutting down %s (fd %d)\n", inet_ntoa(dc
->addr
.sin_addr
),
117 static int dccp_write(int fd
, const char *buf
, size_t len
)
119 size_t send
, written
= 0;
122 send
= MIN(1024, len
- written
);
123 ret
= write(fd
, buf
+ written
, send
);
133 return -E_DCCP_WRITE
;
136 static void dccp_send(__unused
struct audio_format
*af
,
137 long unsigned current_chunk
,
138 __unused
long unsigned chunks_sent
, const char *buf
, size_t len
)
140 struct dccp_client
*dc
, *tmp
;
144 if (listen_fd
< 0 || !len
)
147 list_for_each_entry_safe(dc
, tmp
, &clients
, node
) {
148 ret
= write_ok(dc
->fd
);
150 dccp_shutdown_client(dc
);
155 if (!dc
->header_sent
&& af
->get_header_info
&& current_chunk
) {
156 header_buf
= af
->get_header_info(&header_len
);
157 if (!header_buf
|| header_len
<= 0)
158 continue; /* header not yet available */
159 ret
= dccp_write(dc
->fd
, header_buf
, header_len
);
160 if (ret
!= header_len
) {
162 PARA_ERROR_LOG("header write: %d/%d (%s)\n",
163 ret
, header_len
, ret
< 0?
165 dccp_shutdown_client(dc
);
169 ret
= write_ok(dc
->fd
);
171 dccp_shutdown_client(dc
);
177 // PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
178 ret
= dccp_write(dc
->fd
, buf
, len
);
180 dccp_shutdown_client(dc
);
184 static void dccp_shutdown_clients(void)
186 struct dccp_client
*dc
, *tmp
;
188 list_for_each_entry_safe(dc
, tmp
, &clients
, node
)
189 dccp_shutdown_client(dc
);
192 static char *dccp_info(void)
196 struct dccp_client
*dc
, *tmp
;
199 list_for_each_entry_safe(dc
, tmp
, &clients
, node
)
201 buf
= make_message("dccp connected clients: %d\n", num_clients
);
205 static char *dccp_help(void)
207 return make_message("no help available\n");
211 * the init function of the dccp sender
213 * \param s pointer to the dccp sender struct
215 * It initializes all function pointers of \a s and starts
216 * listening on the given port.
218 void dccp_send_init(struct sender
*s
)
222 INIT_LIST_HEAD(&clients
);
225 s
->pre_select
= dccp_pre_select
;
226 s
->post_select
= dccp_post_select
;
227 s
->shutdown_clients
= dccp_shutdown_clients
;
229 s
->client_cmds
[SENDER_ON
] = NULL
;
230 s
->client_cmds
[SENDER_OFF
] = NULL
;
231 s
->client_cmds
[SENDER_DENY
] = NULL
;
232 s
->client_cmds
[SENDER_ALLOW
] = NULL
;
233 s
->client_cmds
[SENDER_ADD
] = NULL
;
234 s
->client_cmds
[SENDER_DELETE
] = NULL
;
238 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
240 s
->status
= SENDER_ON
;