client.c 5.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/* 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).
 */

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

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

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

Karl Heyes's avatar
Karl Heyes committed
26
27
28
#include "thread/thread.h"
#include "avl/avl.h"
#include "httpp/httpp.h"
Jack Moffitt's avatar
Jack Moffitt committed
29

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

#include "client.h"
#include "logging.h"

39
40
41
#undef CATMODULE
#define CATMODULE "client"

42
43
/* should be called with global lock held */
int client_create (client_t **c_ptr, connection_t *con, http_parser_t *parser)
Jack Moffitt's avatar
Jack Moffitt committed
44
{
45
    ice_config_t *config;
46
    client_t *client = (client_t *)calloc(1, sizeof(client_t));
47
48
49
50
51
52
    int ret = -1;

    if (client == NULL)
        return -1;

    config = config_get_config ();
53
54

    global.clients++;
55
56
57
58
    if (config->client_limit < global.clients)
        WARN2 ("server client limit reached (%d/%d)", config->client_limit, global.clients);
    else
        ret = 0;
Jack Moffitt's avatar
Jack Moffitt committed
59

60
61
62
    config_release_config ();

    stats_event_args (NULL, "clients", "%d", global.clients);
63
64
65
    client->con = con;
    client->parser = parser;
    client->pos = 0;
66
    client->write_to_client = format_generic_write_to_client;
67
    *c_ptr = client;
Jack Moffitt's avatar
Jack Moffitt committed
68

69
    return ret;
Jack Moffitt's avatar
Jack Moffitt committed
70
71
72
73
}

void client_destroy(client_t *client)
{
Karl Heyes's avatar
Karl Heyes committed
74
75
    if (client == NULL)
        return;
76
77
78
79

    if (release_client (client))
        return;

80
    /* write log entry if ip is set (some things don't set it, like outgoing 
81
82
     * slave requests
     */
Karl Heyes's avatar
Karl Heyes committed
83
    if (client->respcode && client->parser)
84
85
        logging_access(client);
    
86
87
    if (client->con)
        connection_close(client->con);
Karl Heyes's avatar
Karl Heyes committed
88
89
    if (client->parser)
        httpp_destroy(client->parser);
Jack Moffitt's avatar
Jack Moffitt committed
90

91
92
93
94
95
    global_lock ();
    global.clients--;
    stats_event_args (NULL, "clients", "%d", global.clients);
    global_unlock ();

Karl Heyes's avatar
Karl Heyes committed
96
97
98
    /* drop ref counts if need be */
    if (client->refbuf)
        refbuf_release (client->refbuf);
Karl Heyes's avatar
Karl Heyes committed
99

100
101
102
103
    /* we need to free client specific format data (if any) */
    if (client->free_client_data)
        client->free_client_data (client);

Michael Smith's avatar
Michael Smith committed
104
    free(client->username);
Karl Heyes's avatar
Karl Heyes committed
105
    free(client->password);
Michael Smith's avatar
Michael Smith committed
106

107
    free(client);
Jack Moffitt's avatar
Jack Moffitt committed
108
}
109

110
111
112
113

/* helper function for reading data from a client */
int client_read_bytes (client_t *client, void *buf, unsigned len)
{
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    int bytes;
    
    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);
        if (client->refbuf->len < len)
        {
            char *ptr = client->refbuf->data;
            memmove (ptr, ptr+len, client->refbuf->len - len);
        }
        client->refbuf->len -= len;
        return len;
    }
    bytes = sock_read_bytes (client->con->sock, buf, len);
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    if (bytes > 0)
        return bytes;

    if (bytes < 0)
    {
        if (sock_recoverable (sock_error()))
            return -1;
        WARN0 ("source connection has died");
    }
    client->con->error = 1;
    return -1;
}


145
146
void client_send_400(client_t *client, char *message) {
    int bytes;
147
    bytes = sock_write(client->con->sock, "HTTP/1.0 400 Bad Request\r\n"
148
149
150
            "Content-Type: text/html\r\n\r\n"
            "<b>%s</b>\r\n", message);
    if(bytes > 0) client->con->sent_bytes = bytes;
151
    client->respcode = 400;
152
153
154
    client_destroy(client);
}

155
156
157
158
159
160
161
void client_send_404(client_t *client, char *message) {

    int bytes;
    bytes = sock_write(client->con->sock, "HTTP/1.0 404 File Not Found\r\n"
            "Content-Type: text/html\r\n\r\n"
            "<b>%s</b>\r\n", message);
    if(bytes > 0) client->con->sent_bytes = bytes;
162
    client->respcode = 404;
163
164
165
    client_destroy(client);
}

Michael Smith's avatar
Michael Smith committed
166
167
168
void client_send_504(client_t *client, char *message) {
    int bytes;
    client->respcode = 504;
169
    bytes = sock_write(client->con->sock, 
Michael Smith's avatar
Michael Smith committed
170
171
172
            "HTTP/1.0 504 Server Full\r\n"
            "Content-Type: text/html\r\n\r\n"
            "<b>%s</b>\r\n", message);
173
174
       if (bytes > 0) client->con->sent_bytes = bytes;
    client_destroy(client);
Michael Smith's avatar
Michael Smith committed
175
176
}

177
178
179
180
181
182
183
void client_send_401(client_t *client) {
    int bytes = sock_write(client->con->sock, 
            "HTTP/1.0 401 Authentication Required\r\n"
            "WWW-Authenticate: Basic realm=\"Icecast2 Server\"\r\n"
            "\r\n"
            "You need to authenticate\r\n");
    if(bytes > 0) client->con->sent_bytes = bytes;
184
    client->respcode = 401;
185
186
    client_destroy(client);
}
187
188
189
190
191
192
193
194
195
196

void client_send_403(client_t *client) {
    int bytes = sock_write(client->con->sock, 
            "HTTP/1.0 403 Forbidden\r\n"
            "\r\n"
            "Access restricted.\r\n");
    if(bytes > 0) client->con->sent_bytes = bytes;
    client->respcode = 403;
    client_destroy(client);
}
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212


/* helper function for sending the data to a client */
int client_send_bytes (client_t *client, const void *buf, unsigned len)
{
    int ret = sock_write_bytes (client->con->sock, buf, len);
    if (ret < 0 && !sock_recoverable (sock_error()))
    {
        DEBUG0 ("Client connection died");
        client->con->error = 1;
    }
    if (ret > 0)
        client->con->sent_bytes += ret;
    return ret;
}

Karl Heyes's avatar
Karl Heyes committed
213
214
215
216
217
void client_set_queue (client_t *client, refbuf_t *refbuf)
{
    refbuf_t *to_release = client->refbuf;

    client->refbuf = refbuf;
Karl Heyes's avatar
Karl Heyes committed
218
219
    if (refbuf)
        refbuf_addref (client->refbuf);
Karl Heyes's avatar
Karl Heyes committed
220
221
222
223
224
    client->pos = 0;
    if (to_release)
        refbuf_release (to_release);
}