annotate lib/inet_ntop.c @ 6259:96c32553b4c6

Use a consistent style for including <config.h>. * __fpending.c, acl.c, argmatch.c, argp-help.c, argp-parse.c, argp-pvh.c, backupfile.c, basename.c, c-stack.c, calloc.c, check-version.c, cloexec.c, closeout.c, copy-file.c, creat-safer.c, cycle-check.c, dirfd.c, dirname.c, dup-safer.c, dup2.c, euidaccess.c, exclude.c, exitfail.c, fatal-signal.c, fd-safer.c, file-type.c, fileblocks.c, filemode.c, filenamecat.c, findprog.c, fnmatch.c, fopen-safer.c, free.c, fsusage.c, ftruncate.c, full-write.c, fwriteerror.c, getaddrinfo.c, getcwd.c, getdelim.c, getline.c, getlogin_r.c, getndelim2.c, getnline.c, getopt1.c, getpass.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c, human.c, idcache.c, inet_ntop.c, isdir.c, long-options.c, malloc.c, memcasecmp.c, memcmp.c, memcoll.c, memcpy.c, memmove.c, mkdir-p.c, modechange.c, mountlist.c, open-safer.c, physmem.c, pipe-safer.c, pipe.c, poll.c, posixver.c, progname.c, progreloc.c, putenv.c, quote.c, quotearg.c, readline.c, readlink.c, realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c, same.c, save-cwd.c, savedir.c, sig2str.c, strcspn.c, strerror.c, stripslash.c, strncasecmp.c, strndup.c, strnlen.c, strnlen1.c, strsep.c, strstr.c, strtod.c, strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c, userspec.c, utimecmp.c, version-etc-fsf.c, version-etc.c, wait-process.c, xalloc-die.c, xgetcwd.c, xmalloc.c, xmemcoll.c, xnanosleep.c, xreadlink.c, xsetenv.c, xstrndup.c, xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c: Standardize inclusion of config.h. * __fpending.h, dirfd.h, getdate.h, human.h, inttostr.h: Removed inclusion of config.h from header files. * inttostr.c: Adjusted in-tree users. * timespec.h: Remove superfluous warning to include config.h. * atexit.c, chdir-long.c chown.c, fchown-stub.c, getgroups.c, gettimeofday.c, lchown.c, lstat.c, mkdir.c, mkstemp.c, nanosleep.c, openat.c, raise.c, readtokens0.c, readutmp.c, unlinkdir.c: Guard inclusion of config.h with HAVE_CONFIG_H.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 19 Sep 2005 17:28:14 +0000
parents 96441be32be1
children be00741570b7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /*
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 * Copyright (c) 1996-1999 by Internet Software Consortium.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 * Copyright (c) 2005 Free Software Foundation, Inc.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 * Permission to use, copy, modify, and distribute this software for any
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 * purpose with or without fee is hereby granted, provided that the above
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 * copyright notice and this permission notice appear in all copies.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 * SOFTWARE.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
6259
96c32553b4c6 Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5825
diff changeset
19 #ifdef HAVE_CONFIG_H
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 # include <config.h>
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #endif
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 /* Specification. */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include "inet_ntop.h"
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <stdio.h>
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <string.h>
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <errno.h>
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #define NS_IN6ADDRSZ 16
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #define NS_INT16SZ 2
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 /*
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 * WARNING: Don't even consider trying to compile this on a system where
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 typedef int verify_int_size[2 * sizeof(int) - 7];
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #if HAVE_IPV4
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #endif
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #if HAVE_IPV6
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #endif
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 /* char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 * inet_ntop(af, src, dst, size)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 * convert a network format address to presentation format.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 * return:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 * pointer to presentation format address (`dst'), or NULL (see errno).
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 * author:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 * Paul Vixie, 1996.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 const char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 inet_ntop(af, src, dst, size)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 int af;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 const void *src;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 char *dst;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 socklen_t size;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 switch (af) {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 #if HAVE_IPV4
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 case AF_INET:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 return (inet_ntop4(src, dst, size));
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 #endif
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 #if HAVE_IPV6
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 case AF_INET6:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 return (inet_ntop6(src, dst, size));
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 #endif
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 default:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 errno = EAFNOSUPPORT;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 return (NULL);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 /* NOTREACHED */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 #if HAVE_IPV4
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 /* const char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 * inet_ntop4(src, dst, size)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 * format an IPv4 address
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 * return:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 * `dst' (as a const)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 * notes:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 * (1) uses no statics
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 * (2) takes a u_char* not an in_addr as input
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 * author:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 * Paul Vixie, 1996.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 static const char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 inet_ntop4(src, dst, size)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 const unsigned char *src;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 char *dst;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 socklen_t size;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 static const char fmt[] = "%u.%u.%u.%u";
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 char tmp[sizeof "255.255.255.255"];
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 if (sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) > size) {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 errno = ENOSPC;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 return (NULL);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 return strcpy(dst, tmp);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 #endif
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 #if HAVE_IPV6
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 /* const char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 * inet_ntop6(src, dst, size)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 * convert IPv6 binary address into presentation (printable) format
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 * author:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 * Paul Vixie, 1996.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 static const char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 inet_ntop6(src, dst, size)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 const unsigned char *src;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 char *dst;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 socklen_t size;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 /*
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 * Note that int32_t and int16_t need only be "at least" large enough
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 * to contain a value of the specified size. On some systems, like
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 * Crays, there is no such thing as an integer variable with 16 bits.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 * Keep this in mind if you think this function should have been coded
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 * to use pointer overlays. All the world's not a VAX.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 struct { int base, len; } best, cur;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 int i;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 /*
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 * Preprocess:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 * Copy the input (bytewise) array into a wordwise array.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 * Find the longest run of 0x00's in src[] for :: shorthanding.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 memset(words, '\0', sizeof words);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 for (i = 0; i < NS_IN6ADDRSZ; i += 2)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 words[i / 2] = (src[i] << 8) | src[i + 1];
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 best.base = -1;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 cur.base = -1;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 if (words[i] == 0) {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 if (cur.base == -1)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 cur.base = i, cur.len = 1;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 else
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 cur.len++;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 } else {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 if (cur.base != -1) {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 if (best.base == -1 || cur.len > best.len)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 best = cur;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 cur.base = -1;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 if (cur.base != -1) {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 if (best.base == -1 || cur.len > best.len)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 best = cur;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 if (best.base != -1 && best.len < 2)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 best.base = -1;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 /*
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 * Format the result.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 tp = tmp;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 /* Are we inside the best run of 0x00's? */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 if (best.base != -1 && i >= best.base &&
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 i < (best.base + best.len)) {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 if (i == best.base)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 *tp++ = ':';
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 continue;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 /* Are we following an initial run of 0x00s or any real hex? */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 if (i != 0)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 *tp++ = ':';
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 /* Is this address an encapsulated IPv4? */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 if (i == 6 && best.base == 0 &&
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 return (NULL);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 tp += strlen(tp);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 break;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 tp += sprintf(tp, "%x", words[i]);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 /* Was it a trailing run of 0x00's? */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 if (best.base != -1 && (best.base + best.len) ==
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 (NS_IN6ADDRSZ / NS_INT16SZ))
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 *tp++ = ':';
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 *tp++ = '\0';
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 /*
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 * Check for overflow, copy, and we're done.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 if ((socklen_t)(tp - tmp) > size) {
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 errno = ENOSPC;
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 return (NULL);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 return strcpy(dst, tmp);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 #endif