NetBSD/lib/libutil/sockaddr_snprintf.3

237 lines
5.8 KiB
Groff
Raw Normal View History

2009-04-11 20:13:49 +04:00
.\" $NetBSD: sockaddr_snprintf.3,v 1.7 2009/04/11 16:13:49 joerg Exp $
2004-11-20 06:06:09 +03:00
.\"
.\" Copyright (c) 2004 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Christos Zoulas.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" 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.
.\"
.Dd April 9, 2005
2004-11-21 02:29:33 +03:00
.Dt SOCKADDR_SNPRINTF 3
2004-11-20 06:06:09 +03:00
.Os
.Sh NAME
.Nm sockaddr_snprintf
.Nd formatting function for socket address structures
2004-12-01 03:06:46 +03:00
.Sh LIBRARY
.Lb libutil
2004-11-20 06:06:09 +03:00
.Sh SYNOPSIS
.In util.h
.Ft int
2004-11-21 02:29:33 +03:00
.Fn sockaddr_snprintf "char *buf" "size_t buflen" "const char *fmt" "const struct sockaddr *sa"
2004-11-20 06:06:09 +03:00
.Sh DESCRIPTION
The
.Fn sockaddr_snprintf
function formats a socket address into a form suitable for printing.
.Pp
This function is convenient because it is protocol independent, i.e. one does
not need to know the address family of the sockaddr in order to print it.
The
.Xr printf 3
like format string specifies how the address is going to be printed.
Some formatting characters are only supported by some address families.
If a certain formatting character is not supported, then the string
2004-11-21 02:29:33 +03:00
.Dq N/A
2004-11-20 06:06:09 +03:00
is printed.
.Pp
The resulting formatted string is placed into
.Fa buf .
Up to
.Fa buflen
characters are placed in
.Fa buf .
.Pp
2004-11-21 02:29:33 +03:00
The following formatting characters are supported (immediately
after a percent
.Pq Sq %
sign):
2004-11-20 06:06:09 +03:00
.Bl -tag -width %a
.It a
The node address portion of the socket address is printed numerically.
For
.Dv AF_INET
the address is printed as:
2004-11-21 02:29:33 +03:00
.Dq A.B.C.D
2004-11-20 06:06:09 +03:00
and
for AF_INET6
the address is printed as:
2004-11-21 02:29:33 +03:00
.Dq A:B...[%if]
2004-11-20 06:06:09 +03:00
using
2004-11-21 02:29:33 +03:00
.Xr getnameinfo 3
2004-11-20 06:06:09 +03:00
internally with
.Dv NI_NUMERICHOST .
2004-11-21 02:29:33 +03:00
For
2004-11-20 06:06:09 +03:00
.Dv AF_APPLETALK
the address is printed as:
2004-11-21 02:29:33 +03:00
.Dq A.B
2004-11-20 06:06:09 +03:00
For
.Dv AF_LOCAL
2004-11-21 02:29:33 +03:00
.Pq Dv AF_UNIX
2004-11-20 06:06:09 +03:00
the address is printed as:
2004-11-21 02:29:33 +03:00
.Dq socket-path
2004-11-20 06:06:09 +03:00
For
.Dv AF_LINK
the address is printed as:
2004-11-21 02:29:33 +03:00
.Dq a.b.c.d.e.f
2004-11-20 06:06:09 +03:00
using
.Xr link_ntoa 3 ,
but the interface portion is skipped (see below).
For
.Dv AF_UNSPEC
nothing is printed.
.It A
The symbolic name of the address is printed.
For
.Dv AF_INET
and
2009-04-11 20:13:49 +04:00
.Dv AF_INET6
this is the hostname associated with the address.
For all other address families, it is the same as the
.Dq a
format.
2004-11-20 06:06:09 +03:00
.It f
The numeric value of the family of the address is printed.
.It l
The length of the socket address is printed.
.It p
For
.Dv AF_INET ,
.Dv AF_INET6 ,
and
.Dv AF_APPLETALK
the numeric value of the port portion of the address is printed.
.It P
For
.Dv AF_INET
and
.Dv AF_INET6
this is the name of the service associated with the port number, if
available.
For all other address families, it is the same as the
.Dq p
format.
2004-11-20 06:06:09 +03:00
.It I
For
.Dv AF_LINK
addresses, the interface name portion is printed.
.It F
For
.Dv AF_INET6
addresses, the flowinfo portion of the address is printed numerically.
.It R
For
.Dv AF_APPLETALK
addresses, the netrange portion of the address is printed as:
2004-11-21 02:29:33 +03:00
.Dq phase:[firstnet,lastnet]
2004-11-20 06:06:09 +03:00
.It S
For
.Dv AF_INET6
addresses, the scope portion of the address is printed numerically.
.It ?
If present between
.Dq %
and the format character, and the selected format does not apply to
the given address family, the
.Dq N/A
string is elided and no output results.
2004-11-20 06:06:09 +03:00
.El
.Sh RETURN VALUES
The
2004-11-21 02:29:33 +03:00
.Fn sockaddr_snprintf
2004-11-20 06:06:09 +03:00
function returns the number of characters that are required to format the
value
.Fa val
given the format string
.Fa fmt
excluding the terminating NUL.
The returned string in
.Fa buf
is always NUL-terminated.
If the address family is not supported,
.Fn sockaddr_snprintf
2004-11-21 02:29:33 +03:00
returns \-1 and sets
.Va errno
to
2004-11-20 06:06:09 +03:00
.Dv EAFNOSUPPORT .
For
.Dv AF_INET
and
2009-04-11 20:13:49 +04:00
.Dv AF_INET6
2004-11-20 06:06:09 +03:00
addresses
.Fn sockaddr_snprintf
2004-11-21 02:29:33 +03:00
returns \-1 if the
2004-11-20 06:06:09 +03:00
.Xr getnameinfo 3
conversion failed, and
.Fa errno
is set to the error value from
.Xr getnameinfo 3 .
.Sh ERRORS
If the buffer
.Fa buf
is too small to hold the formatted output,
.Fn sockaddr_snprintf
will still return the buffer, containing a truncated string.
.Sh SEE ALSO
.Xr getaddrinfo 3 ,
2004-11-21 02:29:33 +03:00
.Xr getnameinfo 3 ,
.Xr link_ntoa 3 ,
2004-11-20 06:06:09 +03:00
.Xr snprintf 3
.Sh HISTORY
The
.Fn sockaddr_snprintf
first appeared in
2004-11-21 02:29:33 +03:00
.Nx 3.0 .
2004-11-20 06:06:09 +03:00
.Sh BUGS
The
.Fn sockaddr_snprintf
interface is experimental and might change in the future.
.Pp
There is no way to specify different formatting styles for particular
addresses.
For example it would be useful to print
.Dv AF_LINK
addresses as
2004-11-21 02:29:33 +03:00
.Dq %.2x:%.2x...
2004-11-20 06:06:09 +03:00
instead of
2004-11-21 02:29:33 +03:00
.Dq %x.%x...
2004-11-20 06:06:09 +03:00
.Pp
This function is supposed to be quick, but
.Xr getnameinfo 3
might use system calls to convert the scope number to an interface
name and the
.Dq A
and
.Dq P
format characters call
.Xr getaddrinfo 3
which may block for a noticeable period of time.
2004-11-20 06:06:09 +03:00
.Pp
Not all formatting characters are supported by all address families and
printing
2004-11-21 02:29:33 +03:00
.Dq N/A
is not very convenient.
The
.Dq \&?
character can suppress this, but other formatting (e.g., spacing or
punctuation) will remain.