From patchwork Wed Apr 10 14:30:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Kolosov X-Patchwork-Id: 1922049 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=K4ixPmUl; 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 4VF4zQ4T3sz1yYB for ; Thu, 11 Apr 2024 00:32:18 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CD76C38460A3 for ; Wed, 10 Apr 2024 14:32:16 +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 4DB6E3858C32 for ; Wed, 10 Apr 2024 14:31:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4DB6E3858C32 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 4DB6E3858C32 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=1712759519; cv=none; b=Yd8dLfhG4MK/TOsoaCtHCIv63Z4Q8jcBdb2wLTk4KrjD4zV+TZ0H0qHhEN0dl+X5SQoXwatC6SlJXiiuVyOaOxrV+MuWv7S5t72PbTz/TDQuOWCtLl3MneuGywADKfDGw/jAbm53RioGeFkuFm/primHvWjidCcwKow3Gw+FpvM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712759519; c=relaxed/simple; bh=NdvK2Hsu6+dskGmQ7q9iwkHK+lkyIDWPE6R+YgRmc7M=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=FiyHBnuaB7L5nM875D+fOXKfbzBYm8Sbs9LHqv1B9IN3DRvfpyV6zETtvXrm8x0H8nNI2otD+8KWgwV0VgkAAiy1+gjpNPzC4T20Kd5+Q+432eF8G7NIBMwLPEpB65oTJG9s/amY5pO7bx8La3n1O48slHnY4O/G9n+go6uy7VA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712759517; 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=kbvW+8qAVvrRp8zFPt5Of2tRul2WfWgJwDv6hz7vrNU=; b=K4ixPmUlmfAxRHaOO1gSJvlxoWVqQbLNFVZTalbI17hbH+lCygnzzqeupUC6cpGB80elJj fdp2MAx+rxqg2Okna+m49yQClxzVuSwMFPYfJGl4Po2UmlzmCxxVHoUxEWS9K3BZWN+iah sT9nbTDM5TbRzRDH5hlPPykoEQR9MwM= 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-508-mhWIcrHSM4SEbsGooOBxWg-1; Wed, 10 Apr 2024 10:31:55 -0400 X-MC-Unique: mhWIcrHSM4SEbsGooOBxWg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 354D780171B for ; Wed, 10 Apr 2024 14:31:55 +0000 (UTC) Received: from sk-carbon-x9.redhat.com (unknown [10.45.224.126]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7A3C21C060D1; Wed, 10 Apr 2024 14:31:54 +0000 (UTC) From: Sergey Kolosov To: libc-alpha@sourceware.org Cc: Arjun Shankar Subject: [PATCH v2 1/2] libsupport: Add xgetpeername Date: Wed, 10 Apr 2024 16:30:46 +0200 Message-ID: <20240410143138.172319-1-skolosov@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.3 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 Wed Apr 10 14:30:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Kolosov X-Patchwork-Id: 1922050 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=JuWe8PHQ; 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 4VF4zV00Hmz1yYB for ; Thu, 11 Apr 2024 00:32:21 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 345CE384B06A for ; Wed, 10 Apr 2024 14:32:20 +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 A5828385840A for ; Wed, 10 Apr 2024 14:31:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A5828385840A 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 A5828385840A 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=1712759521; cv=none; b=ss/zLz8yxPCjy34QqOqydK8UjRUtPct+0VHlXTNZHV0mkWH5nkmpE9OWotUVvFbj/mgJQVvKQtSgNDEMwfP70ewuRHu3HZoF+w5B5FScfe6ptQdgHe1WkfZ1IGjLnTc2BrfdMCAjFiIozGs/4l3o7BsuK81yAosL+RAVis5H2vw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712759521; c=relaxed/simple; bh=UqbzC1wH63J6At/p42j5FvajhFnLEICFqCvbtDYoutA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=etm8jzdx83pf1182PRdcV+592UAz6xq99qh1C6iafVHvzqIt6scb7NhLsENf2jEGKxnnooH7nzko4IGzZIW4yM0pNJtoUuF6iYoVY4eFWv/soex+GPnJ06r/H3OH2I9ryldb8ARoI6X3KZ4KASqyntaXKc2EalH1wQTWm5gSULc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712759519; 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=fdDGHlZvZyDEnXJi53xICtCtR0t6OkvsUf0i/bJgwX4=; b=JuWe8PHQxNOGCeB8071HX4qSOdijsySyfkYO/mD3X20QViCgo1+IzfgmxyFZe5B36P+k5j CJndG7JZ3zlJgEHKTyX7lcLAd7hk0LciwQzYzcWBLnKwytkTdv3I1X0w38EHlOc8psIArp 0RKlQxYNWxnFDviZfLSajVPJ11buUjU= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-135-AIXiCOyxM8yn1PFyadE7Qw-1; Wed, 10 Apr 2024 10:31:57 -0400 X-MC-Unique: AIXiCOyxM8yn1PFyadE7Qw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 A801329AA385 for ; Wed, 10 Apr 2024 14:31:57 +0000 (UTC) Received: from sk-carbon-x9.redhat.com (unknown [10.45.224.126]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F21FF1C060D1; Wed, 10 Apr 2024 14:31:56 +0000 (UTC) From: Sergey Kolosov To: libc-alpha@sourceware.org Cc: Arjun Shankar Subject: [PATCH v2 2/2] socket: Add new test for connect Date: Wed, 10 Apr 2024 16:30:47 +0200 Message-ID: <20240410143138.172319-2-skolosov@redhat.com> In-Reply-To: <20240410143138.172319-1-skolosov@redhat.com> References: <20240410143138.172319-1-skolosov@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.5 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 simple bind/accept/connect test for an IPv4 TCP connection to a local process via the loopback interface. --- Changes from v1: 1. open_socket function changed to open_socket_inet_tcp one without arguments 2. removed unneeded printf 3. added comment about a second connect --- socket/Makefile | 1 + socket/tst-connect.c | 112 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 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..50334a08df --- /dev/null +++ b/socket/tst-connect.c @@ -0,0 +1,112 @@ +/* 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_inet_tcp (void) +{ + int fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (fd < 0) + { + if (errno == EAFNOSUPPORT) + FAIL_UNSUPPORTED ("The host does not support IPv4"); + else + FAIL_EXIT1 ("socket (AF_INET, SOCK_STREAM, IPPROTO_TCP): %m\n"); + } + 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_inet_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); + 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_inet_tcp (); + xconnect (client_sock, (const struct sockaddr *) &server_address, + sizeof (server_address)); + + /* A second connect with same arguments should fail with EISCONN. */ + 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