chiark / gitweb /
volume_id: provide libvolume_id.a file
[elogind.git] / klibc / klibc / inet / inet_ntop.c
1 /*
2  * inet/inet_ntop.c
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <arpa/inet.h>
9 #include <netinet/in6.h>
10
11 const char *inet_ntop(int af, const void *cp, char *buf, size_t len)
12 {
13   size_t xlen;
14
15   switch ( af ) {
16   case AF_INET:
17     {
18       union {
19         uint8_t  b[4];
20         uint32_t l;
21       } a;
22       a.l = ((const struct in_addr *)cp)->s_addr;
23
24       xlen = snprintf(buf, len, "%u.%u.%u.%u", a.b[0], a.b[1], a.b[2], a.b[3]);
25     }
26     break;
27
28   case AF_INET6:
29     {
30       const struct in6_addr *s = (const struct in6_addr *)cp;
31       
32       xlen = snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x",
33                       ntohs(s->s6_addr16[0]), ntohs(s->s6_addr16[1]),
34                       ntohs(s->s6_addr16[2]), ntohs(s->s6_addr16[3]),
35                       ntohs(s->s6_addr16[4]), ntohs(s->s6_addr16[5]),
36                       ntohs(s->s6_addr16[6]), ntohs(s->s6_addr16[7]));
37     }
38     break;
39
40   default:
41     errno = EAFNOSUPPORT;
42     return NULL;
43   }
44
45   if ( xlen > len ) {
46     errno = ENOSPC;
47     return NULL;
48   }
49
50   return buf;
51 }
52