2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/types.h>
28 #include <sys/types.h>
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
37 #ifdef HAVE_NETINET_IN_SYSTM_H
38 #include <netinet/in_systm.h>
40 #ifdef HAVE_NETINET_IP_H
41 #include <netinet/ip.h>
43 #ifdef HAVE_ARPA_INET_H
44 #include <arpa/inet.h>
47 #include <pulse/xmalloc.h>
49 #include <pulsecore/core-util.h>
50 #include <pulsecore/llist.h>
51 #include <pulsecore/log.h>
52 #include <pulsecore/macro.h>
53 #include <pulsecore/winsock.h>
55 #ifndef HAVE_INET_PTON
56 #include "inet_pton.h"
62 PA_LLIST_FIELDS(struct acl_entry
);
64 struct in_addr address_ipv4
;
66 struct in6_addr address_ipv6
;
72 PA_LLIST_HEAD(struct acl_entry
, entries
);
75 pa_ip_acl
* pa_ip_acl_new(const char *s
) {
76 const char *state
= NULL
;
82 acl
= pa_xnew(pa_ip_acl
, 1);
83 PA_LLIST_HEAD_INIT(struct acl_entry
, acl
->entries
);
85 while ((a
= pa_split(s
, ";", &state
))) {
87 struct acl_entry e
, *n
;
90 if ((slash
= strchr(a
, '/'))) {
93 if (pa_atou(slash
, &bits
) < 0) {
94 pa_log_warn("Failed to parse number of bits: %s", slash
);
100 if (inet_pton(AF_INET
, a
, &e
.address_ipv4
) > 0) {
102 e
.bits
= bits
== (uint32_t) -1 ? 32 : (int) bits
;
105 pa_log_warn("Number of bits out of range: %i", e
.bits
);
111 if (e
.bits
< 32 && (uint32_t) (ntohl(e
.address_ipv4
.s_addr
) << e
.bits
) != 0)
112 pa_log_warn("Host part of ACL entry '%s/%u' is not zero!", a
, e
.bits
);
115 } else if (inet_pton(AF_INET6
, a
, &e
.address_ipv6
) > 0) {
117 e
.bits
= bits
== (uint32_t) -1 ? 128 : (int) bits
;
120 pa_log_warn("Number of bits out of range: %i", e
.bits
);
128 for (i
= 0, bits
= (uint32_t) e
.bits
; i
< 16; i
++) {
133 if ((uint8_t) ((e
.address_ipv6
.s6_addr
[i
]) << bits
) != 0) {
142 pa_log_warn("Host part of ACL entry '%s/%u' is not zero!", a
, e
.bits
);
147 pa_log_warn("Failed to parse address: %s", a
);
151 n
= pa_xmemdup(&e
, sizeof(struct acl_entry
));
152 PA_LLIST_PREPEND(struct acl_entry
, acl
->entries
, n
);
166 void pa_ip_acl_free(pa_ip_acl
*acl
) {
169 while (acl
->entries
) {
170 struct acl_entry
*e
= acl
->entries
;
171 PA_LLIST_REMOVE(struct acl_entry
, acl
->entries
, e
);
178 int pa_ip_acl_check(pa_ip_acl
*acl
, int fd
) {
179 struct sockaddr_storage sa
;
187 if (getpeername(fd
, (struct sockaddr
*) &sa
, &salen
) < 0)
191 if (sa
.ss_family
!= AF_INET
&& sa
.ss_family
!= AF_INET6
)
193 if (sa
.ss_family
!= AF_INET
)
197 if (sa
.ss_family
== AF_INET
&& salen
!= sizeof(struct sockaddr_in
))
201 if (sa
.ss_family
== AF_INET6
&& salen
!= sizeof(struct sockaddr_in6
))
205 for (e
= acl
->entries
; e
; e
= e
->next
) {
207 if (e
->family
!= sa
.ss_family
)
210 if (e
->family
== AF_INET
) {
211 struct sockaddr_in
*sai
= (struct sockaddr_in
*) &sa
;
213 if (e
->bits
== 0 || /* this needs special handling because >> takes the right-hand side modulo 32 */
214 (ntohl(sai
->sin_addr
.s_addr
^ e
->address_ipv4
.s_addr
) >> (32 - e
->bits
)) == 0)
217 } else if (e
->family
== AF_INET6
) {
219 struct sockaddr_in6
*sai
= (struct sockaddr_in6
*) &sa
;
222 return memcmp(&sai
->sin6_addr
, &e
->address_ipv6
, 16) == 0;
227 for (i
= 0, bits
= e
->bits
; i
< 16; i
++) {
230 if (sai
->sin6_addr
.s6_addr
[i
] != e
->address_ipv6
.s6_addr
[i
])
235 if ((sai
->sin6_addr
.s6_addr
[i
] ^ e
->address_ipv6
.s6_addr
[i
]) >> (8 - bits
) != 0)