]> git.saurik.com Git - apple/xnu.git/blame - bsd/libkern/inet_ntop.c
xnu-2050.7.9.tar.gz
[apple/xnu.git] / bsd / libkern / inet_ntop.c
CommitLineData
91447636 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
91447636 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
91447636
A
27 */
28
29/*
30 * Copyright 1994, 1995 Massachusetts Institute of Technology
31 *
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
42 * warranty.
43 *
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
55 * SUCH DAMAGE.
56 */
57
58#include <sys/param.h>
59#include <sys/systm.h>
60
61#include <netinet/in.h>
62
63static const char *hexchars = "0123456789abcdef";
64
65static const char *
2d21ac55 66inet_ntop4(const struct in_addr *addr, char *buf, socklen_t len)
91447636
A
67{
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 */
70 int fulllen;
71
72 /*
73 * snprintf returns number of bytes printed (not including NULL) or
74 * number of bytes that would have been printed if more than would
75 * fit
76 */
77 fulllen = snprintf(tmp, sizeof(tmp), "%d.%d.%d.%d",
78 ap[0], ap[1], ap[2], ap[3]);
79 if (fulllen >= (int)len) {
80 return NULL;
81 }
82
83 bcopy(tmp, buf, fulllen + 1);
84
85 return buf;
86}
87
88static const char *
2d21ac55 89inet_ntop6(const struct in6_addr *addr, char *dst, socklen_t size)
91447636
A
90{
91 char hexa[8][5], tmp[MAX_IPv6_STR_LEN];
92 int zr[8];
93 size_t len;
94 int32_t i, j, k, skip;
95 uint8_t x8, hx8;
96 uint16_t x16;
97 struct in_addr a4;
98
99 if (addr == NULL) return NULL;
100
101 bzero(tmp, sizeof(tmp));
102
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))
107 {
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);
114 return dst;
115 }
116
117 k = 0;
118 for (i = 0; i < 16; i += 2)
119 {
120 j = 0;
121 skip = 1;
122
123 bzero(hexa[k], 5);
124
125 x8 = addr->__u6_addr.__u6_addr8[i];
126
127 hx8 = x8 >> 4;
128 if (hx8 != 0)
129 {
130 skip = 0;
131 hexa[k][j++] = hexchars[hx8];
132 }
133
134 hx8 = x8 & 0x0f;
135 if ((skip == 0) || ((skip == 1) && (hx8 != 0)))
136 {
137 skip = 0;
138 hexa[k][j++] = hexchars[hx8];
139 }
140
141 x8 = addr->__u6_addr.__u6_addr8[i + 1];
142
143 hx8 = x8 >> 4;
144 if ((skip == 0) || ((skip == 1) && (hx8 != 0)))
145 {
146 hexa[k][j++] = hexchars[hx8];
147 }
148
149 hx8 = x8 & 0x0f;
150 hexa[k][j++] = hexchars[hx8];
151
152 k++;
153 }
154
155 /* find runs of zeros for :: convention */
156 j = 0;
157 for (i = 7; i >= 0; i--)
158 {
159 zr[i] = j;
160 x16 = addr->__u6_addr.__u6_addr16[i];
161 if (x16 == 0) j++;
162 else j = 0;
163 zr[i] = j;
164 }
165
166 /* find longest run of zeros */
167 k = -1;
168 j = 0;
169 for(i = 0; i < 8; i++)
170 {
171 if (zr[i] > j)
172 {
173 k = i;
174 j = zr[i];
175 }
176 }
177
178 for(i = 0; i < 8; i++)
179 {
180 if (i != k) zr[i] = 0;
181 }
182
183 len = 0;
184 for (i = 0; i < 8; i++)
185 {
186 if (zr[i] != 0)
187 {
188 /* check for leading zero */
189 if (i == 0) tmp[len++] = ':';
190 tmp[len++] = ':';
191 i += (zr[i] - 1);
192 continue;
193 }
194 for (j = 0; hexa[i][j] != '\0'; j++) tmp[len++] = hexa[i][j];
195 if (i != 7) tmp[len++] = ':';
196 }
197
198 /* trailing NULL */
199 len++;
200
201 if (len > size) return NULL;
202 bcopy(tmp, dst, len);
203 return dst;
204}
205
206const char *
2d21ac55 207inet_ntop(int af, const void *addr, char *buf, socklen_t len)
91447636
A
208{
209 if(af==AF_INET6)
210 return inet_ntop6(addr, buf, len);
211 if(af==AF_INET)
212 return inet_ntop4(addr, buf, len);
213 return NULL;
214}