From 523f02e62b1fd3eaaf1b0cb44c8bd678f24de831 Mon Sep 17 00:00:00 2001 From: Yurun Date: Wed, 18 Oct 2023 12:05:07 +0800 Subject: [PATCH] Fix socket_import_stream() --- ext-src/swoole_socket_coro.cc | 14 +++++++--- tests/swoole_socket_coro/import_5.phpt | 38 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/swoole_socket_coro/import_5.phpt diff --git a/ext-src/swoole_socket_coro.cc b/ext-src/swoole_socket_coro.cc index a68e7f26ac2..a78ece50e3d 100644 --- a/ext-src/swoole_socket_coro.cc +++ b/ext-src/swoole_socket_coro.cc @@ -2167,14 +2167,20 @@ static PHP_METHOD(swoole_socket_coro, import) { } int sock_domain = AF_INET, sock_type = SOCK_STREAM; + php_sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); #ifdef SO_DOMAIN socklen_t sock_domain_len = sizeof(sock_domain); - if (getsockopt(socket_fd, SOL_SOCKET, SO_DOMAIN, &sock_domain, &sock_domain_len) < 0) { - php_swoole_sys_error(E_WARNING, "getsockopt(SOL_SOCKET, SO_DOMAIN) failed"); - RETURN_FALSE; - } + if (getsockopt(socket_fd, SOL_SOCKET, SO_DOMAIN, &sock_domain, &sock_domain_len) == 0) { + } else #endif + if (getsockname(socket_fd, (struct sockaddr*)&addr, &addr_len) == 0) { + sock_domain = addr.ss_family; + } else { + php_swoole_sys_error(E_WARNING, "getsockname() failed"); + RETURN_FALSE; + } #ifdef SO_TYPE socklen_t sock_type_len = sizeof(sock_type); diff --git a/tests/swoole_socket_coro/import_5.phpt b/tests/swoole_socket_coro/import_5.phpt new file mode 100644 index 00000000000..135704d5071 --- /dev/null +++ b/tests/swoole_socket_coro/import_5.phpt @@ -0,0 +1,38 @@ +--TEST-- +swoole_socket_coro: import 5 +--SKIPIF-- + +--FILE-- + +--EXPECT--