1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
#include "esd-server.h"
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_FILIO_H
# include <sys/filio.h>
#endif
#ifdef USE_LIBWRAP
#include <tcpd.h>
#include <syslog.h>
int allow_severity = LOG_INFO;
int deny_severity = LOG_WARNING;
#endif
/*******************************************************************/
/* globals */
/* the list of the currently connected clients */
esd_client_t *esd_clients_list;
/*******************************************************************/
/* prototypes */
void dump_clients(void);
void free_client( esd_client_t *client );
/*******************************************************************/
/* for debugging purposes, dump the list of the clients and data */
void dump_clients()
{
long addr;
short port;
esd_client_t *clients = esd_clients_list;
if ( !esdbg_trace ) return;
while ( clients != NULL ) {
port = ntohs( clients->source.sin_port );
addr = ntohl( clients->source.sin_addr.s_addr );
printf( "(%02d) client from: %03u.%03u.%03u.%03u:%05d [%p]\n",
clients->fd, (unsigned int) addr >> 24,
(unsigned int) (addr >> 16) % 256,
(unsigned int) (addr >> 8) % 256,
(unsigned int) addr % 256, port, clients );
clients = clients->next;
}
return;
}
/*******************************************************************/
/* deallocate memory for the client */
void free_client( esd_client_t *client )
{
/* free the client memory */
free( client );
return;
}
/*******************************************************************/
/* add a complete new client into the list of clients at head */
void add_new_client( esd_client_t *new_client )
{
/* printf ( "adding client 0x%08x\n", new_client ); */
new_client->next = esd_clients_list;
esd_clients_list = new_client;
return;
}
/*******************************************************************/
/* erase a client from the client list */
void erase_client( esd_client_t *client )
{
esd_client_t *previous = NULL;
esd_client_t *current = esd_clients_list;
/* iterate until we hit a NULL */
while ( current != NULL )
{
/* see if we hit the target client */
if ( current == client ) {
if( previous != NULL ){
/* we are deleting in the middle of the list */
previous->next = current->next;
} else {
/* we are deleting the head of the list */
esd_clients_list = current->next;
}
ESDBG_TRACE( printf ( "(%02d) closing client connection\n",
client->fd ); );
close( client->fd );
free_client( client );
return;
}
/* iterate through the list */
previous = current;
current = current->next;
}
/* hmm, we didn't find the desired client, just get on with life */
ESDBG_TRACE( printf( "(%02d) client not found\n", client->fd ); );
return;
}
/*******************************************************************/
/* checks for new connections at listener - zero when done */
int get_new_clients( int listen )
{
int fd, nbl;
struct sockaddr_in incoming;
size_t size_in = sizeof(struct sockaddr_in);
esd_client_t *new_client = NULL;
unsigned long addr;
short port;
/* see who awakened us */
do {
fd = accept( listen, (struct sockaddr*) &incoming, &size_in );
if ( fd > 0 ) {
port = ntohs( incoming.sin_port );
addr = ntohl( incoming.sin_addr.s_addr );
ESDBG_TRACE(
printf( "(%02d) new client from: %03u.%03u.%03u.%03u:%05d\n",
fd, (unsigned int) addr >> 24,
(unsigned int) (addr >> 16) % 256,
(unsigned int) (addr >> 8) % 256,
(unsigned int) addr % 256, port ); );
#ifdef USE_LIBWRAP
{
struct request_info req;
struct servent *serv;
request_init( &req, RQ_DAEMON, "esound", RQ_FILE, fd, NULL );
fromhost( &req );
if ( !hosts_access( &req )) {
ESDBG_TRACE(
printf( "connection from %s refused by tcp_wrappers\n",
eval_client( &req ) ); );
close( fd );
continue;
}
}
#endif
ESDBG_COMMS( printf( "================================\n" ); );
/* make sure we have the memory to save the client... */
new_client = (esd_client_t*) malloc( sizeof(esd_client_t) );
if ( new_client == NULL ) {
close( fd );
return -1;
}
/* It appears that not all systems construct the new socket in
* a blocking mode, if the listening socket is non-blocking, so
* let's set that here...
*/
nbl = 0;
if ( ioctl( fd, FIONBIO, &nbl ) < 0 )
{
ESDBG_TRACE( printf( "(%02d) couldn't turn on blocking for client\n",
fd ); );
close( fd );
return -1;
}
/* Reduce buffers on sockets to the minimum needed */
esd_set_socket_buffers( fd, ESD_BITS16, 44100, esd_audio_rate );
/* fill in the new_client structure - sockaddr = works!? */
new_client->next = NULL;
new_client->state = ESD_NEEDS_REQDATA;
new_client->request = ESD_PROTO_CONNECT;
new_client->fd = fd;
new_client->source = incoming;
new_client->proto_data_length = 0;
add_new_client( new_client );
}
} while ( fd > 0 );
return 0;
}
static int is_paused_here = 0;
/*******************************************************************/
/* blocks waiting for data from the listener, and client conns. */
int wait_for_clients_and_data( int listen )
{
fd_set rd_fds;
struct timeval timeout;
struct timeval *timeout_ptr = NULL;
esd_client_t *client = esd_clients_list;
int max_fd = listen, ready;
/* add the listener to the file descriptor list */
FD_ZERO( &rd_fds );
FD_SET( listen, &rd_fds );
/* add the clients to the list, too */
while ( client != NULL )
{
/* add this client, but only if it's not monitoring */
if ( client->state == ESD_STREAMING_DATA &&
client->request == ESD_PROTO_STREAM_MON )
{
client = client->next;
continue;
}
FD_SET( client->fd, &rd_fds );
/* update the maximum fd for the select() */
if ( client->fd > max_fd )
max_fd = client->fd;
/* next client */
client = client->next;
}
/* if we're doing something useful, make sure we return immediately */
if ( esd_recorder || esd_playing_samples ) {
timeout.tv_sec = 0;
timeout.tv_usec = 0;
timeout_ptr = &timeout;
} else {
/* TODO: any reason not to pause indefinitely here? */
/* sample players that's why, if no players, can pause indefinitely */
/* if ( esd_on_autostandby
|| (esd_autostandby_secs < 0 && !esd_playing_samples ) )
timeout_ptr = NULL; else { ... } */
if ( is_paused_here ) {
ESDBG_TRACE( printf( "paused, awaiting instructions.\n" ); );
timeout_ptr = NULL;
} else {
timeout.tv_sec = 0;
/* funky math to make sure a long can hold it all, calulate in ms */
timeout.tv_usec = (long) esd_buf_size_samples * 1000L
/ (long) esd_audio_rate / 4L; /* divide by two for stereo */
timeout.tv_usec *= 1000L; /* convert to microseconds */
timeout_ptr = &timeout;
}
}
ready = select( max_fd+1, &rd_fds, NULL, NULL, timeout_ptr );
ESDBG_COMMS( printf(
"paused=%d, samples=%d, auto=%d, standby=%d, record=%d, ready=%d\n",
is_paused_here, esd_playing_samples,
esd_autostandby_secs, esd_on_standby,
(esd_recorder != 0), ready ); );
/* TODO: return ready, and do this in esd.c */
if ( ready <= 0 ) {
/* if < 0, something horrible happened:
EBADF invalid file descriptor - let individual read sort it out
EINTR non blocked signal caught - o well, no big deal
EINVAL n is negative - not bloody likely
ENOMEM unable to allocate internal tables - o well, no big deal */
if ( !is_paused_here && !esd_playing_samples && (esd_autostandby_secs<0) ) {
ESDBG_TRACE( printf( "doing nothing, pausing server.\n" ); );
esd_audio_flush();
esd_audio_pause();
esd_last_activity = time( NULL );
is_paused_here = 1;
}
if ( !is_paused_here && !esd_playing_samples && !esd_recorder ) {
if ( esd_autostandby_secs >= 0
&& ( time(NULL) > esd_last_activity + esd_autostandby_secs ) ) {
ESDBG_TRACE( printf( "bored, going to standby mode.\n" ); );
esd_server_standby();
esd_on_autostandby = 1;
is_paused_here = 1;
}
}
} else {
is_paused_here = 0;
}
return ready;
}
|