]>
git.saurik.com Git - apple/xnu.git/blob - bsd/libkern/inet_ntop.c
dc07273181e745d499cacb5f820b9968e5546e68
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright 1994, 1995 Massachusetts Institute of Technology
32 * Permission to use, copy, modify, and distribute this software and
33 * its documentation for any purpose and without fee is hereby
34 * granted, provided that both the above copyright notice and this
35 * permission notice appear in all copies, that both the above
36 * copyright notice and this permission notice appear in all
37 * supporting documentation, and that the name of M.I.T. not be used
38 * in advertising or publicity pertaining to distribution of the
39 * software without specific, written prior permission. M.I.T. makes
40 * no representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied
44 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
45 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
46 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
47 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
48 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/param.h>
59 #include <sys/systm.h>
61 #include <netinet/in.h>
63 static const char *hexchars
= "0123456789abcdef";
66 inet_ntop4(const struct in_addr
*addr
, char *buf
, socklen_t len
)
68 const u_int8_t
*ap
= (const u_int8_t
*)&addr
->s_addr
;
69 char tmp
[MAX_IPv4_STR_LEN
]; /* max length of ipv4 addr string */
73 * snprintf returns number of bytes printed (not including NULL) or
74 * number of bytes that would have been printed if more than would
77 fulllen
= snprintf(tmp
, sizeof(tmp
), "%d.%d.%d.%d",
78 ap
[0], ap
[1], ap
[2], ap
[3]);
79 if (fulllen
>= (int)len
) {
83 bcopy(tmp
, buf
, fulllen
+ 1);
89 inet_ntop6(const struct in6_addr
*addr
, char *dst
, socklen_t size
)
91 char hexa
[8][5], tmp
[MAX_IPv6_STR_LEN
];
94 int32_t i
, j
, k
, skip
;
99 if (addr
== NULL
) return NULL
;
101 bzero(tmp
, sizeof(tmp
));
103 /* check for mapped or compat addresses */
104 i
= IN6_IS_ADDR_V4MAPPED(addr
);
105 j
= IN6_IS_ADDR_V4COMPAT(addr
);
106 if ((i
!= 0) || (j
!= 0))
108 char tmp2
[16]; /* max length of ipv4 addr string */
109 a4
.s_addr
= addr
->__u6_addr
.__u6_addr32
[3];
110 len
= snprintf(tmp
, sizeof(tmp
), "::%s%s", (i
!= 0) ? "ffff:" : "",
111 inet_ntop4(&a4
, tmp2
, sizeof(tmp2
)));
112 if (len
>= size
) return NULL
;
113 bcopy(tmp
, dst
, len
+ 1);
118 for (i
= 0; i
< 16; i
+= 2)
125 x8
= addr
->__u6_addr
.__u6_addr8
[i
];
131 hexa
[k
][j
++] = hexchars
[hx8
];
135 if ((skip
== 0) || ((skip
== 1) && (hx8
!= 0)))
138 hexa
[k
][j
++] = hexchars
[hx8
];
141 x8
= addr
->__u6_addr
.__u6_addr8
[i
+ 1];
144 if ((skip
== 0) || ((skip
== 1) && (hx8
!= 0)))
146 hexa
[k
][j
++] = hexchars
[hx8
];
150 hexa
[k
][j
++] = hexchars
[hx8
];
155 /* find runs of zeros for :: convention */
157 for (i
= 7; i
>= 0; i
--)
160 x16
= addr
->__u6_addr
.__u6_addr16
[i
];
166 /* find longest run of zeros */
169 for(i
= 0; i
< 8; i
++)
178 for(i
= 0; i
< 8; i
++)
180 if (i
!= k
) zr
[i
] = 0;
184 for (i
= 0; i
< 8; i
++)
188 /* check for leading zero */
189 if (i
== 0) tmp
[len
++] = ':';
194 for (j
= 0; hexa
[i
][j
] != '\0'; j
++) tmp
[len
++] = hexa
[i
][j
];
195 if (i
!= 7) tmp
[len
++] = ':';
201 if (len
> size
) return NULL
;
202 bcopy(tmp
, dst
, len
);
207 inet_ntop(int af
, const void *addr
, char *buf
, socklen_t len
)
210 return inet_ntop6(addr
, buf
, len
);
212 return inet_ntop4(addr
, buf
, len
);