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

Jack Moffitt's avatar
Jack Moffitt committed
14
/* client.c
15
16
17
18
 **
 ** client interface implementation
 **
 */
Jack Moffitt's avatar
Jack Moffitt committed
19

20
21
22
23
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
24
25
26
#include <stdlib.h>
#include <string.h>

Marvin Scholz's avatar
Marvin Scholz committed
27
28
29
#include "common/thread/thread.h"
#include "common/avl/avl.h"
#include "common/httpp/httpp.h"
Jack Moffitt's avatar
Jack Moffitt committed
30

31
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
32
33
#include "connection.h"
#include "refbuf.h"
Karl Heyes's avatar
Karl Heyes committed
34
#include "format.h"
35
#include "stats.h"
36
#include "fserve.h"
Jack Moffitt's avatar
Jack Moffitt committed
37
38

#include "client.h"
Philipp Schafft's avatar
Philipp Schafft committed
39
#include "auth.h"
Jack Moffitt's avatar
Jack Moffitt committed
40
#include "logging.h"
41

42
43
#include "util.h"

Philipp Schafft's avatar
Philipp Schafft committed
44
45
46
/* for ADMIN_COMMAND_ERROR */
#include "admin.h"

47
48
49
#ifdef _WIN32
#define snprintf _snprintf
#endif
Jack Moffitt's avatar
Jack Moffitt committed
50

51
52
53
#undef CATMODULE
#define CATMODULE "client"

54
55
static inline void client_send_500(client_t *client, const char *message);

56
57
58
59
60
/* create a client_t with the provided connection and parser details. Return
 * 0 on success, -1 if server limit has been reached.  In either case a
 * client_t is returned just in case a message needs to be returned. Should
 * be called with global lock held.
 */
61
int client_create(client_t **c_ptr, connection_t *con, http_parser_t *parser)
Jack Moffitt's avatar
Jack Moffitt committed
62
{
63
    ice_config_t *config;
64
    client_t *client = (client_t *) calloc(1, sizeof(client_t));
65
66
67
    int ret = -1;

    if (client == NULL)
68
        abort();
69

70
    config = config_get_config();
71
72

    global.clients++;
73
    if (config->client_limit < global.clients)
74
        ICECAST_LOG_WARN("server client limit reached (%d/%d)", config->client_limit, global.clients);
75
76
    else
        ret = 0;
Jack Moffitt's avatar
Jack Moffitt committed
77

78
79
80
    config_release_config ();

    stats_event_args (NULL, "clients", "%d", global.clients);
81
82
    client->con = con;
    client->parser = parser;
Philipp Schafft's avatar
Philipp Schafft committed
83
84
    client->protocol = ICECAST_PROTOCOL_HTTP;
    client->admin_command = ADMIN_COMMAND_ERROR;
85
86
    client->refbuf = refbuf_new (PER_CLIENT_REFBUF_SIZE);
    client->refbuf->len = 0; /* force reader code to ignore buffer contents */
87
    client->pos = 0;
88
    client->write_to_client = format_generic_write_to_client;
89
    *c_ptr = client;
Jack Moffitt's avatar
Jack Moffitt committed
90

91
    return ret;
Jack Moffitt's avatar
Jack Moffitt committed
92
93
94
95
}

void client_destroy(client_t *client)
{
Philipp Schafft's avatar
Philipp Schafft committed
96
    ICECAST_LOG_DEBUG("Called to destory client %p", client);
Karl Heyes's avatar
Karl Heyes committed
97
98
    if (client == NULL)
        return;
99

100
101
102
103
104
105
106
107
    /* release the buffer now, as the buffer could be on the source queue
     * and may of disappeared after auth completes */
    if (client->refbuf)
    {
        refbuf_release (client->refbuf);
        client->refbuf = NULL;
    }

Philipp Schafft's avatar
Philipp Schafft committed
108
    if (auth_release_client(client))
109
110
        return;

111
    /* write log entry if ip is set (some things don't set it, like outgoing 
112
113
     * slave requests
     */
Karl Heyes's avatar
Karl Heyes committed
114
    if (client->respcode && client->parser)
115
        logging_access(client);
116

117
118
    if (client->con)
        connection_close(client->con);
Karl Heyes's avatar
Karl Heyes committed
119
120
    if (client->parser)
        httpp_destroy(client->parser);
Jack Moffitt's avatar
Jack Moffitt committed
121

122
    global_lock();
123
    global.clients--;
124
125
    stats_event_args(NULL, "clients", "%d", global.clients);
    global_unlock();
126

127
128
    /* we need to free client specific format data (if any) */
    if (client->free_client_data)
129
        client->free_client_data(client);
130

Michael Smith's avatar
Michael Smith committed
131
    free(client->username);
Karl Heyes's avatar
Karl Heyes committed
132
    free(client->password);
Philipp Schafft's avatar
Philipp Schafft committed
133
134
    free(client->role);
    acl_release(client->acl);
Michael Smith's avatar
Michael Smith committed
135

136
    free(client);
Jack Moffitt's avatar
Jack Moffitt committed
137
}
138

139
/* helper function for reading data from a client */
140
int client_read_bytes(client_t *client, void *buf, unsigned len)
141
{
142
    int bytes;
143

144
145
146
147
148
149
    if (client->refbuf && client->refbuf->len)
    {
        /* we have data to read from a refbuf first */
        if (client->refbuf->len < len)
            len = client->refbuf->len;
        memcpy (buf, client->refbuf->data, len);
150
        if (len < client->refbuf->len)
151
152
153
154
155
156
157
        {
            char *ptr = client->refbuf->data;
            memmove (ptr, ptr+len, client->refbuf->len - len);
        }
        client->refbuf->len -= len;
        return len;
    }
158
    bytes = client->con->read (client->con, buf, len);
159

160
    if (bytes == -1 && client->con->error)
161
        ICECAST_LOG_DEBUG("reading from connection has failed");
162
163

    return bytes;
164
165
}

166
void client_send_error(client_t *client, int status, int plain, const char *message)
167
{
168
169
    ssize_t ret;

170
171
172
173
174
    if (status == 500) {
         client_send_500(client, message);
         return;
    }

175
    ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0,
176
                                 0, status, NULL,
177
                                 plain ? "text/plain" : "text/html", "utf-8",
178
                                 plain ? message : "", NULL);
179

180
181
182
183
184
185
    if (ret == -1 || ret >= PER_CLIENT_REFBUF_SIZE) {
        ICECAST_LOG_ERROR("Dropping client as we can not build response headers.");
        client_send_500(client, "Header generation failed.");
        return;
    }

186
187
188
189
    if (!plain)
        snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
                 "<html><head><title>Error %i</title></head><body><b>%i - %s</b></body></html>\r\n",
                 status, status, message);
190

191
    client->respcode = status;
192
193
    client->refbuf->len = strlen (client->refbuf->data);
    fserve_add_client (client, NULL);
194
195
}

196
197
198
199
200
201
void client_send_100(client_t *client)
{
    /* On demand inject a HTTP/1.1 100 Continue to make sure clients are happy */
    sock_write (client->con->sock, "HTTP/1.1 100 Continue\r\n\r\n");
}

202
/* this function is designed to work even if client is in bad state */
203
static inline void client_send_500(client_t *client, const char *message) {
204
205
    const char header[] = "HTTP/1.0 500 Internal Server Error\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n"
                          "500 - Internal Server Error\n---------------------------\n";
Philipp Schafft's avatar
Philipp Schafft committed
206
207
    const ssize_t header_len = sizeof(header) - 1;
    ssize_t ret;
208
209
210
211
212
213
214
215
216

    ret = client_send_bytes(client, header, header_len);

    /* only send message if we have one AND if header could have transmitted completly */
    if (message && ret == header_len)
        client_send_bytes(client, message, strlen(message));

    client_destroy(client);
}
217
218

/* helper function for sending the data to a client */
219
int client_send_bytes(client_t *client, const void *buf, unsigned len)
220
{
221
    int ret = client->con->send(client->con, buf, len);
222
223

    if (client->con->error)
224
        ICECAST_LOG_DEBUG("Client connection died");
225

226
227
228
    return ret;
}

229
void client_set_queue(client_t *client, refbuf_t *refbuf)
Karl Heyes's avatar
Karl Heyes committed
230
231
232
233
{
    refbuf_t *to_release = client->refbuf;

    client->refbuf = refbuf;
Karl Heyes's avatar
Karl Heyes committed
234
    if (refbuf)
235
        refbuf_addref(client->refbuf);
Karl Heyes's avatar
Karl Heyes committed
236
237
    client->pos = 0;
    if (to_release)
238
        refbuf_release(to_release);
Karl Heyes's avatar
Karl Heyes committed
239
}