]>
git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/inet_ntop.c
3 #include <netinet/in.h>
8 #include <sys/socket.h>
14 #define MAX_V6_ADDR_LEN 64
16 static const char *hexchars
= "0123456789abcdef";
19 inet_ntop6(const struct in6_addr
*addr
, char *dst
, size_t size
)
21 char hexa
[8][5], tmp
[MAX_V6_ADDR_LEN
];
24 int32_t i
, j
, k
, skip
;
29 if (addr
== NULL
) return NULL
;
31 memset(tmp
, 0, MAX_V6_ADDR_LEN
);
33 /* check for mapped or compat addresses */
34 i
= IN6_IS_ADDR_V4MAPPED(addr
);
35 j
= IN6_IS_ADDR_V4COMPAT(addr
);
36 if ((i
!= 0) || (j
!= 0))
38 a4
.s_addr
= addr
->__u6_addr
.__u6_addr32
[3];
39 sprintf(tmp
, "::%s%s", (i
!= 0) ? "ffff:" : "", inet_ntoa(a4
));
40 len
= strlen(tmp
) + 1;
41 if (len
> size
) return NULL
;
42 memcpy(dst
, tmp
, len
);
47 for (i
= 0; i
< 16; i
+= 2)
52 memset(hexa
[k
], 0, 5);
54 x8
= addr
->__u6_addr
.__u6_addr8
[i
];
60 hexa
[k
][j
++] = hexchars
[hx8
];
64 if ((skip
== 0) || ((skip
== 1) && (hx8
!= 0)))
67 hexa
[k
][j
++] = hexchars
[hx8
];
70 x8
= addr
->__u6_addr
.__u6_addr8
[i
+ 1];
73 if ((skip
== 0) || ((skip
== 1) && (hx8
!= 0)))
75 hexa
[k
][j
++] = hexchars
[hx8
];
79 hexa
[k
][j
++] = hexchars
[hx8
];
84 /* find runs of zeros for :: convention */
86 for (i
= 7; i
>= 0; i
--)
89 x16
= addr
->__u6_addr
.__u6_addr16
[i
];
95 /* find longest run of zeros */
98 for(i
= 0; i
< 8; i
++)
107 for(i
= 0; i
< 8; i
++)
109 if (i
!= k
) zr
[i
] = 0;
113 for (i
= 0; i
< 8; i
++)
117 /* check for leading zero */
118 if (i
== 0) tmp
[len
++] = ':';
123 for (j
= 0; hexa
[i
][j
] != '\0'; j
++) tmp
[len
++] = hexa
[i
][j
];
124 if (i
!= 7) tmp
[len
++] = ':';
130 if (len
> size
) return NULL
;
131 memcpy(dst
, tmp
, len
);
136 inet_ntop4(const struct in_addr
*addr
, char *buf
, size_t len
)
138 const u_int8_t
*ap
= (u_int8_t
*)&addr
->s_addr
;
142 for (i
= 0; i
< 4; i
++, ap
++)
144 if (bp
>= buf
+ len
- 1)
150 sprintf(bp
, "%d", *ap
);
152 if (i
!= 3) *bp
++='.';
160 inet_ntop(int af
, const void *addr
, char *buf
, size_t len
)
163 return inet_ntop6(addr
, buf
, len
);
165 return inet_ntop4(addr
, buf
, len
);