]> git.lizzy.rs Git - plan9front.git/blob - sys/src/ape/lib/bsd/inet_ntop.c
audiohda: fix syntax error
[plan9front.git] / sys / src / ape / lib / bsd / inet_ntop.c
1 /* posix */
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 /* bsd extensions */
9 #include <sys/uio.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12
13 #include <errno.h>
14
15 char*
16 inet_ntop(int af, void *src, char *dst, int size)
17 {
18         static unsigned char v4prefix[12] = {
19                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20                 0x00, 0x00, 0xff, 0xff, };
21
22         unsigned char *p;
23         char *t;
24         int i;
25
26         if(af == AF_INET){
27                 p = (unsigned char*)&(((struct in_addr*)src)->s_addr);
28 Dot4:
29                 if(size < INET_ADDRSTRLEN){
30                         errno = ENOSPC;
31                         return 0;
32                 }
33                 snprintf(dst, size, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
34                 return dst;
35         }
36
37         if(af != AF_INET6){
38                 errno = EAFNOSUPPORT;
39                 return 0;
40         }
41
42         p = (unsigned char*)((struct in6_addr*)src)->s6_addr;
43         if(memcmp(p, v4prefix, 12) == 0){
44                 p += 12;
45                 goto Dot4;
46         }
47
48         if(size < INET6_ADDRSTRLEN){
49                 errno = ENOSPC;
50                 return 0;
51         }
52
53         t = dst;
54         for(i=0; i<16; i += 2){
55                 unsigned int w;
56
57                 if(i > 0)
58                         *t++ = ':';
59                 w = p[i]<<8 | p[i+1];
60                 sprintf(t, "%x", w);
61                 t += strlen(t);
62         }
63         return dst;
64 }