]>
code.delx.au - pulseaudio/blob - src/pulsecore/socket-util.c
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2004 Joe Marcus Clarke
8 Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
10 PulseAudio is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published
12 by the Free Software Foundation; either version 2 of the License,
13 or (at your option) any later version.
15 PulseAudio is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with PulseAudio; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 #include <sys/types.h>
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
47 #ifdef HAVE_NETINET_IN_H
48 #include <netinet/in.h>
50 #ifdef HAVE_NETINET_IN_SYSTM_H
51 #include <netinet/in_systm.h>
53 #ifdef HAVE_NETINET_IP_H
54 #include <netinet/ip.h>
56 #ifdef HAVE_NETINET_TCP_H
57 #include <netinet/tcp.h>
62 #ifdef HAVE_ARPA_INET_H
63 #include <arpa/inet.h>
66 #ifndef HAVE_INET_NTOP
67 #include "inet_ntop.h"
72 #include <pulse/xmalloc.h>
74 #include <pulsecore/core-error.h>
75 #include <pulsecore/core-util.h>
76 #include <pulsecore/log.h>
77 #include <pulsecore/macro.h>
79 #include "socket-util.h"
81 void pa_socket_peer_to_string(int fd
, char *c
, size_t l
) {
89 pa_assert_se(fstat(fd
, &st
) == 0);
93 if (S_ISSOCK(st
.st_mode
)) {
97 struct sockaddr_in in
;
98 struct sockaddr_in6 in6
;
100 struct sockaddr_un un
;
103 socklen_t sa_len
= sizeof(sa
);
105 if (getpeername(fd
, &sa
.sa
, &sa_len
) >= 0) {
107 if (sa
.sa
.sa_family
== AF_INET
) {
108 uint32_t ip
= ntohl(sa
.in
.sin_addr
.s_addr
);
110 pa_snprintf(c
, l
, "TCP/IP client from %i.%i.%i.%i:%u",
115 ntohs(sa
.in
.sin_port
));
117 } else if (sa
.sa
.sa_family
== AF_INET6
) {
118 char buf
[INET6_ADDRSTRLEN
];
121 res
= inet_ntop(AF_INET6
, &sa
.in6
.sin6_addr
, buf
, sizeof(buf
));
123 pa_snprintf(c
, l
, "TCP/IP client from [%s]:%u", buf
, ntohs(sa
.in6
.sin6_port
));
127 } else if (sa
.sa
.sa_family
== AF_UNIX
) {
128 pa_snprintf(c
, l
, "UNIX socket client");
135 pa_snprintf(c
, l
, "Unknown network client");
137 } else if (S_ISCHR(st
.st_mode
) && (fd
== 0 || fd
== 1)) {
138 pa_snprintf(c
, l
, "STDIN/STDOUT client");
141 #endif /* OS_IS_WIN32 */
143 pa_snprintf(c
, l
, "Unknown client");
146 void pa_make_socket_low_delay(int fd
) {
153 if (setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, (void*)&priority
, sizeof(priority
)) < 0)
154 pa_log_warn("SO_PRIORITY failed: %s", pa_cstrerror(errno
));
158 void pa_make_tcp_socket_low_delay(int fd
) {
161 pa_make_socket_low_delay(fd
);
163 #if defined(SOL_TCP) || defined(IPPROTO_TCP)
167 if (setsockopt(fd
, SOL_TCP
, TCP_NODELAY
, (void*)&on
, sizeof(on
)) < 0)
169 if (setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (void*)&on
, sizeof(on
)) < 0)
171 pa_log_warn("TCP_NODELAY failed: %s", pa_cstrerror(errno
));
175 #if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || defined(IPPROTO_IP))
177 int tos
= IPTOS_LOWDELAY
;
179 if (setsockopt(fd
, SOL_IP
, IP_TOS
, (void*)&tos
, sizeof(tos
)) < 0)
181 if (setsockopt(fd
, IPPROTO_IP
, IP_TOS
, (void*)&tos
, sizeof(tos
)) < 0)
183 pa_log_warn("IP_TOS failed: %s", pa_cstrerror(errno
));
188 void pa_make_udp_socket_low_delay(int fd
) {
191 pa_make_socket_low_delay(fd
);
193 #if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || defined(IPPROTO_IP))
195 int tos
= IPTOS_LOWDELAY
;
197 if (setsockopt(fd
, SOL_IP
, IP_TOS
, (void*)&tos
, sizeof(tos
)) < 0)
199 if (setsockopt(fd
, IPPROTO_IP
, IP_TOS
, (void*)&tos
, sizeof(tos
)) < 0)
201 pa_log_warn("IP_TOS failed: %s", pa_cstrerror(errno
));
206 int pa_socket_set_rcvbuf(int fd
, size_t l
) {
209 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, (void*)&l
, sizeof(l
)) < 0) {
210 pa_log_warn("SO_RCVBUF: %s", pa_cstrerror(errno
));
217 int pa_socket_set_sndbuf(int fd
, size_t l
) {
220 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, (void*)&l
, sizeof(l
)) < 0) {
221 pa_log("SO_SNDBUF: %s", pa_cstrerror(errno
));
230 int pa_unix_socket_is_stale(const char *fn
) {
231 struct sockaddr_un sa
;
232 int fd
= -1, ret
= -1;
236 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
237 pa_log("socket(): %s", pa_cstrerror(errno
));
241 sa
.sun_family
= AF_UNIX
;
242 strncpy(sa
.sun_path
, fn
, sizeof(sa
.sun_path
)-1);
243 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
245 if (connect(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
246 if (errno
== ECONNREFUSED
)
258 int pa_unix_socket_remove_stale(const char *fn
) {
263 if ((r
= pa_unix_socket_is_stale(fn
)) < 0)
264 return errno
!= ENOENT
? -1 : 0;
269 /* Yes, here is a race condition. But who cares? */
276 #else /* HAVE_SYS_UN_H */
278 int pa_unix_socket_is_stale(const char *fn
) {
282 int pa_unix_socket_remove_stale(const char *fn
) {
286 #endif /* HAVE_SYS_UN_H */