Branch data Line data Source code
1 : : #ifdef HAVE_CONFIG_H
2 : : #include <config.h>
3 : : #endif
4 : :
5 : : #include <sys/types.h>
6 : : #include <stdio.h>
7 : : #include <unistd.h>
8 : : #include <string.h>
9 : :
10 : : #ifdef HAVE_NETINET_IN_H
11 : : #include <netinet/in.h>
12 : : #endif
13 : : #ifdef HAVE_NETINET_IN_SYSTM_H
14 : : #include <netinet/in_systm.h>
15 : : #endif
16 : : #ifdef HAVE_NETINET_IP_H
17 : : #include <netinet/ip.h>
18 : : #endif
19 : :
20 : : #include <pulsecore/log.h>
21 : : #include <pulsecore/macro.h>
22 : : #include <pulsecore/socket.h>
23 : : #include <pulsecore/ipacl.h>
24 : : #include <pulsecore/arpa-inet.h>
25 : :
26 : 14 : static void do_ip_acl_check(const char *s, int fd, int expected) {
27 : : pa_ip_acl *acl;
28 : : int result;
29 : :
30 [ - + ]: 14 : pa_assert_se(acl = pa_ip_acl_new(s));
31 : 14 : result = pa_ip_acl_check(acl, fd);
32 : 14 : pa_ip_acl_free(acl);
33 : :
34 : 14 : pa_log_info("%-20s result=%u (should be %u)", s, result, expected);
35 [ - + ]: 14 : pa_assert(result == expected);
36 : 14 : }
37 : :
38 : 1 : int main(int argc, char *argv[]) {
39 : : struct sockaddr_in sa;
40 : : #ifdef HAVE_IPV6
41 : : struct sockaddr_in6 sa6;
42 : : #endif
43 : : int fd;
44 : : int r;
45 : :
46 [ - + ]: 1 : if (!getenv("MAKE_CHECK"))
47 : 0 : pa_log_set_level(PA_LOG_DEBUG);
48 : :
49 : 1 : fd = socket(PF_INET, SOCK_STREAM, 0);
50 [ - + ]: 1 : pa_assert(fd >= 0);
51 : :
52 : 1 : sa.sin_family = AF_INET;
53 : 1 : sa.sin_port = htons(22);
54 : 1 : sa.sin_addr.s_addr = inet_addr("127.0.0.1");
55 : :
56 : 1 : r = connect(fd, (struct sockaddr*) &sa, sizeof(sa));
57 [ - + ]: 1 : pa_assert(r >= 0);
58 : :
59 : 1 : do_ip_acl_check("127.0.0.1", fd, 1);
60 : 1 : do_ip_acl_check("127.0.0.2/0", fd, 1);
61 : 1 : do_ip_acl_check("127.0.0.1/32", fd, 1);
62 : 1 : do_ip_acl_check("127.0.0.1/7", fd, 1);
63 : 1 : do_ip_acl_check("127.0.0.2", fd, 0);
64 : 1 : do_ip_acl_check("127.0.0.0/8;0.0.0.0/32", fd, 1);
65 : 1 : do_ip_acl_check("128.0.0.2/9", fd, 0);
66 : 1 : do_ip_acl_check("::1/9", fd, 0);
67 : :
68 : 1 : close(fd);
69 : :
70 : : #ifdef HAVE_IPV6
71 [ - + ]: 1 : if ( (fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0 ) {
72 : 0 : pa_log_error("Unable to open IPv6 socket, IPv6 tests ignored");
73 : 0 : return 0;
74 : : }
75 : :
76 : : memset(&sa6, 0, sizeof(sa6));
77 : 1 : sa6.sin6_family = AF_INET6;
78 : 1 : sa6.sin6_port = htons(22);
79 [ - + ]: 1 : pa_assert_se(inet_pton(AF_INET6, "::1", &sa6.sin6_addr) == 1);
80 : :
81 : 1 : r = connect(fd, (struct sockaddr*) &sa6, sizeof(sa6));
82 [ - + ]: 1 : pa_assert(r >= 0);
83 : :
84 : 1 : do_ip_acl_check("::1", fd, 1);
85 : 1 : do_ip_acl_check("::1/9", fd, 1);
86 : 1 : do_ip_acl_check("::/0", fd, 1);
87 : 1 : do_ip_acl_check("::2/128", fd, 0);
88 : 1 : do_ip_acl_check("::2/127", fd, 0);
89 : 1 : do_ip_acl_check("::2/126", fd, 1);
90 : :
91 : 1 : close(fd);
92 : : #endif
93 : :
94 : 1 : return 0;
95 : : }
|