]> code.delx.au - pulseaudio/blob - src/pulsecore/socket-util.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / socket-util.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2004 Joe Marcus Clarke
8 Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
9
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.
14
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.
19
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
23 USA.
24 ***/
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <signal.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <sys/stat.h>
40
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
43 #endif
44 #ifdef HAVE_SYS_UN_H
45 #include <sys/un.h>
46 #endif
47 #ifdef HAVE_NETINET_IN_H
48 #include <netinet/in.h>
49 #endif
50 #ifdef HAVE_NETINET_IN_SYSTM_H
51 #include <netinet/in_systm.h>
52 #endif
53 #ifdef HAVE_NETINET_IP_H
54 #include <netinet/ip.h>
55 #endif
56 #ifdef HAVE_NETINET_TCP_H
57 #include <netinet/tcp.h>
58 #endif
59 #ifdef HAVE_NETDB_H
60 #include <netdb.h>
61 #endif
62 #ifdef HAVE_ARPA_INET_H
63 #include <arpa/inet.h>
64 #endif
65
66 #ifndef HAVE_INET_NTOP
67 #include "inet_ntop.h"
68 #endif
69
70 #include "winsock.h"
71
72 #include <pulse/xmalloc.h>
73
74 #include <pulsecore/core-error.h>
75 #include <pulsecore/core-util.h>
76 #include <pulsecore/log.h>
77 #include <pulsecore/macro.h>
78
79 #include "socket-util.h"
80
81 void pa_socket_peer_to_string(int fd, char *c, size_t l) {
82 struct stat st;
83
84 pa_assert(fd >= 0);
85 pa_assert(c);
86 pa_assert(l > 0);
87
88 #ifndef OS_IS_WIN32
89 pa_assert_se(fstat(fd, &st) == 0);
90 #endif
91
92 #ifndef OS_IS_WIN32
93 if (S_ISSOCK(st.st_mode)) {
94 #endif
95 union {
96 struct sockaddr sa;
97 struct sockaddr_in in;
98 struct sockaddr_in6 in6;
99 #ifdef HAVE_SYS_UN_H
100 struct sockaddr_un un;
101 #endif
102 } sa;
103 socklen_t sa_len = sizeof(sa);
104
105 if (getpeername(fd, &sa.sa, &sa_len) >= 0) {
106
107 if (sa.sa.sa_family == AF_INET) {
108 uint32_t ip = ntohl(sa.in.sin_addr.s_addr);
109
110 pa_snprintf(c, l, "TCP/IP client from %i.%i.%i.%i:%u",
111 ip >> 24,
112 (ip >> 16) & 0xFF,
113 (ip >> 8) & 0xFF,
114 ip & 0xFF,
115 ntohs(sa.in.sin_port));
116 return;
117 } else if (sa.sa.sa_family == AF_INET6) {
118 char buf[INET6_ADDRSTRLEN];
119 const char *res;
120
121 res = inet_ntop(AF_INET6, &sa.in6.sin6_addr, buf, sizeof(buf));
122 if (res) {
123 pa_snprintf(c, l, "TCP/IP client from [%s]:%u", buf, ntohs(sa.in6.sin6_port));
124 return;
125 }
126 #ifdef HAVE_SYS_UN_H
127 } else if (sa.sa.sa_family == AF_UNIX) {
128 pa_snprintf(c, l, "UNIX socket client");
129 return;
130 #endif
131 }
132
133 }
134 #ifndef OS_IS_WIN32
135 pa_snprintf(c, l, "Unknown network client");
136 return;
137 } else if (S_ISCHR(st.st_mode) && (fd == 0 || fd == 1)) {
138 pa_snprintf(c, l, "STDIN/STDOUT client");
139 return;
140 }
141 #endif /* OS_IS_WIN32 */
142
143 pa_snprintf(c, l, "Unknown client");
144 }
145
146 void pa_make_socket_low_delay(int fd) {
147
148 #ifdef SO_PRIORITY
149 int priority;
150 pa_assert(fd >= 0);
151
152 priority = 6;
153 if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, (void*)&priority, sizeof(priority)) < 0)
154 pa_log_warn("SO_PRIORITY failed: %s", pa_cstrerror(errno));
155 #endif
156 }
157
158 void pa_make_tcp_socket_low_delay(int fd) {
159 pa_assert(fd >= 0);
160
161 pa_make_socket_low_delay(fd);
162
163 #if defined(SOL_TCP) || defined(IPPROTO_TCP)
164 {
165 int on = 1;
166 #if defined(SOL_TCP)
167 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (void*)&on, sizeof(on)) < 0)
168 #else
169 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on)) < 0)
170 #endif
171 pa_log_warn("TCP_NODELAY failed: %s", pa_cstrerror(errno));
172 }
173 #endif
174
175 #if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || defined(IPPROTO_IP))
176 {
177 int tos = IPTOS_LOWDELAY;
178 #ifdef SOL_IP
179 if (setsockopt(fd, SOL_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
180 #else
181 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
182 #endif
183 pa_log_warn("IP_TOS failed: %s", pa_cstrerror(errno));
184 }
185 #endif
186 }
187
188 void pa_make_udp_socket_low_delay(int fd) {
189 pa_assert(fd >= 0);
190
191 pa_make_socket_low_delay(fd);
192
193 #if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || defined(IPPROTO_IP))
194 {
195 int tos = IPTOS_LOWDELAY;
196 #ifdef SOL_IP
197 if (setsockopt(fd, SOL_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
198 #else
199 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
200 #endif
201 pa_log_warn("IP_TOS failed: %s", pa_cstrerror(errno));
202 }
203 #endif
204 }
205
206 int pa_socket_set_rcvbuf(int fd, size_t l) {
207 pa_assert(fd >= 0);
208
209 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void*)&l, sizeof(l)) < 0) {
210 pa_log_warn("SO_RCVBUF: %s", pa_cstrerror(errno));
211 return -1;
212 }
213
214 return 0;
215 }
216
217 int pa_socket_set_sndbuf(int fd, size_t l) {
218 pa_assert(fd >= 0);
219
220 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void*)&l, sizeof(l)) < 0) {
221 pa_log("SO_SNDBUF: %s", pa_cstrerror(errno));
222 return -1;
223 }
224
225 return 0;
226 }
227
228 #ifdef HAVE_SYS_UN_H
229
230 int pa_unix_socket_is_stale(const char *fn) {
231 struct sockaddr_un sa;
232 int fd = -1, ret = -1;
233
234 pa_assert(fn);
235
236 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
237 pa_log("socket(): %s", pa_cstrerror(errno));
238 goto finish;
239 }
240
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;
244
245 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
246 if (errno == ECONNREFUSED)
247 ret = 1;
248 } else
249 ret = 0;
250
251 finish:
252 if (fd >= 0)
253 pa_close(fd);
254
255 return ret;
256 }
257
258 int pa_unix_socket_remove_stale(const char *fn) {
259 int r;
260
261 pa_assert(fn);
262
263 if ((r = pa_unix_socket_is_stale(fn)) < 0)
264 return errno != ENOENT ? -1 : 0;
265
266 if (!r)
267 return 0;
268
269 /* Yes, here is a race condition. But who cares? */
270 if (unlink(fn) < 0)
271 return -1;
272
273 return 0;
274 }
275
276 #else /* HAVE_SYS_UN_H */
277
278 int pa_unix_socket_is_stale(const char *fn) {
279 return -1;
280 }
281
282 int pa_unix_socket_remove_stale(const char *fn) {
283 return -1;
284 }
285
286 #endif /* HAVE_SYS_UN_H */