1 /* 2 This file is part of libmicrospdy 3 Copyright Copyright (C) 2012 Andrey Uzunov 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 /** 20 * @file structures.h 21 * @brief internal and public structures -- most of the structs used by 22 * the library are defined here 23 * @author Andrey Uzunov 24 */ 25 26 #ifndef STRUCTURES_H 27 #define STRUCTURES_H 28 29 #include "platform.h" 30 #include "microspdy.h" 31 #include "io.h" 32 33 34 /** 35 * All possible SPDY control frame types. The number is used in the header 36 * of the control frame. 37 */ 38 enum SPDY_CONTROL_FRAME_TYPES 39 { 40 /** 41 * The SYN_STREAM control frame allows the sender to asynchronously 42 * create a stream between the endpoints. 43 */ 44 SPDY_CONTROL_FRAME_TYPES_SYN_STREAM = 1, 45 46 /** 47 * SYN_REPLY indicates the acceptance of a stream creation by 48 * the recipient of a SYN_STREAM frame. 49 */ 50 SPDY_CONTROL_FRAME_TYPES_SYN_REPLY = 2, 51 52 /** 53 * The RST_STREAM frame allows for abnormal termination of a stream. 54 * When sent by the creator of a stream, it indicates the creator 55 * wishes to cancel the stream. When sent by the recipient of a 56 * stream, it indicates an error or that the recipient did not want 57 * to accept the stream, so the stream should be closed. 58 */ 59 SPDY_CONTROL_FRAME_TYPES_RST_STREAM = 3, 60 61 /** 62 * A SETTINGS frame contains a set of id/value pairs for 63 * communicating configuration data about how the two endpoints may 64 * communicate. SETTINGS frames can be sent at any time by either 65 * endpoint, are optionally sent, and are fully asynchronous. When 66 * the server is the sender, the sender can request that 67 * configuration data be persisted by the client across SPDY 68 * sessions and returned to the server in future communications. 69 */ 70 SPDY_CONTROL_FRAME_TYPES_SETTINGS = 4, 71 72 /** 73 * The PING control frame is a mechanism for measuring a minimal 74 * round-trip time from the sender. It can be sent from the client 75 * or the server. Recipients of a PING frame should send an 76 * identical frame to the sender as soon as possible (if there is 77 * other pending data waiting to be sent, PING should take highest 78 * priority). Each ping sent by a sender should use a unique ID. 79 */ 80 SPDY_CONTROL_FRAME_TYPES_PING = 6, 81 82 /** 83 * The GOAWAY control frame is a mechanism to tell the remote side 84 * of the connection to stop creating streams on this session. It 85 * can be sent from the client or the server. 86 */ 87 SPDY_CONTROL_FRAME_TYPES_GOAWAY = 7, 88 89 /** 90 * The HEADERS frame augments a stream with additional headers. It 91 * may be optionally sent on an existing stream at any time. 92 * Specific application of the headers in this frame is 93 * application-dependent. The name/value header block within this 94 * frame is compressed. 95 */ 96 SPDY_CONTROL_FRAME_TYPES_HEADERS = 8, 97 98 /** 99 * The WINDOW_UPDATE control frame is used to implement per stream 100 * flow control in SPDY. Flow control in SPDY is per hop, that is, 101 * only between the two endpoints of a SPDY connection. If there are 102 * one or more intermediaries between the client and the origin 103 * server, flow control signals are not explicitly forwarded by the 104 * intermediaries. 105 */ 106 SPDY_CONTROL_FRAME_TYPES_WINDOW_UPDATE = 9, 107 108 /** 109 * The CREDENTIAL control frame is used by the client to send 110 * additional client certificates to the server. A SPDY client may 111 * decide to send requests for resources from different origins on 112 * the same SPDY session if it decides that that server handles both 113 * origins. For example if the IP address associated with both 114 * hostnames matches and the SSL server certificate presented in the 115 * initial handshake is valid for both hostnames. However, because 116 * the SSL connection can contain at most one client certificate, 117 * the client needs a mechanism to send additional client 118 * certificates to the server. 119 */ 120 SPDY_CONTROL_FRAME_TYPES_CREDENTIAL = 11 121 }; 122 123 124 /** 125 * SPDY_SESSION_STATUS is used to show the current receiving state 126 * of each session, i.e. what is expected to come now, and how it should 127 * be handled. 128 */ 129 enum SPDY_SESSION_STATUS 130 { 131 /** 132 * The session is in closing state, do not read read anything from 133 * it. Do not write anything to it. 134 */ 135 SPDY_SESSION_STATUS_CLOSING = 0, 136 137 /** 138 * Wait for new SPDY frame to come. 139 */ 140 SPDY_SESSION_STATUS_WAIT_FOR_HEADER = 1, 141 142 /** 143 * The standard 8 byte header of the SPDY frame was received and 144 * handled. Wait for the specific (sub)headers according to the 145 * frame type. 146 */ 147 SPDY_SESSION_STATUS_WAIT_FOR_SUBHEADER = 2, 148 149 /** 150 * The specific (sub)headers were received and handled. Wait for the 151 * "body", i.e. wait for the name/value pairs compressed by zlib. 152 */ 153 SPDY_SESSION_STATUS_WAIT_FOR_BODY = 3, 154 155 /** 156 * Ignore all the bytes read from the socket, e.g. larger frames. 157 */ 158 SPDY_SESSION_STATUS_IGNORE_BYTES= 4, 159 160 /** 161 * The session is in pre-closing state, do not read read anything 162 * from it. In this state the output queue will be written to the 163 * socket. 164 */ 165 SPDY_SESSION_STATUS_FLUSHING = 5, 166 }; 167 168 169 /** 170 * Specific flags for the SYN_STREAM control frame. 171 */ 172 enum SPDY_SYN_STREAM_FLAG 173 { 174 /** 175 * The sender won't send any more frames on this stream. 176 */ 177 SPDY_SYN_STREAM_FLAG_FIN = 1, 178 179 /** 180 * The sender creates this stream as unidirectional. 181 */ 182 SPDY_SYN_STREAM_FLAG_UNIDIRECTIONAL = 2 183 }; 184 185 186 /** 187 * Specific flags for the SYN_REPLY control frame. 188 */ 189 enum SPDY_SYN_REPLY_FLAG 190 { 191 /** 192 * The sender won't send any more frames on this stream. 193 */ 194 SPDY_SYN_REPLY_FLAG_FIN = 1 195 }; 196 197 198 /** 199 * Specific flags for the data frame. 200 */ 201 enum SPDY_DATA_FLAG 202 { 203 /** 204 * The sender won't send any more frames on this stream. 205 */ 206 SPDY_DATA_FLAG_FIN = 1, 207 208 /** 209 * The data in the frame is compressed. 210 * This flag appears only in the draft on ietf.org but not on 211 * chromium.org. 212 */ 213 SPDY_DATA_FLAG_COMPRESS = 2 214 }; 215 216 /** 217 * Status code within RST_STREAM control frame. 218 */ 219 enum SPDY_RST_STREAM_STATUS 220 { 221 /** 222 * This is a generic error, and should only be used if a more 223 * specific error is not available. 224 */ 225 SPDY_RST_STREAM_STATUS_PROTOCOL_ERROR = 1, 226 227 /** 228 * This is returned when a frame is received for a stream which is 229 * not active. 230 */ 231 SPDY_RST_STREAM_STATUS_INVALID_STREAM = 2, 232 233 /** 234 * Indicates that the stream was refused before any processing has 235 * been done on the stream. 236 */ 237 SPDY_RST_STREAM_STATUS_REFUSED_STREAM = 3, 238 239 /** 240 * Indicates that the recipient of a stream does not support the 241 * SPDY version requested. 242 */ 243 SPDY_RST_STREAM_STATUS_UNSUPPORTED_VERSION = 4, 244 245 /** 246 * Used by the creator of a stream to indicate that the stream is 247 * no longer needed. 248 */ 249 SPDY_RST_STREAM_STATUS_CANCEL = 5, 250 251 /** 252 * This is a generic error which can be used when the implementation 253 * has internally failed, not due to anything in the protocol. 254 */ 255 SPDY_RST_STREAM_STATUS_INTERNAL_ERROR = 6, 256 257 /** 258 * The endpoint detected that its peer violated the flow control 259 * protocol. 260 */ 261 SPDY_RST_STREAM_STATUS_FLOW_CONTROL_ERROR = 7, 262 263 /** 264 * The endpoint received a SYN_REPLY for a stream already open. 265 */ 266 SPDY_RST_STREAM_STATUS_STREAM_IN_USE = 8, 267 268 /** 269 * The endpoint received a data or SYN_REPLY frame for a stream 270 * which is half closed. 271 */ 272 SPDY_RST_STREAM_STATUS_STREAM_ALREADY_CLOSED = 9, 273 274 /** 275 * The server received a request for a resource whose origin does 276 * not have valid credentials in the client certificate vector. 277 */ 278 SPDY_RST_STREAM_STATUS_INVALID_CREDENTIALS = 10, 279 280 /** 281 * The endpoint received a frame which this implementation could not 282 * support. If FRAME_TOO_LARGE is sent for a SYN_STREAM, HEADERS, 283 * or SYN_REPLY frame without fully processing the compressed 284 * portion of those frames, then the compression state will be 285 * out-of-sync with the other endpoint. In this case, senders of 286 * FRAME_TOO_LARGE MUST close the session. 287 */ 288 SPDY_RST_STREAM_STATUS_FRAME_TOO_LARGE = 11 289 }; 290 291 292 /** 293 * Status code within GOAWAY control frame. 294 */ 295 enum SPDY_GOAWAY_STATUS 296 { 297 /** 298 * This is a normal session teardown. 299 */ 300 SPDY_GOAWAY_STATUS_OK = 0, 301 302 /** 303 * This is a generic error, and should only be used if a more 304 * specific error is not available. 305 */ 306 SPDY_GOAWAY_STATUS_PROTOCOL_ERROR = 1, 307 308 /** 309 * This is a generic error which can be used when the implementation 310 * has internally failed, not due to anything in the protocol. 311 */ 312 SPDY_GOAWAY_STATUS_INTERNAL_ERROR = 11 313 }; 314 315 316 struct SPDYF_Stream; 317 318 struct SPDYF_Response_Queue; 319 320 321 /** 322 * Callback for received new data chunk. 323 * 324 * @param cls client-defined closure 325 * @param stream handler 326 * @param buf data chunk from the data 327 * @param size the size of the data chunk 'buf' in bytes 328 * @param more false if this is the last frame received on this stream. Note: 329 * true does not mean that more data will come, exceptional 330 * situation is possible 331 * @return SPDY_YES to continue calling the function, 332 * SPDY_NO to stop calling the function for this stream 333 */ 334 typedef int 335 (*SPDYF_NewDataCallback) (void * cls, 336 struct SPDYF_Stream *stream, 337 const void * buf, 338 size_t size, 339 bool more); 340 341 342 /** 343 * Callback for new stream. To be used in the application layer of the 344 * lib. 345 * 346 * @param cls 347 * @param stream the new stream 348 * @return SPDY_YES on success, 349 * SPDY_NO if error occurs 350 */ 351 typedef int 352 (*SPDYF_NewStreamCallback) (void *cls, 353 struct SPDYF_Stream * stream); 354 355 356 /** 357 * Callback to be called when the response queue object was handled and 358 * the data was already sent. 359 * 360 * @param cls 361 * @param response_queue the SPDYF_Response_Queue structure which will 362 * be cleaned very soon 363 * @param status shows if actually the response was sent or it was 364 * discarded by the lib for any reason (e.g., closing session, 365 * closing stream, stopping daemon, etc.). It is possible that 366 * status indicates an error but part of the response (in one 367 * or several frames) was sent to the client. 368 */ 369 typedef void 370 (*SPDYF_ResponseQueueResultCallback) (void * cls, 371 struct SPDYF_Response_Queue *response_queue, 372 enum SPDY_RESPONSE_RESULT status); 373 374 375 /** 376 * Representation of the control frame's headers, which are common for 377 * all types. 378 */ 379 struct __attribute__((__packed__)) SPDYF_Control_Frame 380 { 381 uint16_t version : 15; 382 uint16_t control_bit : 1; /* always 1 for control frames */ 383 uint16_t type; 384 uint32_t flags : 8; 385 uint32_t length : 24; 386 }; 387 388 389 /** 390 * Representation of the data frame's headers. 391 */ 392 struct __attribute__((__packed__)) SPDYF_Data_Frame 393 { 394 uint32_t stream_id : 31; 395 uint32_t control_bit : 1; /* always 0 for data frames */ 396 uint32_t flags : 8; 397 uint32_t length : 24; 398 }; 399 400 401 /** 402 * Queue of the responses, to be handled (e.g. compressed) and sent later. 403 */ 404 struct SPDYF_Response_Queue 405 { 406 /** 407 * This is a doubly-linked list. 408 */ 409 struct SPDYF_Response_Queue *next; 410 411 /** 412 * This is a doubly-linked list. 413 */ 414 struct SPDYF_Response_Queue *prev; 415 416 /** 417 * Stream (Request) for which is the response. 418 */ 419 struct SPDYF_Stream *stream; 420 421 /** 422 * Response structure with all the data (uncompressed headers) to be sent. 423 */ 424 struct SPDY_Response *response; 425 426 /** 427 * Control frame. The length field should be set after compressing 428 * the headers! 429 */ 430 struct SPDYF_Control_Frame *control_frame; 431 432 /** 433 * Data frame. The length field should be set after compressing 434 * the body! 435 */ 436 struct SPDYF_Data_Frame *data_frame; 437 438 /** 439 * Data to be sent: name/value pairs in control frames or body in data frames. 440 */ 441 void *data; 442 443 /** 444 * Specific handler for different frame types. 445 */ 446 int (* process_response_handler)(struct SPDY_Session *session); 447 448 /** 449 * Callback to be called when the last bytes from the response was sent 450 * to the client. 451 */ 452 SPDYF_ResponseQueueResultCallback frqcb; 453 454 /** 455 * Closure for frqcb. 456 */ 457 void *frqcb_cls; 458 459 /** 460 * Callback to be used by the application layer. 461 */ 462 SPDY_ResponseResultCallback rrcb; 463 464 /** 465 * Closure for rcb. 466 */ 467 void *rrcb_cls; 468 469 /** 470 * Data size. 471 */ 472 size_t data_size; 473 474 /** 475 * True if data frame should be sent. False if control frame should 476 * be sent. 477 */ 478 bool is_data; 479 }; 480 481 482 483 /** 484 * Collection of HTTP headers used in requests and responses. 485 */ 486 struct SPDY_NameValue 487 { 488 /** 489 * This is a doubly-linked list. 490 */ 491 struct SPDY_NameValue *next; 492 493 /** 494 * This is a doubly-linked list. 495 */ 496 struct SPDY_NameValue *prev; 497 498 /** 499 * Null terminated string for name. 500 */ 501 char *name; 502 503 /** 504 * Array of Null terminated strings for value. num_values is the 505 * length of the array. 506 */ 507 char **value; 508 509 /** 510 * Number of values, this is >= 0. 511 */ 512 unsigned int num_values; 513 }; 514 515 516 /** 517 * Represents a SPDY stream 518 */ 519 struct SPDYF_Stream 520 { 521 /** 522 * This is a doubly-linked list. 523 */ 524 struct SPDYF_Stream *next; 525 526 /** 527 * This is a doubly-linked list. 528 */ 529 struct SPDYF_Stream *prev; 530 531 /** 532 * Reference to the SPDY_Session struct. 533 */ 534 struct SPDY_Session *session; 535 536 /** 537 * Name value pairs, sent within the frame which created the stream. 538 */ 539 struct SPDY_NameValue *headers; 540 541 /** 542 * Any object to be used by the application layer. 543 */ 544 void *cls; 545 546 /** 547 * This stream's ID. 548 */ 549 uint32_t stream_id; 550 551 /** 552 * Stream to which this one is associated. 553 */ 554 uint32_t assoc_stream_id; 555 556 /** 557 * The window of the data within data frames. 558 */ 559 uint32_t window_size; 560 561 /** 562 * Stream priority. 0 is the highest, 7 is the lowest. 563 */ 564 uint8_t priority; 565 566 /** 567 * Integer specifying the index in the server's CREDENTIAL vector of 568 * the client certificate to be used for this request The value 0 569 * means no client certificate should be associated with this stream. 570 */ 571 uint8_t slot; 572 573 /** 574 * If initially the stream was created as unidirectional. 575 */ 576 bool flag_unidirectional; 577 578 /** 579 * If the stream won't be used for receiving frames anymore. The 580 * client has sent FLAG_FIN or the stream was terminated with 581 * RST_STREAM. 582 */ 583 bool is_in_closed; 584 585 /** 586 * If the stream won't be used for sending out frames anymore. The 587 * server has sent FLAG_FIN or the stream was terminated with 588 * RST_STREAM. 589 */ 590 bool is_out_closed; 591 592 /** 593 * Which entity (server/client) has created the stream. 594 */ 595 bool is_server_initiator; 596 }; 597 598 599 /** 600 * Represents a SPDY session which is just a TCP connection 601 */ 602 struct SPDY_Session 603 { 604 /** 605 * zlib stream for decompressing all the name/pair values from the 606 * received frames. All the received compressed data must be 607 * decompressed within one context: this stream. Thus, it should be 608 * unique for the session and initialized at its creation. 609 */ 610 z_stream zlib_recv_stream; 611 612 /** 613 * zlib stream for compressing all the name/pair values from the 614 * frames to be sent. All the sent compressed data must be 615 * compressed within one context: this stream. Thus, it should be 616 * unique for the session and initialized at its creation. 617 */ 618 z_stream zlib_send_stream; 619 620 /** 621 * This is a doubly-linked list. 622 */ 623 struct SPDY_Session *next; 624 625 /** 626 * This is a doubly-linked list. 627 */ 628 struct SPDY_Session *prev; 629 630 /** 631 * Reference to the SPDY_Daemon struct. 632 */ 633 struct SPDY_Daemon *daemon; 634 635 /** 636 * Foreign address (of length addr_len). 637 */ 638 struct sockaddr *addr; 639 640 /** 641 * Head of doubly-linked list of the SPDY streams belonging to the 642 * session. 643 */ 644 struct SPDYF_Stream *streams_head; 645 646 /** 647 * Tail of doubly-linked list of the streams. 648 */ 649 struct SPDYF_Stream *streams_tail; 650 651 /** 652 * Unique IO context for the session. Initialized on each creation 653 * (actually when the TCP connection is established). 654 */ 655 void *io_context; 656 657 /** 658 * Head of doubly-linked list of the responses. 659 */ 660 struct SPDYF_Response_Queue *response_queue_head; 661 662 /** 663 * Tail of doubly-linked list of the responses. 664 */ 665 struct SPDYF_Response_Queue *response_queue_tail; 666 667 /** 668 * Buffer for reading requests. 669 */ 670 void *read_buffer; 671 672 /** 673 * Buffer for writing responses. 674 */ 675 void *write_buffer; 676 677 /** 678 * Specific handler for the frame that is currently being received. 679 */ 680 void (*frame_handler) (struct SPDY_Session * session); 681 682 /** 683 * Closure for frame_handler. 684 */ 685 void *frame_handler_cls; 686 687 /** 688 * Extra field to be used by the user with set/get func for whatever 689 * purpose he wants. 690 */ 691 void *user_cls; 692 693 /** 694 * Function to initialize the IO context for a new session. 695 */ 696 SPDYF_IONewSession fio_new_session; 697 698 /** 699 * Function to deinitialize the IO context for a session. 700 */ 701 SPDYF_IOCloseSession fio_close_session; 702 703 /** 704 * Function to read data from socket. 705 */ 706 SPDYF_IORecv fio_recv; 707 708 /** 709 * Function to write data to socket. 710 */ 711 SPDYF_IOSend fio_send; 712 713 /** 714 * Function to check for pending data in IO buffers. 715 */ 716 SPDYF_IOIsPending fio_is_pending; 717 718 /** 719 * Function to call before writing set of frames. 720 */ 721 SPDYF_IOBeforeWrite fio_before_write; 722 723 /** 724 * Function to call after writing set of frames. 725 */ 726 SPDYF_IOAfterWrite fio_after_write; 727 728 /** 729 * Number of bytes that the lib must ignore immediately after they 730 * are read from the TLS socket without adding them to the read buf. 731 * This is needed, for instance, when receiving frame bigger than 732 * the buffer to avoid deadlock situations. 733 */ 734 size_t read_ignore_bytes; 735 736 /** 737 * Size of read_buffer (in bytes). This value indicates 738 * how many bytes we're willing to read into the buffer; 739 * the real buffer is one byte longer to allow for 740 * adding zero-termination (when needed). 741 */ 742 size_t read_buffer_size; 743 744 /** 745 * Position where we currently append data in 746 * read_buffer (last valid position). 747 */ 748 size_t read_buffer_offset; 749 750 /** 751 * Position until where everything was already read 752 */ 753 size_t read_buffer_beginning; 754 755 /** 756 * Size of write_buffer (in bytes). This value indicates 757 * how many bytes we're willing to prepare for writing. 758 */ 759 size_t write_buffer_size; 760 761 /** 762 * Position where we currently append data in 763 * write_buffer (last valid position). 764 */ 765 size_t write_buffer_offset; 766 767 /** 768 * Position until where everything was already written to the socket 769 */ 770 size_t write_buffer_beginning; 771 772 /** 773 * Last time this connection had any activity 774 * (reading or writing). In milliseconds. 775 */ 776 unsigned long long last_activity; 777 778 /** 779 * Socket for this connection. Set to -1 if 780 * this connection has died (daemon should clean 781 * up in that case). 782 */ 783 int socket_fd; 784 785 /** 786 * Length of the foreign address. 787 */ 788 socklen_t addr_len; 789 790 /** 791 * The biggest stream ID for this session for streams initiated 792 * by the client. 793 */ 794 uint32_t last_in_stream_id; 795 796 /** 797 * The biggest stream ID for this session for streams initiated 798 * by the server. 799 */ 800 uint32_t last_out_stream_id; 801 802 /** 803 * This value is updated whenever SYN_REPLY or RST_STREAM are sent 804 * and is used later in GOAWAY frame. 805 * TODO it is not clear in the draft what happens when streams are 806 * not answered in the order of their IDs. Moreover, why should we 807 * send GOAWAY with the ID of received bogus SYN_STREAM with huge ID? 808 */ 809 uint32_t last_replied_to_stream_id; 810 811 /** 812 * Shows the stream id of the currently handled frame. This value is 813 * to be used when sending RST_STREAM in answer to a problematic 814 * frame, e.g. larger than supported. 815 */ 816 uint32_t current_stream_id; 817 818 /** 819 * Maximum number of frames to be written to the socket at once. The 820 * library tries to send max_num_frames in a single call to SPDY_run 821 * for a single session. This means no requests can be received nor 822 * other sessions can send data as long the current one has enough 823 * frames to send and there is no error on writing. 824 */ 825 uint32_t max_num_frames; 826 827 /** 828 * Shows the current receiving state the session, i.e. what is 829 * expected to come now, and how it shold be handled. 830 */ 831 enum SPDY_SESSION_STATUS status; 832 833 /** 834 * Has this socket been closed for reading (i.e. 835 * other side closed the connection)? If so, 836 * we must completely close the connection once 837 * we are done sending our response (and stop 838 * trying to read from this socket). 839 */ 840 bool read_closed; 841 842 /** 843 * If the server sends GOAWAY, it must ignore all SYN_STREAMS for 844 * this session. Normally the server will soon close the TCP session. 845 */ 846 bool is_goaway_sent; 847 848 /** 849 * If the server receives GOAWAY, it must not send new SYN_STREAMS 850 * on this session. Normally the client will soon close the TCP 851 * session. 852 */ 853 bool is_goaway_received; 854 }; 855 856 857 /** 858 * State and settings kept for each SPDY daemon. 859 */ 860 struct SPDY_Daemon 861 { 862 863 /** 864 * Tail of doubly-linked list of our current, active sessions. 865 */ 866 struct SPDY_Session *sessions_head; 867 868 /** 869 * Tail of doubly-linked list of our current, active sessions. 870 */ 871 struct SPDY_Session *sessions_tail; 872 873 /** 874 * Tail of doubly-linked list of connections to clean up. 875 */ 876 struct SPDY_Session *cleanup_head; 877 878 /** 879 * Tail of doubly-linked list of connections to clean up. 880 */ 881 struct SPDY_Session *cleanup_tail; 882 883 /** 884 * Unique IO context for the daemon. Initialized on daemon start. 885 */ 886 void *io_context; 887 888 /** 889 * Certificate file of the server. File path is kept here. 890 */ 891 char *certfile; 892 893 /** 894 * Key file for the certificate of the server. File path is 895 * kept here. 896 */ 897 char *keyfile; 898 899 900 /** 901 * The address to which the listening socket is bound. 902 */ 903 struct sockaddr *address; 904 905 /** 906 * Callback called when a new SPDY session is 907 * established by a client 908 */ 909 SPDY_NewSessionCallback new_session_cb; 910 911 /** 912 * Callback called when a client closes the session 913 */ 914 SPDY_SessionClosedCallback session_closed_cb; 915 916 /** 917 * Callback called when a client sends request 918 */ 919 SPDY_NewRequestCallback new_request_cb; 920 921 /** 922 * Callback called when HTTP POST params are received 923 * after request. To be used by the application layer 924 */ 925 SPDY_NewDataCallback received_data_cb; 926 927 /** 928 * Callback called when DATA frame is received. 929 */ 930 SPDYF_NewDataCallback freceived_data_cb; 931 932 /** 933 * Closure argument for all the callbacks that can be used by the client. 934 */ 935 void *cls; 936 937 /** 938 * Callback called when new stream is created. 939 */ 940 SPDYF_NewStreamCallback fnew_stream_cb; 941 942 /** 943 * Closure argument for all the callbacks defined in the framing layer. 944 */ 945 void *fcls; 946 947 /** 948 * Function to initialize the IO context for the daemon. 949 */ 950 SPDYF_IOInit fio_init; 951 952 /** 953 * Function to deinitialize the IO context for the daemon. 954 */ 955 SPDYF_IODeinit fio_deinit; 956 957 /** 958 * After how many milliseconds of inactivity should 959 * connections time out? Zero for no timeout. 960 */ 961 unsigned long long session_timeout; 962 963 /** 964 * Listen socket. 965 */ 966 int socket_fd; 967 968 /** 969 * This value is inherited by all sessions of the daemon. 970 * Maximum number of frames to be written to the socket at once. The 971 * library tries to send max_num_frames in a single call to SPDY_run 972 * for a single session. This means no requests can be received nor 973 * other sessions can send data as long the current one has enough 974 * frames to send and there is no error on writing. 975 */ 976 uint32_t max_num_frames; 977 978 /** 979 * Daemon's options. 980 */ 981 enum SPDY_DAEMON_OPTION options; 982 983 /** 984 * Daemon's flags. 985 */ 986 enum SPDY_DAEMON_FLAG flags; 987 988 /** 989 * IO subsystem type used by daemon and all its sessions. 990 */ 991 enum SPDY_IO_SUBSYSTEM io_subsystem; 992 993 /** 994 * Listen port. 995 */ 996 uint16_t port; 997 }; 998 999 1000 /** 1001 * Represents a SPDY response. 1002 */ 1003 struct SPDY_Response 1004 { 1005 /** 1006 * Raw uncompressed stream of the name/value pairs in SPDY frame 1007 * used for the HTTP headers. 1008 */ 1009 void *headers; 1010 1011 /** 1012 * Raw stream of the data to be sent. Equivalent to the body in HTTP 1013 * response. 1014 */ 1015 void *data; 1016 1017 /** 1018 * Callback function to be used when the response data is provided 1019 * with callbacks. In this case data must be NULL and data_size must 1020 * be 0. 1021 */ 1022 SPDY_ResponseCallback rcb; 1023 1024 /** 1025 * Extra argument to rcb. 1026 */ 1027 void *rcb_cls; 1028 1029 /** 1030 * Length of headers. 1031 */ 1032 size_t headers_size; 1033 1034 /** 1035 * Length of data. 1036 */ 1037 size_t data_size; 1038 1039 /** 1040 * The callback func will be called to get that amount of bytes to 1041 * put them into a DATA frame. It is either user preffered or 1042 * the maximum supported by the lib value. 1043 */ 1044 uint32_t rcb_block_size; 1045 }; 1046 1047 1048 /* Macros for handling data and structures */ 1049 1050 1051 /** 1052 * Insert an element at the head of a DLL. Assumes that head, tail and 1053 * element are structs with prev and next fields. 1054 * 1055 * @param head pointer to the head of the DLL (struct ? *) 1056 * @param tail pointer to the tail of the DLL (struct ? *) 1057 * @param element element to insert (struct ? *) 1058 */ 1059 #define DLL_insert(head,tail,element) do { \ 1060 (element)->next = (head); \ 1061 (element)->prev = NULL; \ 1062 if ((tail) == NULL) \ 1063 (tail) = element; \ 1064 else \ 1065 (head)->prev = element; \ 1066 (head) = (element); } while (0) 1067 1068 1069 /** 1070 * Remove an element from a DLL. Assumes 1071 * that head, tail and element are structs 1072 * with prev and next fields. 1073 * 1074 * @param head pointer to the head of the DLL (struct ? *) 1075 * @param tail pointer to the tail of the DLL (struct ? *) 1076 * @param element element to remove (struct ? *) 1077 */ 1078 #define DLL_remove(head,tail,element) do { \ 1079 if ((element)->prev == NULL) \ 1080 (head) = (element)->next; \ 1081 else \ 1082 (element)->prev->next = (element)->next; \ 1083 if ((element)->next == NULL) \ 1084 (tail) = (element)->prev; \ 1085 else \ 1086 (element)->next->prev = (element)->prev; \ 1087 (element)->next = NULL; \ 1088 (element)->prev = NULL; } while (0) 1089 1090 1091 /** 1092 * Convert all integers in a SPDY control frame headers structure from 1093 * host byte order to network byte order. 1094 * 1095 * @param frame input and output structure (struct SPDY_Control_Frame *) 1096 */ 1097 #if HAVE_BIG_ENDIAN 1098 #define SPDYF_CONTROL_FRAME_HTON(frame) 1099 #else 1100 #define SPDYF_CONTROL_FRAME_HTON(frame) do { \ 1101 (*((uint16_t *) frame )) = (*((uint8_t *) (frame) +1 )) | ((*((uint8_t *) frame ))<<8);\ 1102 (frame)->type = htons((frame)->type); \ 1103 (frame)->length = HTON24((frame)->length); \ 1104 } while (0) 1105 #endif 1106 1107 1108 /** 1109 * Convert all integers in a SPDY control frame headers structure from 1110 * network byte order to host byte order. 1111 * 1112 * @param frame input and output structure (struct SPDY_Control_Frame *) 1113 */ 1114 #if HAVE_BIG_ENDIAN 1115 #define SPDYF_CONTROL_FRAME_NTOH(frame) 1116 #else 1117 #define SPDYF_CONTROL_FRAME_NTOH(frame) do { \ 1118 (*((uint16_t *) frame )) = (*((uint8_t *) (frame) +1 )) | ((*((uint8_t *) frame ))<<8);\ 1119 (frame)->type = ntohs((frame)->type); \ 1120 (frame)->length = NTOH24((frame)->length); \ 1121 } while (0) 1122 #endif 1123 1124 1125 /** 1126 * Convert all integers in a SPDY data frame headers structure from 1127 * host byte order to network byte order. 1128 * 1129 * @param frame input and output structure (struct SPDY_Data_Frame *) 1130 */ 1131 #if HAVE_BIG_ENDIAN 1132 #define SPDYF_DATA_FRAME_HTON(frame) 1133 #else 1134 #define SPDYF_DATA_FRAME_HTON(frame) do { \ 1135 *((uint32_t *) frame ) = htonl(*((uint32_t *) frame ));\ 1136 (frame)->length = HTON24((frame)->length); \ 1137 } while (0) 1138 #endif 1139 1140 1141 /** 1142 * Convert all integers in a SPDY data frame headers structure from 1143 * network byte order to host byte order. 1144 * 1145 * @param frame input and output structure (struct SPDY_Data_Frame *) 1146 */ 1147 #if HAVE_BIG_ENDIAN 1148 #define SPDYF_DATA_FRAME_NTOH(frame) 1149 #else 1150 #define SPDYF_DATA_FRAME_NTOH(frame) do { \ 1151 *((uint32_t *) frame ) = ntohl(*((uint32_t *) frame ));\ 1152 (frame)->length = NTOH24((frame)->length); \ 1153 } while (0) 1154 #endif 1155 1156 1157 /** 1158 * Creates one or more new SPDYF_Response_Queue object to be put on the 1159 * response queue. 1160 * 1161 * @param is_data whether new data frame or new control frame will be 1162 * crerated 1163 * @param data the row stream which will be used as the body of the frame 1164 * @param data_size length of data 1165 * @param response object, part of which is the frame 1166 * @param stream on which data is to be sent 1167 * @param closestream TRUE if the frame must close the stream (with flag) 1168 * @param frqcb callback to notify application layer when the frame 1169 * has been sent or discarded 1170 * @param frqcb_cls closure for frqcb 1171 * @param rrcb callback used by the application layer to notify the 1172 * application when the frame has been sent or discarded. 1173 * frqcb will call it 1174 * @param rrcb_cls closure for rrcb 1175 * @return double linked list of SPDYF_Response_Queue structures: one or 1176 * more frames are returned based on the size of the data 1177 */ 1178 struct SPDYF_Response_Queue * 1179 SPDYF_response_queue_create(bool is_data, 1180 void *data, 1181 size_t data_size, 1182 struct SPDY_Response *response, 1183 struct SPDYF_Stream *stream, 1184 bool closestream, 1185 SPDYF_ResponseQueueResultCallback frqcb, 1186 void *frqcb_cls, 1187 SPDY_ResponseResultCallback rrcb, 1188 void *rrcb_cls); 1189 1190 1191 /** 1192 * Destroys SPDYF_Response_Queue structure and whatever is in it. 1193 * 1194 * @param response_queue to destroy 1195 */ 1196 void 1197 SPDYF_response_queue_destroy(struct SPDYF_Response_Queue *response_queue); 1198 1199 1200 /** 1201 * Checks if the container is empty, i.e. created but no values were 1202 * added to it. 1203 * 1204 * @param container 1205 * @return SPDY_YES if empty 1206 * SPDY_NO if not 1207 */ 1208 int 1209 SPDYF_name_value_is_empty(struct SPDY_NameValue *container); 1210 1211 1212 /** 1213 * Transforms raw binary decomressed stream of headers 1214 * into SPDY_NameValue, containing all of the headers and values. 1215 * 1216 * @param stream that is to be transformed 1217 * @param size length of the stream 1218 * @param container will contain the newly created SPDY_NameValue 1219 * container. Should point to NULL. 1220 * @return SPDY_YES on success 1221 * SPDY_NO on memory error 1222 * SPDY_INPUT_ERROR if the provided stream is not valid 1223 */ 1224 int 1225 SPDYF_name_value_from_stream(void *stream, 1226 size_t size, 1227 struct SPDY_NameValue ** container); 1228 1229 1230 /** 1231 * Transforms array of objects of name/values tuples, containing HTTP 1232 * headers, into raw binary stream. The resulting stream is ready to 1233 * be compressed and sent. 1234 * 1235 * @param container one or more SPDY_NameValue objects. Each object 1236 * contains multiple number of name/value tuples. 1237 * @param num_containers length of the array 1238 * @param stream will contain the resulting stream. Should point to NULL. 1239 * @return length of stream or value less than 0 indicating error 1240 */ 1241 ssize_t 1242 SPDYF_name_value_to_stream(struct SPDY_NameValue * container[], 1243 int num_containers, 1244 void **stream); 1245 1246 #endif 1247