]>
git.saurik.com Git - apple/xnu.git/blob - bsd/libkern/inet_ntop.c
a41cd2ba9e12d42bd129c8d5d2ba389970779f26
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Copyright 1994, 1995 Massachusetts Institute of Technology
27 * Permission to use, copy, modify, and distribute this software and
28 * its documentation for any purpose and without fee is hereby
29 * granted, provided that both the above copyright notice and this
30 * permission notice appear in all copies, that both the above
31 * copyright notice and this permission notice appear in all
32 * supporting documentation, and that the name of M.I.T. not be used
33 * in advertising or publicity pertaining to distribution of the
34 * software without specific, written prior permission. M.I.T. makes
35 * no representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied
39 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
40 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
41 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
43 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 #include <sys/param.h>
54 #include <sys/systm.h>
56 #include <netinet/in.h>
58 static const char *hexchars
= "0123456789abcdef";
61 inet_ntop4(const struct in_addr
*addr
, char *buf
, size_t len
)
63 const u_int8_t
*ap
= (const u_int8_t
*)&addr
->s_addr
;
64 char tmp
[MAX_IPv4_STR_LEN
]; /* max length of ipv4 addr string */
68 * snprintf returns number of bytes printed (not including NULL) or
69 * number of bytes that would have been printed if more than would
72 fulllen
= snprintf(tmp
, sizeof(tmp
), "%d.%d.%d.%d",
73 ap
[0], ap
[1], ap
[2], ap
[3]);
74 if (fulllen
>= (int)len
) {
78 bcopy(tmp
, buf
, fulllen
+ 1);
84 inet_ntop6(const struct in6_addr
*addr
, char *dst
, size_t size
)
86 char hexa
[8][5], tmp
[MAX_IPv6_STR_LEN
];
89 int32_t i
, j
, k
, skip
;
94 if (addr
== NULL
) return NULL
;
96 bzero(tmp
, sizeof(tmp
));
98 /* check for mapped or compat addresses */
99 i
= IN6_IS_ADDR_V4MAPPED(addr
);
100 j
= IN6_IS_ADDR_V4COMPAT(addr
);
101 if ((i
!= 0) || (j
!= 0))
103 char tmp2
[16]; /* max length of ipv4 addr string */
104 a4
.s_addr
= addr
->__u6_addr
.__u6_addr32
[3];
105 len
= snprintf(tmp
, sizeof(tmp
), "::%s%s", (i
!= 0) ? "ffff:" : "",
106 inet_ntop4(&a4
, tmp2
, sizeof(tmp2
)));
107 if (len
>= size
) return NULL
;
108 bcopy(tmp
, dst
, len
+ 1);
113 for (i
= 0; i
< 16; i
+= 2)
120 x8
= addr
->__u6_addr
.__u6_addr8
[i
];
126 hexa
[k
][j
++] = hexchars
[hx8
];
130 if ((skip
== 0) || ((skip
== 1) && (hx8
!= 0)))
133 hexa
[k
][j
++] = hexchars
[hx8
];
136 x8
= addr
->__u6_addr
.__u6_addr8
[i
+ 1];
139 if ((skip
== 0) || ((skip
== 1) && (hx8
!= 0)))
141 hexa
[k
][j
++] = hexchars
[hx8
];
145 hexa
[k
][j
++] = hexchars
[hx8
];
150 /* find runs of zeros for :: convention */
152 for (i
= 7; i
>= 0; i
--)
155 x16
= addr
->__u6_addr
.__u6_addr16
[i
];
161 /* find longest run of zeros */
164 for(i
= 0; i
< 8; i
++)
173 for(i
= 0; i
< 8; i
++)
175 if (i
!= k
) zr
[i
] = 0;
179 for (i
= 0; i
< 8; i
++)
183 /* check for leading zero */
184 if (i
== 0) tmp
[len
++] = ':';
189 for (j
= 0; hexa
[i
][j
] != '\0'; j
++) tmp
[len
++] = hexa
[i
][j
];
190 if (i
!= 7) tmp
[len
++] = ':';
196 if (len
> size
) return NULL
;
197 bcopy(tmp
, dst
, len
);
202 inet_ntop(int af
, const void *addr
, char *buf
, size_t len
)
205 return inet_ntop6(addr
, buf
, len
);
207 return inet_ntop4(addr
, buf
, len
);