From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by sourceware.org (Postfix) with ESMTPS id 87E683858D35 for ; Sun, 14 Apr 2024 14:56:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 87E683858D35 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=fastly.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=fastly.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 87E683858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::634 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713106579; cv=none; b=HZcTn10UpDD8EtFrvcjeQ8TccicR/kU/IFycU9dB44bcJqMMrIj0oil5nqhvpvnSmEiJJmq2rg0zn9mdNiUD7POkTLzms/HKQcHJy123IY/9ADB3+aV44RlvtedUYuq/3GYYHlpQEYeuBPjPCIxo5gbmFvtLW9mdyEYD2qoPqZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713106579; c=relaxed/simple; bh=RneRF9xkmj4md7AJlMVgHxldgenrIj5lZMvZb8n3Azc=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=jhnxdPLi9YK6atMcva1edm7TwuMK4foqyYda/gydJv7EtuXT7+catKQ2S73A5wVEdMAG1EU67rvCpAMsfyL+9xqSslFrUTbVYmVkzup9iuwIRDOOisrhL0OC6Vb18cnV23UnPU9if1zve098GPsvY9JQmf97lxWAmWGRxfnYlT4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x634.google.com with SMTP id a640c23a62f3a-a523dad53e0so264783166b.1 for ; Sun, 14 Apr 2024 07:56:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastly.com; s=google; t=1713106575; x=1713711375; darn=sourceware.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=AhrBHTlt43ftVPeP2WNOEymp10be6QMAl958EBM+T14=; b=JTg3+k5gkSAUr+V5BnRt2IjcM7Ii5i6pWxpidfZG9ch+taOBc9vnejQAhOiTn061hN ULAhS97T9/h3tAzU6M1lT8l5/El/22BSWXJi6LD0TQBxWpemMjfOUQ3JKrr18EOP8eh9 mEU8xinVVgLVqsBiqvXoQck7pZtSem2MOyr48= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713106575; x=1713711375; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=AhrBHTlt43ftVPeP2WNOEymp10be6QMAl958EBM+T14=; b=R5vPfEtKcxT5z++d8e9VzG5xbWmNyF0fVYcOOHCH6NXvvwndL3tk4EBkzLDADuXPlT /G3qmQgFTyjZI1PRl4qpZ6D/cpMY4U5Uxpz01xJWhrg6beWnogFJtFDTrTgtvOIuFm/E nV4YR8Qdyvtbl0a1k5JfAUyJI3YPc5pKbs/M3uo7Bq9QLoM7wV3sXyOQISCCQlt6Xjmh e3+2pAZi1G8uQTZmxncJjwc0wNvgdA4aLQxBh5t19TcBKm7SC0N4O2I2p0kslDr9E7/7 3Mb0K5Rkf77EqFMOzKUc9A5rhgp3sEabeCQYSeIA7TchQDf5msnF7TYRh3f5GuoXNyjd Fn4A== X-Forwarded-Encrypted: i=1; AJvYcCWyoDxR7L9VV4rXRSuoWpBuuhvJzIrsVbJGCeak6eM9c98O/swlDTo0qrRT8pA3ZGT2H6CG02hwmMyVvpHov1azncXgUcQtP1BK X-Gm-Message-State: AOJu0Yz8FMO61W0sn5az2HPdB27xMx5MNbQnnDbT88ps/TubDxwATgCB pv9+vMhoWXuyOvm2esClCNx66aPoUXKHxXg7z5MNq5A+hyJjgfK/QGN8EgU4+jY= X-Google-Smtp-Source: AGHT+IEuvT2fM2tBGm3ohbVjUr2V5Ya0HMA523l/AeV2oo19x4+M0Y8hj5ovXtBsKc3VuRF6i+VtqA== X-Received: by 2002:a17:906:840b:b0:a52:2e4f:62f5 with SMTP id n11-20020a170906840b00b00a522e4f62f5mr5974965ejx.33.1713106575078; Sun, 14 Apr 2024 07:56:15 -0700 (PDT) Received: from snel ([2a10:3781:276:3:16f6:d8ff:fe47:2eb7]) by smtp.gmail.com with ESMTPSA id r16-20020a170906281000b00a5267ee40efsm434765ejc.18.2024.04.14.07.56.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Apr 2024 07:56:14 -0700 (PDT) Date: Sun, 14 Apr 2024 16:56:12 +0200 From: Job Snijders To: Zack Weinberg Cc: Andreas Schwab , Florian Weimer , GNU libc development Subject: Re: [PATCH] resolv: add IPv6 support to inet_net_pton() Message-ID: References: <87zfuxcdjd.fsf@oldenburg.str.redhat.com> <3f50bd26-9199-4174-9fde-8f890651f48e@app.fastmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Clacks-Overhead: GNU Terry Pratchett X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPAM_FROM,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 List-Id: Dear all, Are there any other comments, or can this be considered for merging? :-) Kind regards, Job On Mon, Mar 25, 2024 at 09:04:52AM +0000, Job Snijders wrote: > Dear Zack, > > On Fri, Mar 22, 2024 at 10:24:35AM -0400, Zack Weinberg wrote: > > Please change that to "ep == sep", even if you make no other changes. > > > > Anyway, I would have written this as > > > > lbits = strtol(sep, &ep, 10); > > if (ep == sep || *ep != '\0') { > > __set_errno(ENOENT); > > return -1; > > } > > if (lbits < 0 || lbits > 128) { > > __set_errno(EMSGSIZE); > > return -1; > > } > > > > and I do think this is an improvement over what you had, largely > > because it eliminates the need to save and restore the original value > > of errno. > > Thank you for your response, I've adjusted the proposal according to > your notes. > > Kind regards, > > Job > > Signed-off: Job Snijders > --- > resolv/inet_net_pton.c | 69 +++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 65 insertions(+), 4 deletions(-) > > diff --git a/resolv/inet_net_pton.c b/resolv/inet_net_pton.c > index 63a47b7394..93753cb37f 100644 > --- a/resolv/inet_net_pton.c > +++ b/resolv/inet_net_pton.c > @@ -1,4 +1,6 @@ > /* > + * Copyright (c) 2024 Job Snijders > + * Copyright (c) 2012 by Gilles Chehade > * Copyright (c) 1996,1999 by Internet Software Consortium. > * > * Permission to use, copy, modify, and distribute this software for any > @@ -19,6 +21,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -35,13 +38,16 @@ > > static int inet_net_pton_ipv4 (const char *src, u_char *dst, > size_t size) __THROW; > +static int inet_net_pton_ipv6 (const char *src, u_char *dst, > + size_t size) __THROW; > > /* > - * static int > + * int > * inet_net_pton(af, src, dst, size) > - * convert network number from presentation to network format. > - * accepts hex octets, hex strings, decimal octets, and /CIDR. > - * "size" is in bytes and describes "dst". > + * Convert network number from presentation format to network format. > + * If "af" is set to AF_INET, accept various formats like hex octets, > + * hex strings, or decimal octets. If "af" is set to AF_INET6, accept > + * IPv6 addresses. "size" is in bytes and describes "dst". > * return: > * number of bits, either imputed classfully or specified with /CIDR, > * or -1 if some failure occurred (check errno). ENOENT means it was > @@ -55,6 +61,8 @@ inet_net_pton (int af, const char *src, void *dst, size_t size) > switch (af) { > case AF_INET: > return (inet_net_pton_ipv4(src, dst, size)); > + case AF_INET6: > + return (inet_net_pton_ipv6(src, dst, size)); > default: > __set_errno (EAFNOSUPPORT); > return (-1); > @@ -196,3 +204,56 @@ inet_net_pton_ipv4 (const char *src, u_char *dst, size_t size) > __set_errno (EMSGSIZE); > return (-1); > } > + > + > +/* > + * Convert an IPv6 prefix from presentation format to network format. > + * Return the number of bits specified, or -1 as error (check errno). > + */ > +static int > +inet_net_pton_ipv6 (const char *src, u_char *dst, size_t size) > +{ > + struct in6_addr in6; > + int bits; > + long lbits; > + size_t bytes; > + char *ep, *sep; > + > + sep = strchr(src, '/'); > + > + if (__inet_pton_length(AF_INET6, src, sep ? sep - src : strlen(src), > + &in6) != 1) { > + __set_errno (ENOENT); > + return (-1); > + } > + > + if (sep == NULL) { > + bits = 128; > + goto out; > + } > + > + if (sep[0] == '\0' || !isascii(sep[0]) || !isdigit(sep[0])) { > + __set_errno (ENOENT); > + return (-1); > + } > + > + lbits = strtol(sep, &ep, 10); > + if (ep == sep || *ep != '\0') { > + __set_errno (ENOENT); > + return (-1); > + } > + if (lbits < 0 || lbits > 128) { > + __set_errno (EMSGSIZE); > + return (-1); > + } > + bits = lbits; > + > + out: > + bytes = (bits + 7) / 8; > + if (bytes > size) { > + __set_errno (EMSGSIZE); > + return (-1); > + } > + memcpy(dst, &in6.s6_addr, bytes); > + return (bits); > +}