cse.h 10.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
/*
 * Copyright (c) 1999-2008 Caucho Technology.  All rights reserved.
 *
 * This file is part of Resin(R) Open Source
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Resin Open Source is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Resin Open Source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Resin Open Source; if not, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 */

#ifndef CSE_H
#define CSE_H

#ifdef B64
#define PTR jlong
#else
#define PTR jint
#endif
/*
#ifdef WIN32
typedef time_t unsigned int;
#else
#include <time.h>
#endif
*/
#include <time.h>

#ifndef WIN32
#define O_BINARY 0
#define O_TEXT 0
#endif

typedef struct mem_pool_t mem_pool_t;

#define CONN_POOL_SIZE 128

typedef struct registry_t {
  struct registry_t *parent;
  struct registry_t *next;
  struct registry_t *prev;
  struct registry_t *first;
  struct registry_t *last;

  char *key;
  char *value;
} registry_t;
  
typedef struct stream_t stream_t;

typedef struct srun_t {
  int is_valid;
  
  char *hostname;
  struct in_addr *host;
  int port;

  int connect_timeout;       /* time the connect() call should wait  */
  int live_time;             /* time an idle socket should live      */
  int dead_time;             /* time a dead srun stays dead          */
  int read_timeout;          /* how long to wait for a read (iis)    */
  int send_buffer_size;      /* how big the send buffer is           */
  
  void *lock;                /* lock specific to the srun            */
  
  int is_dead;               /* true if the connect() failed         */
  time_t fail_time;    /* when the last connect() failed       */

  void *ssl;                 /* ssl context                          */
  int (*open) (stream_t *);
  int (*read) (stream_t *, void *buf, int len);
  int (*write) (stream_t *, const void *buf, int len);
  int (*close) (int socket, void *ssl);

  struct conn_t {            /* pool of idle sockets (ring)          */
    struct srun_t *srun;     /* owning srun                          */
    int socket;              /* socket file descriptor               */
    void *ssl;               /* ssl context                          */
    unsigned int last_time;  /* last time the socket was used        */
  } conn_pool[CONN_POOL_SIZE];

  int conn_head;             /* head of the pool (most recent used)  */
  int conn_tail;             /* tail of the pool (least recent used) */
  
  int max_sockets;
  int is_default;

  int active_sockets;        /* current number of active connections */
} srun_t;

typedef struct depend_t {
  struct depend_t *next;

  char *path;
  int last_modified;
  int last_size;
} depend_t;

typedef struct cluster_srun_t {
  char *id;
  
  srun_t *srun;
  struct cluster_t *cluster;

  int is_valid;
  int is_backup;
  int index;
} cluster_srun_t;

typedef struct cluster_t {
  char *id;

  struct cluster_t *next;

  struct config_t *config;
  
  cluster_srun_t *srun_list;
  
  int srun_capacity;
  int srun_size;
  
  int round_robin_index;
} cluster_t;

typedef struct location_t {
  struct location_t *next;
  struct web_app_t *application;
  
  char *prefix;
  char *suffix;
  int is_exact;

  int ignore;     /* If true, a matching will defer to the web server. */
} location_t;

typedef struct web_app_t {
  struct web_app_t *next;
  
  struct resin_host_t *host;
  
  char *context_path;

  int has_data;
  
  struct location_t *locations;
} web_app_t;

typedef struct resin_host_t {
  struct resin_host_t *next;
  struct resin_host_t *canonical;

  struct config_t *config;
  struct mem_pool_t *pool;
  
  char *name;
  int port;

  int has_data;
  time_t last_update;
  char etag[32];  /* etag for the last configuration update. */

  cluster_t cluster;
  
  struct web_app_t *applications;

  char error_message[1024];
  char config_source[1024];
} resin_host_t;

typedef struct config_t {
  void *p;

  void *web_pool;

  int has_config; /* true if there's config information, i.e. not defalt */
  
  void *lock;
  void *config_lock;
  void *server_lock;
  char *error;
  
  int enable_caucho_status;
  int disable_sticky_sessions;
  int disable_session_failover;

  char *path;
  char *resin_home;
  
  char work_dir[1024];
  char config_path[1024];
  char config_file[1024];
  
  char error_page[1024];
  char session_url_prefix[256];
  char alt_session_url_prefix[256];
  char session_cookie[256];

  char error_message[1024];
  char *iis_priority;
  int override_iis_authentication;

  int default_host_max;
  
  srun_t **srun_list;
  int srun_capacity;

  cluster_t *cluster_head;
  
  cluster_t config_cluster;

  resin_host_t *hosts;

  /* for direct dispatching */
  resin_host_t *manual_host;

  /* how often to check for updates */
  int update_interval;
  time_t last_update;
  time_t last_file_update;
  time_t start_time;
  int update_count;
  int is_updating;
} config_t;

/* windows needs this smaller than 64k */
#define BUF_LENGTH (16 * 1024)

struct stream_t {
  struct cluster_srun_t *cluster_srun;
  void *pool;
  void *web_pool;
  int update_count;

  int socket;
  void *ssl;
  
  struct config_t *config;

  unsigned char read_buf[BUF_LENGTH + 8];
  int read_offset;
  int read_length;

  unsigned char write_buf[BUF_LENGTH + 8];
  int write_length;

  int sent_data;
};

#define HMUX_CHANNEL        'C'
#define HMUX_ACK            'A'
#define HMUX_ERROR          'E'
#define HMUX_YIELD          'Y'
#define HMUX_QUIT           'Q'
#define HMUX_EXIT           'X'

#define HMUX_DATA           'D'
#define HMUX_URL            'U'
#define HMUX_STRING         'S'
#define HMUX_HEADER         'H'
#define HMUX_META_HEADER    'M'
#define HMUX_PROTOCOL       'P'

#define CSE_NULL            '?'
#define CSE_PATH_INFO       'b'
#define CSE_PROTOCOL        'c'
#define CSE_REMOTE_USER     'd'
#define CSE_QUERY_STRING    'e'
#define CSE_SERVER_PORT     'g'
#define CSE_REMOTE_HOST     'h'
#define CSE_REMOTE_ADDR     'i'
#define CSE_REMOTE_PORT     'j'
#define CSE_REAL_PATH       'k'
#define CSE_AUTH_TYPE       'n'
#define CSE_URI             'o'
#define CSE_CONTENT_LENGTH  'p'
#define CSE_CONTENT_TYPE    'q'
#define CSE_IS_SECURE       'r'
#define CSE_SESSION_GROUP   's'
#define CSE_CLIENT_CERT     't'
#define CSE_SERVER_TYPE	    'u'

#define HMUX_METHOD         'm'
#define HMUX_FLUSH          'f'
#define HMUX_SERVER_NAME    'v'
#define HMUX_STATUS         's'
#define HMUX_CLUSTER        'c'
#define HMUX_SRUN           's'
#define HMUX_SRUN_BACKUP    'b'
#define HMUX_SRUN_SSL       'e'
#define HMUX_UNAVAILABLE    'u'
#define HMUX_WEB_APP_UNAVAILABLE 'U'

#define CSE_HEADER          'H'
#define CSE_VALUE           'V'

#define CSE_STATUS          'S'
#define CSE_SEND_HEADER     'G'

#define CSE_PING            'P'
#define CSE_QUERY           'Q'

#define CSE_ACK             'A'
#define CSE_DATA            'D'
#define CSE_FLUSH           'F'
#define CSE_KEEPALIVE       'K'
#define CSE_END             'Z'
#define CSE_CLOSE           'X'

#define HMUX_DISPATCH_PROTOCOL 0x102
#define HMUX_QUERY 0x102

#ifdef DEBUG
#define LOG(x) cse_log x
#define ERR(x) cse_log x
#else
#define LOG(x)
#define ERR(x)
#endif

#ifndef WIN32
#undef closesocket
#define closesocket(x) close(x)
#endif

mem_pool_t *cse_create_pool(config_t *config);
void cse_free_pool(mem_pool_t *);

/* base malloc */
void *cse_malloc(int size);

void *cse_alloc(mem_pool_t *p, int size);
char *cse_strdup(mem_pool_t *p, const char *string);

void cse_error(config_t *config, char *format, ...);

registry_t *cse_parse(FILE *is, config_t *config, char *path);

registry_t *cse_next_link(registry_t *reg, char *key);
char *cse_find_value(registry_t *reg, char *key);
char *cse_find_inherited_value(registry_t *reg, char *key, char *deflt);
void cse_print_registry(registry_t *registry);

void cse_init_config(config_t *config);
/*
void cse_update_config(config_t *config, time_t now);
*/

resin_host_t *cse_match_request(config_t *config, const char *host, int port,
				const char *url, int should_escape, time_t now);
resin_host_t *cse_match_host(config_t *config,
			     const char *host, int port,
			     time_t now);

cluster_srun_t *
cse_add_cluster_server(mem_pool_t *pool, cluster_t *cluster,
		       const char *host, int port, const char *id,
		       int index, int is_backup, int is_ssl);

cluster_srun_t *cse_add_host(mem_pool_t *pool, cluster_t *cluster,
			     const char *host, int port);
cluster_srun_t *cse_add_backup(mem_pool_t *pool, cluster_t *cluster,
			       const char *host, int port);

void cse_add_config_server(mem_pool_t *pool, config_t *config,
			   const char *host, int port);

void cse_log(char *fmt, ...);

int cse_session_from_string(char *source, char *cookie, int *backup_index);

int cse_open(stream_t *s, cluster_t *cluster, cluster_srun_t *srun,
	     void *p, int wait);
void cse_close(stream_t *s, char *msg);
void cse_close_stream(stream_t *s);
void cse_close_sockets(config_t *config);
void cse_recycle(stream_t *s, time_t now);
int cse_flush(stream_t *s);
int cse_fill_buffer(stream_t *s);
int cse_read_byte(stream_t *s);
void cse_write_byte(stream_t *s, int ch);
void cse_write(stream_t *s, const char *buf, int length);
int cse_read_all(stream_t *s, char *buf, int len);
int cse_skip(stream_t *s, int length);
int cse_read_limit(stream_t *s, char *buf, int buflen, int readlen);

void cse_write_packet(stream_t *s, char code, const char *buf, int length);
void cse_write_string(stream_t *s, char code, const char *buf);
int cse_read_string(stream_t *s, char *buf, int length);

void cse_kill_socket_cleanup(int socket, void *pool);
void cse_set_socket_cleanup(int socket, void *pool);
int
cse_open_connection(stream_t *s, cluster_t *cluster,
		    int session_index, int backup_offset,
                    time_t request_time, void *pool);
int cse_open_any_connection(stream_t *s, cluster_t *cluster,
			    time_t now);
int cse_open_live_connection(stream_t *s, cluster_t *cluster,
			     time_t now);
void close_srun(srun_t *srun, time_t now);

void *cse_create_lock(config_t *config);
void cse_free_lock(config_t *config, void *lock);
int cse_lock(void *lock);
void cse_unlock(void *lock);

void cse_close_all();

void cse_add_depend(config_t *config, char *path);

void hmux_start_channel(stream_t *s, unsigned short channel);
void hmux_write_int(stream_t *s, char code, int i);
void hmux_write_string(stream_t *s, char code, const char *value);
void hmux_write_close(stream_t *s);
int hmux_read_len(stream_t *s);
int hmux_read_string(stream_t *s, char *buffer, int len);

#endif /* CSE_H */