OLD | NEW |
1 #include <sys/socket.h> | 1 #include <sys/socket.h> |
2 #include <arpa/inet.h> | 2 #include <arpa/inet.h> |
3 #include <errno.h> | 3 #include <errno.h> |
4 #include <stdio.h> | 4 #include <stdio.h> |
5 #include <string.h> | 5 #include <string.h> |
6 | 6 |
7 const char *inet_ntop(int af, const void *restrict a0, char *restrict s, socklen
_t l) | 7 const char* inet_ntop(int af, |
8 { | 8 const void* restrict a0, |
9 » const unsigned char *a = a0; | 9 char* restrict s, |
10 » int i, j, max, best; | 10 socklen_t l) { |
11 » char buf[100]; | 11 const unsigned char* a = a0; |
| 12 int i, j, max, best; |
| 13 char buf[100]; |
12 | 14 |
13 » switch (af) { | 15 switch (af) { |
14 » case AF_INET: | 16 case AF_INET: |
15 » » if (snprintf(s, l, "%d.%d.%d.%d", a[0],a[1],a[2],a[3]) < l) | 17 if (snprintf(s, l, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]) < l) |
16 » » » return s; | 18 return s; |
17 » » break; | 19 break; |
18 » case AF_INET6: | 20 case AF_INET6: |
19 » » if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\377\377", 12)) | 21 if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\377\377", 12)) |
20 » » » snprintf(buf, sizeof buf, | 22 snprintf(buf, sizeof buf, "%x:%x:%x:%x:%x:%x:%x:%x", 256 * a[0] + a[1], |
21 » » » » "%x:%x:%x:%x:%x:%x:%x:%x", | 23 256 * a[2] + a[3], 256 * a[4] + a[5], 256 * a[6] + a[7], |
22 » » » » 256*a[0]+a[1],256*a[2]+a[3], | 24 256 * a[8] + a[9], 256 * a[10] + a[11], 256 * a[12] + a[13], |
23 » » » » 256*a[4]+a[5],256*a[6]+a[7], | 25 256 * a[14] + a[15]); |
24 » » » » 256*a[8]+a[9],256*a[10]+a[11], | 26 else |
25 » » » » 256*a[12]+a[13],256*a[14]+a[15]); | 27 snprintf(buf, sizeof buf, "%x:%x:%x:%x:%x:%x:%d.%d.%d.%d", |
26 » » else | 28 256 * a[0] + a[1], 256 * a[2] + a[3], 256 * a[4] + a[5], |
27 » » » snprintf(buf, sizeof buf, | 29 256 * a[6] + a[7], 256 * a[8] + a[9], 256 * a[10] + a[11], |
28 » » » » "%x:%x:%x:%x:%x:%x:%d.%d.%d.%d", | 30 a[12], a[13], a[14], a[15]); |
29 » » » » 256*a[0]+a[1],256*a[2]+a[3], | 31 /* Replace longest /(^0|:)[:0]{2,}/ with "::" */ |
30 » » » » 256*a[4]+a[5],256*a[6]+a[7], | 32 for (i = best = 0, max = 2; buf[i]; i++) { |
31 » » » » 256*a[8]+a[9],256*a[10]+a[11], | 33 if (i && buf[i] != ':') |
32 » » » » a[12],a[13],a[14],a[15]); | 34 continue; |
33 » » /* Replace longest /(^0|:)[:0]{2,}/ with "::" */ | 35 j = strspn(buf + i, ":0"); |
34 » » for (i=best=0, max=2; buf[i]; i++) { | 36 if (j > max) |
35 » » » if (i && buf[i] != ':') continue; | 37 best = i, max = j; |
36 » » » j = strspn(buf+i, ":0"); | 38 } |
37 » » » if (j>max) best=i, max=j; | 39 if (max > 2) { |
38 » » } | 40 buf[best] = buf[best + 1] = ':'; |
39 » » if (max>2) { | 41 memmove(buf + best + 2, buf + best + max, i - best - max + 1); |
40 » » » buf[best] = buf[best+1] = ':'; | 42 } |
41 » » » memmove(buf+best+2, buf+best+max, i-best-max+1); | 43 if (strlen(buf) < l) { |
42 » » } | 44 strcpy(s, buf); |
43 » » if (strlen(buf) < l) { | 45 return s; |
44 » » » strcpy(s, buf); | 46 } |
45 » » » return s; | 47 break; |
46 » » } | 48 default: |
47 » » break; | 49 errno = EAFNOSUPPORT; |
48 » default: | 50 return 0; |
49 » » errno = EAFNOSUPPORT; | 51 } |
50 » » return 0; | 52 errno = ENOSPC; |
51 » } | 53 return 0; |
52 » errno = ENOSPC; | |
53 » return 0; | |
54 } | 54 } |
OLD | NEW |