]> git.saurik.com Git - apple/libc.git/blame - net/FreeBSD/addr2ascii.3
Libc-498.tar.gz
[apple/libc.git] / net / FreeBSD / addr2ascii.3
CommitLineData
5b2abdfb
A
1.\"
2.\" Copyright 1996 Massachusetts Institute of Technology
3.\"
4.\" Permission to use, copy, modify, and distribute this software and
5.\" its documentation for any purpose and without fee is hereby
6.\" granted, provided that both the above copyright notice and this
7.\" permission notice appear in all copies, that both the above
8.\" copyright notice and this permission notice appear in all
9.\" supporting documentation, and that the name of M.I.T. not be used
10.\" in advertising or publicity pertaining to distribution of the
11.\" software without specific, written prior permission. M.I.T. makes
12.\" no representations about the suitability of this software for any
13.\" purpose. It is provided "as is" without express or implied
14.\" warranty.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17.\" ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20.\" SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23.\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27.\" SUCH DAMAGE.
28.\"
29.\" $ANA: addr2ascii.3,v 1.1 1996/06/13 18:41:46 wollman Exp $
3d9156a7 30.\" $FreeBSD: src/lib/libc/net/addr2ascii.3,v 1.17 2004/10/09 17:13:58 maxim Exp $
5b2abdfb
A
31.\"
32.Dd June 13, 1996
33.Dt ADDR2ASCII 3
34.Os
35.Sh NAME
36.Nm addr2ascii ,
37.Nm ascii2addr
38.Nd Generic address formatting routines
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
5b2abdfb
A
42.In arpa/inet.h
43.Ft "char *"
44.Fn addr2ascii "int af" "const void *addrp" "int len" "char *buf"
45.Ft int
46.Fn ascii2addr "int af" "const char *ascii" "void *result"
47.Sh DESCRIPTION
48The routines
49.Fn addr2ascii
50and
51.Fn ascii2addr
52are used to convert network addresses between binary form and a
3d9156a7
A
53printable form appropriate to the address family.
54Both functions take
5b2abdfb
A
55an
56.Fa af
57argument, specifying the address family to be used in the conversion
58process.
59(Currently, only the
60.Dv AF_INET
61and
62.Dv AF_LINK
63address families are supported.)
64.Pp
65The
66.Fn addr2ascii
67function
68is used to convert binary, network-format addresses into printable
3d9156a7
A
69form.
70In addition to
5b2abdfb 71.Fa af ,
3d9156a7
A
72there are three other arguments.
73The
5b2abdfb
A
74.Fa addrp
75argument is a pointer to the network address to be converted.
76The
77.Fa len
3d9156a7
A
78argument is the length of the address.
79The
5b2abdfb
A
80.Fa buf
81argument is an optional pointer to a caller-allocated buffer to hold
82the result; if a null pointer is passed,
83.Fn addr2ascii
84uses a statically-allocated buffer.
85.Pp
86The
87.Fn ascii2addr
88function performs the inverse operation to
89.Fn addr2ascii .
90In addition to
91.Fa af ,
9385eb3d 92it takes two arguments,
5b2abdfb
A
93.Fa ascii
94and
95.Fa result .
96The
97.Fa ascii
9385eb3d 98argument is a pointer to the string which is to be converted into
3d9156a7
A
99binary.
100The
5b2abdfb 101.Fa result
9385eb3d 102argument is a pointer to an appropriate network address structure for
5b2abdfb
A
103the specified family.
104.Pp
105The following gives the appropriate structure to use for binary
106addresses in the specified family:
107.Pp
108.Bl -tag -width AF_INETxxxx -compact
109.It Dv AF_INET
110.Li struct in_addr
111(in
3d9156a7 112.In arpa/inet.h )
5b2abdfb
A
113.It Dv AF_LINK
114.Li struct sockaddr_dl
115(in
3d9156a7 116.In net/if_dl.h )
5b2abdfb
A
117.\" .It Dv AF_INET6
118.\" .Li struct in6_addr
119.\" (in
3d9156a7 120.\" .In netinet6/in6.h )
5b2abdfb 121.El
3d9156a7
A
122.Pp
123.Dv AF_INET and AF_LINK constants are defined in
124.In sys/socket.h
5b2abdfb
A
125.Sh RETURN VALUES
126The
127.Fn addr2ascii
128function returns the address of the buffer it was passed, or a static
129buffer if the a null pointer was passed; on failure, it returns a null
130pointer.
131The
132.Fn ascii2addr
133function returns the length of the binary address in bytes, or -1 on
134failure.
135.Sh EXAMPLES
136The
137.Xr inet 3
138functions
139.Fn inet_ntoa
140and
141.Fn inet_aton
142could be implemented thusly:
143.Bd -literal -offset indent
5b2abdfb 144#include <sys/socket.h>
5b2abdfb
A
145#include <arpa/inet.h>
146
147char *
148inet_ntoa(struct in_addr addr)
149{
150 return addr2ascii(AF_INET, &addr, sizeof addr, 0);
151}
152
153int
154inet_aton(const char *ascii, struct in_addr *addr)
155{
156 return (ascii2addr(AF_INET, ascii, addr)
157 == sizeof(*addr));
158}
159.Ed
160.Pp
161In actuality, this cannot be done because
162.Fn addr2ascii
163and
164.Fn ascii2addr
165are implemented in terms of the
166.Xr inet 3
167functions, rather than the other way around.
168.Sh ERRORS
169When a failure is returned,
170.Li errno
171is set to one of the following values:
172.Bl -tag -width Er
173.It Bq Er ENAMETOOLONG
174The
175.Fn addr2ascii
176routine was passed a
177.Fa len
9385eb3d 178argument which was inappropriate for the address family given by
5b2abdfb
A
179.Fa af .
180.It Bq Er EPROTONOSUPPORT
181Either routine was passed an
182.Fa af
9385eb3d 183argument other than
5b2abdfb
A
184.Dv AF_INET
185or
186.Dv AF_LINK .
187.It Bq Er EINVAL
188The string passed to
189.Fn ascii2addr
190was improperly formatted for address family
191.Fa af .
192.El
193.Sh SEE ALSO
194.Xr inet 3 ,
195.Xr linkaddr 3 ,
196.Xr inet 4
197.Sh HISTORY
198An interface close to this one was originally suggested by Craig
3d9156a7
A
199Partridge.
200This particular interface originally appeared in the
5b2abdfb
A
201.Tn INRIA
202.Tn IPv6
203implementation.
204.Sh AUTHORS
205Code and documentation by
206.An Garrett A. Wollman ,
207MIT Laboratory for Computer Science.
208.Sh BUGS
3d9156a7
A
209The original implementations supported IPv6.
210This support should
211eventually be resurrected.
212The
5b2abdfb
A
213.Tn NRL
214implementation also included support for the
215.Dv AF_ISO
216and
217.Dv AF_NS
218address families.
219.Pp
3d9156a7
A
220The genericity of this interface is somewhat questionable.
221A truly
5b2abdfb
A
222generic interface would provide a means for determining the length of
223the buffer to be used so that it could be dynamically allocated, and
224would always require a
225.Dq Li "struct sockaddr"
3d9156a7
A
226to hold the binary address.
227Unfortunately, this is incompatible with existing
228practice.
229This limitation means that a routine for printing network
5b2abdfb
A
230addresses from arbitrary address families must still have internal
231knowledge of the maximum buffer length needed and the appropriate part
232of the address to use as the binary address.