aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSree Harsha Totakura <sreeharsha@totakura.in>2014-02-24 16:25:43 +0000
committerSree Harsha Totakura <sreeharsha@totakura.in>2014-02-24 16:25:43 +0000
commitbc1137bd60c3eeb2e7ed0e8a968674a4ae9b2a2a (patch)
tree546b29061bb94cd0350f6d77a1a2831ee1fd8673
parentc4c1456adf519c3b587d1e797f9b500d896fa3d2 (diff)
downloadlibmicrohttpd-bc1137bd60c3eeb2e7ed0e8a968674a4ae9b2a2a.tar.gz
libmicrohttpd-bc1137bd60c3eeb2e7ed0e8a968674a4ae9b2a2a.zip
Add support for TCP FASTOPEN connections.
-rw-r--r--AUTHORS2
-rw-r--r--doc/libmicrohttpd.texi16
-rw-r--r--src/include/microhttpd.h20
-rw-r--r--src/microhttpd/daemon.c35
-rw-r--r--src/microhttpd/internal.h10
5 files changed, 80 insertions, 3 deletions
diff --git a/AUTHORS b/AUTHORS
index eaf2d4f4..4d51d9b2 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -46,8 +46,8 @@ Matthew Mundell <matthew.mundell@greenbone.net>
46Scott Goldman <scottjg@github.com> 46Scott Goldman <scottjg@github.com>
47Jared Cantwell 47Jared Cantwell
48Luke-Jr <luke@dashjr.org> 48Luke-Jr <luke@dashjr.org>
49Sree Harsha Totakura <sreeharsha@totakura.in>
49 50
50Documentation contributions also came from: 51Documentation contributions also came from:
51Marco Maggi <marco.maggi-ipsu@poste.it> 52Marco Maggi <marco.maggi-ipsu@poste.it>
52Sebastian Gerhardt <sebgerhardt@gmx.net> 53Sebastian Gerhardt <sebgerhardt@gmx.net>
53
diff --git a/doc/libmicrohttpd.texi b/doc/libmicrohttpd.texi
index 68485546..f670a830 100644
--- a/doc/libmicrohttpd.texi
+++ b/doc/libmicrohttpd.texi
@@ -519,6 +519,12 @@ Enables using @code{MHD_suspend_connection} and
519additional pipes to be created, and code not using these calls should 519additional pipes to be created, and code not using these calls should
520not pay the cost. 520not pay the cost.
521 521
522@item MHD_USE_TCP_FASTOPEN
523@cindex listen
524Enable TCP_FASTOPEN on the listen socket. TCP_FASTOPEN is currently
525supported on Linux >= 3.6. On other systems using this option with
526cause @code{MHD_start_daemon} to fail.
527
522@end table 528@end table
523@end deftp 529@end deftp
524 530
@@ -831,6 +837,16 @@ followed by a @code{size_t}). Not specifying this option or using
831a value of zero means using the system default (which is likely to 837a value of zero means using the system default (which is likely to
832differ based on your platform). 838differ based on your platform).
833 839
840@item MHD_OPTION_TCP_FASTQUEUE_QUEUE_SIZE
841@cindex listen
842When the flag @code{MHD_USE_TCP_FASTOPEN} is used, this option sets the
843connection handshake queue size for the TCP FASTOPEN connections. Note
844that a TCP FASTOPEN connection handshake occupies more resources than a
845TCP handshake as the SYN packets also contain DATA which is kept in the
846associate state until handshake is completed. If this option is not
847given the queue size is set to a default value of 10. This option must
848be followed by a @code{unsigned int}.
849
834@end table 850@end table
835@end deftp 851@end deftp
836 852
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h
index 2dd162fd..0f854cb5 100644
--- a/src/include/microhttpd.h
+++ b/src/include/microhttpd.h
@@ -551,7 +551,14 @@ enum MHD_FLAG
551 * Enable suspend/resume functions, which also implies setting up 551 * Enable suspend/resume functions, which also implies setting up
552 * pipes to signal resume. 552 * pipes to signal resume.
553 */ 553 */
554 MHD_USE_SUSPEND_RESUME = 8192 | MHD_USE_PIPE_FOR_SHUTDOWN 554 MHD_USE_SUSPEND_RESUME = 8192 | MHD_USE_PIPE_FOR_SHUTDOWN,
555
556 /**
557 * Enable TCP_FASTOPEN option. This option is only available on Linux with a
558 * kernel >= 3.6. On other systems, using this option cases #MHD_start_daemon
559 * to fail.
560 */
561 MHD_USE_TCP_FASTOPEN = 16384
555 562
556}; 563};
557 564
@@ -817,7 +824,16 @@ enum MHD_OPTION
817 * to access the SNI data using `gnutls_server_name_get()`. 824 * to access the SNI data using `gnutls_server_name_get()`.
818 * Using this option requires GnuTLS 3.0 or higher. 825 * Using this option requires GnuTLS 3.0 or higher.
819 */ 826 */
820 MHD_OPTION_HTTPS_CERT_CALLBACK = 22 827 MHD_OPTION_HTTPS_CERT_CALLBACK = 22,
828
829 /**
830 * When using #MHD_USE_TCP_FASTOPEN, this option changes the default TCP
831 * fastopen queue length of 50. Note that having a larger queue size can
832 * cause resource exhaustion attack as the TCP stack has to now allocate
833 * resources for the SYN packet along with its DATA. This option should be
834 * followed by an `unsigned int` argument.
835 */
836 MHD_OPTION_TCP_FASTOPEN_QUEUE_SIZE = 23
821 837
822}; 838};
823 839
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
index 6925c923..d5d34f17 100644
--- a/src/microhttpd/daemon.c
+++ b/src/microhttpd/daemon.c
@@ -75,6 +75,13 @@
75 */ 75 */
76#define MHD_POOL_SIZE_DEFAULT (32 * 1024) 76#define MHD_POOL_SIZE_DEFAULT (32 * 1024)
77 77
78#ifdef TCP_FASTOPEN
79/**
80 * Default TCP fastopen queue size.
81 */
82#define MHD_TCP_FASTOPEN_QUEUE_SIZE_DEFAULT 10
83#endif
84
78/** 85/**
79 * Print extra messages with reasons for closing 86 * Print extra messages with reasons for closing
80 * sockets? (only adds non-error messages). 87 * sockets? (only adds non-error messages).
@@ -2957,6 +2964,11 @@ parse_options_va (struct MHD_Daemon *daemon,
2957 case MHD_OPTION_THREAD_STACK_SIZE: 2964 case MHD_OPTION_THREAD_STACK_SIZE:
2958 daemon->thread_stack_size = va_arg (ap, size_t); 2965 daemon->thread_stack_size = va_arg (ap, size_t);
2959 break; 2966 break;
2967#ifdef TCP_FASTOPEN
2968 case MHD_OPTION_TCP_FASTOPEN_QUEUE_SIZE:
2969 daemon->fastopen_queue_size = va_arg (ap, unsigned int);
2970 break;
2971#endif
2960 case MHD_OPTION_ARRAY: 2972 case MHD_OPTION_ARRAY:
2961 oa = va_arg (ap, struct MHD_OptionItem*); 2973 oa = va_arg (ap, struct MHD_OptionItem*);
2962 i = 0; 2974 i = 0;
@@ -2981,6 +2993,7 @@ parse_options_va (struct MHD_Daemon *daemon,
2981 case MHD_OPTION_CONNECTION_TIMEOUT: 2993 case MHD_OPTION_CONNECTION_TIMEOUT:
2982 case MHD_OPTION_PER_IP_CONNECTION_LIMIT: 2994 case MHD_OPTION_PER_IP_CONNECTION_LIMIT:
2983 case MHD_OPTION_THREAD_POOL_SIZE: 2995 case MHD_OPTION_THREAD_POOL_SIZE:
2996 case MHD_OPTION_TCP_FASTOPEN_QUEUE_SIZE:
2984 if (MHD_YES != parse_options (daemon, 2997 if (MHD_YES != parse_options (daemon,
2985 servaddr, 2998 servaddr,
2986 opt, 2999 opt,
@@ -3229,6 +3242,10 @@ MHD_start_daemon_va (unsigned int flags,
3229 if (0 != (flags & MHD_USE_SSL)) 3242 if (0 != (flags & MHD_USE_SSL))
3230 return NULL; 3243 return NULL;
3231#endif 3244#endif
3245#ifndef TCP_FASTOPEN
3246 if (0 != (flags & MHD_USE_TCP_FASTSEND))
3247 return NULL;
3248#endif
3232 if (NULL == dh) 3249 if (NULL == dh)
3233 return NULL; 3250 return NULL;
3234 if (NULL == (daemon = malloc (sizeof (struct MHD_Daemon)))) 3251 if (NULL == (daemon = malloc (sizeof (struct MHD_Daemon))))
@@ -3541,6 +3558,24 @@ MHD_start_daemon_va (unsigned int flags,
3541 MHD_PANIC ("close failed\n"); 3558 MHD_PANIC ("close failed\n");
3542 goto free_and_fail; 3559 goto free_and_fail;
3543 } 3560 }
3561#ifdef TCP_FASTOPEN
3562 if (0 != (flags & MHD_USE_TCP_FASTOPEN))
3563 {
3564 if (0 == daemon->fastopen_queue_size)
3565 daemon->fastopen_queue_size = MHD_TCP_FASTOPEN_QUEUE_SIZE_DEFAULT;
3566 if (0 != setsockopt (socket_fd,
3567 IPPROTO_TCP, TCP_FASTOPEN,
3568 &daemon->fastopen_queue_size,
3569 sizeof (daemon->fastopen_queue_size)))
3570 {
3571#if HAVE_MESSAGES
3572 MHD_DLOG (daemon,
3573 "setsockopt failed: %s\n",
3574 MHD_socket_last_strerr_ ());
3575#endif
3576 }
3577 }
3578#endif
3544#if EPOLL_SUPPORT 3579#if EPOLL_SUPPORT
3545 if (0 != (flags & MHD_USE_EPOLL_LINUX_ONLY)) 3580 if (0 != (flags & MHD_USE_EPOLL_LINUX_ONLY))
3546 { 3581 {
diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h
index 05ea244a..53d1f85f 100644
--- a/src/microhttpd/internal.h
+++ b/src/microhttpd/internal.h
@@ -39,6 +39,10 @@
39#if EPOLL_SUPPORT 39#if EPOLL_SUPPORT
40#include <sys/epoll.h> 40#include <sys/epoll.h>
41#endif 41#endif
42#if HAVE_NETINET_TCP_H
43/* for TCP_FASTOPEN */
44#include <netinet/tcp.h>
45#endif
42 46
43 47
44/** 48/**
@@ -1228,6 +1232,12 @@ struct MHD_Daemon
1228 1232
1229#endif 1233#endif
1230 1234
1235#ifdef TCP_FASTOPEN
1236 /**
1237 * The queue size for incoming SYN + DATA packets.
1238 */
1239 unsigned int fastopen_queue_size;
1240#endif
1231}; 1241};
1232 1242
1233 1243