connection.c 49.4 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"
Michael Smith's avatar
Michael Smith committed
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
        ICECAST_LOG_INFO("SSL certificate found at %s", config->cert_file);
229
        ICECAST_LOG_INFO("SSL using ciphers %s", config->cipher_list);
230
        return;
231
    } while (0);
232
    ICECAST_LOG_INFO("No SSL 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
    ICECAST_LOG_INFO("No SSL capability");
277
278
279
280
281
282
283
}
#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
284
static int connection_read(connection_t *con, void *buf, size_t len)
285
{
Marvin Scholz's avatar
Marvin Scholz committed
286
    int bytes = sock_read_bytes(con->sock, buf, len);
287
288
    if (bytes == 0)
        con->error = 1;
Marvin Scholz's avatar
Marvin Scholz committed
289
    if (bytes == -1 && !sock_recoverable(sock_error()))
290
291
292
293
        con->error = 1;
    return bytes;
}

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


307
308
309
/* 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.
 */
310
static void recheck_ip_file(cache_file_contents *cache)
311
312
{
    time_t now = time(NULL);
Marvin Scholz's avatar
Marvin Scholz committed
313
314
315
316
317
318
    if (now >= cache->file_recheck) {
        struct      stat file_stat;
        FILE       *file             = NULL;
        int         count            = 0;
        avl_tree   *new_ips;
        char        line[MAX_LINE_LEN];
319
320

        cache->file_recheck = now + 10;
Marvin Scholz's avatar
Marvin Scholz committed
321
322
        if (cache->filename == NULL) {
            if (cache->contents) {
323
324
325
326
327
                avl_tree_free (cache->contents, free_filtered_ip);
                cache->contents = NULL;
            }
            return;
        }
Marvin Scholz's avatar
Marvin Scholz committed
328
        if (stat(cache->filename, &file_stat) < 0) {
329
            ICECAST_LOG_WARN("failed to check status of \"%s\": %s", cache->filename, strerror(errno));
330
331
332
333
334
335
336
337
            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
338
        if (file == NULL) {
339
            ICECAST_LOG_WARN("Failed to open file \"%s\": %s", cache->filename, strerror (errno));
340
341
342
            return;
        }

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

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

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


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

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

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


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

405
    return con;
406
407
}

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

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

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

    FD_ZERO(&rfds);

    for(i=0; i < global.server_sockets; i++) {
        FD_SET(global.serversock[i], &rfds);
475
        if (max == SOCK_ERROR || global.serversock[i] > max)
476
477
478
479
480
            max = global.serversock[i];
    }

    if(timeout >= 0) {
        tv.tv_sec = timeout/1000;
481
        tv.tv_usec = (timeout % 1000) * 1000;
482
483
484
485
486
        p = &tv;
    }

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

500
static connection_t *_accept_connection(int duration)
Jack Moffitt's avatar
Jack Moffitt committed
501
{
502
    sock_t sock, serversock;
503
    char *ip;
Jack Moffitt's avatar
Jack Moffitt committed
504

505
    serversock = wait_for_serversock (duration);
506
    if (serversock == SOCK_ERROR)
507
        return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
508

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

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

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


535
536
537
538
/* 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
 */
539
static void _add_connection(client_queue_t *node)
Jack Moffitt's avatar
Jack Moffitt committed
540
{
541
    thread_spin_lock(&_connection_lock);
542
    *_con_queue_tail = node;
543
544
    _con_queue_tail = (volatile client_queue_t **) &node->next;
    thread_spin_unlock(&_connection_lock);
Jack Moffitt's avatar
Jack Moffitt committed
545
546
547
}


548
549
550
551
552
553
/* 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
554

Marvin Scholz's avatar
Marvin Scholz committed
555
    thread_spin_lock(&_connection_lock);
556

Marvin Scholz's avatar
Marvin Scholz committed
557
    if (_con_queue){
558
559
560
561
        node = (client_queue_t *)_con_queue;
        _con_queue = node->next;
        if (_con_queue == NULL)
            _con_queue_tail = &_con_queue;
562
        node->next = NULL;
563
    }
564

Marvin Scholz's avatar
Marvin Scholz committed
565
    thread_spin_unlock(&_connection_lock);
566
567
    return node;
}
Jack Moffitt's avatar
Jack Moffitt committed
568
569


570
/* run along queue checking for any data that has come in or a timeout */
571
static void process_request_queue (void)
572
573
{
    client_queue_t **node_ref = (client_queue_t **)&_req_queue;
Marvin Scholz's avatar
Marvin Scholz committed
574
    ice_config_t *config = config_get_config();
575
576
    int timeout = config->header_timeout;
    config_release_config();
Jack Moffitt's avatar
Jack Moffitt committed
577

Marvin Scholz's avatar
Marvin Scholz committed
578
    while (*node_ref) {
579
580
581
582
        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
583

Marvin Scholz's avatar
Marvin Scholz committed
584
585
        if (len > 0) {
            if (client->con->con_time + timeout <= time(NULL)) {
586
                len = 0;
Marvin Scholz's avatar
Marvin Scholz committed
587
588
589
            } else {
                len = client_read_bytes(client, buf, len);
            }
590
        }
Jack Moffitt's avatar
Jack Moffitt committed
591

Marvin Scholz's avatar
Marvin Scholz committed
592
        if (len > 0) {
593
594
595
            int pass_it = 1;
            char *ptr;

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

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

653

654
655
656
/* 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
657
static void _add_request_queue(client_queue_t *node)
658
659
660
{
    *_req_queue_tail = node;
    _req_queue_tail = (volatile client_queue_t **)&node->next;
Jack Moffitt's avatar
Jack Moffitt committed
661
662
}

663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
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;
}
690

691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
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();
        ICECAST_LOG_WARN("failed to set tcp options on client connection, dropping");
        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
726
void connection_accept_loop(void)
Jack Moffitt's avatar
Jack Moffitt committed
727
{
728
    connection_t *con;
729
    ice_config_t *config;
730
    int duration = 300;
731

Marvin Scholz's avatar
Marvin Scholz committed
732
733
734
    config = config_get_config();
    get_ssl_certificate(config);
    config_release_config();
Jack Moffitt's avatar
Jack Moffitt committed
735

Marvin Scholz's avatar
Marvin Scholz committed
736
    while (global.running == ICECAST_RUNNING) {
737
        con = _accept_connection (duration);
738

Marvin Scholz's avatar
Marvin Scholz committed
739
        if (con) {
740
            connection_queue(con);
741
            duration = 5;
Marvin Scholz's avatar
Marvin Scholz committed
742
        } else {
743
744
            if (_req_queue == NULL)
                duration = 300; /* use longer timeouts when nothing waiting */
745
        }
Marvin Scholz's avatar
Marvin Scholz committed
746
        process_request_queue();
747
    }
Jack Moffitt's avatar
Jack Moffitt committed
748

749
750
751
    /* Give all the other threads notification to shut down */
    thread_cond_broadcast(&global.shutdown_cond);

752
753
754
    /* 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
755
756
}

757
758
759

/* Called when activating a source. Verifies that the source count is not
 * exceeded and applies any initial parameters.
760
 */
Marvin Scholz's avatar
Marvin Scholz committed
761
int connection_complete_source(source_t *source, int response)
762
{
763
    ice_config_t *config;
764

Marvin Scholz's avatar
Marvin Scholz committed
765
    global_lock();
766
    ICECAST_LOG_DEBUG("sources count is %d", global.sources);
767

768
    config = config_get_config();
Marvin Scholz's avatar
Marvin Scholz committed
769
    if (global.sources < config->source_limit) {
770
        const char *contenttype;
771
        const char *expectcontinue;
772
        mount_proxy *mountinfo;
773
774
775
776
        format_type_t format_type;

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

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

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

817
818
        /* For PUT support we check for 100-continue and send back a 100 to stay in spec */
        expectcontinue = httpp_getvar (source->parser, "expect");
Marvin Scholz's avatar
Marvin Scholz committed
819
        if (expectcontinue != NULL) {
820
#ifdef HAVE_STRCASESTR
821
            if (strcasestr (expectcontinue, "100-continue") != NULL)
822
#else
823
824
            ICECAST_LOG_WARN("OS doesn't support case insenestive substring checks...");
            if (strstr (expectcontinue, "100-continue") != NULL)
825
#endif
826
827
828
829
            {
                client_send_100 (source->client);
            }
        }
830

831
        global.sources++;
Marvin Scholz's avatar
Marvin Scholz committed
832
        stats_event_args(NULL, "sources", "%d", global.sources);
833
        global_unlock();
834

835
        source->running = 1;
Marvin Scholz's avatar
Marvin Scholz committed
836
837
        mountinfo = config_find_mount(config, source->mount, MOUNT_TYPE_NORMAL);
        source_update_settings(config, source, mountinfo);
838
        config_release_config();
839
        slave_rebuild_mounts();
840
841

        source->shutdown_rwlock = &_source_shutdown_rwlock;
842
        ICECAST_LOG_DEBUG("source is ready to start");
843
844
845

        return 0;
    }
846
    ICECAST_LOG_WARN("Request to add source when maximum source limit "
Marvin Scholz's avatar
Marvin Scholz committed
847
        "reached %d", global.sources);
848
849
850
851

    global_unlock();
    config_release_config();

Marvin Scholz's avatar
Marvin Scholz committed
852
    if (response) {
853
        client_send_error(source->client, 403, 1, "too many sources connected");
854
855
        source->client = NULL;
    }
856
857
858
859

    return -1;
}

Marvin Scholz's avatar
Marvin Scholz committed
860
static inline void source_startup(client_t *client, const char *uri)
861
862
{
    source_t *source;
Marvin Scholz's avatar
Marvin Scholz committed
863
    source = source_reserve(uri);
864

Marvin Scholz's avatar
Marvin Scholz committed
865
    if (source) {
866
        source->client = client;
867
868
        source->parser = client->parser;
        source->con = client->con;
Marvin Scholz's avatar
Marvin Scholz committed
869
870
871
        if (connection_complete_source(source, 1) < 0) {
            source_clear_source(source);
            source_free_source(source);
872
873
874
            return;
        }
        client->respcode = 200;
Philipp Schafft's avatar
Philipp Schafft committed
875
876
877
878
879
        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");
880
            source->shoutcast_compat = 1;
Marvin Scholz's avatar
Marvin Scholz committed
881
            source_client_callback(client, source);
Philipp Schafft's avatar
Philipp Schafft committed
882
        } else {
Marvin Scholz's avatar
Marvin Scholz committed
883
            refbuf_t *ok = refbuf_new(PER_CLIENT_REFBUF_SIZE);
884
            client->respcode = 200;
885
            util_http_build_header(ok->data, PER_CLIENT_REFBUF_SIZE, 0, 0, 200, NULL, NULL, NULL, "", NULL, client);
Marvin Scholz's avatar
Marvin Scholz committed
886
            ok->len = strlen(ok->data);
887
888
889
            /* 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
890
            fserve_add_client_callback(client, source_client_callback, source);
891
        }
Marvin Scholz's avatar
Marvin Scholz committed
892
    } else {
893
        client_send_error(client, 403, 1, "Mountpoint in use");
894
        ICECAST_LOG_WARN("Mountpoint %s in use", uri);
895
    }
896
897
}

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

Marvin Scholz's avatar
Marvin Scholz committed
904
    if (uri[0] != '/') {
Philipp Schafft's avatar
Philipp Schafft committed
905
906
        ICECAST_LOG_WARN("source mountpoint not starting with /");
        client_send_error(client, 400, 1, "source mountpoint not starting with /");
907
        return;
908
    }
909

Philipp Schafft's avatar
Philipp Schafft committed
910
911
912
913
    source_startup(client, uri);
}


Marvin Scholz's avatar
Marvin Scholz committed
914
static void _handle_stats_request(client_t *client, char *uri)
Philipp Schafft's avatar
Philipp Schafft committed
915
916
917
{
    stats_event_inc(NULL, "stats_connections");

918
    client->respcode = 200;
919
    snprintf (client->refbuf->data, PER_CLIENT_REFBUF_SIZE,
Marvin Scholz's avatar
Marvin Scholz committed
920
921
922
        "HTTP/1.0 200 OK\r\n\r\n");
    client->refbuf->len = strlen(client->refbuf->data);
    fserve_add_client_callback(client, stats_callback, NULL);
923
924
}

Philipp Schafft's avatar
Philipp Schafft committed
925
926
927
/* 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
928
static int __add_listener_to_source(source_t *source, client_t *client)
929
{
Philipp Schafft's avatar
Philipp Schafft committed
930
    size_t loop = 10;
Michael Smith's avatar
Michael Smith committed
931

Marvin Scholz's avatar
Marvin Scholz committed
932
    do {
Philipp Schafft's avatar
Philipp Schafft committed
933
        ICECAST_LOG_DEBUG("max on %s is %ld (cur %lu)", source->mount,
Marvin Scholz's avatar