xref: /minix3/lib/libutil/sockaddr_snprintf.3 (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc.\"     $NetBSD: sockaddr_snprintf.3,v 1.8 2013/06/07 17:23:26 christos Exp $
20c3983b2SBen Gras.\"
30c3983b2SBen Gras.\" Copyright (c) 2004 The NetBSD Foundation, Inc.
40c3983b2SBen Gras.\" All rights reserved.
50c3983b2SBen Gras.\"
60c3983b2SBen Gras.\" This code is derived from software contributed to The NetBSD Foundation
70c3983b2SBen Gras.\" by Christos Zoulas.
80c3983b2SBen Gras.\"
90c3983b2SBen Gras.\" Redistribution and use in source and binary forms, with or without
100c3983b2SBen Gras.\" modification, are permitted provided that the following conditions
110c3983b2SBen Gras.\" are met:
120c3983b2SBen Gras.\" 1. Redistributions of source code must retain the above copyright
130c3983b2SBen Gras.\"    notice, this list of conditions and the following disclaimer.
140c3983b2SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
150c3983b2SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
160c3983b2SBen Gras.\"    documentation and/or other materials provided with the distribution.
170c3983b2SBen Gras.\"
180c3983b2SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
190c3983b2SBen Gras.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
200c3983b2SBen Gras.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
210c3983b2SBen Gras.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
220c3983b2SBen Gras.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
230c3983b2SBen Gras.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
240c3983b2SBen Gras.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
250c3983b2SBen Gras.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
260c3983b2SBen Gras.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
270c3983b2SBen Gras.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
280c3983b2SBen Gras.\" POSSIBILITY OF SUCH DAMAGE.
290c3983b2SBen Gras.\"
30*84d9c625SLionel Sambuc.Dd June 7, 2013
310c3983b2SBen Gras.Dt SOCKADDR_SNPRINTF 3
320c3983b2SBen Gras.Os
330c3983b2SBen Gras.Sh NAME
340c3983b2SBen Gras.Nm sockaddr_snprintf
350c3983b2SBen Gras.Nd formatting function for socket address structures
360c3983b2SBen Gras.Sh LIBRARY
370c3983b2SBen Gras.Lb libutil
380c3983b2SBen Gras.Sh SYNOPSIS
390c3983b2SBen Gras.In util.h
400c3983b2SBen Gras.Ft int
410c3983b2SBen Gras.Fn sockaddr_snprintf "char *buf" "size_t buflen" "const char *fmt" "const struct sockaddr *sa"
420c3983b2SBen Gras.Sh DESCRIPTION
430c3983b2SBen GrasThe
440c3983b2SBen Gras.Fn sockaddr_snprintf
450c3983b2SBen Grasfunction formats a socket address into a form suitable for printing.
460c3983b2SBen Gras.Pp
470c3983b2SBen GrasThis function is convenient because it is protocol independent, i.e. one does
480c3983b2SBen Grasnot need to know the address family of the sockaddr in order to print it.
490c3983b2SBen GrasThe
500c3983b2SBen Gras.Xr printf 3
510c3983b2SBen Graslike format string specifies how the address is going to be printed.
520c3983b2SBen GrasSome formatting characters are only supported by some address families.
530c3983b2SBen GrasIf a certain formatting character is not supported, then the string
540c3983b2SBen Gras.Dq N/A
550c3983b2SBen Grasis printed.
560c3983b2SBen Gras.Pp
570c3983b2SBen GrasThe resulting formatted string is placed into
580c3983b2SBen Gras.Fa buf .
590c3983b2SBen GrasUp to
600c3983b2SBen Gras.Fa buflen
610c3983b2SBen Grascharacters are placed in
620c3983b2SBen Gras.Fa buf .
630c3983b2SBen Gras.Pp
640c3983b2SBen GrasThe following formatting characters are supported (immediately
650c3983b2SBen Grasafter a percent
660c3983b2SBen Gras.Pq Sq %
670c3983b2SBen Grassign):
680c3983b2SBen Gras.Bl -tag -width %a
690c3983b2SBen Gras.It a
700c3983b2SBen GrasThe node address portion of the socket address is printed numerically.
710c3983b2SBen GrasFor
720c3983b2SBen Gras.Dv AF_INET
730c3983b2SBen Grasthe address is printed as:
740c3983b2SBen Gras.Dq A.B.C.D
750c3983b2SBen Grasand
760c3983b2SBen Grasfor AF_INET6
770c3983b2SBen Grasthe address is printed as:
780c3983b2SBen Gras.Dq A:B...[%if]
790c3983b2SBen Grasusing
800c3983b2SBen Gras.Xr getnameinfo 3
810c3983b2SBen Grasinternally with
820c3983b2SBen Gras.Dv NI_NUMERICHOST .
830c3983b2SBen GrasFor
840c3983b2SBen Gras.Dv AF_APPLETALK
850c3983b2SBen Grasthe address is printed as:
860c3983b2SBen Gras.Dq A.B
870c3983b2SBen GrasFor
880c3983b2SBen Gras.Dv AF_LOCAL
890c3983b2SBen Gras.Pq Dv AF_UNIX
900c3983b2SBen Grasthe address is printed as:
910c3983b2SBen Gras.Dq socket-path
920c3983b2SBen GrasFor
930c3983b2SBen Gras.Dv AF_LINK
940c3983b2SBen Grasthe address is printed as:
950c3983b2SBen Gras.Dq a.b.c.d.e.f
960c3983b2SBen Grasusing
970c3983b2SBen Gras.Xr link_ntoa 3 ,
980c3983b2SBen Grasbut the interface portion is skipped (see below).
990c3983b2SBen GrasFor
1000c3983b2SBen Gras.Dv AF_UNSPEC
1010c3983b2SBen Grasnothing is printed.
1020c3983b2SBen Gras.It A
1030c3983b2SBen GrasThe symbolic name of the address is printed.
1040c3983b2SBen GrasFor
1050c3983b2SBen Gras.Dv AF_INET
1060c3983b2SBen Grasand
1070c3983b2SBen Gras.Dv AF_INET6
1080c3983b2SBen Grasthis is the hostname associated with the address.
1090c3983b2SBen GrasFor all other address families, it is the same as the
1100c3983b2SBen Gras.Dq a
1110c3983b2SBen Grasformat.
112*84d9c625SLionel Sambuc.It D
113*84d9c625SLionel SambucDebugging output:
114*84d9c625SLionel SambucFor all addresses, print all their fields as
115*84d9c625SLionel Sambuc.Dq field_name=value .
1160c3983b2SBen Gras.It f
1170c3983b2SBen GrasThe numeric value of the family of the address is printed.
1180c3983b2SBen Gras.It l
1190c3983b2SBen GrasThe length of the socket address is printed.
1200c3983b2SBen Gras.It p
1210c3983b2SBen GrasFor
1220c3983b2SBen Gras.Dv AF_INET ,
1230c3983b2SBen Gras.Dv AF_INET6 ,
1240c3983b2SBen Grasand
1250c3983b2SBen Gras.Dv AF_APPLETALK
1260c3983b2SBen Grasthe numeric value of the port portion of the address is printed.
1270c3983b2SBen Gras.It P
1280c3983b2SBen GrasFor
1290c3983b2SBen Gras.Dv AF_INET
1300c3983b2SBen Grasand
1310c3983b2SBen Gras.Dv AF_INET6
1320c3983b2SBen Grasthis is the name of the service associated with the port number, if
1330c3983b2SBen Grasavailable.
1340c3983b2SBen GrasFor all other address families, it is the same as the
1350c3983b2SBen Gras.Dq p
1360c3983b2SBen Grasformat.
1370c3983b2SBen Gras.It I
1380c3983b2SBen GrasFor
1390c3983b2SBen Gras.Dv AF_LINK
1400c3983b2SBen Grasaddresses, the interface name portion is printed.
1410c3983b2SBen Gras.It F
1420c3983b2SBen GrasFor
1430c3983b2SBen Gras.Dv AF_INET6
1440c3983b2SBen Grasaddresses, the flowinfo portion of the address is printed numerically.
1450c3983b2SBen Gras.It R
1460c3983b2SBen GrasFor
1470c3983b2SBen Gras.Dv AF_APPLETALK
1480c3983b2SBen Grasaddresses, the netrange portion of the address is printed as:
1490c3983b2SBen Gras.Dq phase:[firstnet,lastnet]
1500c3983b2SBen Gras.It S
1510c3983b2SBen GrasFor
1520c3983b2SBen Gras.Dv AF_INET6
1530c3983b2SBen Grasaddresses, the scope portion of the address is printed numerically.
1540c3983b2SBen Gras.It ?
1550c3983b2SBen GrasIf present between
1560c3983b2SBen Gras.Dq %
1570c3983b2SBen Grasand the format character, and the selected format does not apply to
1580c3983b2SBen Grasthe given address family, the
1590c3983b2SBen Gras.Dq N/A
1600c3983b2SBen Grasstring is elided and no output results.
1610c3983b2SBen Gras.El
1620c3983b2SBen Gras.Sh RETURN VALUES
1630c3983b2SBen GrasThe
1640c3983b2SBen Gras.Fn sockaddr_snprintf
1650c3983b2SBen Grasfunction returns the number of characters that are required to format the
1660c3983b2SBen Grasvalue
1670c3983b2SBen Gras.Fa val
1680c3983b2SBen Grasgiven the format string
1690c3983b2SBen Gras.Fa fmt
1700c3983b2SBen Grasexcluding the terminating NUL.
1710c3983b2SBen GrasThe returned string in
1720c3983b2SBen Gras.Fa buf
1730c3983b2SBen Grasis always NUL-terminated.
1740c3983b2SBen GrasIf the address family is not supported,
1750c3983b2SBen Gras.Fn sockaddr_snprintf
1760c3983b2SBen Grasreturns \-1 and sets
1770c3983b2SBen Gras.Va errno
1780c3983b2SBen Grasto
1790c3983b2SBen Gras.Dv EAFNOSUPPORT .
1800c3983b2SBen GrasFor
1810c3983b2SBen Gras.Dv AF_INET
1820c3983b2SBen Grasand
1830c3983b2SBen Gras.Dv AF_INET6
1840c3983b2SBen Grasaddresses
1850c3983b2SBen Gras.Fn sockaddr_snprintf
1860c3983b2SBen Grasreturns \-1 if the
1870c3983b2SBen Gras.Xr getnameinfo 3
1880c3983b2SBen Grasconversion failed, and
1890c3983b2SBen Gras.Fa errno
1900c3983b2SBen Grasis set to the error value from
1910c3983b2SBen Gras.Xr getnameinfo 3 .
1920c3983b2SBen Gras.Sh ERRORS
1930c3983b2SBen GrasIf the buffer
1940c3983b2SBen Gras.Fa buf
1950c3983b2SBen Grasis too small to hold the formatted output,
1960c3983b2SBen Gras.Fn sockaddr_snprintf
1970c3983b2SBen Graswill still return the buffer, containing a truncated string.
1980c3983b2SBen Gras.Sh SEE ALSO
1990c3983b2SBen Gras.Xr getaddrinfo 3 ,
2000c3983b2SBen Gras.Xr getnameinfo 3 ,
2010c3983b2SBen Gras.Xr link_ntoa 3 ,
2020c3983b2SBen Gras.Xr snprintf 3
2030c3983b2SBen Gras.Sh HISTORY
2040c3983b2SBen GrasThe
2050c3983b2SBen Gras.Fn sockaddr_snprintf
2060c3983b2SBen Grasfirst appeared in
2070c3983b2SBen Gras.Nx 3.0 .
2080c3983b2SBen Gras.Sh BUGS
2090c3983b2SBen GrasThe
2100c3983b2SBen Gras.Fn sockaddr_snprintf
2110c3983b2SBen Grasinterface is experimental and might change in the future.
2120c3983b2SBen Gras.Pp
2130c3983b2SBen GrasThere is no way to specify different formatting styles for particular
2140c3983b2SBen Grasaddresses.
2150c3983b2SBen GrasFor example it would be useful to print
2160c3983b2SBen Gras.Dv AF_LINK
2170c3983b2SBen Grasaddresses as
2180c3983b2SBen Gras.Dq %.2x:%.2x...
2190c3983b2SBen Grasinstead of
2200c3983b2SBen Gras.Dq %x.%x...
2210c3983b2SBen Gras.Pp
2220c3983b2SBen GrasThis function is supposed to be quick, but
2230c3983b2SBen Gras.Xr getnameinfo 3
2240c3983b2SBen Grasmight use system calls to convert the scope number to an interface
2250c3983b2SBen Grasname and the
2260c3983b2SBen Gras.Dq A
2270c3983b2SBen Grasand
2280c3983b2SBen Gras.Dq P
2290c3983b2SBen Grasformat characters call
2300c3983b2SBen Gras.Xr getaddrinfo 3
2310c3983b2SBen Graswhich may block for a noticeable period of time.
2320c3983b2SBen Gras.Pp
2330c3983b2SBen GrasNot all formatting characters are supported by all address families and
2340c3983b2SBen Grasprinting
2350c3983b2SBen Gras.Dq N/A
2360c3983b2SBen Grasis not very convenient.
2370c3983b2SBen GrasThe
2380c3983b2SBen Gras.Dq \&?
2390c3983b2SBen Grascharacter can suppress this, but other formatting (e.g., spacing or
2400c3983b2SBen Graspunctuation) will remain.
241