blob: 0f9c9e2b085a7ad011df762277afc3dea8e7ae35 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
/* posix */
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* bsd extensions */
#include <sys/uio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
char*
inet_ntop(int af, void *src, char *dst, int size)
{
unsigned char *p;
char *t, *e;
int i;
if(af == AF_INET){
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;
}
if(af != AF_INET6){
errno = EAFNOSUPPORT;
return 0;
}
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);
t += strlen(t);
}
return dst;
}
|