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
|
ADDR2ASCII(3) BSD Library Functions Manual ADDR2ASCII(3)
NAME
addr2ascii, ascii2addr -- Generic address formatting routines
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
char *
addr2ascii(int af, const void *addrp, int len, char *buf);
int
ascii2addr(int af, const char *ascii, void *result);
DESCRIPTION
The routines addr2ascii() and ascii2addr() are used to convert network addresses between bi-
nary form and a printable form appropriate to the address family. Both functions take an af
argument, specifying the address family to be used in the conversion process. (Currently,
only the AF_INET and AF_LINK address families are supported.)
The addr2ascii() function is used to convert binary, network-format addresses into printable
form. In addition to af, there are three other arguments. The addrp argument is a pointer
to the network address to be converted. The len argument is the length of the address. The
buf argument is an optional pointer to a caller-allocated buffer to hold the result; if a
null pointer is passed, addr2ascii() uses a statically-allocated buffer.
The ascii2addr() function performs the inverse operation to addr2ascii(). In addition to
af, it takes two parameters, ascii and result. The ascii parameter is a pointer to the
string which is to be converted into binary. The result parameter is a pointer to an appro-
priate network address structure for the specified family.
The following gives the appropriate structure to use for binary addresses in the specified
family:
AF_INET struct in_addr (in <netinet/in.h>)
AF_LINK struct sockaddr_dl (in <net/if_dl.h>)
RETURN VALUES
The addr2ascii() function returns the address of the buffer it was passed, or a static buf-
fer if the a null pointer was passed; on failure, it returns a null pointer. The
ascii2addr() function returns the length of the binary address in bytes, or -1 on failure.
EXAMPLES
The inet(3) functions inet_ntoa() and inet_aton() could be implemented thusly:
#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));
}
In actuality, this cannot be done because addr2ascii() and ascii2addr() are implemented in
terms of the inet(3) functions, rather than the other way around.
ERRORS
When a failure is returned, errno is set to one of the following values:
[ENAMETOOLONG] The addr2ascii() routine was passed a len parameter which was inappropri-
ate for the address family given by af.
[EPROTONOSUPPORT] Either routine was passed an af parameter other than AF_INET or AF_LINK.
[EINVAL] The string passed to ascii2addr() was improperly formatted for address
family af.
SEE ALSO
inet(3), linkaddr(3), inet(4)
HISTORY
An interface close to this one was originally suggested by Craig Partridge. This particular
interface originally appeared in the INRIA IPv6 implementation.
AUTHORS
Code and documentation by Garrett A. Wollman, MIT Laboratory for Computer Science.
BUGS
The original implementations supported IPv6. This support should eventually be resurrected.
The NRL implementation also included support for the AF_ISO and AF_NS address families.
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 dy-
namically allocated, and would always require a "struct sockaddr" to hold the binary ad-
dress. 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.
BSD June 13, 1996 BSD
|