]>
Commit | Line | Data |
---|---|---|
91447636 A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * The contents of this file constitute Original Code as defined in and | |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
11 | * | |
12 | * This Original Code and all software distributed under the License are | |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | ||
23 | /* | |
24 | * Copyright 1994, 1995 Massachusetts Institute of Technology | |
25 | * | |
26 | * Permission to use, copy, modify, and distribute this software and | |
27 | * its documentation for any purpose and without fee is hereby | |
28 | * granted, provided that both the above copyright notice and this | |
29 | * permission notice appear in all copies, that both the above | |
30 | * copyright notice and this permission notice appear in all | |
31 | * supporting documentation, and that the name of M.I.T. not be used | |
32 | * in advertising or publicity pertaining to distribution of the | |
33 | * software without specific, written prior permission. M.I.T. makes | |
34 | * no representations about the suitability of this software for any | |
35 | * purpose. It is provided "as is" without express or implied | |
36 | * warranty. | |
37 | * | |
38 | * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS | |
39 | * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
40 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
41 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT | |
42 | * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
44 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |
45 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
46 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
47 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
48 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
49 | * SUCH DAMAGE. | |
50 | */ | |
51 | ||
52 | #include <sys/param.h> | |
53 | #include <sys/systm.h> | |
54 | ||
55 | #include <netinet/in.h> | |
56 | ||
57 | static const char *hexchars = "0123456789abcdef"; | |
58 | ||
59 | static const char * | |
60 | inet_ntop4(const struct in_addr *addr, char *buf, size_t len) | |
61 | { | |
62 | const u_int8_t *ap = (const u_int8_t *)&addr->s_addr; | |
63 | char tmp[MAX_IPv4_STR_LEN]; /* max length of ipv4 addr string */ | |
64 | int fulllen; | |
65 | ||
66 | /* | |
67 | * snprintf returns number of bytes printed (not including NULL) or | |
68 | * number of bytes that would have been printed if more than would | |
69 | * fit | |
70 | */ | |
71 | fulllen = snprintf(tmp, sizeof(tmp), "%d.%d.%d.%d", | |
72 | ap[0], ap[1], ap[2], ap[3]); | |
73 | if (fulllen >= (int)len) { | |
74 | return NULL; | |
75 | } | |
76 | ||
77 | bcopy(tmp, buf, fulllen + 1); | |
78 | ||
79 | return buf; | |
80 | } | |
81 | ||
82 | static const char * | |
83 | inet_ntop6(const struct in6_addr *addr, char *dst, size_t size) | |
84 | { | |
85 | char hexa[8][5], tmp[MAX_IPv6_STR_LEN]; | |
86 | int zr[8]; | |
87 | size_t len; | |
88 | int32_t i, j, k, skip; | |
89 | uint8_t x8, hx8; | |
90 | uint16_t x16; | |
91 | struct in_addr a4; | |
92 | ||
93 | if (addr == NULL) return NULL; | |
94 | ||
95 | bzero(tmp, sizeof(tmp)); | |
96 | ||
97 | /* check for mapped or compat addresses */ | |
98 | i = IN6_IS_ADDR_V4MAPPED(addr); | |
99 | j = IN6_IS_ADDR_V4COMPAT(addr); | |
100 | if ((i != 0) || (j != 0)) | |
101 | { | |
102 | char tmp2[16]; /* max length of ipv4 addr string */ | |
103 | a4.s_addr = addr->__u6_addr.__u6_addr32[3]; | |
104 | len = snprintf(tmp, sizeof(tmp), "::%s%s", (i != 0) ? "ffff:" : "", | |
105 | inet_ntop4(&a4, tmp2, sizeof(tmp2))); | |
106 | if (len >= size) return NULL; | |
107 | bcopy(tmp, dst, len + 1); | |
108 | return dst; | |
109 | } | |
110 | ||
111 | k = 0; | |
112 | for (i = 0; i < 16; i += 2) | |
113 | { | |
114 | j = 0; | |
115 | skip = 1; | |
116 | ||
117 | bzero(hexa[k], 5); | |
118 | ||
119 | x8 = addr->__u6_addr.__u6_addr8[i]; | |
120 | ||
121 | hx8 = x8 >> 4; | |
122 | if (hx8 != 0) | |
123 | { | |
124 | skip = 0; | |
125 | hexa[k][j++] = hexchars[hx8]; | |
126 | } | |
127 | ||
128 | hx8 = x8 & 0x0f; | |
129 | if ((skip == 0) || ((skip == 1) && (hx8 != 0))) | |
130 | { | |
131 | skip = 0; | |
132 | hexa[k][j++] = hexchars[hx8]; | |
133 | } | |
134 | ||
135 | x8 = addr->__u6_addr.__u6_addr8[i + 1]; | |
136 | ||
137 | hx8 = x8 >> 4; | |
138 | if ((skip == 0) || ((skip == 1) && (hx8 != 0))) | |
139 | { | |
140 | hexa[k][j++] = hexchars[hx8]; | |
141 | } | |
142 | ||
143 | hx8 = x8 & 0x0f; | |
144 | hexa[k][j++] = hexchars[hx8]; | |
145 | ||
146 | k++; | |
147 | } | |
148 | ||
149 | /* find runs of zeros for :: convention */ | |
150 | j = 0; | |
151 | for (i = 7; i >= 0; i--) | |
152 | { | |
153 | zr[i] = j; | |
154 | x16 = addr->__u6_addr.__u6_addr16[i]; | |
155 | if (x16 == 0) j++; | |
156 | else j = 0; | |
157 | zr[i] = j; | |
158 | } | |
159 | ||
160 | /* find longest run of zeros */ | |
161 | k = -1; | |
162 | j = 0; | |
163 | for(i = 0; i < 8; i++) | |
164 | { | |
165 | if (zr[i] > j) | |
166 | { | |
167 | k = i; | |
168 | j = zr[i]; | |
169 | } | |
170 | } | |
171 | ||
172 | for(i = 0; i < 8; i++) | |
173 | { | |
174 | if (i != k) zr[i] = 0; | |
175 | } | |
176 | ||
177 | len = 0; | |
178 | for (i = 0; i < 8; i++) | |
179 | { | |
180 | if (zr[i] != 0) | |
181 | { | |
182 | /* check for leading zero */ | |
183 | if (i == 0) tmp[len++] = ':'; | |
184 | tmp[len++] = ':'; | |
185 | i += (zr[i] - 1); | |
186 | continue; | |
187 | } | |
188 | for (j = 0; hexa[i][j] != '\0'; j++) tmp[len++] = hexa[i][j]; | |
189 | if (i != 7) tmp[len++] = ':'; | |
190 | } | |
191 | ||
192 | /* trailing NULL */ | |
193 | len++; | |
194 | ||
195 | if (len > size) return NULL; | |
196 | bcopy(tmp, dst, len); | |
197 | return dst; | |
198 | } | |
199 | ||
200 | const char * | |
201 | inet_ntop(int af, const void *addr, char *buf, size_t len) | |
202 | { | |
203 | if(af==AF_INET6) | |
204 | return inet_ntop6(addr, buf, len); | |
205 | if(af==AF_INET) | |
206 | return inet_ntop4(addr, buf, len); | |
207 | return NULL; | |
208 | } |