ape: inet_ntop() print v4 mapped addresses in v6 as dotted quad
This commit is contained in:
parent
cfadfd97b8
commit
4ae7954547
1 changed files with 15 additions and 5 deletions
|
@ -15,16 +15,21 @@
|
|||
char*
|
||||
inet_ntop(int af, void *src, char *dst, int size)
|
||||
{
|
||||
static unsigned char v4prefix[12] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0xff, 0xff, };
|
||||
|
||||
unsigned char *p;
|
||||
char *t, *e;
|
||||
char *t;
|
||||
int i;
|
||||
|
||||
if(af == AF_INET){
|
||||
p = (unsigned char*)&(((struct in_addr*)src)->s_addr);
|
||||
Dot4:
|
||||
if(size < INET_ADDRSTRLEN){
|
||||
errno = ENOSPC;
|
||||
return 0;
|
||||
}
|
||||
p = (unsigned char*)&(((struct in_addr*)src)->s_addr);
|
||||
snprintf(dst, size, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
|
||||
return dst;
|
||||
}
|
||||
|
@ -33,21 +38,26 @@ inet_ntop(int af, void *src, char *dst, int size)
|
|||
errno = EAFNOSUPPORT;
|
||||
return 0;
|
||||
}
|
||||
|
||||
p = (unsigned char*)((struct in6_addr*)src)->s6_addr;
|
||||
if(memcmp(p, v4prefix, 12) == 0){
|
||||
p += 12;
|
||||
goto Dot4;
|
||||
}
|
||||
|
||||
if(size < INET6_ADDRSTRLEN){
|
||||
errno = ENOSPC;
|
||||
return 0;
|
||||
}
|
||||
|
||||
p = (unsigned char*)((struct in6_addr*)src)->s6_addr;
|
||||
t = dst;
|
||||
e = t + size;
|
||||
for(i=0; i<16; i += 2){
|
||||
unsigned int w;
|
||||
|
||||
if(i > 0)
|
||||
*t++ = ':';
|
||||
w = p[i]<<8 | p[i+1];
|
||||
snprintf(t, e - t, "%x", w);
|
||||
sprintf(t, "%x", w);
|
||||
t += strlen(t);
|
||||
}
|
||||
return dst;
|
||||
|
|
Loading…
Reference in a new issue