GNU libmicrohttpd  0.9.29
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
internal.h
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  (C) 2007-2013 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 
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 <gnutls/gnutls.h>
35 #if GNUTLS_VERSION_MAJOR >= 3
36 #include <gnutls/abstract.h>
37 #endif
38 #endif
39 #if EPOLL_SUPPORT
40 #include <sys/epoll.h>
41 #endif
42 #if HAVE_NETINET_TCP_H
43 /* for TCP_FASTOPEN */
44 #include <netinet/tcp.h>
45 #endif
46 
47 
52 #define EXTRA_CHECKS MHD_NO
53 
54 #define MHD_MAX(a,b) ((a)<(b)) ? (b) : (a)
55 #define MHD_MIN(a,b) ((a)<(b)) ? (a) : (b)
56 
57 
65 #define MHD_BUF_INC_SIZE 1024
66 
67 
72 
76 extern void *mhd_panic_cls;
77 
78 /* If we have Clang or gcc >= 4.5, use __buildin_unreachable() */
79 #if defined(__clang__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
80 #define BUILTIN_NOT_REACHED __builtin_unreachable()
81 #else
82 #define BUILTIN_NOT_REACHED
83 #endif
84 
85 
86 #if HAVE_MESSAGES
87 
92 #define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, msg); BUILTIN_NOT_REACHED; } while (0)
93 #else
94 
99 #define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, NULL); BUILTIN_NOT_REACHED; } while (0)
100 #endif
101 
102 
107  {
108 
114 
120 
126 
131 
136 
141  };
142 
143 
148  {
153 
158 
163 
168  };
169 
170 
177 #define MAX_NONCE_LENGTH 129
178 
179 
185 {
186 
191  unsigned long int nc;
192 
197 
198 };
199 
200 #if HAVE_MESSAGES
201 
205 void
206 MHD_DLOG (const struct MHD_Daemon *daemon,
207  const char *format, ...);
208 #endif
209 
210 
215 {
220 
225  char *header;
226 
230  char *value;
231 
237 
238 };
239 
240 
245 {
246 
253 
258  char *data;
259 
264  void *crc_cls;
265 
271 
277 
282  MHD_mutex_ mutex;
283 
287  uint64_t total_size;
288 
293  uint64_t data_start;
294 
298  off_t fd_off;
299 
304  size_t data_size;
305 
310 
315  unsigned int reference_count;
316 
320  int fd;
321 
326 
327 };
328 
329 
345 {
351 
356 
361 
366 
371 
376 
381 
386 
392 
398 
404 
409 
414 
420 
425 
430 
435 
440 
445 
450 
455 
456  /*
457  * SSL/TLS connection states
458  */
459 
466 
467 };
468 
472 #define DEBUG_STATES MHD_NO
473 
474 
475 #if HAVE_MESSAGES
476 #if DEBUG_STATES
477 const char *
478 MHD_state_to_string (enum MHD_CONNECTION_STATE state);
479 #endif
480 #endif
481 
490 typedef ssize_t (*ReceiveCallback) (struct MHD_Connection * conn,
491  void *write_to, size_t max_bytes);
492 
493 
502 typedef ssize_t (*TransmitCallback) (struct MHD_Connection * conn,
503  const void *write_to, size_t max_bytes);
504 
505 
510 {
511 
512 #if EPOLL_SUPPORT
513 
516  struct MHD_Connection *nextE;
517 
521  struct MHD_Connection *prevE;
522 #endif
523 
528 
533 
542 
547 
552 
557 
562 
567 
578  struct MemoryPool *pool;
579 
587 
592  char *method;
593 
598  char *url;
599 
604  char *version;
605 
612  char *read_buffer;
613 
619 
625  char *last;
626 
633  char *colon;
634 
639  struct sockaddr *addr;
640 
645  MHD_thread_handle_ pid;
646 
654 
660 
665 
670 
676 
682 
689 
695 
699  socklen_t addr_len;
700 
706 
711  unsigned int connection_timeout;
712 
719 
726 
734 
739 
743  int in_idle;
744 
745 #if EPOLL_SUPPORT
746 
749  enum MHD_EpollState epoll_state;
750 #endif
751 
756 
761 
766  unsigned int responseCode;
767 
776 
785 
793 
799 
803  int (*read_handler) (struct MHD_Connection * connection);
804 
808  int (*write_handler) (struct MHD_Connection * connection);
809 
813  int (*idle_handler) (struct MHD_Connection * connection);
814 
819 
824 
825 #if HTTPS_SUPPORT
826 
829  gnutls_session_t tls_session;
830 
834  int protocol;
835 
839  int cipher;
840 
845  int tls_read_ready;
846 #endif
847 
852 
856  int resuming;
857 };
858 
867 typedef void * (*LogCallback)(void * cls,
868  const char * uri,
869  struct MHD_Connection *con);
870 
880 typedef size_t (*UnescapeCallback)(void *cls,
881  struct MHD_Connection *conn,
882  char *uri);
883 
884 
893 {
894 
899 
904 
909 
914 
919 
924 
929 
934 
935 #if EPOLL_SUPPORT
936 
939  struct MHD_Connection *eready_head;
940 
944  struct MHD_Connection *eready_tail;
945 #endif
946 
960 
966 
973 
979 
985 
989  void *apc_cls;
990 
996 
1001 
1010 
1015 
1020 
1025 
1026 #if HAVE_MESSAGES
1027 
1031  void (*custom_error_log) (void *cls, const char *fmt, va_list va);
1032 
1036  void *custom_error_log_cls;
1037 #endif
1038 
1043 
1048 
1053 
1057  size_t pool_size;
1058 
1063 
1068 
1072  unsigned int worker_pool_size;
1073 
1077  MHD_thread_handle_ pid;
1078 
1083 
1088 
1093 
1102 
1103 #if EPOLL_SUPPORT
1104 
1107  int epoll_fd;
1108 
1113  int listen_socket_in_epoll;
1114 #endif
1115 
1124 
1129 
1130  /*
1131  * Do we need to process resuming connections?
1132  */
1134 
1138  unsigned int connections;
1139 
1143  unsigned int connection_limit;
1144 
1149  unsigned int connection_timeout;
1150 
1156 
1161 
1165  uint16_t port;
1166 
1167 #if HTTPS_SUPPORT
1168 
1171  gnutls_priority_t priority_cache;
1172 
1177  gnutls_credentials_type_t cred_type;
1178 
1182  gnutls_certificate_credentials_t x509_cred;
1183 
1187  gnutls_dh_params_t dh_params;
1188 
1189 #if GNUTLS_VERSION_MAJOR >= 3
1190 
1194  gnutls_certificate_retrieve_function2 *cert_callback;
1195 #endif
1196 
1200  const char *https_mem_key;
1201 
1205  const char *https_mem_cert;
1206 
1210  const char *https_mem_trust;
1211 
1215  gnutls_dh_params_t https_mem_dhparams;
1216 
1220  int have_dhparams;
1221 
1228  unsigned int num_tls_read_ready;
1229 
1230 #endif
1231 
1232 #ifdef DAUTH_SUPPORT
1233 
1237  const char *digest_auth_random;
1238 
1242  struct MHD_NonceNc *nnc;
1243 
1247  MHD_mutex_ nnc_lock;
1248 
1252  size_t digest_auth_rand_size;
1253 
1257  unsigned int nonce_nc_size;
1258 
1259 #endif
1260 
1261 #ifdef TCP_FASTOPEN
1262 
1265  unsigned int fastopen_queue_size;
1266 #endif
1267 };
1268 
1269 
1270 #if EXTRA_CHECKS
1271 #define EXTRA_CHECK(a) do { if (!(a)) abort(); } while (0)
1272 #else
1273 #define EXTRA_CHECK(a)
1274 #endif
1275 
1276 
1285 #define DLL_insert(head,tail,element) do { \
1286  EXTRA_CHECK (NULL == (element)->next); \
1287  EXTRA_CHECK (NULL == (element)->prev); \
1288  (element)->next = (head); \
1289  (element)->prev = NULL; \
1290  if ((tail) == NULL) \
1291  (tail) = element; \
1292  else \
1293  (head)->prev = element; \
1294  (head) = (element); } while (0)
1295 
1296 
1306 #define DLL_remove(head,tail,element) do { \
1307  EXTRA_CHECK ( (NULL != (element)->next) || ((element) == (tail))); \
1308  EXTRA_CHECK ( (NULL != (element)->prev) || ((element) == (head))); \
1309  if ((element)->prev == NULL) \
1310  (head) = (element)->next; \
1311  else \
1312  (element)->prev->next = (element)->next; \
1313  if ((element)->next == NULL) \
1314  (tail) = (element)->prev; \
1315  else \
1316  (element)->next->prev = (element)->prev; \
1317  (element)->next = NULL; \
1318  (element)->prev = NULL; } while (0)
1319 
1320 
1321 
1330 #define XDLL_insert(head,tail,element) do { \
1331  EXTRA_CHECK (NULL == (element)->nextX); \
1332  EXTRA_CHECK (NULL == (element)->prevX); \
1333  (element)->nextX = (head); \
1334  (element)->prevX = NULL; \
1335  if (NULL == (tail)) \
1336  (tail) = element; \
1337  else \
1338  (head)->prevX = element; \
1339  (head) = (element); } while (0)
1340 
1341 
1351 #define XDLL_remove(head,tail,element) do { \
1352  EXTRA_CHECK ( (NULL != (element)->nextX) || ((element) == (tail))); \
1353  EXTRA_CHECK ( (NULL != (element)->prevX) || ((element) == (head))); \
1354  if (NULL == (element)->prevX) \
1355  (head) = (element)->nextX; \
1356  else \
1357  (element)->prevX->nextX = (element)->nextX; \
1358  if (NULL == (element)->nextX) \
1359  (tail) = (element)->prevX; \
1360  else \
1361  (element)->nextX->prevX = (element)->prevX; \
1362  (element)->nextX = NULL; \
1363  (element)->prevX = NULL; } while (0)
1364 
1365 
1374 #define EDLL_insert(head,tail,element) do { \
1375  (element)->nextE = (head); \
1376  (element)->prevE = NULL; \
1377  if ((tail) == NULL) \
1378  (tail) = element; \
1379  else \
1380  (head)->prevE = element; \
1381  (head) = (element); } while (0)
1382 
1383 
1393 #define EDLL_remove(head,tail,element) do { \
1394  if ((element)->prevE == NULL) \
1395  (head) = (element)->nextE; \
1396  else \
1397  (element)->prevE->nextE = (element)->nextE; \
1398  if ((element)->nextE == NULL) \
1399  (tail) = (element)->prevE; \
1400  else \
1401  (element)->nextE->prevE = (element)->prevE; \
1402  (element)->nextE = NULL; \
1403  (element)->prevE = NULL; } while (0)
1404 
1405 
1413 time_t
1414 MHD_monotonic_time(void);
1415 
1416 
1422 void
1423 MHD_unescape_plus (char *arg);
1424 
1425 
1426 #endif
unsigned int per_ip_connection_limit
Definition: internal.h:1155
void * unescape_callback_cls
Definition: internal.h:1024
uint64_t total_size
Definition: internal.h:287
ssize_t(* TransmitCallback)(struct MHD_Connection *conn, const void *write_to, size_t max_bytes)
Definition: internal.h:502
off_t fd_off
Definition: internal.h:298
int(* write_handler)(struct MHD_Connection *connection)
Definition: internal.h:808
socklen_t addr_len
Definition: internal.h:699
int thread_joined
Definition: internal.h:738
enum MHD_CONNECTION_STATE state
Definition: internal.h:755
int(* idle_handler)(struct MHD_Connection *connection)
Definition: internal.h:813
internal platform abstraction functions
uint64_t response_write_position
Definition: internal.h:688
public interface to libmicrohttpd
enum MHD_ConnectionEventLoopInfo event_loop_info
Definition: internal.h:760
size_t current_chunk_size
Definition: internal.h:792
MHD_PanicCallback mhd_panic
Definition: daemon.c:146
MHD_ContentReaderFreeCallback crfc
Definition: internal.h:276
MHD_CONNECTION_STATE
Definition: internal.h:344
char * version
Definition: internal.h:604
void(* MHD_ContentReaderFreeCallback)(void *cls)
Definition: microhttpd.h:1321
MHD_socket socket_fd
Definition: internal.h:1092
struct MHD_Response * response
Definition: internal.h:566
char * colon
Definition: internal.h:633
char * write_buffer
Definition: internal.h:618
void(* MHD_RequestCompletedCallback)(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
Definition: microhttpd.h:1232
struct MHD_Connection * normal_timeout_tail
Definition: internal.h:965
void *(* LogCallback)(void *cls, const char *uri, struct MHD_Connection *con)
Definition: internal.h:867
size_t data_size
Definition: internal.h:304
unsigned long int nc
Definition: internal.h:191
int have_chunked_upload
Definition: internal.h:784
enum MHD_ValueKind kind
Definition: internal.h:236
struct MHD_HTTP_Header * first_header
Definition: internal.h:252
MHD_AccessHandlerCallback default_handler
Definition: internal.h:898
int MHD_socket
Definition: microhttpd.h:185
size_t current_chunk_offset
Definition: internal.h:798
uint64_t remaining_upload_size
Definition: internal.h:681
unsigned int responseCode
Definition: internal.h:766
platform-specific includes for libmicrohttpd
char * value
Definition: internal.h:230
void * uri_log_callback_cls
Definition: internal.h:1014
int(* read_handler)(struct MHD_Connection *connection)
Definition: internal.h:803
struct MHD_Daemon * daemon
Definition: internal.h:551
int MHD_pipe
Definition: platform.h:195
int listening_address_reuse
Definition: internal.h:1101
MHD_mutex_ per_ip_connection_mutex
Definition: internal.h:1082
struct MHD_Connection * manual_timeout_head
Definition: internal.h:972
struct MHD_Connection * cleanup_head
Definition: internal.h:928
int client_aware
Definition: internal.h:718
struct MHD_Connection * cleanup_tail
Definition: internal.h:933
struct MHD_Connection * prev
Definition: internal.h:532
size_t data_buffer_size
Definition: internal.h:309
size_t write_buffer_send_offset
Definition: internal.h:669
struct MHD_Daemon * worker_pool
Definition: internal.h:1047
size_t read_buffer_size
Definition: internal.h:653
void * client_context
Definition: internal.h:586
struct MHD_Connection * nextX
Definition: internal.h:541
struct MHD_Connection * manual_timeout_tail
Definition: internal.h:978
size_t continue_message_write_offset
Definition: internal.h:694
MHD_socket socket_fd
Definition: internal.h:725
char * method
Definition: internal.h:592
MHD_thread_handle_ pid
Definition: internal.h:645
unsigned int connection_limit
Definition: internal.h:1143
char nonce[MAX_NONCE_LENGTH]
Definition: internal.h:196
unsigned int worker_pool_size
Definition: internal.h:1072
LogCallback uri_log_callback
Definition: internal.h:1009
unsigned int reference_count
Definition: internal.h:315
int response_unready
Definition: internal.h:775
int(* MHD_AcceptPolicyCallback)(void *cls, const struct sockaddr *addr, socklen_t addrlen)
Definition: microhttpd.h:1164
int shutdown
Definition: internal.h:1128
#define MAX_NONCE_LENGTH
Definition: internal.h:177
ssize_t(* MHD_ContentReaderCallback)(void *cls, uint64_t pos, char *buf, size_t max)
Definition: microhttpd.h:1305
size_t(* UnescapeCallback)(void *cls, struct MHD_Connection *conn, char *uri)
Definition: internal.h:880
uint16_t port
Definition: internal.h:1165
char * last
Definition: internal.h:625
unsigned int connections
Definition: internal.h:1138
ssize_t(* ReceiveCallback)(struct MHD_Connection *conn, void *write_to, size_t max_bytes)
Definition: internal.h:490
struct MHD_Connection * normal_timeout_head
Definition: internal.h:959
MHD_ValueKind
Definition: microhttpd.h:900
char * read_buffer
Definition: internal.h:612
void * mhd_panic_cls
Definition: daemon.c:151
ReceiveCallback recv_cls
Definition: internal.h:818
size_t thread_stack_size
Definition: internal.h:1067
char * url
Definition: internal.h:598
size_t write_buffer_size
Definition: internal.h:664
uint64_t data_start
Definition: internal.h:293
UnescapeCallback unescape_callback
Definition: internal.h:1019
struct MHD_Connection * connections_head
Definition: internal.h:908
struct MHD_Daemon * master
Definition: internal.h:1042
size_t pool_size
Definition: internal.h:1057
char * header
Definition: internal.h:225
struct MHD_Connection * next
Definition: internal.h:527
MHD_AcceptPolicyCallback apc
Definition: internal.h:984
time_t last_activity
Definition: internal.h:705
unsigned int connection_timeout
Definition: internal.h:711
enum MHD_ResponseFlags flags
Definition: internal.h:325
struct MemoryPool * pool
Definition: internal.h:578
MHD_pipe wpipe[2]
Definition: internal.h:1123
size_t write_buffer_append_offset
Definition: internal.h:675
int resuming
Definition: internal.h:1133
struct MHD_HTTP_Header * next
Definition: internal.h:219
MHD_RequestCompletedCallback notify_completed
Definition: internal.h:995
struct MHD_Connection * prevX
Definition: internal.h:546
enum MHD_OPTION options
Definition: internal.h:1160
MHD_thread_handle_ pid
Definition: internal.h:1077
void * notify_completed_cls
Definition: internal.h:1000
TransmitCallback send_cls
Definition: internal.h:823
MHD_OPTION
MHD options.
Definition: microhttpd.h:595
void * apc_cls
Definition: internal.h:989
MHD_ContentReaderCallback crc
Definition: internal.h:270
void * crc_cls
Definition: internal.h:264
struct sockaddr * addr
Definition: internal.h:639
MHD_mutex_ mutex
Definition: internal.h:282
struct MHD_Connection * suspended_connections_tail
Definition: internal.h:923
MHD_ConnectionEventLoopInfo
Definition: internal.h:147
MHD_EpollState
Definition: internal.h:106
time_t MHD_monotonic_time(void)
Definition: internal.c:182
void * per_ip_connection_count
Definition: internal.h:1052
char * data
Definition: internal.h:258
int(* MHD_AccessHandlerCallback)(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)
Definition: microhttpd.h:1209
size_t read_buffer_offset
Definition: internal.h:659
void * default_handler_cls
Definition: internal.h:903
unsigned int connection_timeout
Definition: internal.h:1149
MHD_ResponseFlags
Definition: microhttpd.h:1790
MHD_mutex_ cleanup_connection_mutex
Definition: internal.h:1087
struct MHD_HTTP_Header * headers_received
Definition: internal.h:556
struct MHD_Connection * connections_tail
Definition: internal.h:913
struct MHD_HTTP_Header * headers_received_tail
Definition: internal.h:561
size_t pool_increment
Definition: internal.h:1062
void MHD_unescape_plus(char *arg)
Definition: internal.c:113
struct MHD_Connection * suspended_connections_head
Definition: internal.h:918
void(* MHD_PanicCallback)(void *cls, const char *file, unsigned int line, const char *reason)
Definition: microhttpd.h:1150