summaryrefslogtreecommitdiffstats
path: root/c/src/exec/libnetworking/libc/addr2ascii.3
blob: 10c839ea74d8402a5d3fa6237c4c7c9fb0440649 (plain) (blame)
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
.\"
.\" Copyright 1996 Massachusetts Institute of Technology
.\"
.\" Permission to use, copy, modify, and distribute this software and
.\" its documentation for any purpose and without fee is hereby
.\" granted, provided that both the above copyright notice and this
.\" permission notice appear in all copies, that both the above
.\" copyright notice and this permission notice appear in all
.\" supporting documentation, and that the name of M.I.T. not be used
.\" in advertising or publicity pertaining to distribution of the
.\" software without specific, written prior permission.  M.I.T. makes
.\" no representations about the suitability of this software for any
.\" purpose.  It is provided "as is" without express or implied
.\" warranty.
.\" 
.\" THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
.\" ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
.\" SHALL M.I.T. 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.
.\"
.\"	$ANA: addr2ascii.3,v 1.1 1996/06/13 18:41:46 wollman Exp $
.\"     $Id$
.\"
.Dd June 13, 1996
.Dt ADDR2ASCII 3
.Os
.Sh NAME
.Nm addr2ascii ,
.Nm ascii2addr
.Nd Generic address formatting routines
.Sh SYNOPSIS
.Fd #include <sys/types.h>
.Fd #include <netinet/in.h>
.Fd #include <arpa/inet.h>
.Ft "char *"
.Fn addr2ascii "int af" "const void *addrp" "int len" "char *buf"
.Ft int
.Fn ascii2addr "int af" "const char *ascii" "void *result"
.Sh DESCRIPTION
The routines
.Fn addr2ascii
and
.Fn ascii2addr
are used to convert network addresses between binary form and a
printable form appropriate to the address family.  Both functions take
an
.Fa af
argument, specifying the address family to be used in the conversion
process.
(Currently, only the
.Dv AF_INET
and
.Dv AF_LINK
address families are supported.)
.Pp
The
.Fn addr2ascii
function
is used to convert binary, network-format addresses into printable
form.  In addition to
.Fa af ,
there are three other arguments.  The
.Fa addrp
argument is a pointer to the network address to be converted.
The
.Fa len
argument is the length of the address.  The
.Fa buf
argument is an optional pointer to a caller-allocated buffer to hold
the result; if a null pointer is passed,
.Fn addr2ascii
uses a statically-allocated buffer.
.Pp
The
.Fn ascii2addr
function performs the inverse operation to
.Fn addr2ascii .
In addition to
.Fa af ,
it takes two parameters,
.Fa ascii
and
.Fa result .
The
.Fa ascii
parameter is a pointer to the string which is to be converted into
binary.  The
.Fa result
parameter is a pointer to an appropriate network address structure for
the specified family.
.Pp
The following gives the appropriate structure to use for binary
addresses in the specified family:
.Pp
.Bl -tag -width AF_INETxxxx -compact
.It Dv AF_INET
.Li struct in_addr
.Pq in Aq Pa netinet/in.h
.It Dv AF_LINK
.Li struct sockaddr_dl
.Pq in Aq Pa net/if_dl.h
.\" .It Dv AF_INET6
.\" .Li struct in6_addr
.\" .Pq in Aq Pa netinet6/in6.h
.El
.Sh RETURN VALUES
The
.Fn addr2ascii
function returns the address of the buffer it was passed, or a static
buffer if the a null pointer was passed; on failure, it returns a null
pointer.
The
.Fn ascii2addr
function returns the length of the binary address in bytes, or -1 on
failure.
.Sh EXAMPLES
The
.Xr inet 3
functions
.Fn inet_ntoa
and
.Fn inet_aton
could be implemented thusly:
.Bd -literal -offset indent
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

char *
inet_ntoa(struct in_addr addr)
{
	return addr2ascii(AF_INET, &addr, sizeof addr, 0);
}

int
inet_aton(const char *ascii, struct in_addr *addr)
{
	return (ascii2addr(AF_INET, ascii, addr) 
	    == sizeof(*addr));
}
.Ed
.Pp
In actuality, this cannot be done because
.Fn addr2ascii
and
.Fn ascii2addr
are implemented in terms of the
.Xr inet 3
functions, rather than the other way around.
.Sh ERRORS
When a failure is returned,
.Li errno
is set to one of the following values:
.Bl -tag -width [EPROTONOSUPPORT]
.It Bq Er ENAMETOOLONG
The
.Fn addr2ascii
routine was passed a
.Fa len
parameter which was inappropriate for the address family given by
.Fa af .
.It Bq Er EPROTONOSUPPORT
Either routine was passed an
.Fa af
parameter other than
.Dv AF_INET
or
.Dv AF_LINK .
.It Bq Er EINVAL
The string passed to
.Fn ascii2addr
was improperly formatted for address family
.Fa af .
.El
.Sh SEE ALSO
.Xr inet 3 ,
.Xr linkaddr 3 ,
.Xr inet 4
.Sh HISTORY
An interface close to this one was originally suggested by Craig
Partridge.  This particular interface originally appeared in the
.Tn INRIA
.Tn IPv6
implementation.
.Sh AUTHORS
Code and documentation by
.An Garrett A. Wollman ,
MIT Laboratory for Computer Science.
.Sh BUGS
The original implementations supported IPv6.  This support should
eventually be resurrected.  The
.Tn NRL
implementation also included support for the
.Dv AF_ISO
and
.Dv AF_NS
address families.
.Pp
The genericity of this interface is somewhat questionable.  A truly
generic interface would provide a means for determining the length of
the buffer to be used so that it could be dynamically allocated, and
would always require a
.Dq Li "struct sockaddr"
to hold the binary address.  Unfortunately, this is incompatible with existing
practice.  This limitation means that a routine for printing network
addresses from arbitrary address families must still have internal
knowledge of the maximum buffer length needed and the appropriate part
of the address to use as the binary address.