connection.c 57.1 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

Marvin Scholz's avatar
Marvin Scholz committed
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"
Michael Smith's avatar
Michael Smith committed
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
    char *bodybuffer;
    size_t bodybufferlen;
87
    int tried_body;
88
89
    struct client_queue_tag *next;
} client_queue_t;
Jack Moffitt's avatar
Jack Moffitt committed
90

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

95
96
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;
97
static volatile client_queue_t *_body_queue = NULL, **_body_queue_tail = &_body_queue;
98
static int tls_ok;
99
static tls_ctx_t *tls_ctx;
100

101
/* filtering client connection based on IP */
102
static matchfile_t *banned_ip, *allowed_ip;
103

104
rwlock_t _source_shutdown_rwlock;
Jack Moffitt's avatar
Jack Moffitt committed
105

106
static void _handle_connection(void);
107
static void get_tls_certificate(ice_config_t *config);
Jack Moffitt's avatar
Jack Moffitt committed
108
109
110

void connection_initialize(void)
{
Marvin Scholz's avatar
Marvin Scholz committed
111
112
    if (_initialized)
        return;
113

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

125
    _initialized = 1;
Jack Moffitt's avatar
Jack Moffitt committed
126
127
128
129
}

void connection_shutdown(void)
{
Marvin Scholz's avatar
Marvin Scholz committed
130
131
    if (!_initialized)
        return;
132

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

142
    _initialized = 0;
Jack Moffitt's avatar
Jack Moffitt committed
143
144
}

145
void connection_reread_config(ice_config_t *config)
146
{
147
    get_tls_certificate(config);
148
149
}

Jack Moffitt's avatar
Jack Moffitt committed
150
151
static unsigned long _next_connection_id(void)
{
152
    unsigned long id;
Jack Moffitt's avatar
Jack Moffitt committed
153

154
    thread_spin_lock(&_connection_lock);
155
    id = _current_id++;
156
    thread_spin_unlock(&_connection_lock);
Jack Moffitt's avatar
Jack Moffitt committed
157

158
    return id;
Jack Moffitt's avatar
Jack Moffitt committed
159
160
}

161

162
#ifdef ICECAST_CAP_TLS
163
static void get_tls_certificate(ice_config_t *config)
164
{
165
166
    const char *keyfile;

167
    config->tls_ok = tls_ok = 0;
168

169
170
171
172
    keyfile = config->tls_context.key_file;
    if (!keyfile)
        keyfile = config->tls_context.cert_file;

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

180
    config->tls_ok = tls_ok = 1;
181
182
183
}


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

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

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

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

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


/* 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
227
static int connection_read(connection_t *con, void *buf, size_t len)
228
{
Marvin Scholz's avatar
Marvin Scholz committed
229
    int bytes = sock_read_bytes(con->sock, buf, len);
230
231
    if (bytes == 0)
        con->error = 1;
Marvin Scholz's avatar
Marvin Scholz committed
232
    if (bytes == -1 && !sock_recoverable(sock_error()))
233
234
235
236
        con->error = 1;
    return bytes;
}

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

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

264
    return con;
265
266
}

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

275
276
277
278
279
280
    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;
    }

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

290
291
ssize_t connection_read_bytes(connection_t *con, void *buf, size_t len)
{
292
293
294
295
296
297
298
299
    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);
300
            ICECAST_LOG_DEBUG("New fill in buffer=<empty>");
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
354
355
356
            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;
    }
357
358
}

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

    FD_ZERO(&rfds);

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

    if(timeout >= 0) {
        tv.tv_sec = timeout/1000;
416
        tv.tv_usec = (timeout % 1000) * 1000;
417
418
419
420
421
        p = &tv;
    }

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

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

440
    serversock = wait_for_serversock (duration);
441
    if (serversock == SOCK_ERROR)
442
        return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
443

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

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

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


470
471
472
473
/* 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
 */
474
static void _add_connection(client_queue_t *node)
Jack Moffitt's avatar
Jack Moffitt committed
475
{
476
    thread_spin_lock(&_connection_lock);
477
    *_con_queue_tail = node;
478
479
    _con_queue_tail = (volatile client_queue_t **) &node->next;
    thread_spin_unlock(&_connection_lock);
Jack Moffitt's avatar
Jack Moffitt committed
480
481
482
}


483
484
485
486
487
488
/* 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
489

Marvin Scholz's avatar
Marvin Scholz committed
490
    thread_spin_lock(&_connection_lock);
491

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

Marvin Scholz's avatar
Marvin Scholz committed
500
    thread_spin_unlock(&_connection_lock);
501
502
    return node;
}
Jack Moffitt's avatar
Jack Moffitt committed
503
504


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

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

Marvin Scholz's avatar
Marvin Scholz committed
517
    while (*node_ref) {
518
519
520
521
        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
522

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

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

Marvin Scholz's avatar
Marvin Scholz committed
539
        if (len > 0) {
540
541
542
            int pass_it = 1;
            char *ptr;

543
544
            /* handle \n, \r\n and nsvcap which for some strange reason has
             * EOL as \r\r\n */
545
            node->offset += len;
Marvin Scholz's avatar
Marvin Scholz committed
546
547
548
            client->refbuf->data[node->offset] = '\000';
            do {
                if (node->shoutcast == 1) {
549
                    /* password line */
550
551
                    if (strstr (client->refbuf->data, "\r\r\n") != NULL)
                        break;
552
553
554
555
556
557
558
                    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
559
560
                ptr = strstr(client->refbuf->data, "\r\r\n\r\r\n");
                if (ptr) {
561
562
563
                    node->stream_offset = (ptr+6) - client->refbuf->data;
                    break;
                }
Marvin Scholz's avatar
Marvin Scholz committed
564
565
                ptr = strstr(client->refbuf->data, "\r\n\r\n");
                if (ptr) {
566
567
568
                    node->stream_offset = (ptr+4) - client->refbuf->data;
                    break;
                }
Marvin Scholz's avatar
Marvin Scholz committed
569
570
                ptr = strstr(client->refbuf->data, "\n\n");
                if (ptr) {
571
572
573
574
575
                    node->stream_offset = (ptr+2) - client->refbuf->data;
                    break;
                }
                pass_it = 0;
            } while (0);
Jack Moffitt's avatar
Jack Moffitt committed
576

Marvin Scholz's avatar
Marvin Scholz committed
577
            if (pass_it) {
578
579
580
581
                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
582
                _add_connection(node);
583
                continue;
584
            }
Marvin Scholz's avatar
Marvin Scholz committed
585
586
        } else {
            if (len == 0 || client->con->error) {
587
588
589
                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
590
591
                client_destroy(client);
                free(node);
592
593
594
595
                continue;
            }
        }
        node_ref = &node->next;
596
    }
597
    _handle_connection();
Jack Moffitt's avatar
Jack Moffitt committed
598
599
}

600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/* 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;
617
618
619
    ice_config_t *config;
    time_t timeout;
    size_t body_size_limit;
620
621
622
623
624

    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);

625
626
627
628
629
    config = config_get_config();
    timeout = time(NULL) - config->body_timeout;
    body_size_limit = config->body_size_limit;
    config_release_config();

630
631
632
633
634
    while (*node_ref) {
        client_queue_t *node = *node_ref;
        client_t *client = node->client;
        client_slurp_result_t res;

635
636
        node->tried_body = 1;

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

639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
        if (client->parser->req_type == httpp_req_post) {
            if (node->bodybuffer == NULL && client->request_body_read == 0) {
                if (client->request_body_length < 0) {
                    node->bodybufferlen = body_size_limit;
                    node->bodybuffer = malloc(node->bodybufferlen);
                } else if (client->request_body_length <= (ssize_t)body_size_limit) {
                    node->bodybufferlen = client->request_body_length;
                    node->bodybuffer = malloc(node->bodybufferlen);
                }
            }
        }

        if (node->bodybuffer) {
            res = client_body_slurp(client, node->bodybuffer, &(node->bodybufferlen));
            if (res == CLIENT_SLURP_SUCCESS) {
                httpp_parse_postdata(client->parser, node->bodybuffer, node->bodybufferlen);
                free(node->bodybuffer);
                node->bodybuffer = NULL;
            }
        } else {
            res = client_body_skip(client);
        }
661

662
        if (res != CLIENT_SLURP_NEEDS_MORE_DATA || client->con->con_time <= timeout || client->request_body_read >= body_size_limit) {
663
664
665
666
667
668
669
670
671
672
673
674
            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;
    }
}
675

676
677
678
/* 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
679
static void _add_request_queue(client_queue_t *node)
680
681
682
{
    *_req_queue_tail = node;
    _req_queue_tail = (volatile client_queue_t **)&node->next;
Jack Moffitt's avatar
Jack Moffitt committed
683
684
}

685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
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;
702
703
        client->con->tlsmode = listener->tls;
        if (listener->tls == ICECAST_TLSMODE_RFC2818 && tls_ok)
704
            connection_uses_tls(client->con);
705
706
707
708
709
710
711
712
        if (listener->shoutcast_mount)
            node->shoutcast_mount = strdup(listener->shoutcast_mount);
    }

    config_release_config();

    return node;
}
713

714
715
716
717
718
719
720
721
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();
722
        client_send_error_by_id(client, ICECAST_ERROR_GEN_CLIENT_LIMIT);
723
724
725
726
727
728
729
730
731
732
        /* 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();
733
        ICECAST_LOG_WARN("Failed to set tcp options on client connection, dropping");
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
        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
749
void connection_accept_loop(void)
Jack Moffitt's avatar
Jack Moffitt committed
750
{
751
    connection_t *con;
752
    ice_config_t *config;
753
    int duration = 300;
754

Marvin Scholz's avatar
Marvin Scholz committed
755
    config = config_get_config();
756
    get_tls_certificate(config);
Marvin Scholz's avatar
Marvin Scholz committed
757
    config_release_config();
Jack Moffitt's avatar
Jack Moffitt committed
758

Marvin Scholz's avatar
Marvin Scholz committed
759
    while (global.running == ICECAST_RUNNING) {
760
        con = _accept_connection (duration);
761

Marvin Scholz's avatar
Marvin Scholz committed
762
        if (con) {
763
            connection_queue(con);
764
            duration = 5;
Marvin Scholz's avatar
Marvin Scholz committed
765
        } else {
766
767
            if (_req_queue == NULL)
                duration = 300; /* use longer timeouts when nothing waiting */
768
        }
Marvin Scholz's avatar
Marvin Scholz committed
769
        process_request_queue();
770
        process_request_body_queue();
771
    }
Jack Moffitt's avatar
Jack Moffitt committed
772

773
774
775
    /* Give all the other threads notification to shut down */
    thread_cond_broadcast(&global.shutdown_cond);

776
777
778
    /* 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
779
780
}

781
782
783

/* Called when activating a source. Verifies that the source count is not
 * exceeded and applies any initial parameters.
784
 */
Marvin Scholz's avatar
Marvin Scholz committed
785
int connection_complete_source(source_t *source, int response)
786
{
787
    ice_config_t *config;
788

Marvin Scholz's avatar
Marvin Scholz committed
789
    global_lock();
790
    ICECAST_LOG_DEBUG("sources count is %d", global.sources);
791

792
    config = config_get_config();
Marvin Scholz's avatar
Marvin Scholz committed
793
    if (global.sources < config->source_limit) {
794
        const char *contenttype;
795
        mount_proxy *mountinfo;
796
797
798
799
        format_type_t format_type;

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

Marvin Scholz's avatar
Marvin Scholz committed
803
            if (format_type == FORMAT_ERROR) {
804
                config_release_config();
805
                global_unlock();
806
                if (response) {
807
                    client_send_error_by_id(source->client, ICECAST_ERROR_CON_CONTENT_TYPE_NOSYS);
808
809
                    source->client = NULL;
                }
810
                ICECAST_LOG_WARN("Content-type \"%s\" not supported, dropping source", contenttype);
811
812
                return -1;
            }
813
814
815
816
        } else if (source->parser->req_type == httpp_req_put) {
            config_release_config();
            global_unlock();
            if (response) {
817
                client_send_error_by_id(source->client, ICECAST_ERROR_CON_NO_CONTENT_TYPE_GIVEN);
818
819
820
821
822
823
824
825
                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!");
826
            format_type = FORMAT_TYPE_GENERIC;
827
828
        }

Marvin Scholz's avatar
Marvin Scholz committed
829
        if (format_get_plugin (format_type, source) < 0) {
830
831
            global_unlock();
            config_release_config();
Marvin Scholz's avatar
Marvin Scholz committed
832
            if (response) {
833
                client_send_error_by_id(source->client, ICECAST_ERROR_CON_INTERNAL_FORMAT_ALLOC_ERROR);
834
835
                source->client = NULL;
            }
836
            ICECAST_LOG_WARN("plugin format failed for \"%s\"", source->mount);
837
838
839
            return -1;
        }

840
        global.sources++;
Marvin Scholz's avatar
Marvin Scholz committed
841
        stats_event_args(NULL, "sources", "%d", global.sources);
842
        global_unlock();
843

844
        source->running = 1;
Marvin Scholz's avatar
Marvin Scholz committed
845
846
        mountinfo = config_find_mount(config, source->mount, MOUNT_TYPE_NORMAL);
        source_update_settings(config, source, mountinfo);
847
        config_release_config();
848
        slave_rebuild_mounts();
849
850

        source->shutdown_rwlock = &_source_shutdown_rwlock;
851
        ICECAST_LOG_DEBUG("source is ready to start");
852
853
854

        return 0;
    }
855
    ICECAST_LOG_WARN("Request to add source when maximum source limit "
Marvin Scholz's avatar
Marvin Scholz committed
856
        "reached %d", global.sources);
857
858
859
860

    global_unlock();
    config_release_config();

Marvin Scholz's avatar
Marvin Scholz committed
861
    if (response) {
862
        client_send_error_by_id(source->client, ICECAST_ERROR_CON_SOURCE_CLIENT_LIMIT);
863
864
        source->client = NULL;
    }
865
866
867
868

    return -1;
}

Marvin Scholz's avatar
Marvin Scholz committed
869
static inline void source_startup(client_t *client, const char *uri)
870
871
{
    source_t *source;
Marvin Scholz's avatar
Marvin Scholz committed
872
    source = source_reserve(uri);
873

Marvin Scholz's avatar
Marvin Scholz committed
874
    if (source) {
875
        source->client = client;
876
877
        source->parser = client->parser;
        source->con = client->con;
Marvin Scholz's avatar
Marvin Scholz committed
878
879
880
        if (connection_complete_source(source, 1) < 0) {
            source_clear_source(source);
            source_free_source(source);
881
882
883
            return;
        }
        client->respcode = 200;
Philipp Schafft's avatar
Philipp Schafft committed
884
885
886
887
        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 */
888
            client_send_bytes(client, "OK2\r\nicy-caps:11\r\n\r\n", 20); /* TODO: Replace Magic Number! */
889
            source->shoutcast_compat = 1;
Marvin Scholz's avatar
Marvin Scholz committed
890
            source_client_callback(client, source);
Philipp Schafft's avatar
Philipp Schafft committed
891
        } else {
Marvin Scholz's avatar
Marvin Scholz committed
892
            refbuf_t *ok = refbuf_new(PER_CLIENT_REFBUF_SIZE);
893
            const char *expectcontinue;
894
            const char *transfer_encoding;
895
            int status_to_send = 200;
896
            ssize_t ret;
897

898
899
900
901
            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) {
902
                    client_send_error_by_id(client, ICECAST_ERROR_CON_UNIMPLEMENTED);
903
904
905
906
                    return;
                }
            }

907
908
909
910
911
912
913
            /* 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
914
                ICECAST_LOG_WARN("OS doesn't support case insensitive substring checks...");
915
916
917
918
919
920
921
                if (strstr (expectcontinue, "100-continue") != NULL)
#endif
                {
                    status_to_send = 100;
                }
            }

922
            client->respcode = 200;
923
924
            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
925
            ok->len = strlen(ok->data);
926
927
928
            /* 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
929
            fserve_add_client_callback(client, source_client_callback, source);
930
        }
Marvin Scholz's avatar
Marvin Scholz committed
931
    } else {
932
        client_send_error_by_id(client, ICECAST_ERROR_CON_MOUNT_IN_USE);
933
        ICECAST_LOG_WARN("Mountpoint %s in use", uri);
934
    }
935
936
}

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

Marvin Scholz's avatar
Marvin Scholz committed
943
    if (uri[0] != '/') {
Philipp Schafft's avatar
Philipp Schafft committed
944
        ICECAST_LOG_WARN("source mountpoint not starting with /");
945
        client_send_error_by_id(client, ICECAST_ERROR_CON_MOUNTPOINT_NOT_STARTING_WITH_SLASH);
946
        return;
947
    }
948

Philipp Schafft's avatar
Philipp Schafft committed
949
950
951
952
    source_startup(client, uri);
}


Marvin Scholz's avatar
Marvin Scholz committed
953
static void _handle_stats_request(client_t *client, char *uri)
Philipp Schafft's avatar
Philipp Schafft committed
954
955
956
{
    stats_event_inc(NULL, "stats_connections");

957
    client->respcode = 200;
958
    snprintf (client->refbuf->data, PER_CLIENT_REFBUF_SIZE,