From: Job Snijders <job@fastly.com>
To: Zack Weinberg <zack@owlfolio.org>
Cc: Andreas Schwab <schwab@suse.de>,
Florian Weimer <fweimer@redhat.com>,
GNU libc development <libc-alpha@sourceware.org>
Subject: Re: [PATCH] resolv: add IPv6 support to inet_net_pton()
Date: Mon, 25 Mar 2024 09:04:52 +0000 [thread overview]
Message-ID: <ZgE-NDHHMLLNv2qe@feather.sobornost.net> (raw)
In-Reply-To: <3f50bd26-9199-4174-9fde-8f890651f48e@app.fastmail.com>
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 <job@fastly.com>
---
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 <job@fastly.com>
+ * Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org>
* Copyright (c) 1996,1999 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
@@ -19,6 +21,7 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <resolv/resolv-internal.h>
#include <assert.h>
#include <ctype.h>
@@ -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);
+}
next prev parent reply other threads:[~2024-03-25 9:04 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-22 17:54 Job Snijders
2022-12-22 18:21 ` Florian Weimer
2022-12-22 18:28 ` copying a string with truncation (was: [PATCH] resolv: add IPv6 support to inet_net_pton()) Alejandro Colomar
2022-12-22 20:25 ` Alejandro Colomar
2022-12-23 6:55 ` Sam James
2022-12-23 7:00 ` Sam James
2022-12-23 11:42 ` Alejandro Colomar
2022-12-23 11:45 ` Alejandro Colomar
2022-12-31 15:11 ` Sam James
2023-01-17 10:56 ` [PATCH] resolv: add IPv6 support to inet_net_pton() Job Snijders
2023-04-19 11:31 ` Job Snijders
2024-03-17 1:23 ` Job Snijders
2024-03-17 3:19 ` Job Snijders
2024-03-17 11:18 ` Florian Weimer
2024-03-18 8:59 ` Job Snijders
2024-03-18 9:23 ` Andreas Schwab
2024-03-18 23:01 ` Job Snijders
2024-03-19 8:20 ` Andreas Schwab
2024-03-19 8:29 ` Job Snijders
2024-03-19 9:50 ` Andreas Schwab
2024-03-22 4:16 ` Job Snijders
2024-03-22 14:24 ` Zack Weinberg
2024-03-25 9:04 ` Job Snijders [this message]
2024-04-14 14:56 ` Job Snijders
2024-04-15 8:15 ` Xi Ruoyao
2024-03-25 8:45 ` Andreas Schwab
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZgE-NDHHMLLNv2qe@feather.sobornost.net \
--to=job@fastly.com \
--cc=fweimer@redhat.com \
--cc=libc-alpha@sourceware.org \
--cc=schwab@suse.de \
--cc=zack@owlfolio.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).