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