xref: /netbsd-src/lib/libutil/sockaddr_snprintf.3 (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1.\"     $NetBSD: sockaddr_snprintf.3,v 1.7 2009/04/11 16:13:49 joerg Exp $
2.\"
3.\" Copyright (c) 2004 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Christos Zoulas.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd April 9, 2005
31.Dt SOCKADDR_SNPRINTF 3
32.Os
33.Sh NAME
34.Nm sockaddr_snprintf
35.Nd formatting function for socket address structures
36.Sh LIBRARY
37.Lb libutil
38.Sh SYNOPSIS
39.In util.h
40.Ft int
41.Fn sockaddr_snprintf "char *buf" "size_t buflen" "const char *fmt" "const struct sockaddr *sa"
42.Sh DESCRIPTION
43The
44.Fn sockaddr_snprintf
45function formats a socket address into a form suitable for printing.
46.Pp
47This function is convenient because it is protocol independent, i.e. one does
48not need to know the address family of the sockaddr in order to print it.
49The
50.Xr printf 3
51like format string specifies how the address is going to be printed.
52Some formatting characters are only supported by some address families.
53If a certain formatting character is not supported, then the string
54.Dq N/A
55is printed.
56.Pp
57The resulting formatted string is placed into
58.Fa buf .
59Up to
60.Fa buflen
61characters are placed in
62.Fa buf .
63.Pp
64The following formatting characters are supported (immediately
65after a percent
66.Pq Sq %
67sign):
68.Bl -tag -width %a
69.It a
70The node address portion of the socket address is printed numerically.
71For
72.Dv AF_INET
73the address is printed as:
74.Dq A.B.C.D
75and
76for AF_INET6
77the address is printed as:
78.Dq A:B...[%if]
79using
80.Xr getnameinfo 3
81internally with
82.Dv NI_NUMERICHOST .
83For
84.Dv AF_APPLETALK
85the address is printed as:
86.Dq A.B
87For
88.Dv AF_LOCAL
89.Pq Dv AF_UNIX
90the address is printed as:
91.Dq socket-path
92For
93.Dv AF_LINK
94the address is printed as:
95.Dq a.b.c.d.e.f
96using
97.Xr link_ntoa 3 ,
98but the interface portion is skipped (see below).
99For
100.Dv AF_UNSPEC
101nothing is printed.
102.It A
103The symbolic name of the address is printed.
104For
105.Dv AF_INET
106and
107.Dv AF_INET6
108this is the hostname associated with the address.
109For all other address families, it is the same as the
110.Dq a
111format.
112.It f
113The numeric value of the family of the address is printed.
114.It l
115The length of the socket address is printed.
116.It p
117For
118.Dv AF_INET ,
119.Dv AF_INET6 ,
120and
121.Dv AF_APPLETALK
122the numeric value of the port portion of the address is printed.
123.It P
124For
125.Dv AF_INET
126and
127.Dv AF_INET6
128this is the name of the service associated with the port number, if
129available.
130For all other address families, it is the same as the
131.Dq p
132format.
133.It I
134For
135.Dv AF_LINK
136addresses, the interface name portion is printed.
137.It F
138For
139.Dv AF_INET6
140addresses, the flowinfo portion of the address is printed numerically.
141.It R
142For
143.Dv AF_APPLETALK
144addresses, the netrange portion of the address is printed as:
145.Dq phase:[firstnet,lastnet]
146.It S
147For
148.Dv AF_INET6
149addresses, the scope portion of the address is printed numerically.
150.It ?
151If present between
152.Dq %
153and the format character, and the selected format does not apply to
154the given address family, the
155.Dq N/A
156string is elided and no output results.
157.El
158.Sh RETURN VALUES
159The
160.Fn sockaddr_snprintf
161function returns the number of characters that are required to format the
162value
163.Fa val
164given the format string
165.Fa fmt
166excluding the terminating NUL.
167The returned string in
168.Fa buf
169is always NUL-terminated.
170If the address family is not supported,
171.Fn sockaddr_snprintf
172returns \-1 and sets
173.Va errno
174to
175.Dv EAFNOSUPPORT .
176For
177.Dv AF_INET
178and
179.Dv AF_INET6
180addresses
181.Fn sockaddr_snprintf
182returns \-1 if the
183.Xr getnameinfo 3
184conversion failed, and
185.Fa errno
186is set to the error value from
187.Xr getnameinfo 3 .
188.Sh ERRORS
189If the buffer
190.Fa buf
191is too small to hold the formatted output,
192.Fn sockaddr_snprintf
193will still return the buffer, containing a truncated string.
194.Sh SEE ALSO
195.Xr getaddrinfo 3 ,
196.Xr getnameinfo 3 ,
197.Xr link_ntoa 3 ,
198.Xr snprintf 3
199.Sh HISTORY
200The
201.Fn sockaddr_snprintf
202first appeared in
203.Nx 3.0 .
204.Sh BUGS
205The
206.Fn sockaddr_snprintf
207interface is experimental and might change in the future.
208.Pp
209There is no way to specify different formatting styles for particular
210addresses.
211For example it would be useful to print
212.Dv AF_LINK
213addresses as
214.Dq %.2x:%.2x...
215instead of
216.Dq %x.%x...
217.Pp
218This function is supposed to be quick, but
219.Xr getnameinfo 3
220might use system calls to convert the scope number to an interface
221name and the
222.Dq A
223and
224.Dq P
225format characters call
226.Xr getaddrinfo 3
227which may block for a noticeable period of time.
228.Pp
229Not all formatting characters are supported by all address families and
230printing
231.Dq N/A
232is not very convenient.
233The
234.Dq \&?
235character can suppress this, but other formatting (e.g., spacing or
236punctuation) will remain.
237