diff --git a/src/dns_client.c b/src/dns_client.c index c8f584c..0c93a14 100644 --- a/src/dns_client.c +++ b/src/dns_client.c @@ -60,6 +60,8 @@ #define DNS_TCP_CONNECT_TIMEOUT (5) #define DNS_QUERY_TIMEOUT (500) #define DNS_QUERY_RETRY (3) +#define SOCKET_PRIORITY (6) +#define SOCKET_IP_TOS (IPTOS_LOWDELAY | IPTOS_RELIABILITY) #ifndef TCP_FASTOPEN_CONNECT #define TCP_FASTOPEN_CONNECT 30 @@ -1312,6 +1314,8 @@ static int _dns_client_create_socket_udp(struct dns_server_info *server_info) struct epoll_event event; const int on = 1; const int val = 255; + const int priority = SOCKET_PRIORITY; + const int ip_tos = SOCKET_IP_TOS; fd = socket(server_info->ai_family, SOCK_DGRAM, 0); if (fd < 0) { @@ -1331,6 +1335,8 @@ static int _dns_client_create_socket_udp(struct dns_server_info *server_info) server_info->status = DNS_SERVER_STATUS_CONNECTIONLESS; setsockopt(server_info->fd, IPPROTO_IP, IP_RECVTTL, &on, sizeof(on)); setsockopt(server_info->fd, SOL_IP, IP_TTL, &val, sizeof(val)); + setsockopt(server_info->fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)); + setsockopt(server_info->fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos)); if (server_info->ai_family == AF_INET6) { /* for recving ip ttl value */ setsockopt(server_info->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, sizeof(on)); @@ -1352,6 +1358,8 @@ static int _DNS_client_create_socket_tcp(struct dns_server_info *server_info) int fd = 0; struct epoll_event event; int yes = 1; + const int priority = SOCKET_PRIORITY; + const int ip_tos = SOCKET_IP_TOS; fd = socket(server_info->ai_family, SOCK_STREAM, 0); if (fd < 0) { @@ -1370,6 +1378,8 @@ static int _DNS_client_create_socket_tcp(struct dns_server_info *server_info) } setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)); + setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)); + setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos)); if (connect(fd, (struct sockaddr *)&server_info->addr, server_info->ai_addrlen) != 0) { if (errno != EINPROGRESS) { @@ -1406,6 +1416,8 @@ static int _DNS_client_create_socket_tls(struct dns_server_info *server_info, ch struct epoll_event event; SSL *ssl = NULL; int yes = 1; + const int priority = SOCKET_PRIORITY; + const int ip_tos = SOCKET_IP_TOS; if (server_info->ssl_ctx == NULL) { tlog(TLOG_ERROR, "create ssl ctx failed."); @@ -1435,6 +1447,8 @@ static int _DNS_client_create_socket_tls(struct dns_server_info *server_info, ch // ? this cause ssl crash ? // setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)); + setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)); + setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos)); if (connect(fd, (struct sockaddr *)&server_info->addr, server_info->ai_addrlen) != 0) { if (errno != EINPROGRESS) { diff --git a/src/dns_server.c b/src/dns_server.c index bab1fc5..100bdce 100644 --- a/src/dns_server.c +++ b/src/dns_server.c @@ -38,7 +38,9 @@ #include #include #include -#include /* See NOTES */ +#include +#include +#include #define DNS_MAX_EVENTS 256 #define DNS_SERVER_TMOUT_TTL (5 * 60) @@ -47,6 +49,8 @@ #define DNS_PING_TIMEOUT (DNS_REQUEST_MAX_TIMEOUT) #define DNS_TCPPING_START (300) #define DNS_PING_SECOND_TIMEOUT (DNS_REQUEST_MAX_TIMEOUT - DNS_TCPPING_START) +#define SOCKET_IP_TOS (IPTOS_LOWDELAY | IPTOS_RELIABILITY) +#define SOCKET_PRIORITY (6) #define RECV_ERROR_AGAIN 1 #define RECV_ERROR_OK 0 @@ -2832,6 +2836,9 @@ static int _dns_create_socket(const char *host_ip, int type) int port; char *host = NULL; int optval = 1; + int yes = 1; + const int priority = SOCKET_PRIORITY; + const int ip_tos = SOCKET_IP_TOS; if (parse_ip(host_ip, ip, &port) == 0) { host = ip; @@ -2859,10 +2866,13 @@ static int _dns_create_socket(const char *host_ip, int type) tlog(TLOG_ERROR, "set socket opt failed."); goto errout; } + setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)); } else { setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &optval, sizeof(optval)); setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval, sizeof(optval)); } + setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)); + setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos)); if (bind(fd, gai->ai_addr, gai->ai_addrlen) != 0) { tlog(TLOG_ERROR, "bind service failed, %s\n", strerror(errno)); diff --git a/src/fast_ping.c b/src/fast_ping.c index 5be3673..19200b1 100644 --- a/src/fast_ping.c +++ b/src/fast_ping.c @@ -46,6 +46,7 @@ #define ICMP_INPACKET_SIZE 1024 #define IPV4_ADDR_LEN 4 #define IPV6_ADDR_LEN 16 +#define SOCKET_PRIORITY (6) #ifndef ICMP_FILTER #define ICMP_FILTER 1 @@ -550,6 +551,9 @@ static int _fast_ping_sendping_tcp(struct ping_host_struct *ping_host) struct epoll_event event; int flags; int fd = -1; + int yes = 1; + const int priority = SOCKET_PRIORITY; + const int ip_tos = IP_TOS; _fast_ping_close_host_sock(ping_host); @@ -560,6 +564,9 @@ static int _fast_ping_sendping_tcp(struct ping_host_struct *ping_host) flags = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, flags | O_NONBLOCK); + setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)); + setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)); + setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos)); ping_host->seq++; if (connect(fd, (struct sockaddr *)&ping_host->addr, ping_host->addr_len) != 0) { @@ -635,6 +642,7 @@ static int _fast_ping_create_icmp_sock(FAST_PING_TYPE type) socklen_t optlen = sizeof(buffsize); const int val = 255; const int on = 1; + const int ip_tos = (IPTOS_LOWDELAY | IPTOS_RELIABILITY); switch (type) { case FAST_PING_ICMP: @@ -669,6 +677,7 @@ static int _fast_ping_create_icmp_sock(FAST_PING_TYPE type) setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const char *)&buffsize, optlen); setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char *)&buffsize, optlen); setsockopt(fd, SOL_IP, IP_TTL, &val, sizeof(val)); + setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos)); memset(&event, 0, sizeof(event)); event.events = EPOLLIN; @@ -732,6 +741,7 @@ static int _fast_ping_create_udp_sock(FAST_PING_TYPE type) struct epoll_event event; const int val = 255; const int on = 1; + const int ip_tos = (IPTOS_LOWDELAY | IPTOS_RELIABILITY); switch (type) { case FAST_PING_UDP: @@ -763,6 +773,7 @@ static int _fast_ping_create_udp_sock(FAST_PING_TYPE type) setsockopt(fd, SOL_IP, IP_TTL, &val, sizeof(val)); setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &on, sizeof(on)); + setsockopt(fd, IPPROTO_IP, IP_TOS, &ip_tos, sizeof(ip_tos)); memset(&event, 0, sizeof(event)); event.events = EPOLLIN;