]>
code.delx.au - pulseaudio/blob - src/pulsecore/socket-util.c
673058e2a994638922a94ebbac23f4c74a9bbc56
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
37 #include <sys/types.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
48 #ifdef HAVE_NETINET_IN_H
49 #include <netinet/in.h>
51 #ifdef HAVE_NETINET_IN_SYSTM_H
52 #include <netinet/in_systm.h>
54 #ifdef HAVE_NETINET_IP_H
55 #include <netinet/ip.h>
57 #ifdef HAVE_NETINET_TCP_H
58 #include <netinet/tcp.h>
63 #ifdef HAVE_ARPA_INET_H
64 #include <arpa/inet.h>
67 #ifndef HAVE_INET_NTOP
68 #include "inet_ntop.h"
73 #include <pulse/xmalloc.h>
75 #include <pulsecore/core-error.h>
76 #include <pulsecore/core-util.h>
77 #include <pulsecore/log.h>
79 #include "socket-util.h"
81 void pa_socket_peer_to_string(int fd
, char *c
, size_t l
) {
84 assert(c
&& l
&& fd
>= 0);
87 if (fstat(fd
, &st
) < 0) {
88 snprintf(c
, l
, "Invalid client fd");
94 if (S_ISSOCK(st
.st_mode
)) {
98 struct sockaddr_in in
;
99 struct sockaddr_in6 in6
;
101 struct sockaddr_un un
;
104 socklen_t sa_len
= sizeof(sa
);
106 if (getpeername(fd
, &sa
.sa
, &sa_len
) >= 0) {
108 if (sa
.sa
.sa_family
== AF_INET
) {
109 uint32_t ip
= ntohl(sa
.in
.sin_addr
.s_addr
);
111 snprintf(c
, l
, "TCP/IP client from %i.%i.%i.%i:%u",
116 ntohs(sa
.in
.sin_port
));
118 } else if (sa
.sa
.sa_family
== AF_INET6
) {
119 char buf
[INET6_ADDRSTRLEN
];
122 res
= inet_ntop(AF_INET6
, &sa
.in6
.sin6_addr
, buf
, sizeof(buf
));
124 snprintf(c
, l
, "TCP/IP client from [%s]:%u", buf
, ntohs(sa
.in6
.sin6_port
));
128 } else if (sa
.sa
.sa_family
== AF_UNIX
) {
129 snprintf(c
, l
, "UNIX socket client");
136 snprintf(c
, l
, "Unknown network client");
138 } else if (S_ISCHR(st
.st_mode
) && (fd
== 0 || fd
== 1)) {
139 snprintf(c
, l
, "STDIN/STDOUT client");
142 #endif /* OS_IS_WIN32 */
144 snprintf(c
, l
, "Unknown client");
147 int pa_socket_low_delay(int fd
) {
153 if (setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, (void*)&priority
, sizeof(priority
)) < 0)
160 int pa_socket_tcp_low_delay(int fd
) {
165 ret
= pa_socket_low_delay(fd
);
170 #if defined(SOL_TCP) || defined(IPPROTO_TCP)
172 if (setsockopt(fd
, SOL_TCP
, TCP_NODELAY
, (void*)&on
, sizeof(on
)) < 0)
174 if (setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (void*)&on
, sizeof(on
)) < 0)
179 #if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || \
181 tos
= IPTOS_LOWDELAY
;
183 if (setsockopt(fd
, SOL_IP
, IP_TOS
, (void*)&tos
, sizeof(tos
)) < 0)
185 if (setsockopt(fd
, IPPROTO_IP
, IP_TOS
, (void*)&tos
, sizeof(tos
)) < 0)
194 int pa_socket_set_rcvbuf(int fd
, size_t l
) {
197 /* if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void*)&l, sizeof(l)) < 0) { */
198 /* pa_log("SO_RCVBUF: %s", strerror(errno)); */
205 int pa_socket_set_sndbuf(int fd
, size_t l
) {
208 /* if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void*)&l, sizeof(l)) < 0) { */
209 /* pa_log("SO_SNDBUF: %s", strerror(errno)); */
218 int pa_unix_socket_is_stale(const char *fn
) {
219 struct sockaddr_un sa
;
220 int fd
= -1, ret
= -1;
222 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
223 pa_log("socket(): %s", pa_cstrerror(errno
));
227 sa
.sun_family
= AF_UNIX
;
228 strncpy(sa
.sun_path
, fn
, sizeof(sa
.sun_path
)-1);
229 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
231 if (connect(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
232 if (errno
== ECONNREFUSED
)
244 int pa_unix_socket_remove_stale(const char *fn
) {
247 if ((r
= pa_unix_socket_is_stale(fn
)) < 0)
248 return errno
!= ENOENT
? -1 : 0;
253 /* Yes, here is a race condition. But who cares? */
260 #else /* HAVE_SYS_UN_H */
262 int pa_unix_socket_is_stale(const char *fn
) {
266 int pa_unix_socket_remove_stale(const char *fn
) {
270 #endif /* HAVE_SYS_UN_H */