sl@0
|
1 |
/** @file ../include/arpa/inet.h
|
sl@0
|
2 |
@internalComponent
|
sl@0
|
3 |
*/
|
sl@0
|
4 |
|
sl@0
|
5 |
/** @fn inet_addr(const char *cp)
|
sl@0
|
6 |
@param cp
|
sl@0
|
7 |
|
sl@0
|
8 |
Note: This description also covers the following functions -
|
sl@0
|
9 |
inet_aton() inet_ntoa() inet_ntop() inet_pton()
|
sl@0
|
10 |
|
sl@0
|
11 |
@code
|
sl@0
|
12 |
struct in_addr or some other internal binary representation, in network byte order).
|
sl@0
|
13 |
It returns 1 if the address was valid for the specified address family, or
|
sl@0
|
14 |
0 if the address was not parseable in the specified address family, or -1
|
sl@0
|
15 |
if some system error occurred.
|
sl@0
|
16 |
This function is presently valid for AF_INET and AF_INET6.
|
sl@0
|
17 |
@endcode
|
sl@0
|
18 |
@code
|
sl@0
|
19 |
struct in_addr or some other binary form, in network byte order) to presentation format
|
sl@0
|
20 |
(suitable for external display purposes).
|
sl@0
|
21 |
The size argument specifies the size, in bytes, of the buffer *dst It returns NULL if a system error occurs (in which case, errno will have been set), or it returns a pointer to the destination string.
|
sl@0
|
22 |
This function is presently valid for AF_INET and AF_INET6.
|
sl@0
|
23 |
@endcode
|
sl@0
|
24 |
@code
|
sl@0
|
25 |
The routines inet_addr and inet_aton interpret character strings representing
|
sl@0
|
26 |
numbers expressed in the Internet standard ‘.’ notation.
|
sl@0
|
27 |
@endcode
|
sl@0
|
28 |
|
sl@0
|
29 |
The inet_pton function converts a presentation format address (that is, printable form
|
sl@0
|
30 |
as held in a character string) to network format (usually a struct in_addr or some other internal binary representation, in network byte order).
|
sl@0
|
31 |
It returns 1 if the address was valid for the specified address family, or
|
sl@0
|
32 |
0 if the address was not parseable in the specified address family, or -1
|
sl@0
|
33 |
if some system error occurred.
|
sl@0
|
34 |
This function is presently valid for AF_INET and AF_INET6.
|
sl@0
|
35 |
|
sl@0
|
36 |
The inet_aton routine interprets the specified character string as an Internet address,
|
sl@0
|
37 |
placing the address into the structure provided.
|
sl@0
|
38 |
It returns 1 if the string was successfully interpreted,
|
sl@0
|
39 |
or 0 if the string is invalid.
|
sl@0
|
40 |
The inet_addr functions return numbers suitable for use
|
sl@0
|
41 |
as Internet addresses.
|
sl@0
|
42 |
|
sl@0
|
43 |
The function inet_ntop converts an address *src from network format
|
sl@0
|
44 |
(usually a struct in_addr or some other binary form, in network byte order) to presentation format
|
sl@0
|
45 |
(suitable for external display purposes).
|
sl@0
|
46 |
The size argument specifies the size, in bytes, of the buffer *dst It returns NULL if a system error occurs (in which case, errno will have been set), or it returns a pointer to the destination string.
|
sl@0
|
47 |
This function is presently valid for AF_INET and AF_INET6.
|
sl@0
|
48 |
|
sl@0
|
49 |
The routine inet_ntoa takes an Internet address and returns an ASCII string representing the address in ' . '
|
sl@0
|
50 |
notation.
|
sl@0
|
51 |
|
sl@0
|
52 |
All Internet addresses are returned in network
|
sl@0
|
53 |
order (bytes ordered from left to right).
|
sl@0
|
54 |
All network numbers and local address parts are
|
sl@0
|
55 |
returned as machine byte order integer values.
|
sl@0
|
56 |
|
sl@0
|
57 |
Diagnostics:
|
sl@0
|
58 |
|
sl@0
|
59 |
The constant INADDR_NONE is returned by inet_addr for malformed requests.
|
sl@0
|
60 |
|
sl@0
|
61 |
@see gethostbyname()
|
sl@0
|
62 |
|
sl@0
|
63 |
|
sl@0
|
64 |
Examples:
|
sl@0
|
65 |
@code
|
sl@0
|
66 |
#include <stdio.h>
|
sl@0
|
67 |
#include <arpa/inet.h>
|
sl@0
|
68 |
#include <netinet/in.h>
|
sl@0
|
69 |
#define IPV6ADDRSIZE 48
|
sl@0
|
70 |
int main()
|
sl@0
|
71 |
{
|
sl@0
|
72 |
unsigned int nbo_value;
|
sl@0
|
73 |
char *ipaddrstring="1.2.3.4";
|
sl@0
|
74 |
char *ipaddrholdr=NULL;
|
sl@0
|
75 |
char *ipv6addrstring="8000::123:4567:89AB:CDEF";
|
sl@0
|
76 |
struct in_addr ipstruct;
|
sl@0
|
77 |
struct in6_addr ipv6struct;
|
sl@0
|
78 |
char result[IPV6ADDRSIZE];
|
sl@0
|
79 |
int err;
|
sl@0
|
80 |
int size;
|
sl@0
|
81 |
const char* error;
|
sl@0
|
82 |
nbo_value=inet_addr(ipaddrstring);
|
sl@0
|
83 |
if(nbo_value == -1)
|
sl@0
|
84 |
{
|
sl@0
|
85 |
printf("inet_addr failed0);
|
sl@0
|
86 |
}
|
sl@0
|
87 |
else
|
sl@0
|
88 |
{
|
sl@0
|
89 |
printf("inet_addr passed0);
|
sl@0
|
90 |
}
|
sl@0
|
91 |
ipstruct.s_addr=nbo_value;
|
sl@0
|
92 |
ipaddrholdr=inet_ntoa(ipstruct);
|
sl@0
|
93 |
if(ipaddrholdr==NULL)
|
sl@0
|
94 |
{
|
sl@0
|
95 |
printf("inet_ntoa failed0);
|
sl@0
|
96 |
}
|
sl@0
|
97 |
else
|
sl@0
|
98 |
{
|
sl@0
|
99 |
printf("ipaddr is %s0,ipaddrholdr);
|
sl@0
|
100 |
}
|
sl@0
|
101 |
err=inet_pton(AF_INET6,ipv6addrstring ,&ipv6struct;);
|
sl@0
|
102 |
if(err ==0 || err==-1)
|
sl@0
|
103 |
printf("inet_pton Failed0);
|
sl@0
|
104 |
else
|
sl@0
|
105 |
printf("inet_pton passed0);
|
sl@0
|
106 |
size=sizeof(result);
|
sl@0
|
107 |
error=inet_ntop(AF_INET6,&ipv6struct.s6;_addr,result,size);
|
sl@0
|
108 |
if(error==NULL)
|
sl@0
|
109 |
{
|
sl@0
|
110 |
printf("inet_ntop failed");
|
sl@0
|
111 |
}
|
sl@0
|
112 |
else
|
sl@0
|
113 |
{
|
sl@0
|
114 |
printf("inet_ntop passed");
|
sl@0
|
115 |
}
|
sl@0
|
116 |
err=inet_aton(ipaddrstring,&ipstruct;);
|
sl@0
|
117 |
if(err==0)
|
sl@0
|
118 |
{
|
sl@0
|
119 |
printf("invalid address ");
|
sl@0
|
120 |
}
|
sl@0
|
121 |
else
|
sl@0
|
122 |
{
|
sl@0
|
123 |
printf("inet_aton passed ");
|
sl@0
|
124 |
}
|
sl@0
|
125 |
|
sl@0
|
126 |
return 0;
|
sl@0
|
127 |
}
|
sl@0
|
128 |
Output:
|
sl@0
|
129 |
inet_addr passed
|
sl@0
|
130 |
ipaddr is 1.2.3.4
|
sl@0
|
131 |
inet_pton passed
|
sl@0
|
132 |
inet_ntop passed
|
sl@0
|
133 |
inet_aton passed
|
sl@0
|
134 |
|
sl@0
|
135 |
@endcode
|
sl@0
|
136 |
The inet_ntop and inet_pton functions conform to -xns5.2. Note that inet_pton does not accept 1-, 2-, or 3-part dotted addresses; all four parts
|
sl@0
|
137 |
must be specified and are interpreted only as decimal values.
|
sl@0
|
138 |
This is a narrower input set than that accepted by inet_aton. These
|
sl@0
|
139 |
functions appeared in BSD 4.2.
|
sl@0
|
140 |
|
sl@0
|
141 |
Bugs:
|
sl@0
|
142 |
|
sl@0
|
143 |
The value INADDR_NONE (0xffffffff) is a valid broadcast address, but inet_addr cannot return that value without indicating failure.
|
sl@0
|
144 |
The newer inet_aton function does not share this problem.
|
sl@0
|
145 |
The problem of host byte ordering versus network byte ordering is
|
sl@0
|
146 |
confusing.
|
sl@0
|
147 |
The string returned by inet_ntoa resides in a static memory area. Inet_addr should return a struct in_addr.
|
sl@0
|
148 |
|
sl@0
|
149 |
|
sl@0
|
150 |
|
sl@0
|
151 |
@publishedAll
|
sl@0
|
152 |
@externallyDefinedApi
|
sl@0
|
153 |
*/
|
sl@0
|
154 |
|
sl@0
|
155 |
/** @fn inet_ntoa(struct in_addr in)
|
sl@0
|
156 |
@param in
|
sl@0
|
157 |
|
sl@0
|
158 |
Refer to inet_addr() for the documentation
|
sl@0
|
159 |
@see gethostbyname()
|
sl@0
|
160 |
|
sl@0
|
161 |
|
sl@0
|
162 |
|
sl@0
|
163 |
|
sl@0
|
164 |
@publishedAll
|
sl@0
|
165 |
@externallyDefinedApi
|
sl@0
|
166 |
*/
|
sl@0
|
167 |
|
sl@0
|
168 |
/** @fn inet_ntop( int af , const void * src, char * dst, socklen_t size)
|
sl@0
|
169 |
@param af
|
sl@0
|
170 |
@param src
|
sl@0
|
171 |
@param dst
|
sl@0
|
172 |
@param size
|
sl@0
|
173 |
|
sl@0
|
174 |
Refer to inet_addr() for the documentation
|
sl@0
|
175 |
|
sl@0
|
176 |
@see gethostbyname()
|
sl@0
|
177 |
|
sl@0
|
178 |
|
sl@0
|
179 |
|
sl@0
|
180 |
|
sl@0
|
181 |
@publishedAll
|
sl@0
|
182 |
@externallyDefinedApi
|
sl@0
|
183 |
*/
|
sl@0
|
184 |
|
sl@0
|
185 |
/** @fn inet_pton(int af, const char * src, void * dst)
|
sl@0
|
186 |
@param af
|
sl@0
|
187 |
@param src
|
sl@0
|
188 |
@param dst
|
sl@0
|
189 |
|
sl@0
|
190 |
Refer to inet_addr() for the documentation
|
sl@0
|
191 |
|
sl@0
|
192 |
@see gethostbyname()
|
sl@0
|
193 |
|
sl@0
|
194 |
|
sl@0
|
195 |
|
sl@0
|
196 |
|
sl@0
|
197 |
@publishedAll
|
sl@0
|
198 |
@externallyDefinedApi
|
sl@0
|
199 |
*/
|
sl@0
|
200 |
|
sl@0
|
201 |
/** @fn inet_aton(const char *cp, struct in_addr *pin)
|
sl@0
|
202 |
@param cp
|
sl@0
|
203 |
@param pin
|
sl@0
|
204 |
|
sl@0
|
205 |
Refer to inet_addr() for the documentation
|
sl@0
|
206 |
|
sl@0
|
207 |
@see gethostbyname()
|
sl@0
|
208 |
|
sl@0
|
209 |
|
sl@0
|
210 |
|
sl@0
|
211 |
|
sl@0
|
212 |
@publishedAll
|
sl@0
|
213 |
@externallyDefinedApi
|
sl@0
|
214 |
*/
|
sl@0
|
215 |
|
sl@0
|
216 |
/** @fn htonl(uint32_t hl)
|
sl@0
|
217 |
@param hl
|
sl@0
|
218 |
|
sl@0
|
219 |
Note: This description also covers the following functions -
|
sl@0
|
220 |
htons() ntohl() ntohs()
|
sl@0
|
221 |
|
sl@0
|
222 |
These routines convert 16 and 32 bit quantities between network
|
sl@0
|
223 |
byte order and host byte order.
|
sl@0
|
224 |
On machines which have a byte order which is the same as the network
|
sl@0
|
225 |
order, routines are defined as null macros.
|
sl@0
|
226 |
|
sl@0
|
227 |
These routines are most often used in conjunction with Internet
|
sl@0
|
228 |
addresses and ports as returned by gethostbyname
|
sl@0
|
229 |
and getservent .
|
sl@0
|
230 |
|
sl@0
|
231 |
@see gethostbyaddr()
|
sl@0
|
232 |
@see getservent()
|
sl@0
|
233 |
|
sl@0
|
234 |
|
sl@0
|
235 |
Bugs:
|
sl@0
|
236 |
|
sl@0
|
237 |
On the VAX bytes are handled backwards from most everyone else in
|
sl@0
|
238 |
the world.
|
sl@0
|
239 |
|
sl@0
|
240 |
|
sl@0
|
241 |
|
sl@0
|
242 |
@publishedAll
|
sl@0
|
243 |
@externallyDefinedApi
|
sl@0
|
244 |
*/
|
sl@0
|
245 |
|
sl@0
|
246 |
/** @fn htons(uint16_t hs)
|
sl@0
|
247 |
@param hs
|
sl@0
|
248 |
|
sl@0
|
249 |
Refer to htonl() for the documentation
|
sl@0
|
250 |
@see gethostbyaddr()
|
sl@0
|
251 |
@see getservent()
|
sl@0
|
252 |
|
sl@0
|
253 |
|
sl@0
|
254 |
|
sl@0
|
255 |
|
sl@0
|
256 |
@publishedAll
|
sl@0
|
257 |
@externallyDefinedApi
|
sl@0
|
258 |
*/
|
sl@0
|
259 |
|
sl@0
|
260 |
/** @def ntohl
|
sl@0
|
261 |
|
sl@0
|
262 |
These are also declared as functions.
|
sl@0
|
263 |
|
sl@0
|
264 |
@publishedAll
|
sl@0
|
265 |
@externallyDefinedApi
|
sl@0
|
266 |
*/
|
sl@0
|
267 |
|
sl@0
|
268 |
/** @def ntohs
|
sl@0
|
269 |
|
sl@0
|
270 |
These are also declared as functions.
|
sl@0
|
271 |
|
sl@0
|
272 |
@publishedAll
|
sl@0
|
273 |
@externallyDefinedApi
|
sl@0
|
274 |
*/
|
sl@0
|
275 |
|
sl@0
|
276 |
/** @def inet_addr
|
sl@0
|
277 |
|
sl@0
|
278 |
These are also declared as functions.
|
sl@0
|
279 |
|
sl@0
|
280 |
@publishedAll
|
sl@0
|
281 |
@externallyDefinedApi
|
sl@0
|
282 |
*/
|
sl@0
|
283 |
|
sl@0
|
284 |
|
sl@0
|
285 |
/** @def inet_ntoa
|
sl@0
|
286 |
|
sl@0
|
287 |
These are also declared as functions.
|
sl@0
|
288 |
|
sl@0
|
289 |
@publishedAll
|
sl@0
|
290 |
@externallyDefinedApi
|
sl@0
|
291 |
*/
|
sl@0
|
292 |
|
sl@0
|
293 |
|
sl@0
|
294 |
/** @def inet_pton
|
sl@0
|
295 |
|
sl@0
|
296 |
These are also declared as functions.
|
sl@0
|
297 |
|
sl@0
|
298 |
@publishedAll
|
sl@0
|
299 |
@externallyDefinedApi
|
sl@0
|
300 |
*/
|
sl@0
|
301 |
|
sl@0
|
302 |
|
sl@0
|
303 |
/** @def inet_ntop
|
sl@0
|
304 |
|
sl@0
|
305 |
These are also declared as functions.
|
sl@0
|
306 |
|
sl@0
|
307 |
@publishedAll
|
sl@0
|
308 |
@externallyDefinedApi
|
sl@0
|
309 |
*/
|
sl@0
|
310 |
|
sl@0
|
311 |
|