Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support c-ares, Refactor DNS #4275

Merged
merged 14 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ set(SWOOLE_VERSION 4.7.0-dev)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
cmake_minimum_required(VERSION 2.8)

file(READ ./config.h SWOOLE_CONFIG_FILE)

set(CMAKE_MACOSX_RPATH 1)
set(SWOOLE_LINK_LIBRARIES pthread dl)

if (APPLE)
set(CMAKE_SHARED_LINKER_FLAGS "-undefined dynamic_lookup")
include_directories(BEFORE /usr/local/include)
link_directories(BEFORE /usr/local/lib)
else()
list(APPEND SWOOLE_LINK_LIBRARIES rt crypt)
endif()
Expand Down Expand Up @@ -82,6 +86,17 @@ if (DEFINED brotli_dir)
link_directories(${brotli_dir}/lib)
endif()

foreach (LINE ${SWOOLE_CONFIG_FILE})
if ("${LINE}" MATCHES "define SW_USE_CARES 1")
message(STATUS "enable c-ares")
list(APPEND SWOOLE_LINK_LIBRARIES cares)
endif()
endforeach()

if (DEFINED enable_trace_log)
add_definitions(-DSW_LOG_TRACE_OPEN)
endif()

execute_process(COMMAND php-config --includes OUTPUT_VARIABLE PHP_INCLUDES OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND php-config --extension-dir OUTPUT_VARIABLE PHP_EXTENSION_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PHP_INCLUDES}")
Expand Down
13 changes: 12 additions & 1 deletion config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ PHP_ARG_ENABLE([mysqlnd],
[enable mysqlnd support],
[AS_HELP_STRING([--enable-mysqlnd],
[Enable mysqlnd])], [no], [no])

PHP_ARG_ENABLE([cares],
[enable c-ares support],
[AS_HELP_STRING([--enable-cares],
[Enable cares])], [no], [no])

PHP_ARG_WITH([openssl_dir],
[dir of openssl],
Expand Down Expand Up @@ -354,7 +359,8 @@ if test "$PHP_SWOOLE" != "no"; then
AC_CHECK_LIB(pthread, pthread_mutexattr_setrobust, AC_DEFINE(HAVE_PTHREAD_MUTEXATTR_SETROBUST, 1, [have pthread_mutexattr_setrobust]))
AC_CHECK_LIB(pthread, pthread_mutex_consistent, AC_DEFINE(HAVE_PTHREAD_MUTEX_CONSISTENT, 1, [have pthread_mutex_consistent]))
AC_CHECK_LIB(pcre, pcre_compile, AC_DEFINE(HAVE_PCRE, 1, [have pcre]))

AC_CHECK_LIB(cares, ares_gethostbyname, AC_DEFINE(HAVE_CARES, 1, [have c-ares]))

if test "$PHP_SWOOLE_DEV" = "yes"; then
AX_CHECK_COMPILE_FLAG(-Wbool-conversion, _MAINTAINER_CFLAGS="$_MAINTAINER_CFLAGS -Wbool-conversion")
AX_CHECK_COMPILE_FLAG(-Wignored-qualifiers, _MAINTAINER_CFLAGS="$_MAINTAINER_CFLAGS -Wignored-qualifiers")
Expand Down Expand Up @@ -457,6 +463,11 @@ if test "$PHP_SWOOLE" != "no"; then
if test "$PHP_THREAD" = "yes"; then
AC_DEFINE(SW_USE_THREAD, 1, [enable thread support])
fi

if test "$PHP_CARES" = "yes"; then
AC_DEFINE(SW_USE_CARES, 1, [do we enable c-ares support])
PHP_ADD_LIBRARY(cares, 1, SWOOLE_SHARED_LIBADD)
fi

AC_SWOOLE_CPU_AFFINITY
AC_SWOOLE_HAVE_REUSEPORT
Expand Down
7 changes: 7 additions & 0 deletions core-tests/src/coroutine/gethostbyname.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ TEST(coroutine_gethostbyname, resolve_cache) {
});
}

TEST(coroutine_gethostbyname, impl_async) {
coroutine::run([](void *arg) {
auto result = swoole::coroutine::gethostbyname_impl_with_async("www.baidu.com", AF_INET);
ASSERT_EQ(result.empty(), false);
});
}

TEST(coroutine_gethostbyname, resolve_cache_inet4_and_inet6) {
coroutine::run([](void *arg) {
System::set_dns_cache_capacity(10);
Expand Down
2 changes: 1 addition & 1 deletion core-tests/src/coroutine/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ TEST(coroutine_system, flock) {
}

TEST(coroutine_system, cancel_sleep) {
coroutine::run([](void *arg) {
test::coroutine::run([](void *arg) {
auto co = Coroutine::get_current_safe();
Coroutine::create([co](void *){
System::sleep(0.002);
Expand Down
43 changes: 41 additions & 2 deletions core-tests/src/network/dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,55 @@
#include "swoole_socket.h"

using namespace swoole;
using swoole::coroutine::Socket;
using swoole::coroutine::System;
using namespace swoole::test;
using namespace std;

TEST(dns, lookup) {
TEST(dns, lookup1) {
test::coroutine::run([](void *arg) {
auto list = swoole::coroutine::dns_lookup("www.baidu.com", 10);
auto list = swoole::coroutine::dns_lookup("www.baidu.com", AF_INET, 10);
ASSERT_GE(list.size(), 1);
});
}

TEST(dns, lookup_ipv6) {
test::coroutine::run([](void *arg) {
auto list = swoole::coroutine::dns_lookup("www.google.com", AF_INET6, 2);
ASSERT_GE(list.size(), 1);
});
}

TEST(dns, domain_not_found) {
test::coroutine::run([](void *arg) {
auto list = swoole::coroutine::dns_lookup("www.baidu.com-not-found", AF_INET, 2);
ASSERT_EQ(list.size(), 0);
ASSERT_EQ(swoole_get_last_error(), SW_ERROR_DNSLOOKUP_RESOLVE_FAILED);
});
}

TEST(dns, bad_family) {
test::coroutine::run([](void *arg) {
auto list = swoole::coroutine::dns_lookup("www.google.com", 9999, 2);
ASSERT_GE(list.size(), 1);
});
}

TEST(dns, cancel) {
// swoole_set_trace_flags(SW_TRACE_CARES);
// swoole_set_log_level(SW_LOG_TRACE);
test::coroutine::run([](void *arg) {
auto co = Coroutine::get_current_safe();
Coroutine::create([co](void *){
System::sleep(0.002);
co->cancel();
});
auto list1 = swoole::coroutine::dns_lookup("www.baidu-not-found-for-cancel.com", AF_INET, 2);
ASSERT_EQ(list1.size(), 0);
ASSERT_EQ(swoole_get_last_error(), SW_ERROR_CO_CANCELED);
});
}

TEST(dns, getaddrinfo) {
char buf[1024] = {};
swoole::network::GetaddrinfoRequest req = {};
Expand Down
8 changes: 7 additions & 1 deletion core-tests/src/os/process_pool.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#include "test_core.h"
#include "swoole_process_pool.h"

#include <signal.h>

#ifdef __MACH__
#define sysv_signal signal
#endif

using namespace swoole;

static void test_func(ProcessPool &pool) {
Expand Down Expand Up @@ -114,4 +120,4 @@ TEST(process_pool, shutdown) {
pool.destroy();

ASSERT_EQ(*shm_value, magic_number);
}
}
4 changes: 4 additions & 0 deletions examples/http/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function dump($var)
//'open_cpu_affinity' => 1,
//'task_worker_num' => 100,
//'enable_port_reuse' => true,
// 'http_compression' => false,
'worker_num' => 1,
//'log_file' => __DIR__.'/swoole.log',
// 'reactor_num' => 24,
Expand Down Expand Up @@ -89,6 +90,9 @@ function no_chunk(swoole_http_request $request, swoole_http_response $response)
$response->status(404);
$response->end();
return;
} else if ($request->server['request_uri'] == '/big_response') {
var_dump($response->end(str_repeat('A', 16 * 1024 * 1024)));
return;
} else if ($request->server['request_uri'] == '/code') {
$response->sendfile(__FILE__);
return;
Expand Down
13 changes: 9 additions & 4 deletions ext-src/php_swoole.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ END_EXTERN_C()
#include <brotli/decode.h>
#endif

#ifdef SW_USE_CARES
#include <ares.h>
#endif

using swoole::network::Socket;

ZEND_DECLARE_MODULE_GLOBALS(swoole)
Expand Down Expand Up @@ -93,6 +97,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_async_dns_lookup_coro, 0, 0, 1)
ZEND_ARG_INFO(0, domain_name)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, type)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_coroutine_create, 0, 0, 1)
Expand Down Expand Up @@ -344,10 +349,7 @@ void php_swoole_set_global_option(HashTable *vht) {
SWOOLE_G(display_errors) = zval_is_true(ztmp);
}
if (php_swoole_array_get_value(vht, "dns_server", ztmp)) {
if (SwooleG.dns_server_v4) {
sw_free(SwooleG.dns_server_v4);
}
SwooleG.dns_server_v4 = zend::String(ztmp).dup();
swoole_set_dns_server(zend::String(ztmp).to_std_string());
}

auto timeout_format = [](zval *v) -> double {
Expand Down Expand Up @@ -879,6 +881,9 @@ PHP_MINFO_FUNCTION(swoole) {
#ifdef HAVE_PCRE
php_info_print_table_row(2, "pcre", "enabled");
#endif
#ifdef SW_USE_CARES
php_info_print_table_row(2, "c-ares", ares_version(nullptr));
#endif
#ifdef SW_HAVE_ZLIB
#ifdef ZLIB_VERSION
php_info_print_table_row(2, "zlib", ZLIB_VERSION);
Expand Down
1 change: 1 addition & 0 deletions ext-src/php_swoole_coroutine_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_coroutine_system_dnsLookup, 0, 0, 1)
ZEND_ARG_INFO(0, domain_name)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, type)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_coroutine_system_getaddrinfo, 0, 0, 1)
Expand Down
4 changes: 2 additions & 2 deletions ext-src/php_swoole_mysql_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ class lcb_packet : public server_packet
{
swMysqlPacketDump(header.length, header.number, data, "Protocol::LengthCodedBinary");
bytes_length = read_lcb(data + SW_MYSQL_PACKET_HEADER_SIZE, &length, &nul);
swTraceLog(SW_TRACE_MYSQL_CLIENT, "binary_length=%" PRIu64 ", nul=%u", header.length, nul);
swTraceLog(SW_TRACE_MYSQL_CLIENT, "binary_length=%u, nul=%u", header.length, nul);
}
bool is_vaild()
{
Expand Down Expand Up @@ -801,7 +801,7 @@ class row_data_text
swTraceLog(
SW_TRACE_MYSQL_CLIENT,
"text[%" PRIu64 "]: %.*s%s",
length, SW_MIN(64, length), body,
length, (int) SW_MIN(64, length), body,
nul ? "null" : ((length > 64 /*|| length > readable_length*/) ? "..." : "")
);
}
Expand Down
6 changes: 6 additions & 0 deletions ext-src/php_swoole_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ extern PHPAPI int php_array_merge(zend_array *dest, zend_array *src);
#endif
#endif

#ifdef SW_USE_CARES
#ifndef HAVE_CARES
#error "Enable c-ares support, require c-ares library"
#endif
#endif

#ifdef SW_SOCKETS
#include "ext/sockets/php_sockets.h"
#define SWOOLE_SOCKETS_SUPPORT
Expand Down
5 changes: 3 additions & 2 deletions ext-src/swoole_async_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ PHP_FUNCTION(swoole_async_dns_lookup_coro) {
Coroutine::get_current_safe();

zval *domain;
long type = AF_INET;
double timeout = swoole::network::Socket::default_dns_timeout;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|d", &domain, &timeout) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|dl", &domain, &timeout, &type) == FAILURE) {
RETURN_FALSE;
}

Expand All @@ -147,7 +148,7 @@ PHP_FUNCTION(swoole_async_dns_lookup_coro) {

php_swoole_check_reactor();

vector<string> result = swoole::coroutine::dns_lookup(Z_STRVAL_P(domain), timeout);
vector<string> result = swoole::coroutine::dns_lookup(Z_STRVAL_P(domain), type, timeout);
if (result.empty()) {
swoole_set_last_error(SW_ERROR_DNSLOOKUP_RESOLVE_FAILED);
RETURN_FALSE;
Expand Down
10 changes: 5 additions & 5 deletions ext-src/swoole_http_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ using http_context = swoole::http::Context;
zend_class_entry *swoole_http_response_ce;
static zend_object_handlers swoole_http_response_handlers;

static void http_build_header(http_context *ctx, swString *response, size_t body_length);
static ssize_t http_build_trailer(http_context *ctx, swString *response);
static void http_build_header(http_context *ctx, String *response, size_t body_length);
static ssize_t http_build_trailer(http_context *ctx, String *response);

static inline void http_header_key_format(char *key, int length) {
int i, state = 0;
Expand Down Expand Up @@ -360,7 +360,7 @@ static bool parse_header_flags(http_context *ctx, const char *key, size_t keylen
return true;
}

static void http_build_header(http_context *ctx, swString *response, size_t body_length) {
static void http_build_header(http_context *ctx, String *response, size_t body_length) {
char *buf = sw_tg_buffer()->str;
size_t l_buf = sw_tg_buffer()->size;
int n;
Expand Down Expand Up @@ -503,7 +503,7 @@ static void http_build_header(http_context *ctx, swString *response, size_t body
ctx->send_header_ = 1;
}

static ssize_t http_build_trailer(http_context *ctx, swString *response) {
static ssize_t http_build_trailer(http_context *ctx, String *response) {
char *buf = sw_tg_buffer()->str;
size_t l_buf = sw_tg_buffer()->size;
int n;
Expand Down Expand Up @@ -1257,7 +1257,7 @@ static PHP_METHOD(swoole_http_response, recv) {

Socket *sock = (Socket *) ctx->private_data;
ssize_t retval = sock->recv_packet(timeout);
swString _tmp;
String _tmp;

if (retval < 0) {
swoole_set_last_error(sock->errCode);
Expand Down
Loading