connection.c 54.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
 * 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
#ifdef HAVE_POLL
25
#include <poll.h>
26
#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"
61
62
#include "refobject.h"
#include "listensocket.h"
63
#include "fastevent.h"
Jack Moffitt's avatar
Jack Moffitt committed
64
65
66

#define CATMODULE "connection"

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

82
83
84
85
typedef struct client_queue_tag {
    client_t *client;
    int offset;
    int shoutcast;
86
    char *shoutcast_mount;
87
88
    char *bodybuffer;
    size_t bodybufferlen;
89
    int tried_body;
90
91
    struct client_queue_tag *next;
} client_queue_t;
Jack Moffitt's avatar
Jack Moffitt committed
92

93
static spin_t _connection_lock; // protects _current_id, _con_queue, _con_queue_tail
94
static volatile connection_id_t _current_id = 0;
Jack Moffitt's avatar
Jack Moffitt committed
95
96
static int _initialized = 0;

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

103
/* filtering client connection based on IP */
104
static matchfile_t *banned_ip, *allowed_ip;
105

106
rwlock_t _source_shutdown_rwlock;
Jack Moffitt's avatar
Jack Moffitt committed
107

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

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

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

127
    _initialized = 1;
Jack Moffitt's avatar
Jack Moffitt committed
128
129
130
131
}

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

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

144
    _initialized = 0;
Jack Moffitt's avatar
Jack Moffitt committed
145
146
}

147
void connection_reread_config(ice_config_t *config)
148
{
149
    get_tls_certificate(config);
150
    listensocket_container_configure_and_setup(global.listensockets, config);
151
152
}

153
static connection_id_t _next_connection_id(void)
Jack Moffitt's avatar
Jack Moffitt committed
154
{
155
    connection_id_t id;
Jack Moffitt's avatar
Jack Moffitt committed
156

157
    thread_spin_lock(&_connection_lock);
158
    id = _current_id++;
159
    thread_spin_unlock(&_connection_lock);
Jack Moffitt's avatar
Jack Moffitt committed
160

161
    return id;
Jack Moffitt's avatar
Jack Moffitt committed
162
163
}

164

165
#ifdef ICECAST_CAP_TLS
166
static void get_tls_certificate(ice_config_t *config)
167
{
168
169
    const char *keyfile;

170
    config->tls_ok = tls_ok = 0;
171

172
173
174
175
    keyfile = config->tls_context.key_file;
    if (!keyfile)
        keyfile = config->tls_context.cert_file;

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

183
    config->tls_ok = tls_ok = 1;
184
185
186
}


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

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

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

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

214
215
216
217
    return bytes;
}
#else

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


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

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

251
252
253
    return bytes;
}

254
connection_t *connection_create(sock_t sock, listensocket_t *listensocket_real, listensocket_t* listensocket_effective, char *ip)
255
{
256
    connection_t *con;
257
258
259
260

    if (!matchfile_match_allow_deny(allowed_ip, banned_ip, ip))
        return NULL;

261
    con = (connection_t *)calloc(1, sizeof(connection_t));
Marvin Scholz's avatar
Marvin Scholz committed
262
    if (con) {
263
264
265
        refobject_ref(listensocket_real);
        refobject_ref(listensocket_effective);

Marvin Scholz's avatar
Marvin Scholz committed
266
        con->sock       = sock;
267
268
        con->listensocket_real = listensocket_real;
        con->listensocket_effective = listensocket_effective;
Marvin Scholz's avatar
Marvin Scholz committed
269
270
271
        con->con_time   = time(NULL);
        con->id         = _next_connection_id();
        con->ip         = ip;
272
        con->tlsmode    = ICECAST_TLSMODE_AUTO;
Marvin Scholz's avatar
Marvin Scholz committed
273
274
        con->read       = connection_read;
        con->send       = connection_send;
275
    }
Michael Smith's avatar
Michael Smith committed
276

277
278
    fastevent_emit(FASTEVENT_TYPE_CONNECTION_CREATE, FASTEVENT_FLAG_MODIFICATION_ALLOWED, FASTEVENT_DATATYPE_CONNECTION, con);

279
    return con;
280
281
}

282
/* prepare connection for interacting over a TLS connection
283
 */
284
void connection_uses_tls(connection_t *con)
285
{
286
#ifdef ICECAST_CAP_TLS
287
    if (con->tls)
288
289
        return;

290
291
292
293
294
295
    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;
    }

296
    con->tlsmode = ICECAST_TLSMODE_RFC2818;
297
298
    con->read = connection_read_tls;
    con->send = connection_send_tls;
299
300
301
    con->tls = tls_new(tls_ctx);
    tls_set_incoming(con->tls);
    tls_set_socket(con->tls, con->sock);
302
303
304
#endif
}

305
306
ssize_t connection_send_bytes(connection_t *con, const void *buf, size_t len)
{
307
308
309
310
311
    ssize_t ret = con->send(con, buf, len);

    fastevent_emit(FASTEVENT_TYPE_CONNECTION_WRITE, FASTEVENT_FLAG_MODIFICATION_ALLOWED, FASTEVENT_DATATYPE_OBRD, con, buf, len, ret);

    return ret;
312
313
}

314
static inline ssize_t connection_read_bytes_real(connection_t *con, void *buf, size_t len)
315
{
316
317
318
319
320
321
322
323
    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);
324
            con->readbuffer = NULL;
325
            ICECAST_LOG_DEBUG("New fill in buffer=<empty>");
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
            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;
}

356
357
358
359
360
361
362
363
364
ssize_t connection_read_bytes(connection_t *con, void *buf, size_t len)
{
    ssize_t ret = connection_read_bytes_real(con, buf, len);

    fastevent_emit(FASTEVENT_TYPE_CONNECTION_READ, FASTEVENT_FLAG_MODIFICATION_ALLOWED, FASTEVENT_DATATYPE_OBRD, con, buf, len, ret);

    return ret;
}

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

369
370
    fastevent_emit(FASTEVENT_TYPE_CONNECTION_PUTBACK, FASTEVENT_FLAG_MODIFICATION_ALLOWED, FASTEVENT_DATATYPE_OBR, con, buf, len);

371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
    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;
    }
393
394
}

395
396
397
398
/* 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
 */
399
static void _add_connection(client_queue_t *node)
Jack Moffitt's avatar
Jack Moffitt committed
400
{
401
    thread_spin_lock(&_connection_lock);
402
    *_con_queue_tail = node;
403
404
    _con_queue_tail = (volatile client_queue_t **) &node->next;
    thread_spin_unlock(&_connection_lock);
Jack Moffitt's avatar
Jack Moffitt committed
405
406
407
}


408
409
410
411
412
413
/* 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
414

Marvin Scholz's avatar
Marvin Scholz committed
415
    thread_spin_lock(&_connection_lock);
416

Marvin Scholz's avatar
Marvin Scholz committed
417
    if (_con_queue){
418
419
420
421
        node = (client_queue_t *)_con_queue;
        _con_queue = node->next;
        if (_con_queue == NULL)
            _con_queue_tail = &_con_queue;
422
        node->next = NULL;
423
    }
424

Marvin Scholz's avatar
Marvin Scholz committed
425
    thread_spin_unlock(&_connection_lock);
426
427
    return node;
}
Jack Moffitt's avatar
Jack Moffitt committed
428
429


430
/* run along queue checking for any data that has come in or a timeout */
431
static void process_request_queue (void)
432
433
{
    client_queue_t **node_ref = (client_queue_t **)&_req_queue;
434
435
436
437
438
439
    ice_config_t *config;
    int timeout;
    char peak;

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

Marvin Scholz's avatar
Marvin Scholz committed
442
    while (*node_ref) {
443
444
445
446
        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
447

448
        if (client->con->tlsmode == ICECAST_TLSMODE_AUTO || client->con->tlsmode == ICECAST_TLSMODE_AUTO_NO_PLAIN) {
449
450
            if (recv(client->con->sock, &peak, 1, MSG_PEEK) == 1) {
                if (peak == 0x16) { /* TLS Record Protocol Content type 0x16 == Handshake */
451
                    connection_uses_tls(client->con);
452
453
454
455
                }
            }
        }

Marvin Scholz's avatar
Marvin Scholz committed
456
457
        if (len > 0) {
            if (client->con->con_time + timeout <= time(NULL)) {
458
                len = 0;
Marvin Scholz's avatar
Marvin Scholz committed
459
460
461
            } else {
                len = client_read_bytes(client, buf, len);
            }
462
        }
Jack Moffitt's avatar
Jack Moffitt committed
463

Marvin Scholz's avatar
Marvin Scholz committed
464
        if (len > 0) {
465
            ssize_t stream_offset = -1;
466
467
468
            int pass_it = 1;
            char *ptr;

469
470
            /* handle \n, \r\n and nsvcap which for some strange reason has
             * EOL as \r\r\n */
471
            node->offset += len;
Marvin Scholz's avatar
Marvin Scholz committed
472
473
474
            client->refbuf->data[node->offset] = '\000';
            do {
                if (node->shoutcast == 1) {
475
                    /* password line */
476
477
                    if (strstr (client->refbuf->data, "\r\r\n") != NULL)
                        break;
478
479
480
481
482
483
484
                    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
485
486
                ptr = strstr(client->refbuf->data, "\r\r\n\r\r\n");
                if (ptr) {
487
                    stream_offset = (ptr+6) - client->refbuf->data;
488
489
                    break;
                }
Marvin Scholz's avatar
Marvin Scholz committed
490
491
                ptr = strstr(client->refbuf->data, "\r\n\r\n");
                if (ptr) {
492
                    stream_offset = (ptr+4) - client->refbuf->data;
493
494
                    break;
                }
Marvin Scholz's avatar
Marvin Scholz committed
495
496
                ptr = strstr(client->refbuf->data, "\n\n");
                if (ptr) {
497
                    stream_offset = (ptr+2) - client->refbuf->data;
498
499
500
501
                    break;
                }
                pass_it = 0;
            } while (0);
Jack Moffitt's avatar
Jack Moffitt committed
502

Marvin Scholz's avatar
Marvin Scholz committed
503
            if (pass_it) {
504
505
506
507
                if (stream_offset != -1) {
                    connection_read_put_back(client->con, client->refbuf->data + stream_offset, node->offset - stream_offset);
                    node->offset = stream_offset;
                }
508
509
510
511
                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
512
                _add_connection(node);
513
                continue;
514
            }
Marvin Scholz's avatar
Marvin Scholz committed
515
516
        } else {
            if (len == 0 || client->con->error) {
517
518
519
                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
520
521
                client_destroy(client);
                free(node);
522
523
524
525
                continue;
            }
        }
        node_ref = &node->next;
526
    }
527
    _handle_connection();
Jack Moffitt's avatar
Jack Moffitt committed
528
529
}

530
531
532
533
534
535
536
537
538
539
540
541
/* 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);
}

542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
static client_slurp_result_t process_request_body_queue_one(client_queue_t *node, time_t timeout, size_t body_size_limit)
{
        client_t *client = node->client;
        client_slurp_result_t res;

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

        if (res != CLIENT_SLURP_SUCCESS) {
            if (client->con->con_time <= timeout || client->request_body_read >= body_size_limit) {
                return CLIENT_SLURP_ERROR;
            }
        }

        return res;
}
578
579
580
581
582

/* 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;
583
584
585
    ice_config_t *config;
    time_t timeout;
    size_t body_size_limit;
586
587
588
589
590

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

591
592
593
594
595
    config = config_get_config();
    timeout = time(NULL) - config->body_timeout;
    body_size_limit = config->body_size_limit;
    config_release_config();

596
597
598
599
600
    while (*node_ref) {
        client_queue_t *node = *node_ref;
        client_t *client = node->client;
        client_slurp_result_t res;

601
602
        node->tried_body = 1;

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

605
        res = process_request_body_queue_one(node, timeout, body_size_limit);
606

607
        if (res != CLIENT_SLURP_NEEDS_MORE_DATA) {
608
609
610
611
612
613
614
615
616
617
618
619
            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;
    }
}
620

621
622
623
/* 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
624
static void _add_request_queue(client_queue_t *node)
625
626
627
{
    *_req_queue_tail = node;
    _req_queue_tail = (volatile client_queue_t **)&node->next;
Jack Moffitt's avatar
Jack Moffitt committed
628
629
}

630
631
632
static client_queue_t *create_client_node(client_t *client)
{
    client_queue_t *node = calloc (1, sizeof (client_queue_t));
633
    const listener_t *listener;
634
635
636
637
638
639

    if (!node)
        return NULL;

    node->client = client;

640
    listener = listensocket_get_listener(client->con->listensocket_effective);
641
642
643
644

    if (listener) {
        if (listener->shoutcast_compat)
            node->shoutcast = 1;
645
646
        client->con->tlsmode = listener->tls;
        if (listener->tls == ICECAST_TLSMODE_RFC2818 && tls_ok)
647
            connection_uses_tls(client->con);
648
649
650
651
        if (listener->shoutcast_mount)
            node->shoutcast_mount = strdup(listener->shoutcast_mount);
    }

652
653
    listensocket_release_listener(client->con->listensocket_effective);

654
655
    return node;
}
656

657
658
659
660
661
662
663
664
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();
665
        client_send_error_by_id(client, ICECAST_ERROR_GEN_CLIENT_LIMIT);
666
667
668
669
670
671
672
673
674
675
        /* 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();
676
        ICECAST_LOG_WARN("Failed to set tcp options on client connection, dropping");
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
        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
692
void connection_accept_loop(void)
Jack Moffitt's avatar
Jack Moffitt committed
693
{
694
    connection_t *con;
695
    ice_config_t *config;
696
    int duration = 300;
697

Marvin Scholz's avatar
Marvin Scholz committed
698
    config = config_get_config();
699
    get_tls_certificate(config);
Marvin Scholz's avatar
Marvin Scholz committed
700
    config_release_config();
Jack Moffitt's avatar
Jack Moffitt committed
701

Marvin Scholz's avatar
Marvin Scholz committed
702
    while (global.running == ICECAST_RUNNING) {
703
        con = listensocket_container_accept(global.listensockets, duration);
704

Marvin Scholz's avatar
Marvin Scholz committed
705
        if (con) {
706
            connection_queue(con);
707
            duration = 5;
Marvin Scholz's avatar
Marvin Scholz committed
708
        } else {
709
710
            if (_req_queue == NULL)
                duration = 300; /* use longer timeouts when nothing waiting */
711
        }
Marvin Scholz's avatar
Marvin Scholz committed
712
        process_request_queue();
713
        process_request_body_queue();
714
    }
Jack Moffitt's avatar
Jack Moffitt committed
715

716
717
718
    /* Give all the other threads notification to shut down */
    thread_cond_broadcast(&global.shutdown_cond);

719
720
721
    /* 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
722
723
}

724
725
726

/* Called when activating a source. Verifies that the source count is not
 * exceeded and applies any initial parameters.
727
 */
Marvin Scholz's avatar
Marvin Scholz committed
728
int connection_complete_source(source_t *source, int response)
729
{
730
    ice_config_t *config;
731

Marvin Scholz's avatar
Marvin Scholz committed
732
    global_lock();
733
    ICECAST_LOG_DEBUG("sources count is %d", global.sources);
734

735
    config = config_get_config();
Marvin Scholz's avatar
Marvin Scholz committed
736
    if (global.sources < config->source_limit) {
737
        const char *contenttype;
738
        mount_proxy *mountinfo;
739
740
741
742
        format_type_t format_type;

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

Marvin Scholz's avatar
Marvin Scholz committed
746
            if (format_type == FORMAT_ERROR) {
747
                config_release_config();
748
                global_unlock();
749
                if (response) {
750
                    client_send_error_by_id(source->client, ICECAST_ERROR_CON_CONTENT_TYPE_NOSYS);
751
752
                    source->client = NULL;
                }
753
                ICECAST_LOG_WARN("Content-type \"%s\" not supported, dropping source", contenttype);
754
755
                return -1;
            }
756
757
758
759
        } else if (source->parser->req_type == httpp_req_put) {
            config_release_config();
            global_unlock();
            if (response) {
760
                client_send_error_by_id(source->client, ICECAST_ERROR_CON_NO_CONTENT_TYPE_GIVEN);
761
762
763
764
765
766
767
768
                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!");
769
            format_type = FORMAT_TYPE_GENERIC;
770
771
        }

Marvin Scholz's avatar
Marvin Scholz committed
772
        if (format_get_plugin (format_type, source) < 0) {
773
774
            global_unlock();
            config_release_config();
Marvin Scholz's avatar
Marvin Scholz committed
775
            if (response) {
776
                client_send_error_by_id(source->client, ICECAST_ERROR_CON_INTERNAL_FORMAT_ALLOC_ERROR);
777
778
                source->client = NULL;
            }
779
            ICECAST_LOG_WARN("plugin format failed for \"%s\"", source->mount);
780
781
782
            return -1;
        }

783
        global.sources++;
Marvin Scholz's avatar
Marvin Scholz committed
784
        stats_event_args(NULL, "sources", "%d", global.sources);
785
        global_unlock();
786

787
        source->running = 1;
Marvin Scholz's avatar
Marvin Scholz committed
788
789
        mountinfo = config_find_mount(config, source->mount, MOUNT_TYPE_NORMAL);
        source_update_settings(config, source, mountinfo);
790
        config_release_config();
791
        slave_rebuild_mounts();
792
793

        source->shutdown_rwlock = &_source_shutdown_rwlock;
794
        ICECAST_LOG_DEBUG("source is ready to start");
795
796
797

        return 0;
    }
798
    ICECAST_LOG_WARN("Request to add source when maximum source limit "
Marvin Scholz's avatar
Marvin Scholz committed
799
        "reached %d", global.sources);
800
801
802
803

    global_unlock();
    config_release_config();

Marvin Scholz's avatar
Marvin Scholz committed
804
    if (response) {
805
        client_send_error_by_id(source->client, ICECAST_ERROR_CON_SOURCE_CLIENT_LIMIT);
806
807
        source->client = NULL;
    }
808
809
810
811

    return -1;
}

Marvin Scholz's avatar
Marvin Scholz committed
812
static inline void source_startup(client_t *client, const char *uri)
813
814
{
    source_t *source;
Marvin Scholz's avatar
Marvin Scholz committed
815
    source = source_reserve(uri);
816

Marvin Scholz's avatar
Marvin Scholz committed
817
    if (source) {
818
        source->client = client;
819
820
        source->parser = client->parser;
        source->con = client->con;
Marvin Scholz's avatar
Marvin Scholz committed
821
822
823
        if (connection_complete_source(source, 1) < 0) {
            source_clear_source(source);
            source_free_source(source);
824
825
826
            return;
        }
        client->respcode = 200;
Philipp Schafft's avatar
Philipp Schafft committed
827
828
829
830
        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 */
831
            client_send_bytes(client, "OK2\r\nicy-caps:11\r\n\r\n", 20); /* TODO: Replace Magic Number! */
832
            source->shoutcast_compat = 1;
Marvin Scholz's avatar
Marvin Scholz committed
833
            source_client_callback(client, source);
Philipp Schafft's avatar
Philipp Schafft committed
834
        } else {
Marvin Scholz's avatar
Marvin Scholz committed
835
            refbuf_t *ok = refbuf_new(PER_CLIENT_REFBUF_SIZE);
836
            const char *expectcontinue;
837
            const char *transfer_encoding;
838
            int status_to_send = 200;
839
            ssize_t ret;
840

841
842
843
844
            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) {
845
                    client_send_error_by_id(client, ICECAST_ERROR_CON_UNIMPLEMENTED);
846
847
848
849
                    return;
                }
            }

850
851
852
853
854
855
856
            /* 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
857
                ICECAST_LOG_WARN("OS doesn't support case insensitive substring checks...");
858
859
860
861
862
863
864
                if (strstr (expectcontinue, "100-continue") != NULL)
#endif
                {
                    status_to_send = 100;
                }
            }

865
            client->respcode = 200;
866
867
            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
868
            ok->len = strlen(ok->data);
869
            refbuf_release(client->refbuf);
870
            client->refbuf = ok;
Marvin Scholz's avatar
Marvin Scholz committed
871
            fserve_add_client_callback(client, source_client_callback, source);
872
        }
Marvin Scholz's avatar
Marvin Scholz committed
873
    } else {
874
        client_send_error_by_id(client, ICECAST_ERROR_CON_MOUNT_IN_USE);
875
        ICECAST_LOG_WARN("Mountpoint %s in use", uri);
876
    }
877
878
}

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

Marvin Scholz's avatar
Marvin Scholz committed
885
    if (uri[0] != '/') {
Philipp Schafft's avatar
Philipp Schafft committed
886
        ICECAST_LOG_WARN("source mountpoint not starting with /");
887
        client_send_error_by_id(client, ICECAST_ERROR_CON_MOUNTPOINT_NOT_STARTING_WITH_SLASH);
888
        return;
889
    }
890

Philipp Schafft's avatar
Philipp Schafft committed
891
892
893
894
    source_startup(client, uri);
}


Marvin Scholz's avatar
Marvin Scholz committed
895
static void _handle_stats_request(client_t *client, char *uri)
Philipp Schafft's avatar
Philipp Schafft committed
896
897
898
{
    stats_event_inc(NULL, "stats_connections");

899
    client->respcode = 200;
900
    snprintf (client->refbuf->data, PER_CLIENT_REFBUF_SIZE,
Marvin Scholz's avatar
Marvin Scholz committed
901
902
903
        "HTTP/1.0 200 OK\r\n\r\n");
    client->refbuf->len = strlen(client->refbuf->data);
    fserve_add_client_callback(client, stats_callback, NULL);
904
905
}

Philipp Schafft's avatar
Philipp Schafft committed
906
907
908
/* 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
909
static int __add_listener_to_source(source_t *source, client_t *client)
910
{
Philipp Schafft's avatar
Philipp Schafft committed
911
    size_t loop = 10;
Michael Smith's avatar
Michael Smith committed
912

Marvin Scholz's avatar
Marvin Scholz committed
913
    do {
Philipp Schafft's avatar
Philipp Schafft committed
914
        ICECAST_LOG_DEBUG("max on %s is %ld (cur %lu)", source->mount,
Marvin Scholz's avatar
Marvin Scholz committed
915
            source->max_listeners, source->listeners);
Philipp Schafft's avatar
Philipp Schafft committed
916
917
918
919
        if (source->max_listeners == -1)
            break;
        if (source->listeners < (unsigned long)source->max_listeners)
            break;
920

Marvin Scholz's avatar
Marvin Scholz committed
921
        if (loop && source->fallback_when_full && source->fallback_mount) {
Philipp Schafft's avatar
Philipp Schafft committed
922
923
924
            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
925
                    source->mount, source->fallback_mount);
Philipp Schafft's avatar
Philipp Schafft committed
926
927
                return -1;
            }
928
            ICECAST_LOG_INFO("stream full, trying %s", next->mount);
Philipp Schafft's avatar
Philipp Schafft committed
929
930
931
932
933
934
935
936
937
938
939
            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
940
    memset(client->refbuf->data, 0, PER_CLIENT_REFBUF_SIZE);
Philipp Schafft's avatar
Philipp Schafft committed
941
942

    /* lets add the client to the active list */
Marvin Scholz's avatar
Marvin Scholz committed
943
944
945
    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
946

Marvin Scholz's avatar
Marvin Scholz committed
947
    if (source->running == 0 && source->on_demand) {
Philipp Schafft's avatar
Philipp Schafft committed
948
949
950
        /* enable on-demand relay to start, wake up the slave thread */
        ICECAST_LOG_DEBUG("kicking off on-demand relay");
        source->on_demand_req = 1;
951
    }
Philipp Schafft's avatar
Philipp Schafft committed
952
953
954
    ICECAST_LOG_DEBUG("Added client to %s", source->mount);
    return 0;
}
955

Philipp Schafft's avatar
Philipp Schafft committed
956
957
958
959
960
961
962
963
964
965
966
967
/* count the number of clients on a mount with same username and same role as the given one */
static inline ssize_t __count_user_role_on_mount (source_t *source, client_t *client) {
    ssize_t ret = 0;
    avl_node *node;

    avl_tree_rlock(source->client_tree);
    node = avl_get_first(source->client_tree);
    while (node) {
        client_t *existing_client = (client_t *)node->key;
        if (existing_client->username && client->username &&
            strcmp(existing_client->username, client->username) == 0 &&
            existing_client->role && client->role &&
Marvin Scholz's avatar
Marvin Scholz committed
968
            strcmp(existing_client->role, client->role) == 0) {