client.c 7.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* 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).
Philipp Schafft's avatar
Philipp Schafft committed
11
 * Copyright 2011-2012, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
12 13
 */

Jack Moffitt's avatar
Jack Moffitt committed
14 15 16 17 18 19
/* client.c
**
** client interface implementation
**
*/

20 21 22 23
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
24 25 26
#include <stdlib.h>
#include <string.h>

Karl Heyes's avatar
Karl Heyes committed
27 28 29
#include "thread/thread.h"
#include "avl/avl.h"
#include "httpp/httpp.h"
Jack Moffitt's avatar
Jack Moffitt committed
30

31
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
32 33
#include "connection.h"
#include "refbuf.h"
Karl Heyes's avatar
Karl Heyes committed
34
#include "format.h"
35
#include "stats.h"
36
#include "fserve.h"
Jack Moffitt's avatar
Jack Moffitt committed
37 38 39

#include "client.h"
#include "logging.h"
40

41 42
#include "util.h"

43 44 45
#ifdef _WIN32
#define snprintf _snprintf
#endif
Jack Moffitt's avatar
Jack Moffitt committed
46

47 48 49
#undef CATMODULE
#define CATMODULE "client"

50 51
static inline void client_send_500(client_t *client, const char *message);

52 53 54 55 56
/* create a client_t with the provided connection and parser details. Return
 * 0 on success, -1 if server limit has been reached.  In either case a
 * client_t is returned just in case a message needs to be returned. Should
 * be called with global lock held.
 */
57
int client_create (client_t **c_ptr, connection_t *con, http_parser_t *parser)
Jack Moffitt's avatar
Jack Moffitt committed
58
{
59
    ice_config_t *config;
60
    client_t *client = (client_t *)calloc(1, sizeof(client_t));
61 62 63
    int ret = -1;

    if (client == NULL)
64
        abort();
65 66

    config = config_get_config ();
67 68

    global.clients++;
69
    if (config->client_limit < global.clients)
70
        ICECAST_LOG_WARN("server client limit reached (%d/%d)", config->client_limit, global.clients);
71 72
    else
        ret = 0;
Jack Moffitt's avatar
Jack Moffitt committed
73

74 75 76
    config_release_config ();

    stats_event_args (NULL, "clients", "%d", global.clients);
77 78
    client->con = con;
    client->parser = parser;
79 80
    client->refbuf = refbuf_new (PER_CLIENT_REFBUF_SIZE);
    client->refbuf->len = 0; /* force reader code to ignore buffer contents */
81
    client->pos = 0;
82
    client->write_to_client = format_generic_write_to_client;
83
    *c_ptr = client;
Jack Moffitt's avatar
Jack Moffitt committed
84

85
    return ret;
Jack Moffitt's avatar
Jack Moffitt committed
86 87 88 89
}

void client_destroy(client_t *client)
{
Karl Heyes's avatar
Karl Heyes committed
90 91
    if (client == NULL)
        return;
92

93 94 95 96 97 98 99 100
    /* release the buffer now, as the buffer could be on the source queue
     * and may of disappeared after auth completes */
    if (client->refbuf)
    {
        refbuf_release (client->refbuf);
        client->refbuf = NULL;
    }

101
    if (auth_release_listener (client))
102 103
        return;

104
    /* write log entry if ip is set (some things don't set it, like outgoing 
105 106
     * slave requests
     */
Karl Heyes's avatar
Karl Heyes committed
107
    if (client->respcode && client->parser)
108
        logging_access(client);
109

110 111
    if (client->con)
        connection_close(client->con);
Karl Heyes's avatar
Karl Heyes committed
112 113
    if (client->parser)
        httpp_destroy(client->parser);
Jack Moffitt's avatar
Jack Moffitt committed
114

115 116 117 118 119
    global_lock ();
    global.clients--;
    stats_event_args (NULL, "clients", "%d", global.clients);
    global_unlock ();

120 121 122 123
    /* we need to free client specific format data (if any) */
    if (client->free_client_data)
        client->free_client_data (client);

Michael Smith's avatar
Michael Smith committed
124
    free(client->username);
Karl Heyes's avatar
Karl Heyes committed
125
    free(client->password);
Michael Smith's avatar
Michael Smith committed
126

127
    free(client);
Jack Moffitt's avatar
Jack Moffitt committed
128
}
129

130 131 132 133 134 135 136 137
/* return -1 for failed, 0 for authenticated, 1 for pending
 */
int client_check_source_auth (client_t *client, const char *mount)
{
    ice_config_t *config = config_get_config();
    char *pass = config->source_password;
    char *user = "source";
    int ret = -1;
138
    mount_proxy *mountinfo = config_find_mount (config, mount, MOUNT_TYPE_NORMAL);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

    do
    {
        if (mountinfo)
        {
            ret = 1;
            if (auth_stream_authenticate (client, mount, mountinfo) > 0)
                break;
            ret = -1;
            if (mountinfo->password)
                pass = mountinfo->password;
            if (mountinfo->username)
                user = mountinfo->username;
        }
        if (connection_check_pass (client->parser, user, pass) > 0)
            ret = 0;
    } while (0);
    config_release_config();
    return ret;
}

160 161 162 163

/* helper function for reading data from a client */
int client_read_bytes (client_t *client, void *buf, unsigned len)
{
164
    int bytes;
165

166 167 168 169 170 171
    if (client->refbuf && client->refbuf->len)
    {
        /* we have data to read from a refbuf first */
        if (client->refbuf->len < len)
            len = client->refbuf->len;
        memcpy (buf, client->refbuf->data, len);
172
        if (len < client->refbuf->len)
173 174 175 176 177 178 179
        {
            char *ptr = client->refbuf->data;
            memmove (ptr, ptr+len, client->refbuf->len - len);
        }
        client->refbuf->len -= len;
        return len;
    }
180
    bytes = client->con->read (client->con, buf, len);
181

182
    if (bytes == -1 && client->con->error)
183
        ICECAST_LOG_DEBUG("reading from connection has failed");
184 185

    return bytes;
186 187
}

188
void client_send_error(client_t *client, int status, int plain, const char *message)
189
{
190 191
    ssize_t ret;

192 193 194 195 196
    if (status == 500) {
         client_send_500(client, message);
         return;
    }

197
    ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
198
                                 0, status, NULL,
199
                                 plain ? "text/plain" : "text/html", "utf-8",
200
                                 plain ? message : "", NULL);
201

202 203 204 205 206 207
    if (ret == -1 || ret >= PER_CLIENT_REFBUF_SIZE) {
        ICECAST_LOG_ERROR("Dropping client as we can not build response headers.");
        client_send_500(client, "Header generation failed.");
        return;
    }

208 209 210 211
    if (!plain)
        snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
                 "<html><head><title>Error %i</title></head><body><b>%i - %s</b></body></html>\r\n",
                 status, status, message);
212

213
    client->respcode = status;
214 215
    client->refbuf->len = strlen (client->refbuf->data);
    fserve_add_client (client, NULL);
216 217
}

218 219 220 221 222 223
void client_send_100(client_t *client)
{
    /* On demand inject a HTTP/1.1 100 Continue to make sure clients are happy */
    sock_write (client->con->sock, "HTTP/1.1 100 Continue\r\n\r\n");
}

224
/* this function is designed to work even if client is in bad state */
225
static inline void client_send_500(client_t *client, const char *message) {
226 227 228 229 230 231 232 233 234 235 236 237 238
    const char header[] = "HTTP/1.0 500 Internal Server Error\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n"
                          "500 - Internal Server Error\n---------------------------\n";
    const size_t header_len = sizeof(header) - 1;
    int ret;

    ret = client_send_bytes(client, header, header_len);

    /* only send message if we have one AND if header could have transmitted completly */
    if (message && ret == header_len)
        client_send_bytes(client, message, strlen(message));

    client_destroy(client);
}
239 240 241 242

/* helper function for sending the data to a client */
int client_send_bytes (client_t *client, const void *buf, unsigned len)
{
243 244 245
    int ret = client->con->send (client->con, buf, len);

    if (client->con->error)
246
        ICECAST_LOG_DEBUG("Client connection died");
247

248 249 250
    return ret;
}

Karl Heyes's avatar
Karl Heyes committed
251 252 253 254 255
void client_set_queue (client_t *client, refbuf_t *refbuf)
{
    refbuf_t *to_release = client->refbuf;

    client->refbuf = refbuf;
Karl Heyes's avatar
Karl Heyes committed
256 257
    if (refbuf)
        refbuf_addref (client->refbuf);
Karl Heyes's avatar
Karl Heyes committed
258 259 260 261 262
    client->pos = 0;
    if (to_release)
        refbuf_release (to_release);
}