1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007-2015 Daniel Pittman and Christian Grothoff 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library 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 GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 /** 21 * @file microhttpd/internal.h 22 * @brief internal shared structures 23 * @author Daniel Pittman 24 * @author Christian Grothoff 25 */ 26 27 #ifndef INTERNAL_H 28 #define INTERNAL_H 29 30 #include "platform.h" 31 #include "microhttpd.h" 32 #include "platform_interface.h" 33 #if HTTPS_SUPPORT 34 #include <openssl/ssl.h> 35 #endif 36 #if EPOLL_SUPPORT 37 #include <sys/epoll.h> 38 #endif 39 #if HAVE_NETINET_TCP_H 40 /* for TCP_FASTOPEN */ 41 #include <netinet/tcp.h> 42 #endif 43 44 45 /** 46 * Should we perform additional sanity checks at runtime (on our internal 47 * invariants)? This may lead to aborts, but can be useful for debugging. 48 */ 49 #define EXTRA_CHECKS MHD_NO 50 51 #define MHD_MAX(a,b) ((a)<(b)) ? (b) : (a) 52 #define MHD_MIN(a,b) ((a)<(b)) ? (a) : (b) 53 54 55 /** 56 * Minimum size by which MHD tries to increment read/write buffers. 57 * We usually begin with half the available pool space for the 58 * IO-buffer, but if absolutely needed we additively grow by the 59 * number of bytes given here (up to -- theoretically -- the full pool 60 * space). 61 */ 62 #define MHD_BUF_INC_SIZE 1024 63 64 65 /** 66 * Handler for fatal errors. 67 */ 68 extern MHD_PanicCallback mhd_panic; 69 70 /** 71 * Closure argument for "mhd_panic". 72 */ 73 extern void *mhd_panic_cls; 74 75 /* If we have Clang or gcc >= 4.5, use __buildin_unreachable() */ 76 #if defined(__clang__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) 77 #define BUILTIN_NOT_REACHED __builtin_unreachable() 78 #else 79 #define BUILTIN_NOT_REACHED 80 #endif 81 82 83 #if HAVE_MESSAGES 84 /** 85 * Trigger 'panic' action based on fatal errors. 86 * 87 * @param msg error message (const char *) 88 */ 89 #define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, msg); BUILTIN_NOT_REACHED; } while (0) 90 #else 91 /** 92 * Trigger 'panic' action based on fatal errors. 93 * 94 * @param msg error message (const char *) 95 */ 96 #define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, NULL); BUILTIN_NOT_REACHED; } while (0) 97 #endif 98 99 100 /** 101 * State of the socket with respect to epoll (bitmask). 102 */ 103 enum MHD_EpollState 104 { 105 106 /** 107 * The socket is not involved with a defined state in epoll right 108 * now. 109 */ 110 MHD_EPOLL_STATE_UNREADY = 0, 111 112 /** 113 * epoll told us that data was ready for reading, and we did 114 * not consume all of it yet. 115 */ 116 MHD_EPOLL_STATE_READ_READY = 1, 117 118 /** 119 * epoll told us that space was available for writing, and we did 120 * not consume all of it yet. 121 */ 122 MHD_EPOLL_STATE_WRITE_READY = 2, 123 124 /** 125 * Is this connection currently in the 'eready' EDLL? 126 */ 127 MHD_EPOLL_STATE_IN_EREADY_EDLL = 4, 128 129 /** 130 * Is this connection currently in the 'epoll' set? 131 */ 132 MHD_EPOLL_STATE_IN_EPOLL_SET = 8, 133 134 /** 135 * Is this connection currently suspended? 136 */ 137 MHD_EPOLL_STATE_SUSPENDED = 16 138 }; 139 140 141 /** 142 * What is this connection waiting for? 143 */ 144 enum MHD_ConnectionEventLoopInfo 145 { 146 /** 147 * We are waiting to be able to read. 148 */ 149 MHD_EVENT_LOOP_INFO_READ = 0, 150 151 /** 152 * We are waiting to be able to write. 153 */ 154 MHD_EVENT_LOOP_INFO_WRITE = 1, 155 156 /** 157 * We are waiting for the application to provide data. 158 */ 159 MHD_EVENT_LOOP_INFO_BLOCK = 2, 160 161 /** 162 * We are finished and are awaiting cleanup. 163 */ 164 MHD_EVENT_LOOP_INFO_CLEANUP = 3 165 }; 166 167 168 /** 169 * Maximum length of a nonce in digest authentication. 32(MD5 Hex) + 170 * 8(Timestamp Hex) + 1(NULL); hence 41 should suffice, but Opera 171 * (already) takes more (see Mantis #1633), so we've increased the 172 * value to support something longer... 173 */ 174 #define MAX_NONCE_LENGTH 129 175 176 177 /** 178 * A structure representing the internal holder of the 179 * nonce-nc map. 180 */ 181 struct MHD_NonceNc 182 { 183 184 /** 185 * Nonce counter, a value that increases for each subsequent 186 * request for the same nonce. 187 */ 188 unsigned long int nc; 189 190 /** 191 * Nonce value: 192 */ 193 char nonce[MAX_NONCE_LENGTH]; 194 195 }; 196 197 #if HAVE_MESSAGES 198 /** 199 * fprintf-like helper function for logging debug 200 * messages. 201 */ 202 void 203 MHD_DLOG (const struct MHD_Daemon *daemon, 204 const char *format, ...); 205 #endif 206 207 208 /** 209 * Header or cookie in HTTP request or response. 210 */ 211 struct MHD_HTTP_Header 212 { 213 /** 214 * Headers are kept in a linked list. 215 */ 216 struct MHD_HTTP_Header *next; 217 218 /** 219 * The name of the header (key), without 220 * the colon. 221 */ 222 char *header; 223 224 /** 225 * The value of the header. 226 */ 227 char *value; 228 229 /** 230 * Type of the header (where in the HTTP 231 * protocol is this header from). 232 */ 233 enum MHD_ValueKind kind; 234 235 }; 236 237 238 /** 239 * Representation of a response. 240 */ 241 struct MHD_Response 242 { 243 244 /** 245 * Headers to send for the response. Initially 246 * the linked list is created in inverse order; 247 * the order should be inverted before sending! 248 */ 249 struct MHD_HTTP_Header *first_header; 250 251 /** 252 * Buffer pointing to data that we are supposed 253 * to send as a response. 254 */ 255 char *data; 256 257 /** 258 * Closure to give to the content reader @e crc 259 * and content reader free callback @e crfc. 260 */ 261 void *crc_cls; 262 263 /** 264 * How do we get more data? NULL if we are 265 * given all of the data up front. 266 */ 267 MHD_ContentReaderCallback crc; 268 269 /** 270 * NULL if data must not be freed, otherwise 271 * either user-specified callback or "&free". 272 */ 273 MHD_ContentReaderFreeCallback crfc; 274 275 /** 276 * Mutex to synchronize access to @e data, @e size and 277 * @e reference_count. 278 */ 279 MHD_mutex_ mutex; 280 281 /** 282 * Set to #MHD_SIZE_UNKNOWN if size is not known. 283 */ 284 uint64_t total_size; 285 286 /** 287 * At what offset in the stream is the 288 * beginning of @e data located? 289 */ 290 uint64_t data_start; 291 292 /** 293 * Offset to start reading from when using @e fd. 294 */ 295 off_t fd_off; 296 297 /** 298 * Number of bytes ready in @e data (buffer may be larger 299 * than what is filled with payload). 300 */ 301 size_t data_size; 302 303 /** 304 * Size of the data buffer @e data. 305 */ 306 size_t data_buffer_size; 307 308 /** 309 * Reference count for this response. Free 310 * once the counter hits zero. 311 */ 312 unsigned int reference_count; 313 314 /** 315 * File-descriptor if this response is FD-backed. 316 */ 317 int fd; 318 319 /** 320 * Flags set for the MHD response. 321 */ 322 enum MHD_ResponseFlags flags; 323 324 }; 325 326 327 /** 328 * States in a state machine for a connection. 329 * 330 * Transitions are any-state to CLOSED, any state to state+1, 331 * FOOTERS_SENT to INIT. CLOSED is the terminal state and 332 * INIT the initial state. 333 * 334 * Note that transitions for *reading* happen only after 335 * the input has been processed; transitions for 336 * *writing* happen after the respective data has been 337 * put into the write buffer (the write does not have 338 * to be completed yet). A transition to CLOSED or INIT 339 * requires the write to be complete. 340 */ 341 enum MHD_CONNECTION_STATE 342 { 343 /** 344 * Connection just started (no headers received). 345 * Waiting for the line with the request type, URL and version. 346 */ 347 MHD_CONNECTION_INIT = 0, 348 349 /** 350 * 1: We got the URL (and request type and version). Wait for a header line. 351 */ 352 MHD_CONNECTION_URL_RECEIVED = MHD_CONNECTION_INIT + 1, 353 354 /** 355 * 2: We got part of a multi-line request header. Wait for the rest. 356 */ 357 MHD_CONNECTION_HEADER_PART_RECEIVED = MHD_CONNECTION_URL_RECEIVED + 1, 358 359 /** 360 * 3: We got the request headers. Process them. 361 */ 362 MHD_CONNECTION_HEADERS_RECEIVED = MHD_CONNECTION_HEADER_PART_RECEIVED + 1, 363 364 /** 365 * 4: We have processed the request headers. Send 100 continue. 366 */ 367 MHD_CONNECTION_HEADERS_PROCESSED = MHD_CONNECTION_HEADERS_RECEIVED + 1, 368 369 /** 370 * 5: We have processed the headers and need to send 100 CONTINUE. 371 */ 372 MHD_CONNECTION_CONTINUE_SENDING = MHD_CONNECTION_HEADERS_PROCESSED + 1, 373 374 /** 375 * 6: We have sent 100 CONTINUE (or do not need to). Read the message body. 376 */ 377 MHD_CONNECTION_CONTINUE_SENT = MHD_CONNECTION_CONTINUE_SENDING + 1, 378 379 /** 380 * 7: We got the request body. Wait for a line of the footer. 381 */ 382 MHD_CONNECTION_BODY_RECEIVED = MHD_CONNECTION_CONTINUE_SENT + 1, 383 384 /** 385 * 8: We got part of a line of the footer. Wait for the 386 * rest. 387 */ 388 MHD_CONNECTION_FOOTER_PART_RECEIVED = MHD_CONNECTION_BODY_RECEIVED + 1, 389 390 /** 391 * 9: We received the entire footer. Wait for a response to be queued 392 * and prepare the response headers. 393 */ 394 MHD_CONNECTION_FOOTERS_RECEIVED = MHD_CONNECTION_FOOTER_PART_RECEIVED + 1, 395 396 /** 397 * 10: We have prepared the response headers in the writ buffer. 398 * Send the response headers. 399 */ 400 MHD_CONNECTION_HEADERS_SENDING = MHD_CONNECTION_FOOTERS_RECEIVED + 1, 401 402 /** 403 * 11: We have sent the response headers. Get ready to send the body. 404 */ 405 MHD_CONNECTION_HEADERS_SENT = MHD_CONNECTION_HEADERS_SENDING + 1, 406 407 /** 408 * 12: We are ready to send a part of a non-chunked body. Send it. 409 */ 410 MHD_CONNECTION_NORMAL_BODY_READY = MHD_CONNECTION_HEADERS_SENT + 1, 411 412 /** 413 * 13: We are waiting for the client to provide more 414 * data of a non-chunked body. 415 */ 416 MHD_CONNECTION_NORMAL_BODY_UNREADY = MHD_CONNECTION_NORMAL_BODY_READY + 1, 417 418 /** 419 * 14: We are ready to send a chunk. 420 */ 421 MHD_CONNECTION_CHUNKED_BODY_READY = MHD_CONNECTION_NORMAL_BODY_UNREADY + 1, 422 423 /** 424 * 15: We are waiting for the client to provide a chunk of the body. 425 */ 426 MHD_CONNECTION_CHUNKED_BODY_UNREADY = MHD_CONNECTION_CHUNKED_BODY_READY + 1, 427 428 /** 429 * 16: We have sent the response body. Prepare the footers. 430 */ 431 MHD_CONNECTION_BODY_SENT = MHD_CONNECTION_CHUNKED_BODY_UNREADY + 1, 432 433 /** 434 * 17: We have prepared the response footer. Send it. 435 */ 436 MHD_CONNECTION_FOOTERS_SENDING = MHD_CONNECTION_BODY_SENT + 1, 437 438 /** 439 * 18: We have sent the response footer. Shutdown or restart. 440 */ 441 MHD_CONNECTION_FOOTERS_SENT = MHD_CONNECTION_FOOTERS_SENDING + 1, 442 443 /** 444 * 19: This connection is to be closed. 445 */ 446 MHD_CONNECTION_CLOSED = MHD_CONNECTION_FOOTERS_SENT + 1, 447 448 /** 449 * 20: This connection is finished (only to be freed) 450 */ 451 MHD_CONNECTION_IN_CLEANUP = MHD_CONNECTION_CLOSED + 1, 452 453 /* 454 * SSL/TLS connection states 455 */ 456 457 /** 458 * The initial connection state for all secure connectoins 459 * Handshake messages will be processed in this state & while 460 * in the 'MHD_TLS_HELLO_REQUEST' state 461 */ 462 MHD_TLS_CONNECTION_INIT = MHD_CONNECTION_IN_CLEANUP + 1 463 464 }; 465 466 /** 467 * Should all state transitions be printed to stderr? 468 */ 469 #define DEBUG_STATES MHD_NO 470 471 472 #if HAVE_MESSAGES 473 #if DEBUG_STATES 474 const char * 475 MHD_state_to_string (enum MHD_CONNECTION_STATE state); 476 #endif 477 #endif 478 479 /** 480 * Function to receive plaintext data. 481 * 482 * @param conn the connection struct 483 * @param write_to where to write received data 484 * @param max_bytes maximum number of bytes to receive 485 * @return number of bytes written to write_to 486 */ 487 typedef ssize_t 488 (*ReceiveCallback) (struct MHD_Connection *conn, 489 void *write_to, 490 size_t max_bytes); 491 492 493 /** 494 * Function to transmit plaintext data. 495 * 496 * @param conn the connection struct 497 * @param read_from where to read data to transmit 498 * @param max_bytes maximum number of bytes to transmit 499 * @return number of bytes transmitted 500 */ 501 typedef ssize_t 502 (*TransmitCallback) (struct MHD_Connection *conn, 503 const void *write_to, 504 size_t max_bytes); 505 506 507 /** 508 * State kept for each HTTP request. 509 */ 510 struct MHD_Connection 511 { 512 513 #if EPOLL_SUPPORT 514 /** 515 * Next pointer for the EDLL listing connections that are epoll-ready. 516 */ 517 struct MHD_Connection *nextE; 518 519 /** 520 * Previous pointer for the EDLL listing connections that are epoll-ready. 521 */ 522 struct MHD_Connection *prevE; 523 #endif 524 525 /** 526 * Next pointer for the DLL describing our IO state. 527 */ 528 struct MHD_Connection *next; 529 530 /** 531 * Previous pointer for the DLL describing our IO state. 532 */ 533 struct MHD_Connection *prev; 534 535 /** 536 * Next pointer for the XDLL organizing connections by timeout. 537 * This DLL can be either the 538 * 'manual_timeout_head/manual_timeout_tail' or the 539 * 'normal_timeout_head/normal_timeout_tail', depending on whether a 540 * custom timeout is set for the connection. 541 */ 542 struct MHD_Connection *nextX; 543 544 /** 545 * Previous pointer for the XDLL organizing connections by timeout. 546 */ 547 struct MHD_Connection *prevX; 548 549 /** 550 * Reference to the MHD_Daemon struct. 551 */ 552 struct MHD_Daemon *daemon; 553 554 /** 555 * Linked list of parsed headers. 556 */ 557 struct MHD_HTTP_Header *headers_received; 558 559 /** 560 * Tail of linked list of parsed headers. 561 */ 562 struct MHD_HTTP_Header *headers_received_tail; 563 564 /** 565 * Response to transmit (initially NULL). 566 */ 567 struct MHD_Response *response; 568 569 /** 570 * The memory pool is created whenever we first read 571 * from the TCP stream and destroyed at the end of 572 * each request (and re-created for the next request). 573 * In the meantime, this pointer is NULL. The 574 * pool is used for all connection-related data 575 * except for the response (which maybe shared between 576 * connections) and the IP address (which persists 577 * across individual requests). 578 */ 579 struct MemoryPool *pool; 580 581 /** 582 * We allow the main application to associate some pointer with the 583 * HTTP request, which is passed to each #MHD_AccessHandlerCallback 584 * and some other API calls. Here is where we store it. (MHD does 585 * not know or care what it is). 586 */ 587 void *client_context; 588 589 /** 590 * We allow the main application to associate some pointer with the 591 * TCP connection (which may span multiple HTTP requests). Here is 592 * where we store it. (MHD does not know or care what it is). 593 * The location is given to the #MHD_NotifyConnectionCallback and 594 * also accessible via #MHD_CONNECTION_INFO_SOCKET_CONTEXT. 595 */ 596 void *socket_context; 597 598 /** 599 * Request method. Should be GET/POST/etc. Allocated 600 * in pool. 601 */ 602 char *method; 603 604 /** 605 * Requested URL (everything after "GET" only). Allocated 606 * in pool. 607 */ 608 char *url; 609 610 /** 611 * HTTP version string (i.e. http/1.1). Allocated 612 * in pool. 613 */ 614 char *version; 615 616 /** 617 * Buffer for reading requests. Allocated 618 * in pool. Actually one byte larger than 619 * @e read_buffer_size (if non-NULL) to allow for 620 * 0-termination. 621 */ 622 char *read_buffer; 623 624 /** 625 * Buffer for writing response (headers only). Allocated 626 * in pool. 627 */ 628 char *write_buffer; 629 630 /** 631 * Last incomplete header line during parsing of headers. 632 * Allocated in pool. Only valid if state is 633 * either #MHD_CONNECTION_HEADER_PART_RECEIVED or 634 * #MHD_CONNECTION_FOOTER_PART_RECEIVED. 635 */ 636 char *last; 637 638 /** 639 * Position after the colon on the last incomplete header 640 * line during parsing of headers. 641 * Allocated in pool. Only valid if state is 642 * either #MHD_CONNECTION_HEADER_PART_RECEIVED or 643 * #MHD_CONNECTION_FOOTER_PART_RECEIVED. 644 */ 645 char *colon; 646 647 /** 648 * Foreign address (of length @e addr_len). MALLOCED (not 649 * in pool!). 650 */ 651 struct sockaddr *addr; 652 653 /** 654 * Thread handle for this connection (if we are using 655 * one thread per connection). 656 */ 657 MHD_thread_handle_ pid; 658 659 /** 660 * Size of read_buffer (in bytes). This value indicates 661 * how many bytes we're willing to read into the buffer; 662 * the real buffer is one byte longer to allow for 663 * adding zero-termination (when needed). 664 */ 665 size_t read_buffer_size; 666 667 /** 668 * Position where we currently append data in 669 * read_buffer (last valid position). 670 */ 671 size_t read_buffer_offset; 672 673 /** 674 * Size of write_buffer (in bytes). 675 */ 676 size_t write_buffer_size; 677 678 /** 679 * Offset where we are with sending from write_buffer. 680 */ 681 size_t write_buffer_send_offset; 682 683 /** 684 * Last valid location in write_buffer (where do we 685 * append and up to where is it safe to send?) 686 */ 687 size_t write_buffer_append_offset; 688 689 /** 690 * How many more bytes of the body do we expect 691 * to read? #MHD_SIZE_UNKNOWN for unknown. 692 */ 693 uint64_t remaining_upload_size; 694 695 /** 696 * Current write position in the actual response 697 * (excluding headers, content only; should be 0 698 * while sending headers). 699 */ 700 uint64_t response_write_position; 701 702 /** 703 * Position in the 100 CONTINUE message that 704 * we need to send when receiving http 1.1 requests. 705 */ 706 size_t continue_message_write_offset; 707 708 /** 709 * Length of the foreign address. 710 */ 711 socklen_t addr_len; 712 713 /** 714 * Last time this connection had any activity 715 * (reading or writing). 716 */ 717 time_t last_activity; 718 719 /** 720 * After how many seconds of inactivity should 721 * this connection time out? Zero for no timeout. 722 */ 723 unsigned int connection_timeout; 724 725 /** 726 * Did we ever call the "default_handler" on this connection? 727 * (this flag will determine if we call the 'notify_completed' 728 * handler when the connection closes down). 729 */ 730 int client_aware; 731 732 /** 733 * Socket for this connection. Set to #MHD_INVALID_SOCKET if 734 * this connection has died (daemon should clean 735 * up in that case). 736 */ 737 MHD_socket socket_fd; 738 739 /** 740 * Has this socket been closed for reading (i.e. other side closed 741 * the connection)? If so, we must completely close the connection 742 * once we are done sending our response (and stop trying to read 743 * from this socket). 744 */ 745 int read_closed; 746 747 /** 748 * Set to #MHD_YES if the thread has been joined. 749 */ 750 int thread_joined; 751 752 /** 753 * Are we currently inside the "idle" handler (to avoid recursively invoking it). 754 */ 755 int in_idle; 756 757 #if EPOLL_SUPPORT 758 /** 759 * What is the state of this socket in relation to epoll? 760 */ 761 enum MHD_EpollState epoll_state; 762 #endif 763 764 /** 765 * State in the FSM for this connection. 766 */ 767 enum MHD_CONNECTION_STATE state; 768 769 /** 770 * What is this connection waiting for? 771 */ 772 enum MHD_ConnectionEventLoopInfo event_loop_info; 773 774 /** 775 * HTTP response code. Only valid if response object 776 * is already set. 777 */ 778 unsigned int responseCode; 779 780 /** 781 * Set to MHD_YES if the response's content reader 782 * callback failed to provide data the last time 783 * we tried to read from it. In that case, the 784 * write socket should be marked as unready until 785 * the CRC call succeeds. 786 */ 787 int response_unready; 788 789 /** 790 * Are we receiving with chunked encoding? This will be set to 791 * MHD_YES after we parse the headers and are processing the body 792 * with chunks. After we are done with the body and we are 793 * processing the footers; once the footers are also done, this will 794 * be set to MHD_NO again (before the final call to the handler). 795 */ 796 int have_chunked_upload; 797 798 /** 799 * If we are receiving with chunked encoding, where are we right 800 * now? Set to 0 if we are waiting to receive the chunk size; 801 * otherwise, this is the size of the current chunk. A value of 802 * zero is also used when we're at the end of the chunks. 803 */ 804 size_t current_chunk_size; 805 806 /** 807 * If we are receiving with chunked encoding, where are we currently 808 * with respect to the current chunk (at what offset / position)? 809 */ 810 size_t current_chunk_offset; 811 812 /** 813 * Handler used for processing read connection operations 814 */ 815 int (*read_handler) (struct MHD_Connection *connection); 816 817 /** 818 * Handler used for processing write connection operations 819 */ 820 int (*write_handler) (struct MHD_Connection *connection); 821 822 /** 823 * Handler used for processing idle connection operations 824 */ 825 int (*idle_handler) (struct MHD_Connection *connection); 826 827 /** 828 * Function used for reading HTTP request stream. 829 */ 830 ReceiveCallback recv_cls; 831 832 /** 833 * Function used for writing HTTP response stream. 834 */ 835 TransmitCallback send_cls; 836 837 #if HTTPS_SUPPORT 838 /** 839 * State required for HTTPS/SSL/TLS support. 840 */ 841 SSL* tls_session; 842 843 /** 844 * Memory location to return for protocol session info. 845 */ 846 const char* protocol; 847 848 /** 849 * Memory location to return for protocol session info. 850 */ 851 const char* cipher; 852 853 /** 854 * Could it be that we are ready to read due to TLS buffers 855 * even though the socket is not? 856 */ 857 int tls_read_ready; 858 #endif 859 860 /** 861 * Is the connection suspended? 862 */ 863 int suspended; 864 865 /** 866 * Is the connection wanting to resume? 867 */ 868 int resuming; 869 }; 870 871 /** 872 * Signature of function called to log URI accesses. 873 * 874 * @param cls closure 875 * @param uri uri being accessed 876 * @param con connection handle 877 * @return new closure 878 */ 879 typedef void * 880 (*LogCallback)(void * cls, 881 const char * uri, 882 struct MHD_Connection *con); 883 884 /** 885 * Signature of function called to unescape URIs. See also 886 * #MHD_http_unescape(). 887 * 888 * @param cls closure 889 * @param conn connection handle 890 * @param uri 0-terminated string to unescape (should be updated) 891 * @return length of the resulting string 892 */ 893 typedef size_t 894 (*UnescapeCallback)(void *cls, 895 struct MHD_Connection *conn, 896 char *uri); 897 898 899 /** 900 * State kept for each MHD daemon. All connections are kept in two 901 * doubly-linked lists. The first one reflects the state of the 902 * connection in terms of what operations we are waiting for (read, 903 * write, locally blocked, cleanup) whereas the second is about its 904 * timeout state (default or custom). 905 */ 906 struct MHD_Daemon 907 { 908 909 /** 910 * Callback function for all requests. 911 */ 912 MHD_AccessHandlerCallback default_handler; 913 914 /** 915 * Closure argument to default_handler. 916 */ 917 void *default_handler_cls; 918 919 /** 920 * Head of doubly-linked list of our current, active connections. 921 */ 922 struct MHD_Connection *connections_head; 923 924 /** 925 * Tail of doubly-linked list of our current, active connections. 926 */ 927 struct MHD_Connection *connections_tail; 928 929 /** 930 * Head of doubly-linked list of our current but suspended connections. 931 */ 932 struct MHD_Connection *suspended_connections_head; 933 934 /** 935 * Tail of doubly-linked list of our current but suspended connections. 936 */ 937 struct MHD_Connection *suspended_connections_tail; 938 939 /** 940 * Head of doubly-linked list of connections to clean up. 941 */ 942 struct MHD_Connection *cleanup_head; 943 944 /** 945 * Tail of doubly-linked list of connections to clean up. 946 */ 947 struct MHD_Connection *cleanup_tail; 948 949 #if EPOLL_SUPPORT 950 /** 951 * Head of EDLL of connections ready for processing (in epoll mode). 952 */ 953 struct MHD_Connection *eready_head; 954 955 /** 956 * Tail of EDLL of connections ready for processing (in epoll mode) 957 */ 958 struct MHD_Connection *eready_tail; 959 #endif 960 961 /** 962 * Head of the XDLL of ALL connections with a default ('normal') 963 * timeout, sorted by timeout (earliest at the tail, most recently 964 * used connection at the head). MHD can just look at the tail of 965 * this list to determine the timeout for all of its elements; 966 * whenever there is an event of a connection, the connection is 967 * moved back to the tail of the list. 968 * 969 * All connections by default start in this list; if a custom 970 * timeout that does not match 'connection_timeout' is set, they 971 * are moved to the 'manual_timeout_head'-XDLL. 972 */ 973 struct MHD_Connection *normal_timeout_head; 974 975 /** 976 * Tail of the XDLL of ALL connections with a default timeout, 977 * sorted by timeout (earliest timeout at the tail). 978 */ 979 struct MHD_Connection *normal_timeout_tail; 980 981 /** 982 * Head of the XDLL of ALL connections with a non-default/custom 983 * timeout, unsorted. MHD will do a O(n) scan over this list to 984 * determine the current timeout. 985 */ 986 struct MHD_Connection *manual_timeout_head; 987 988 /** 989 * Tail of the XDLL of ALL connections with a non-default/custom 990 * timeout, unsorted. 991 */ 992 struct MHD_Connection *manual_timeout_tail; 993 994 /** 995 * Function to call to check if we should accept or reject an 996 * incoming request. May be NULL. 997 */ 998 MHD_AcceptPolicyCallback apc; 999 1000 /** 1001 * Closure argument to apc. 1002 */ 1003 void *apc_cls; 1004 1005 /** 1006 * Function to call when we are done processing 1007 * a particular request. May be NULL. 1008 */ 1009 MHD_RequestCompletedCallback notify_completed; 1010 1011 /** 1012 * Closure argument to notify_completed. 1013 */ 1014 void *notify_completed_cls; 1015 1016 /** 1017 * Function to call when we are starting/stopping 1018 * a connection. May be NULL. 1019 */ 1020 MHD_NotifyConnectionCallback notify_connection; 1021 1022 /** 1023 * Closure argument to notify_connection. 1024 */ 1025 void *notify_connection_cls; 1026 1027 /** 1028 * Function to call with the full URI at the 1029 * beginning of request processing. May be NULL. 1030 * <p> 1031 * Returns the initial pointer to internal state 1032 * kept by the client for the request. 1033 */ 1034 LogCallback uri_log_callback; 1035 1036 /** 1037 * Closure argument to @e uri_log_callback. 1038 */ 1039 void *uri_log_callback_cls; 1040 1041 /** 1042 * Function to call when we unescape escape sequences. 1043 */ 1044 UnescapeCallback unescape_callback; 1045 1046 /** 1047 * Closure for @e unescape_callback. 1048 */ 1049 void *unescape_callback_cls; 1050 1051 #if HAVE_MESSAGES 1052 /** 1053 * Function for logging error messages (if we 1054 * support error reporting). 1055 */ 1056 void (*custom_error_log) (void *cls, const char *fmt, va_list va); 1057 1058 /** 1059 * Closure argument to custom_error_log. 1060 */ 1061 void *custom_error_log_cls; 1062 #endif 1063 1064 /** 1065 * Pointer to master daemon (NULL if this is the master) 1066 */ 1067 struct MHD_Daemon *master; 1068 1069 /** 1070 * Worker daemons (one per thread) 1071 */ 1072 struct MHD_Daemon *worker_pool; 1073 1074 /** 1075 * Table storing number of connections per IP 1076 */ 1077 void *per_ip_connection_count; 1078 1079 /** 1080 * Size of the per-connection memory pools. 1081 */ 1082 size_t pool_size; 1083 1084 /** 1085 * Increment for growth of the per-connection memory pools. 1086 */ 1087 size_t pool_increment; 1088 1089 /** 1090 * Size of threads created by MHD. 1091 */ 1092 size_t thread_stack_size; 1093 1094 /** 1095 * Number of worker daemons 1096 */ 1097 unsigned int worker_pool_size; 1098 1099 /** 1100 * The select thread handle (if we have internal select) 1101 */ 1102 MHD_thread_handle_ pid; 1103 1104 /** 1105 * Mutex for per-IP connection counts. 1106 */ 1107 MHD_mutex_ per_ip_connection_mutex; 1108 1109 /** 1110 * Mutex for (modifying) access to the "cleanup" connection DLL. 1111 */ 1112 MHD_mutex_ cleanup_connection_mutex; 1113 1114 /** 1115 * Listen socket. 1116 */ 1117 MHD_socket socket_fd; 1118 1119 /** 1120 * Whether to allow/disallow/ignore reuse of listening address. 1121 * The semantics is the following: 1122 * 0: ignore (user did not ask for neither allow/disallow, use SO_REUSEADDR) 1123 * >0: allow (use SO_REUSEPORT on most platforms, SO_REUSEADDR on Windows) 1124 * <0: disallow (mostly no action, SO_EXCLUSIVEADDRUSE on Windows) 1125 */ 1126 int listening_address_reuse; 1127 1128 #if EPOLL_SUPPORT 1129 /** 1130 * File descriptor associated with our epoll loop. 1131 */ 1132 int epoll_fd; 1133 1134 /** 1135 * MHD_YES if the listen socket is in the 'epoll' set, 1136 * MHD_NO if not. 1137 */ 1138 int listen_socket_in_epoll; 1139 #endif 1140 1141 /** 1142 * Pipe we use to signal shutdown, unless 1143 * 'HAVE_LISTEN_SHUTDOWN' is defined AND we have a listen 1144 * socket (which we can then 'shutdown' to stop listening). 1145 * MHD can be build with usage of socketpair instead of 1146 * pipe (forced on W32). 1147 */ 1148 MHD_pipe wpipe[2]; 1149 1150 /** 1151 * Are we shutting down? 1152 */ 1153 int shutdown; 1154 1155 /* 1156 * Do we need to process resuming connections? 1157 */ 1158 int resuming; 1159 1160 /** 1161 * Number of active parallel connections. 1162 */ 1163 unsigned int connections; 1164 1165 /** 1166 * Limit on the number of parallel connections. 1167 */ 1168 unsigned int connection_limit; 1169 1170 /** 1171 * After how many seconds of inactivity should 1172 * connections time out? Zero for no timeout. 1173 */ 1174 unsigned int connection_timeout; 1175 1176 /** 1177 * Maximum number of connections per IP, or 0 for 1178 * unlimited. 1179 */ 1180 unsigned int per_ip_connection_limit; 1181 1182 /** 1183 * Daemon's flags (bitfield). 1184 */ 1185 enum MHD_FLAG options; 1186 1187 /** 1188 * Listen port. 1189 */ 1190 uint16_t port; 1191 1192 #if HTTPS_SUPPORT 1193 SSL_CTX* tls_context; 1194 /** 1195 * Pointer to our SSL/TLS key (in PEM) in memory. 1196 */ 1197 const char *https_mem_key; 1198 1199 /** 1200 * Pointer to our SSL/TLS certificate (in PEM) in memory. 1201 */ 1202 const char *https_mem_cert; 1203 1204 /** 1205 * Pointer to 0-terminated HTTPS passphrase in memory. 1206 */ 1207 const char *https_key_password; 1208 1209 /** 1210 * Pointer to our SSL/TLS certificate authority (in PEM) in memory. 1211 */ 1212 const char *https_mem_trust; 1213 1214 /** 1215 * Our Diffie-Hellman parameters (in PEM) in memory. 1216 */ 1217 const char *https_mem_dhparams; 1218 1219 /** 1220 * Pointer to SSL/TLS cipher string in memory. 1221 */ 1222 const char *https_mem_cipher; 1223 1224 /** 1225 * For how many connections do we have 'tls_read_ready' set to MHD_YES? 1226 * Used to avoid O(n) traversal over all connections when determining 1227 * event-loop timeout (as it needs to be zero if there is any connection 1228 * which might have ready data within TLS). 1229 */ 1230 unsigned int num_tls_read_ready; 1231 1232 #endif 1233 1234 #ifdef DAUTH_SUPPORT 1235 1236 /** 1237 * Character array of random values. 1238 */ 1239 const char *digest_auth_random; 1240 1241 /** 1242 * An array that contains the map nonce-nc. 1243 */ 1244 struct MHD_NonceNc *nnc; 1245 1246 /** 1247 * A rw-lock for synchronizing access to `nnc'. 1248 */ 1249 MHD_mutex_ nnc_lock; 1250 1251 /** 1252 * Size of `digest_auth_random. 1253 */ 1254 size_t digest_auth_rand_size; 1255 1256 /** 1257 * Size of the nonce-nc array. 1258 */ 1259 unsigned int nonce_nc_size; 1260 1261 #endif 1262 1263 #ifdef TCP_FASTOPEN 1264 /** 1265 * The queue size for incoming SYN + DATA packets. 1266 */ 1267 unsigned int fastopen_queue_size; 1268 #endif 1269 }; 1270 1271 1272 #if EXTRA_CHECKS 1273 #define EXTRA_CHECK(a) do { if (!(a)) abort(); } while (0) 1274 #else 1275 #define EXTRA_CHECK(a) 1276 #endif 1277 1278 1279 /** 1280 * Insert an element at the head of a DLL. Assumes that head, tail and 1281 * element are structs with prev and next fields. 1282 * 1283 * @param head pointer to the head of the DLL 1284 * @param tail pointer to the tail of the DLL 1285 * @param element element to insert 1286 */ 1287 #define DLL_insert(head,tail,element) do { \ 1288 EXTRA_CHECK (NULL == (element)->next); \ 1289 EXTRA_CHECK (NULL == (element)->prev); \ 1290 (element)->next = (head); \ 1291 (element)->prev = NULL; \ 1292 if ((tail) == NULL) \ 1293 (tail) = element; \ 1294 else \ 1295 (head)->prev = element; \ 1296 (head) = (element); } while (0) 1297 1298 1299 /** 1300 * Remove an element from a DLL. Assumes 1301 * that head, tail and element are structs 1302 * with prev and next fields. 1303 * 1304 * @param head pointer to the head of the DLL 1305 * @param tail pointer to the tail of the DLL 1306 * @param element element to remove 1307 */ 1308 #define DLL_remove(head,tail,element) do { \ 1309 EXTRA_CHECK ( (NULL != (element)->next) || ((element) == (tail))); \ 1310 EXTRA_CHECK ( (NULL != (element)->prev) || ((element) == (head))); \ 1311 if ((element)->prev == NULL) \ 1312 (head) = (element)->next; \ 1313 else \ 1314 (element)->prev->next = (element)->next; \ 1315 if ((element)->next == NULL) \ 1316 (tail) = (element)->prev; \ 1317 else \ 1318 (element)->next->prev = (element)->prev; \ 1319 (element)->next = NULL; \ 1320 (element)->prev = NULL; } while (0) 1321 1322 1323 1324 /** 1325 * Insert an element at the head of a XDLL. Assumes that head, tail and 1326 * element are structs with prevX and nextX fields. 1327 * 1328 * @param head pointer to the head of the XDLL 1329 * @param tail pointer to the tail of the XDLL 1330 * @param element element to insert 1331 */ 1332 #define XDLL_insert(head,tail,element) do { \ 1333 EXTRA_CHECK (NULL == (element)->nextX); \ 1334 EXTRA_CHECK (NULL == (element)->prevX); \ 1335 (element)->nextX = (head); \ 1336 (element)->prevX = NULL; \ 1337 if (NULL == (tail)) \ 1338 (tail) = element; \ 1339 else \ 1340 (head)->prevX = element; \ 1341 (head) = (element); } while (0) 1342 1343 1344 /** 1345 * Remove an element from a XDLL. Assumes 1346 * that head, tail and element are structs 1347 * with prevX and nextX fields. 1348 * 1349 * @param head pointer to the head of the XDLL 1350 * @param tail pointer to the tail of the XDLL 1351 * @param element element to remove 1352 */ 1353 #define XDLL_remove(head,tail,element) do { \ 1354 EXTRA_CHECK ( (NULL != (element)->nextX) || ((element) == (tail))); \ 1355 EXTRA_CHECK ( (NULL != (element)->prevX) || ((element) == (head))); \ 1356 if (NULL == (element)->prevX) \ 1357 (head) = (element)->nextX; \ 1358 else \ 1359 (element)->prevX->nextX = (element)->nextX; \ 1360 if (NULL == (element)->nextX) \ 1361 (tail) = (element)->prevX; \ 1362 else \ 1363 (element)->nextX->prevX = (element)->prevX; \ 1364 (element)->nextX = NULL; \ 1365 (element)->prevX = NULL; } while (0) 1366 1367 1368 /** 1369 * Insert an element at the head of a EDLL. Assumes that head, tail and 1370 * element are structs with prevE and nextE fields. 1371 * 1372 * @param head pointer to the head of the EDLL 1373 * @param tail pointer to the tail of the EDLL 1374 * @param element element to insert 1375 */ 1376 #define EDLL_insert(head,tail,element) do { \ 1377 (element)->nextE = (head); \ 1378 (element)->prevE = NULL; \ 1379 if ((tail) == NULL) \ 1380 (tail) = element; \ 1381 else \ 1382 (head)->prevE = element; \ 1383 (head) = (element); } while (0) 1384 1385 1386 /** 1387 * Remove an element from a EDLL. Assumes 1388 * that head, tail and element are structs 1389 * with prevE and nextE fields. 1390 * 1391 * @param head pointer to the head of the EDLL 1392 * @param tail pointer to the tail of the EDLL 1393 * @param element element to remove 1394 */ 1395 #define EDLL_remove(head,tail,element) do { \ 1396 if ((element)->prevE == NULL) \ 1397 (head) = (element)->nextE; \ 1398 else \ 1399 (element)->prevE->nextE = (element)->nextE; \ 1400 if ((element)->nextE == NULL) \ 1401 (tail) = (element)->prevE; \ 1402 else \ 1403 (element)->nextE->prevE = (element)->prevE; \ 1404 (element)->nextE = NULL; \ 1405 (element)->prevE = NULL; } while (0) 1406 1407 1408 /** 1409 * Equivalent to `time(NULL)` but tries to use some sort of monotonic 1410 * clock that isn't affected by someone setting the system real time 1411 * clock. 1412 * 1413 * @return 'current' time 1414 */ 1415 time_t 1416 MHD_monotonic_time(void); 1417 1418 1419 /** 1420 * Convert all occurences of '+' to ' '. 1421 * 1422 * @param arg string that is modified (in place), must be 0-terminated 1423 */ 1424 void 1425 MHD_unescape_plus (char *arg); 1426 1427 1428 #endif 1429