Nuke ascii2addr() and addr2ascii(). They have no consumers anywhere

in FreeBSD, and originated from INRIA IPv6.

Stub out netstat reference to addr2ascii() I mistakenly introduced.
Update misleading man page sections.

Merge NetBSD's getnameinfo() AF_LINK extensions for a portable way to
print link-layer addresses given a sockaddr_dl(), minus the IEEE 1394
bits which don't map directly to our code.

Obtained from:	NetBSD (getnameinfo.c)
Discussed on:	current (March 2006)
This commit is contained in:
bms 2007-02-28 21:18:38 +00:00
parent 2010e1d527
commit 0738a9d791
9 changed files with 125 additions and 441 deletions

View File

@ -4,7 +4,7 @@
# machine-independent net sources
.PATH: ${.CURDIR}/net
SRCS+= addr2ascii.c ascii2addr.c base64.c ether_addr.c eui64.c \
SRCS+= base64.c ether_addr.c eui64.c \
gai_strerror.c getaddrinfo.c \
gethostbydns.c gethostbyht.c gethostbynis.c gethostnamadr.c \
getifaddrs.c getifmaddrs.c getnameinfo.c \
@ -43,7 +43,7 @@ nslexer.c: nslexer.l nsparser.h
${LEX} ${LFLAGS} -o/dev/stdout ${.IMPSRC} | \
sed -e '/YY_BUF_SIZE/s/16384/1024/' >${.TARGET}
MAN+= addr2ascii.3 byteorder.3 ethers.3 eui64.3 \
MAN+= byteorder.3 ethers.3 eui64.3 \
getaddrinfo.3 gai_strerror.3 gethostbyname.3 \
getifaddrs.3 getifmaddrs.3 getipnodebyname.3 \
getnameinfo.3 getnetent.3 getprotoent.3 getservent.3 \
@ -56,7 +56,6 @@ MAN+= addr2ascii.3 byteorder.3 ethers.3 eui64.3 \
sctp_getaddrlen.3 sctp_getassocid.3 sctp_getpaddrs.3 \
sctp_opt_info.3 sctp_recvmsg.3 sctp_send.3 sctp_sendmsg.3 \
MLINKS+=addr2ascii.3 ascii2addr.3
MLINKS+=byteorder.3 htonl.3 byteorder.3 htons.3 byteorder.3 ntohl.3 \
byteorder.3 ntohs.3
MLINKS+=ethers.3 ether_aton.3 ethers.3 ether_hostton.3 ethers.3 ether_line.3 \

View File

@ -1,8 +1,6 @@
# $FreeBSD$
FBSD_1.0 {
addr2ascii;
ascii2addr;
__b64_ntop;
__b64_pton;
ether_line;

View File

@ -1,235 +0,0 @@
.\"
.\" Copyright 1996 Massachusetts Institute of Technology
.\"
.\" Permission to use, copy, modify, and distribute this software and
.\" its documentation for any purpose and without fee is hereby
.\" granted, provided that both the above copyright notice and this
.\" permission notice appear in all copies, that both the above
.\" copyright notice and this permission notice appear in all
.\" supporting documentation, and that the name of M.I.T. not be used
.\" in advertising or publicity pertaining to distribution of the
.\" software without specific, written prior permission. M.I.T. makes
.\" no representations about the suitability of this software for any
.\" purpose. It is provided "as is" without express or implied
.\" warranty.
.\"
.\" THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
.\" ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
.\" SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
.\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $ANA: addr2ascii.3,v 1.1 1996/06/13 18:41:46 wollman Exp $
.\" $FreeBSD$
.\"
.Dd June 13, 1996
.Dt ADDR2ASCII 3
.Os
.Sh NAME
.Nm addr2ascii ,
.Nm ascii2addr
.Nd Generic address formatting routines
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In arpa/inet.h
.Ft "char *"
.Fn addr2ascii "int af" "const void *addrp" "int len" "char *buf"
.Ft int
.Fn ascii2addr "int af" "const char *ascii" "void *result"
.Sh DESCRIPTION
The routines
.Fn addr2ascii
and
.Fn ascii2addr
are used to convert network addresses between binary form and a
printable form appropriate to the address family.
Both functions take
an
.Fa af
argument, specifying the address family to be used in the conversion
process.
(Currently, only the
.Dv AF_INET
and
.Dv AF_LINK
address families are supported.)
.Pp
The
.Fn addr2ascii
function
is used to convert binary, network-format addresses into printable
form.
In addition to
.Fa af ,
there are three other arguments.
The
.Fa addrp
argument is a pointer to the network address to be converted.
The
.Fa len
argument is the length of the address.
The
.Fa buf
argument is an optional pointer to a caller-allocated buffer to hold
the result; if a null pointer is passed,
.Fn addr2ascii
uses a statically-allocated buffer.
.Pp
The
.Fn ascii2addr
function performs the inverse operation to
.Fn addr2ascii .
In addition to
.Fa af ,
it takes two arguments,
.Fa ascii
and
.Fa result .
The
.Fa ascii
argument is a pointer to the string which is to be converted into
binary.
The
.Fa result
argument is a pointer to an appropriate network address structure for
the specified family.
.Pp
The following gives the appropriate structure to use for binary
addresses in the specified family:
.Pp
.Bl -tag -width AF_INETxxxx -compact
.It Dv AF_INET
.Vt "struct in_addr"
(in
.In arpa/inet.h )
.It Dv AF_LINK
.Vt "struct sockaddr_dl"
(in
.In net/if_dl.h )
.\" .It Dv AF_INET6
.\" .Vt "struct in6_addr"
.\" (in
.\" .In netinet6/in6.h )
.El
.Pp
.Dv AF_INET
and
.Dv AF_LINK
constants are defined in
.In sys/socket.h .
.Sh RETURN VALUES
The
.Fn addr2ascii
function returns the address of the buffer it was passed, or a static
buffer if the a null pointer was passed; on failure, it returns a null
pointer.
The
.Fn ascii2addr
function returns the length of the binary address in bytes, or -1 on
failure.
.Sh EXAMPLES
The
.Xr inet 3
functions
.Fn inet_ntoa
and
.Fn inet_aton
could be implemented thusly:
.Bd -literal -offset indent
#include <sys/socket.h>
#include <arpa/inet.h>
char *
inet_ntoa(struct in_addr addr)
{
return addr2ascii(AF_INET, &addr, sizeof addr, 0);
}
int
inet_aton(const char *ascii, struct in_addr *addr)
{
return (ascii2addr(AF_INET, ascii, addr)
== sizeof(*addr));
}
.Ed
.Pp
In actuality, this cannot be done because
.Fn addr2ascii
and
.Fn ascii2addr
are implemented in terms of the
.Xr inet 3
functions, rather than the other way around.
.Sh ERRORS
When a failure is returned,
.Va errno
is set to one of the following values:
.Bl -tag -width Er
.It Bq Er ENAMETOOLONG
The
.Fn addr2ascii
routine was passed a
.Fa len
argument which was inappropriate for the address family given by
.Fa af .
.It Bq Er EPROTONOSUPPORT
Either routine was passed an
.Fa af
argument other than
.Dv AF_INET
or
.Dv AF_LINK .
.It Bq Er EINVAL
The string passed to
.Fn ascii2addr
was improperly formatted for address family
.Fa af .
.El
.Sh SEE ALSO
.Xr inet 3 ,
.Xr linkaddr 3 ,
.Xr inet 4
.Sh HISTORY
An interface close to this one was originally suggested by Craig
Partridge.
This particular interface originally appeared in the
.Tn INRIA
.Tn IPv6
implementation.
.Sh AUTHORS
Code and documentation by
.An Garrett A. Wollman ,
MIT Laboratory for Computer Science.
.Sh BUGS
The original implementations supported IPv6.
This support should
eventually be resurrected.
The
.Tn NRL
implementation also included support for the
.Dv AF_ISO
and
.Dv AF_NS
address families.
.Pp
The genericity of this interface is somewhat questionable.
A truly
generic interface would provide a means for determining the length of
the buffer to be used so that it could be dynamically allocated, and
would always require a
.Vt "struct sockaddr"
to hold the binary address.
Unfortunately, this is incompatible with existing
practice.
This limitation means that a routine for printing network
addresses from arbitrary address families must still have internal
knowledge of the maximum buffer length needed and the appropriate part
of the address to use as the binary address.

View File

@ -1,95 +0,0 @@
/*
* Copyright 1996 Massachusetts Institute of Technology
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that both the above copyright notice and this
* permission notice appear in all copies, that both the above
* copyright notice and this permission notice appear in all
* supporting documentation, and that the name of M.I.T. not be used
* in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission. M.I.T. makes
* no representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied
* warranty.
*
* THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
* ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
* SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $ANA: addr2ascii.c,v 1.1 1996/06/13 18:41:46 wollman Exp $
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*-
* Convert a network address from binary to printable numeric format.
* This API is copied from INRIA's IPv6 implementation, but it is a
* bit bogus in two ways:
*
* 1) There is no value in passing both an address family and
* an address length; either one should imply the other,
* or we should be passing sockaddrs instead.
* 2) There should by contrast be /added/ a length for the buffer
* that we pass in, so that programmers are spared the need to
* manually calculate (read: ``guess'') the maximum length.
*
* Flash: the API is also the same in the NRL implementation, and seems to
* be some sort of standard, so we appear to be stuck with both the bad
* naming and the poor choice of arguments.
*/
char *
addr2ascii(af, addrp, len, buf)
int af;
const void *addrp;
int len; /* should be size_t XXX */
char *buf; /* XXX should pass length of buffer */
{
static char staticbuf[64]; /* 64 for AF_LINK > 16 for AF_INET */
if (!buf)
buf = staticbuf;
switch(af) {
case AF_INET:
if (len != sizeof(struct in_addr)) {
errno = ENAMETOOLONG;
return 0;
}
strcpy(buf, inet_ntoa(*(const struct in_addr *)addrp));
break;
case AF_LINK:
if (len != sizeof(struct sockaddr_dl)) {
errno = ENAMETOOLONG;
return 0;
}
strcpy(buf, link_ntoa((const struct sockaddr_dl *)addrp));
break;
default:
errno = EPROTONOSUPPORT;
return 0;
}
return buf;
}

View File

@ -1,75 +0,0 @@
/*
* Copyright 1996 Massachusetts Institute of Technology
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that both the above copyright notice and this
* permission notice appear in all copies, that both the above
* copyright notice and this permission notice appear in all
* supporting documentation, and that the name of M.I.T. not be used
* in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission. M.I.T. makes
* no representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied
* warranty.
*
* THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
* ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
* SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $ANA: ascii2addr.c,v 1.2 1996/06/13 18:46:02 wollman Exp $
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int
ascii2addr(af, ascii, result)
int af;
const char *ascii;
void *result;
{
struct in_addr *ina;
char strbuf[4*sizeof("123")]; /* long enough for V4 only */
switch(af) {
case AF_INET:
ina = result;
strbuf[0] = '\0';
strncat(strbuf, ascii, (sizeof strbuf)-1);
if (inet_aton(strbuf, ina))
return sizeof(struct in_addr);
errno = EINVAL;
break;
case AF_LINK:
link_addr(ascii, result);
/* oops... no way to detect failure */
return sizeof(struct sockaddr_dl);
default:
errno = EPROTONOSUPPORT;
break;
}
return -1;
}

View File

@ -2,6 +2,7 @@
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* Copyright (c) 2000 Ben Harris.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -49,6 +50,8 @@ __FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
@ -58,6 +61,38 @@ __FBSDID("$FreeBSD$");
#include <stddef.h>
#include <errno.h>
static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *,
size_t, char *, size_t, int);
#ifdef INET6
static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
size_t, int);
static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
#endif
static int getnameinfo_link(const struct sockaddr *, socklen_t, char *,
size_t, char *, size_t, int);
static int hexname(const u_int8_t *, size_t, char *, size_t);
int
getnameinfo(const struct sockaddr *sa, socklen_t salen,
char *host, size_t hostlen, char *serv, size_t servlen,
int flags)
{
switch (sa->sa_family) {
case AF_INET:
#ifdef INET6
case AF_INET6:
#endif
return getnameinfo_inet(sa, salen, host, hostlen, serv,
servlen, flags);
case AF_LINK:
return getnameinfo_link(sa, salen, host, hostlen, serv,
servlen, flags);
default:
return EAI_FAMILY;
}
}
static const struct afd {
int a_af;
size_t a_addrlen;
@ -79,14 +114,8 @@ struct sockinet {
u_short si_port;
};
#ifdef INET6
static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
size_t, int);
static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
#endif
int
getnameinfo(const struct sockaddr *sa, socklen_t salen,
static int
getnameinfo_inet(const struct sockaddr *sa, socklen_t salen,
char *host, size_t hostlen, char *serv, size_t servlen,
int flags)
{
@ -343,3 +372,82 @@ ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
return n;
}
#endif /* INET6 */
/*
* getnameinfo_link():
* Format a link-layer address into a printable format, paying attention to
* the interface type.
*/
/* ARGSUSED */
static int
getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
char *host, size_t hostlen, char *serv, size_t servlen, int flags)
{
const struct sockaddr_dl *sdl =
(const struct sockaddr_dl *)(const void *)sa;
int n;
if (serv != NULL && servlen > 0)
*serv = '\0';
if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
if (n > hostlen) {
*host = '\0';
return EAI_MEMORY;
}
return 0;
}
switch (sdl->sdl_type) {
/*
* The following have zero-length addresses.
* IFT_ATM (net/if_atmsubr.c)
* IFT_FAITH (net/if_faith.c)
* IFT_GIF (net/if_gif.c)
* IFT_LOOP (net/if_loop.c)
* IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
* IFT_SLIP (net/if_sl.c, net/if_strip.c)
* IFT_STF (net/if_stf.c)
* IFT_L2VLAN (net/if_vlan.c)
* IFT_BRIDGE (net/if_bridge.h>
*/
/*
* The following use IPv4 addresses as link-layer addresses:
* IFT_OTHER (net/if_gre.c)
* IFT_OTHER (netinet/ip_ipip.c)
*/
/* default below is believed correct for all these. */
case IFT_ARCNET:
case IFT_ETHER:
case IFT_FDDI:
case IFT_HIPPI:
case IFT_ISO88025:
default:
return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen,
host, hostlen);
}
}
static int
hexname(cp, len, host, hostlen)
const u_int8_t *cp;
char *host;
size_t len, hostlen;
{
int i, n;
char *outp = host;
*outp = '\0';
for (i = 0; i < len; i++) {
n = snprintf(outp, hostlen, "%s%02x",
i ? ":" : "", cp[i]);
if (n < 0 || n >= hostlen) {
*host = '\0';
return EAI_MEMORY;
}
outp += n;
hostlen -= n;
}
return 0;
}

View File

@ -28,7 +28,7 @@
.\" From: @(#)inet.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$
.\"
.Dd June 14, 2004
.Dd February 28, 2007
.Dt INET 3
.Os
.Sh NAME
@ -212,19 +212,6 @@ may be decimal, octal, or hexadecimal, as specified
in the C language (i.e., a leading 0x or 0X implies
hexadecimal; otherwise, a leading 0 implies octal;
otherwise, the number is interpreted as decimal).
.Pp
The
.Fn inet_aton
and
.Fn inet_ntoa
functions are semi-deprecated in favor of the
.Xr addr2ascii 3
family.
However, since those functions are not yet widely implemented,
portable programs cannot rely on their presence and will continue
to use the
.Xr inet 3
functions for some time.
.Sh DIAGNOSTICS
The constant
.Dv INADDR_NONE
@ -250,9 +237,10 @@ or
family address.
.El
.Sh SEE ALSO
.Xr addr2ascii 3 ,
.Xr byteorder 3 ,
.Xr getaddrinfo 3 ,
.Xr gethostbyname 3 ,
.Xr getnameinfo 3 ,
.Xr getnetent 3 ,
.Xr inet_net 3 ,
.Xr hosts 5 ,

View File

@ -31,7 +31,7 @@
.\" From: @(#)linkaddr.3 8.1 (Berkeley) 7/28/93
.\" $FreeBSD$
.\"
.Dd June 17, 1996
.Dd February 28, 2007
.Dt LINK_ADDR 3
.Os
.Sh NAME
@ -92,11 +92,6 @@ Thus
.Li le0:8.0.9.13.d.30
represents an ethernet address
to be transmitted on the first Lance ethernet interface.
.Pp
The direct use of these functions is deprecated in favor of the
.Xr addr2ascii 3
interface; however, portable programs cannot rely on the latter as it is
not yet widely implemented.
.Sh RETURN VALUES
The
.Fn link_ntoa
@ -109,8 +104,7 @@ has no return value.
(See
.Sx BUGS . )
.Sh SEE ALSO
.Xr addr2ascii 3
.\" .Xr iso 4
.Xr getnameinfo 3
.Sh HISTORY
The
.Fn link_addr

View File

@ -132,11 +132,13 @@ ifmalist_dump_af(const struct ifmaddrs * const ifmap, int const af)
(psa->sdl.sdl_type == IFT_ETHER)) {
pgroup =
ether_ntoa((struct ether_addr *)&psa->sdl.sdl_data);
#if 0 /* XXX */
} else {
pgroup = addr2ascii(AF_LINK,
&psa->sdl,
sizeof(struct sockaddr_dl),
addrbuf);
#endif
}
break;
default: