]> code.delx.au - pulseaudio/blob - src/pulsecore/socket-util.c
add new pa_socket_udp_low_delay() API
[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 int pa_socket_low_delay(int fd) {
147
148 #ifdef SO_PRIORITY
149 int priority;
150 pa_assert(fd >= 0);
151
152 priority = 7;
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 return -1;
156 }
157 #endif
158
159 return 0;
160 }
161
162 int pa_socket_tcp_low_delay(int fd) {
163 int ret, tos, on;
164
165 pa_assert(fd >= 0);
166
167 ret = pa_socket_low_delay(fd);
168
169 on = 1;
170 tos = 0;
171
172 #if defined(SOL_TCP) || defined(IPPROTO_TCP)
173 #if defined(SOL_TCP)
174 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (void*)&on, sizeof(on)) < 0)
175 #else
176 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on)) < 0)
177 #endif
178 {
179 pa_log_warn("TCP_NODELAY failed: %s", pa_cstrerror(errno));
180 ret = -1;
181 }
182 #endif
183
184 #if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || defined(IPPROTO_IP))
185 tos = IPTOS_LOWDELAY;
186 #ifdef SOL_IP
187 if (setsockopt(fd, SOL_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
188 #else
189 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
190 #endif
191 {
192 pa_log_warn("IP_TOS failed: %s", pa_cstrerror(errno));
193 ret = -1;
194 }
195 #endif
196
197 return ret;
198 }
199
200 int pa_socket_udp_low_delay(int fd) {
201 int ret, tos;
202
203 pa_assert(fd >= 0);
204
205 ret = pa_socket_low_delay(fd);
206
207 tos = 0;
208
209 #if defined(IPTOS_LOWDELAY) && defined(IP_TOS) && (defined(SOL_IP) || defined(IPPROTO_IP))
210 tos = IPTOS_LOWDELAY;
211 #ifdef SOL_IP
212 if (setsockopt(fd, SOL_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
213 #else
214 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
215 #endif
216 {
217 ret = -1;
218 pa_log_warn("IP_TOS failed: %s", pa_cstrerror(errno));
219 }
220 #endif
221
222 return ret;
223 }
224
225 int pa_socket_set_rcvbuf(int fd, size_t l) {
226 pa_assert(fd >= 0);
227
228 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void*)&l, sizeof(l)) < 0) {
229 pa_log_warn("SO_RCVBUF: %s", pa_cstrerror(errno));
230 return -1;
231 }
232
233 return 0;
234 }
235
236 int pa_socket_set_sndbuf(int fd, size_t l) {
237 pa_assert(fd >= 0);
238
239 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void*)&l, sizeof(l)) < 0) {
240 pa_log("SO_SNDBUF: %s", pa_cstrerror(errno));
241 return -1;
242 }
243
244 return 0;
245 }
246
247 #ifdef HAVE_SYS_UN_H
248
249 int pa_unix_socket_is_stale(const char *fn) {
250 struct sockaddr_un sa;
251 int fd = -1, ret = -1;
252
253 pa_assert(fn);
254
255 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
256 pa_log("socket(): %s", pa_cstrerror(errno));
257 goto finish;
258 }
259
260 sa.sun_family = AF_UNIX;
261 strncpy(sa.sun_path, fn, sizeof(sa.sun_path)-1);
262 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
263
264 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
265 if (errno == ECONNREFUSED)
266 ret = 1;
267 } else
268 ret = 0;
269
270 finish:
271 if (fd >= 0)
272 pa_close(fd);
273
274 return ret;
275 }
276
277 int pa_unix_socket_remove_stale(const char *fn) {
278 int r;
279
280 pa_assert(fn);
281
282 if ((r = pa_unix_socket_is_stale(fn)) < 0)
283 return errno != ENOENT ? -1 : 0;
284
285 if (!r)
286 return 0;
287
288 /* Yes, here is a race condition. But who cares? */
289 if (unlink(fn) < 0)
290 return -1;
291
292 return 0;
293 }
294
295 #else /* HAVE_SYS_UN_H */
296
297 int pa_unix_socket_is_stale(const char *fn) {
298 return -1;
299 }
300
301 int pa_unix_socket_remove_stale(const char *fn) {
302 return -1;
303 }
304
305 #endif /* HAVE_SYS_UN_H */