summaryrefslogtreecommitdiff
path: root/lib/libutil/sockaddr_snprintf.3
blob: e26ed47be8ec0e967b64b5ee5a0e3a14ad96c314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
.\"     $NetBSD: sockaddr_snprintf.3,v 1.8 2013/06/07 17:23:26 christos Exp $
.\"
.\" 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 June 7, 2013
.Dt SOCKADDR_SNPRINTF 3
.Os
.Sh NAME
.Nm sockaddr_snprintf
.Nd formatting function for socket address structures
.Sh LIBRARY
.Lb libutil
.Sh SYNOPSIS
.In util.h
.Ft int
.Fn sockaddr_snprintf "char *buf" "size_t buflen" "const char *fmt" "const struct sockaddr *sa"
.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
.Dq N/A
is printed.
.Pp
The resulting formatted string is placed into
.Fa buf .
Up to
.Fa buflen
characters are placed in
.Fa buf .
.Pp
The following formatting characters are supported (immediately
after a percent
.Pq Sq %
sign):
.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:
.Dq A.B.C.D
and
for AF_INET6
the address is printed as:
.Dq A:B...[%if]
using
.Xr getnameinfo 3
internally with
.Dv NI_NUMERICHOST .
For
.Dv AF_APPLETALK
the address is printed as:
.Dq A.B
For
.Dv AF_LOCAL
.Pq Dv AF_UNIX
the address is printed as:
.Dq socket-path
For
.Dv AF_LINK
the address is printed as:
.Dq a.b.c.d.e.f
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
.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.
.It D
Debugging output:
For all addresses, print all their fields as
.Dq field_name=value .
.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.
.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:
.Dq phase:[firstnet,lastnet]
.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.
.El
.Sh RETURN VALUES
The
.Fn sockaddr_snprintf
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
returns \-1 and sets
.Va errno
to
.Dv EAFNOSUPPORT .
For
.Dv AF_INET
and
.Dv AF_INET6
addresses
.Fn sockaddr_snprintf
returns \-1 if the
.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 ,
.Xr getnameinfo 3 ,
.Xr link_ntoa 3 ,
.Xr snprintf 3
.Sh HISTORY
The
.Fn sockaddr_snprintf
first appeared in
.Nx 3.0 .
.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
.Dq %.2x:%.2x...
instead of
.Dq %x.%x...
.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.
.Pp
Not all formatting characters are supported by all address families and
printing
.Dq N/A
is not very convenient.
The
.Dq \&?
character can suppress this, but other formatting (e.g., spacing or
punctuation) will remain.