1#include <arpa/inet.h>
2#include <errno.h>
3#include <netinet/in.h>
4#include <stdio.h>
5#include <string.h>
6
7static int
8do_test (void)
9{
10 struct in_addr addr4;
11 struct in6_addr addr6;
12 char buf[64];
13 int result = 0;
14
15 addr4.s_addr = 0xe0e0e0e0;
16 addr6.s6_addr16[0] = 0;
17 addr6.s6_addr16[1] = 0;
18 addr6.s6_addr16[2] = 0;
19 addr6.s6_addr16[3] = 0;
20 addr6.s6_addr16[4] = 0;
21 addr6.s6_addr16[5] = 0xffff;
22 addr6.s6_addr32[3] = 0xe0e0e0e0;
23 memset (s: buf, c: 'x', n: sizeof buf);
24
25 if (inet_ntop (AF_INET, cp: &addr4, buf: buf, len: 15) != NULL)
26 {
27 puts (s: "1st inet_ntop returned non-NULL");
28 result++;
29 }
30 else if (errno != ENOSPC)
31 {
32 puts (s: "1st inet_ntop didn't fail with ENOSPC");
33 result++;
34 }
35 if (buf[15] != 'x')
36 {
37 puts (s: "1st inet_ntop wrote past the end of buffer");
38 result++;
39 }
40
41 if (inet_ntop (AF_INET, cp: &addr4, buf: buf, len: 16) != buf)
42 {
43 puts (s: "2nd inet_ntop did not return buf");
44 result++;
45 }
46 if (memcmp (s1: buf, s2: "224.224.224.224\0" "xxxxxxxx", n: 24) != 0)
47 {
48 puts (s: "2nd inet_ntop wrote past the end of buffer");
49 result++;
50 }
51
52 if (inet_ntop (AF_INET6, cp: &addr6, buf: buf, len: 22) != NULL)
53 {
54 puts (s: "3rd inet_ntop returned non-NULL");
55 result++;
56 }
57 else if (errno != ENOSPC)
58 {
59 puts (s: "3rd inet_ntop didn't fail with ENOSPC");
60 result++;
61 }
62 if (buf[22] != 'x')
63 {
64 puts (s: "3rd inet_ntop wrote past the end of buffer");
65 result++;
66 }
67
68 if (inet_ntop (AF_INET6, cp: &addr6, buf: buf, len: 23) != buf)
69 {
70 puts (s: "4th inet_ntop did not return buf");
71 result++;
72 }
73 if (memcmp (s1: buf, s2: "::ffff:224.224.224.224\0" "xxxxxxxx", n: 31) != 0)
74 {
75 puts (s: "4th inet_ntop wrote past the end of buffer");
76 result++;
77 }
78
79 memset (s: &addr6.s6_addr, c: 0xe0, n: sizeof (addr6.s6_addr));
80
81 if (inet_ntop (AF_INET6, cp: &addr6, buf: buf, len: 39) != NULL)
82 {
83 puts (s: "5th inet_ntop returned non-NULL");
84 result++;
85 }
86 else if (errno != ENOSPC)
87 {
88 puts (s: "5th inet_ntop didn't fail with ENOSPC");
89 result++;
90 }
91 if (buf[39] != 'x')
92 {
93 puts (s: "5th inet_ntop wrote past the end of buffer");
94 result++;
95 }
96
97 if (inet_ntop (AF_INET6, cp: &addr6, buf: buf, len: 40) != buf)
98 {
99 puts (s: "6th inet_ntop did not return buf");
100 result++;
101 }
102 if (memcmp (s1: buf, s2: "e0e0:e0e0:e0e0:e0e0:e0e0:e0e0:e0e0:e0e0\0"
103 "xxxxxxxx", n: 48) != 0)
104 {
105 puts (s: "6th inet_ntop wrote past the end of buffer");
106 result++;
107 }
108
109
110 return result;
111}
112
113#define TEST_FUNCTION do_test ()
114#include "../test-skeleton.c"
115

source code of glibc/resolv/tst-inet_ntop.c