Module Name: othersrc Committed By: lukem Date: Sat Sep 23 02:20:39 UTC 2023
Modified Files: othersrc/libexec/tnftpd/libnetbsd: inet_ntop.c Log Message: sync lib/libc/inet/inet_ntop.c 1.12 Functional changes since upstream 1.3: - 1.10, 1.12: always set errno when returning NULL Retain tnftpd local changes: - 1.3: don't use non-standard u_char u_int - 1.1: only enable IPv6 ifdef INET6. Comment out other unneeded upstream code to minimise differences. To generate a diff of this commit: cvs rdiff -u -r1.4 -r1.5 othersrc/libexec/tnftpd/libnetbsd/inet_ntop.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: othersrc/libexec/tnftpd/libnetbsd/inet_ntop.c diff -u othersrc/libexec/tnftpd/libnetbsd/inet_ntop.c:1.4 othersrc/libexec/tnftpd/libnetbsd/inet_ntop.c:1.5 --- othersrc/libexec/tnftpd/libnetbsd/inet_ntop.c:1.4 Sun Sep 21 16:35:25 2008 +++ othersrc/libexec/tnftpd/libnetbsd/inet_ntop.c Sat Sep 23 02:20:39 2023 @@ -1,5 +1,7 @@ -/* $NetBSD: inet_ntop.c,v 1.4 2008/09/21 16:35:25 lukem Exp $ */ -/* from NetBSD: inet_ntop.c,v 1.3 2006/05/10 21:53:15 mrg Exp */ +/* $NetBSD: inet_ntop.c,v 1.5 2023/09/23 02:20:39 lukem Exp $ */ + +/* from: NetBSD: inet_ntop.c,v 1.12 2018/03/02 06:31:53 lukem Exp */ +/* upstream: lib/libc/inet/inet_ntop.c */ /* * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") @@ -20,7 +22,41 @@ #include "tnftpd.h" -/* +#if 0 +#include <sys/cdefs.h> +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "Id: inet_ntop.c,v 1.5 2005/11/03 22:59:52 marka Exp"; +#else +__RCSID("$NetBSD: inet_ntop.c,v 1.5 2023/09/23 02:20:39 lukem Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "port_before.h" + +#include "namespace.h" +#include <sys/param.h> +#include <sys/types.h> +#include <sys/socket.h> + +#include <netinet/in.h> +#include <arpa/inet.h> +#include <arpa/nameser.h> + +#include <assert.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "port_after.h" + +#ifdef __weak_alias +__weak_alias(inet_ntop,_inet_ntop) +#endif +#endif + +/*% * WARNING: Don't even consider trying to compile this on a system where * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. */ @@ -42,16 +78,19 @@ const char * inet_ntop(int af, const void *src, char *dst, socklen_t size) { + _DIAGASSERT(src != NULL); + _DIAGASSERT(dst != NULL); + switch (af) { case AF_INET: - return (inet_ntop4(src, dst, size)); + return inet_ntop4(src, dst, size); #ifdef INET6 case AF_INET6: - return (inet_ntop6(src, dst, size)); + return inet_ntop6(src, dst, size); #endif /* INET6 */ default: errno = EAFNOSUPPORT; - return (NULL); + return NULL; } /* NOTREACHED */ } @@ -73,14 +112,17 @@ inet_ntop4(const unsigned char *src, cha char tmp[sizeof "255.255.255.255"]; int l; + _DIAGASSERT(src != NULL); + _DIAGASSERT(dst != NULL); + l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u", src[0], src[1], src[2], src[3]); if (l <= 0 || (socklen_t) l >= size) { errno = ENOSPC; - return (NULL); + return NULL; } strlcpy(dst, tmp, size); - return (dst); + return dst; } #ifdef INET6 @@ -107,6 +149,9 @@ inet_ntop6(const unsigned char *src, cha int i; int advance; + _DIAGASSERT(src != NULL); + _DIAGASSERT(dst != NULL); + /* * Preprocess: * Copy the input (bytewise) array into a wordwise array. @@ -116,9 +161,9 @@ inet_ntop6(const unsigned char *src, cha for (i = 0; i < NS_IN6ADDRSZ; i++) words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); best.base = -1; + best.len = 0; cur.base = -1; - best.len = -1; /* XXX gcc */ - cur.len = -1; /* XXX gcc */ + cur.len = 0; for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { if (words[i] == 0) { if (cur.base == -1) @@ -156,7 +201,7 @@ inet_ntop6(const unsigned char *src, cha /* Are we following an initial run of 0x00s or any real hex? */ if (i != 0) { if (tp + 1 >= ep) - return (NULL); + goto out; *tp++ = ':'; } /* Is this address an encapsulated IPv4? */ @@ -164,35 +209,38 @@ inet_ntop6(const unsigned char *src, cha (best.len == 6 || (best.len == 7 && words[7] != 0x0001) || (best.len == 5 && words[5] == 0xffff))) { - if (!inet_ntop4(src+12, tp, (socklen_t)(ep - tp))) - return (NULL); + if (!inet_ntop4(src + 12, tp, (socklen_t)(ep - tp))) + goto out; tp += strlen(tp); break; } advance = snprintf(tp, (size_t)(ep - tp), "%x", words[i]); if (advance <= 0 || advance >= ep - tp) - return (NULL); + goto out; tp += advance; } /* Was it a trailing run of 0x00's? */ if (best.base != -1 && (best.base + best.len) == (NS_IN6ADDRSZ / NS_INT16SZ)) { if (tp + 1 >= ep) - return (NULL); + goto out; *tp++ = ':'; } if (tp + 1 >= ep) - return (NULL); + goto out; *tp++ = '\0'; /* * Check for overflow, copy, and we're done. */ - if ((size_t)(tp - tmp) > size) { - errno = ENOSPC; - return (NULL); - } + if ((size_t)(tp - tmp) > size) + goto out; strlcpy(dst, tmp, size); - return (dst); + return dst; +out: + errno = ENOSPC; + return NULL; } #endif /* INET6 */ + +/*! \file */