nodejs/deps/uv/test/test-getsockname.c

363 lines
8.8 KiB
C
Raw Permalink Normal View History

2011-07-14 14:28:52 -07:00
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "task.h"
#include <stdio.h>
2011-08-26 00:23:17 +02:00
#include <stdlib.h>
#include <string.h>
2011-07-14 14:28:52 -07:00
2011-09-04 22:25:40 +02:00
static const int server_port = TEST_PORT;
/* Will be updated right after making the uv_connect_call */
static int connect_port = -1;
2011-07-14 14:28:52 -07:00
deps: upgrade to libuv 1.44.2 Notable changes: - Build regression fixes for various platform updates (https://github.com/libuv/libuv/pull/3428, https://github.com/libuv/libuv/pull/3419, https://github.com/libuv/libuv/pull/3423, https://github.com/libuv/libuv/pull/3413, https://github.com/libuv/libuv/pull/3431) - Support for GNU/Hurd (https://github.com/libuv/libuv/pull/3450) - Release tool improvements (https://github.com/libuv/libuv-release-tool/pull/13) - Better performing rw locks on Win32 (https://github.com/libuv/libuv/pull/3383) - Support for posix_spawn API (https://github.com/libuv/libuv/pull/3257) - Fix regression on OpenBSD (https://github.com/libuv/libuv/pull/3506) - Add uv_available_parallelism() (https://github.com/libuv/libuv/pull/3499) - Don't use thread-unsafe strtok() (https://github.com/libuv/libuv/pull/3524) - Fix hang after NOTE_EXIT (https://github.com/libuv/libuv/pull/3521) - Better align order-of-events behavior between platforms (https://github.com/libuv/libuv/pull/3598) - Fix fs event not fired if the watched file is moved/removed/recreated (https://github.com/libuv/libuv/pull/3540) - Fix pipe resource leak if closed during connect (and other bugs) (https://github.com/libuv/libuv/pull/3611) - Don't error when killing a zombie process (https://github.com/libuv/libuv/pull/3625) - Avoid posix_spawnp() cwd bug (https://github.com/libuv/libuv/pull/3597) - Skip EVFILT_PROC events when invalidating events for an fd (https://github.com/libuv/libuv/pull/3629) Fixes: https://github.com/nodejs/node/issues/42290 PR-URL: https://github.com/nodejs/node/pull/42340 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-03-15 09:34:01 +01:00
static int getsocknamecount_tcp = 0;
2011-09-04 22:25:40 +02:00
static int getpeernamecount = 0;
deps: upgrade to libuv 1.44.2 Notable changes: - Build regression fixes for various platform updates (https://github.com/libuv/libuv/pull/3428, https://github.com/libuv/libuv/pull/3419, https://github.com/libuv/libuv/pull/3423, https://github.com/libuv/libuv/pull/3413, https://github.com/libuv/libuv/pull/3431) - Support for GNU/Hurd (https://github.com/libuv/libuv/pull/3450) - Release tool improvements (https://github.com/libuv/libuv-release-tool/pull/13) - Better performing rw locks on Win32 (https://github.com/libuv/libuv/pull/3383) - Support for posix_spawn API (https://github.com/libuv/libuv/pull/3257) - Fix regression on OpenBSD (https://github.com/libuv/libuv/pull/3506) - Add uv_available_parallelism() (https://github.com/libuv/libuv/pull/3499) - Don't use thread-unsafe strtok() (https://github.com/libuv/libuv/pull/3524) - Fix hang after NOTE_EXIT (https://github.com/libuv/libuv/pull/3521) - Better align order-of-events behavior between platforms (https://github.com/libuv/libuv/pull/3598) - Fix fs event not fired if the watched file is moved/removed/recreated (https://github.com/libuv/libuv/pull/3540) - Fix pipe resource leak if closed during connect (and other bugs) (https://github.com/libuv/libuv/pull/3611) - Don't error when killing a zombie process (https://github.com/libuv/libuv/pull/3625) - Avoid posix_spawnp() cwd bug (https://github.com/libuv/libuv/pull/3597) - Skip EVFILT_PROC events when invalidating events for an fd (https://github.com/libuv/libuv/pull/3629) Fixes: https://github.com/nodejs/node/issues/42290 PR-URL: https://github.com/nodejs/node/pull/42340 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-03-15 09:34:01 +01:00
static int getsocknamecount_udp = 0;
2011-07-14 14:28:52 -07:00
static uv_loop_t* loop;
2011-07-14 14:28:52 -07:00
static uv_tcp_t tcp;
2011-08-26 00:23:17 +02:00
static uv_udp_t udp;
static uv_connect_t connect_req;
2011-07-14 14:28:52 -07:00
static uv_tcp_t tcpServer;
2011-08-26 00:23:17 +02:00
static uv_udp_t udpServer;
static uv_udp_send_t send_req;
2011-07-14 14:28:52 -07:00
static void alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
buf->base = malloc(suggested_size);
buf->len = suggested_size;
2011-07-14 14:28:52 -07:00
}
static void on_close(uv_handle_t* peer) {
free(peer);
uv_close((uv_handle_t*)&tcpServer, NULL);
}
static void after_shutdown(uv_shutdown_t* req, int status) {
uv_close((uv_handle_t*) req->handle, on_close);
2011-07-14 14:28:52 -07:00
free(req);
}
static void after_read(uv_stream_t* handle,
ssize_t nread,
const uv_buf_t* buf) {
uv_shutdown_t* req;
int r;
2011-07-14 14:28:52 -07:00
if (buf->base) {
free(buf->base);
2011-07-14 14:28:52 -07:00
}
req = (uv_shutdown_t*) malloc(sizeof *req);
r = uv_shutdown(req, handle, after_shutdown);
ASSERT_OK(r);
2011-07-14 14:28:52 -07:00
}
2011-09-04 22:25:40 +02:00
static void check_sockname(struct sockaddr* addr, const char* compare_ip,
int compare_port, const char* context) {
struct sockaddr_in check_addr = *(struct sockaddr_in*) addr;
struct sockaddr_in compare_addr;
2011-09-04 22:25:40 +02:00
char check_ip[17];
int r;
ASSERT_OK(uv_ip4_addr(compare_ip, compare_port, &compare_addr));
2011-12-01 22:50:09 +01:00
/* Both addresses should be ipv4 */
ASSERT_EQ(check_addr.sin_family, AF_INET);
ASSERT_EQ(compare_addr.sin_family, AF_INET);
2011-09-04 22:25:40 +02:00
/* Check if the ip matches */
ASSERT_OK(memcmp(&check_addr.sin_addr,
&compare_addr.sin_addr,
sizeof compare_addr.sin_addr));
2011-09-04 22:25:40 +02:00
/* Check if the port matches. If port == 0 anything goes. */
ASSERT(compare_port == 0 || check_addr.sin_port == compare_addr.sin_port);
r = uv_ip4_name(&check_addr, (char*) check_ip, sizeof check_ip);
ASSERT_OK(r);
2011-09-04 22:25:40 +02:00
printf("%s: %s:%d\n", context, check_ip, ntohs(check_addr.sin_port));
}
2011-07-27 03:54:00 +02:00
static void on_connection(uv_stream_t* server, int status) {
2011-09-04 22:25:40 +02:00
struct sockaddr sockname, peername;
int namelen;
2012-11-16 17:57:15 +01:00
uv_tcp_t* handle;
2011-07-14 14:28:52 -07:00
int r;
if (status != 0) {
2013-07-16 21:04:31 +02:00
fprintf(stderr, "Connect error %s\n", uv_err_name(status));
2011-07-14 14:28:52 -07:00
}
ASSERT_OK(status);
2011-07-14 14:28:52 -07:00
2012-11-16 17:57:15 +01:00
handle = malloc(sizeof(*handle));
ASSERT_NOT_NULL(handle);
2011-07-14 14:28:52 -07:00
2012-11-16 17:57:15 +01:00
r = uv_tcp_init(loop, handle);
ASSERT_OK(r);
2011-07-14 14:28:52 -07:00
/* associate server with stream */
handle->data = server;
r = uv_accept(server, (uv_stream_t*)handle);
ASSERT_OK(r);
2011-07-14 14:28:52 -07:00
2011-09-04 22:25:40 +02:00
namelen = sizeof sockname;
2012-11-16 17:57:15 +01:00
r = uv_tcp_getsockname(handle, &sockname, &namelen);
ASSERT_OK(r);
2011-09-04 22:25:40 +02:00
check_sockname(&sockname, "127.0.0.1", server_port, "accepted socket");
deps: upgrade to libuv 1.44.2 Notable changes: - Build regression fixes for various platform updates (https://github.com/libuv/libuv/pull/3428, https://github.com/libuv/libuv/pull/3419, https://github.com/libuv/libuv/pull/3423, https://github.com/libuv/libuv/pull/3413, https://github.com/libuv/libuv/pull/3431) - Support for GNU/Hurd (https://github.com/libuv/libuv/pull/3450) - Release tool improvements (https://github.com/libuv/libuv-release-tool/pull/13) - Better performing rw locks on Win32 (https://github.com/libuv/libuv/pull/3383) - Support for posix_spawn API (https://github.com/libuv/libuv/pull/3257) - Fix regression on OpenBSD (https://github.com/libuv/libuv/pull/3506) - Add uv_available_parallelism() (https://github.com/libuv/libuv/pull/3499) - Don't use thread-unsafe strtok() (https://github.com/libuv/libuv/pull/3524) - Fix hang after NOTE_EXIT (https://github.com/libuv/libuv/pull/3521) - Better align order-of-events behavior between platforms (https://github.com/libuv/libuv/pull/3598) - Fix fs event not fired if the watched file is moved/removed/recreated (https://github.com/libuv/libuv/pull/3540) - Fix pipe resource leak if closed during connect (and other bugs) (https://github.com/libuv/libuv/pull/3611) - Don't error when killing a zombie process (https://github.com/libuv/libuv/pull/3625) - Avoid posix_spawnp() cwd bug (https://github.com/libuv/libuv/pull/3597) - Skip EVFILT_PROC events when invalidating events for an fd (https://github.com/libuv/libuv/pull/3629) Fixes: https://github.com/nodejs/node/issues/42290 PR-URL: https://github.com/nodejs/node/pull/42340 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-03-15 09:34:01 +01:00
getsocknamecount_tcp++;
2011-07-14 14:28:52 -07:00
2011-09-04 22:25:40 +02:00
namelen = sizeof peername;
2012-11-16 17:57:15 +01:00
r = uv_tcp_getpeername(handle, &peername, &namelen);
ASSERT_OK(r);
2011-09-04 22:25:40 +02:00
check_sockname(&peername, "127.0.0.1", connect_port, "accepted socket peer");
getpeernamecount++;
2011-07-14 14:28:52 -07:00
r = uv_read_start((uv_stream_t*)handle, alloc, after_read);
ASSERT_OK(r);
2011-07-14 14:28:52 -07:00
}
static void on_connect(uv_connect_t* req, int status) {
2011-09-04 22:25:40 +02:00
struct sockaddr sockname, peername;
int r, namelen;
2011-07-14 14:28:52 -07:00
ASSERT_OK(status);
2011-09-04 22:25:40 +02:00
namelen = sizeof sockname;
r = uv_tcp_getsockname((uv_tcp_t*) req->handle, &sockname, &namelen);
ASSERT_OK(r);
2011-09-04 22:25:40 +02:00
check_sockname(&sockname, "127.0.0.1", 0, "connected socket");
deps: upgrade to libuv 1.44.2 Notable changes: - Build regression fixes for various platform updates (https://github.com/libuv/libuv/pull/3428, https://github.com/libuv/libuv/pull/3419, https://github.com/libuv/libuv/pull/3423, https://github.com/libuv/libuv/pull/3413, https://github.com/libuv/libuv/pull/3431) - Support for GNU/Hurd (https://github.com/libuv/libuv/pull/3450) - Release tool improvements (https://github.com/libuv/libuv-release-tool/pull/13) - Better performing rw locks on Win32 (https://github.com/libuv/libuv/pull/3383) - Support for posix_spawn API (https://github.com/libuv/libuv/pull/3257) - Fix regression on OpenBSD (https://github.com/libuv/libuv/pull/3506) - Add uv_available_parallelism() (https://github.com/libuv/libuv/pull/3499) - Don't use thread-unsafe strtok() (https://github.com/libuv/libuv/pull/3524) - Fix hang after NOTE_EXIT (https://github.com/libuv/libuv/pull/3521) - Better align order-of-events behavior between platforms (https://github.com/libuv/libuv/pull/3598) - Fix fs event not fired if the watched file is moved/removed/recreated (https://github.com/libuv/libuv/pull/3540) - Fix pipe resource leak if closed during connect (and other bugs) (https://github.com/libuv/libuv/pull/3611) - Don't error when killing a zombie process (https://github.com/libuv/libuv/pull/3625) - Avoid posix_spawnp() cwd bug (https://github.com/libuv/libuv/pull/3597) - Skip EVFILT_PROC events when invalidating events for an fd (https://github.com/libuv/libuv/pull/3629) Fixes: https://github.com/nodejs/node/issues/42290 PR-URL: https://github.com/nodejs/node/pull/42340 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-03-15 09:34:01 +01:00
getsocknamecount_tcp++;
2011-07-14 14:28:52 -07:00
2011-09-04 22:25:40 +02:00
namelen = sizeof peername;
r = uv_tcp_getpeername((uv_tcp_t*) req->handle, &peername, &namelen);
ASSERT_OK(r);
2011-09-04 22:25:40 +02:00
check_sockname(&peername, "127.0.0.1", server_port, "connected socket peer");
getpeernamecount++;
2011-07-14 14:28:52 -07:00
uv_close((uv_handle_t*)&tcp, NULL);
}
2013-01-11 13:49:45 +01:00
static int tcp_listener(void) {
struct sockaddr_in addr;
2011-09-04 22:25:40 +02:00
struct sockaddr sockname, peername;
int namelen;
2011-07-14 14:28:52 -07:00
int r;
ASSERT_OK(uv_ip4_addr("0.0.0.0", server_port, &addr));
r = uv_tcp_init(loop, &tcpServer);
2011-07-14 14:28:52 -07:00
if (r) {
fprintf(stderr, "Socket creation error\n");
return 1;
}
2014-01-27 21:30:51 +04:00
r = uv_tcp_bind(&tcpServer, (const struct sockaddr*) &addr, 0);
2011-07-14 14:28:52 -07:00
if (r) {
fprintf(stderr, "Bind error\n");
return 1;
}
2011-07-27 03:54:00 +02:00
r = uv_listen((uv_stream_t*)&tcpServer, 128, on_connection);
2011-07-14 14:28:52 -07:00
if (r) {
fprintf(stderr, "Listen error\n");
return 1;
}
2011-08-26 00:23:17 +02:00
memset(&sockname, -1, sizeof sockname);
2011-09-04 22:25:40 +02:00
namelen = sizeof sockname;
r = uv_tcp_getsockname(&tcpServer, &sockname, &namelen);
ASSERT_OK(r);
2011-09-04 22:25:40 +02:00
check_sockname(&sockname, "0.0.0.0", server_port, "server socket");
deps: upgrade to libuv 1.44.2 Notable changes: - Build regression fixes for various platform updates (https://github.com/libuv/libuv/pull/3428, https://github.com/libuv/libuv/pull/3419, https://github.com/libuv/libuv/pull/3423, https://github.com/libuv/libuv/pull/3413, https://github.com/libuv/libuv/pull/3431) - Support for GNU/Hurd (https://github.com/libuv/libuv/pull/3450) - Release tool improvements (https://github.com/libuv/libuv-release-tool/pull/13) - Better performing rw locks on Win32 (https://github.com/libuv/libuv/pull/3383) - Support for posix_spawn API (https://github.com/libuv/libuv/pull/3257) - Fix regression on OpenBSD (https://github.com/libuv/libuv/pull/3506) - Add uv_available_parallelism() (https://github.com/libuv/libuv/pull/3499) - Don't use thread-unsafe strtok() (https://github.com/libuv/libuv/pull/3524) - Fix hang after NOTE_EXIT (https://github.com/libuv/libuv/pull/3521) - Better align order-of-events behavior between platforms (https://github.com/libuv/libuv/pull/3598) - Fix fs event not fired if the watched file is moved/removed/recreated (https://github.com/libuv/libuv/pull/3540) - Fix pipe resource leak if closed during connect (and other bugs) (https://github.com/libuv/libuv/pull/3611) - Don't error when killing a zombie process (https://github.com/libuv/libuv/pull/3625) - Avoid posix_spawnp() cwd bug (https://github.com/libuv/libuv/pull/3597) - Skip EVFILT_PROC events when invalidating events for an fd (https://github.com/libuv/libuv/pull/3629) Fixes: https://github.com/nodejs/node/issues/42290 PR-URL: https://github.com/nodejs/node/pull/42340 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-03-15 09:34:01 +01:00
getsocknamecount_tcp++;
2011-07-14 14:28:52 -07:00
2011-09-04 22:25:40 +02:00
namelen = sizeof sockname;
r = uv_tcp_getpeername(&tcpServer, &peername, &namelen);
ASSERT_EQ(r, UV_ENOTCONN);
2011-09-04 22:25:40 +02:00
getpeernamecount++;
2011-07-14 14:28:52 -07:00
return 0;
}
2013-01-11 13:49:45 +01:00
static void tcp_connector(void) {
struct sockaddr_in server_addr;
2011-09-04 22:25:40 +02:00
struct sockaddr sockname;
int r, namelen;
2011-07-14 14:28:52 -07:00
ASSERT_OK(uv_ip4_addr("127.0.0.1", server_port, &server_addr));
r = uv_tcp_init(loop, &tcp);
2011-07-14 14:28:52 -07:00
tcp.data = &connect_req;
ASSERT(!r);
r = uv_tcp_connect(&connect_req,
&tcp,
(const struct sockaddr*) &server_addr,
on_connect);
2011-07-14 14:28:52 -07:00
ASSERT(!r);
2011-09-04 22:25:40 +02:00
/* Fetch the actual port used by the connecting socket. */
namelen = sizeof sockname;
r = uv_tcp_getsockname(&tcp, &sockname, &namelen);
ASSERT(!r);
ASSERT_EQ(sockname.sa_family, AF_INET);
2011-09-04 22:25:40 +02:00
connect_port = ntohs(((struct sockaddr_in*) &sockname)->sin_port);
ASSERT_GT(connect_port, 0);
2011-07-14 14:28:52 -07:00
}
2011-08-26 00:23:17 +02:00
static void udp_recv(uv_udp_t* handle,
ssize_t nread,
const uv_buf_t* buf,
const struct sockaddr* addr,
2011-08-26 00:23:17 +02:00
unsigned flags) {
struct sockaddr sockname;
int namelen;
int r;
ASSERT_GE(nread, 0);
free(buf->base);
2011-08-26 00:23:17 +02:00
if (nread == 0) {
return;
}
2011-09-04 22:25:40 +02:00
memset(&sockname, -1, sizeof sockname);
2011-08-26 00:23:17 +02:00
namelen = sizeof(sockname);
2011-09-04 22:25:40 +02:00
r = uv_udp_getsockname(&udp, &sockname, &namelen);
ASSERT_OK(r);
2011-09-04 22:25:40 +02:00
check_sockname(&sockname, "0.0.0.0", 0, "udp receiving socket");
deps: upgrade to libuv 1.44.2 Notable changes: - Build regression fixes for various platform updates (https://github.com/libuv/libuv/pull/3428, https://github.com/libuv/libuv/pull/3419, https://github.com/libuv/libuv/pull/3423, https://github.com/libuv/libuv/pull/3413, https://github.com/libuv/libuv/pull/3431) - Support for GNU/Hurd (https://github.com/libuv/libuv/pull/3450) - Release tool improvements (https://github.com/libuv/libuv-release-tool/pull/13) - Better performing rw locks on Win32 (https://github.com/libuv/libuv/pull/3383) - Support for posix_spawn API (https://github.com/libuv/libuv/pull/3257) - Fix regression on OpenBSD (https://github.com/libuv/libuv/pull/3506) - Add uv_available_parallelism() (https://github.com/libuv/libuv/pull/3499) - Don't use thread-unsafe strtok() (https://github.com/libuv/libuv/pull/3524) - Fix hang after NOTE_EXIT (https://github.com/libuv/libuv/pull/3521) - Better align order-of-events behavior between platforms (https://github.com/libuv/libuv/pull/3598) - Fix fs event not fired if the watched file is moved/removed/recreated (https://github.com/libuv/libuv/pull/3540) - Fix pipe resource leak if closed during connect (and other bugs) (https://github.com/libuv/libuv/pull/3611) - Don't error when killing a zombie process (https://github.com/libuv/libuv/pull/3625) - Avoid posix_spawnp() cwd bug (https://github.com/libuv/libuv/pull/3597) - Skip EVFILT_PROC events when invalidating events for an fd (https://github.com/libuv/libuv/pull/3629) Fixes: https://github.com/nodejs/node/issues/42290 PR-URL: https://github.com/nodejs/node/pull/42340 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-03-15 09:34:01 +01:00
getsocknamecount_udp++;
2011-08-26 00:23:17 +02:00
uv_close((uv_handle_t*) &udp, NULL);
uv_close((uv_handle_t*) handle, NULL);
2011-08-26 00:23:17 +02:00
}
static void udp_send(uv_udp_send_t* req, int status) {
}
2013-01-11 13:49:45 +01:00
static int udp_listener(void) {
struct sockaddr_in addr;
2011-08-26 00:23:17 +02:00
struct sockaddr sockname;
2011-09-04 22:25:40 +02:00
int namelen;
2011-08-26 00:23:17 +02:00
int r;
ASSERT_OK(uv_ip4_addr("0.0.0.0", server_port, &addr));
r = uv_udp_init(loop, &udpServer);
2011-08-26 00:23:17 +02:00
if (r) {
fprintf(stderr, "Socket creation error\n");
return 1;
}
r = uv_udp_bind(&udpServer, (const struct sockaddr*) &addr, 0);
2011-08-26 00:23:17 +02:00
if (r) {
fprintf(stderr, "Bind error\n");
return 1;
}
memset(&sockname, -1, sizeof sockname);
2011-09-04 22:25:40 +02:00
namelen = sizeof sockname;
r = uv_udp_getsockname(&udpServer, &sockname, &namelen);
ASSERT_OK(r);
2011-09-04 22:25:40 +02:00
check_sockname(&sockname, "0.0.0.0", server_port, "udp listener socket");
deps: upgrade to libuv 1.44.2 Notable changes: - Build regression fixes for various platform updates (https://github.com/libuv/libuv/pull/3428, https://github.com/libuv/libuv/pull/3419, https://github.com/libuv/libuv/pull/3423, https://github.com/libuv/libuv/pull/3413, https://github.com/libuv/libuv/pull/3431) - Support for GNU/Hurd (https://github.com/libuv/libuv/pull/3450) - Release tool improvements (https://github.com/libuv/libuv-release-tool/pull/13) - Better performing rw locks on Win32 (https://github.com/libuv/libuv/pull/3383) - Support for posix_spawn API (https://github.com/libuv/libuv/pull/3257) - Fix regression on OpenBSD (https://github.com/libuv/libuv/pull/3506) - Add uv_available_parallelism() (https://github.com/libuv/libuv/pull/3499) - Don't use thread-unsafe strtok() (https://github.com/libuv/libuv/pull/3524) - Fix hang after NOTE_EXIT (https://github.com/libuv/libuv/pull/3521) - Better align order-of-events behavior between platforms (https://github.com/libuv/libuv/pull/3598) - Fix fs event not fired if the watched file is moved/removed/recreated (https://github.com/libuv/libuv/pull/3540) - Fix pipe resource leak if closed during connect (and other bugs) (https://github.com/libuv/libuv/pull/3611) - Don't error when killing a zombie process (https://github.com/libuv/libuv/pull/3625) - Avoid posix_spawnp() cwd bug (https://github.com/libuv/libuv/pull/3597) - Skip EVFILT_PROC events when invalidating events for an fd (https://github.com/libuv/libuv/pull/3629) Fixes: https://github.com/nodejs/node/issues/42290 PR-URL: https://github.com/nodejs/node/pull/42340 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2022-03-15 09:34:01 +01:00
getsocknamecount_udp++;
2011-08-26 00:23:17 +02:00
r = uv_udp_recv_start(&udpServer, alloc, udp_recv);
ASSERT_OK(r);
2011-08-26 00:23:17 +02:00
return 0;
}
static void udp_sender(void) {
struct sockaddr_in server_addr;
uv_buf_t buf;
int r;
r = uv_udp_init(loop, &udp);
2011-08-26 00:23:17 +02:00
ASSERT(!r);
buf = uv_buf_init("PING", 4);
ASSERT_OK(uv_ip4_addr("127.0.0.1", server_port, &server_addr));
2011-08-26 00:23:17 +02:00
r = uv_udp_send(&send_req,
&udp,
&buf,
1,
(const struct sockaddr*) &server_addr,
udp_send);
2011-08-26 00:23:17 +02:00
ASSERT(!r);
}
TEST_IMPL(getsockname_tcp) {
loop = uv_default_loop();
2011-07-14 14:28:52 -07:00
2011-09-04 22:25:40 +02:00
if (tcp_listener())
2011-07-14 14:28:52 -07:00
return 1;
tcp_connector();
2013-01-16 23:38:02 +01:00
uv_run(loop, UV_RUN_DEFAULT);
2011-07-14 14:28:52 -07:00
ASSERT_EQ(3, getsocknamecount_tcp);
ASSERT_EQ(3, getpeernamecount);
2011-07-14 14:28:52 -07:00
MAKE_VALGRIND_HAPPY(loop);
2011-07-14 14:28:52 -07:00
return 0;
}
2011-08-26 00:23:17 +02:00
TEST_IMPL(getsockname_udp) {
loop = uv_default_loop();
2011-08-26 00:23:17 +02:00
2011-09-04 22:25:40 +02:00
if (udp_listener())
2011-08-26 00:23:17 +02:00
return 1;
udp_sender();
2013-01-16 23:38:02 +01:00
uv_run(loop, UV_RUN_DEFAULT);
2011-08-26 00:23:17 +02:00
ASSERT_EQ(2, getsocknamecount_udp);
2011-08-26 00:23:17 +02:00
ASSERT_OK(udp.send_queue_size);
ASSERT_OK(udpServer.send_queue_size);
2014-08-07 15:03:17 +04:00
MAKE_VALGRIND_HAPPY(loop);
2011-08-26 00:23:17 +02:00
return 0;
}