00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00026 #ifndef STRUCTURES_H
00027 #define STRUCTURES_H
00028
00029 #include "platform.h"
00030 #include "microspdy.h"
00031 #include "io.h"
00032
00033
00038 enum SPDY_CONTROL_FRAME_TYPES
00039 {
00044 SPDY_CONTROL_FRAME_TYPES_SYN_STREAM = 1,
00045
00050 SPDY_CONTROL_FRAME_TYPES_SYN_REPLY = 2,
00051
00059 SPDY_CONTROL_FRAME_TYPES_RST_STREAM = 3,
00060
00070 SPDY_CONTROL_FRAME_TYPES_SETTINGS = 4,
00071
00080 SPDY_CONTROL_FRAME_TYPES_PING = 6,
00081
00087 SPDY_CONTROL_FRAME_TYPES_GOAWAY = 7,
00088
00096 SPDY_CONTROL_FRAME_TYPES_HEADERS = 8,
00097
00106 SPDY_CONTROL_FRAME_TYPES_WINDOW_UPDATE = 9,
00107
00120 SPDY_CONTROL_FRAME_TYPES_CREDENTIAL = 11
00121 };
00122
00123
00129 enum SPDY_SESSION_STATUS
00130 {
00135 SPDY_SESSION_STATUS_CLOSING = 0,
00136
00140 SPDY_SESSION_STATUS_WAIT_FOR_HEADER = 1,
00141
00147 SPDY_SESSION_STATUS_WAIT_FOR_SUBHEADER = 2,
00148
00153 SPDY_SESSION_STATUS_WAIT_FOR_BODY = 3,
00154
00158 SPDY_SESSION_STATUS_IGNORE_BYTES= 4,
00159
00165 SPDY_SESSION_STATUS_FLUSHING = 5,
00166 };
00167
00168
00172 enum SPDY_SYN_STREAM_FLAG
00173 {
00177 SPDY_SYN_STREAM_FLAG_FIN = 1,
00178
00182 SPDY_SYN_STREAM_FLAG_UNIDIRECTIONAL = 2
00183 };
00184
00185
00189 enum SPDY_SYN_REPLY_FLAG
00190 {
00194 SPDY_SYN_REPLY_FLAG_FIN = 1
00195 };
00196
00197
00201 enum SPDY_DATA_FLAG
00202 {
00206 SPDY_DATA_FLAG_FIN = 1,
00207
00213 SPDY_DATA_FLAG_COMPRESS = 2
00214 };
00215
00219 enum SPDY_RST_STREAM_STATUS
00220 {
00225 SPDY_RST_STREAM_STATUS_PROTOCOL_ERROR = 1,
00226
00231 SPDY_RST_STREAM_STATUS_INVALID_STREAM = 2,
00232
00237 SPDY_RST_STREAM_STATUS_REFUSED_STREAM = 3,
00238
00243 SPDY_RST_STREAM_STATUS_UNSUPPORTED_VERSION = 4,
00244
00249 SPDY_RST_STREAM_STATUS_CANCEL = 5,
00250
00255 SPDY_RST_STREAM_STATUS_INTERNAL_ERROR = 6,
00256
00261 SPDY_RST_STREAM_STATUS_FLOW_CONTROL_ERROR = 7,
00262
00266 SPDY_RST_STREAM_STATUS_STREAM_IN_USE = 8,
00267
00272 SPDY_RST_STREAM_STATUS_STREAM_ALREADY_CLOSED = 9,
00273
00278 SPDY_RST_STREAM_STATUS_INVALID_CREDENTIALS = 10,
00279
00288 SPDY_RST_STREAM_STATUS_FRAME_TOO_LARGE = 11
00289 };
00290
00291
00295 enum SPDY_GOAWAY_STATUS
00296 {
00300 SPDY_GOAWAY_STATUS_OK = 0,
00301
00306 SPDY_GOAWAY_STATUS_PROTOCOL_ERROR = 1,
00307
00312 SPDY_GOAWAY_STATUS_INTERNAL_ERROR = 11
00313 };
00314
00315
00316 struct SPDYF_Stream;
00317
00318 struct SPDYF_Response_Queue;
00319
00320
00334 typedef int
00335 (*SPDYF_NewDataCallback) (void * cls,
00336 struct SPDYF_Stream *stream,
00337 const void * buf,
00338 size_t size,
00339 bool more);
00340
00341
00351 typedef int
00352 (*SPDYF_NewStreamCallback) (void *cls,
00353 struct SPDYF_Stream * stream);
00354
00355
00369 typedef void
00370 (*SPDYF_ResponseQueueResultCallback) (void * cls,
00371 struct SPDYF_Response_Queue *response_queue,
00372 enum SPDY_RESPONSE_RESULT status);
00373
00374
00379 struct __attribute__((__packed__)) SPDYF_Control_Frame
00380 {
00381 uint16_t version : 15;
00382 uint16_t control_bit : 1;
00383 uint16_t type;
00384 uint32_t flags : 8;
00385 uint32_t length : 24;
00386 };
00387
00388
00392 struct __attribute__((__packed__)) SPDYF_Data_Frame
00393 {
00394 uint32_t stream_id : 31;
00395 uint32_t control_bit : 1;
00396 uint32_t flags : 8;
00397 uint32_t length : 24;
00398 };
00399
00400
00404 struct SPDYF_Response_Queue
00405 {
00409 struct SPDYF_Response_Queue *next;
00410
00414 struct SPDYF_Response_Queue *prev;
00415
00419 struct SPDYF_Stream *stream;
00420
00424 struct SPDY_Response *response;
00425
00430 struct SPDYF_Control_Frame *control_frame;
00431
00436 struct SPDYF_Data_Frame *data_frame;
00437
00441 void *data;
00442
00446 int (* process_response_handler)(struct SPDY_Session *session);
00447
00452 SPDYF_ResponseQueueResultCallback frqcb;
00453
00457 void *frqcb_cls;
00458
00462 SPDY_ResponseResultCallback rrcb;
00463
00467 void *rrcb_cls;
00468
00472 size_t data_size;
00473
00478 bool is_data;
00479 };
00480
00481
00482
00486 struct SPDY_NameValue
00487 {
00491 struct SPDY_NameValue *next;
00492
00496 struct SPDY_NameValue *prev;
00497
00501 char *name;
00502
00507 char **value;
00508
00512 unsigned int num_values;
00513 };
00514
00515
00519 struct SPDYF_Stream
00520 {
00524 struct SPDYF_Stream *next;
00525
00529 struct SPDYF_Stream *prev;
00530
00534 struct SPDY_Session *session;
00535
00539 struct SPDY_NameValue *headers;
00540
00544 void *cls;
00545
00549 uint32_t stream_id;
00550
00554 uint32_t assoc_stream_id;
00555
00559 uint32_t window_size;
00560
00564 uint8_t priority;
00565
00571 uint8_t slot;
00572
00576 bool flag_unidirectional;
00577
00583 bool is_in_closed;
00584
00590 bool is_out_closed;
00591
00595 bool is_server_initiator;
00596 };
00597
00598
00602 struct SPDY_Session
00603 {
00610 z_stream zlib_recv_stream;
00611
00618 z_stream zlib_send_stream;
00619
00623 struct SPDY_Session *next;
00624
00628 struct SPDY_Session *prev;
00629
00633 struct SPDY_Daemon *daemon;
00634
00638 struct sockaddr *addr;
00639
00644 struct SPDYF_Stream *streams_head;
00645
00649 struct SPDYF_Stream *streams_tail;
00650
00655 void *io_context;
00656
00660 struct SPDYF_Response_Queue *response_queue_head;
00661
00665 struct SPDYF_Response_Queue *response_queue_tail;
00666
00670 void *read_buffer;
00671
00675 void *write_buffer;
00676
00680 void (*frame_handler) (struct SPDY_Session * session);
00681
00685 void *frame_handler_cls;
00686
00691 void *user_cls;
00692
00696 SPDYF_IONewSession fio_new_session;
00697
00701 SPDYF_IOCloseSession fio_close_session;
00702
00706 SPDYF_IORecv fio_recv;
00707
00711 SPDYF_IOSend fio_send;
00712
00716 SPDYF_IOIsPending fio_is_pending;
00717
00721 SPDYF_IOBeforeWrite fio_before_write;
00722
00726 SPDYF_IOAfterWrite fio_after_write;
00727
00734 size_t read_ignore_bytes;
00735
00742 size_t read_buffer_size;
00743
00748 size_t read_buffer_offset;
00749
00753 size_t read_buffer_beginning;
00754
00759 size_t write_buffer_size;
00760
00765 size_t write_buffer_offset;
00766
00770 size_t write_buffer_beginning;
00771
00776 unsigned long long last_activity;
00777
00783 int socket_fd;
00784
00788 socklen_t addr_len;
00789
00794 uint32_t last_in_stream_id;
00795
00800 uint32_t last_out_stream_id;
00801
00809 uint32_t last_replied_to_stream_id;
00810
00816 uint32_t current_stream_id;
00817
00825 uint32_t max_num_frames;
00826
00831 enum SPDY_SESSION_STATUS status;
00832
00840 bool read_closed;
00841
00846 bool is_goaway_sent;
00847
00853 bool is_goaway_received;
00854 };
00855
00856
00860 struct SPDY_Daemon
00861 {
00862
00866 struct SPDY_Session *sessions_head;
00867
00871 struct SPDY_Session *sessions_tail;
00872
00876 struct SPDY_Session *cleanup_head;
00877
00881 struct SPDY_Session *cleanup_tail;
00882
00886 void *io_context;
00887
00891 char *certfile;
00892
00897 char *keyfile;
00898
00899
00903 struct sockaddr *address;
00904
00909 SPDY_NewSessionCallback new_session_cb;
00910
00914 SPDY_SessionClosedCallback session_closed_cb;
00915
00919 SPDY_NewRequestCallback new_request_cb;
00920
00925 SPDY_NewDataCallback received_data_cb;
00926
00930 SPDYF_NewDataCallback freceived_data_cb;
00931
00935 void *cls;
00936
00940 SPDYF_NewStreamCallback fnew_stream_cb;
00941
00945 void *fcls;
00946
00950 SPDYF_IOInit fio_init;
00951
00955 SPDYF_IODeinit fio_deinit;
00956
00961 unsigned long long session_timeout;
00962
00966 int socket_fd;
00967
00976 uint32_t max_num_frames;
00977
00981 enum SPDY_DAEMON_OPTION options;
00982
00986 enum SPDY_DAEMON_FLAG flags;
00987
00991 enum SPDY_IO_SUBSYSTEM io_subsystem;
00992
00996 uint16_t port;
00997 };
00998
00999
01003 struct SPDY_Response
01004 {
01009 void *headers;
01010
01015 void *data;
01016
01022 SPDY_ResponseCallback rcb;
01023
01027 void *rcb_cls;
01028
01032 size_t headers_size;
01033
01037 size_t data_size;
01038
01044 uint32_t rcb_block_size;
01045 };
01046
01047
01048
01049
01050
01059 #define DLL_insert(head,tail,element) do { \
01060 (element)->next = (head); \
01061 (element)->prev = NULL; \
01062 if ((tail) == NULL) \
01063 (tail) = element; \
01064 else \
01065 (head)->prev = element; \
01066 (head) = (element); } while (0)
01067
01068
01078 #define DLL_remove(head,tail,element) do { \
01079 if ((element)->prev == NULL) \
01080 (head) = (element)->next; \
01081 else \
01082 (element)->prev->next = (element)->next; \
01083 if ((element)->next == NULL) \
01084 (tail) = (element)->prev; \
01085 else \
01086 (element)->next->prev = (element)->prev; \
01087 (element)->next = NULL; \
01088 (element)->prev = NULL; } while (0)
01089
01090
01097 #if HAVE_BIG_ENDIAN
01098 #define SPDYF_CONTROL_FRAME_HTON(frame)
01099 #else
01100 #define SPDYF_CONTROL_FRAME_HTON(frame) do { \
01101 (*((uint16_t *) frame )) = (*((uint8_t *) (frame) +1 )) | ((*((uint8_t *) frame ))<<8);\
01102 (frame)->type = htons((frame)->type); \
01103 (frame)->length = HTON24((frame)->length); \
01104 } while (0)
01105 #endif
01106
01107
01114 #if HAVE_BIG_ENDIAN
01115 #define SPDYF_CONTROL_FRAME_NTOH(frame)
01116 #else
01117 #define SPDYF_CONTROL_FRAME_NTOH(frame) do { \
01118 (*((uint16_t *) frame )) = (*((uint8_t *) (frame) +1 )) | ((*((uint8_t *) frame ))<<8);\
01119 (frame)->type = ntohs((frame)->type); \
01120 (frame)->length = NTOH24((frame)->length); \
01121 } while (0)
01122 #endif
01123
01124
01131 #if HAVE_BIG_ENDIAN
01132 #define SPDYF_DATA_FRAME_HTON(frame)
01133 #else
01134 #define SPDYF_DATA_FRAME_HTON(frame) do { \
01135 *((uint32_t *) frame ) = htonl(*((uint32_t *) frame ));\
01136 (frame)->length = HTON24((frame)->length); \
01137 } while (0)
01138 #endif
01139
01140
01147 #if HAVE_BIG_ENDIAN
01148 #define SPDYF_DATA_FRAME_NTOH(frame)
01149 #else
01150 #define SPDYF_DATA_FRAME_NTOH(frame) do { \
01151 *((uint32_t *) frame ) = ntohl(*((uint32_t *) frame ));\
01152 (frame)->length = NTOH24((frame)->length); \
01153 } while (0)
01154 #endif
01155
01156
01178 struct SPDYF_Response_Queue *
01179 SPDYF_response_queue_create(bool is_data,
01180 void *data,
01181 size_t data_size,
01182 struct SPDY_Response *response,
01183 struct SPDYF_Stream *stream,
01184 bool closestream,
01185 SPDYF_ResponseQueueResultCallback frqcb,
01186 void *frqcb_cls,
01187 SPDY_ResponseResultCallback rrcb,
01188 void *rrcb_cls);
01189
01190
01196 void
01197 SPDYF_response_queue_destroy(struct SPDYF_Response_Queue *response_queue);
01198
01199
01208 int
01209 SPDYF_name_value_is_empty(struct SPDY_NameValue *container);
01210
01211
01224 int
01225 SPDYF_name_value_from_stream(void *stream,
01226 size_t size,
01227 struct SPDY_NameValue ** container);
01228
01229
01241 ssize_t
01242 SPDYF_name_value_to_stream(struct SPDY_NameValue * container[],
01243 int num_containers,
01244 void **stream);
01245
01246 #endif