1 /* Icecast
2  *
3  * This program is distributed under the GNU General Public License, version 2.
4  * A copy of this license is included with this source.
5  *
6  * Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
7  *                      Michael Smith <msmith@xiph.org>,
8  *                      oddsock <oddsock@xiph.org>,
9  *                      Karl Heyes <karl@xiph.org>
10  *                      and others (see AUTHORS for details).
11  */
12 
13 /* client.h
14 **
15 ** client data structions and function definitions
16 **
17 */
18 #ifndef __CLIENT_H__
19 #define __CLIENT_H__
20 
21 typedef struct _client_tag client_t;
22 typedef struct _worker_t worker_t;
23 
24 #include "cfgfile.h"
25 #include "connection.h"
26 #include "refbuf.h"
27 #include "httpp/httpp.h"
28 #include "compat.h"
29 #include "thread/thread.h"
30 
31 struct _worker_t
32 {
33     int running;
34     int count, pending_count;
35     int move_allocations;
36     spin_t lock;
37     FD_t wakeup_fd[2];
38 
39     client_t *pending_clients;
40     client_t **pending_clients_tail,
41              *clients;
42     client_t **last_p;
43     thread_type *thread;
44     struct timespec current_time;
45     uint64_t time_ms;
46     uint64_t wakeup_ms;
47     struct _worker_t *next;
48 };
49 
50 
51 extern worker_t *workers;
52 extern int worker_count;
53 extern rwlock_t workers_lock;
54 
55 struct _client_functions
56 {
57     int  (*process)(struct _client_tag *client);
58     void (*release)(struct _client_tag *client);
59 };
60 
61 struct _client_tag
62 {
63     uint64_t schedule_ms;
64     char *wakeup;
65 
66     /* various states the client could be in */
67     unsigned int flags;
68 
69     /* position in first buffer */
70     unsigned int pos;
71 
72     client_t *next_on_worker;
73 
74     /* functions to process client */
75     struct _client_functions *ops;
76 
77     /* function to check if refbuf needs updating */
78     int (*check_buffer)(struct _client_tag *client);
79 
80     /* generic handle */
81     void *shared_data;
82 
83     /* current mountpoint */
84     const char *mount;
85 
86     /* the clients connection */
87     connection_t connection;
88 
89     /* the client's http headers */
90     http_parser_t *parser;
91 
92     /* reference to incoming connection details */
93     listener_t *server_conn;
94 
95     /* is client getting intro data */
96     off_t intro_offset;
97 
98     /* where in the queue the client is */
99     refbuf_t *refbuf;
100 
101     /* byte count in queue */
102     uint64_t queue_pos;
103 
104     /* Client username, if authenticated */
105     char *username;
106 
107     /* Client password, if authenticated */
108     char *password;
109 
110     /* Format-handler-specific data for this client */
111     void *format_data;
112 
113     /* the worker the client is attached to */
114     worker_t *worker;
115 
116     uint64_t timer_start;
117     uint64_t counter;
118     uint64_t aux_data;
119 
120     /* function to call to release format specific resources */
121     void (*free_client_data)(struct _client_tag *client);
122 
123     /* http response code for this client */
124     int respcode;
125     unsigned int throttle;
126 };
127 
128 void client_register (client_t *client);
129 void client_destroy(client_t *client);
130 int  client_add_cors (client_t *client, char *buf, int remain);
131 int  client_send_options(client_t *client);
132 int  client_send_501(client_t *client);
133 int  client_send_416(client_t *client);
134 int  client_send_404(client_t *client, const char *message);
135 int  client_send_401(client_t *client, const char *realm);
136 int  client_send_403(client_t *client, const char *reason);
137 int  client_send_403redirect (client_t *client, const char *mount, const char *reason);
138 int  client_send_400(client_t *client, const char *message);
139 int  client_send_302(client_t *client, const char *location);
140 int  client_send_bytes (client_t *client, const void *buf, unsigned len);
141 int  client_send_buffer_callback (client_t *client, int(*callback)(client_t*));
142 int  client_read_bytes (client_t *client, void *buf, unsigned len);
143 void client_set_queue (client_t *client, refbuf_t *refbuf);
144 int  client_compare (void *compare_arg, void *a, void *b);
145 int  client_connected (client_t *client);
146 const char *client_keepalive_header (client_t *client);
147 
148 int  client_change_worker (client_t *client, worker_t *dest_worker);
149 void client_add_worker (client_t *client);
150 void client_add_incoming (client_t *client);
151 worker_t *worker_selected (void);
152 void worker_balance_trigger (time_t now);
153 void workers_adjust (int new_count);
154 void worker_wakeup (worker_t *worker);
155 void worker_logger_init (void);
156 void worker_logger (int stop);
157 int  is_worker_incoming (worker_t *w);
158 
159 
160 /* client flags bitmask */
161 #define CLIENT_ACTIVE               (1)
162 #define CLIENT_AUTHENTICATED        (1<<1)
163 #define CLIENT_IS_SLAVE             (1<<2)
164 #define CLIENT_IN_FSERVE            (1<<3)
165 #define CLIENT_NO_CONTENT_LENGTH    (1<<4)
166 #define CLIENT_HAS_INTRO_CONTENT    (1<<5)
167 #define CLIENT_SKIP_ACCESSLOG       (1<<6)
168 #define CLIENT_HAS_MOVED            (1<<7)
169 #define CLIENT_IP_BAN_LIFT          (1<<8)
170 #define CLIENT_META_INSTREAM        (1<<9)
171 #define CLIENT_HIJACKER             (1<<10)
172 #define CLIENT_RANGE_END            (1<<11)
173 #define CLIENT_KEEPALIVE            (1<<12)
174 #define CLIENT_CHUNKED              (1<<13)
175 #define CLIENT_FORMAT_BIT           (1<<16)
176 
177 #endif  /* __CLIENT_H__ */
178