From patchwork Mon Apr 8 10:25:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Kolosov X-Patchwork-Id: 1920765 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=D08e7vnW; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VClcr6y9Dz1yYf for ; Mon, 8 Apr 2024 20:26:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0EBC43858C66 for ; Mon, 8 Apr 2024 10:26:35 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 02C143858D32 for ; Mon, 8 Apr 2024 10:26:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 02C143858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 02C143858D32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712571975; cv=none; b=rrRXbiZUA0/SBZxHEGHjcyLVqbmNE1J+5MCSRW8tgB0EVWSVkTehPpX0TDq/WSAbp1QPBe8XmjZbxvJYPbNBFgjKq3f2sQgFo9rpiIaOK9Ak1sBtVLWswoxBlYT+TtkZYwt9AkTucTfa5Yk3/fAVksmk3Jon6HfhilRXHHPyWoE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712571975; c=relaxed/simple; bh=aMRzstUqrLOTeJTGELL6gCmSWLOW2ckugn2y3AvmbgM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=etCUTcbJSQXGVkzc/Ba/gWbUWgYWTxBSRA21WoIjmffOi/CXG/oE380msmlfLIhKJZZo3eL7GXovzVtS/isa/3ssN2UmHRTnMIdTIxwzliNsx9nK+j6lqRW4AOL0xY9C9WPRj7tDVkgEfUnUDQ+eAm7aiFC5sRAjO6LfYKYUgDM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712571973; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=hODdPK2JXBQtwXpaFNSkS5PZNB6AdqqOMkzLqv5Tr6M=; b=D08e7vnW08Et5cFd5L/4zdhvbaFvFKJQBkCCiREmKuDlWQyCJSHOSzeP/eK+pH0Ig4i4jL ttnYKvbCtmUDED3cnsJPQAlFdjvdxh+HhfOO+AnVGHf9TOTv0V8KXE5w2I/ZnmXn5YfxAU L/KbRyKeWZUoFw3lPv36/pD8L2nlkfI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-88-lXk9KPkeNBG7xyeIAFAziw-1; Mon, 08 Apr 2024 06:26:12 -0400 X-MC-Unique: lXk9KPkeNBG7xyeIAFAziw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1816A889F44 for ; Mon, 8 Apr 2024 10:26:12 +0000 (UTC) Received: from sk-carbon-x9.redhat.com (unknown [10.45.225.71]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 98DEC40AE784; Mon, 8 Apr 2024 10:26:11 +0000 (UTC) From: Sergey Kolosov To: libc-alpha@sourceware.org Cc: Sergey Kolosov Subject: [PATCH 1/2] libsupport: Add xgetpeername Date: Mon, 8 Apr 2024 12:25:39 +0200 Message-ID: <20240408102543.18143-1-skolosov@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org The patch adds redirections for getpeername. Reviewed-by: Arjun Shankar --- support/Makefile | 1 + support/xgetpeername.c | 30 ++++++++++++++++++++++++++++++ support/xsocket.h | 1 + 3 files changed, 32 insertions(+) create mode 100644 support/xgetpeername.c diff --git a/support/Makefile b/support/Makefile index 362a51f882..aa57207bdc 100644 --- a/support/Makefile +++ b/support/Makefile @@ -131,6 +131,7 @@ libsupport-routines = \ xfreopen \ xftruncate \ xgetline \ + xgetpeername \ xgetsockname \ xlisten \ xlseek \ diff --git a/support/xgetpeername.c b/support/xgetpeername.c new file mode 100644 index 0000000000..6f448e456a --- /dev/null +++ b/support/xgetpeername.c @@ -0,0 +1,30 @@ +/* getpeername with error checking. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include +#include +#include + +void +xgetpeername (int fd, struct sockaddr *sa, socklen_t *plen) +{ + if (getpeername (fd, sa, plen) != 0) + FAIL_EXIT1 ("getpeername (%d): %m", fd); +} diff --git a/support/xsocket.h b/support/xsocket.h index 3e44103546..4ac0e1f5ff 100644 --- a/support/xsocket.h +++ b/support/xsocket.h @@ -26,6 +26,7 @@ int xsocket (int, int, int); void xsetsockopt (int, int, int, const void *, socklen_t); void xgetsockname (int, struct sockaddr *, socklen_t *); +void xgetpeername (int, struct sockaddr *, socklen_t *); void xconnect (int, const struct sockaddr *, socklen_t); void xbind (int, const struct sockaddr *, socklen_t); void xlisten (int, int); From patchwork Mon Apr 8 10:25:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Kolosov X-Patchwork-Id: 1920771 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=e+E0YqRh; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VCldl5Rq5z1yYf for ; Mon, 8 Apr 2024 20:27:23 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 103C43858408 for ; Mon, 8 Apr 2024 10:27:22 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id D14143858D39 for ; Mon, 8 Apr 2024 10:26:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D14143858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D14143858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712571979; cv=none; b=I1jUKyHOKUjun7XUb1HO+z5GKAi2s1ZHl11AA0o9SUAWfyDWbCiO64vk/PxgiZkYgltMtDIX4nWxANturfNxqMnY+icOs95IWVv+3hzJ2npqgCofZlkPJr7H1YFSl3LPoBkjVIlpHMmglhmJoU07M3cW9VcVgWGYzMgM++iGBFo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712571979; c=relaxed/simple; bh=P8+DA8wvVWuYhD46+V0rxE0vWvG2Iyv/RaUIrRmzMBI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=psdcl4xdJvJg3xSVJu1FrzJnMze7PRoCy0oxSGPsHp+vNUHgkEo1omfqzauvtNpNL3oW5nWjrDI8eoJJgX7IDERVfOnVozZa0Igna/bX9/n9IEXoKJbLf4sQ3Z5R7636/4Jc3K6cGuJefE5EdZsGiZDi+GaYz/LjqjeHFAZflf8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712571976; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RKzxV+syTaCTCTKvknEedvsb3uMGAcxWIOSZ4PR5DUI=; b=e+E0YqRhUsyNpG17bvKw5EIat+l7+0k1wycd3ULXJbdTACbUijZpWUC7nwVbjZGHQay1RY y4R52Cmvv0XLCNjzjRLNI1gBX/FEbESSwA80kee0611byt3G3GJtxTdmNbAo/Vsg0wqZmj 0zqLJ2LWCkwIHjBCv1uTd7MtwCNqF5g= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-343-YWOtFfoyPpiyGo0Vv6MNpQ-1; Mon, 08 Apr 2024 06:26:15 -0400 X-MC-Unique: YWOtFfoyPpiyGo0Vv6MNpQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E5C74881E60 for ; Mon, 8 Apr 2024 10:26:14 +0000 (UTC) Received: from sk-carbon-x9.redhat.com (unknown [10.45.225.71]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 71B8B40AE782; Mon, 8 Apr 2024 10:26:14 +0000 (UTC) From: Sergey Kolosov To: libc-alpha@sourceware.org Cc: Sergey Kolosov Subject: [PATCH 2/2] socket: Add new test for connect Date: Mon, 8 Apr 2024 12:25:40 +0200 Message-ID: <20240408102543.18143-2-skolosov@redhat.com> In-Reply-To: <20240408102543.18143-1-skolosov@redhat.com> References: <20240408102543.18143-1-skolosov@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org This commit adds a new test for the connect function. --- socket/Makefile | 1 + socket/tst-connect.c | 109 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 socket/tst-connect.c diff --git a/socket/Makefile b/socket/Makefile index 74ca5b8452..fc1bd0a260 100644 --- a/socket/Makefile +++ b/socket/Makefile @@ -70,6 +70,7 @@ tests := \ tst-accept4 \ tst-cmsg_cloexec \ tst-cmsghdr \ + tst-connect \ tst-sockopt \ # tests diff --git a/socket/tst-connect.c b/socket/tst-connect.c new file mode 100644 index 0000000000..44999d2953 --- /dev/null +++ b/socket/tst-connect.c @@ -0,0 +1,109 @@ +/* Test the connect function. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static struct sockaddr_in server_address; + +int +open_socket (int domain, int type, int protocol) +{ + int fd = socket (domain, type, protocol); + if (fd < 0) + { + if (errno == EAFNOSUPPORT) + FAIL_UNSUPPORTED ("The host does not support IPv4"); + else + FAIL_EXIT1 ("socket (%d, %d, %d): %m\n", domain, type, protocol); + } + return fd; +} + +static pid_t +start_server (void) +{ + + server_address.sin_family = AF_INET; + server_address.sin_port = 0; + server_address.sin_addr.s_addr = htonl (INADDR_LOOPBACK); + + int server_sock = open_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); + + xbind (server_sock, (struct sockaddr *) &server_address, + sizeof (server_address)); + + socklen_t sa_len = sizeof (server_address); + xgetsockname (server_sock, (struct sockaddr *) &server_address, &sa_len); + printf ("Server listen on port %d\n",ntohs (server_address.sin_port)); + xlisten (server_sock, 5); + + pid_t my_pid = xfork (); + if (my_pid > 0) + { + xclose (server_sock); + return my_pid; + } + + struct sockaddr_in client_address; + socklen_t ca_len = sizeof (server_address); + int client_sock = xaccept (server_sock, + (struct sockaddr *) &client_address, &ca_len); + printf ("socket accepted %d\n",client_sock); + + _exit (0); +} + +static int +do_test (void) +{ + pid_t serv_pid; + struct sockaddr_in peer; + socklen_t peer_len; + + serv_pid = start_server (); + int client_sock = open_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); + xconnect (client_sock, (const struct sockaddr *) &server_address, + sizeof (server_address)); + int result = connect (client_sock, (const struct sockaddr *) &server_address, + sizeof (server_address)); + if (result == 0 || errno != EISCONN) + FAIL_EXIT1 ("Second connect (%d), should fail with EISCONN: %m", + client_sock); + + peer_len = sizeof (peer); + xgetpeername (client_sock, (struct sockaddr *) &peer, &peer_len); + TEST_COMPARE (peer_len, sizeof (peer)); + TEST_COMPARE (peer.sin_port, server_address.sin_port); + TEST_COMPARE_BLOB (&peer.sin_addr, sizeof (peer.sin_addr), + &server_address.sin_addr, sizeof (server_address.sin_addr)); + + int status; + xwaitpid (serv_pid, &status, 0); + TEST_COMPARE (status, 0); + return 0; +} +#include