2004-01-28 20:02:12 -05:00
|
|
|
/* Icecast
|
|
|
|
*
|
|
|
|
* This program is distributed under the GNU General Public License, version 2.
|
|
|
|
* A copy of this license is included with this source.
|
|
|
|
*
|
|
|
|
* Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
|
|
|
|
* Michael Smith <msmith@xiph.org>,
|
|
|
|
* oddsock <oddsock@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>
|
|
|
|
* and others (see AUTHORS for details).
|
|
|
|
*/
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
/* client.c
|
|
|
|
**
|
|
|
|
** client interface implementation
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2003-07-20 21:58:54 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2003-07-16 15:41:59 -04:00
|
|
|
#include "thread/thread.h"
|
|
|
|
#include "avl/avl.h"
|
|
|
|
#include "httpp/httpp.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2005-05-06 11:57:15 -04:00
|
|
|
#include "cfgfile.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
#include "connection.h"
|
|
|
|
#include "refbuf.h"
|
2005-05-06 11:57:15 -04:00
|
|
|
#include "stats.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
|
|
|
|
#include "client.h"
|
|
|
|
#include "logging.h"
|
|
|
|
|
2004-07-16 11:47:12 -04:00
|
|
|
#undef CATMODULE
|
|
|
|
#define CATMODULE "client"
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
client_t *client_create(connection_t *con, http_parser_t *parser)
|
|
|
|
{
|
2005-05-06 11:57:15 -04:00
|
|
|
ice_config_t *config = config_get_config ();
|
2003-03-14 21:10:19 -05:00
|
|
|
client_t *client = (client_t *)calloc(1, sizeof(client_t));
|
2005-05-06 11:57:15 -04:00
|
|
|
int client_limit = config->client_limit;
|
|
|
|
config_release_config ();
|
|
|
|
|
|
|
|
global_lock();
|
|
|
|
if (global.clients >= client_limit || client == NULL)
|
|
|
|
{
|
|
|
|
client_limit = global.clients;
|
|
|
|
global_unlock();
|
|
|
|
free (client);
|
|
|
|
WARN1 ("server client limit reached (%d clients)", client_limit);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
global.clients++;
|
|
|
|
stats_event_args (NULL, "clients", "%d", global.clients);
|
|
|
|
global_unlock();
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
client->con = con;
|
|
|
|
client->parser = parser;
|
2004-08-20 11:13:59 -04:00
|
|
|
client->refbuf = NULL;
|
2003-03-14 21:10:19 -05:00
|
|
|
client->pos = 0;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return client;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void client_destroy(client_t *client)
|
|
|
|
{
|
2004-02-02 19:48:02 -05:00
|
|
|
if (client == NULL)
|
|
|
|
return;
|
2003-03-14 21:10:19 -05:00
|
|
|
/* write log entry if ip is set (some things don't set it, like outgoing
|
2003-02-14 06:44:08 -05:00
|
|
|
* slave requests
|
|
|
|
*/
|
|
|
|
if(client->con->ip)
|
2003-03-14 21:10:19 -05:00
|
|
|
logging_access(client);
|
|
|
|
|
|
|
|
connection_close(client->con);
|
|
|
|
httpp_destroy(client->parser);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2005-05-06 11:57:15 -04:00
|
|
|
global_lock ();
|
|
|
|
global.clients--;
|
|
|
|
stats_event_args (NULL, "clients", "%d", global.clients);
|
|
|
|
global_unlock ();
|
|
|
|
|
2004-08-20 11:13:59 -04:00
|
|
|
/* drop ref counts if need be */
|
|
|
|
if (client->refbuf)
|
|
|
|
refbuf_release (client->refbuf);
|
2004-02-29 09:38:15 -05:00
|
|
|
/* we need to free client specific format data (if any) */
|
|
|
|
if (client->free_client_data)
|
|
|
|
client->free_client_data (client);
|
|
|
|
|
2004-01-14 20:01:09 -05:00
|
|
|
free(client->username);
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
free(client);
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
2002-08-12 10:48:31 -04:00
|
|
|
|
2002-12-31 01:28:39 -05:00
|
|
|
void client_send_400(client_t *client, char *message) {
|
|
|
|
int bytes;
|
2004-04-20 03:05:07 -04:00
|
|
|
bytes = sock_write(client->con->sock, "HTTP/1.0 400 Bad Request\r\n"
|
2002-12-31 01:28:39 -05:00
|
|
|
"Content-Type: text/html\r\n\r\n"
|
|
|
|
"<b>%s</b>\r\n", message);
|
|
|
|
if(bytes > 0) client->con->sent_bytes = bytes;
|
2004-04-20 03:05:07 -04:00
|
|
|
client->respcode = 400;
|
2002-12-31 01:28:39 -05:00
|
|
|
client_destroy(client);
|
|
|
|
}
|
|
|
|
|
2002-08-12 10:48:31 -04:00
|
|
|
void client_send_404(client_t *client, char *message) {
|
|
|
|
|
|
|
|
int bytes;
|
|
|
|
bytes = sock_write(client->con->sock, "HTTP/1.0 404 File Not Found\r\n"
|
|
|
|
"Content-Type: text/html\r\n\r\n"
|
|
|
|
"<b>%s</b>\r\n", message);
|
|
|
|
if(bytes > 0) client->con->sent_bytes = bytes;
|
2002-08-16 11:28:46 -04:00
|
|
|
client->respcode = 404;
|
2002-08-12 10:48:31 -04:00
|
|
|
client_destroy(client);
|
|
|
|
}
|
|
|
|
|
2003-03-02 05:13:59 -05:00
|
|
|
void client_send_504(client_t *client, char *message) {
|
|
|
|
int bytes;
|
|
|
|
client->respcode = 504;
|
2003-03-14 21:10:19 -05:00
|
|
|
bytes = sock_write(client->con->sock,
|
2003-03-02 05:13:59 -05:00
|
|
|
"HTTP/1.0 504 Server Full\r\n"
|
|
|
|
"Content-Type: text/html\r\n\r\n"
|
|
|
|
"<b>%s</b>\r\n", message);
|
2003-03-14 21:10:19 -05:00
|
|
|
if (bytes > 0) client->con->sent_bytes = bytes;
|
|
|
|
client_destroy(client);
|
2003-03-02 05:13:59 -05:00
|
|
|
}
|
|
|
|
|
2002-08-16 10:26:48 -04:00
|
|
|
void client_send_401(client_t *client) {
|
|
|
|
int bytes = sock_write(client->con->sock,
|
|
|
|
"HTTP/1.0 401 Authentication Required\r\n"
|
|
|
|
"WWW-Authenticate: Basic realm=\"Icecast2 Server\"\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"You need to authenticate\r\n");
|
|
|
|
if(bytes > 0) client->con->sent_bytes = bytes;
|
2002-08-16 11:28:46 -04:00
|
|
|
client->respcode = 401;
|
2002-08-16 10:26:48 -04:00
|
|
|
client_destroy(client);
|
|
|
|
}
|
2004-05-17 00:33:46 -04:00
|
|
|
|
|
|
|
void client_send_403(client_t *client) {
|
|
|
|
int bytes = sock_write(client->con->sock,
|
|
|
|
"HTTP/1.0 403 Forbidden\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"Access restricted.\r\n");
|
|
|
|
if(bytes > 0) client->con->sent_bytes = bytes;
|
|
|
|
client->respcode = 403;
|
|
|
|
client_destroy(client);
|
|
|
|
}
|
2004-07-16 11:47:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* helper function for sending the data to a client */
|
|
|
|
int client_send_bytes (client_t *client, const void *buf, unsigned len)
|
|
|
|
{
|
|
|
|
int ret = sock_write_bytes (client->con->sock, buf, len);
|
|
|
|
if (ret < 0 && !sock_recoverable (sock_error()))
|
|
|
|
{
|
|
|
|
DEBUG0 ("Client connection died");
|
|
|
|
client->con->error = 1;
|
|
|
|
}
|
|
|
|
if (ret > 0)
|
|
|
|
client->con->sent_bytes += ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-08-20 11:13:59 -04:00
|
|
|
void client_set_queue (client_t *client, refbuf_t *refbuf)
|
|
|
|
{
|
|
|
|
refbuf_t *to_release = client->refbuf;
|
|
|
|
|
|
|
|
client->refbuf = refbuf;
|
2004-08-23 15:01:18 -04:00
|
|
|
if (refbuf)
|
|
|
|
refbuf_addref (client->refbuf);
|
2004-08-20 11:13:59 -04:00
|
|
|
client->pos = 0;
|
|
|
|
if (to_release)
|
|
|
|
refbuf_release (to_release);
|
|
|
|
}
|
|
|
|
|