summaryrefslogtreecommitdiffstats
path: root/gl/inet_ntop.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/inet_ntop.c')
-rw-r--r--gl/inet_ntop.c92
1 files changed, 44 insertions, 48 deletions
diff --git a/gl/inet_ntop.c b/gl/inet_ntop.c
index be091ff9..baaa23f8 100644
--- a/gl/inet_ntop.c
+++ b/gl/inet_ntop.c
@@ -1,6 +1,6 @@
1/* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form 1/* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form
2 2
3 Copyright (C) 2005, 2006, 2008 Free Software Foundation, Inc. 3 Copyright (C) 2005-2006, 2008-2010 Free Software Foundation, Inc.
4 4
5 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
@@ -42,10 +42,6 @@
42#include <string.h> 42#include <string.h>
43#include <errno.h> 43#include <errno.h>
44 44
45#ifndef EAFNOSUPPORT
46# define EAFNOSUPPORT EINVAL
47#endif
48
49#define NS_IN6ADDRSZ 16 45#define NS_IN6ADDRSZ 16
50#define NS_INT16SZ 2 46#define NS_INT16SZ 2
51 47
@@ -63,15 +59,15 @@ static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t si
63 59
64/* char * 60/* char *
65 * inet_ntop(af, src, dst, size) 61 * inet_ntop(af, src, dst, size)
66 * convert a network format address to presentation format. 62 * convert a network format address to presentation format.
67 * return: 63 * return:
68 * pointer to presentation format address (`dst'), or NULL (see errno). 64 * pointer to presentation format address (`dst'), or NULL (see errno).
69 * author: 65 * author:
70 * Paul Vixie, 1996. 66 * Paul Vixie, 1996.
71 */ 67 */
72const char * 68const char *
73inet_ntop (int af, const void *restrict src, 69inet_ntop (int af, const void *restrict src,
74 char *restrict dst, socklen_t cnt) 70 char *restrict dst, socklen_t cnt)
75{ 71{
76 switch (af) 72 switch (af)
77 { 73 {
@@ -94,14 +90,14 @@ inet_ntop (int af, const void *restrict src,
94 90
95/* const char * 91/* const char *
96 * inet_ntop4(src, dst, size) 92 * inet_ntop4(src, dst, size)
97 * format an IPv4 address 93 * format an IPv4 address
98 * return: 94 * return:
99 * `dst' (as a const) 95 * `dst' (as a const)
100 * notes: 96 * notes:
101 * (1) uses no statics 97 * (1) uses no statics
102 * (2) takes a u_char* not an in_addr as input 98 * (2) takes a u_char* not an in_addr as input
103 * author: 99 * author:
104 * Paul Vixie, 1996. 100 * Paul Vixie, 1996.
105 */ 101 */
106static const char * 102static const char *
107inet_ntop4 (const unsigned char *src, char *dst, socklen_t size) 103inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
@@ -126,9 +122,9 @@ inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
126 122
127/* const char * 123/* const char *
128 * inet_ntop6(src, dst, size) 124 * inet_ntop6(src, dst, size)
129 * convert IPv6 binary address into presentation (printable) format 125 * convert IPv6 binary address into presentation (printable) format
130 * author: 126 * author:
131 * Paul Vixie, 1996. 127 * Paul Vixie, 1996.
132 */ 128 */
133static const char * 129static const char *
134inet_ntop6 (const unsigned char *src, char *dst, socklen_t size) 130inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
@@ -161,26 +157,26 @@ inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
161 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) 157 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
162 { 158 {
163 if (words[i] == 0) 159 if (words[i] == 0)
164 { 160 {
165 if (cur.base == -1) 161 if (cur.base == -1)
166 cur.base = i, cur.len = 1; 162 cur.base = i, cur.len = 1;
167 else 163 else
168 cur.len++; 164 cur.len++;
169 } 165 }
170 else 166 else
171 { 167 {
172 if (cur.base != -1) 168 if (cur.base != -1)
173 { 169 {
174 if (best.base == -1 || cur.len > best.len) 170 if (best.base == -1 || cur.len > best.len)
175 best = cur; 171 best = cur;
176 cur.base = -1; 172 cur.base = -1;
177 } 173 }
178 } 174 }
179 } 175 }
180 if (cur.base != -1) 176 if (cur.base != -1)
181 { 177 {
182 if (best.base == -1 || cur.len > best.len) 178 if (best.base == -1 || cur.len > best.len)
183 best = cur; 179 best = cur;
184 } 180 }
185 if (best.base != -1 && best.len < 2) 181 if (best.base != -1 && best.len < 2)
186 best.base = -1; 182 best.base = -1;
@@ -193,28 +189,28 @@ inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
193 { 189 {
194 /* Are we inside the best run of 0x00's? */ 190 /* Are we inside the best run of 0x00's? */
195 if (best.base != -1 && i >= best.base && i < (best.base + best.len)) 191 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
196 { 192 {
197 if (i == best.base) 193 if (i == best.base)
198 *tp++ = ':'; 194 *tp++ = ':';
199 continue; 195 continue;
200 } 196 }
201 /* Are we following an initial run of 0x00s or any real hex? */ 197 /* Are we following an initial run of 0x00s or any real hex? */
202 if (i != 0) 198 if (i != 0)
203 *tp++ = ':'; 199 *tp++ = ':';
204 /* Is this address an encapsulated IPv4? */ 200 /* Is this address an encapsulated IPv4? */
205 if (i == 6 && best.base == 0 && 201 if (i == 6 && best.base == 0 &&
206 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) 202 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
207 { 203 {
208 if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp))) 204 if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
209 return (NULL); 205 return (NULL);
210 tp += strlen (tp); 206 tp += strlen (tp);
211 break; 207 break;
212 } 208 }
213 { 209 {
214 int len = sprintf (tp, "%x", words[i]); 210 int len = sprintf (tp, "%x", words[i]);
215 if (len < 0) 211 if (len < 0)
216 return NULL; 212 return NULL;
217 tp += len; 213 tp += len;
218 } 214 }
219 } 215 }
220 /* Was it a trailing run of 0x00's? */ 216 /* Was it a trailing run of 0x00's? */