Browse Source

Add support for TCP_FASTOPEN_CONNECT

Nick Peng 7 years ago
parent
commit
7ab837b980
1 changed files with 15 additions and 0 deletions
  1. 15 0
      src/dns_client.c

+ 15 - 0
src/dns_client.c

@@ -33,6 +33,7 @@
 #include <netinet/ip.h>
 #include <netinet/ip6.h>
 #include <netinet/ip_icmp.h>
+#include <netinet/tcp.h>
 #include <openssl/err.h>
 #include <openssl/ssl.h>
 #include <pthread.h>
@@ -50,6 +51,10 @@
 #define DNS_HOSTNAME_LEN 128
 #define DNS_TCP_BUFFER (16 * 1024)
 
+#ifndef TCP_FASTOPEN_CONNECT
+#define TCP_FASTOPEN_CONNECT 30 
+#endif
+
 struct dns_client_ecs {
 	int enable;
 	unsigned int family;
@@ -739,6 +744,7 @@ static int _DNS_client_create_socket_tcp(struct dns_server_info *server_info)
 {
 	int fd = 0;
 	struct epoll_event event;
+	int yes = 1;
 
 	fd = socket(server_info->ai_family, SOCK_STREAM, 0);
 	if (fd < 0) {
@@ -751,6 +757,10 @@ static int _DNS_client_create_socket_tcp(struct dns_server_info *server_info)
 		goto errout;
 	}
 
+	if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &yes, sizeof(yes)) != 0 ) {
+		tlog(TLOG_DEBUG, "enable TCP fast open failed.");
+	}
+
 	if (connect(fd, (struct sockaddr *)&server_info->addr, server_info->ai_addrlen) != 0) {
 		if (errno != EINPROGRESS) {
 			tlog(TLOG_ERROR, "connect failed.");
@@ -784,6 +794,7 @@ static int _DNS_client_create_socket_tls(struct dns_server_info *server_info)
 	struct epoll_event event;
 	SSL_CTX *ctx = NULL;
 	SSL *ssl = NULL;
+	int yes = 1;
 
 	ctx = SSL_CTX_new(SSLv23_client_method());
 	if (ctx == NULL) {
@@ -808,6 +819,10 @@ static int _DNS_client_create_socket_tls(struct dns_server_info *server_info)
 		goto errout;
 	}
 
+	if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &yes, sizeof(yes)) != 0 ) {
+		tlog(TLOG_DEBUG, "enable TCP fast open failed.");
+	}
+
 	if (connect(fd, (struct sockaddr *)&server_info->addr, server_info->ai_addrlen) != 0) {
 		if (errno != EINPROGRESS) {
 			tlog(TLOG_ERROR, "connect failed.");