Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 286 lines (230 sloc) 7.435 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
/*
Copyright (C) 2006, 2007, 2008, 2009, 2010 Anthony Catel <a.catel@weelya.com>

This file is part of APE Server.
APE 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.

APE 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. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with APE ; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

/* main.h */


#ifndef _MAIN_H
#define _MAIN_H


#include <stdarg.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

#include "hash.h"

#define MAX_IO 4096
#define DEFAULT_BUFFER_SIZE 2048

#define MAX_NICK_LEN 16 // move to module
#define MAX_CHAN_LEN 40
#define MAX_MSG_LEN 1024
#define MAX_RAW_LEN 1024

#define TIMEOUT_SEC 45

#define SERVER_NAME "APE.Server"
#define _VERSION "1.01dev"

int server_is_running;

struct _transport_properties {
struct {
struct {
char *val;
int len;
} left;
struct {
char *val;
int len;
} right;
} padding;
};

struct _ape_transports {
struct {
struct _transport_properties properties;
} jsonp;

struct {
struct _transport_properties properties;
} xhrstreaming;

struct {
struct _transport_properties properties;
} sse;

struct {
struct _transport_properties properties;
} websocket;
};

typedef struct _http_state http_state;
struct _http_state
{
struct _http_header_line *hlines;

char *uri;

void *buffer_addr;
const char *data;
const char *host;

int pos;
int contentlength;
int read;

unsigned short int step;
unsigned short int type; /* HTTP_GET or HTTP_POST */
unsigned short int error;
};

typedef struct _websocket_state websocket_state;
struct _websocket_state
{
struct _http_state *http;
const char *data;
unsigned short int offset;
unsigned short int error;
};

typedef enum {
STREAM_IN,
STREAM_OUT,
STREAM_SERVER,
STREAM_DELEGATE
} ape_socket_t;

typedef enum {
STREAM_ONLINE,
STREAM_PROGRESS
} ape_socket_state_t;

typedef struct _ape_buffer ape_buffer;
struct _ape_buffer {
char *data;
void *slot;

unsigned int size;
unsigned int length;

int islot;
};

typedef struct _acetables
{
struct {
struct _callback_hook *head;
struct _callback_hook *foot;
} cmd_hook;

struct {
struct _ape_proxy *list;
struct _ape_proxy_cache *hosts;
} proxy;

struct {
struct _ticks_callback *timers;
unsigned int ntimers;
} timers;

struct {
unsigned int lvl;
unsigned int use_syslog;
int fd;
} logs;

struct _ape_transports transports;

HTBL *hLogin;
HTBL *hSessid;
HTBL *hLusers;
HTBL *hCallback;
HTBL *hPubid;

struct apeconfig *srv;
struct _callback_hook *bad_cmd_callbacks;
struct USERS *uHead;
struct _socks_bufout *bufout;
struct _ace_plugins *plugins;
struct _fdevent *events;
struct _ape_socket **co;
struct _extend *properties;

const char *confs_path;

int is_daemon;
int basemem;
unsigned int nConnected;
} acetables;


typedef struct _ape_parser ape_parser;
struct _ape_parser {
void (*parser_func)(struct _ape_socket *, acetables *);
void (*destroy)(struct _ape_parser *);
void (*onready)(struct _ape_parser *, acetables *);
void *data;
struct _ape_socket *socket;
short int ready;
};

typedef struct _ape_socket ape_socket;
struct _ape_socket {
struct {
void (*on_accept)(struct _ape_socket *client, acetables *g_ape);
void (*on_connect)(struct _ape_socket *client, acetables *g_ape); /* ajouter in/out ? ou faire un onaccept */
void (*on_disconnect)(struct _ape_socket *client, acetables *g_ape);
void (*on_read)(struct _ape_socket *client, struct _ape_buffer *buf, size_t offset, acetables *g_ape);
void (*on_read_lf)(struct _ape_socket *client, char *data, acetables *g_ape);
void (*on_data_completly_sent)(struct _ape_socket *client, acetables *g_ape);
void (*on_write)(struct _ape_socket *client, acetables *g_ape);
} callbacks;

ape_parser parser;

ape_buffer buffer_in;

char ip_client[16];
long int idle;

void *attach;
void *data;

int fd;
int burn_after_writing;

ape_socket_state_t state;
ape_socket_t stream_type;
};

#define HEADER_DEFAULT "HTTP/1.1 200 OK\r\nPragma: no-cache\r\nCache-Control: no-cache, must-revalidate\r\nExpires: Thu, 27 Dec 1986 07:30:00 GMT\r\nContent-Type: text/html\r\n\r\n"
#define HEADER_DEFAULT_LEN 144

#define HEADER_SSE "HTTP/1.1 200 OK\r\nPragma: no-cache\r\nCache-Control: no-cache, must-revalidate\r\nExpires: Thu, 27 Dec 1986 07:30:00 GMT\r\nContent-Type: application/x-dom-event-stream\r\n\r\n"
#define HEADER_SSE_LEN 165

#define HEADER_XHR "HTTP/1.1 200 OK\r\nPragma: no-cache\r\nCache-Control: no-cache, must-revalidate\r\nExpires: Thu, 27 Dec 1986 07:30:00 GMT\r\nContent-Type: application/x-ape-event-stream\r\n\r\n "
#define HEADER_XHR_LEN 421

#define CONTENT_NOTFOUND "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>APE Server</title></head><body><h1>APE Server</h1><p>No command given.</p><hr><address>http://www.ape-project.org/ - Server "_VERSION" (Build "__DATE__" "__TIME__")</address></body></html>"

/* http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-55 : The first three lines in each case are hard-coded (the exact case and order matters); */
#define WEBSOCKET_HARDCODED_HEADERS_OLD "HTTP/1.1 101 Web Socket Protocol Handshake\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\n"
#define WEBSOCKET_HARDCODED_HEADERS_NEW "HTTP/1.1 101 WebSocket Protocol Handshake\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\n"

#define FIRE_EVENT(event, ret, arg...) \
if (g_ape->plugins != NULL) { \
ace_plugins *cplug = g_ape->plugins; \
while (cplug != NULL) { \
if (cplug->cb != NULL && cplug->cb->c_##event != NULL && cplug->fire.c_##event == 0) { \
cplug->fire.c_##event = 1; \
ret = cplug->cb->c_##event(arg); \
cplug->fire.c_##event = 0; \
\
if (ret == NULL) { \
return NULL; \
} \
\
break; \
} \
cplug = cplug->next; \
} \
} \
if (ret != NULL) { \
return ret; \
}
#define FIRE_EVENT_NULL(event, arg...) \
if (g_ape->plugins != NULL) { \
ace_plugins *cplug = g_ape->plugins; \
while (cplug != NULL) { \
if (cplug->cb != NULL && cplug->cb->c_##event != NULL && cplug->fire.c_##event == 0) { \
cplug->fire.c_##event = 1; \
cplug->cb->c_##event(arg); \
cplug->fire.c_##event = 0; \
return; \
break; \
} \
cplug = cplug->next; \
} \
}

#define FIRE_EVENT_NONSTOP(event, arg...) \
if (g_ape->plugins != NULL) { \
ace_plugins *cplug = g_ape->plugins; \
while (cplug != NULL) { \
if (cplug->cb != NULL && cplug->cb->c_##event != NULL && cplug->fire.c_##event == 0) { \
cplug->fire.c_##event = 1; \
cplug->cb->c_##event(arg); \
cplug->fire.c_##event = 0; \
} \
cplug = cplug->next; \
} \
}
#endif
Something went wrong with that request. Please try again.