]>
git.saurik.com Git - apple/xnu.git/blob - bsd/libkern/inet_ntop.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
24 * Copyright 1994, 1995 Massachusetts Institute of Technology
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
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
52 #include <sys/param.h>
53 #include <sys/systm.h>
55 #include <netinet/in.h>
57 static const char *hexchars
= "0123456789abcdef";
60 inet_ntop4(const struct in_addr
*addr
, char *buf
, size_t len
)
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 */
67 * snprintf returns number of bytes printed (not including NULL) or
68 * number of bytes that would have been printed if more than would
71 fulllen
= snprintf(tmp
, sizeof(tmp
), "%d.%d.%d.%d",
72 ap
[0], ap
[1], ap
[2], ap
[3]);
73 if (fulllen
>= (int)len
) {
77 bcopy(tmp
, buf
, fulllen
+ 1);
83 inet_ntop6(const struct in6_addr
*addr
, char *dst
, size_t size
)
85 char hexa
[8][5], tmp
[MAX_IPv6_STR_LEN
];
88 int32_t i
, j
, k
, skip
;
93 if (addr
== NULL
) return NULL
;
95 bzero(tmp
, sizeof(tmp
));
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))
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);
112 for (i
= 0; i
< 16; i
+= 2)
119 x8
= addr
->__u6_addr
.__u6_addr8
[i
];
125 hexa
[k
][j
++] = hexchars
[hx8
];
129 if ((skip
== 0) || ((skip
== 1) && (hx8
!= 0)))
132 hexa
[k
][j
++] = hexchars
[hx8
];
135 x8
= addr
->__u6_addr
.__u6_addr8
[i
+ 1];
138 if ((skip
== 0) || ((skip
== 1) && (hx8
!= 0)))
140 hexa
[k
][j
++] = hexchars
[hx8
];
144 hexa
[k
][j
++] = hexchars
[hx8
];
149 /* find runs of zeros for :: convention */
151 for (i
= 7; i
>= 0; i
--)
154 x16
= addr
->__u6_addr
.__u6_addr16
[i
];
160 /* find longest run of zeros */
163 for(i
= 0; i
< 8; i
++)
172 for(i
= 0; i
< 8; i
++)
174 if (i
!= k
) zr
[i
] = 0;
178 for (i
= 0; i
< 8; i
++)
182 /* check for leading zero */
183 if (i
== 0) tmp
[len
++] = ':';
188 for (j
= 0; hexa
[i
][j
] != '\0'; j
++) tmp
[len
++] = hexa
[i
][j
];
189 if (i
!= 7) tmp
[len
++] = ':';
195 if (len
> size
) return NULL
;
196 bcopy(tmp
, dst
, len
);
201 inet_ntop(int af
, const void *addr
, char *buf
, size_t len
)
204 return inet_ntop6(addr
, buf
, len
);
206 return inet_ntop4(addr
, buf
, len
);