connection.c 50.2 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 12
 * Copyright 2011,      Dave 'justdave' Miller <justdave@mozilla.com>,
 * Copyright 2011-2014, 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 28
#include <sys/types.h>
#include <sys/stat.h>
29 30

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

37
#include "compat.h"
Jack Moffitt's avatar
Jack Moffitt committed
38

Marvin Scholz's avatar
Marvin Scholz committed
39 40 41 42
#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
43

44
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
45 46 47 48 49 50 51
#include "global.h"
#include "util.h"
#include "connection.h"
#include "refbuf.h"
#include "client.h"
#include "stats.h"
#include "logging.h"
52
#include "xslt.h"
53
#include "fserve.h"
54
#include "sighandler.h"
55 56

#include "yp.h"
Jack Moffitt's avatar
Jack Moffitt committed
57
#include "source.h"
58
#include "format.h"
59
#include "format_mp3.h"
60
#include "admin.h"
Michael Smith's avatar
Michael Smith committed
61
#include "auth.h"
Jack Moffitt's avatar
Jack Moffitt committed
62 63 64

#define CATMODULE "connection"

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/* 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.
*/

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

typedef struct _thread_queue_tag {
90 91
    thread_type *thread_id;
    struct _thread_queue_tag *next;
Jack Moffitt's avatar
Jack Moffitt committed
92 93
} thread_queue_t;

94 95 96 97 98 99 100 101
typedef struct
{
    char *filename;
    time_t file_recheck;
    time_t file_mtime;
    avl_tree *contents;
} cache_file_contents;

102
static spin_t _connection_lock; // protects _current_id, _con_queue, _con_queue_tail
103
static volatile unsigned long _current_id = 0;
Jack Moffitt's avatar
Jack Moffitt committed
104 105
static int _initialized = 0;

106 107
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;
108 109 110 111 112
static int ssl_ok;
#ifdef HAVE_OPENSSL
static SSL_CTX *ssl_ctx;
#endif

113
/* filtering client connection based on IP */
114
static cache_file_contents banned_ip, allowed_ip;
115

116
rwlock_t _source_shutdown_rwlock;
Jack Moffitt's avatar
Jack Moffitt committed
117

118
static void _handle_connection(void);
Jack Moffitt's avatar
Jack Moffitt committed
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
static int compare_ip (void *arg, void *a, void *b)
{
    const char *ip = (const char *)a;
    const char *pattern = (const char *)b;

    return strcmp (pattern, ip);
}


static int free_filtered_ip (void*x)
{
    free (x);
    return 1;
}


Jack Moffitt's avatar
Jack Moffitt committed
136 137
void connection_initialize(void)
{
Marvin Scholz's avatar
Marvin Scholz committed
138 139
    if (_initialized)
        return;
140

141
    thread_spin_create (&_connection_lock);
142
    thread_mutex_create(&move_clients_mutex);
143
    thread_rwlock_create(&_source_shutdown_rwlock);
144
    thread_cond_create(&global.shutdown_cond);
145 146 147 148
    _req_queue = NULL;
    _req_queue_tail = &_req_queue;
    _con_queue = NULL;
    _con_queue_tail = &_con_queue;
Jack Moffitt's avatar
Jack Moffitt committed
149

150 151 152 153 154 155
    banned_ip.contents = NULL;
    banned_ip.file_mtime = 0;

    allowed_ip.contents = NULL;
    allowed_ip.file_mtime = 0;

156
    _initialized = 1;
Jack Moffitt's avatar
Jack Moffitt committed
157 158 159 160
}

void connection_shutdown(void)
{
Marvin Scholz's avatar
Marvin Scholz committed
161 162
    if (!_initialized)
        return;
163

164 165 166
#ifdef HAVE_OPENSSL
    SSL_CTX_free (ssl_ctx);
#endif
167 168
    if (banned_ip.contents)  avl_tree_free (banned_ip.contents, free_filtered_ip);
    if (allowed_ip.contents) avl_tree_free (allowed_ip.contents, free_filtered_ip);
169

170
    thread_cond_destroy(&global.shutdown_cond);
171
    thread_rwlock_destroy(&_source_shutdown_rwlock);
172
    thread_spin_destroy (&_connection_lock);
173
    thread_mutex_destroy(&move_clients_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
174

175
    _initialized = 0;
Jack Moffitt's avatar
Jack Moffitt committed
176 177 178 179
}

static unsigned long _next_connection_id(void)
{
180
    unsigned long id;
Jack Moffitt's avatar
Jack Moffitt committed
181

182
    thread_spin_lock(&_connection_lock);
183
    id = _current_id++;
184
    thread_spin_unlock(&_connection_lock);
Jack Moffitt's avatar
Jack Moffitt committed
185

186
    return id;
Jack Moffitt's avatar
Jack Moffitt committed
187 188
}

189 190

#ifdef HAVE_OPENSSL
Marvin Scholz's avatar
Marvin Scholz committed
191
static void get_ssl_certificate(ice_config_t *config)
192
{
193
    SSL_METHOD *method;
194
    long ssl_opts;
195
    config->tls_ok = ssl_ok = 0;
196

197 198
    SSL_load_error_strings(); /* readable error messages */
    SSL_library_init(); /* initialize library */
199 200

    method = SSLv23_server_method();
Marvin Scholz's avatar
Marvin Scholz committed
201 202
    ssl_ctx = SSL_CTX_new(method);
    ssl_opts = SSL_CTX_get_options(ssl_ctx);
203
#ifdef SSL_OP_NO_COMPRESSION
Marvin Scholz's avatar
Marvin Scholz committed
204
    SSL_CTX_set_options(ssl_ctx, ssl_opts|SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_COMPRESSION);
205
#else
Marvin Scholz's avatar
Marvin Scholz committed
206
    SSL_CTX_set_options(ssl_ctx, ssl_opts|SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3);
207
#endif
208

Marvin Scholz's avatar
Marvin Scholz committed
209
    do {
210 211
        if (config->cert_file == NULL)
            break;
Marvin Scholz's avatar
Marvin Scholz committed
212
        if (SSL_CTX_use_certificate_chain_file (ssl_ctx, config->cert_file) <= 0) {
213
            ICECAST_LOG_WARN("Invalid cert file %s", config->cert_file);
214 215
            break;
        }
Marvin Scholz's avatar
Marvin Scholz committed
216
        if (SSL_CTX_use_PrivateKey_file (ssl_ctx, config->cert_file, SSL_FILETYPE_PEM) <= 0) {
217
            ICECAST_LOG_WARN("Invalid private key file %s", config->cert_file);
218 219
            break;
        }
Marvin Scholz's avatar
Marvin Scholz committed
220
        if (!SSL_CTX_check_private_key (ssl_ctx)) {
221
            ICECAST_LOG_ERROR("Invalid %s - Private key does not match cert public key", config->cert_file);
222 223
            break;
        }
Marvin Scholz's avatar
Marvin Scholz committed
224
        if (SSL_CTX_set_cipher_list(ssl_ctx, config->cipher_list) <= 0) {
225 226
            ICECAST_LOG_WARN("Invalid cipher list: %s", config->cipher_list);
        }
227
        config->tls_ok = ssl_ok = 1;
228 229
        ICECAST_LOG_INFO("Certificate found at %s", config->cert_file);
        ICECAST_LOG_INFO("Using ciphers %s", config->cipher_list);
230
        return;
231
    } while (0);
232
    ICECAST_LOG_INFO("No TLS capability on any configured ports");
233 234 235 236 237 238
}


/* handlers for reading and writing a connection_t when there is ssl
 * configured on the listening port
 */
Marvin Scholz's avatar
Marvin Scholz committed
239
static int connection_read_ssl(connection_t *con, void *buf, size_t len)
240
{
Marvin Scholz's avatar
Marvin Scholz committed
241
    int bytes = SSL_read(con->ssl, buf, len);
242

Marvin Scholz's avatar
Marvin Scholz committed
243 244
    if (bytes < 0) {
        switch (SSL_get_error(con->ssl, bytes)) {
245 246
            case SSL_ERROR_WANT_READ:
            case SSL_ERROR_WANT_WRITE:
247
            return -1;
248 249 250 251 252 253
        }
        con->error = 1;
    }
    return bytes;
}

Marvin Scholz's avatar
Marvin Scholz committed
254
static int connection_send_ssl(connection_t *con, const void *buf, size_t len)
255 256 257
{
    int bytes = SSL_write (con->ssl, buf, len);

Marvin Scholz's avatar
Marvin Scholz committed
258 259
    if (bytes < 0) {
        switch (SSL_get_error(con->ssl, bytes)){
260 261 262 263 264
            case SSL_ERROR_WANT_READ:
            case SSL_ERROR_WANT_WRITE:
                return -1;
        }
        con->error = 1;
Marvin Scholz's avatar
Marvin Scholz committed
265
    } else {
266
        con->sent_bytes += bytes;
Marvin Scholz's avatar
Marvin Scholz committed
267
    }
268 269 270 271 272
    return bytes;
}
#else

/* SSL not compiled in, so at least log it */
273
static void get_ssl_certificate(ice_config_t *config)
274 275
{
    ssl_ok = 0;
276 277
    ICECAST_LOG_INFO("No TLS capability. "
                     "Rebuild Icecast with openSSL support to enable this.");
278 279 280 281 282 283 284
}
#endif /* HAVE_OPENSSL */


/* 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
285
static int connection_read(connection_t *con, void *buf, size_t len)
286
{
Marvin Scholz's avatar
Marvin Scholz committed
287
    int bytes = sock_read_bytes(con->sock, buf, len);
288 289
    if (bytes == 0)
        con->error = 1;
Marvin Scholz's avatar
Marvin Scholz committed
290
    if (bytes == -1 && !sock_recoverable(sock_error()))
291 292 293 294
        con->error = 1;
    return bytes;
}

Marvin Scholz's avatar
Marvin Scholz committed
295
static int connection_send(connection_t *con, const void *buf, size_t len)
296
{
Marvin Scholz's avatar
Marvin Scholz committed
297 298 299
    int bytes = sock_write_bytes(con->sock, buf, len);
    if (bytes < 0) {
        if (!sock_recoverable(sock_error()))
300
            con->error = 1;
Marvin Scholz's avatar
Marvin Scholz committed
301
    } else {
302
        con->sent_bytes += bytes;
Marvin Scholz's avatar
Marvin Scholz committed
303
    }
304 305 306 307
    return bytes;
}


308 309 310
/* function to handle the re-populating of the avl tree containing IP addresses
 * for deciding whether a connection of an incoming request is to be dropped.
 */
311
static void recheck_ip_file(cache_file_contents *cache)
312 313
{
    time_t now = time(NULL);
Marvin Scholz's avatar
Marvin Scholz committed
314 315 316 317 318 319
    if (now >= cache->file_recheck) {
        struct      stat file_stat;
        FILE       *file             = NULL;
        int         count            = 0;
        avl_tree   *new_ips;
        char        line[MAX_LINE_LEN];
320 321

        cache->file_recheck = now + 10;
Marvin Scholz's avatar
Marvin Scholz committed
322 323
        if (cache->filename == NULL) {
            if (cache->contents) {
324 325 326 327 328
                avl_tree_free (cache->contents, free_filtered_ip);
                cache->contents = NULL;
            }
            return;
        }
Marvin Scholz's avatar
Marvin Scholz committed
329
        if (stat(cache->filename, &file_stat) < 0) {
330
            ICECAST_LOG_WARN("Failed to check status of \"%s\": %s", cache->filename, strerror(errno));
331 332 333 334 335 336 337 338
            return;
        }
        if (file_stat.st_mtime == cache->file_mtime)
            return; /* common case, no update to file */

        cache->file_mtime = file_stat.st_mtime;

        file = fopen (cache->filename, "r");
Marvin Scholz's avatar
Marvin Scholz committed
339
        if (file == NULL) {
340
            ICECAST_LOG_WARN("Failed to open file \"%s\": %s", cache->filename, strerror (errno));
341 342 343
            return;
        }

Marvin Scholz's avatar
Marvin Scholz committed
344
        new_ips = avl_tree_new(compare_ip, NULL);
345

Marvin Scholz's avatar
Marvin Scholz committed
346
        while (get_line(file, line, MAX_LINE_LEN)) {
347 348 349 350 351 352
            char *str;
            if(!line[0] || line[0] == '#')
                continue;
            count++;
            str = strdup (line);
            if (str)
Marvin Scholz's avatar
Marvin Scholz committed
353
                avl_insert(new_ips, str);
354 355
        }
        fclose (file);
356
        ICECAST_LOG_INFO("%d entries read from file \"%s\"", count, cache->filename);
357

Marvin Scholz's avatar
Marvin Scholz committed
358 359
        if (cache->contents)
            avl_tree_free(cache->contents, free_filtered_ip);
360 361 362 363 364 365
        cache->contents = new_ips;
    }
}


/* return 0 if the passed ip address is not to be handled by icecast, non-zero otherwise */
366
static int accept_ip_address(char *ip)
367 368 369
{
    void *result;

370 371
    recheck_ip_file(&banned_ip);
    recheck_ip_file(&allowed_ip);
372

Marvin Scholz's avatar
Marvin Scholz committed
373 374
    if (banned_ip.contents) {
        if (avl_get_by_key (banned_ip.contents, ip, &result) == 0) {
375
            ICECAST_LOG_DEBUG("%s is banned", ip);
376 377 378
            return 0;
        }
    }
Marvin Scholz's avatar
Marvin Scholz committed
379 380
    if (allowed_ip.contents) {
        if (avl_get_by_key (allowed_ip.contents, ip, &result) == 0) {
381
            ICECAST_LOG_DEBUG("%s is allowed", ip);
382
            return 1;
Marvin Scholz's avatar
Marvin Scholz committed
383
        } else {
384
            ICECAST_LOG_DEBUG("%s is not allowed", ip);
385 386 387 388 389 390 391
            return 0;
        }
    }
    return 1;
}


392 393
connection_t *connection_create (sock_t sock, sock_t serversock, char *ip)
{
394
    connection_t *con;
395
    con = (connection_t *)calloc(1, sizeof(connection_t));
Marvin Scholz's avatar
Marvin Scholz committed
396 397
    if (con) {
        con->sock       = sock;
398
        con->serversock = serversock;
Marvin Scholz's avatar
Marvin Scholz committed
399 400 401 402 403
        con->con_time   = time(NULL);
        con->id         = _next_connection_id();
        con->ip         = ip;
        con->read       = connection_read;
        con->send       = connection_send;
404
    }
405

406
    return con;
407 408
}

409 410
/* prepare connection for interacting over a SSL connection
 */
411
void connection_uses_ssl(connection_t *con)
412 413
{
#ifdef HAVE_OPENSSL
414 415 416
    if (con->ssl)
        return;

417 418
    con->read = connection_read_ssl;
    con->send = connection_send_ssl;
Marvin Scholz's avatar
Marvin Scholz committed
419 420 421
    con->ssl = SSL_new(ssl_ctx);
    SSL_set_accept_state(con->ssl);
    SSL_set_fd(con->ssl, con->sock);
422 423 424
#endif
}

425 426 427 428 429
ssize_t connection_read_bytes(connection_t *con, void *buf, size_t len)
{
    return con->read(con, buf, len);
}

430
static sock_t wait_for_serversock(int timeout)
431 432
{
#ifdef HAVE_POLL
433
    struct pollfd ufds [global.server_sockets];
434 435 436 437 438 439 440 441 442 443
    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) {
444
        return SOCK_ERROR;
Marvin Scholz's avatar
Marvin Scholz committed
445
    } else if(ret == 0) {
446
        return SOCK_ERROR;
Marvin Scholz's avatar
Marvin Scholz committed
447
    } else {
448
        int dst;
449
        for(i=0; i < global.server_sockets; i++) {
450
            if(ufds[i].revents & POLLIN)
451
                return ufds[i].fd;
Marvin Scholz's avatar
Marvin Scholz committed
452 453
            if(ufds[i].revents & (POLLHUP|POLLERR|POLLNVAL)) {
                if (ufds[i].revents & (POLLHUP|POLLERR)) {
454
                    sock_close (global.serversock[i]);
455
                    ICECAST_LOG_WARN("Had to close a listening socket");
456
                }
457
                global.serversock[i] = SOCK_ERROR;
458
            }
459
        }
460
        /* remove any closed sockets */
Marvin Scholz's avatar
Marvin Scholz committed
461
        for(i=0, dst=0; i < global.server_sockets; i++) {
462
            if (global.serversock[i] == SOCK_ERROR)
463
            continue;
464
            if (i!=dst)
465
            global.serversock[dst] = global.serversock[i];
466 467 468
            dst++;
        }
        global.server_sockets = dst;
469
        return SOCK_ERROR;
470 471 472 473 474
    }
#else
    fd_set rfds;
    struct timeval tv, *p=NULL;
    int i, ret;
475
    sock_t max = SOCK_ERROR;
476 477 478 479 480

    FD_ZERO(&rfds);

    for(i=0; i < global.server_sockets; i++) {
        FD_SET(global.serversock[i], &rfds);
481
        if (max == SOCK_ERROR || global.serversock[i] > max)
482 483 484 485 486
            max = global.serversock[i];
    }

    if(timeout >= 0) {
        tv.tv_sec = timeout/1000;
487
        tv.tv_usec = (timeout % 1000) * 1000;
488 489 490 491 492
        p = &tv;
    }

    ret = select(max+1, &rfds, NULL, NULL, p);
    if(ret < 0) {
493
        return SOCK_ERROR;
Marvin Scholz's avatar
Marvin Scholz committed
494
    } else if(ret == 0) {
495
        return SOCK_ERROR;
Marvin Scholz's avatar
Marvin Scholz committed
496
    } else {
497 498 499 500
        for(i=0; i < global.server_sockets; i++) {
            if(FD_ISSET(global.serversock[i], &rfds))
                return global.serversock[i];
        }
501
        return SOCK_ERROR; /* Should be impossible, stop compiler warnings */
502 503 504 505
    }
#endif
}

506
static connection_t *_accept_connection(int duration)
Jack Moffitt's avatar
Jack Moffitt committed
507
{
508
    sock_t sock, serversock;
509
    char *ip;
Jack Moffitt's avatar
Jack Moffitt committed
510

511
    serversock = wait_for_serversock (duration);
512
    if (serversock == SOCK_ERROR)
513
        return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
514

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

518
    sock = sock_accept(serversock, ip, MAX_ADDR_LEN);
Marvin Scholz's avatar
Marvin Scholz committed
519
    if (sock != SOCK_ERROR) {
520
        connection_t *con = NULL;
521
        /* Make any IPv4 mapped IPv6 address look like a normal IPv4 address */
Marvin Scholz's avatar
Marvin Scholz committed
522 523
        if (strncmp(ip, "::ffff:", 7) == 0)
            memmove(ip, ip+7, strlen (ip+7)+1);
Jack Moffitt's avatar
Jack Moffitt committed
524

Marvin Scholz's avatar
Marvin Scholz committed
525 526
        if (accept_ip_address(ip))
            con = connection_create(sock, serversock, ip);
527 528
        if (con)
            return con;
Marvin Scholz's avatar
Marvin Scholz committed
529 530 531
        sock_close(sock);
    } else {
        if (!sock_recoverable(sock_error())) {
532
            ICECAST_LOG_WARN("accept() failed with error %d: %s", sock_error(), strerror(sock_error()));
Marvin Scholz's avatar
Marvin Scholz committed
533
            thread_sleep(500000);
534
        }
535 536 537
    }
    free(ip);
    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
538 539 540
}


541 542 543 544
/* 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
 */
545
static void _add_connection(client_queue_t *node)
Jack Moffitt's avatar
Jack Moffitt committed
546
{
547
    thread_spin_lock(&_connection_lock);
548
    *_con_queue_tail = node;
549 550
    _con_queue_tail = (volatile client_queue_t **) &node->next;
    thread_spin_unlock(&_connection_lock);
Jack Moffitt's avatar
Jack Moffitt committed
551 552 553
}


554 555 556 557 558 559
/* 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
560

Marvin Scholz's avatar
Marvin Scholz committed
561
    thread_spin_lock(&_connection_lock);
562

Marvin Scholz's avatar
Marvin Scholz committed
563
    if (_con_queue){
564 565 566 567
        node = (client_queue_t *)_con_queue;
        _con_queue = node->next;
        if (_con_queue == NULL)
            _con_queue_tail = &_con_queue;
568
        node->next = NULL;
569
    }
570

Marvin Scholz's avatar
Marvin Scholz committed
571
    thread_spin_unlock(&_connection_lock);
572 573
    return node;
}
Jack Moffitt's avatar
Jack Moffitt committed
574 575


576
/* run along queue checking for any data that has come in or a timeout */
577
static void process_request_queue (void)
578 579
{
    client_queue_t **node_ref = (client_queue_t **)&_req_queue;
Marvin Scholz's avatar
Marvin Scholz committed
580
    ice_config_t *config = config_get_config();
581 582
    int timeout = config->header_timeout;
    config_release_config();
Jack Moffitt's avatar
Jack Moffitt committed
583

Marvin Scholz's avatar
Marvin Scholz committed
584
    while (*node_ref) {
585 586 587 588
        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
589

Marvin Scholz's avatar
Marvin Scholz committed
590 591
        if (len > 0) {
            if (client->con->con_time + timeout <= time(NULL)) {
592
                len = 0;
Marvin Scholz's avatar
Marvin Scholz committed
593 594 595
            } else {
                len = client_read_bytes(client, buf, len);
            }
596
        }
Jack Moffitt's avatar
Jack Moffitt committed
597

Marvin Scholz's avatar
Marvin Scholz committed
598
        if (len > 0) {
599 600 601
            int pass_it = 1;
            char *ptr;

602 603
            /* handle \n, \r\n and nsvcap which for some strange reason has
             * EOL as \r\r\n */
604
            node->offset += len;
Marvin Scholz's avatar
Marvin Scholz committed
605 606 607
            client->refbuf->data[node->offset] = '\000';
            do {
                if (node->shoutcast == 1) {
608
                    /* password line */
609 610
                    if (strstr (client->refbuf->data, "\r\r\n") != NULL)
                        break;
611 612 613 614 615 616 617
                    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
618 619
                ptr = strstr(client->refbuf->data, "\r\r\n\r\r\n");
                if (ptr) {
620 621 622
                    node->stream_offset = (ptr+6) - client->refbuf->data;
                    break;
                }
Marvin Scholz's avatar
Marvin Scholz committed
623 624
                ptr = strstr(client->refbuf->data, "\r\n\r\n");
                if (ptr) {
625 626 627
                    node->stream_offset = (ptr+4) - client->refbuf->data;
                    break;
                }
Marvin Scholz's avatar
Marvin Scholz committed
628 629
                ptr = strstr(client->refbuf->data, "\n\n");
                if (ptr) {
630 631 632 633 634
                    node->stream_offset = (ptr+2) - client->refbuf->data;
                    break;
                }
                pass_it = 0;
            } while (0);
Jack Moffitt's avatar
Jack Moffitt committed
635

Marvin Scholz's avatar
Marvin Scholz committed
636
            if (pass_it) {
637 638 639 640
                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
641
                _add_connection(node);
642
                continue;
643
            }
Marvin Scholz's avatar
Marvin Scholz committed
644 645
        } else {
            if (len == 0 || client->con->error) {
646 647 648
                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
649 650
                client_destroy(client);
                free(node);
651 652 653 654
                continue;
            }
        }
        node_ref = &node->next;
655
    }
656
    _handle_connection();
Jack Moffitt's avatar
Jack Moffitt committed
657 658
}

659

660 661 662
/* 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
663
static void _add_request_queue(client_queue_t *node)
664 665 666
{
    *_req_queue_tail = node;
    _req_queue_tail = (volatile client_queue_t **)&node->next;
Jack Moffitt's avatar
Jack Moffitt committed
667 668
}

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
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;
        if (listener->ssl && ssl_ok)
            connection_uses_ssl(client->con);
        if (listener->shoutcast_mount)
            node->shoutcast_mount = strdup(listener->shoutcast_mount);
    }

    config_release_config();

    return node;
}
696

697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
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();
        client_send_error(client, 403, 1, "Icecast connection limit reached");
        /* 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();
716
        ICECAST_LOG_WARN("Failed to set tcp options on client connection, dropping");
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
        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
732
void connection_accept_loop(void)
Jack Moffitt's avatar
Jack Moffitt committed
733
{
734
    connection_t *con;
735
    ice_config_t *config;
736
    int duration = 300;
737

Marvin Scholz's avatar
Marvin Scholz committed
738 739 740
    config = config_get_config();
    get_ssl_certificate(config);
    config_release_config();
Jack Moffitt's avatar
Jack Moffitt committed
741

Marvin Scholz's avatar
Marvin Scholz committed
742
    while (global.running == ICECAST_RUNNING) {
743
        con = _accept_connection (duration);
744

Marvin Scholz's avatar
Marvin Scholz committed
745
        if (con) {
746
            connection_queue(con);
747
            duration = 5;
Marvin Scholz's avatar
Marvin Scholz committed
748
        } else {
749 750
            if (_req_queue == NULL)
                duration = 300; /* use longer timeouts when nothing waiting */
751
        }
Marvin Scholz's avatar
Marvin Scholz committed
752
        process_request_queue();
753
    }
Jack Moffitt's avatar
Jack Moffitt committed
754

755 756 757
    /* Give all the other threads notification to shut down */
    thread_cond_broadcast(&global.shutdown_cond);

758 759 760
    /* 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
761 762
}

763 764 765

/* Called when activating a source. Verifies that the source count is not
 * exceeded and applies any initial parameters.
766
 */
Marvin Scholz's avatar
Marvin Scholz committed
767
int connection_complete_source(source_t *source, int response)
768
{
769
    ice_config_t *config;
770

Marvin Scholz's avatar
Marvin Scholz committed
771
    global_lock();
772
    ICECAST_LOG_DEBUG("sources count is %d", global.sources);
773

774
    config = config_get_config();
Marvin Scholz's avatar
Marvin Scholz committed
775
    if (global.sources < config->source_limit) {
776
        const char *contenttype;
777
        mount_proxy *mountinfo;
778 779 780 781
        format_type_t format_type;

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

Marvin Scholz's avatar
Marvin Scholz committed
785
            if (format_type == FORMAT_ERROR) {
786
                config_release_config();
787
                global_unlock();
788
                if (response) {
789
                    client_send_error(source->client, 403, 1, "Content-type not supported");
790 791
                    source->client = NULL;
                }
792
                ICECAST_LOG_WARN("Content-type \"%s\" not supported, dropping source", contenttype);
793 794
                return -1;
            }
795 796 797 798
        } else if (source->parser->req_type == httpp_req_put) {
            config_release_config();
            global_unlock();
            if (response) {
799
                client_send_error(source->client, 403, 1, "No Content-type given");
800 801 802 803 804 805 806 807
                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!");
808
            format_type = FORMAT_TYPE_GENERIC;
809 810
        }

Marvin Scholz's avatar
Marvin Scholz committed
811
        if (format_get_plugin (format_type, source) < 0) {
812 813
            global_unlock();
            config_release_config();
Marvin Scholz's avatar
Marvin Scholz committed
814
            if (response) {
815
                client_send_error(source->client, 403, 1, "internal format allocation problem");
816 817
                source->client = NULL;
            }
818
            ICECAST_LOG_WARN("plugin format failed for \"%s\"", source->mount);
819 820 821
            return -1;
        }

822
        global.sources++;
Marvin Scholz's avatar
Marvin Scholz committed
823
        stats_event_args(NULL, "sources", "%d", global.sources);
824
        global_unlock();
825

826
        source->running = 1;
Marvin Scholz's avatar
Marvin Scholz committed
827 828
        mountinfo = config_find_mount(config, source->mount, MOUNT_TYPE_NORMAL);
        source_update_settings(config, source, mountinfo);
829
        config_release_config();
830
        slave_rebuild_mounts();
831 832

        source->shutdown_rwlock = &_source_shutdown_rwlock;
833
        ICECAST_LOG_DEBUG("source is ready to start");
834 835 836

        return 0;
    }
837
    ICECAST_LOG_WARN("Request to add source when maximum source limit "
Marvin Scholz's avatar
Marvin Scholz committed
838
        "reached %d", global.sources);
839 840 841 842

    global_unlock();
    config_release_config();

Marvin Scholz's avatar
Marvin Scholz committed
843
    if (response) {
844
        client_send_error(source->client, 403, 1, "too many sources connected");
845 846
        source->client = NULL;
    }
847 848 849 850

    return -1;
}

Marvin Scholz's avatar
Marvin Scholz committed
851
static inline void source_startup(client_t *client, const char *uri)
852 853
{
    source_t *source;
Marvin Scholz's avatar
Marvin Scholz committed
854
    source = source_reserve(uri);
855

Marvin Scholz's avatar
Marvin Scholz committed
856
    if (source) {
857
        source->client = client;
858 859
        source->parser = client->parser;
        source->con = client->con;
Marvin Scholz's avatar
Marvin Scholz committed
860 861 862
        if (connection_complete_source(source, 1) < 0) {
            source_clear_source(source);
            source_free_source(source);
863 864 865
            return;
        }
        client->respcode = 200;
Philipp Schafft's avatar
Philipp Schafft committed
866 867 868 869 870
        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 */
            sock_write (client->con->sock, "OK2\r\nicy-caps:11\r\n\r\n");
871
            source->shoutcast_compat = 1;
Marvin Scholz's avatar
Marvin Scholz committed
872
            source_client_callback(client, source);
Philipp Schafft's avatar
Philipp Schafft committed
873
        } else {
Marvin Scholz's avatar
Marvin Scholz committed
874
            refbuf_t *ok = refbuf_new(PER_CLIENT_REFBUF_SIZE);
875
            const char *expectcontinue;
876
            const char *transfer_encoding;
877 878
            int status_to_send = 200;

879 880 881 882 883 884 885 886 887
            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) {
                    client_send_error(client, 501, 1, "Unimplemented");
                    return;
                }
            }

888 889 890 891 892 893 894
            /* 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
895
                ICECAST_LOG_WARN("OS doesn't support case insensitive substring checks...");
896 897 898 899 900 901 902
                if (strstr (expectcontinue, "100-continue") != NULL)
#endif
                {
                    status_to_send = 100;
                }
            }

903
            client->respcode = 200;
904
            util_http_build_header(ok->data, PER_CLIENT_REFBUF_SIZE, 0, 0, status_to_send, NULL, NULL, NULL, "", NULL, client);
Marvin Scholz's avatar
Marvin Scholz committed
905
            ok->len = strlen(ok->data);
906 907 908
            /* 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
909
            fserve_add_client_callback(client, source_client_callback, source);
910
        }
Marvin Scholz's avatar
Marvin Scholz committed
911
    } else {
912
        client_send_error(client, 403, 1, "Mountpoint in use");
913
        ICECAST_LOG_WARN("Mountpoint %s in use", uri);
914
    }
915 916
}

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

Marvin Scholz's avatar
Marvin Scholz committed
923
    if (uri[0] != '/') {
Philipp Schafft's avatar
Philipp Schafft committed
924 925
        ICECAST_LOG_WARN("source mountpoint not starting with /");
        client_send_error(client, 400, 1, "source mountpoint not starting with /");
926
        return;
927
    }
928

Philipp Schafft's avatar
Philipp Schafft committed
929 930 931 932
    source_startup(client, uri);
}


Marvin Scholz's avatar
Marvin Scholz committed
933
static void _handle_stats_request(client_t *client, char *uri)
Philipp Schafft's avatar
Philipp Schafft committed
934 935 936
{
    stats_event_inc(NULL, "stats_connections");

937
    client->respcode = 200;
938
    snprintf (client->refbuf->data, PER_CLIENT_REFBUF_SIZE,
Marvin Scholz's avatar
Marvin Scholz committed
939 940 941
        "HTTP/1.0 200 OK\r\n\r\n");
    client->refbuf->len = strlen(client->refbuf->data);
    fserve_add_client_callback(client, stats_callback, NULL);
942 943
}

Philipp Schafft's avatar
Philipp Schafft committed
944 945 946
/* 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
947
static int __add_listener_to_source(source_t *source, client_t *client)
948
{
Philipp Schafft's avatar
Philipp Schafft committed
949
    size_t loop = 10;
950

Marvin Scholz's avatar
Marvin Scholz committed
951
    do {
Philipp Schafft's avatar
Philipp Schafft committed
952
        ICECAST_LOG_DEBUG("max on %s is %ld (cur %lu)", source->mount,
Marvin Scholz's avatar
Marvin Scholz committed
953
            source->max_listeners, source->listeners);
Philipp Schafft's avatar
Philipp Schafft committed
954 955 956 957
        if (source->max_listeners == -1)
            break;
        if (source->listeners < (unsigned long)source->max_listeners)
            break;
958

Marvin Scholz's avatar
Marvin Scholz committed
959
        if (loop && source->fallback_when_full && source->fallback_mount) {
Philipp Schafft's avatar
Philipp Schafft committed
960 961 962
            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
963
                    source->mount, source->fallback_mount);
Philipp Schafft's avatar
Philipp Schafft committed
964 965
                return -1;
            }
966
            ICECAST_LOG_INFO("stream full, trying %s", next->mount);
Philipp Schafft's avatar
Philipp Schafft committed
967 968 969 970 971 972 973 974 975 976 977
            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
978
    memset(client->refbuf->data, 0, PER_CLIENT_REFBUF_SIZE);
Philipp Schafft's avatar
Philipp Schafft committed
979 980

    /* lets add the client to the active list */
Marvin Scholz's avatar
Marvin Scholz committed
981 982 983
    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
984

Marvin Scholz's avatar
Marvin Scholz committed
985
    if (source->running == 0 && source->on_demand) {
Philipp Schafft's avatar
Philipp Schafft committed
986 987 988
        /* enable on-demand relay to start, wake up the slave thread */
        ICECAST_LOG_DEBUG("kicking off on-demand relay");
        source->on_demand_req = 1;
Michael Smith's avatar