connection.c 55.6 KB
Newer Older
1 2 3 4 5
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
6
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
7 8 9 10
 *                      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,      Dave 'justdave' Miller <justdave@mozilla.com>,
12
 * Copyright 2011-2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
13 14
 */

15
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
16 17 18 19
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
20 21
#include <stdio.h>
#include <stdlib.h>
22
#include <errno.h>
Jack Moffitt's avatar
Jack Moffitt committed
23
#include <string.h>
24 25 26
#ifdef HAVE_POLL
#include <sys/poll.h>
#endif
27
#include <sys/types.h>
28 29

#ifndef _WIN32
Jack Moffitt's avatar
Jack Moffitt committed
30 31
#include <sys/socket.h>
#include <netinet/in.h>
32
#else
33
#include <winsock2.h>
34
#endif
Jack Moffitt's avatar
Jack Moffitt committed
35

36 37 38 39
#include "common/thread/thread.h"
#include "common/avl/avl.h"
#include "common/net/sock.h"
#include "common/httpp/httpp.h"
Jack Moffitt's avatar
Jack Moffitt committed
40

41 42
#include "compat.h"
#include "connection.h"
43
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
44 45
#include "global.h"
#include "util.h"
46
#include "refobject.h"
Jack Moffitt's avatar
Jack Moffitt committed
47 48
#include "refbuf.h"
#include "client.h"
49
#include "errors.h"
Jack Moffitt's avatar
Jack Moffitt committed
50 51
#include "stats.h"
#include "logging.h"
52
#include "fserve.h"
53
#include "slave.h"
54

Jack Moffitt's avatar
Jack Moffitt committed
55
#include "source.h"
56
#include "admin.h"
57
#include "auth.h"
58
#include "matchfile.h"
59
#include "tls.h"
60
#include "acl.h"
Jack Moffitt's avatar
Jack Moffitt committed
61 62 63

#define CATMODULE "connection"

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/* Two different major types of source authentication.
   Shoutcast style is used only by the Shoutcast DSP
   and is a crazy version of HTTP.  It looks like :
     Source Client -> Connects to port + 1
     Source Client -> sends encoder password (plaintext)\r\n
     Icecast -> reads encoder password, if ok, sends OK2\r\n, else disconnects
     Source Client -> reads OK2\r\n, then sends http-type request headers
                      that contain the stream details (icy-name, etc..)
     Icecast -> reads headers, stores them
     Source Client -> starts sending MP3 data
     Source Client -> periodically updates metadata via admin.cgi call

   Icecast auth style uses HTTP and Basic Authorization.
*/

79 80 81 82 83
typedef struct client_queue_tag {
    client_t *client;
    int offset;
    int stream_offset;
    int shoutcast;
84
    char *shoutcast_mount;
85 86
    struct client_queue_tag *next;
} client_queue_t;
Jack Moffitt's avatar
Jack Moffitt committed
87

88
static spin_t _connection_lock; // protects _current_id, _con_queue, _con_queue_tail
89
static volatile unsigned long _current_id = 0;
Jack Moffitt's avatar
Jack Moffitt committed
90 91
static int _initialized = 0;

92 93
static volatile client_queue_t *_req_queue = NULL, **_req_queue_tail = &_req_queue;
static volatile client_queue_t *_con_queue = NULL, **_con_queue_tail = &_con_queue;
94
static volatile client_queue_t *_body_queue = NULL, **_body_queue_tail = &_body_queue;
95
static int tls_ok;
96
static tls_ctx_t *tls_ctx;
97

98
/* filtering client connection based on IP */
99
static matchfile_t *banned_ip, *allowed_ip;
100

101
rwlock_t _source_shutdown_rwlock;
Jack Moffitt's avatar
Jack Moffitt committed
102

103
static void _handle_connection(void);
104
static void get_tls_certificate(ice_config_t *config);
Jack Moffitt's avatar
Jack Moffitt committed
105 106 107

void connection_initialize(void)
{
Marvin Scholz's avatar
Marvin Scholz committed
108 109
    if (_initialized)
        return;
110

111
    thread_spin_create (&_connection_lock);
112
    thread_mutex_create(&move_clients_mutex);
113
    thread_rwlock_create(&_source_shutdown_rwlock);
114
    thread_cond_create(&global.shutdown_cond);
115 116 117 118
    _req_queue = NULL;
    _req_queue_tail = &_req_queue;
    _con_queue = NULL;
    _con_queue_tail = &_con_queue;
119 120
    _body_queue = NULL;
    _body_queue_tail = &_body_queue;
Jack Moffitt's avatar
Jack Moffitt committed
121

122
    _initialized = 1;
Jack Moffitt's avatar
Jack Moffitt committed
123 124 125 126
}

void connection_shutdown(void)
{
Marvin Scholz's avatar
Marvin Scholz committed
127 128
    if (!_initialized)
        return;
129

130
    tls_ctx_unref(tls_ctx);
131 132 133
    matchfile_release(banned_ip);
    matchfile_release(allowed_ip);
 
134
    thread_cond_destroy(&global.shutdown_cond);
135
    thread_rwlock_destroy(&_source_shutdown_rwlock);
136
    thread_spin_destroy (&_connection_lock);
137
    thread_mutex_destroy(&move_clients_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
138

139
    _initialized = 0;
Jack Moffitt's avatar
Jack Moffitt committed
140 141
}

142
void connection_reread_config(ice_config_t *config)
143
{
144
    get_tls_certificate(config);
145 146
}

Jack Moffitt's avatar
Jack Moffitt committed
147 148
static unsigned long _next_connection_id(void)
{
149
    unsigned long id;
Jack Moffitt's avatar
Jack Moffitt committed
150

151
    thread_spin_lock(&_connection_lock);
152
    id = _current_id++;
153
    thread_spin_unlock(&_connection_lock);
Jack Moffitt's avatar
Jack Moffitt committed
154

155
    return id;
Jack Moffitt's avatar
Jack Moffitt committed
156 157
}

158

159
#ifdef ICECAST_CAP_TLS
160
static void get_tls_certificate(ice_config_t *config)
161
{
162 163
    const char *keyfile;

164
    config->tls_ok = tls_ok = 0;
165

166 167 168 169
    keyfile = config->tls_context.key_file;
    if (!keyfile)
        keyfile = config->tls_context.cert_file;

170
    tls_ctx_unref(tls_ctx);
171
    tls_ctx = tls_ctx_new(config->tls_context.cert_file, keyfile, config->tls_context.cipher_list);
172 173
    if (!tls_ctx) {
        ICECAST_LOG_INFO("No TLS capability on any configured ports");
174
        return;
175 176
    }

177
    config->tls_ok = tls_ok = 1;
178 179 180
}


181
/* handlers for reading and writing a connection_t when there is TLS
182 183
 * configured on the listening port
 */
184
static int connection_read_tls(connection_t *con, void *buf, size_t len)
185
{
186
    ssize_t bytes = tls_read(con->tls, buf, len);
187

Marvin Scholz's avatar
Marvin Scholz committed
188
    if (bytes < 0) {
189
        if (tls_want_io(con->tls) > 0)
190
            return -1;
191 192 193 194 195
        con->error = 1;
    }
    return bytes;
}

196
static int connection_send_tls(connection_t *con, const void *buf, size_t len)
197
{
198
    ssize_t bytes = tls_write(con->tls, buf, len);
199

Marvin Scholz's avatar
Marvin Scholz committed
200
    if (bytes < 0) {
201 202
        if (tls_want_io(con->tls) > 0)
            return -1;
203
        con->error = 1;
Marvin Scholz's avatar
Marvin Scholz committed
204
    } else {
205
        con->sent_bytes += bytes;
Marvin Scholz's avatar
Marvin Scholz committed
206
    }
207 208 209 210
    return bytes;
}
#else

211 212
/* TLS not compiled in, so at least log it */
static void get_tls_certificate(ice_config_t *config)
213
{
214
    tls_ok = 0;
215
    ICECAST_LOG_INFO("No TLS capability. "
Philipp Schafft's avatar
Philipp Schafft committed
216
                     "Rebuild Icecast with OpenSSL support to enable this.");
217
}
218
#endif /* ICECAST_CAP_TLS */
219 220 221 222 223


/* handlers (default) for reading and writing a connection_t, no encrpytion
 * used just straight access to the socket
 */
Marvin Scholz's avatar
Marvin Scholz committed
224
static int connection_read(connection_t *con, void *buf, size_t len)
225
{
Marvin Scholz's avatar
Marvin Scholz committed
226
    int bytes = sock_read_bytes(con->sock, buf, len);
227 228
    if (bytes == 0)
        con->error = 1;
Marvin Scholz's avatar
Marvin Scholz committed
229
    if (bytes == -1 && !sock_recoverable(sock_error()))
230 231 232 233
        con->error = 1;
    return bytes;
}

Marvin Scholz's avatar
Marvin Scholz committed
234
static int connection_send(connection_t *con, const void *buf, size_t len)
235
{
Marvin Scholz's avatar
Marvin Scholz committed
236 237 238
    int bytes = sock_write_bytes(con->sock, buf, len);
    if (bytes < 0) {
        if (!sock_recoverable(sock_error()))
239
            con->error = 1;
Marvin Scholz's avatar
Marvin Scholz committed
240
    } else {
241
        con->sent_bytes += bytes;
Marvin Scholz's avatar
Marvin Scholz committed
242
    }
243 244 245
    return bytes;
}

246 247
connection_t *connection_create (sock_t sock, sock_t serversock, char *ip)
{
248
    connection_t *con;
249
    con = (connection_t *)calloc(1, sizeof(connection_t));
Marvin Scholz's avatar
Marvin Scholz committed
250 251
    if (con) {
        con->sock       = sock;
252
        con->serversock = serversock;
Marvin Scholz's avatar
Marvin Scholz committed
253 254 255
        con->con_time   = time(NULL);
        con->id         = _next_connection_id();
        con->ip         = ip;
256
        con->tlsmode    = ICECAST_TLSMODE_AUTO;
Marvin Scholz's avatar
Marvin Scholz committed
257 258
        con->read       = connection_read;
        con->send       = connection_send;
259
    }
260

261
    return con;
262 263
}

264
/* prepare connection for interacting over a TLS connection
265
 */
266
void connection_uses_tls(connection_t *con)
267
{
268
#ifdef ICECAST_CAP_TLS
269
    if (con->tls)
270 271
        return;

272 273 274 275 276 277
    if (con->readbufferlen) {
        ICECAST_LOG_ERROR("Connection is now using TLS but has data put back. BAD. Discarding putback data.");
        free(con->readbuffer);
        con->readbufferlen = 0;
    }

278
    con->tlsmode = ICECAST_TLSMODE_RFC2818;
279 280
    con->read = connection_read_tls;
    con->send = connection_send_tls;
281 282 283
    con->tls = tls_new(tls_ctx);
    tls_set_incoming(con->tls);
    tls_set_socket(con->tls, con->sock);
284 285 286
#endif
}

287 288
ssize_t connection_read_bytes(connection_t *con, void *buf, size_t len)
{
289 290 291 292 293 294 295 296
    ssize_t done = 0;
    ssize_t ret;

    if (con->readbufferlen) {
        ICECAST_LOG_DEBUG("On connection %p we read from putback buffer, filled with %zu bytes, requested are %zu bytes", con, con->readbufferlen, len);
        if (len >= con->readbufferlen) {
            memcpy(buf, con->readbuffer, con->readbufferlen);
            free(con->readbuffer);
297
            ICECAST_LOG_DEBUG("New fill in buffer=<empty>");
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
            if (len == con->readbufferlen) {
                con->readbufferlen = 0;
                return len;
            } else {
                len -= con->readbufferlen;
                buf += con->readbufferlen;
                done = con->readbufferlen;
                con->readbufferlen = 0;
            }
        } else {
            memcpy(buf, con->readbuffer, len);
            memmove(con->readbuffer, con->readbuffer+len, con->readbufferlen-len);
            con->readbufferlen -= len;
            return len;
        }
    }

    ret = con->read(con, buf, len);

    if (ret < 0) {
        if (done == 0) {
            return ret;
        } else {
            return done;
        }
    }

    return done + ret;
}

int connection_read_put_back(connection_t *con, const void *buf, size_t len)
{
    void *n;

    if (con->readbufferlen) {
        n = realloc(con->readbuffer, con->readbufferlen + len);
        if (!n)
            return -1;

        memcpy(n + con->readbufferlen, buf, len);
        con->readbuffer = n;
        con->readbufferlen += len;

        ICECAST_LOG_DEBUG("On connection %p %zu bytes have been put back.", con, len);
        return 0;
    } else {
        n = malloc(len);
        if (!n)
            return -1;

        memcpy(n, buf, len);
        con->readbuffer = n;
        con->readbufferlen = len;
        ICECAST_LOG_DEBUG("On connection %p %zu bytes have been put back.", con, len);
        return 0;
    }
354 355
}

356
static sock_t wait_for_serversock(int timeout)
357 358
{
#ifdef HAVE_POLL
359
    struct pollfd ufds [global.server_sockets];
360 361 362 363 364 365 366 367 368 369
    int i, ret;

    for(i=0; i < global.server_sockets; i++) {
        ufds[i].fd = global.serversock[i];
        ufds[i].events = POLLIN;
        ufds[i].revents = 0;
    }

    ret = poll(ufds, global.server_sockets, timeout);
    if(ret < 0) {
370
        return SOCK_ERROR;
Marvin Scholz's avatar
Marvin Scholz committed
371
    } else if(ret == 0) {
372
        return SOCK_ERROR;
Marvin Scholz's avatar
Marvin Scholz committed
373
    } else {
374
        int dst;
375
        for(i=0; i < global.server_sockets; i++) {
376
            if(ufds[i].revents & POLLIN)
377
                return ufds[i].fd;
Marvin Scholz's avatar
Marvin Scholz committed
378 379
            if(ufds[i].revents & (POLLHUP|POLLERR|POLLNVAL)) {
                if (ufds[i].revents & (POLLHUP|POLLERR)) {
380
                    sock_close (global.serversock[i]);
381
                    ICECAST_LOG_WARN("Had to close a listening socket");
382
                }
383
                global.serversock[i] = SOCK_ERROR;
384
            }
385
        }
386
        /* remove any closed sockets */
Marvin Scholz's avatar
Marvin Scholz committed
387
        for(i=0, dst=0; i < global.server_sockets; i++) {
388
            if (global.serversock[i] == SOCK_ERROR)
389
            continue;
390
            if (i!=dst)
391
            global.serversock[dst] = global.serversock[i];
392 393 394
            dst++;
        }
        global.server_sockets = dst;
395
        return SOCK_ERROR;
396 397 398 399 400
    }
#else
    fd_set rfds;
    struct timeval tv, *p=NULL;
    int i, ret;
401
    sock_t max = SOCK_ERROR;
402 403 404 405 406

    FD_ZERO(&rfds);

    for(i=0; i < global.server_sockets; i++) {
        FD_SET(global.serversock[i], &rfds);
407
        if (max == SOCK_ERROR || global.serversock[i] > max)
408 409 410 411 412
            max = global.serversock[i];
    }

    if(timeout >= 0) {
        tv.tv_sec = timeout/1000;
413
        tv.tv_usec = (timeout % 1000) * 1000;
414 415 416 417 418
        p = &tv;
    }

    ret = select(max+1, &rfds, NULL, NULL, p);
    if(ret < 0) {
419
        return SOCK_ERROR;
Marvin Scholz's avatar
Marvin Scholz committed
420
    } else if(ret == 0) {
421
        return SOCK_ERROR;
Marvin Scholz's avatar
Marvin Scholz committed
422
    } else {
423 424 425 426
        for(i=0; i < global.server_sockets; i++) {
            if(FD_ISSET(global.serversock[i], &rfds))
                return global.serversock[i];
        }
427
        return SOCK_ERROR; /* Should be impossible, stop compiler warnings */
428 429 430 431
    }
#endif
}

432
static connection_t *_accept_connection(int duration)
Jack Moffitt's avatar
Jack Moffitt committed
433
{
434
    sock_t sock, serversock;
435
    char *ip;
Jack Moffitt's avatar
Jack Moffitt committed
436

437
    serversock = wait_for_serversock (duration);
438
    if (serversock == SOCK_ERROR)
439
        return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
440

441 442
    /* malloc enough room for a full IP address (including ipv6) */
    ip = (char *)malloc(MAX_ADDR_LEN);
Jack Moffitt's avatar
Jack Moffitt committed
443

444
    sock = sock_accept(serversock, ip, MAX_ADDR_LEN);
Marvin Scholz's avatar
Marvin Scholz committed
445
    if (sock != SOCK_ERROR) {
446
        connection_t *con = NULL;
447
        /* Make any IPv4 mapped IPv6 address look like a normal IPv4 address */
Marvin Scholz's avatar
Marvin Scholz committed
448 449
        if (strncmp(ip, "::ffff:", 7) == 0)
            memmove(ip, ip+7, strlen (ip+7)+1);
Jack Moffitt's avatar
Jack Moffitt committed
450

451 452
        if (matchfile_match_allow_deny(allowed_ip, banned_ip, ip))
            con = connection_create (sock, serversock, ip);
453 454
        if (con)
            return con;
Marvin Scholz's avatar
Marvin Scholz committed
455 456 457
        sock_close(sock);
    } else {
        if (!sock_recoverable(sock_error())) {
458
            ICECAST_LOG_WARN("accept() failed with error %d: %s", sock_error(), strerror(sock_error()));
Marvin Scholz's avatar
Marvin Scholz committed
459
            thread_sleep(500000);
460
        }
461 462 463
    }
    free(ip);
    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
464 465 466
}


467 468 469 470
/* add client to connection queue. At this point some header information
 * has been collected, so we now pass it onto the connection thread for
 * further processing
 */
471
static void _add_connection(client_queue_t *node)
Jack Moffitt's avatar
Jack Moffitt committed
472
{
473
    thread_spin_lock(&_connection_lock);
474
    *_con_queue_tail = node;
475 476
    _con_queue_tail = (volatile client_queue_t **) &node->next;
    thread_spin_unlock(&_connection_lock);
Jack Moffitt's avatar
Jack Moffitt committed
477 478 479
}


480 481 482 483 484 485
/* this returns queued clients for the connection thread. headers are
 * already provided, but need to be parsed.
 */
static client_queue_t *_get_connection(void)
{
    client_queue_t *node = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
486

Marvin Scholz's avatar
Marvin Scholz committed
487
    thread_spin_lock(&_connection_lock);
488

Marvin Scholz's avatar
Marvin Scholz committed
489
    if (_con_queue){
490 491 492 493
        node = (client_queue_t *)_con_queue;
        _con_queue = node->next;
        if (_con_queue == NULL)
            _con_queue_tail = &_con_queue;
494
        node->next = NULL;
495
    }
496

Marvin Scholz's avatar
Marvin Scholz committed
497
    thread_spin_unlock(&_connection_lock);
498 499
    return node;
}
Jack Moffitt's avatar
Jack Moffitt committed
500 501


502
/* run along queue checking for any data that has come in or a timeout */
503
static void process_request_queue (void)
504 505
{
    client_queue_t **node_ref = (client_queue_t **)&_req_queue;
506 507 508 509 510 511
    ice_config_t *config;
    int timeout;
    char peak;

    config = config_get_config();
    timeout = config->header_timeout;
512
    config_release_config();
Jack Moffitt's avatar
Jack Moffitt committed
513

Marvin Scholz's avatar
Marvin Scholz committed
514
    while (*node_ref) {
515 516 517 518
        client_queue_t *node = *node_ref;
        client_t *client = node->client;
        int len = PER_CLIENT_REFBUF_SIZE - 1 - node->offset;
        char *buf = client->refbuf->data + node->offset;
Jack Moffitt's avatar
Jack Moffitt committed
519

520
        if (client->con->tlsmode == ICECAST_TLSMODE_AUTO || client->con->tlsmode == ICECAST_TLSMODE_AUTO_NO_PLAIN) {
521 522
            if (recv(client->con->sock, &peak, 1, MSG_PEEK) == 1) {
                if (peak == 0x16) { /* TLS Record Protocol Content type 0x16 == Handshake */
523
                    connection_uses_tls(client->con);
524 525 526 527
                }
            }
        }

Marvin Scholz's avatar
Marvin Scholz committed
528 529
        if (len > 0) {
            if (client->con->con_time + timeout <= time(NULL)) {
530
                len = 0;
Marvin Scholz's avatar
Marvin Scholz committed
531 532 533
            } else {
                len = client_read_bytes(client, buf, len);
            }
534
        }
Jack Moffitt's avatar
Jack Moffitt committed
535

Marvin Scholz's avatar
Marvin Scholz committed
536
        if (len > 0) {
537 538 539
            int pass_it = 1;
            char *ptr;

540 541
            /* handle \n, \r\n and nsvcap which for some strange reason has
             * EOL as \r\r\n */
542
            node->offset += len;
Marvin Scholz's avatar
Marvin Scholz committed
543 544 545
            client->refbuf->data[node->offset] = '\000';
            do {
                if (node->shoutcast == 1) {
546
                    /* password line */
547 548
                    if (strstr (client->refbuf->data, "\r\r\n") != NULL)
                        break;
549 550 551 552 553 554 555
                    if (strstr (client->refbuf->data, "\r\n") != NULL)
                        break;
                    if (strstr (client->refbuf->data, "\n") != NULL)
                        break;
                }
                /* stream_offset refers to the start of any data sent after the
                 * http style headers, we don't want to lose those */
Marvin Scholz's avatar
Marvin Scholz committed
556 557
                ptr = strstr(client->refbuf->data, "\r\r\n\r\r\n");
                if (ptr) {
558 559 560
                    node->stream_offset = (ptr+6) - client->refbuf->data;
                    break;
                }
Marvin Scholz's avatar
Marvin Scholz committed
561 562
                ptr = strstr(client->refbuf->data, "\r\n\r\n");
                if (ptr) {
563 564 565
                    node->stream_offset = (ptr+4) - client->refbuf->data;
                    break;
                }
Marvin Scholz's avatar
Marvin Scholz committed
566 567
                ptr = strstr(client->refbuf->data, "\n\n");
                if (ptr) {
568 569 570 571 572
                    node->stream_offset = (ptr+2) - client->refbuf->data;
                    break;
                }
                pass_it = 0;
            } while (0);
Jack Moffitt's avatar
Jack Moffitt committed
573

Marvin Scholz's avatar
Marvin Scholz committed
574
            if (pass_it) {
575 576 577 578
                if ((client_queue_t **)_req_queue_tail == &(node->next))
                    _req_queue_tail = (volatile client_queue_t **)node_ref;
                *node_ref = node->next;
                node->next = NULL;
Marvin Scholz's avatar
Marvin Scholz committed
579
                _add_connection(node);
580
                continue;
581
            }
Marvin Scholz's avatar
Marvin Scholz committed
582 583
        } else {
            if (len == 0 || client->con->error) {
584 585 586
                if ((client_queue_t **)_req_queue_tail == &node->next)
                    _req_queue_tail = (volatile client_queue_t **)node_ref;
                *node_ref = node->next;
Marvin Scholz's avatar
Marvin Scholz committed
587 588
                client_destroy(client);
                free(node);
589 590 591 592
                continue;
            }
        }
        node_ref = &node->next;
593
    }
594
    _handle_connection();
Jack Moffitt's avatar
Jack Moffitt committed
595 596
}

597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
/* add client to body queue.
 */
static void _add_body_client(client_queue_t *node)
{
    ICECAST_LOG_DEBUG("Putting client %p in body queue.", node->client);

    thread_spin_lock(&_connection_lock);
    *_body_queue_tail = node;
    _body_queue_tail = (volatile client_queue_t **) &node->next;
    thread_spin_unlock(&_connection_lock);
}


/* This queue reads data from the body of clients. */
static void process_request_body_queue (void)
{
    client_queue_t **node_ref = (client_queue_t **)&_body_queue;

    ICECAST_LOG_DEBUG("Processing body queue.");

    ICECAST_LOG_DEBUG("_body_queue=%p, &_body_queue=%p, _body_queue_tail=%p", _body_queue, &_body_queue, _body_queue_tail);

    while (*node_ref) {
        client_queue_t *node = *node_ref;
        client_t *client = node->client;
        client_slurp_result_t res;

        ICECAST_LOG_DEBUG("Got client %p in body queue.", client);

        res = client_body_skip(client);

        if (res != CLIENT_SLURP_NEEDS_MORE_DATA) {
            ICECAST_LOG_DEBUG("Putting client %p back in connection queue.", client);

            if ((client_queue_t **)_body_queue_tail == &(node->next))
                _body_queue_tail = (volatile client_queue_t **)node_ref;
            *node_ref = node->next;
            node->next = NULL;
            _add_connection(node);
            continue;
        }
        node_ref = &node->next;
    }
}
641

642 643 644
/* add node to the queue of requests. This is where the clients are when
 * initial http details are read.
 */
Marvin Scholz's avatar
Marvin Scholz committed
645
static void _add_request_queue(client_queue_t *node)
646 647 648
{
    *_req_queue_tail = node;
    _req_queue_tail = (volatile client_queue_t **)&node->next;
Jack Moffitt's avatar
Jack Moffitt committed
649 650
}

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
static client_queue_t *create_client_node(client_t *client)
{
    client_queue_t *node = calloc (1, sizeof (client_queue_t));
    ice_config_t *config;
    listener_t *listener;

    if (!node)
        return NULL;

    node->client = client;

    config = config_get_config();
    listener = config_get_listen_sock(config, client->con);

    if (listener) {
        if (listener->shoutcast_compat)
            node->shoutcast = 1;
668 669
        client->con->tlsmode = listener->tls;
        if (listener->tls == ICECAST_TLSMODE_RFC2818 && tls_ok)
670
            connection_uses_tls(client->con);
671 672 673 674 675 676 677 678
        if (listener->shoutcast_mount)
            node->shoutcast_mount = strdup(listener->shoutcast_mount);
    }

    config_release_config();

    return node;
}
679

680 681 682 683 684 685 686 687
void connection_queue(connection_t *con)
{
    client_queue_t *node;
    client_t *client = NULL;

    global_lock();
    if (client_create(&client, con, NULL) < 0) {
        global_unlock();
688
        client_send_error_by_id(client, ICECAST_ERROR_GEN_CLIENT_LIMIT);
689 690 691 692 693 694 695 696 697 698
        /* don't be too eager as this is an imposed hard limit */
        thread_sleep(400000);
        return;
    }

    /* setup client for reading incoming http */
    client->refbuf->data[PER_CLIENT_REFBUF_SIZE-1] = '\000';

    if (sock_set_blocking(client->con->sock, 0) || sock_set_nodelay(client->con->sock)) {
        global_unlock();
699
        ICECAST_LOG_WARN("Failed to set tcp options on client connection, dropping");
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
        client_destroy(client);
        return;
    }
    node = create_client_node(client);
    global_unlock();

    if (node == NULL) {
        client_destroy(client);
        return;
    }

    _add_request_queue(node);
    stats_event_inc(NULL, "connections");
}

Marvin Scholz's avatar
Marvin Scholz committed
715
void connection_accept_loop(void)
Jack Moffitt's avatar
Jack Moffitt committed
716
{
717
    connection_t *con;
718
    ice_config_t *config;
719
    int duration = 300;
720

Marvin Scholz's avatar
Marvin Scholz committed
721
    config = config_get_config();
722
    get_tls_certificate(config);
Marvin Scholz's avatar
Marvin Scholz committed
723
    config_release_config();
Jack Moffitt's avatar
Jack Moffitt committed
724

Marvin Scholz's avatar
Marvin Scholz committed
725
    while (global.running == ICECAST_RUNNING) {
726
        con = _accept_connection (duration);
727

Marvin Scholz's avatar
Marvin Scholz committed
728
        if (con) {
729
            connection_queue(con);
730
            duration = 5;
Marvin Scholz's avatar
Marvin Scholz committed
731
        } else {
732 733
            if (_req_queue == NULL)
                duration = 300; /* use longer timeouts when nothing waiting */
734
        }
Marvin Scholz's avatar
Marvin Scholz committed
735
        process_request_queue();
736
        process_request_body_queue();
737
    }
Jack Moffitt's avatar
Jack Moffitt committed
738

739 740 741
    /* Give all the other threads notification to shut down */
    thread_cond_broadcast(&global.shutdown_cond);

742 743 744
    /* wait for all the sources to shutdown */
    thread_rwlock_wlock(&_source_shutdown_rwlock);
    thread_rwlock_unlock(&_source_shutdown_rwlock);
Jack Moffitt's avatar
Jack Moffitt committed
745 746
}

747 748 749

/* Called when activating a source. Verifies that the source count is not
 * exceeded and applies any initial parameters.
750
 */
Marvin Scholz's avatar
Marvin Scholz committed
751
int connection_complete_source(source_t *source, int response)
752
{
753
    ice_config_t *config;
754

Marvin Scholz's avatar
Marvin Scholz committed
755
    global_lock();
756
    ICECAST_LOG_DEBUG("sources count is %d", global.sources);
757

758
    config = config_get_config();
Marvin Scholz's avatar
Marvin Scholz committed
759
    if (global.sources < config->source_limit) {
760
        const char *contenttype;
761
        mount_proxy *mountinfo;
762 763 764 765
        format_type_t format_type;

        /* setup format handler */
        contenttype = httpp_getvar (source->parser, "content-type");
Marvin Scholz's avatar
Marvin Scholz committed
766 767
        if (contenttype != NULL) {
            format_type = format_get_type(contenttype);
768

Marvin Scholz's avatar
Marvin Scholz committed
769
            if (format_type == FORMAT_ERROR) {
770
                config_release_config();
771
                global_unlock();
772
                if (response) {
773
                    client_send_error_by_id(source->client, ICECAST_ERROR_CON_CONTENT_TYPE_NOSYS);
774 775
                    source->client = NULL;
                }
776
                ICECAST_LOG_WARN("Content-type \"%s\" not supported, dropping source", contenttype);
777 778
                return -1;
            }
779 780 781 782
        } else if (source->parser->req_type == httpp_req_put) {
            config_release_config();
            global_unlock();
            if (response) {
783
                client_send_error_by_id(source->client, ICECAST_ERROR_CON_NO_CONTENT_TYPE_GIVEN);
784 785 786 787 788 789 790 791
                source->client = NULL;
            }
            ICECAST_LOG_ERROR("Content-type not given in PUT request, dropping source");
            return -1;
        } else {
            ICECAST_LOG_ERROR("No content-type header, falling back to backwards compatibility mode "
                    "for icecast 1.x relays. Assuming content is mp3. This behaviour is deprecated "
                    "and the source client will NOT work with future Icecast versions!");
792
            format_type = FORMAT_TYPE_GENERIC;
793 794
        }

Marvin Scholz's avatar
Marvin Scholz committed
795
        if (format_get_plugin (format_type, source) < 0) {
796 797
            global_unlock();
            config_release_config();
Marvin Scholz's avatar
Marvin Scholz committed
798
            if (response) {
799
                client_send_error_by_id(source->client, ICECAST_ERROR_CON_INTERNAL_FORMAT_ALLOC_ERROR);
800 801
                source->client = NULL;
            }
802
            ICECAST_LOG_WARN("plugin format failed for \"%s\"", source->mount);
803 804 805
            return -1;
        }

806
        global.sources++;
Marvin Scholz's avatar
Marvin Scholz committed
807
        stats_event_args(NULL, "sources", "%d", global.sources);
808
        global_unlock();
809

810
        source->running = 1;
Marvin Scholz's avatar
Marvin Scholz committed
811 812
        mountinfo = config_find_mount(config, source->mount, MOUNT_TYPE_NORMAL);
        source_update_settings(config, source, mountinfo);
813
        config_release_config();
814
        slave_rebuild_mounts();
815 816

        source->shutdown_rwlock = &_source_shutdown_rwlock;
817
        ICECAST_LOG_DEBUG("source is ready to start");
818 819 820

        return 0;
    }
821
    ICECAST_LOG_WARN("Request to add source when maximum source limit "
Marvin Scholz's avatar
Marvin Scholz committed
822
        "reached %d", global.sources);
823 824 825 826

    global_unlock();
    config_release_config();

Marvin Scholz's avatar
Marvin Scholz committed
827
    if (response) {
828
        client_send_error_by_id(source->client, ICECAST_ERROR_CON_SOURCE_CLIENT_LIMIT);
829 830
        source->client = NULL;
    }
831 832 833 834

    return -1;
}

Marvin Scholz's avatar
Marvin Scholz committed
835
static inline void source_startup(client_t *client, const char *uri)
836 837
{
    source_t *source;
Marvin Scholz's avatar
Marvin Scholz committed
838
    source = source_reserve(uri);
839

Marvin Scholz's avatar
Marvin Scholz committed
840
    if (source) {
841
        source->client = client;
842 843
        source->parser = client->parser;
        source->con = client->con;
Marvin Scholz's avatar
Marvin Scholz committed
844 845 846
        if (connection_complete_source(source, 1) < 0) {
            source_clear_source(source);
            source_free_source(source);
847 848 849
            return;
        }
        client->respcode = 200;
Philipp Schafft's avatar
Philipp Schafft committed
850 851 852 853
        if (client->protocol == ICECAST_PROTOCOL_SHOUTCAST) {
            client->respcode = 200;
            /* send this non-blocking but if there is only a partial write
             * then leave to header timeout */
854
            client_send_bytes(client, "OK2\r\nicy-caps:11\r\n\r\n", 20); /* TODO: Replace Magic Number! */
855
            source->shoutcast_compat = 1;
Marvin Scholz's avatar
Marvin Scholz committed
856
            source_client_callback(client, source);
Philipp Schafft's avatar
Philipp Schafft committed
857
        } else {
Marvin Scholz's avatar
Marvin Scholz committed
858
            refbuf_t *ok = refbuf_new(PER_CLIENT_REFBUF_SIZE);
859
            const char *expectcontinue;
860
            const char *transfer_encoding;
861
            int status_to_send = 200;
862
            ssize_t ret;
863

864 865 866 867
            transfer_encoding = httpp_getvar(source->parser, "transfer-encoding");
            if (transfer_encoding && strcasecmp(transfer_encoding, HTTPP_ENCODING_IDENTITY) != 0) {
                client->encoding = httpp_encoding_new(transfer_encoding);
                if (!client->encoding) {
868
                    client_send_error_by_id(client, ICECAST_ERROR_CON_UNIMPLEMENTED);
869 870 871 872
                    return;
                }
            }

873 874 875 876 877 878 879
            /* For PUT support we check for 100-continue and send back a 100 to stay in spec */
            expectcontinue = httpp_getvar (source->parser, "expect");

            if (expectcontinue != NULL) {
#ifdef HAVE_STRCASESTR
                if (strcasestr (expectcontinue, "100-continue") != NULL)
#else
880
                ICECAST_LOG_WARN("OS doesn't support case insensitive substring checks...");
881 882 883 884 885 886 887
                if (strstr (expectcontinue, "100-continue") != NULL)
#endif
                {
                    status_to_send = 100;
                }
            }

888
            client->respcode = 200;
889 890
            ret = util_http_build_header(ok->data, PER_CLIENT_REFBUF_SIZE, 0, 0, status_to_send, NULL, NULL, NULL, NULL, NULL, client);
            snprintf(ok->data + ret, PER_CLIENT_REFBUF_SIZE - ret, "Content-Length: 0\r\n\r\n");
Marvin Scholz's avatar
Marvin Scholz committed
891
            ok->len = strlen(ok->data);
892 893 894
            /* we may have unprocessed data read in, so don't overwrite it */
            ok->associated = client->refbuf;
            client->refbuf = ok;
Marvin Scholz's avatar
Marvin Scholz committed
895
            fserve_add_client_callback(client, source_client_callback, source);
896
        }
Marvin Scholz's avatar
Marvin Scholz committed
897
    } else {
898
        client_send_error_by_id(client, ICECAST_ERROR_CON_MOUNT_IN_USE);
899
        ICECAST_LOG_WARN("Mountpoint %s in use", uri);
900
    }
901 902
}

Philipp Schafft's avatar
Philipp Schafft committed
903
/* only called for native icecast source clients */
Marvin Scholz's avatar
Marvin Scholz committed
904
static void _handle_source_request(client_t *client, const char *uri)
Jack Moffitt's avatar
Jack Moffitt committed
905
{
906 907
    ICECAST_LOG_INFO("Source logging in at mountpoint \"%s\" from %s as role %s",
        uri, client->con->ip, client->role);
908

Marvin Scholz's avatar
Marvin Scholz committed
909
    if (uri[0] != '/') {
Philipp Schafft's avatar
Philipp Schafft committed
910
        ICECAST_LOG_WARN("source mountpoint not starting with /");
911
        client_send_error_by_id(client, ICECAST_ERROR_CON_MOUNTPOINT_NOT_STARTING_WITH_SLASH);
912
        return;
913
    }
914

Philipp Schafft's avatar
Philipp Schafft committed
915 916 917 918
    source_startup(client, uri);
}


Marvin Scholz's avatar
Marvin Scholz committed
919
static void _handle_stats_request(client_t *client, char *uri)
Philipp Schafft's avatar
Philipp Schafft committed
920 921 922
{
    stats_event_inc(NULL, "stats_connections");

923
    client->respcode = 200;
924
    snprintf (client->refbuf->data, PER_CLIENT_REFBUF_SIZE,
Marvin Scholz's avatar
Marvin Scholz committed
925 926 927
        "HTTP/1.0 200 OK\r\n\r\n");
    client->refbuf->len = strlen(client->refbuf->data);
    fserve_add_client_callback(client, stats_callback, NULL);
928 929
}

Philipp Schafft's avatar
Philipp Schafft committed
930 931 932
/* if 0 is returned then the client should not be touched, however if -1
 * is returned then the caller is responsible for handling the client
 */
Marvin Scholz's avatar
Marvin Scholz committed
933
static int __add_listener_to_source(source_t *source, client_t *client)
934
{
Philipp Schafft's avatar
Philipp Schafft committed
935
    size_t loop = 10;
936

Marvin Scholz's avatar
Marvin Scholz committed
937
    do {
Philipp Schafft's avatar
Philipp Schafft committed
938
        ICECAST_LOG_DEBUG("max on %s is %ld (cur %lu)", source->mount,
Marvin Scholz's avatar
Marvin Scholz committed
939
            source->max_listeners, source->listeners);
Philipp Schafft's avatar
Philipp Schafft committed
940 941 942 943
        if (source->max_listeners == -1)
            break;
        if (source->listeners < (unsigned long)source->max_listeners)
            break;
944

Marvin Scholz's avatar
Marvin Scholz committed
945
        if (loop && source->fallback_when_full && source->fallback_mount) {
Philipp Schafft's avatar
Philipp Schafft committed
946 947 948
            source_t *next = source_find_mount (source->fallback_mount);
            if (!next) {
                ICECAST_LOG_ERROR("Fallback '%s' for full source '%s' not found",
Marvin Scholz's avatar
Marvin Scholz committed
949
                    source->mount, source->fallback_mount);
Philipp Schafft's avatar
Philipp Schafft committed
950 951
                return -1;
            }
952
            ICECAST_LOG_INFO("stream full, trying %s", next->mount);
Philipp Schafft's avatar
Philipp Schafft committed
953 954 955 956 957 958 959 960 961 962 963
            source = next;
            loop--;
            continue;
        }
        /* now we fail the client */
        return -1;
    } while (1);

    client->write_to_client = format_generic_write_to_client;
    client->check_buffer = format_check_http_buffer;
    client->refbuf->len = PER_CLIENT_REFBUF_SIZE;
Marvin Scholz's avatar
Marvin Scholz committed
964
    memset(client->refbuf->data, 0, PER_CLIENT_REFBUF_SIZE);
Philipp Schafft's avatar
Philipp Schafft committed
965 966

    /* lets add the client to the active list */
Marvin Scholz's avatar
Marvin Scholz committed
967 968 969
    avl_tree_wlock(source->pending_tree);
    avl_insert(source->pending_tree, client);
    avl_tree_unlock(source->pending_tree);
Philipp Schafft's avatar
Philipp Schafft committed
970

Marvin Scholz's avatar
Marvin Scholz committed
971
    if (source->running == 0 && source->on_demand) {
Philipp Schafft's avatar
Philipp Schafft committed
972 973 974
        /* enable on-demand relay to start, wake up the slave thread */
        ICECAST_LOG_DEBUG("kicking off on-demand relay");
        source->on_demand_req = 1;
975
    }
Philipp Schafft's avatar
Philipp Schafft committed
976 977 978
    ICECAST_LOG_DEBUG("Added client to %s", source->mount);
    return 0;
}
979

Philipp Schafft's avatar
Philipp Schafft committed
980 981 982 983 984 985 986 987 988 989 990 991
/* count the number of clients on a mount with same username and same role as the given one */
static inline ssize_t __count_user_role_on_mount (source_t *source, client_t *client) {
    ssize_t ret = 0;
    avl_node *node;

    avl_tree_rlock(source->client_tree);
    node = avl_get_first(source->client_tree);
    while (node) {
        client_t *existing_client = (client_t *)node->key;
        if (existing_client->username && client->username &&
            strcmp(existing_client->username, client->username) == 0 &&
            existing_client->role && client->role &&
Marvin Scholz's avatar
Marvin Scholz committed
992
            strcmp(existing_client->role, client->role) == 0) {
Philipp Schafft's avatar
Philipp Schafft committed
993
            ret++;
Marvin Scholz's avatar
Marvin Scholz committed
994
        }
Philipp Schafft's avatar
Philipp Schafft committed
995
        node = avl_get_next(node);
996
    }
Philipp Schafft's avatar
Philipp Schafft committed
997 998 999 1000
    avl_tree_unlock(source->client_tree);

    avl_tree_rlock(source->pending_tree);
    node = avl_get_first(source->pending_tree);
Marvin Scholz's avatar
Marvin Scholz committed
1001
    while (node) {
Philipp Schafft's avatar
Philipp Schafft committed
1002 1003 1004 1005
        client_t *existing_client = (client_t *)node->key;
        if (existing_client->username && client->username &&
            strcmp(existing_client->username, client->username) == 0 &&
            existing_client->role && client->role &&
Marvin Scholz's avatar
Marvin Scholz committed
1006
            strcmp(existing_client->role, client->role) == 0){
Philipp Schafft's avatar
Philipp Schafft committed
1007
            ret++;
Marvin Scholz's avatar
Marvin Scholz committed
1008
        }
Philipp Schafft's avatar
Philipp Schafft committed
1009
        node = avl_get_next(node);
1010
    }
Philipp Schafft's avatar
Philipp Schafft committed
1011 1012 1013 1014
    avl_tree_unlock(source->pending_tree);
    return ret;
}

Marvin Scholz's avatar
Marvin Scholz committed
1015
static void _handle_get_request(client_t *client, char *uri) {
Philipp Schafft's avatar
Philipp Schafft committed
1016 1017 1018 1019 1020 1021 1022 1023 1024
    source_t *source = NULL;

    ICECAST_LOG_DEBUG("Got client %p with URI %H", client, uri);

    /* there are several types of HTTP GET clients
     * media clients, which are looking for a source (eg, URI = /stream.ogg),
     * stats clients, which are looking for /admin/stats.xml and
     * fserve clients, which are looking for static files.
     */
1025 1026

    stats_event_inc(NULL, "client_connections");
1027

Philipp Schafft's avatar
Philipp Schafft committed
1028 1029 1030 1031 1032 1033
    /* this is a web/ request. let's check if we are allowed to do that. */
    if (acl_test_web(client->acl) != ACL_POLICY_ALLOW) {
        /* doesn't seem so, sad client :( */
        if (client->protocol == ICECAST_PROTOCOL_SHOUTCAST) {
            client_destroy(client);
        } else {
1034
            client_send_error_by_id(client, ICECAST_ERROR_GEN_CLIENT_NEEDS_TO_AUTHENTICATE);
Philipp Schafft's avatar
Philipp Schafft committed
1035 1036 1037 1038
        }
        return;
    }

1039 1040 1041 1042 1043
    if (client->parser->req_type == httpp_req_options) {
        client_send_204(client);
        return;
    }

Philipp Schafft's avatar
Philipp Schafft committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    if (util_check_valid_extension(uri) == XSLT_CONTENT) {
        /* If the file exists, then transform it, otherwise, write a 404 */
        ICECAST_LOG_DEBUG("Stats request, sending XSL transformed stats");
        stats_transform_xslt(client, uri);
        return;
    }

    avl_tree_rlock(global.source_tree);
    /* let's see if this is a source or just a random fserve file */
    source = source_find_mount(uri);
    if (source) {
        /* true mount */
        int in_error = 0;
        ssize_t max_connections_per_user = acl_get_max_connections_per_user(client->acl);
        /* check for duplicate_logins */
        if (max_connections_per_user > 0) { /* -1 = not set (-> default=unlimited), 0 = unlimited */
            if (max_connections_per_user <= __count_user_role_on_mount(source, client)) {
1061
                client_send_error_by_id(client, ICECAST_ERROR_CON_PER_CRED_CLIENT_LIMIT);
Philipp Schafft's avatar
Philipp Schafft committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
                in_error = 1;
            }
        }


        /* Set max listening duration in case not already set. */
        if (!in_error && client->con->discon_time == 0) {
            time_t connection_duration = acl_get_max_connection_duration(client->acl);
            if (connection_duration == -1) {
                ice_config_t *config = config_get_config();
                mount_proxy *mount = config_find_mount(config, source->mount, MOUNT_TYPE_NORMAL);
                if (mount && mount->max_listener_duration)
                    connection_duration = mount->max_listener_duration;
                config_release_config();
            }

            if (connection_duration > 0) /* -1 = not set (-> default=unlimited), 0 = unlimited */
                client->con->discon_time = connection_duration + time(NULL);
        }
        if (!in_error && __add_listener_to_source(source, client) == -1) {
1082
            client_send_error_by_id(client, ICECAST_ERROR_CON_rejecting_client_for_whatever_reason);
Philipp Schafft's avatar
Philipp Schafft committed
1083 1084 1085 1086 1087 1088 1089
        }
        avl_tree_unlock(global.source_tree);
    } else {
        /* file */
        avl_tree_unlock(global.source_tree);
        fserve_client_create(client, uri);
    }
1090 1091
}

Marvin Scholz's avatar
Marvin Scholz committed
1092
static void _handle_shoutcast_compatible(client_queue_t *node)
1093
{
1094 1095 1096
    char *http_compliant;
    int http_compliant_len = 0;
    http_parser_t *parser;
Philipp Schafft's avatar
Philipp Schafft committed
1097
    const char *shoutcast_mount;
1098
    client_t *client = node->client;
Philipp Schafft's avatar
Philipp Schafft committed
1099
    ice_config_t *config;
1100

1101 1102
    if (node->shoutcast == 1)
    {
Philipp Schafft's avatar
Philipp Schafft committed
1103
        char *ptr, *headers;
1104 1105

        /* Get rid of trailing \r\n or \n after password */
Marvin Scholz's avatar
Marvin Scholz committed
1106 1107
        ptr = strstr(client->refbuf->data, "\r\r\n");
        if (ptr) {
1108
            headers = ptr+3;
Marvin Scholz's avatar
Marvin Scholz committed
1109 1110 1111
        } else {
            ptr = strstr(client->refbuf->data, "\r\n");
            if (ptr) {
1112
                headers = ptr+2;
Marvin Scholz's avatar
Marvin Scholz committed
1113 1114
            } else {
                ptr = strstr(client->refbuf->data, "\n");
1115 1116 1117
                if (ptr)
                    headers = ptr+1;
            }
1118
        }
1119

Marvin Scholz's avatar
Marvin Scholz committed
1120 1121 1122 1123
        if (ptr == NULL){
            client_destroy(client);
            free(node->shoutcast_mount);
            free(node);
1124 1125 1126 1127
            return;
        }
        *ptr = '\0';

Philipp Schafft's avatar
Philipp Schafft committed
1128
        client->password = strdup(client->refbuf->data);
1129 1130 1131
        config = config_get_config();
        client->username = strdup(config->shoutcast_user);
        config_release_config();
Philipp Schafft's avatar
Philipp Schafft committed
1132
        node->offset -= (headers - client->refbuf->data);
Marvin Scholz's avatar
Marvin Scholz committed
1133
        memmove(client->refbuf->data, headers, node->offset+1);
Philipp Schafft's avatar
Philipp Schafft committed
1134 1135
        node->shoutcast = 2;
        /* we've checked the password, now send it back for reading headers */
Marvin Scholz's avatar
Marvin Scholz committed
1136
        _add_request_queue(node);
1137 1138
        return;
    }
1139
    /* actually make a copy as we are dropping the config lock */
1140 1141 1142
    /* Here we create a valid HTTP request based of the information
       that was passed in via the non-HTTP style protocol above. This
       means we can use some of our existing code to handle this case */
Philipp Schafft's avatar
Philipp Schafft committed
1143 1144 1145 1146 1147 1148 1149
    config = config_get_config();
    if (node->shoutcast_mount) {
        shoutcast_mount = node->shoutcast_mount;
    } else {
        shoutcast_mount = config->shoutcast_mount;
    }
    http_compliant_len = 20 + strlen(shoutcast_mount) + node->offset;
1150
    http_compliant = (char *)calloc(1, http_compliant_len);
Philipp Schafft's avatar
Philipp Schafft committed
1151
    snprintf(http_compliant, http_compliant_len,
1152
            "SOURCE %s HTTP/1.0\r\n%s", shoutcast_mount, client->refbuf->data);
Philipp Schafft's avatar
Philipp Schafft committed
1153 1154
    config_release_config();

1155 1156
    parser = httpp_create_parser();
    httpp_initialize(parser, NULL);
Marvin Scholz's avatar
Marvin Scholz committed
1157
    if (httpp_parse(parser, http_compliant, strlen(http_compliant))) {
1158
        /* we may have more than just headers, so prepare for it */
1159 1160
        if (node->stream_offset != node->offset) {
            connection_read_put_back(client->con, client->refbuf->data + node->stream_offset, node->offset - node->stream_offset);
1161
            node->offset = node->stream_offset;